[PATCH] D123950: Look through calls to std::addressof to compute pointer alignment.
Eli Friedman via Phabricator via cfe-commits
cfe-commits at lists.llvm.org
Mon Apr 18 10:31:51 PDT 2022
efriedma created this revision.
efriedma added reviewers: jyknight, rsmith, rjmccall.
Herald added a project: All.
efriedma requested review of this revision.
Herald added a project: clang.
This is sort of a followup to D37310 <https://reviews.llvm.org/D37310>; that basically fixed the same issue, but then the libstdc++ implementation of <atomic> changed. Re-fix the the issue in essentially the same way: look through the addressof operation to find the alignment of the underlying object.
Fixes https://github.com/llvm/llvm-project/issues/54963 . Alternative to D123642 <https://reviews.llvm.org/D123642>.
Repository:
rG LLVM Github Monorepo
https://reviews.llvm.org/D123950
Files:
clang/lib/CodeGen/CGExpr.cpp
clang/test/CodeGenCXX/atomic-align.cpp
Index: clang/test/CodeGenCXX/atomic-align.cpp
===================================================================
--- clang/test/CodeGenCXX/atomic-align.cpp
+++ clang/test/CodeGenCXX/atomic-align.cpp
@@ -1,4 +1,4 @@
-// RUN: %clang_cc1 %s -std=c++11 -emit-llvm -o - -triple=x86_64-linux-gnu | FileCheck %s
+// RUN: %clang_cc1 %s -std=c++14 -emit-llvm -o - -triple=x86_64-linux-gnu | FileCheck %s
struct AM {
int f1, f2;
@@ -28,3 +28,23 @@
__atomic_load(&bm.f2, &am, 0);
return am;
}
+
+namespace std {
+ template <class _Tp>
+ inline constexpr
+ __attribute__ ((__visibility__("hidden"), __internal_linkage__))
+ _Tp* __addressof(_Tp& __x) noexcept
+ {
+ return __builtin_addressof(__x);
+ }
+}
+
+AM load3() {
+ AM am;
+ // m is declared to align to 8bytes, so generate load atomic instead
+ // of libcall.
+ // CHECK-LABEL: @_Z5load3v
+ // CHECK: load atomic {{.*}} monotonic, align 8
+ __atomic_load(std::__addressof(m), &am, 0);
+ return am;
+}
Index: clang/lib/CodeGen/CGExpr.cpp
===================================================================
--- clang/lib/CodeGen/CGExpr.cpp
+++ clang/lib/CodeGen/CGExpr.cpp
@@ -1170,6 +1170,22 @@
}
}
+ // std::addressof and variants.
+ if (auto *Call = dyn_cast<CallExpr>(E)) {
+ switch (Call->getBuiltinCallee()) {
+ default:
+ break;
+ case Builtin::BIaddressof:
+ case Builtin::BI__addressof:
+ case Builtin::BI__builtin_addressof: {
+ LValue LV = EmitLValue(Call->getArg(0));
+ if (BaseInfo) *BaseInfo = LV.getBaseInfo();
+ if (TBAAInfo) *TBAAInfo = LV.getTBAAInfo();
+ return LV.getAddress(*this);
+ }
+ }
+ }
+
// TODO: conditional operators, comma.
// Otherwise, use the alignment of the type.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D123950.423424.patch
Type: text/x-patch
Size: 1745 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20220418/5a0977cd/attachment.bin>
More information about the cfe-commits
mailing list