[Mlir-commits] [mlir] [MLIR][JitRunner] Correctly register symbol map (PR #90381)

llvmlistbot at llvm.org llvmlistbot at llvm.org
Mon May 6 20:11:42 PDT 2024


================
@@ -295,4 +296,66 @@ TEST(NativeMemRefJit, MAYBE_JITCallback) {
     ASSERT_EQ(elt, coefficient * count++);
 }
 
+namespace {
+struct TestFile {
+  SmallString<256> filename;
+  std::error_code ec;
+  TestFile(StringRef filename, StringRef contents) : filename{filename} {
+    llvm::raw_fd_ostream outf{filename, ec, llvm::sys::fs::OF_None};
+    if (!ec) {
+      outf << contents;
+    }
+  }
+  ~TestFile() {
+    if (!ec) {
+      llvm::sys::fs::remove(filename);
+    }
+  }
+};
+static int32_t callbackval = 0;
+static void intcallback(int32_t v) { callbackval = v; }
+} // namespace
+
+TEST(MLIRExecutionEngine, SKIP_WITHOUT_JIT(JitRunnerSymbol)) {
+  std::string moduleStr = R"mlir(
+module {
+  llvm.func @callback(i32) attributes {sym_visibility = "private"}
+  llvm.func @caller_for_callback() attributes {llvm.emit_c_interface} {
+    %0 = llvm.mlir.constant(114514 : i32) : i32
+    llvm.call @callback(%0) : (i32) -> ()
+    llvm.return
+  }
+  llvm.func @_mlir_ciface_caller_for_callback() attributes {llvm.emit_c_interface} {
+    llvm.call @caller_for_callback() : () -> ()
+    llvm.return
+  }
+}
+  )mlir";
+  char path[] = "mlir_executionengine_jitrunnertest.mlir";
+  TestFile testfile{path, moduleStr};
+  ASSERT_TRUE(!testfile.ec);
+  DialectRegistry registry;
+  registerAllDialects(registry);
+  registerBuiltinDialectTranslation(registry);
+  registerLLVMDialectTranslation(registry);
+  mlir::JitRunnerConfig config;
+  config.runtimesymbolMap = [](llvm::orc::MangleAndInterner interner) {
+    llvm::orc::SymbolMap symbolMap;
+    symbolMap[interner("callback")] = {
+        llvm::orc::ExecutorAddr::fromPtr(intcallback),
+        llvm::JITSymbolFlags::Exported};
+    return symbolMap;
+  };
+
+  char S_toolname[] = "test-runner";
+  char S_e[] = "-e";
+  char S_main[] = "caller_for_callback";
+  char S_entryPoint[] = "-entry-point-result=void";
+  char *argv[] = {S_toolname, path, S_e, S_main, S_entryPoint};
+  int exitcode = mlir::JitRunnerMain(sizeof(argv) / sizeof(argv[0]), argv,
+                                     registry, config);
----------------
Menooker wrote:

> It is meant to be exposed for command line tools, and so should be testable through command line tool.

I understand it looks strange to test in that way. Which way do you suggest to test the case that `runtimesymbolMap` is used? Maybe we need to add a testing executable which sets `runtimesymbolMap`?

> I don't see any use of runtimesymbolMap upstream, maybe we should just remove this right now already.

`runtimesymbolMap` is useful in some downstream tools. For example, we may need to override some C-runtime functions for a specific MLIR module and leave the non-MLIR-Jitted code unchanged - like "hooking" some extern functions in the module. Using `runtimesymbolMap` is a easy way to do that. I am new to MLIR. Could you suggest another way to do that in `JitRunnerMain` for command line tools?



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


More information about the Mlir-commits mailing list