yaxunl created this revision.
yaxunl added a reviewer: rjmccall.

Some targets have constant address space (e.g. amdgcn). For them string literal 
should be
emitted in constant address space then casted to default address space.


https://reviews.llvm.org/D46643

Files:
  lib/CodeGen/CGDecl.cpp
  lib/CodeGen/CodeGenModule.cpp
  test/CodeGenCXX/amdgcn-string-literal.cpp

Index: test/CodeGenCXX/amdgcn-string-literal.cpp
===================================================================
--- /dev/null
+++ test/CodeGenCXX/amdgcn-string-literal.cpp
@@ -0,0 +1,28 @@
+// RUN: %clang_cc1 -triple amdgcn-amd-amdhsa -emit-llvm %s -o - | FileCheck %s
+
+// CHECK: @.str = private unnamed_addr addrspace(4) constant [6 x i8] c"g_str\00", align 1
+// CHECK: @g_str = addrspace(1) global i8* addrspacecast (i8 addrspace(4)* getelementptr inbounds ([6 x i8], [6 x i8] addrspace(4)* @.str, i32 0, i32 0) to i8*), align 8
+// CHECK: @g_array = addrspace(1) global [8 x i8] c"g_array\00", align 1
+// CHECK: @.str.1 = private unnamed_addr addrspace(4) constant [6 x i8] c"l_str\00", align 1
+// CHECK: @_ZZ1fvE7l_array = private unnamed_addr addrspace(4) constant [8 x i8] c"l_array\00", align 1
+
+const char* g_str = "g_str";
+char g_array[] = "g_array";
+
+void g(const char* p);
+
+// CHECK-LABEL: define void @_Z1fv()
+void f() {
+  const char* l_str = "l_str";
+  
+  // CHECK: call void @llvm.memcpy.p5i8.p4i8.i64
+  char l_array[] = "l_array";
+
+  g(g_str);
+  g(g_array);
+  g(l_str);
+  g(l_array);
+
+  const char* p = g_str;
+  g(p);
+}
\ No newline at end of file
Index: lib/CodeGen/CodeGenModule.cpp
===================================================================
--- lib/CodeGen/CodeGenModule.cpp
+++ lib/CodeGen/CodeGenModule.cpp
@@ -4032,6 +4032,9 @@
   unsigned AddrSpace = 0;
   if (CGM.getLangOpts().OpenCL)
     AddrSpace = CGM.getContext().getTargetAddressSpace(LangAS::opencl_constant);
+  else if (auto AS = CGM.getTarget().getConstantAddressSpace()) {
+    AddrSpace = CGM.getContext().getTargetAddressSpace(AS.getValue());
+  }
 
   llvm::Module &M = CGM.getModule();
   // Create a global variable for this string
@@ -4093,7 +4096,19 @@
 
   SanitizerMD->reportGlobalToASan(GV, S->getStrTokenLoc(0), "<string literal>",
                                   QualType());
-  return ConstantAddress(GV, Alignment);
+
+  llvm::Constant *Cast = GV;
+  if (!getLangOpts().OpenCL) {
+    if (auto AS = getTarget().getConstantAddressSpace()) {
+      if (AS != LangAS::Default)
+        Cast = getTargetCodeGenInfo().performAddrSpaceCast(
+            *this, GV, AS.getValue(), LangAS::Default,
+            GV->getValueType()->getPointerTo(
+                getContext().getTargetAddressSpace(LangAS::Default)));
+    }
+  }
+
+  return ConstantAddress(Cast, Alignment);
 }
 
 /// GetAddrOfConstantStringFromObjCEncode - Return a pointer to a constant
@@ -4137,7 +4152,17 @@
                                   GlobalName, Alignment);
   if (Entry)
     *Entry = GV;
-  return ConstantAddress(GV, Alignment);
+  llvm::Constant *Cast = GV;
+  if (!getLangOpts().OpenCL) {
+    if (auto AS = getTarget().getConstantAddressSpace()) {
+      if (AS != LangAS::Default)
+        Cast = getTargetCodeGenInfo().performAddrSpaceCast(
+            *this, GV, AS.getValue(), LangAS::Default,
+            GV->getValueType()->getPointerTo(
+                getContext().getTargetAddressSpace(LangAS::Default)));
+    }
+  }
+  return ConstantAddress(Cast, Alignment);
 }
 
 ConstantAddress CodeGenModule::GetAddrOfGlobalTemporary(
Index: lib/CodeGen/CGDecl.cpp
===================================================================
--- lib/CodeGen/CGDecl.cpp
+++ lib/CodeGen/CGDecl.cpp
@@ -1371,7 +1371,8 @@
 
   llvm::Type *BP = AllocaInt8PtrTy;
   if (Loc.getType() != BP)
-    Loc = Builder.CreateBitCast(Loc, BP);
+    Loc = Address(EmitCastToVoidPtrInAllocaAddrSpace(Loc.getPointer()),
+                  Loc.getAlignment());
 
   // If the initializer is all or mostly zeros, codegen with memset then do
   // a few stores afterward.
@@ -1394,7 +1395,11 @@
     if (getLangOpts().OpenCL) {
       AS = CGM.getContext().getTargetAddressSpace(LangAS::opencl_constant);
       BP = llvm::PointerType::getInt8PtrTy(getLLVMContext(), AS);
+    } else if (auto OptionalAS = CGM.getTarget().getConstantAddressSpace()) {
+      AS = CGM.getContext().getTargetAddressSpace(OptionalAS.getValue());
+      BP = llvm::PointerType::getInt8PtrTy(getLLVMContext(), AS);
     }
+
     llvm::GlobalVariable *GV =
       new llvm::GlobalVariable(CGM.getModule(), constant->getType(), true,
                                llvm::GlobalValue::PrivateLinkage,
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to