Skip to content

Commit

Permalink
[mlir] Convert function signatures before converting globals
Browse files Browse the repository at this point in the history
Summary: This allows global initializers to reference functions.

Differential Revision: https://reviews.llvm.org/D83266
  • Loading branch information
silvasean committed Jul 7, 2020
1 parent 6cff71e commit a084b94
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 2 deletions.
3 changes: 3 additions & 0 deletions mlir/include/mlir/Target/LLVMIR/ModuleTranslation.h
Expand Up @@ -61,6 +61,8 @@ class ModuleTranslation {
LLVM::ensureDistinctSuccessors(m);

T translator(m, std::move(llvmModule));
if (failed(translator.convertFunctionSignatures()))
return nullptr;
if (failed(translator.convertGlobals()))
return nullptr;
if (failed(translator.convertFunctions()))
Expand Down Expand Up @@ -94,6 +96,7 @@ class ModuleTranslation {
/// Check whether the module contains only supported ops directly in its body.
static LogicalResult checkSupportedModuleOps(Operation *m);

LogicalResult convertFunctionSignatures();
LogicalResult convertFunctions();
LogicalResult convertGlobals();
LogicalResult convertOneFunction(LLVMFuncOp func);
Expand Down
13 changes: 11 additions & 2 deletions mlir/lib/Target/LLVMIR/ModuleTranslation.cpp
Expand Up @@ -783,12 +783,13 @@ LogicalResult ModuleTranslation::checkSupportedModuleOps(Operation *m) {
return success();
}

LogicalResult ModuleTranslation::convertFunctions() {
LogicalResult ModuleTranslation::convertFunctionSignatures() {
// Lock access to the llvm context.
llvm::sys::SmartScopedLock<true> scopedLock(
llvmDialect->getLLVMContextMutex());

// Declare all functions first because there may be function calls that form a
// call graph with cycles.
// call graph with cycles, or global initializers that reference functions.
for (auto function : getModuleBody(mlirModule).getOps<LLVMFuncOp>()) {
llvm::FunctionCallee llvmFuncCst = llvmModule->getOrInsertFunction(
function.getName(),
Expand All @@ -802,6 +803,14 @@ LogicalResult ModuleTranslation::convertFunctions() {
return failure();
}

return success();
}

LogicalResult ModuleTranslation::convertFunctions() {
// Lock access to the llvm context.
llvm::sys::SmartScopedLock<true> scopedLock(
llvmDialect->getLLVMContextMutex());

// Convert functions.
for (auto function : getModuleBody(mlirModule).getOps<LLVMFuncOp>()) {
// Ignore external functions.
Expand Down
10 changes: 10 additions & 0 deletions mlir/test/Target/llvmir.mlir
Expand Up @@ -1230,3 +1230,13 @@ llvm.func @constant_bf16() -> !llvm<"bfloat"> {

// CHECK: ret bfloat 0xR4120

// -----

llvm.func @address_taken() {
llvm.return
}

llvm.mlir.global internal constant @taker_of_address() : !llvm<"void()*"> {
%0 = llvm.mlir.addressof @address_taken : !llvm<"void()*">
llvm.return %0 : !llvm<"void()*">
}

0 comments on commit a084b94

Please sign in to comment.