Skip to content

Instantly share code, notes, and snippets.

@iwa0
Created December 25, 2022 13:29
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save iwa0/0a898f47a4a2ad1fab19f0d450d0831c to your computer and use it in GitHub Desktop.
Save iwa0/0a898f47a4a2ad1fab19f0d450d0831c to your computer and use it in GitHub Desktop.
mlir-opt matmult.mlir -convert-linalg-to-loops -lower-affine -convert-scf-to-cf -convert-linalg-to-llvm -convert-memref-to-llvm -convert-func-to-llvm -reconcile-unrealized-casts > out.mlir
mlir-cpu-runner out.mlir -O3 -e main -entry-point-result=void --shared-libs=libmlir_runner_utils.dylib
// C += A * B.
func.func @matmul(%A: memref<2048x2048xf64>, %B: memref<2048x2048xf64>, %C: memref<2048x2048xf64>) {
affine.for %arg3 = 0 to 2048 {
affine.for %arg4 = 0 to 2048 {
affine.for %arg5 = 0 to 2048 {
%a = affine.load %A[%arg3, %arg5] : memref<2048x2048xf64>
%b = affine.load %B[%arg5, %arg4] : memref<2048x2048xf64>
%ci = affine.load %C[%arg3, %arg4] : memref<2048x2048xf64>
%p = arith.mulf %a, %b : f64
%co = arith.addf %ci, %p : f64
affine.store %co, %C[%arg3, %arg4] : memref<2048x2048xf64>
}
}
}
return
}
func.func @main() {
%A = memref.alloc() : memref<2048x2048xf64>
%B = memref.alloc() : memref<2048x2048xf64>
%C = memref.alloc() : memref<2048x2048xf64>
%cf1 = llvm.mlir.constant(1.00000e+00 : f64) : f64
linalg.fill ins(%cf1 : f64) outs(%A : memref<2048x2048xf64>)
linalg.fill ins(%cf1 : f64) outs(%B : memref<2048x2048xf64>)
linalg.fill ins(%cf1 : f64) outs(%C : memref<2048x2048xf64>)
call @matmul(%A, %B, %C) : (memref<2048x2048xf64>, memref<2048x2048xf64>, memref<2048x2048xf64>) -> ()
%C.cast = memref.cast %C : memref<2048x2048xf64> to memref<*xf64>
call @printMemrefF64(%C.cast): (memref<*xf64>) -> ()
return
}
func.func private @printMemrefF64(memref<*xf64>)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment