-
Notifications
You must be signed in to change notification settings - Fork 13.5k
[clang] Add __builtin_start_object_lifetime builtin. #82776
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4521,10 +4521,15 @@ RValue CodeGenFunction::EmitBuiltinExpr(const GlobalDecl GD, unsigned BuiltinID, | |
|
||
return RValue::get(nullptr); | ||
} | ||
case Builtin::BI__builtin_start_object_lifetime: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. is there something to be commented here about why what we do here is sufficient? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added a comment for it, please take a look. |
||
// FIXME: we need some TBAA fences to prevent strict-aliasing miscompiles. | ||
case Builtin::BI__builtin_launder: { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think the name is fine, we're in the CodeGen, and the BuiltinLaunder in |
||
const Expr *Arg = E->getArg(0); | ||
QualType ArgTy = Arg->getType()->getPointeeType(); | ||
Value *Ptr = EmitScalarExpr(Arg); | ||
// Arguments of __builtin_launder and __builtin_start_object_lifetime may | ||
// need the LLVM IR launder.invariant.group intrinsic barrier to prevent | ||
// alising-based optimizations (e.g. -fstrict-vtable-pointers). | ||
if (TypeRequiresBuiltinLaunder(CGM, ArgTy)) | ||
Ptr = Builder.CreateLaunderInvariantGroup(Ptr); | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit, I'd consider
__builtin_start_lifetime
both for brevity and to parallelstd::start_lifetime_as
.I don't think it's really ambiguous what we're starting the lifetime of.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IMO if the intent of this intrinsic is to handle polymorphic classes (which is not covered by
std::start_lifetime_as
), its name should diverge from "plain"start_lifetime
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't have strong opinion on the name. Since this builtin doesn't strictly align with std::start_lifetime_as, I think it's better to have some divergence in the name.