LLVM Discussion Forums

Mlir to llvm ir

I want convert mlir to llvm ir and then to machine code .please tell me which instruction to use to do so.

Thank You

You need to convert everything to the LLVM dialect in MLIR as a first step. The conversions to run depend on where you start. If the IR contains only the standard dialect, mlir-opt -convert-std-to-llvm should suffice. Once MLIR uses only the LLVM dialect, you can translate it to LLVM IR using mlir-translate -mlir-to-llvmir and use LLVM tools like on any other LLVM IR module. In particular llc gives you the assembly.

when I try to convert llvm dialect to llvm ir Iam getting error as : error: custom op ‘llvm.insertvalue’ expected wrapped LLVM IR structure/array type
%7 = llvm.insertvalue %6, %4[0 : i32] : !llvm.ptr<struct<(ptr, ptr, i64, array<4 x i64>, array<4 x i64>)>>

Thank You

How did you obtain the LLVM dialect? It is invalid, as the error indicates, llvm.insertvalue cannot be applied to pointers.

1.I first converted onnx to llvm dialect using onnx-mlir --EmitLLVMIR .

  1. I also used another method to get LLVM IR that is get the affine dialect from onnx using onnx-mlir --EmitMLIR.The I lower the affine to LLVM dialect using mlir-opt --allow-unregistered-dialect --lower-affine --convert-scf-to-std --convert-std-to-llvm.
    But I got error as error: ‘std.store’ op operand #2 must be index, but got ‘!llvm.i64’
    affine.store %cst_1, %13[symbol(%arg1), symbol(%arg2), symbol(%arg3), symbol(%arg4)] : memref<1x1x32x32xf32>
    ^
    model.onnx.mlir:72:13: note: see current operation: “std.store”(%20, %83, %90, %95, %100, %105) : (!llvm.float, memref<1x1x32x32xf32>, !llvm.i64, !llvm.i64, !llvm.i64, !llvm.i64) -> ()

Thank You

I first converted ONNX to LLVM dialect using onnx-mlir --EmitLLVMIR

I also tried another method that is first converted onnx to MLIR using onnx-mlir --EmitMLIR. I got affine dialect .Then I tried lower that to LLVM dialect using mlir-opt --allow-unregistered-dialect --lower-affine --convert-scf-to-std --convert-std-to-llvm .But I got error as error: ‘std.store’ op operand #2 must be index, but got ‘!llvm.i64’
affine.store %cst_1, %13[symbol(%arg1), symbol(%arg2), symbol(%arg3), symbol(%arg4)] : memref<1x1x32x32xf32>
^
model.onnx.mlir:72:13: note: see current operation: “std.store”(%20, %83, %90, %95, %100, %105) : (!llvm.float, memref<1x1x32x32xf32>, !llvm.i64, !llvm.i64, !llvm.i64, !llvm.i64) -> ()

Thank You

It looks like ONNX is emitting invalid MLIR. You should reach out to the developers of the ONNX dialect and report the problem to them.

You shouldn’t be needing --allow-unregistered-dialect. MLIR does will not know how to convert unregistered operations to the LLVM dialect, so the conversion will fail. If mlir-opt complains without this option, then it cannot be used for this flow.

We cannot guess what went wrong unless there is a way to reproduce the problem. You can try passing -debug-only=dialect-conversion and analyzing the output to see which conversion fails and why.

Thank you for your response