[PATCH] D158223: [clang] Add clang::unnamed_addr attribute that marks globals' address as not significant
Arthur Eubanks via Phabricator via cfe-commits
cfe-commits at lists.llvm.org
Thu Aug 17 15:08:47 PDT 2023
aeubanks created this revision.
Herald added a reviewer: aaron.ballman.
Herald added a project: All.
aeubanks requested review of this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.
This simply marks the global with the `unnamed_addr` IR attribute so it
be merged with other globals. This can break C++ pointer identity.
Repository:
rG LLVM Github Monorepo
https://reviews.llvm.org/D158223
Files:
clang/include/clang/Basic/Attr.td
clang/include/clang/Basic/AttrDocs.td
clang/lib/CodeGen/CodeGenModule.cpp
clang/test/CodeGen/unnamed-addr.c
Index: clang/test/CodeGen/unnamed-addr.c
===================================================================
--- /dev/null
+++ clang/test/CodeGen/unnamed-addr.c
@@ -0,0 +1,5 @@
+// RUN: %clang_cc1 -triple x86_64-unknown-unknown -O1 -emit-llvm -o - -disable-llvm-passes %s | FileCheck %s
+
+// CHECK: @i = unnamed_addr constant i32 8
+
+[[clang::unnamed_addr]] const int i = 8;
Index: clang/lib/CodeGen/CodeGenModule.cpp
===================================================================
--- clang/lib/CodeGen/CodeGenModule.cpp
+++ clang/lib/CodeGen/CodeGenModule.cpp
@@ -5254,6 +5254,9 @@
GV->setConstant(true);
}
+ if (D->hasAttr<UnnamedAddrAttr>())
+ GV->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global);
+
CharUnits AlignVal = getContext().getDeclAlign(D);
// Check for alignment specifed in an 'omp allocate' directive.
if (std::optional<CharUnits> AlignValFromAllocate =
Index: clang/include/clang/Basic/AttrDocs.td
===================================================================
--- clang/include/clang/Basic/AttrDocs.td
+++ clang/include/clang/Basic/AttrDocs.td
@@ -1408,6 +1408,24 @@
}];
}
+def UnnamedAddrDocs : Documentation {
+ let Category = DocCatField;
+ let Content = [{
+The ``clang::unnamed_addr`` attribute specifies that the address of a global is
+not significant. This allows global constants with the same contents to be
+merged. This can break global pointer identity, i.e. two different globals have
+the same address.
+
+Example usage:
+
+.. code-block:: c
+
+ // i and j may have the same address.
+ [[clang::unnamed_addr]] const int i = 42;
+ [[clang::unnamed_addr]] const int j = 42;
+ }];
+}
+
def ObjCRequiresSuperDocs : Documentation {
let Category = DocCatFunction;
let Content = [{
Index: clang/include/clang/Basic/Attr.td
===================================================================
--- clang/include/clang/Basic/Attr.td
+++ clang/include/clang/Basic/Attr.td
@@ -1794,6 +1794,13 @@
let SimpleHandler = 1;
}
+def UnnamedAddr : InheritableAttr {
+ let Spellings = [Clang<"unnamed_addr">];
+ let Subjects = SubjectList<[GlobalVar], ErrorDiag>;
+ let Documentation = [UnnamedAddrDocs];
+ let SimpleHandler = 1;
+}
+
def ReturnsTwice : InheritableAttr {
let Spellings = [GCC<"returns_twice">];
let Subjects = SubjectList<[Function]>;
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D158223.551287.patch
Type: text/x-patch
Size: 2342 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20230817/74da341b/attachment.bin>
More information about the cfe-commits
mailing list