Skip to content

Commit d420110

Browse files
committed
[NVPTX] Fix constant expression initializers for global variables
Before this patch the code in printScalarConstant was unable to handle nested constant expressions like (gep (addrspacecast ptr)) and crashed with: LLVM ERROR: Unsupported expression in static initializer: addrspacecast ([4 x i8] addrspace(1)* @ga to [4 x i8]*) We can use lowerConstantForGV instead which is a customized version of lowerConstant that supports generic() and nested expressions. Differential Revision: https://reviews.llvm.org/D127878
1 parent 5585d99 commit d420110

File tree

2 files changed

+38
-19
lines changed

2 files changed

+38
-19
lines changed

llvm/lib/Target/NVPTX/NVPTXAsmPrinter.cpp

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1780,25 +1780,9 @@ void NVPTXAsmPrinter::printScalarConstant(const Constant *CPV, raw_ostream &O) {
17801780
return;
17811781
}
17821782
if (const ConstantExpr *Cexpr = dyn_cast<ConstantExpr>(CPV)) {
1783-
const Value *v = Cexpr->stripPointerCasts();
1784-
PointerType *PTy = dyn_cast<PointerType>(Cexpr->getType());
1785-
bool IsNonGenericPointer = false;
1786-
if (PTy && PTy->getAddressSpace() != 0) {
1787-
IsNonGenericPointer = true;
1788-
}
1789-
if (const GlobalValue *GVar = dyn_cast<GlobalValue>(v)) {
1790-
if (EmitGeneric && !isa<Function>(v) && !IsNonGenericPointer) {
1791-
O << "generic(";
1792-
getSymbol(GVar)->print(O, MAI);
1793-
O << ")";
1794-
} else {
1795-
getSymbol(GVar)->print(O, MAI);
1796-
}
1797-
return;
1798-
} else {
1799-
lowerConstant(CPV)->print(O, MAI);
1800-
return;
1801-
}
1783+
const MCExpr *E = lowerConstantForGV(cast<Constant>(Cexpr), false);
1784+
printMCExpr(*E, O);
1785+
return;
18021786
}
18031787
llvm_unreachable("Not scalar type found in printScalarConstant()");
18041788
}

llvm/test/CodeGen/NVPTX/addrspacecast-gvar.ll

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,48 @@
22
; RUN: %if ptxas %{ llc < %s -march=nvptx -mcpu=sm_20 | %ptxas-verify %}
33

44
; CHECK: .visible .global .align 4 .u32 g = 42;
5+
; CHECK: .visible .global .align 1 .b8 ga[4] = {0, 1, 2, 3};
56
; CHECK: .visible .global .align 4 .u32 g2 = generic(g);
67
; CHECK: .visible .global .align 4 .u32 g3 = g;
78
; CHECK: .visible .global .align 8 .u32 g4[2] = {0, generic(g)};
89
; CHECK: .visible .global .align 8 .u32 g5[2] = {0, generic(g)+8};
910

1011
@g = addrspace(1) global i32 42
12+
@ga = addrspace(1) global [4 x i8] c"\00\01\02\03"
1113
@g2 = addrspace(1) global i32* addrspacecast (i32 addrspace(1)* @g to i32*)
1214
@g3 = addrspace(1) global i32 addrspace(1)* @g
1315
@g4 = constant {i32*, i32*} {i32* null, i32* addrspacecast (i32 addrspace(1)* @g to i32*)}
1416
@g5 = constant {i32*, i32*} {i32* null, i32* addrspacecast (i32 addrspace(1)* getelementptr (i32, i32 addrspace(1)* @g, i32 2) to i32*)}
17+
18+
; CHECK: .visible .global .align 4 .u32 g6 = generic(ga)+2;
19+
@g6 = addrspace(1) global i8* getelementptr inbounds (
20+
[4 x i8], [4 x i8]* addrspacecast ([4 x i8] addrspace(1)* @ga to [4 x i8]*),
21+
i32 0, i32 2
22+
)
23+
24+
; CHECK: .visible .global .align 4 .u32 g7 = generic(g);
25+
@g7 = addrspace(1) global i8* addrspacecast (
26+
i8 addrspace(1)* bitcast (i32 addrspace(1)* @g to i8 addrspace(1)*)
27+
to i8*
28+
)
29+
30+
; CHECK: .visible .global .align 4 .u32 g8[2] = {0, g};
31+
@g8 = addrspace(1) global [2 x i32 addrspace(1)*] [i32 addrspace(1)* null, i32 addrspace(1)* @g]
32+
33+
; CHECK: .visible .global .align 4 .u32 g9[2] = {0, generic(g)};
34+
@g9 = addrspace(1) global [2 x i32*] [
35+
i32* null,
36+
i32* addrspacecast (i32 addrspace(1)* @g to i32*)
37+
]
38+
39+
; CHECK: .visible .global .align 4 .u32 g10[2] = {0, g};
40+
@g10 = addrspace(1) global [2 x i8 addrspace(1)*] [
41+
i8 addrspace(1)* null,
42+
i8 addrspace(1)* bitcast (i32 addrspace(1)* @g to i8 addrspace(1)*)
43+
]
44+
45+
; CHECK: .visible .global .align 4 .u32 g11[2] = {0, generic(g)};
46+
@g11 = addrspace(1) global [2 x i8*] [
47+
i8* null,
48+
i8* bitcast (i32* addrspacecast (i32 addrspace(1)* @g to i32*) to i8*)
49+
]

0 commit comments

Comments
 (0)