aboutsummaryrefslogtreecommitdiff
path: root/clang/lib/CodeGen/CodeGenModule.cpp
diff options
context:
space:
mode:
authorBrendan Dahl <brendan.dahl@gmail.com>2023-11-29 15:13:30 -0800
committerGitHub <noreply@github.com>2023-11-29 15:13:30 -0800
commitc6d70722b49db01914d5f64cc86ea5ed609ad9fd (patch)
tree77ba601cfe40fae3dd863127213a6f452ae50933 /clang/lib/CodeGen/CodeGenModule.cpp
parentefc60dc00796fd113e057a92c6861eeb57e649ae (diff)
downloadllvm-c6d70722b49db01914d5f64cc86ea5ed609ad9fd.zip
llvm-c6d70722b49db01914d5f64cc86ea5ed609ad9fd.tar.gz
llvm-c6d70722b49db01914d5f64cc86ea5ed609ad9fd.tar.bz2
[clang][CodeGen] Emit annotations for function declarations. (#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.
Diffstat (limited to 'clang/lib/CodeGen/CodeGenModule.cpp')
-rw-r--r--clang/lib/CodeGen/CodeGenModule.cpp23
1 files changed, 21 insertions, 2 deletions
diff --git a/clang/lib/CodeGen/CodeGenModule.cpp b/clang/lib/CodeGen/CodeGenModule.cpp
index 3225c98..6cb308e 100644
--- a/clang/lib/CodeGen/CodeGenModule.cpp
+++ b/clang/lib/CodeGen/CodeGenModule.cpp
@@ -699,6 +699,7 @@ void CodeGenModule::checkAliases() {
void CodeGenModule::clear() {
DeferredDeclsToEmit.clear();
EmittedDeferredDecls.clear();
+ DeferredAnnotations.clear();
if (OpenMPRuntime)
OpenMPRuntime->clear();
}
@@ -3165,6 +3166,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;
@@ -3678,6 +3686,14 @@ void CodeGenModule::EmitGlobal(GlobalDecl GD) {
// Ignore declarations, they will be emitted on their first use.
if (const auto *FD = dyn_cast<FunctionDecl>(Global)) {
+ // Update deferred annotations with the latest declaration if the function
+ // function was already used or defined.
+ if (FD->hasAttr<AnnotateAttr>()) {
+ StringRef MangledName = getMangledName(GD);
+ if (GetGlobalValue(MangledName))
+ DeferredAnnotations[MangledName] = FD;
+ }
+
// Forward declarations are emitted lazily on first use.
if (!FD->doesThisDeclarationHaveABody()) {
if (!FD->doesDeclarationForceExternallyVisibleDefinition())
@@ -4462,6 +4478,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<AnnotateAttr>())
+ DeferredAnnotations[MangledName] = cast<ValueDecl>(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.
@@ -5748,8 +5769,6 @@ void CodeGenModule::EmitGlobalFunctionDefinition(GlobalDecl GD,
AddGlobalCtor(Fn, CA->getPriority());
if (const DestructorAttr *DA = D->getAttr<DestructorAttr>())
AddGlobalDtor(Fn, DA->getPriority(), true);
- if (D->hasAttr<AnnotateAttr>())
- AddGlobalAnnotations(D, Fn);
if (getLangOpts().OpenMP && D->hasAttr<OMPDeclareTargetDeclAttr>())
getOpenMPRuntime().emitDeclareTargetFunction(D, GV);
}