diff --git a/third_party/mlir/include/mlir/Target/LLVMIR/ModuleTranslation.h b/third_party/mlir/include/mlir/Target/LLVMIR/ModuleTranslation.h index 71831fee00f..063ce7d173b 100644 --- a/third_party/mlir/include/mlir/Target/LLVMIR/ModuleTranslation.h +++ b/third_party/mlir/include/mlir/Target/LLVMIR/ModuleTranslation.h @@ -54,6 +54,7 @@ class ModuleTranslation { T translator(m); translator.llvmModule = std::move(llvmModule); + translator.convertGlobals(); if (translator.convertFunctions()) return nullptr; @@ -72,6 +73,7 @@ class ModuleTranslation { private: bool convertFunctions(); + void convertGlobals(); bool convertOneFunction(FuncOp func); void connectPHINodes(FuncOp func); bool convertBlock(Block &bb, bool ignoreArguments); diff --git a/third_party/mlir/lib/Target/LLVMIR/ModuleTranslation.cpp b/third_party/mlir/lib/Target/LLVMIR/ModuleTranslation.cpp index 08255b47f9f..bf43848c9ef 100644 --- a/third_party/mlir/lib/Target/LLVMIR/ModuleTranslation.cpp +++ b/third_party/mlir/lib/Target/LLVMIR/ModuleTranslation.cpp @@ -289,6 +289,16 @@ bool ModuleTranslation::convertBlock(Block &bb, bool ignoreArguments) { return false; } +// Create named global variables that correspond to llvm.global definitions. +void ModuleTranslation::convertGlobals() { + for (auto op : mlirModule.getOps()) { + llvm::Type *type = op.getType().getUnderlyingType(); + new llvm::GlobalVariable( + *llvmModule, type, op.constant(), llvm::GlobalValue::InternalLinkage, + getLLVMConstant(type, op.value(), op.getLoc()), op.sym_name()); + } +} + // Get the SSA value passed to the current block from the terminator operation // of its predecessor. static Value *getPHISourceValue(Block *current, Block *pred,