Fix __attribute__((annotate("")) with non-zero globals AS

The existing code attempting to bitcast from a value in the default globals AS
to i8 addrspace(0)* was triggering an assertion failure in our downstream fork.
I found this while compiling poppler for CHERI-RISC-V (we use AS200 for all
globals). The test case uses AMDGPU since that is one of the in-tree targets
with a non-zero default globals address space.
The new test previously triggered a "Invalid constantexpr bitcast!" assertion
and now correctly generates code with addrspace(1) pointers.

Reviewed By: rjmccall

Differential Revision: https://reviews.llvm.org/D105972
This commit is contained in:
Alex Richardson 2021-08-26 09:50:05 +01:00
parent bf66b0eefc
commit 7cab90a7b1
3 changed files with 26 additions and 11 deletions

View File

@ -130,8 +130,9 @@ CodeGenModule::CodeGenModule(ASTContext &C, const HeaderSearchOptions &HSO,
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::EmitAnnotationLineNo(SourceLocation L) {
llvm::Constant *CodeGenModule::EmitAnnotationArgs(const AnnotateAttr *Attr) {
ArrayRef<Expr *> 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 @@ llvm::Constant *CodeGenModule::EmitAnnotationArgs(const AnnotateAttr *Attr) {
".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 @@ llvm::Constant *CodeGenModule::EmitAnnotateAttr(llvm::GlobalValue *GV,
*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,
};

View File

@ -69,6 +69,12 @@ struct CodeGenTypeCache {
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 {

View File

@ -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 @@ __attribute((address_space(1))) __attribute__((annotate("addrspace1_ann"))) char
// 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,