Skip to content

Commit cd9d487

Browse files
authored
[MLIR][ExecutionEngine] don't dump decls (llvm#164478)
Currently ExecutionEngine tries to dump all functions declared in the module, even those which are "external" (i.e., linked/loaded at runtime). E.g. ```mlir func.func private @printF32(f32) func.func @supported_arg_types(%arg0: i32, %arg1: f32) { call @printF32(%arg1) : (f32) -> () return } ``` fails with ``` Could not compile printF32: Symbols not found: [ __mlir_printF32 ] Program aborted due to an unhandled Error: Symbols not found: [ __mlir_printF32 ] ``` even though `printF32` can be provided at final build time (i.e., when the object file is linked to some executable or shlib). E.g, if our own `libmlir_c_runner_utils` is linked. So just skip functions which have no bodies during dump (i.e., are decls without defns).
1 parent 267b5b8 commit cd9d487

File tree

3 files changed

+23
-8
lines changed

3 files changed

+23
-8
lines changed

mlir/lib/ExecutionEngine/ExecutionEngine.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,8 @@ ExecutionEngine::create(Operation *m, const ExecutionEngineOptions &options,
239239
// Remember all entry-points if object dumping is enabled.
240240
if (options.enableObjectDump) {
241241
for (auto funcOp : m->getRegion(0).getOps<LLVM::LLVMFuncOp>()) {
242+
if (funcOp.getBlocks().empty())
243+
continue;
242244
StringRef funcName = funcOp.getSymName();
243245
engine->functionNames.push_back(funcName.str());
244246
}

mlir/test/python/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ add_public_tablegen_target(MLIRPythonTestIncGen)
1111

1212
add_subdirectory(lib)
1313

14-
set(MLIR_PYTHON_TEST_DEPENDS MLIRPythonModules)
14+
set(MLIR_PYTHON_TEST_DEPENDS MLIRPythonModules mlir-runner)
1515
if(NOT MLIR_STANDALONE_BUILD)
1616
list(APPEND MLIR_PYTHON_TEST_DEPENDS FileCheck count not)
1717
endif()

mlir/test/python/execution_engine.py

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# RUN: env MLIR_RUNNER_UTILS=%mlir_runner_utils MLIR_C_RUNNER_UTILS=%mlir_c_runner_utils %PYTHON %s 2>&1 | FileCheck %s
22
# REQUIRES: host-supports-jit
33
import gc, sys, os, tempfile
4+
from textwrap import dedent
45
from mlir.ir import *
56
from mlir.passmanager import *
67
from mlir.execution_engine import *
@@ -21,6 +22,7 @@
2122
"MLIR_C_RUNNER_UTILS", "../../../../lib/libmlir_c_runner_utils.so"
2223
)
2324

25+
2426
# Log everything to stderr and flush so that we have a unified stream to match
2527
# errors/info emitted by MLIR to stderr.
2628
def log(*args):
@@ -337,6 +339,7 @@ def callback(a):
337339
ctypes.pointer(ctypes.pointer(get_ranked_memref_descriptor(inp_arr))),
338340
)
339341

342+
340343
run(testUnrankedMemRefWithOffsetCallback)
341344

342345

@@ -785,15 +788,25 @@ def testDumpToObjectFile():
785788
try:
786789
with Context():
787790
module = Module.parse(
788-
"""
789-
module {
790-
func.func @main() attributes { llvm.emit_c_interface } {
791-
return
792-
}
793-
}"""
791+
dedent(
792+
"""
793+
func.func private @printF32(f32)
794+
func.func @main(%arg0: f32) attributes { llvm.emit_c_interface } {
795+
call @printF32(%arg0) : (f32) -> ()
796+
return
797+
}
798+
"""
799+
)
794800
)
795801

796-
execution_engine = ExecutionEngine(lowerToLLVM(module), opt_level=3)
802+
execution_engine = ExecutionEngine(
803+
lowerToLLVM(module),
804+
opt_level=3,
805+
# Loading MLIR_C_RUNNER_UTILS is necessary even though we don't actually run the code (i.e., call printF32)
806+
# because RTDyldObjectLinkingLayer::emit will try to resolve symbols before dumping
807+
# (see the jitLinkForORC call at the bottom there).
808+
shared_libs=[MLIR_C_RUNNER_UTILS],
809+
)
797810

798811
# CHECK: Object file exists: True
799812
print(f"Object file exists: {os.path.exists(object_path)}")

0 commit comments

Comments
 (0)