Skip to content

Commit 11fd760

Browse files
tqchenjoker-eph
andauthored
[MLIR][ExecutionEngine] Enable PIC option (#170995)
This PR enables the MLIR execution engine to dump object file as PIC code, which is needed when the object file is later bundled into a dynamic shared library. --------- Co-authored-by: Mehdi Amini <joker.eph@gmail.com>
1 parent 3fc7419 commit 11fd760

File tree

6 files changed

+18
-11
lines changed

6 files changed

+18
-11
lines changed

mlir/include/mlir-c/ExecutionEngine.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,13 @@ DEFINE_C_API_STRUCT(MlirExecutionEngine, void);
4141
/// generation. The number and array of paths corresponding to shared libraries
4242
/// that will be loaded are specified via `numPaths` and `sharedLibPaths`
4343
/// respectively.
44+
/// The `enablePIC` arguments controls the relocation model, when true the
45+
/// generated code is emitted as "position independent", making it possible to
46+
/// save it and reload it as a shared object in another process.
4447
/// TODO: figure out other options.
4548
MLIR_CAPI_EXPORTED MlirExecutionEngine mlirExecutionEngineCreate(
4649
MlirModule op, int optLevel, int numPaths,
47-
const MlirStringRef *sharedLibPaths, bool enableObjectDump);
50+
const MlirStringRef *sharedLibPaths, bool enableObjectDump, bool enablePIC);
4851

4952
/// Initialize the ExecutionEngine. Global constructors specified by
5053
/// `llvm.mlir.global_ctors` will be run. One common scenario is that kernel

mlir/lib/Bindings/Python/ExecutionEngineModule.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -75,21 +75,21 @@ NB_MODULE(_mlirExecutionEngine, m) {
7575
"__init__",
7676
[](PyExecutionEngine &self, MlirModule module, int optLevel,
7777
const std::vector<std::string> &sharedLibPaths,
78-
bool enableObjectDump) {
78+
bool enableObjectDump, bool enablePIC) {
7979
llvm::SmallVector<MlirStringRef, 4> libPaths;
8080
for (const std::string &path : sharedLibPaths)
8181
libPaths.push_back({path.c_str(), path.length()});
82-
MlirExecutionEngine executionEngine =
83-
mlirExecutionEngineCreate(module, optLevel, libPaths.size(),
84-
libPaths.data(), enableObjectDump);
82+
MlirExecutionEngine executionEngine = mlirExecutionEngineCreate(
83+
module, optLevel, libPaths.size(), libPaths.data(),
84+
enableObjectDump, enablePIC);
8585
if (mlirExecutionEngineIsNull(executionEngine))
8686
throw std::runtime_error(
8787
"Failure while creating the ExecutionEngine.");
8888
new (&self) PyExecutionEngine(executionEngine);
8989
},
9090
nb::arg("module"), nb::arg("opt_level") = 2,
9191
nb::arg("shared_libs") = nb::list(),
92-
nb::arg("enable_object_dump") = true,
92+
nb::arg("enable_object_dump") = true, nb::arg("enable_pic") = false,
9393
"Create a new ExecutionEngine instance for the given Module. The "
9494
"module must contain only dialects that can be translated to LLVM. "
9595
"Perform transformations and code generation at the optimization "

mlir/lib/CAPI/ExecutionEngine/ExecutionEngine.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ using namespace mlir;
2222
extern "C" MlirExecutionEngine
2323
mlirExecutionEngineCreate(MlirModule op, int optLevel, int numPaths,
2424
const MlirStringRef *sharedLibPaths,
25-
bool enableObjectDump) {
25+
bool enableObjectDump, bool enablePIC) {
2626
static bool initOnce = [] {
2727
llvm::InitializeNativeTarget();
2828
llvm::InitializeNativeTargetAsmParser(); // needed for inline_asm
@@ -43,6 +43,8 @@ mlirExecutionEngineCreate(MlirModule op, int optLevel, int numPaths,
4343
consumeError(tmBuilderOrError.takeError());
4444
return MlirExecutionEngine{nullptr};
4545
}
46+
if (enablePIC)
47+
tmBuilderOrError->setRelocationModel(llvm::Reloc::PIC_);
4648
auto tmOrError = tmBuilderOrError->createTargetMachine();
4749
if (!tmOrError) {
4850
llvm::errs() << "Failed to create a TargetMachine for the host because: \n";
@@ -63,7 +65,8 @@ mlirExecutionEngineCreate(MlirModule op, int optLevel, int numPaths,
6365
jitOptions.jitCodeGenOptLevel = static_cast<llvm::CodeGenOptLevel>(optLevel);
6466
jitOptions.sharedLibPaths = libPaths;
6567
jitOptions.enableObjectDump = enableObjectDump;
66-
auto jitOrError = ExecutionEngine::create(unwrap(op), jitOptions);
68+
auto jitOrError = ExecutionEngine::create(unwrap(op), jitOptions,
69+
std::move(tmOrError.get()));
6770
if (!jitOrError) {
6871
llvm::errs() << "Failed to create an ExecutionEngine because: \n";
6972
consumeError(jitOrError.takeError());

mlir/test/CAPI/execution_engine.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ void testSimpleExecution(void) {
6969
mlirRegisterAllLLVMTranslations(ctx);
7070
MlirExecutionEngine jit = mlirExecutionEngineCreate(
7171
module, /*optLevel=*/2, /*numPaths=*/0, /*sharedLibPaths=*/NULL,
72-
/*enableObjectDump=*/false);
72+
/*enableObjectDump=*/false, /*enablePIC=*/false);
7373
if (mlirExecutionEngineIsNull(jit)) {
7474
fprintf(stderr, "Execution engine creation failed");
7575
exit(2);
@@ -125,7 +125,7 @@ void testOmpCreation(void) {
125125
// against the OpenMP library.
126126
MlirExecutionEngine jit = mlirExecutionEngineCreate(
127127
module, /*optLevel=*/2, /*numPaths=*/0, /*sharedLibPaths=*/NULL,
128-
/*enableObjectDump=*/false);
128+
/*enableObjectDump=*/false, /*enablePIC=*/false);
129129
if (mlirExecutionEngineIsNull(jit)) {
130130
fprintf(stderr, "Engine creation failed with OpenMP");
131131
exit(2);

mlir/test/CAPI/global_constructors.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ void testGlobalCtorJitCallback(void) {
7979
// Create execution engine with initialization disabled
8080
MlirExecutionEngine jit = mlirExecutionEngineCreate(
8181
module, /*optLevel=*/2, /*numPaths=*/0, /*sharedLibPaths=*/NULL,
82-
/*enableObjectDump=*/false);
82+
/*enableObjectDump=*/false, /*enablePIC=*/false);
8383

8484
if (mlirExecutionEngineIsNull(jit)) {
8585
fprintf(stderr, "Execution engine creation failed");

mlir/test/python/execution_engine.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -807,6 +807,7 @@ def testDumpToObjectFile():
807807
# because RTDyldObjectLinkingLayer::emit will try to resolve symbols before dumping
808808
# (see the jitLinkForORC call at the bottom there).
809809
shared_libs=[MLIR_C_RUNNER_UTILS],
810+
enable_pic=True,
810811
)
811812

812813
# CHECK: Object file exists: True

0 commit comments

Comments
 (0)