[NVPTX] Fix constant expression initializers for global variables
authorAndrew Savonichev <andrew.savonichev@gmail.com>
Mon, 13 Jun 2022 16:05:50 +0000 (19:05 +0300)
committerAndrew Savonichev <andrew.savonichev@gmail.com>
Mon, 3 Oct 2022 21:29:42 +0000 (00:29 +0300)
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

llvm/lib/Target/NVPTX/NVPTXAsmPrinter.cpp
llvm/test/CodeGen/NVPTX/addrspacecast-gvar.ll

index 7fada30..9eacf6b 100644 (file)
@@ -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()");
 }
index d0321d7..9997155 100644 (file)
@@ -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
+@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;
+@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);
+@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};
+@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)};
+@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};
+@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)};
+@g11 = addrspace(1) global [2 x i8*] [
+  i8* null,
+  i8* bitcast (i32* addrspacecast (i32 addrspace(1)* @g to i32*) to i8*)
+]