-
Notifications
You must be signed in to change notification settings - Fork 14.9k
[MLIR] Split ExecutionEngine Initialization out of ctor into an explicit method call #153524
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
joker-eph
merged 18 commits into
llvm:main
from
jackalcooper:split-exe-engine-init-into-explicit-method
Aug 17, 2025
Merged
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
975a39e
port changes from last PR
jackalcooper 793e83a
update GPU docs to reflect latest impl
jackalcooper e948795
add module symbol->ptr load
jackalcooper fe4d555
__has_feature(address_sanitizer)
jackalcooper 0a55a9b
remove test in C
jackalcooper 637e13f
better test naming
jackalcooper 25db89b
skip asserts for AArch64
jackalcooper 4135510
add more sanitizers to skip
jackalcooper 03a62ac
fix typo
jackalcooper b5a4244
py version test
jackalcooper acf71c4
doc @binary_unload()
jackalcooper 5dd9b20
fmt
jackalcooper 6a78eea
restore c tests
jackalcooper 1a0e331
refine C++ and Python tests
jackalcooper c8ac9dc
fix fmt
jackalcooper 05b0d0c
fix cond
jackalcooper 44df150
Update mlir/unittests/ExecutionEngine/Invoke.cpp
jackalcooper eae73d5
mv TODO to skip msg & fmt
jackalcooper File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
//===- global_constructors.c - Test JIT with the global constructors ------===// | ||
// | ||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM | ||
// Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
// UNSUPPORTED: target=aarch64{{.*}}, target=arm64{{.*}} | ||
/* RUN: mlir-capi-global-constructors-test 2>&1 | FileCheck %s | ||
*/ | ||
/* REQUIRES: host-supports-jit | ||
*/ | ||
|
||
#include "mlir-c/Conversion.h" | ||
#include "mlir-c/ExecutionEngine.h" | ||
#include "mlir-c/IR.h" | ||
#include "mlir-c/RegisterEverything.h" | ||
|
||
#include <assert.h> | ||
#include <math.h> | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <string.h> | ||
|
||
static void registerAllUpstreamDialects(MlirContext ctx) { | ||
MlirDialectRegistry registry = mlirDialectRegistryCreate(); | ||
mlirRegisterAllDialects(registry); | ||
mlirContextAppendDialectRegistry(ctx, registry); | ||
mlirDialectRegistryDestroy(registry); | ||
} | ||
|
||
void lowerModuleToLLVM(MlirContext ctx, MlirModule module) { | ||
MlirPassManager pm = mlirPassManagerCreate(ctx); | ||
MlirOpPassManager opm = mlirPassManagerGetNestedUnder( | ||
pm, mlirStringRefCreateFromCString("func.func")); | ||
mlirPassManagerAddOwnedPass(pm, mlirCreateConversionConvertFuncToLLVMPass()); | ||
mlirOpPassManagerAddOwnedPass( | ||
opm, mlirCreateConversionArithToLLVMConversionPass()); | ||
MlirLogicalResult status = | ||
mlirPassManagerRunOnOp(pm, mlirModuleGetOperation(module)); | ||
if (mlirLogicalResultIsFailure(status)) { | ||
fprintf(stderr, "Unexpected failure running pass pipeline\n"); | ||
exit(2); | ||
} | ||
mlirPassManagerDestroy(pm); | ||
} | ||
|
||
// Helper variable to track callback invocations | ||
static int initCnt = 0; | ||
|
||
// Callback function that will be called during JIT initialization | ||
static void initCallback(void) { initCnt += 1; } | ||
|
||
// CHECK-LABEL: Running test 'testGlobalCtorJitCallback' | ||
void testGlobalCtorJitCallback(void) { | ||
MlirContext ctx = mlirContextCreate(); | ||
registerAllUpstreamDialects(ctx); | ||
|
||
// Create module with global constructor that calls our callback | ||
MlirModule module = mlirModuleCreateParse( | ||
ctx, mlirStringRefCreateFromCString( | ||
// clang-format off | ||
"module { \n" | ||
" llvm.mlir.global_ctors ctors = [@ctor], priorities = [0 : i32], data = [#llvm.zero] \n" | ||
" llvm.func @ctor() { \n" | ||
" func.call @init_callback() : () -> () \n" | ||
" llvm.return \n" | ||
" } \n" | ||
" func.func private @init_callback() attributes { llvm.emit_c_interface } \n" | ||
"} \n" | ||
// clang-format on | ||
)); | ||
|
||
lowerModuleToLLVM(ctx, module); | ||
mlirRegisterAllLLVMTranslations(ctx); | ||
|
||
// Create execution engine with initialization disabled | ||
MlirExecutionEngine jit = mlirExecutionEngineCreate( | ||
module, /*optLevel=*/2, /*numPaths=*/0, /*sharedLibPaths=*/NULL, | ||
/*enableObjectDump=*/false); | ||
|
||
if (mlirExecutionEngineIsNull(jit)) { | ||
fprintf(stderr, "Execution engine creation failed"); | ||
exit(2); | ||
} | ||
|
||
// Register callback symbol before initialization | ||
mlirExecutionEngineRegisterSymbol( | ||
jit, mlirStringRefCreateFromCString("_mlir_ciface_init_callback"), | ||
(void *)(uintptr_t)initCallback); | ||
|
||
mlirExecutionEngineInitialize(jit); | ||
|
||
// CHECK: Init count: 1 | ||
printf("Init count: %d\n", initCnt); | ||
|
||
mlirExecutionEngineDestroy(jit); | ||
mlirModuleDestroy(module); | ||
mlirContextDestroy(ctx); | ||
} | ||
|
||
int main(void) { | ||
|
||
#define _STRINGIFY(x) #x | ||
#define STRINGIFY(x) _STRINGIFY(x) | ||
#define TEST(test) \ | ||
printf("Running test '" STRINGIFY(test) "'\n"); \ | ||
test(); | ||
TEST(testGlobalCtorJitCallback); | ||
return 0; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
# UNSUPPORTED: target=aarch64{{.*}}, target=arm64{{.*}} | ||
# RUN: %PYTHON %s 2>&1 | FileCheck %s | ||
# REQUIRES: host-supports-jit | ||
import gc, sys, os, tempfile | ||
from mlir.ir import * | ||
from mlir.passmanager import * | ||
from mlir.execution_engine import * | ||
from mlir.runtime import * | ||
|
||
|
||
# Log everything to stderr and flush so that we have a unified stream to match | ||
# errors/info emitted by MLIR to stderr. | ||
def log(*args): | ||
print(*args, file=sys.stderr) | ||
sys.stderr.flush() | ||
|
||
|
||
def run(f): | ||
log("\nTEST:", f.__name__) | ||
f() | ||
gc.collect() | ||
assert Context._get_live_count() == 0 | ||
|
||
|
||
def lowerToLLVM(module): | ||
pm = PassManager.parse( | ||
"builtin.module(convert-func-to-llvm,reconcile-unrealized-casts)" | ||
) | ||
pm.run(module.operation) | ||
return module | ||
|
||
|
||
# Test JIT callback in global constructor | ||
# CHECK-LABEL: TEST: testJITCallbackInGlobalCtor | ||
def testJITCallbackInGlobalCtor(): | ||
init_cnt = 0 | ||
|
||
@ctypes.CFUNCTYPE(None) | ||
def initCallback(): | ||
nonlocal init_cnt | ||
init_cnt += 1 | ||
|
||
with Context(): | ||
module = Module.parse( | ||
r""" | ||
llvm.mlir.global_ctors ctors = [@ctor], priorities = [0 : i32], data = [#llvm.zero] | ||
llvm.func @ctor() { | ||
func.call @init_callback() : () -> () | ||
llvm.return | ||
} | ||
func.func private @init_callback() attributes { llvm.emit_c_interface } | ||
""" | ||
) | ||
|
||
# Setup execution engine | ||
execution_engine = ExecutionEngine(lowerToLLVM(module)) | ||
|
||
# Validate initialization hasn't run yet | ||
assert init_cnt == 0 | ||
|
||
# # Register callback | ||
execution_engine.register_runtime("init_callback", initCallback) | ||
|
||
# # Initialize and verify | ||
execution_engine.initialize() | ||
assert init_cnt == 1 | ||
# # Second initialization should be no-op | ||
execution_engine.initialize() | ||
assert init_cnt == 1 | ||
|
||
|
||
run(testJITCallbackInGlobalCtor) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.