diff --git a/mlir/lib/Target/LLVMIR/ModuleTranslation.cpp b/mlir/lib/Target/LLVMIR/ModuleTranslation.cpp --- a/mlir/lib/Target/LLVMIR/ModuleTranslation.cpp +++ b/mlir/lib/Target/LLVMIR/ModuleTranslation.cpp @@ -1388,6 +1388,15 @@ LLVM::ensureDistinctSuccessors(module); ModuleTranslation translator(module, std::move(llvmModule)); + llvm::IRBuilder<> llvmBuilder(llvmContext); + + // Convert module before functions and operations inside, so dialect + // attributes can be used to change dialect-specific global configurations via + // `amendOperation()`. These configurations can then influence the translation + // of operations afterwards. + if (failed(translator.convertOperation(*module, llvmBuilder))) + return nullptr; + if (failed(translator.convertComdats())) return nullptr; if (failed(translator.convertFunctionSignatures())) @@ -1400,7 +1409,6 @@ return nullptr; // Convert other top-level operations if possible. - llvm::IRBuilder<> llvmBuilder(llvmContext); for (Operation &o : getModuleBody(module).getOperations()) { if (!isa(&o) && @@ -1416,10 +1424,6 @@ if (failed(translator.convertFunctions())) return nullptr; - // Convert module itself. - if (failed(translator.convertOperation(*module, llvmBuilder))) - return nullptr; - if (llvm::verifyModule(*translator.llvmModule, &llvm::errs())) return nullptr;