[llvm] d420110 - [NVPTX] Fix constant expression initializers for global variables
Andrew Savonichev via llvm-commits
llvm-commits at lists.llvm.org
Mon Oct 3 14:30:49 PDT 2022
Author: Andrew Savonichev
Date: 2022-10-04T00:29:42+03:00
New Revision: d420110a1e9d620632d47366d569d234026b9e0e
URL: https://github.com/llvm/llvm-project/commit/d420110a1e9d620632d47366d569d234026b9e0e
DIFF: https://github.com/llvm/llvm-project/commit/d420110a1e9d620632d47366d569d234026b9e0e.diff
LOG: [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
Added:
Modified:
llvm/lib/Target/NVPTX/NVPTXAsmPrinter.cpp
llvm/test/CodeGen/NVPTX/addrspacecast-gvar.ll
Removed:
################################################################################
diff --git a/llvm/lib/Target/NVPTX/NVPTXAsmPrinter.cpp b/llvm/lib/Target/NVPTX/NVPTXAsmPrinter.cpp
index 7fada302e05ff..9eacf6b475f84 100644
--- a/llvm/lib/Target/NVPTX/NVPTXAsmPrinter.cpp
+++ b/llvm/lib/Target/NVPTX/NVPTXAsmPrinter.cpp
@@ -1780,25 +1780,9 @@ void NVPTXAsmPrinter::printScalarConstant(const Constant *CPV, raw_ostream &O) {
return;
}
if (const ConstantExpr *Cexpr = dyn_cast<ConstantExpr>(CPV)) {
- const Value *v = Cexpr->stripPointerCasts();
- PointerType *PTy = dyn_cast<PointerType>(Cexpr->getType());
- bool IsNonGenericPointer = false;
- if (PTy && PTy->getAddressSpace() != 0) {
- IsNonGenericPointer = true;
- }
- if (const GlobalValue *GVar = dyn_cast<GlobalValue>(v)) {
- if (EmitGeneric && !isa<Function>(v) && !IsNonGenericPointer) {
- O << "generic(";
- getSymbol(GVar)->print(O, MAI);
- O << ")";
- } else {
- getSymbol(GVar)->print(O, MAI);
- }
- return;
- } else {
- lowerConstant(CPV)->print(O, MAI);
- return;
- }
+ const MCExpr *E = lowerConstantForGV(cast<Constant>(Cexpr), false);
+ printMCExpr(*E, O);
+ return;
}
llvm_unreachable("Not scalar type found in printScalarConstant()");
}
diff --git a/llvm/test/CodeGen/NVPTX/addrspacecast-gvar.ll b/llvm/test/CodeGen/NVPTX/addrspacecast-gvar.ll
index d0321d7228835..99971557db436 100644
--- a/llvm/test/CodeGen/NVPTX/addrspacecast-gvar.ll
+++ b/llvm/test/CodeGen/NVPTX/addrspacecast-gvar.ll
@@ -2,13 +2,48 @@
; RUN: %if ptxas %{ llc < %s -march=nvptx -mcpu=sm_20 | %ptxas-verify %}
; CHECK: .visible .global .align 4 .u32 g = 42;
+; CHECK: .visible .global .align 1 .b8 ga[4] = {0, 1, 2, 3};
; CHECK: .visible .global .align 4 .u32 g2 = generic(g);
; CHECK: .visible .global .align 4 .u32 g3 = g;
; CHECK: .visible .global .align 8 .u32 g4[2] = {0, generic(g)};
; CHECK: .visible .global .align 8 .u32 g5[2] = {0, generic(g)+8};
@g = addrspace(1) global i32 42
+ at ga = addrspace(1) global [4 x i8] c"\00\01\02\03"
@g2 = addrspace(1) global i32* addrspacecast (i32 addrspace(1)* @g to i32*)
@g3 = addrspace(1) global i32 addrspace(1)* @g
@g4 = constant {i32*, i32*} {i32* null, i32* addrspacecast (i32 addrspace(1)* @g to i32*)}
@g5 = constant {i32*, i32*} {i32* null, i32* addrspacecast (i32 addrspace(1)* getelementptr (i32, i32 addrspace(1)* @g, i32 2) to i32*)}
+
+; CHECK: .visible .global .align 4 .u32 g6 = generic(ga)+2;
+ at g6 = addrspace(1) global i8* getelementptr inbounds (
+ [4 x i8], [4 x i8]* addrspacecast ([4 x i8] addrspace(1)* @ga to [4 x i8]*),
+ i32 0, i32 2
+)
+
+; CHECK: .visible .global .align 4 .u32 g7 = generic(g);
+ at g7 = addrspace(1) global i8* addrspacecast (
+ i8 addrspace(1)* bitcast (i32 addrspace(1)* @g to i8 addrspace(1)*)
+ to i8*
+)
+
+; CHECK: .visible .global .align 4 .u32 g8[2] = {0, g};
+ at g8 = addrspace(1) global [2 x i32 addrspace(1)*] [i32 addrspace(1)* null, i32 addrspace(1)* @g]
+
+; CHECK: .visible .global .align 4 .u32 g9[2] = {0, generic(g)};
+ at g9 = addrspace(1) global [2 x i32*] [
+ i32* null,
+ i32* addrspacecast (i32 addrspace(1)* @g to i32*)
+]
+
+; CHECK: .visible .global .align 4 .u32 g10[2] = {0, g};
+ at g10 = addrspace(1) global [2 x i8 addrspace(1)*] [
+ i8 addrspace(1)* null,
+ i8 addrspace(1)* bitcast (i32 addrspace(1)* @g to i8 addrspace(1)*)
+]
+
+; CHECK: .visible .global .align 4 .u32 g11[2] = {0, generic(g)};
+ at g11 = addrspace(1) global [2 x i8*] [
+ i8* null,
+ i8* bitcast (i32* addrspacecast (i32 addrspace(1)* @g to i32*) to i8*)
+]
More information about the llvm-commits
mailing list