Skip to content

Commit a084b94

Browse files
committed
[mlir] Convert function signatures before converting globals
Summary: This allows global initializers to reference functions. Differential Revision: https://reviews.llvm.org/D83266
1 parent 6cff71e commit a084b94

File tree

3 files changed

+24
-2
lines changed

3 files changed

+24
-2
lines changed

mlir/include/mlir/Target/LLVMIR/ModuleTranslation.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@ class ModuleTranslation {
6161
LLVM::ensureDistinctSuccessors(m);
6262

6363
T translator(m, std::move(llvmModule));
64+
if (failed(translator.convertFunctionSignatures()))
65+
return nullptr;
6466
if (failed(translator.convertGlobals()))
6567
return nullptr;
6668
if (failed(translator.convertFunctions()))
@@ -94,6 +96,7 @@ class ModuleTranslation {
9496
/// Check whether the module contains only supported ops directly in its body.
9597
static LogicalResult checkSupportedModuleOps(Operation *m);
9698

99+
LogicalResult convertFunctionSignatures();
97100
LogicalResult convertFunctions();
98101
LogicalResult convertGlobals();
99102
LogicalResult convertOneFunction(LLVMFuncOp func);

mlir/lib/Target/LLVMIR/ModuleTranslation.cpp

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -783,12 +783,13 @@ LogicalResult ModuleTranslation::checkSupportedModuleOps(Operation *m) {
783783
return success();
784784
}
785785

786-
LogicalResult ModuleTranslation::convertFunctions() {
786+
LogicalResult ModuleTranslation::convertFunctionSignatures() {
787787
// Lock access to the llvm context.
788788
llvm::sys::SmartScopedLock<true> scopedLock(
789789
llvmDialect->getLLVMContextMutex());
790+
790791
// Declare all functions first because there may be function calls that form a
791-
// call graph with cycles.
792+
// call graph with cycles, or global initializers that reference functions.
792793
for (auto function : getModuleBody(mlirModule).getOps<LLVMFuncOp>()) {
793794
llvm::FunctionCallee llvmFuncCst = llvmModule->getOrInsertFunction(
794795
function.getName(),
@@ -802,6 +803,14 @@ LogicalResult ModuleTranslation::convertFunctions() {
802803
return failure();
803804
}
804805

806+
return success();
807+
}
808+
809+
LogicalResult ModuleTranslation::convertFunctions() {
810+
// Lock access to the llvm context.
811+
llvm::sys::SmartScopedLock<true> scopedLock(
812+
llvmDialect->getLLVMContextMutex());
813+
805814
// Convert functions.
806815
for (auto function : getModuleBody(mlirModule).getOps<LLVMFuncOp>()) {
807816
// Ignore external functions.

mlir/test/Target/llvmir.mlir

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1230,3 +1230,13 @@ llvm.func @constant_bf16() -> !llvm<"bfloat"> {
12301230

12311231
// CHECK: ret bfloat 0xR4120
12321232

1233+
// -----
1234+
1235+
llvm.func @address_taken() {
1236+
llvm.return
1237+
}
1238+
1239+
llvm.mlir.global internal constant @taker_of_address() : !llvm<"void()*"> {
1240+
%0 = llvm.mlir.addressof @address_taken : !llvm<"void()*">
1241+
llvm.return %0 : !llvm<"void()*">
1242+
}

0 commit comments

Comments
 (0)