[clang] clang: add unnamed_addr function attribute (PR #92499)
https://github.com/yamt created https://github.com/llvm/llvm-project/pull/92499 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. >From 1c1bedf655de10d2e4f0122975db1950f2203a96 Mon Sep 17 00:00:00 2001 From: YAMAMOTO Takashi Date: Fri, 17 May 2024 14:47:06 +0900 Subject: [PATCH] clang: add unnamed_addr function attribute 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. --- clang/include/clang/Basic/Attr.td | 7 +++ clang/lib/CodeGen/CodeGenModule.cpp | 4 2 files changed, 11 insertions(+) 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()) { +F->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global); + } + F->addFnAttrs(B); unsigned alignment = D->getMaxAlignment() / Context.getCharWidth(); ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] clang: add unnamed_addr function attribute (PR #92499)
llvmbot wrote: @llvm/pr-subscribers-clang Author: YAMAMOTO Takashi (yamt) Changes 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. --- Full diff: https://github.com/llvm/llvm-project/pull/92499.diff 2 Files Affected: - (modified) clang/include/clang/Basic/Attr.td (+7) - (modified) clang/lib/CodeGen/CodeGenModule.cpp (+4) ``diff 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()) { +F->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global); + } + F->addFnAttrs(B); unsigned alignment = D->getMaxAlignment() / Context.getCharWidth(); `` https://github.com/llvm/llvm-project/pull/92499 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] clang: add unnamed_addr function attribute (PR #92499)
https://github.com/yamt updated https://github.com/llvm/llvm-project/pull/92499 >From 52b744c91bdba1cf8cda9d6164ec8fc130d75fab Mon Sep 17 00:00:00 2001 From: YAMAMOTO Takashi Date: Fri, 17 May 2024 14:47:06 +0900 Subject: [PATCH] [clang] add unnamed_addr function attribute 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. --- clang/include/clang/Basic/Attr.td | 7 +++ clang/lib/CodeGen/CodeGenModule.cpp| 4 .../Misc/pragma-attribute-supported-attributes-list.test | 1 + 3 files changed, 12 insertions(+) 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()) { +F->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global); + } + F->addFnAttrs(B); unsigned alignment = D->getMaxAlignment() / Context.getCharWidth(); diff --git a/clang/test/Misc/pragma-attribute-supported-attributes-list.test b/clang/test/Misc/pragma-attribute-supported-attributes-list.test index 318bfb2df2a7a..62ab5cee27a71 100644 --- a/clang/test/Misc/pragma-attribute-supported-attributes-list.test +++ b/clang/test/Misc/pragma-attribute-supported-attributes-list.test @@ -199,6 +199,7 @@ // CHECK-NEXT: TestTypestate (SubjectMatchRule_function_is_member) // CHECK-NEXT: TrivialABI (SubjectMatchRule_record) // CHECK-NEXT: Uninitialized (SubjectMatchRule_variable_is_local) +// CHECK-NEXT: UnnamedAddr (SubjectMatchRule_function) // CHECK-NEXT: UnsafeBufferUsage (SubjectMatchRule_function) // CHECK-NEXT: UseHandle (SubjectMatchRule_variable_is_parameter) // CHECK-NEXT: VecReturn (SubjectMatchRule_record) ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang] add unnamed_addr function attribute (PR #92499)
https://github.com/yamt edited https://github.com/llvm/llvm-project/pull/92499 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang] add unnamed_addr function attribute (PR #92499)
https://github.com/Sirraide commented: 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. https://github.com/llvm/llvm-project/pull/92499 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang] add unnamed_addr function attribute (PR #92499)
@@ -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]; Sirraide wrote: 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. https://github.com/llvm/llvm-project/pull/92499 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang] add unnamed_addr function attribute (PR #92499)
https://github.com/Sirraide edited https://github.com/llvm/llvm-project/pull/92499 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang] add unnamed_addr function attribute (PR #92499)
efriedma-quic wrote: 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. https://github.com/llvm/llvm-project/pull/92499 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang] add unnamed_addr function attribute (PR #92499)
https://github.com/yamt updated https://github.com/llvm/llvm-project/pull/92499 >From 52b744c91bdba1cf8cda9d6164ec8fc130d75fab Mon Sep 17 00:00:00 2001 From: YAMAMOTO Takashi Date: Fri, 17 May 2024 14:47:06 +0900 Subject: [PATCH 1/3] [clang] add unnamed_addr function attribute 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. --- clang/include/clang/Basic/Attr.td | 7 +++ clang/lib/CodeGen/CodeGenModule.cpp| 4 .../Misc/pragma-attribute-supported-attributes-list.test | 1 + 3 files changed, 12 insertions(+) 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()) { +F->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global); + } + F->addFnAttrs(B); unsigned alignment = D->getMaxAlignment() / Context.getCharWidth(); diff --git a/clang/test/Misc/pragma-attribute-supported-attributes-list.test b/clang/test/Misc/pragma-attribute-supported-attributes-list.test index 318bfb2df2a7a..62ab5cee27a71 100644 --- a/clang/test/Misc/pragma-attribute-supported-attributes-list.test +++ b/clang/test/Misc/pragma-attribute-supported-attributes-list.test @@ -199,6 +199,7 @@ // CHECK-NEXT: TestTypestate (SubjectMatchRule_function_is_member) // CHECK-NEXT: TrivialABI (SubjectMatchRule_record) // CHECK-NEXT: Uninitialized (SubjectMatchRule_variable_is_local) +// CHECK-NEXT: UnnamedAddr (SubjectMatchRule_function) // CHECK-NEXT: UnsafeBufferUsage (SubjectMatchRule_function) // CHECK-NEXT: UseHandle (SubjectMatchRule_variable_is_parameter) // CHECK-NEXT: VecReturn (SubjectMatchRule_record) >From d96139b848f7536c927505d11a71c933d782a938 Mon Sep 17 00:00:00 2001 From: YAMAMOTO Takashi Date: Mon, 20 May 2024 13:57:26 +0900 Subject: [PATCH 2/3] style fix --- clang/lib/CodeGen/CodeGenModule.cpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/clang/lib/CodeGen/CodeGenModule.cpp b/clang/lib/CodeGen/CodeGenModule.cpp index ac9f082a1049b..84b0ddf58059a 100644 --- a/clang/lib/CodeGen/CodeGenModule.cpp +++ b/clang/lib/CodeGen/CodeGenModule.cpp @@ -2506,9 +2506,8 @@ void CodeGenModule::SetLLVMFunctionAttributesForDefinition(const Decl *D, B.addAttribute(llvm::Attribute::MinSize); } - if (D->hasAttr()) { + if (D->hasAttr()) F->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global); - } F->addFnAttrs(B); >From 68e92e93b05f956b10121953ee519297bf83c09a Mon Sep 17 00:00:00 2001 From: YAMAMOTO Takashi Date: Mon, 20 May 2024 14:16:20 +0900 Subject: [PATCH 3/3] document clang unnamed_addr attribute --- clang/include/clang/Basic/Attr.td | 2 +- clang/include/clang/Basic/AttrDocs.td | 13 + 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/clang/include/clang/Basic/Attr.td b/clang/include/clang/Basic/Attr.td index 3ee7d43d339f1..0e19dce3b59f3 100644 --- a/clang/include/clang/Basic/Attr.td +++ b/clang/include/clang/Basic/Attr.td @@ -1947,7 +1947,7 @@ def ReturnsTwice : InheritableAttr { def UnnamedAddr : InheritableAttr { let Spellings = [Clang<"unnamed_addr">]; let Subjects = SubjectList<[Function]>; - let Documentation = [Undocumented]; + let Documentation = [UnnamedAddrDocs]; let SimpleHandler = 1; } diff --git a/clang/include/clang/Basic/AttrDocs.td b/clang/include/clang/Basic/AttrDocs.td index f351822ac74bd..96ff6e38286c0 100644 --- a/clang/include/clang/Basic/AttrDocs.td +++ b/clang/include/clang/Basic/AttrDocs.td @@ -4960,6 +4960,19 @@ templates. }]; } +def UnnamedAddrDocs : Documentation { + let Category = DocCatFunction; + let Content = [{ +The ``unnamed_addr`` attribute instructs the backend that the pointer of +the marked function is not significant. +While this attribute opens a possibility of certain optimizations l
[clang] [clang] add unnamed_addr function attribute (PR #92499)
@@ -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]; yamt wrote: i added documentation https://github.com/llvm/llvm-project/pull/92499 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang] add unnamed_addr function attribute (PR #92499)
https://github.com/erichkeane commented: 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. https://github.com/llvm/llvm-project/pull/92499 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang] add unnamed_addr function attribute (PR #92499)
yamt wrote: > 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. do you mean my use case is not reasonable? or it isn't well explained? or something else? https://github.com/llvm/llvm-project/pull/92499 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang] add unnamed_addr function attribute (PR #92499)
yamt wrote: > If we're going to do this, it should probably also work for constants. for completeness, maybe. i myself have no use cases though. > 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. interesting. i was not aware of it. https://github.com/llvm/llvm-project/pull/92499 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang] add unnamed_addr function attribute (PR #92499)
erichkeane wrote: > > 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. > > do you mean my use case is not reasonable? or it isn't well explained? or > something else? 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. https://github.com/llvm/llvm-project/pull/92499 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang] add unnamed_addr function attribute (PR #92499)
efriedma-quic wrote: 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. https://github.com/llvm/llvm-project/pull/92499 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang] add unnamed_addr function attribute (PR #92499)
yamt wrote: > That said, the description of the use case is ~3 short sentences. ok. here is a concrete example and a bit longer explanation: https://github.com/yamt/toywasm/blob/9ee6ec86f56723819fd8411866094f72247dba78/lib/insn.c#L515-L527 https://github.com/llvm/llvm-project/pull/92499 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang] add unnamed_addr function attribute (PR #92499)
yamt wrote: > 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. may i update this to cover constants as well? or, it would be a waste of effort as it seems no chances to be merged? https://github.com/llvm/llvm-project/pull/92499 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang] add unnamed_addr function attribute (PR #92499)
efriedma-quic wrote: If you want to push this forward, start a Discourse thread (discourse.llvm.org) with a writeup of the proposal. Including an expanded rationale for why we want this. See also https://clang.llvm.org/get_involved.html https://github.com/llvm/llvm-project/pull/92499 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits