brendandahl created this revision.
Herald added a project: All.
brendandahl requested review of this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.

Previously, annotations were only emitted for function definitions. With
this change annotations are also emitted for declarations. Emitting for
both also more closely matches the behavior of regular function attributes.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D156172

Files:
  clang/lib/CodeGen/CodeGenModule.cpp
  clang/test/CodeGen/annotations-declaration.c
  clang/test/CodeGen/annotations-global.c
  clang/test/CodeGenCXX/attr-annotate.cpp

Index: clang/test/CodeGenCXX/attr-annotate.cpp
===================================================================
--- clang/test/CodeGenCXX/attr-annotate.cpp
+++ clang/test/CodeGenCXX/attr-annotate.cpp
@@ -1,11 +1,11 @@
 // RUN: %clang_cc1 %s -S -emit-llvm -triple x86_64-unknown-linux-gnu -o - | FileCheck %s
 
+//CHECK: @[[STR:.*]] = private unnamed_addr constant [5 x i8] c"test\00", section "llvm.metadata"
 //CHECK: @[[STR1:.*]] = private unnamed_addr constant [{{.*}} x i8] c"{{.*}}attr-annotate.cpp\00", section "llvm.metadata"
 //CHECK: @[[STR2:.*]] = private unnamed_addr constant [4 x i8] c"abc\00", align 1
-//CHECK: @[[STR:.*]] = private unnamed_addr constant [5 x i8] c"test\00", section "llvm.metadata"
 //CHECK: @[[ARGS:.*]] = private unnamed_addr constant { i32, ptr, i32 } { i32 9, ptr @[[STR2:.*]], i32 8 }, section "llvm.metadata"
-//CHECK: @[[ARGS2:.*]] = private unnamed_addr constant { %struct.Struct } { %struct.Struct { ptr @_ZN1AIjLj9EE2SVE, ptr getelementptr (i8, ptr @_ZN1AIjLj9EE2SVE, i64 4) } }, section "llvm.metadata"
-//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]] }]
+//CHECK: @[[ARGS3:.*]] = private unnamed_addr constant { %struct.Struct } { %struct.Struct { ptr @_ZN1AIjLj9EE2SVE, ptr getelementptr (i8, ptr @_ZN1AIjLj9EE2SVE, i64 4) } }, section "llvm.metadata"
+//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, ptr @.str.1, i32 24, ptr @[[ARGS3]] }]
 
 constexpr const char* str() {
   return "abc";
@@ -51,12 +51,12 @@
 // CHECK-NEXT:    store ptr [[ARGV:%.*]], ptr [[ARGV_ADDR]], align 8
 // CHECK-NEXT:    [[TMP0:%.*]] = load i32, ptr [[ARGC_ADDR]], align 4
 // CHECK-NEXT:    [[V:%.*]] = getelementptr inbounds %"struct.B<int, 7>::foo", ptr [[F]], i32 0, i32 0
-// CHECK-NEXT:    [[TMP2:%.*]] = call ptr @llvm.ptr.annotation.p0.p0(ptr [[V]], ptr @.str, ptr @.str.1, i32 {{.*}}, ptr @.args)
-// CHECK-NEXT:    [[TMP5:%.*]] = call ptr @llvm.ptr.annotation.p0.p0(ptr [[TMP2]], ptr @.str.3, ptr @.str.1, i32 {{.*}}, ptr @.args.4)
+// CHECK-NEXT:    [[TMP2:%.*]] = call ptr @llvm.ptr.annotation.p0.p0(ptr [[V]], ptr @.str.4, ptr @.str.1, i32 {{.*}}, ptr @.args.5)
+// CHECK-NEXT:    [[TMP5:%.*]] = call ptr @llvm.ptr.annotation.p0.p0(ptr [[TMP2]], ptr @.str.6, ptr @.str.1, i32 {{.*}}, ptr @.args.7)
 // CHECK-NEXT:    store i32 [[TMP0]], ptr [[TMP5]], align 4
 // CHECK-NEXT:    [[TMP7:%.*]] = load i32, ptr [[ARGC_ADDR]], align 4
-// CHECK-NEXT:    [[TMP8:%.*]] = call ptr @llvm.ptr.annotation.p0.p0(ptr @_ZL2gf, ptr @.str, ptr @.str.1, i32 {{.*}}, ptr @.args.5)
-// CHECK-NEXT:    [[TMP11:%.*]] = call ptr @llvm.ptr.annotation.p0.p0(ptr [[TMP8]], ptr @.str.3, ptr @.str.1, i32 {{.*}}, ptr @.args.4)
+// CHECK-NEXT:    [[TMP8:%.*]] = call ptr @llvm.ptr.annotation.p0.p0(ptr @_ZL2gf, ptr @.str.4, ptr @.str.1, i32 {{.*}}, ptr @.args.8)
+// CHECK-NEXT:    [[TMP11:%.*]] = call ptr @llvm.ptr.annotation.p0.p0(ptr [[TMP8]], ptr @.str.6, ptr @.str.1, i32 {{.*}}, ptr @.args.7)
 // CHECK-NEXT:    store i32 [[TMP7]], ptr [[TMP11]], align 4
 // CHECK-NEXT:    ret i32 0
 //
Index: clang/test/CodeGen/annotations-global.c
===================================================================
--- clang/test/CodeGen/annotations-global.c
+++ clang/test/CodeGen/annotations-global.c
@@ -31,12 +31,12 @@
 // CHECK-DAG: private unnamed_addr constant [15 x i8] c"addrspace1_ann\00", section "llvm.metadata"
 
 // CHECK: @llvm.global.annotations = appending global [11 x { ptr, ptr, ptr, i32, ptr }] [{
-// CHECK-SAME: { ptr @a.bar,
-// CHECK-SAME: { ptr @a.bar,
 // CHECK-SAME: { ptr @a,
 // CHECK-SAME: { ptr @a,
 // CHECK-SAME: { ptr @a,
 // CHECK-SAME: { ptr @a,
+// CHECK-SAME: { ptr @a.bar,
+// CHECK-SAME: { ptr @a.bar,
 // CHECK-SAME: { ptr @sfoo,
 // CHECK-SAME: { ptr @sfoo,
 // CHECK-SAME: { ptr @foo,
Index: clang/test/CodeGen/annotations-declaration.c
===================================================================
--- /dev/null
+++ clang/test/CodeGen/annotations-declaration.c
@@ -0,0 +1,15 @@
+// RUN: %clang_cc1 %s -emit-llvm -o - | FileCheck %s
+
+__attribute__((annotate("bar"))) int foo();
+
+int main() {
+  return foo();
+}
+
+// CHECK: target triple
+// CHECK-DAG: private unnamed_addr constant [4 x i8] c"bar\00", section "llvm.metadata"
+
+// CHECK: @llvm.global.annotations = appending global [1 x { ptr, ptr, ptr, i32, ptr }] [{
+// CHECK-SAME: { ptr @foo,
+// CHECK-SAME: }], section "llvm.metadata"
+
Index: clang/lib/CodeGen/CodeGenModule.cpp
===================================================================
--- clang/lib/CodeGen/CodeGenModule.cpp
+++ clang/lib/CodeGen/CodeGenModule.cpp
@@ -4372,8 +4372,13 @@
   }
 
   assert(F->getName() == MangledName && "name was uniqued!");
-  if (D)
+  if (D) {
     SetFunctionAttributes(GD, F, IsIncompleteFunction, IsThunk);
+    if (D->hasAttr<AnnotateAttr>()) {
+      const auto *FD = cast<FunctionDecl>(D);
+      AddGlobalAnnotations(FD, F);
+    }
+  }
   if (ExtraAttrs.hasFnAttrs()) {
     llvm::AttrBuilder B(F->getContext(), ExtraAttrs.getFnAttrs());
     F->addFnAttrs(B);
@@ -5666,8 +5671,6 @@
     AddGlobalCtor(Fn, CA->getPriority());
   if (const DestructorAttr *DA = D->getAttr<DestructorAttr>())
     AddGlobalDtor(Fn, DA->getPriority(), true);
-  if (D->hasAttr<AnnotateAttr>())
-    AddGlobalAnnotations(D, Fn);
 }
 
 void CodeGenModule::EmitAliasDefinition(GlobalDecl GD) {
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to