I have the following C file -
#include <stdio.h>
#include <stdint.h>
extern void matmul(int64_t M, int64_t N, int64_t K, float *A, float *B, float *C);
void print_matrix(float *matrix, int64_t rows, int64_t cols) {
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
printf("%f ", matrix[i * cols + j]);
}
printf("\n");
}
}
int main() {
int64_t M = 2, N = 2, K = 2;
float A[] = {1, 2, 3, 4};
float B[] = {5, 6, 7, 8};
float C[4] = {0};
printf("Before matmul call\n");
matmul(M, N, K, A, B, C);
printf("Result matrix:\n");
print_matrix(C, M, N);
return 0;
}
And the following MLIR file -
module {
func.func @matmul(%M: i64, %N: i64, %K: i64, %A: memref<?xf32>, %B: memref<?xf32>, %C: memref<?xf32>) {
%c0 = arith.constant 0 : index
// Load A[0]
%a = memref.load %A[%c0] : memref<?xf32>
// Load B[0]
%b = memref.load %B[%c0] : memref<?xf32>
// Multiply A[0] and B[0]
%mul = arith.mulf %a, %b : f32
// Store the result in C[0]
memref.store %mul, %C[%c0] : memref<?xf32>
return
}
}
I lower the MLIR file to an object file by:
mlir-opt --convert-scf-to-cf --convert-func-to-llvm --convert-arith-to-llvm --finalize-memref-to-llvm --reconcile-unrealized-casts foo_mm.mlir -o foo_mm_opt.mlir
mlir-translate --mlir-to-llvmir foo_mm_opt.mlir -o foo_mm.ll
llc -filetype=obj foo_mm.ll -o foo_mm.o
I lower the C file to an object file by:
clang -c -g mlir_main2.c -o mlir_main2.o
And then link them with:
clang -g foo_mm.o mlir_main2.o -o program
But when I execute it, I get a segmentation fault.
If I comment out memref.store %mul, %C[%c0] : memref<?xf32>
in the MLIR file, then there is no segmentation fault.
Is there some memory layout issue happening here?