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 @@ -1391,8 +1391,6 @@ return nullptr; if (failed(translator.createTBAAMetadata())) return nullptr; - if (failed(translator.convertFunctions())) - return nullptr; // Convert other top-level operations if possible. llvm::IRBuilder<> llvmBuilder(llvmContext); @@ -1405,6 +1403,12 @@ } } + // Convert function bodies after all other operations, breaking possible + // declaration-reference issues between top-level non-LLVM Ops and non-LLVM + // ops inside function bodies. + if (failed(translator.convertFunctions())) + return nullptr; + // Convert module itself. if (failed(translator.convertOperation(*module, llvmBuilder))) return nullptr;