-
Notifications
You must be signed in to change notification settings - Fork 10.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[clang] add unnamed_addr function attribute #92499
base: main
Are you sure you want to change the base?
Conversation
@llvm/pr-subscribers-clang-codegen @llvm/pr-subscribers-clang Author: YAMAMOTO Takashi (yamt) ChangesIt simply applies the LLVM attribute with the same name to the function. Sometimes, a programmer knows that function pointer uniqueness doesn't really matter for some of their functions. In that case, this attribute opens a possibility of certain optimizations like mergefunc with aliases. It's especially useful for code generators. Full diff: https://github.com/llvm/llvm-project/pull/92499.diff 2 Files Affected:
diff --git a/clang/include/clang/Basic/Attr.td b/clang/include/clang/Basic/Attr.td
index 52552ba488560..3ee7d43d339f1 100644
--- a/clang/include/clang/Basic/Attr.td
+++ b/clang/include/clang/Basic/Attr.td
@@ -1944,6 +1944,13 @@ def ReturnsTwice : InheritableAttr {
let SimpleHandler = 1;
}
+def UnnamedAddr : InheritableAttr {
+ let Spellings = [Clang<"unnamed_addr">];
+ let Subjects = SubjectList<[Function]>;
+ let Documentation = [Undocumented];
+ let SimpleHandler = 1;
+}
+
def DisableTailCalls : InheritableAttr {
let Spellings = [Clang<"disable_tail_calls">];
let Subjects = SubjectList<[Function, ObjCMethod]>;
diff --git a/clang/lib/CodeGen/CodeGenModule.cpp b/clang/lib/CodeGen/CodeGenModule.cpp
index 489c08a4d4819..ac9f082a1049b 100644
--- a/clang/lib/CodeGen/CodeGenModule.cpp
+++ b/clang/lib/CodeGen/CodeGenModule.cpp
@@ -2506,6 +2506,10 @@ void CodeGenModule::SetLLVMFunctionAttributesForDefinition(const Decl *D,
B.addAttribute(llvm::Attribute::MinSize);
}
+ if (D->hasAttr<UnnamedAddrAttr>()) {
+ F->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global);
+ }
+
F->addFnAttrs(B);
unsigned alignment = D->getMaxAlignment() / Context.getCharWidth();
|
It simply applies the LLVM attribute with the same name to the function. Sometimes, a programmer knows that function pointer uniqueness doesn't really matter for some of their functions. In that case, this attribute opens a possibility of certain optimizations like mergefunc with aliases. It's especially useful for code generators.
1c1bedf
to
52b744c
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I’m not sure how useful such an attribute would be, but the implementation of this looks ok, though this needs some documentation, a release note, and some sema tests to make sure we diagnose it when it’s applied to a non-function and codegen tests as well to make sure we actually emit it.
clang/include/clang/Basic/Attr.td
Outdated
def UnnamedAddr : InheritableAttr { | ||
let Spellings = [Clang<"unnamed_addr">]; | ||
let Subjects = SubjectList<[Function]>; | ||
let Documentation = [Undocumented]; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think for new attributes, we generally do prefer to document them. This attribute especially seems like it would benefit from documentation, so please add some for it. Doesn’t have to be much, just a sentence or two explaining that it adds the LLVM attribute and an example would be enough imo.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i added documentation
If we're going to do this, it should probably also work for constants. Also, I think I'd prefer to sort out the situation with the C++ standard's rules for constant merging before we start extending those rules. See #63628. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm... I'm not sure this meets our requirements for inclusion as an attribute. The semantics of this are pretty opaque, no obvious significant motivation/applicability in the base languages, etc. There doesn't seem to be any reasonable use case that I can see.
The attribute itself in LLVM isn't sufficient motivation for inclusion of this attribute.
do you mean my use case is not reasonable? |
for completeness, maybe. i myself have no use cases though.
interesting. i was not aware of it. |
This could be either. At the moment, I don't see the use of this to be compelling enough to be in the compiler. That said, the description of the use case is ~3 short sentences. In general, implementing LLVM attributes in Clang directly is typically not sufficient. Also, I agree with Eli, IF we do decide to do something like this, it needs to be holistically integrated/designed with the source language. |
I think the underlying functionality is pretty clearly useful: identical code folding gives significant codesize reductions. In fact, on Windows, the linker does this kind of folding automatically by default (despite the fact that it isn't standards-compliant). I imagine if we had this implemented, libc++ would want to use it. |
ok. |
It simply applies the LLVM attribute with the same name to the function.
Sometimes, a programmer knows that function pointer uniqueness doesn't really matter for some of their functions. In that case, this attribute opens a possibility of certain optimizations like mergefunc with aliases. It's especially useful for code generators.