Skip to content

Commit f21c081

Browse files
committed
CodeGen: Allow annotations on globals in non-zero address space
Summary: Attribute annotations are recorded in a special global composite variable that points to annotation strings and the annotated objects. As a restriction of the LLVM IR type system, those pointers are all pointers to address space 0, so let's insert an addrspacecast when the annotated global is in a non-0 address space. Since this addrspacecast is only reachable from the global annotations object, this should allow us to represent annotations on all globals regardless of which addrspacecasts are usually legal for the target. Reviewers: rjmccall Subscribers: cfe-commits Tags: #clang Differential Revision: https://reviews.llvm.org/D71208
1 parent 5ceb36b commit f21c081

File tree

2 files changed

+17
-5
lines changed

2 files changed

+17
-5
lines changed

clang/lib/CodeGen/CodeGenModule.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2212,9 +2212,15 @@ llvm::Constant *CodeGenModule::EmitAnnotateAttr(llvm::GlobalValue *GV,
22122212
*UnitGV = EmitAnnotationUnit(L),
22132213
*LineNoCst = EmitAnnotationLineNo(L);
22142214

2215+
llvm::Constant *ASZeroGV = GV;
2216+
if (GV->getAddressSpace() != 0) {
2217+
ASZeroGV = llvm::ConstantExpr::getAddrSpaceCast(
2218+
GV, GV->getValueType()->getPointerTo(0));
2219+
}
2220+
22152221
// Create the ConstantStruct for the global annotation.
22162222
llvm::Constant *Fields[4] = {
2217-
llvm::ConstantExpr::getBitCast(GV, Int8PtrTy),
2223+
llvm::ConstantExpr::getBitCast(ASZeroGV, Int8PtrTy),
22182224
llvm::ConstantExpr::getBitCast(AnnoGV, Int8PtrTy),
22192225
llvm::ConstantExpr::getBitCast(UnitGV, Int8PtrTy),
22202226
LineNoCst

clang/test/CodeGen/annotations-global.c

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// RUN: FileCheck --check-prefix=A %s < %t1
44
// RUN: FileCheck --check-prefix=BAR %s < %t1
55
// RUN: FileCheck --check-prefix=FOOS %s < %t1
6+
// RUN: FileCheck --check-prefix=ADDRSPACE %s < %t1
67
// END.
78

89
static __attribute((annotate("sfoo_0"))) __attribute((annotate("sfoo_1"))) char sfoo;
@@ -14,28 +15,33 @@ void __attribute((annotate("ann_a_0"))) __attribute((annotate("ann_a_1"))) a(cha
1415
sfoo = 0;
1516
}
1617

18+
__attribute((address_space(1))) __attribute__((annotate("addrspace1_ann"))) char addrspace1_var;
19+
1720
// FOOS: target triple
1821
// FOOS: private unnamed_addr constant [7 x i8] c"sfoo_{{.}}\00", section "llvm.metadata"
1922
// FOOS: private unnamed_addr constant [7 x i8] c"sfoo_{{.}}\00", section "llvm.metadata"
2023
// FOOS-NOT: sfoo_
21-
// FOOS: @llvm.global.annotations = appending global [10 x { i8*, i8*, i8*, i32 }] {{.*}}i8* @sfoo{{.*}}i8* @sfoo{{.*}}, section "llvm.metadata"
24+
// FOOS: @llvm.global.annotations = appending global [11 x { i8*, i8*, i8*, i32 }] {{.*}}i8* @sfoo{{.*}}i8* @sfoo{{.*}}, section "llvm.metadata"
2225

2326
// FOO: target triple
2427
// FOO: private unnamed_addr constant [6 x i8] c"foo_{{.}}\00", section "llvm.metadata"
2528
// FOO: private unnamed_addr constant [6 x i8] c"foo_{{.}}\00", section "llvm.metadata"
2629
// FOO-NOT: foo_
27-
// FOO: @llvm.global.annotations = appending global [10 x { i8*, i8*, i8*, i32 }] {{.*}}i8* @foo{{.*}}i8* @foo{{.*}}, section "llvm.metadata"
30+
// FOO: @llvm.global.annotations = appending global [11 x { i8*, i8*, i8*, i32 }] {{.*}}i8* @foo{{.*}}i8* @foo{{.*}}, section "llvm.metadata"
2831

2932
// A: target triple
3033
// A: private unnamed_addr constant [8 x i8] c"ann_a_{{.}}\00", section "llvm.metadata"
3134
// A: private unnamed_addr constant [8 x i8] c"ann_a_{{.}}\00", section "llvm.metadata"
3235
// A: private unnamed_addr constant [8 x i8] c"ann_a_{{.}}\00", section "llvm.metadata"
3336
// A: private unnamed_addr constant [8 x i8] c"ann_a_{{.}}\00", section "llvm.metadata"
3437
// A-NOT: ann_a_
35-
// A: @llvm.global.annotations = appending global [10 x { i8*, i8*, i8*, i32 }] {{.*}}i8* bitcast (void (i8*)* @a to i8*){{.*}}i8* bitcast (void (i8*)* @a to i8*){{.*}}i8* bitcast (void (i8*)* @a to i8*){{.*}}i8* bitcast (void (i8*)* @a to i8*){{.*}}, section "llvm.metadata"
38+
// A: @llvm.global.annotations = appending global [11 x { i8*, i8*, i8*, i32 }] {{.*}}i8* bitcast (void (i8*)* @a to i8*){{.*}}i8* bitcast (void (i8*)* @a to i8*){{.*}}i8* bitcast (void (i8*)* @a to i8*){{.*}}i8* bitcast (void (i8*)* @a to i8*){{.*}}, section "llvm.metadata"
3639

3740
// BAR: target triple
3841
// BAR: private unnamed_addr constant [6 x i8] c"bar_{{.}}\00", section "llvm.metadata"
3942
// BAR: private unnamed_addr constant [6 x i8] c"bar_{{.}}\00", section "llvm.metadata"
4043
// BAR-NOT: bar_
41-
// BAR: @llvm.global.annotations = appending global [10 x { i8*, i8*, i8*, i32 }] {{.*}}i8* @a.bar{{.*}}i8* @a.bar{{.*}}, section "llvm.metadata"
44+
// BAR: @llvm.global.annotations = appending global [11 x { i8*, i8*, i8*, i32 }] {{.*}}i8* @a.bar{{.*}}i8* @a.bar{{.*}}, section "llvm.metadata"
45+
46+
// ADDRSPACE: target triple
47+
// ADDRSPACE: @llvm.global.annotations = appending global {{.*}} addrspacecast (i8 addrspace(1)* @addrspace1_var to i8*), {{.*}}

0 commit comments

Comments
 (0)