LLVM Discussion Forums

Vector Matrix Multiply Error

Hie,
I am trying to use vector dialect for matrix multiply. The code runs into error when tried to run with mlir-cpu-runner. The code is as follows :

// mlir-opt  -convert-linalg-to-affine-loops -convert-vector-to-scf -lower-affine -convert-scf-to-std -convert-vector-to-llvm -print-ir-after="convert-linalg-to-affine-loops" $1 |  mlir-cpu-runner -O3 -e main -entry-point-result=void

#identity_map = affine_map<(d0) -> (d0)>

func @main() {
%A = alloc() : memref<9xf32>
%B = alloc() : memref<9xf32>
%C = alloc() : memref<9xf32>

%c0 = constant 0.000 : f32
%c1 = constant 1.000 : f32
%i0 = constant 0 : index

linalg.fill(%A,%c1) : memref<9xf32> , f32
linalg.fill(%B,%c1) : memref<9xf32> , f32


%a = vector.transfer_read %A[%i0], %c0 { permutation_map = #identity_map } : memref<9xf32>, vector<9xf32>
%b = vector.transfer_read %B[%i0], %c0 { permutation_map = #identity_map } : memref<9xf32>, vector<9xf32>

%c = vector.matrix_multiply %a, %b { lhs_rows = 3 : i32, lhs_columns = 3 : i32, rhs_columns = 3 : i32 } : (vector<9xf32>, vector<9xf32>) -> vector<9xf32>

vector.transfer_write %cc, %C[%i0] {permutation_map = #identity_map } : vector<9xf32>, memref<9xf32>

return
} 

The error I am getting is :

WidenVectorResult #0: t37: v9f32 = llvm.matrix.multiply TargetConstant:i64<178>, t87, t82, TargetConstant:i32<3>, TargetConstant:i32<3>, TargetConstant:i32<3>, <stdin>:141:12

Do not know how to widen the result of this operator!
UNREACHABLE executed at /data/sharwari/MLIR/llvm-project/llvm/lib/CodeGen/SelectionDAG/LegalizeVectorTypes.cpp:2773!

Thanks,
Sharwari

The problem is solved. One needs to pass -lower-matrix-intrinsics to mlir-cpu-runner and the code will work fine.

There’s a great piece of work by @aartbik and @nicolasvasilache which helped me solve this problem. Links mentioned below :

  1. Case Study Docs on Vector Dialect CPU Codegen
  2. [VectorOps] [RFC] Add suite of "integration" tests for Vector dialect operations

Sorry for the delayed reply (I was in transit), but I am glad you found the flag (-lower-matrix-intrinsics). Note that another flag that may be of interest controls a row-major or column-major interpretation of the flattened matrix (e.g. -matrix-default-layout=column-major).

1 Like