Skip to content

[CodeGen] Set target features in StartFunction() - #207691

Closed
nikic wants to merge 2 commits into
llvm:mainfrom
nikic:coro-features
Closed

nikic wants to merge 2 commits into
llvm:mainfrom
nikic:coro-features

Conversation

@nikic

@nikic nikic commented Jul 6, 2026 •

Copy link
Copy Markdown
Contributor

Set target features in StartFunction(), which is in particular used by generateAwaitSuspendWrapper(). This ensures that inlining works without having to rely on target-specific feature compatibility logic (after #205113).

TBH I'm very confused by Clang's attribute assignment logic for functions -- there are so many different APIs for setting attributes, and it feels like the attribute assignment is split randomly across them. I don't understand what the layering here is supposed to be.

I initially wanted to move this case from ConstructAttributeList() to StartFunction() entirely, as target-features should only be assigned to definitions, not declarations (as we currently do), but then it turns out that some places only use SetLLVMFunctionAttributes() but not StartFunction(), so that doesn't work.

Or maybe the right fix is to not change StartFunction() at all and instead call SetLLVMFunctionAttributes() from the coro code? But if you're supposed to do that, then why does StartFunction() set any attributes at all?

@nikic
nikic requested a review from efriedma-quic July 6, 2026 09:56
@llvmorg-github-actions llvmorg-github-actions Bot added clang Clang issues not falling into any other category clang:codegen IR generation bugs: mangling, exceptions, etc. coroutines C++20 coroutines labels Jul 6, 2026
@llvmorg-github-actions

llvmorg-github-actions Bot commented Jul 6, 2026 •

Copy link
Copy Markdown

@llvm/pr-subscribers-clang-codegen

@llvm/pr-subscribers-clang

Author: Nikita Popov (nikic)

Changes

Set target features in StartFunction(), which is in particular used by generateAwaitSuspendWrapper(). This ensures that inlining works without having to rely on target-specific feature compatibility logic (after #205113).

TBH I'm very confused by Clang's attribute assignment logic for functions -- there are so many different APIs for setting attributes, and it feels like the attribute assignment is split randomly across them. I don't understand what the layering here is supposed to be.

I initially wanted to move this case from ConstructAttributeList() to StartFunction() entirely, as target-features should only be assigned to definitions, not declarations (as we currently do), but then it turns out that some places only use SetLLVMFunctionAttributes() but not StartFunction(), so that doesn't work.

Or maybe the right fix is to not change StartFunction() at all and instead call SetLLVMFunctionAttributes() from the coro code?


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

5 Files Affected:

  • (modified) clang/lib/CodeGen/CodeGenFunction.cpp (+4)
  • (modified) clang/lib/CodeGen/CodeGenModule.h (+2)
  • (modified) clang/test/CodeGenCoroutines/coro-elide.cpp (-3)
  • (modified) clang/test/CodeGenCoroutines/coro-halo.cpp (-3)
  • (modified) clang/test/CodeGenCoroutines/pr65018.cpp (-3)
diff --git a/clang/lib/CodeGen/CodeGenFunction.cpp b/clang/lib/CodeGen/CodeGenFunction.cpp
index b920266b59808..7fcbcbed8efeb 100644
--- a/clang/lib/CodeGen/CodeGenFunction.cpp
+++ b/clang/lib/CodeGen/CodeGenFunction.cpp
@@ -1124,6 +1124,10 @@ void CodeGenFunction::StartFunction(GlobalDecl GD, QualType RetTy,
         getLLVMContext(), VScaleRange->first, VScaleRange->second));
   }
 
+  llvm::AttrBuilder Attrs(getLLVMContext());
+  CGM.GetCPUAndFeaturesAttributes(GD, Attrs);
+  Fn->addFnAttrs(Attrs);
+
   llvm::BasicBlock *EntryBB = createBasicBlock("entry", CurFn);
 
   // Create a marker to make it easy to insert allocas into the entryblock
diff --git a/clang/lib/CodeGen/CodeGenModule.h b/clang/lib/CodeGen/CodeGenModule.h
index b1e70a7c347db..b1137d900dccf 100644
--- a/clang/lib/CodeGen/CodeGenModule.h
+++ b/clang/lib/CodeGen/CodeGenModule.h
@@ -1992,9 +1992,11 @@ class CodeGenModule : public CodeGenTypeCache {
   void UpdateMultiVersionNames(GlobalDecl GD, const FunctionDecl *FD,
                                StringRef &CurName);
 
+public:
   bool GetCPUAndFeaturesAttributes(GlobalDecl GD,
                                    llvm::AttrBuilder &AttrBuilder,
                                    bool SetTargetFeatures = true);
+private:
   void setNonAliasAttributes(GlobalDecl GD, llvm::GlobalObject *GO);
 
   /// Set function attributes for a function declaration.
diff --git a/clang/test/CodeGenCoroutines/coro-elide.cpp b/clang/test/CodeGenCoroutines/coro-elide.cpp
index 3bff2a29c6e07..1902dd64b5e5a 100644
--- a/clang/test/CodeGenCoroutines/coro-elide.cpp
+++ b/clang/test/CodeGenCoroutines/coro-elide.cpp
@@ -1,8 +1,5 @@
 // RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -std=c++20 -O2 -emit-llvm %s -o - | FileCheck %s
 
-// FIXME: Generate wrappers with correct target features.
-// REQUIRES: x86-registered-target
-
 #include "Inputs/coroutine.h"
 
 namespace {
diff --git a/clang/test/CodeGenCoroutines/coro-halo.cpp b/clang/test/CodeGenCoroutines/coro-halo.cpp
index 6dd785f5c1f8e..e75bedaf81fa2 100644
--- a/clang/test/CodeGenCoroutines/coro-halo.cpp
+++ b/clang/test/CodeGenCoroutines/coro-halo.cpp
@@ -3,9 +3,6 @@
 // RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -std=c++20 -O2 -emit-llvm %s \
 // RUN:   -fcxx-exceptions -fexceptions -o - | FileCheck %s
 
-// FIXME: Generate wrappers with correct target features.
-// REQUIRES: x86-registered-target
-
 #include "Inputs/coroutine.h"
 #include "Inputs/numeric.h"
 
diff --git a/clang/test/CodeGenCoroutines/pr65018.cpp b/clang/test/CodeGenCoroutines/pr65018.cpp
index f3b903dc0b396..f1cde461cabc2 100644
--- a/clang/test/CodeGenCoroutines/pr65018.cpp
+++ b/clang/test/CodeGenCoroutines/pr65018.cpp
@@ -1,9 +1,6 @@
 // RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -std=c++20 \
 // RUN:      -O1 -emit-llvm %s -o - | FileCheck %s
 
-// FIXME: Generate wrappers with correct target features.
-// REQUIRES: x86-registered-target
-
 #include "Inputs/coroutine.h"
 
 // A simple awaiter type with an await_suspend method that can't be

@github-actions

github-actions Bot commented Jul 6, 2026 •

Copy link
Copy Markdown

✅ With the latest revision this PR passed the C/C++ code formatter.

Comment on lines +1127 to +1129
llvm::AttrBuilder Attrs(getLLVMContext());
CGM.GetCPUAndFeaturesAttributes(GD, Attrs);
Fn->addFnAttrs(Attrs);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The right thing to do is to call SetInternalFunctionAttributes() from generateAwaitSuspendWrapper().

The helpers for setting attributes for declarations/definitions could use some refactoring, but it's hard to enforce consistency here.

@nikic

nikic commented Jul 7, 2026

Copy link
Copy Markdown
Contributor Author

Opened #207961 for calling SetInternalFunctionAttributes() instead.

@nikic nikic closed this Jul 7, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

clang:codegen IR generation bugs: mangling, exceptions, etc. clang Clang issues not falling into any other category coroutines C++20 coroutines

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants