llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT--> @llvm/pr-subscribers-clang Author: Haojian Wu (hokein) <details> <summary>Changes</summary> This patch implements a clang builtin `__builtin_start_object_lifetime`, it has the same semantics as C++23's `std::start_lifetime_as`, but without the implicit-lifetime type restriction, it could be used for implementing `std::start_lifetime_as` in the future. Due to the current clang lowering, the builtin reuses the existing `__builtin_launder` implementation: - it is a no-op for the most part; - with `-fstrict-vtable-pointers` flag, we update the vtpr assumption correctly (mark the load/store vptr with appropriate invariant group intrinsics) to prevent incorrect vptr load folding; - for now, it is non-constant, thus cannot be executed in constant evaluation; CAVEAT: - this builtin may cause TBAA miscomplies without the `-fno-strict-aliasing` flag. These TBAA miscompiles are known issues and may need more LLVM IR support for the fix, fixing them is orthogonal to the implementation of the builtin. Context: https://discourse.llvm.org/t/extension-for-creating-objects-via-memcpy --- Full diff: https://github.com/llvm/llvm-project/pull/82776.diff 6 Files Affected: - (modified) clang/include/clang/Basic/Builtins.td (+6) - (modified) clang/lib/CodeGen/CGBuiltin.cpp (+1) - (modified) clang/lib/Sema/SemaChecking.cpp (+2) - (modified) clang/test/CodeGen/builtins.c (+10) - (added) clang/test/CodeGenCXX/builtin-start-object-life-time.cpp (+49) - (modified) clang/test/SemaCXX/builtins.cpp (+32-1) ``````````diff diff --git a/clang/include/clang/Basic/Builtins.td b/clang/include/clang/Basic/Builtins.td index df74026c5d2d50..70361afe69a980 100644 --- a/clang/include/clang/Basic/Builtins.td +++ b/clang/include/clang/Basic/Builtins.td @@ -896,6 +896,12 @@ def Launder : Builtin { let Prototype = "void*(void*)"; } +def StartObjectLifeTime : Builtin { + let Spellings = ["__builtin_start_object_lifetime"]; + let Attributes = [NoThrow, CustomTypeChecking]; + let Prototype = "void*(void*)"; +} + def IsConstantEvaluated : LangBuiltin<"CXX_LANG"> { let Spellings = ["__builtin_is_constant_evaluated"]; let Attributes = [NoThrow, Constexpr]; diff --git a/clang/lib/CodeGen/CGBuiltin.cpp b/clang/lib/CodeGen/CGBuiltin.cpp index d454ccc1dd8613..7a98f734f32ada 100644 --- a/clang/lib/CodeGen/CGBuiltin.cpp +++ b/clang/lib/CodeGen/CGBuiltin.cpp @@ -4386,6 +4386,7 @@ RValue CodeGenFunction::EmitBuiltinExpr(const GlobalDecl GD, unsigned BuiltinID, return RValue::get(nullptr); } + case Builtin::BI__builtin_start_object_lifetime: case Builtin::BI__builtin_launder: { const Expr *Arg = E->getArg(0); QualType ArgTy = Arg->getType()->getPointeeType(); diff --git a/clang/lib/Sema/SemaChecking.cpp b/clang/lib/Sema/SemaChecking.cpp index 8e763384774444..f79cb7e0445260 100644 --- a/clang/lib/Sema/SemaChecking.cpp +++ b/clang/lib/Sema/SemaChecking.cpp @@ -37,6 +37,7 @@ #include "clang/AST/TypeLoc.h" #include "clang/AST/UnresolvedSet.h" #include "clang/Basic/AddressSpaces.h" +#include "clang/Basic/Builtins.h" #include "clang/Basic/CharInfo.h" #include "clang/Basic/Diagnostic.h" #include "clang/Basic/IdentifierTable.h" @@ -2386,6 +2387,7 @@ Sema::CheckBuiltinFunctionCall(FunctionDecl *FDecl, unsigned BuiltinID, TheCall->setType(Context.IntTy); break; } + case Builtin::BI__builtin_start_object_lifetime: case Builtin::BI__builtin_launder: return SemaBuiltinLaunder(*this, TheCall); case Builtin::BI__sync_fetch_and_add: diff --git a/clang/test/CodeGen/builtins.c b/clang/test/CodeGen/builtins.c index 88282120283b8a..f46d6eb7632afc 100644 --- a/clang/test/CodeGen/builtins.c +++ b/clang/test/CodeGen/builtins.c @@ -143,6 +143,7 @@ int main(void) { P(signbit, (1.0)); R(launder, (&N)); + R(start_object_lifetime, (&N)); return 0; } @@ -511,6 +512,15 @@ void test_builtin_launder(int *p) { int *d = __builtin_launder(p); } +/// It should be a NOP in C since there are no vtables. +// CHECK-LABEL: define{{.*}} void @test_builtin_start_object_lifetime +void test_builtin_start_object_lifetime(int *p) { + // CHECK: [[TMP:%.*]] = load ptr, + // CHECK-NOT: @llvm.launder + // CHECK: store ptr [[TMP]], + int *d = __builtin_start_object_lifetime(p); +} + // __warn_memset_zero_len should be NOP, see https://sourceware.org/bugzilla/show_bug.cgi?id=25399 // CHECK-LABEL: define{{.*}} void @test___warn_memset_zero_len void test___warn_memset_zero_len(void) { diff --git a/clang/test/CodeGenCXX/builtin-start-object-life-time.cpp b/clang/test/CodeGenCXX/builtin-start-object-life-time.cpp new file mode 100644 index 00000000000000..58012f52cc0ef5 --- /dev/null +++ b/clang/test/CodeGenCXX/builtin-start-object-life-time.cpp @@ -0,0 +1,49 @@ +// RUN: %clang_cc1 -triple=x86_64-linux-gnu -emit-llvm -fstrict-vtable-pointers -o - %s \ +// RUN: | FileCheck --check-prefixes=CHECK,CHECK-STRICT %s +// RUN: %clang_cc1 -triple=x86_64-linux-gnu -emit-llvm -o - %s \ +// RUN: | FileCheck --check-prefixes=CHECK,CHECK-NONSTRICT %s + +struct TestVirtualFn { + virtual void foo(); +}; +// CHECK-LABEL: define{{.*}} void @test_dynamic_class +extern "C" void test_dynamic_class(TestVirtualFn *p) { + // CHECK: store ptr %p, ptr %p.addr + // CHECK-NEXT: [[TMP0:%.*]] = load ptr, ptr %p.addr + + // CHECK-NONSTRICT-NEXT: store ptr [[TMP0]], ptr %d + + // CHECK-STRICT-NEXT: [[TMP2:%.*]] = call ptr @llvm.launder.invariant.group.p0(ptr [[TMP0]]) + // CHECK-STRICT-NEXT: store ptr [[TMP2]], ptr %d + + // CHECK-NEXT: ret void + TestVirtualFn *d = __builtin_start_object_lifetime(p); +} + +// CHECK-LABEL: define{{.*}} void @test_scalar_pointer +extern "C" void test_scalar_pointer(int *p) { + // CHECK: entry + // CHECK-NEXT: %p.addr = alloca ptr + // CHECK-NEXT: %d = alloca ptr + // CHECK-NEXT: store ptr %p, ptr %p.addr, align 8 + // CHECK-NEXT: [[TMP:%.*]] = load ptr, ptr %p.addr + // CHECK-NEXT: store ptr [[TMP]], ptr %d + // CHECK-NEXT: ret void + int *d = __builtin_start_object_lifetime(p); +} + +struct TestNoInvariant { + int x; +}; +// CHECK-LABEL: define{{.*}} void @test_non_dynamic_class +extern "C" void test_non_dynamic_class(TestNoInvariant *p) { + // CHECK: entry + // CHECK-NOT: llvm.launder.invariant.group + // CHECK-NEXT: %p.addr = alloca ptr, align 8 + // CHECK-NEXT: %d = alloca ptr + // CHECK-NEXT: store ptr %p, ptr %p.addr + // CHECK-NEXT: [[TMP:%.*]] = load ptr, ptr %p.addr + // CHECK-NEXT: store ptr [[TMP]], ptr %d + // CHECK-NEXT: ret void + TestNoInvariant *d = __builtin_start_object_lifetime(p); +} diff --git a/clang/test/SemaCXX/builtins.cpp b/clang/test/SemaCXX/builtins.cpp index 567094c94c171b..4334b5bbf63663 100644 --- a/clang/test/SemaCXX/builtins.cpp +++ b/clang/test/SemaCXX/builtins.cpp @@ -1,4 +1,4 @@ -// RUN: %clang_cc1 %s -fsyntax-only -verify -std=c++11 -fcxx-exceptions +// RUN: %clang_cc1 %s -fsyntax-only -verify -DCXX11 -std=c++11 -fcxx-exceptions // RUN: %clang_cc1 %s -fsyntax-only -verify -std=c++1z -fcxx-exceptions typedef const struct __CFString * CFStringRef; #define CFSTR __builtin___CFStringMakeConstantString @@ -156,6 +156,37 @@ void test_noexcept(int *i) { #undef TEST_TYPE } // end namespace test_launder +namespace test_start_object_lifetime { +// The builtin is non-constant. +constexpr int test_non_constexpr(int i) { // expected-error {{constexpr function never produces a constant expression}} + __builtin_start_object_lifetime(&i); // expected-note {{subexpression not valid in a constant expression}} +#ifdef CXX11 + // expected-warning@-2 {{use of this statement in a constexpr function is a C++14 extension}} +#endif + return 0; +} + +struct Incomplete; // expected-note {{forward declaration}} +void test_incomplete(Incomplete *i) { + // Requires a complete type + __builtin_start_object_lifetime(i); // expected-error {{incomplete type 'Incomplete' where a complete type is required}} +} + +// The builtin is type-generic. +#define TEST_TYPE(Ptr, Type) \ + static_assert(__is_same(decltype(__builtin_launder(Ptr)), Type), "expected same type") +void test_type_generic() { + char * p; + int * i; + TEST_TYPE(p, char*); + TEST_TYPE(i, int*); +} +// The builtin is noexcept. +void test_noexcept(int *i) { + static_assert(noexcept(__builtin_start_object_lifetime(i)), ""); +} +} + template<typename T> void test_builtin_complex(T v, double d) { (void)__builtin_complex(v, d); // expected-error {{different types}} expected-error {{not a real floating}} (void)__builtin_complex(d, v); // expected-error {{different types}} expected-error {{not a real floating}} `````````` </details> https://github.com/llvm/llvm-project/pull/82776 _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits