[clang] [clang][CodeGen] Emit annotations for function declarations. (PR #66716)

2023-11-29 Thread Eli Friedman via cfe-commits

https://github.com/efriedma-quic closed 
https://github.com/llvm/llvm-project/pull/66716
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang][CodeGen] Emit annotations for function declarations. (PR #66716)

2023-11-29 Thread Brendan Dahl via cfe-commits

brendandahl wrote:

@efriedma-quic missed your comment. I don't have commit access. Can you merge 
for me?

Thanks!

https://github.com/llvm/llvm-project/pull/66716
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang][CodeGen] Emit annotations for function declarations. (PR #66716)

2023-10-31 Thread Eli Friedman via cfe-commits

efriedma-quic wrote:

(Not sure if you have commit access; let me know if you want me to merge for 
you.)

https://github.com/llvm/llvm-project/pull/66716
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang][CodeGen] Emit annotations for function declarations. (PR #66716)

2023-10-31 Thread Eli Friedman via cfe-commits

https://github.com/efriedma-quic approved this pull request.

LGTM

https://github.com/llvm/llvm-project/pull/66716
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang][CodeGen] Emit annotations for function declarations. (PR #66716)

2023-10-17 Thread Brendan Dahl via cfe-commits

brendandahl wrote:

@AaronBallman or @efriedma-quic ping are you able to add reviewers?

https://github.com/llvm/llvm-project/pull/66716
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang][CodeGen] Emit annotations for function declarations. (PR #66716)

2023-09-19 Thread Brendan Dahl via cfe-commits

brendandahl wrote:

@efriedma-quic could you re-review? The only changes were 
https://github.com/llvm/llvm-project/pull/66716/files#diff-e724febedab9c1a2832bf2056d208ff02ddcb2e6f90b5a653afc9b19ac78a5d7R3098-R3100

https://github.com/llvm/llvm-project/pull/66716
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang][CodeGen] Emit annotations for function declarations. (PR #66716)

2023-09-18 Thread Brendan Dahl via cfe-commits

brendandahl wrote:

This is relanding the patch from [here](https://reviews.llvm.org/D156172). It 
fixes the [backout 
failure](https://reviews.llvm.org/rG88b7e06dcf9723d0869b0c6bee030b4140e4366d) 
and adds a test for it.

https://github.com/llvm/llvm-project/pull/66716
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang][CodeGen] Emit annotations for function declarations. (PR #66716)

2023-09-18 Thread via cfe-commits

llvmbot wrote:




@llvm/pr-subscribers-clang


Changes

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.

---
Full diff: https://github.com/llvm/llvm-project/pull/66716.diff


9 Files Affected:

- (modified) clang/lib/CodeGen/CodeGenModule.cpp (+21-2) 
- (modified) clang/lib/CodeGen/CodeGenModule.h (+4) 
- (added) clang/test/CodeGen/annotations-decl-use-decl.c (+16) 
- (added) clang/test/CodeGen/annotations-decl-use-define.c (+16) 
- (added) clang/test/CodeGen/annotations-declaration.c (+17) 
- (modified) clang/test/CodeGen/annotations-global.c (+4-4) 
- (added) clang/test/CodeGenCXX/attr-annotate-constructor.cpp (+10) 
- (added) clang/test/CodeGenCXX/attr-annotate-destructor.cpp (+10) 
- (modified) clang/test/CodeGenCXX/attr-annotate.cpp (+3-3) 


``diff
diff --git a/clang/lib/CodeGen/CodeGenModule.cpp 
b/clang/lib/CodeGen/CodeGenModule.cpp
index 8b0c9340775cbe9..5108e6c91bfb30c 100644
--- a/clang/lib/CodeGen/CodeGenModule.cpp
+++ b/clang/lib/CodeGen/CodeGenModule.cpp
@@ -697,6 +697,7 @@ void CodeGenModule::checkAliases() {
 void CodeGenModule::clear() {
   DeferredDeclsToEmit.clear();
   EmittedDeferredDecls.clear();
+  DeferredAnnotations.clear();
   if (OpenMPRuntime)
 OpenMPRuntime->clear();
 }
@@ -3093,6 +3094,13 @@ void CodeGenModule::EmitVTablesOpportunistically() {
 }
 
 void CodeGenModule::EmitGlobalAnnotations() {
+  for (const auto& [MangledName, VD] : DeferredAnnotations) {
+llvm::GlobalValue *GV = GetGlobalValue(MangledName);
+if (GV)
+  AddGlobalAnnotations(VD, GV);
+  }
+  DeferredAnnotations.clear();
+
   if (Annotations.empty())
 return;
 
@@ -3597,6 +3605,14 @@ void CodeGenModule::EmitGlobal(GlobalDecl GD) {
 
   // Ignore declarations, they will be emitted on their first use.
   if (const auto *FD = dyn_cast(Global)) {
+// Update deferred annotations with the latest declaration if the function
+// function was already used or defined.
+if (FD->hasAttr()) {
+  StringRef MangledName = getMangledName(GD);
+  if (GetGlobalValue(MangledName))
+DeferredAnnotations[MangledName] = FD;
+}
+
 // Forward declarations are emitted lazily on first use.
 if (!FD->doesThisDeclarationHaveABody()) {
   if (!FD->doesDeclarationForceExternallyVisibleDefinition())
@@ -4370,6 +4386,11 @@ llvm::Constant *CodeGenModule::GetOrCreateLLVMFunction(
   llvm::Function::Create(FTy, llvm::Function::ExternalLinkage,
  Entry ? StringRef() : MangledName, &getModule());
 
+  // Store the declaration associated with this function so it is potentially
+  // updated by further declarations or definitions and emitted at the end.
+  if (D && D->hasAttr())
+DeferredAnnotations[MangledName] = cast(D);
+
   // If we already created a function with the same mangled name (but different
   // type) before, take its name and add it to the list of functions to be
   // replaced with F at the end of CodeGen.
@@ -5664,8 +5685,6 @@ void 
CodeGenModule::EmitGlobalFunctionDefinition(GlobalDecl GD,
 AddGlobalCtor(Fn, CA->getPriority());
   if (const DestructorAttr *DA = D->getAttr())
 AddGlobalDtor(Fn, DA->getPriority(), true);
-  if (D->hasAttr())
-AddGlobalAnnotations(D, Fn);
   if (getLangOpts().OpenMP && D->hasAttr())
 getOpenMPRuntime().emitDeclareTargetFunction(D, GV);
 }
diff --git a/clang/lib/CodeGen/CodeGenModule.h 
b/clang/lib/CodeGen/CodeGenModule.h
index 073b471c6e3cc11..8b0d68afbd0ecd2 100644
--- a/clang/lib/CodeGen/CodeGenModule.h
+++ b/clang/lib/CodeGen/CodeGenModule.h
@@ -431,6 +431,10 @@ class CodeGenModule : public CodeGenTypeCache {
   /// Global annotations.
   std::vector Annotations;
 
+  // Store deferred function annotations so they can be emitted at the end with
+  // most up to date ValueDecl that will have all the inherited annotations.
+  llvm::DenseMap DeferredAnnotations;
+
   /// Map used to get unique annotation strings.
   llvm::StringMap AnnotationStrings;
 
diff --git a/clang/test/CodeGen/annotations-decl-use-decl.c 
b/clang/test/CodeGen/annotations-decl-use-decl.c
new file mode 100644
index 000..f43ba91a34d876f
--- /dev/null
+++ b/clang/test/CodeGen/annotations-decl-use-decl.c
@@ -0,0 +1,16 @@
+// RUN: %clang_cc1 %s -emit-llvm -o - | FileCheck %s
+
+// Test annotation attributes are still emitted when the function is used 
before
+// it is defined with annotations.
+
+void foo(void);
+void *xxx = (void*)foo;
+void __attribute__((annotate("bar"))) 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"

[clang] [clang][CodeGen] Emit annotations for function declarations. (PR #66716)

2023-09-18 Thread Brendan Dahl via cfe-commits

https://github.com/brendandahl created 
https://github.com/llvm/llvm-project/pull/66716

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.

>From 846deb6e2055a8e458530c9e27bbd512a68deb5c Mon Sep 17 00:00:00 2001
From: Brendan Dahl 
Date: Tue, 12 Sep 2023 12:53:24 -0700
Subject: [PATCH] [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.
---
 clang/lib/CodeGen/CodeGenModule.cpp   | 23 +--
 clang/lib/CodeGen/CodeGenModule.h |  4 
 .../test/CodeGen/annotations-decl-use-decl.c  | 16 +
 .../CodeGen/annotations-decl-use-define.c | 16 +
 clang/test/CodeGen/annotations-declaration.c  | 17 ++
 clang/test/CodeGen/annotations-global.c   |  8 +++
 .../CodeGenCXX/attr-annotate-constructor.cpp  | 10 
 .../CodeGenCXX/attr-annotate-destructor.cpp   | 10 
 clang/test/CodeGenCXX/attr-annotate.cpp   |  6 ++---
 9 files changed, 101 insertions(+), 9 deletions(-)
 create mode 100644 clang/test/CodeGen/annotations-decl-use-decl.c
 create mode 100644 clang/test/CodeGen/annotations-decl-use-define.c
 create mode 100644 clang/test/CodeGen/annotations-declaration.c
 create mode 100644 clang/test/CodeGenCXX/attr-annotate-constructor.cpp
 create mode 100644 clang/test/CodeGenCXX/attr-annotate-destructor.cpp

diff --git a/clang/lib/CodeGen/CodeGenModule.cpp 
b/clang/lib/CodeGen/CodeGenModule.cpp
index 8b0c9340775cbe9..5108e6c91bfb30c 100644
--- a/clang/lib/CodeGen/CodeGenModule.cpp
+++ b/clang/lib/CodeGen/CodeGenModule.cpp
@@ -697,6 +697,7 @@ void CodeGenModule::checkAliases() {
 void CodeGenModule::clear() {
   DeferredDeclsToEmit.clear();
   EmittedDeferredDecls.clear();
+  DeferredAnnotations.clear();
   if (OpenMPRuntime)
 OpenMPRuntime->clear();
 }
@@ -3093,6 +3094,13 @@ void CodeGenModule::EmitVTablesOpportunistically() {
 }
 
 void CodeGenModule::EmitGlobalAnnotations() {
+  for (const auto& [MangledName, VD] : DeferredAnnotations) {
+llvm::GlobalValue *GV = GetGlobalValue(MangledName);
+if (GV)
+  AddGlobalAnnotations(VD, GV);
+  }
+  DeferredAnnotations.clear();
+
   if (Annotations.empty())
 return;
 
@@ -3597,6 +3605,14 @@ void CodeGenModule::EmitGlobal(GlobalDecl GD) {
 
   // Ignore declarations, they will be emitted on their first use.
   if (const auto *FD = dyn_cast(Global)) {
+// Update deferred annotations with the latest declaration if the function
+// function was already used or defined.
+if (FD->hasAttr()) {
+  StringRef MangledName = getMangledName(GD);
+  if (GetGlobalValue(MangledName))
+DeferredAnnotations[MangledName] = FD;
+}
+
 // Forward declarations are emitted lazily on first use.
 if (!FD->doesThisDeclarationHaveABody()) {
   if (!FD->doesDeclarationForceExternallyVisibleDefinition())
@@ -4370,6 +4386,11 @@ llvm::Constant *CodeGenModule::GetOrCreateLLVMFunction(
   llvm::Function::Create(FTy, llvm::Function::ExternalLinkage,
  Entry ? StringRef() : MangledName, &getModule());
 
+  // Store the declaration associated with this function so it is potentially
+  // updated by further declarations or definitions and emitted at the end.
+  if (D && D->hasAttr())
+DeferredAnnotations[MangledName] = cast(D);
+
   // If we already created a function with the same mangled name (but different
   // type) before, take its name and add it to the list of functions to be
   // replaced with F at the end of CodeGen.
@@ -5664,8 +5685,6 @@ void 
CodeGenModule::EmitGlobalFunctionDefinition(GlobalDecl GD,
 AddGlobalCtor(Fn, CA->getPriority());
   if (const DestructorAttr *DA = D->getAttr())
 AddGlobalDtor(Fn, DA->getPriority(), true);
-  if (D->hasAttr())
-AddGlobalAnnotations(D, Fn);
   if (getLangOpts().OpenMP && D->hasAttr())
 getOpenMPRuntime().emitDeclareTargetFunction(D, GV);
 }
diff --git a/clang/lib/CodeGen/CodeGenModule.h 
b/clang/lib/CodeGen/CodeGenModule.h
index 073b471c6e3cc11..8b0d68afbd0ecd2 100644
--- a/clang/lib/CodeGen/CodeGenModule.h
+++ b/clang/lib/CodeGen/CodeGenModule.h
@@ -431,6 +431,10 @@ class CodeGenModule : public CodeGenTypeCache {
   /// Global annotations.
   std::vector Annotations;
 
+  // Store deferred function annotations so they can be emitted at the end with
+  // most up to date ValueDecl that will have all the inherited annotations.
+  llvm::DenseMap DeferredAnnotations;
+
   /// Map used to get unique annotation strings.
   llvm::St