diff --git a/clang/lib/CodeGen/CodeGenModule.cpp b/clang/lib/CodeGen/CodeGenModule.cpp --- a/clang/lib/CodeGen/CodeGenModule.cpp +++ b/clang/lib/CodeGen/CodeGenModule.cpp @@ -130,8 +130,9 @@ C.getTargetInfo().getMaxPointerWidth()); Int8PtrTy = Int8Ty->getPointerTo(0); Int8PtrPtrTy = Int8PtrTy->getPointerTo(0); - AllocaInt8PtrTy = Int8Ty->getPointerTo( - M.getDataLayout().getAllocaAddrSpace()); + const llvm::DataLayout &DL = M.getDataLayout(); + AllocaInt8PtrTy = Int8Ty->getPointerTo(DL.getAllocaAddrSpace()); + GlobalsInt8PtrTy = Int8Ty->getPointerTo(DL.getDefaultGlobalsAddressSpace()); ASTAllocaAddressSpace = getTargetCodeGenInfo().getASTAllocaAddressSpace(); RuntimeCC = getTargetCodeGenInfo().getABIInfo().getRuntimeCC(); @@ -2532,7 +2533,7 @@ llvm::Constant *CodeGenModule::EmitAnnotationArgs(const AnnotateAttr *Attr) { ArrayRef Exprs = {Attr->args_begin(), Attr->args_size()}; if (Exprs.empty()) - return llvm::ConstantPointerNull::get(Int8PtrTy); + return llvm::ConstantPointerNull::get(GlobalsInt8PtrTy); llvm::FoldingSetNodeID ID; for (Expr *E : Exprs) { @@ -2556,7 +2557,7 @@ ".args"); GV->setSection(AnnotationSection); GV->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global); - auto *Bitcasted = llvm::ConstantExpr::getBitCast(GV, Int8PtrTy); + auto *Bitcasted = llvm::ConstantExpr::getBitCast(GV, GlobalsInt8PtrTy); Lookup = Bitcasted; return Bitcasted; @@ -2571,17 +2572,19 @@ *LineNoCst = EmitAnnotationLineNo(L), *Args = EmitAnnotationArgs(AA); - llvm::Constant *ASZeroGV = GV; - if (GV->getAddressSpace() != 0) { - ASZeroGV = llvm::ConstantExpr::getAddrSpaceCast( - GV, GV->getValueType()->getPointerTo(0)); + llvm::Constant *GVInGlobalsAS = GV; + if (GV->getAddressSpace() != + getDataLayout().getDefaultGlobalsAddressSpace()) { + GVInGlobalsAS = llvm::ConstantExpr::getAddrSpaceCast( + GV, GV->getValueType()->getPointerTo( + getDataLayout().getDefaultGlobalsAddressSpace())); } // Create the ConstantStruct for the global annotation. llvm::Constant *Fields[] = { - llvm::ConstantExpr::getBitCast(ASZeroGV, Int8PtrTy), - llvm::ConstantExpr::getBitCast(AnnoGV, Int8PtrTy), - llvm::ConstantExpr::getBitCast(UnitGV, Int8PtrTy), + llvm::ConstantExpr::getBitCast(GVInGlobalsAS, GlobalsInt8PtrTy), + llvm::ConstantExpr::getBitCast(AnnoGV, GlobalsInt8PtrTy), + llvm::ConstantExpr::getBitCast(UnitGV, GlobalsInt8PtrTy), LineNoCst, Args, }; diff --git a/clang/lib/CodeGen/CodeGenTypeCache.h b/clang/lib/CodeGen/CodeGenTypeCache.h --- a/clang/lib/CodeGen/CodeGenTypeCache.h +++ b/clang/lib/CodeGen/CodeGenTypeCache.h @@ -69,6 +69,12 @@ llvm::PointerType *AllocaInt8PtrTy; }; + /// void* in default globals address space + union { + llvm::PointerType *GlobalsVoidPtrTy; + llvm::PointerType *GlobalsInt8PtrTy; + }; + /// The size and alignment of the builtin C type 'int'. This comes /// up enough in various ABI lowering tasks to be worth pre-computing. union { diff --git a/clang/test/CodeGen/annotations-global.c b/clang/test/CodeGen/annotations-global.c --- a/clang/test/CodeGen/annotations-global.c +++ b/clang/test/CodeGen/annotations-global.c @@ -4,6 +4,7 @@ // RUN: FileCheck --check-prefix=BAR %s < %t1 // RUN: FileCheck --check-prefix=FOOS %s < %t1 // RUN: FileCheck --check-prefix=ADDRSPACE %s < %t1 +// RUN: %clang_cc1 %s -triple r600 -emit-llvm -o - | FileCheck %s --check-prefix AS1-GLOBALS // END. static __attribute((annotate("sfoo_0"))) __attribute((annotate("sfoo_1"))) char sfoo; @@ -45,3 +46,8 @@ // ADDRSPACE: target triple // ADDRSPACE: @llvm.global.annotations = appending global {{.*}} addrspacecast (i8 addrspace(1)* @addrspace1_var to i8*), {{.*}} + +// AS1-GLOBALS: target datalayout = "{{.+}}-A5-G1" +// AS1-GLOBALS: @llvm.global.annotations = appending addrspace(1) global [11 x { i8 addrspace(1)*, i8 addrspace(1)*, i8 addrspace(1)*, i32, i8 addrspace(1)* }] +// AS1-GLOBALS-SAME: { i8 addrspace(1)* @a.bar, +// AS1-GLOBALS-SAME: { i8 addrspace(1)* @addrspace1_var,