Skip to content

Commit c6a33ff

Browse files
brendandahldschuff
authored andcommitted
[clang][CodeGen] Emit annotations for function declarations.
Previously, annotations were only emitted for function definitions. With this change annotations are also emitted for declarations. Also, emitting function annotations is now deferred until the end so that the most up to date declaration is used which will have any inherited annotations. Differential Revision: https://reviews.llvm.org/D156172/new/
1 parent 3635c74 commit c6a33ff

8 files changed

+88
-9
lines changed

clang/lib/CodeGen/CodeGenModule.cpp

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -697,6 +697,7 @@ void CodeGenModule::checkAliases() {
697697
void CodeGenModule::clear() {
698698
DeferredDeclsToEmit.clear();
699699
EmittedDeferredDecls.clear();
700+
DeferredAnnotations.clear();
700701
if (OpenMPRuntime)
701702
OpenMPRuntime->clear();
702703
}
@@ -3093,6 +3094,10 @@ void CodeGenModule::EmitVTablesOpportunistically() {
30933094
}
30943095

30953096
void CodeGenModule::EmitGlobalAnnotations() {
3097+
for (const auto& [MangledName, VD] : DeferredAnnotations)
3098+
AddGlobalAnnotations(VD, GetGlobalValue(MangledName));
3099+
DeferredAnnotations.clear();
3100+
30963101
if (Annotations.empty())
30973102
return;
30983103

@@ -3597,6 +3602,14 @@ void CodeGenModule::EmitGlobal(GlobalDecl GD) {
35973602

35983603
// Ignore declarations, they will be emitted on their first use.
35993604
if (const auto *FD = dyn_cast<FunctionDecl>(Global)) {
3605+
// Update deferred annotations with the latest declaration if the function
3606+
// function was already used or defined.
3607+
if (FD->hasAttr<AnnotateAttr>()) {
3608+
StringRef MangledName = getMangledName(GD);
3609+
if (GetGlobalValue(MangledName))
3610+
DeferredAnnotations[MangledName] = FD;
3611+
}
3612+
36003613
// Forward declarations are emitted lazily on first use.
36013614
if (!FD->doesThisDeclarationHaveABody()) {
36023615
if (!FD->doesDeclarationForceExternallyVisibleDefinition())
@@ -4370,6 +4383,11 @@ llvm::Constant *CodeGenModule::GetOrCreateLLVMFunction(
43704383
llvm::Function::Create(FTy, llvm::Function::ExternalLinkage,
43714384
Entry ? StringRef() : MangledName, &getModule());
43724385

4386+
// Store the declaration associated with this function so it is potentially
4387+
// updated by further declarations or definitions and emitted at the end.
4388+
if (D && D->hasAttr<AnnotateAttr>())
4389+
DeferredAnnotations[MangledName] = cast<ValueDecl>(D);
4390+
43734391
// If we already created a function with the same mangled name (but different
43744392
// type) before, take its name and add it to the list of functions to be
43754393
// replaced with F at the end of CodeGen.
@@ -5664,8 +5682,6 @@ void CodeGenModule::EmitGlobalFunctionDefinition(GlobalDecl GD,
56645682
AddGlobalCtor(Fn, CA->getPriority());
56655683
if (const DestructorAttr *DA = D->getAttr<DestructorAttr>())
56665684
AddGlobalDtor(Fn, DA->getPriority(), true);
5667-
if (D->hasAttr<AnnotateAttr>())
5668-
AddGlobalAnnotations(D, Fn);
56695685
if (getLangOpts().OpenMP && D->hasAttr<OMPDeclareTargetDeclAttr>())
56705686
getOpenMPRuntime().emitDeclareTargetFunction(D, GV);
56715687
}

clang/lib/CodeGen/CodeGenModule.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -431,6 +431,10 @@ class CodeGenModule : public CodeGenTypeCache {
431431
/// Global annotations.
432432
std::vector<llvm::Constant*> Annotations;
433433

434+
// Store deferred function annotations so they can be emitted at the end with
435+
// most up to date ValueDecl that will have all the inherited annotations.
436+
llvm::DenseMap<StringRef, const ValueDecl *> DeferredAnnotations;
437+
434438
/// Map used to get unique annotation strings.
435439
llvm::StringMap<llvm::Constant*> AnnotationStrings;
436440

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// RUN: %clang_cc1 %s -emit-llvm -o - | FileCheck %s
2+
3+
// Test annotation attributes are still emitted when the function is used before
4+
// it is defined with annotations.
5+
6+
void foo(void);
7+
void *xxx = (void*)foo;
8+
void __attribute__((annotate("bar"))) foo();
9+
10+
// CHECK: target triple
11+
// CHECK-DAG: private unnamed_addr constant [4 x i8] c"bar\00", section "llvm.metadata"
12+
13+
// CHECK: @llvm.global.annotations = appending global [1 x { ptr, ptr, ptr, i32, ptr }] [{
14+
// CHECK-SAME: { ptr @foo,
15+
// CHECK-SAME: }], section "llvm.metadata"
16+
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// RUN: %clang_cc1 %s -emit-llvm -o - | FileCheck %s
2+
3+
// Test annotation attributes are still emitted when the function is used before
4+
// it is defined with annotations.
5+
6+
void foo(void);
7+
void *xxx = (void*)foo;
8+
void __attribute__((annotate("bar"))) foo() {}
9+
10+
// CHECK: target triple
11+
// CHECK-DAG: private unnamed_addr constant [4 x i8] c"bar\00", section "llvm.metadata"
12+
13+
// CHECK: @llvm.global.annotations = appending global [1 x { ptr, ptr, ptr, i32, ptr }] [{
14+
// CHECK-SAME: { ptr @foo,
15+
// CHECK-SAME: }], section "llvm.metadata"
16+
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// RUN: %clang_cc1 %s -emit-llvm -o - | FileCheck %s
2+
3+
// Test annotation attributes are emitted for declarations.
4+
5+
__attribute__((annotate("bar"))) int foo();
6+
7+
int main() {
8+
return foo();
9+
}
10+
11+
// CHECK: target triple
12+
// CHECK-DAG: private unnamed_addr constant [4 x i8] c"bar\00", section "llvm.metadata"
13+
14+
// CHECK: @llvm.global.annotations = appending global [1 x { ptr, ptr, ptr, i32, ptr }] [{
15+
// CHECK-SAME: { ptr @foo,
16+
// CHECK-SAME: }], section "llvm.metadata"
17+

clang/test/CodeGen/annotations-global.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,15 @@ __attribute((address_space(1))) __attribute__((annotate("addrspace1_ann"))) char
3333
// CHECK: @llvm.global.annotations = appending global [11 x { ptr, ptr, ptr, i32, ptr }] [{
3434
// CHECK-SAME: { ptr @a.bar,
3535
// CHECK-SAME: { ptr @a.bar,
36-
// CHECK-SAME: { ptr @a,
37-
// CHECK-SAME: { ptr @a,
38-
// CHECK-SAME: { ptr @a,
39-
// CHECK-SAME: { ptr @a,
4036
// CHECK-SAME: { ptr @sfoo,
4137
// CHECK-SAME: { ptr @sfoo,
4238
// CHECK-SAME: { ptr @foo,
4339
// CHECK-SAME: { ptr @foo,
4440
// CHECK-SAME: { ptr addrspacecast (ptr addrspace(1) @addrspace1_var to ptr),
41+
// CHECK-SAME: { ptr @a,
42+
// CHECK-SAME: { ptr @a,
43+
// CHECK-SAME: { ptr @a,
44+
// CHECK-SAME: { ptr @a,
4545
// CHECK-SAME: }], section "llvm.metadata"
4646

4747
// AS1-GLOBALS: target datalayout = "{{.+}}-A5-G1"
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// RUN: %clang_cc1 %s -S -emit-llvm -triple x86_64-unknown-linux-gnu -o - | FileCheck %s
2+
3+
// Test annotation attributes on destructors doesn't crash.
4+
5+
struct k {
6+
~k() __attribute__((annotate(""))) {}
7+
};
8+
void m() { k(); }
9+
10+
// CHECK: @llvm.global.annotations = appending global [2 x { ptr, ptr, ptr, i32, ptr }] [{

clang/test/CodeGenCXX/attr-annotate.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
//CHECK: @[[STR1:.*]] = private unnamed_addr constant [{{.*}} x i8] c"{{.*}}attr-annotate.cpp\00", section "llvm.metadata"
44
//CHECK: @[[STR2:.*]] = private unnamed_addr constant [4 x i8] c"abc\00", align 1
55
//CHECK: @[[STR:.*]] = private unnamed_addr constant [5 x i8] c"test\00", section "llvm.metadata"
6-
//CHECK: @[[ARGS:.*]] = private unnamed_addr constant { i32, ptr, i32 } { i32 9, ptr @[[STR2:.*]], i32 8 }, section "llvm.metadata"
7-
//CHECK: @[[ARGS2:.*]] = private unnamed_addr constant { %struct.Struct } { %struct.Struct { ptr @_ZN1AIjLj9EE2SVE, ptr getelementptr (i8, ptr @_ZN1AIjLj9EE2SVE, i64 4) } }, section "llvm.metadata"
8-
//CHECK: @llvm.global.annotations = appending global [2 x { ptr, ptr, ptr, i32, ptr }] [{ ptr, ptr, ptr, i32, ptr } { ptr @_ZN1AIjLj9EE4testILi8EEEvv, ptr @[[STR:.*]], ptr @[[STR1:.*]], i32 {{.*}}, ptr @[[ARGS:.*]] }, { ptr, ptr, ptr, i32, ptr } { ptr @_ZN1AIjLj9EE5test2Ev, ptr @.str.6, ptr @.str.1, i32 24, ptr @[[ARGS2]] }]
6+
//CHECK: @[[ARGS:.*]] = private unnamed_addr constant { %struct.Struct } { %struct.Struct { ptr @_ZN1AIjLj9EE2SVE, ptr getelementptr (i8, ptr @_ZN1AIjLj9EE2SVE, i64 4) } }, section "llvm.metadata"
7+
//CHECK: @[[ARGS2:.*]] = private unnamed_addr constant { i32, ptr, i32 } { i32 9, ptr @[[STR2:.*]], i32 8 }, section "llvm.metadata"
8+
//CHECK: @llvm.global.annotations = appending global [2 x { ptr, ptr, ptr, i32, ptr }] [{ ptr, ptr, ptr, i32, ptr } { ptr @_ZN1AIjLj9EE5test2Ev, ptr @.str.6, ptr @.str.1, i32 24, ptr @[[ARGS]] }, { ptr, ptr, ptr, i32, ptr } { ptr @_ZN1AIjLj9EE4testILi8EEEvv, ptr @[[STR:.*]], ptr @[[STR1:.*]], i32 {{.*}}, ptr @[[ARGS2:.*]] }]
99

1010
constexpr const char* str() {
1111
return "abc";

0 commit comments

Comments
 (0)