llvmorg-github-actions[bot] wrote:
<!--LLVM PR SUMMARY COMMENT--> @llvm/pr-subscribers-clang Author: David Li (david-xl) <details> <summary>Changes</summary> Placement-new inference drops a variable on any use other than a read. Binding the size field of a span to a reference, as in std::min(res.n, max), cannot change the storage pointer, so treat it like a read. --- Full diff: https://github.com/llvm/llvm-project/pull/244802.diff 2 Files Affected: - (modified) clang/lib/AST/InferAlloc.cpp (+11) - (modified) clang/test/CodeGenCXX/alloc-token.cpp (+11) ``````````diff diff --git a/clang/lib/AST/InferAlloc.cpp b/clang/lib/AST/InferAlloc.cpp index 6767b3bf4dd308..04c8508e56ab30 100644 --- a/clang/lib/AST/InferAlloc.cpp +++ b/clang/lib/AST/InferAlloc.cpp @@ -264,6 +264,17 @@ class PlacementNewCollector : public ConstDynamicRecursiveASTVisitor { return true; } + // The integer field of a span (its size) cannot hold the storage pointer, so + // any use of it, such as binding it to a reference in std::min(res.n, max), + // is as harmless as a read. + bool VisitMemberExpr(const MemberExpr *ME) override { + if (!ME->isArrow() && ME->getType()->isIntegerType()) + if (const auto *DRE = + dyn_cast<DeclRefExpr>(ME->getBase()->IgnoreParenImpCasts())) + Reads.insert(DRE); + return true; + } + bool VisitCXXNewExpr(const CXXNewExpr *NE) override { // Only the reserved placement form is known to construct into its // argument; dependent new-expressions have no operator new yet. diff --git a/clang/test/CodeGenCXX/alloc-token.cpp b/clang/test/CodeGenCXX/alloc-token.cpp index 387d5ae83b79f8..5d259139c2a491 100644 --- a/clang/test/CodeGenCXX/alloc-token.cpp +++ b/clang/test/CodeGenCXX/alloc-token.cpp @@ -308,6 +308,17 @@ void test_placement_new_read_only_var(bool c) { sink = reinterpret_cast<void *>(s.n); } +// The size field of a span cannot hold the storage, so a use of it other than +// a read, such as binding it to a reference, keeps the variable. +const size_t &min_ref(const size_t &a, const size_t &b); +// CHECK-LABEL: define dso_local void @_Z29test_placement_new_size_fieldv( +// CHECK: call { ptr, i64 } @_Z13alloc_storagem(i64 noundef 64){{.*}} !alloc_token [[META_PLACEDNOPTR]] +void test_placement_new_size_field() { + Storage s = alloc_storage(64); + sink = new (s.p) PlacedNoPtr(); + sink = reinterpret_cast<void *>(min_ref(s.n, 32)); +} + // Any use of the variable other than a read may change its value and // disqualifies it: assignment (so the storage the placement new ends up in // would depend on control flow, and the sizeof or cast of the other `````````` </details> https://github.com/llvm/llvm-project/pull/244802 _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
