[Mlir-commits] [mlir] [mlir] Add mlirTranslateModuleToLLVMIR to MLIR-C (PR #73117)

Oleksandr Alex Zinenko llvmlistbot at llvm.org
Wed Nov 22 06:17:56 PST 2023


================
@@ -73,11 +79,47 @@ static void testTypeCreation(MlirContext ctx) {
           mlirTypeEqual(i32_i64_s, i32_i64_s_ref));
 }
 
+// CHECK-LABEL: testToLLVMIR()
+static void testToLLVMIR(MlirContext ctx) {
+  fprintf(stderr, "testToLLVMIR()\n");
+  LLVMContextRef llvmCtx = LLVMContextCreate();
+
+  const char *moduleString = "llvm.func @add(%arg0: i64, %arg1: i64) -> i64 { \
+                                %0 = llvm.add %arg0, %arg1  : i64 \
+                                llvm.return %0 : i64 \
+                             }";
+
+  mlirRegisterAllLLVMTranslations(ctx);
+
+  MlirModule module =
+      mlirModuleCreateParse(ctx, mlirStringRefCreateFromCString(moduleString));
+
+  MlirOperation operation = mlirModuleGetOperation(module);
+
+  LLVMModuleRef llvmModule = mlirTranslateModuleToLLVMIR(operation, llvmCtx);
+
+  // clang-format off
+  // CHECK: ; ModuleID = 'LLVMDialectModule'
+  // CHECK-NEXT: source_filename = "LLVMDialectModule"
+  // CHECK: declare ptr @malloc(i64 %0)
+  // CHECK: declare void @free(ptr %0)
+  // CHECK: define i64 @add(i64 %0, i64 %1) {
+  // CHECK-NEXT:   %3 = add i64 %0, %1
+  // CHECK-NEXT:   ret i64 %3
+  // CHECK-NEXT: }
----------------
ftynse wrote:

Don't pattern-match SSA value names, these are not guaranteed to be stable. Use something like `%[[var:.*]]` to capture the name of the value at definition point and something like `%[[var]]` to use it as use points. It is also possible to do wildcard matching when names don't matter. See https://mlir.llvm.org/getting_started/TestingGuide/#filecheck-best-practices for more details.

https://github.com/llvm/llvm-project/pull/73117


More information about the Mlir-commits mailing list