[Mlir-commits] [mlir] [MLIR][LLVM] Allow importing of nameless globals (PR #101918)
llvmlistbot at llvm.org
llvmlistbot at llvm.org
Sun Aug 4 19:21:51 PDT 2024
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-mlir-llvm
@llvm/pr-subscribers-mlir
Author: Ivan R. Ivanov (ivanradanov)
<details>
<summary>Changes</summary>
LLVM allows nameless globals for private global variables whereas in MLIR globals must be addressed using symbols. We attach symbols to nameless globals in order to enable their import.
---
Full diff: https://github.com/llvm/llvm-project/pull/101918.diff
3 Files Affected:
- (modified) mlir/include/mlir/Target/LLVMIR/ModuleImport.h (+3)
- (modified) mlir/lib/Target/LLVMIR/ModuleImport.cpp (+19-2)
- (modified) mlir/test/Target/LLVMIR/Import/global-variables.ll (+14-1)
``````````diff
diff --git a/mlir/include/mlir/Target/LLVMIR/ModuleImport.h b/mlir/include/mlir/Target/LLVMIR/ModuleImport.h
index 04d098d38155b..2667530963109 100644
--- a/mlir/include/mlir/Target/LLVMIR/ModuleImport.h
+++ b/mlir/include/mlir/Target/LLVMIR/ModuleImport.h
@@ -19,6 +19,7 @@
#include "mlir/Target/LLVMIR/Import.h"
#include "mlir/Target/LLVMIR/LLVMImportInterface.h"
#include "mlir/Target/LLVMIR/TypeFromLLVM.h"
+#include "llvm/IR/GlobalVariable.h"
namespace llvm {
class BasicBlock;
@@ -367,6 +368,8 @@ class ModuleImport {
ModuleOp mlirModule;
/// The LLVM module being imported.
std::unique_ptr<llvm::Module> llvmModule;
+ /// Nameless globals
+ DenseMap<llvm::GlobalVariable *, FlatSymbolRefAttr> namelessGlobals;
/// A dialect interface collection used for dispatching the import to specific
/// dialects.
diff --git a/mlir/lib/Target/LLVMIR/ModuleImport.cpp b/mlir/lib/Target/LLVMIR/ModuleImport.cpp
index 8b40b7b2df6c7..f84ad30a546ea 100644
--- a/mlir/lib/Target/LLVMIR/ModuleImport.cpp
+++ b/mlir/lib/Target/LLVMIR/ModuleImport.cpp
@@ -12,6 +12,7 @@
//===----------------------------------------------------------------------===//
#include "mlir/Target/LLVMIR/ModuleImport.h"
+#include "mlir/IR/BuiltinAttributes.h"
#include "mlir/Target/LLVMIR/Import.h"
#include "AttrKindDetail.h"
@@ -73,6 +74,11 @@ static constexpr StringRef getGlobalCtorsVarName() {
return "llvm.global_ctors";
}
+/// Prefix used for symbols of nameless llvm globals.
+static constexpr StringRef getNamelessGlobalPrefix() {
+ return "mlir.llvm.nameless_global.";
+}
+
/// Returns the name of the global_dtors global variables.
static constexpr StringRef getGlobalDtorsVarName() {
return "llvm.global_dtors";
@@ -884,9 +890,15 @@ LogicalResult ModuleImport::convertGlobal(llvm::GlobalVariable *globalVar) {
globalExpressionAttr =
debugImporter->translateGlobalVariableExpression(globalExpressions[0]);
+ std::string globalName = globalVar->getName().str();
+ if (globalName == "") {
+ globalName = getNamelessGlobalPrefix().str() +
+ std::to_string(namelessGlobals.size());
+ namelessGlobals[globalVar] = FlatSymbolRefAttr::get(context, globalName);
+ }
GlobalOp globalOp = builder.create<GlobalOp>(
mlirModule.getLoc(), type, globalVar->isConstant(),
- convertLinkageFromLLVM(globalVar->getLinkage()), globalVar->getName(),
+ convertLinkageFromLLVM(globalVar->getLinkage()), globalName.c_str(),
valueAttr, alignment, /*addr_space=*/globalVar->getAddressSpace(),
/*dso_local=*/globalVar->isDSOLocal(),
/*thread_local=*/globalVar->isThreadLocal(), /*comdat=*/SymbolRefAttr(),
@@ -1061,7 +1073,12 @@ FailureOr<Value> ModuleImport::convertConstant(llvm::Constant *constant) {
// Convert global variable accesses.
if (auto *globalVar = dyn_cast<llvm::GlobalVariable>(constant)) {
Type type = convertType(globalVar->getType());
- auto symbolRef = FlatSymbolRefAttr::get(context, globalVar->getName());
+ StringRef globalName = globalVar->getName();
+ FlatSymbolRefAttr symbolRef;
+ if (globalName == "")
+ symbolRef = namelessGlobals[globalVar];
+ else
+ symbolRef = FlatSymbolRefAttr::get(context, globalName);
return builder.create<AddressOfOp>(loc, type, symbolRef).getResult();
}
diff --git a/mlir/test/Target/LLVMIR/Import/global-variables.ll b/mlir/test/Target/LLVMIR/Import/global-variables.ll
index 902f77bd7e6cb..d8c33e311c626 100644
--- a/mlir/test/Target/LLVMIR/Import/global-variables.ll
+++ b/mlir/test/Target/LLVMIR/Import/global-variables.ll
@@ -280,7 +280,20 @@ define void @bar() {
; CHECK-DAG: #[[GLOBAL_VAR:.*]] = #llvm.di_global_variable<file = #[[FILE]], line = 268, type = #[[COMPOSITE_TYPE]], isLocalToUnit = true, isDefined = true>
; CHECK-DAG: #[[GLOBAL_VAR_EXPR:.*]] = #llvm.di_global_variable_expression<var = #[[GLOBAL_VAR]], expr = <>>
-; CHECK-DAG: llvm.mlir.global external constant @".str.1"() {addr_space = 0 : i32, dbg_expr = #[[GLOBAL_VAR_EXPR]]}
+; CHECK: llvm.mlir.global private unnamed_addr constant @mlir.llvm.nameless_global.0("0\00") {addr_space = 0 : i32, dso_local}
+; CHECK: llvm.mlir.global private unnamed_addr constant @mlir.llvm.nameless_global.1("1\00") {addr_space = 0 : i32, dso_local}
+;
+; CHECK: llvm.mlir.global internal constant @zero() {addr_space = 0 : i32, dso_local} : !llvm.ptr {
+; CHECK: llvm.mlir.addressof @mlir.llvm.nameless_global.0 : !llvm.ptr
+; CHECK: llvm.mlir.global internal constant @one() {addr_space = 0 : i32, dso_local} : !llvm.ptr {
+; CHECK: llvm.mlir.addressof @mlir.llvm.nameless_global.1 : !llvm.ptr
+
+; CHECK: llvm.mlir.global external constant @".str.1"() {addr_space = 0 : i32, dbg_expr = #[[GLOBAL_VAR_EXPR]]}
+
+ at 0 = private unnamed_addr constant [2 x i8] c"0\00"
+ at 1 = private unnamed_addr constant [2 x i8] c"1\00"
+ at zero = internal constant ptr @0
+ at one = internal constant ptr @1
@.str.1 = external constant [10 x i8], !dbg !0
``````````
</details>
https://github.com/llvm/llvm-project/pull/101918
More information about the Mlir-commits
mailing list