Skip to content

Commit 1d3bfbb

Browse files
authored
[MLIR][LLVM] Fix import of globals with references to other globals (#111703)
This commit addresses an issue with importing globals that reference other globals. This case did not properly work due to not considering that `llvm::GlobalVariables` are derived from `llvm::Constant`.
1 parent 527cd11 commit 1d3bfbb

File tree

2 files changed

+30
-6
lines changed

2 files changed

+30
-6
lines changed

mlir/lib/Target/LLVMIR/ModuleImport.cpp

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -792,10 +792,6 @@ Attribute ModuleImport::getConstantAsAttr(llvm::Constant *constant) {
792792
if (Attribute scalarAttr = getScalarConstantAsAttr(builder, constant))
793793
return scalarAttr;
794794

795-
// Convert function references.
796-
if (auto *func = dyn_cast<llvm::Function>(constant))
797-
return SymbolRefAttr::get(builder.getContext(), func->getName());
798-
799795
// Returns the static shape of the provided type if possible.
800796
auto getConstantShape = [&](llvm::Type *type) {
801797
return llvm::dyn_cast_if_present<ShapedType>(
@@ -1019,6 +1015,14 @@ ModuleImport::getConstantsToConvert(llvm::Constant *constant) {
10191015
workList.insert(constant);
10201016
while (!workList.empty()) {
10211017
llvm::Constant *current = workList.back();
1018+
// References of global objects are just pointers to the object. Avoid
1019+
// walking the elements of these here.
1020+
if (isa<llvm::GlobalObject>(current)) {
1021+
orderedSet.insert(current);
1022+
workList.pop_back();
1023+
continue;
1024+
}
1025+
10221026
// Collect all dependencies of the current constant and add them to the
10231027
// adjacency list if none has been computed before.
10241028
auto [adjacencyIt, inserted] = adjacencyLists.try_emplace(current);
@@ -1096,12 +1100,13 @@ FailureOr<Value> ModuleImport::convertConstant(llvm::Constant *constant) {
10961100
}
10971101

10981102
// Convert global variable accesses.
1099-
if (auto *globalVar = dyn_cast<llvm::GlobalVariable>(constant)) {
1103+
if (auto *globalVar = dyn_cast<llvm::GlobalObject>(constant)) {
11001104
Type type = convertType(globalVar->getType());
11011105
StringRef globalName = globalVar->getName();
11021106
FlatSymbolRefAttr symbolRef;
1107+
// Empty names are only allowed for global variables.
11031108
if (globalName.empty())
1104-
symbolRef = namelessGlobals[globalVar];
1109+
symbolRef = namelessGlobals[cast<llvm::GlobalVariable>(globalVar)];
11051110
else
11061111
symbolRef = FlatSymbolRefAttr::get(context, globalName);
11071112
return builder.create<AddressOfOp>(loc, type, symbolRef).getResult();

mlir/test/Target/LLVMIR/Import/global-variables.ll

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,25 @@
4141

4242
; // -----
4343

44+
; Verifies that converting a reference to a global does not convert the global
45+
; a second time.
46+
47+
; CHECK-LABEL: llvm.mlir.global external constant @reference
48+
; CHECK-NEXT: %[[ADDR:.*]] = llvm.mlir.addressof @simple
49+
; CHECK-NEXT: llvm.return %[[ADDR]]
50+
@reference = constant ptr @simple
51+
52+
@simple = global { ptr } { ptr null }
53+
54+
; // -----
55+
56+
; CHECK-LABEL: llvm.mlir.global external @recursive
57+
; CHECK: %[[ADDR:.*]] = llvm.mlir.addressof @recursive
58+
; CHECK: llvm.return %[[ADDR]]
59+
@recursive = global ptr @recursive
60+
61+
; // -----
62+
4463
; alignment attribute.
4564

4665
; CHECK: llvm.mlir.global private @global_int_align_32

0 commit comments

Comments
 (0)