diff options
| author | Joshua Wong <joshuawong@anticentri.st> | 2024-10-09 14:24:05 -0400 |
|---|---|---|
| committer | Joshua Wong <joshuawong@anticentri.st> | 2024-10-10 09:49:24 -0400 |
| commit | dd0620b86721ae8cae86736443acd3f72ba6fc32 (patch) | |
| tree | 42eeb2a8ef0bfb161566045fe9c6e868071cc370 /tests/codegen | |
| parent | 8a1462265f839607f96acea4bed183736598422d (diff) | |
| download | rust-dd0620b86721ae8cae86736443acd3f72ba6fc32.tar.gz rust-dd0620b86721ae8cae86736443acd3f72ba6fc32.zip | |
allocate before calling T::default in <Box<T>>::default()
The `Box<T: Default>` impl currently calls `T::default()` before allocating the `Box`. Most `Default` impls are trivial, which should in theory allow LLVM to construct `T: Default` directly in the `Box` allocation when calling `<Box<T>>::default()`. However, the allocation may fail, which necessitates calling `T's` destructor if it has one. If the destructor is non-trivial, then LLVM has a hard time proving that it's sound to elide, which makes it construct `T` on the stack first, and then copy it into the allocation. Create an uninit `Box` first, and then write `T::default` into it, so that LLVM now only needs to prove that the `T::default` can't panic, which should be trivial for most `Default` impls.
Diffstat (limited to 'tests/codegen')
| -rw-r--r-- | tests/codegen/placement-new.rs | 4 |
1 files changed, 2 insertions, 2 deletions
diff --git a/tests/codegen/placement-new.rs b/tests/codegen/placement-new.rs index 68260a65a3f..d5c7969dd19 100644 --- a/tests/codegen/placement-new.rs +++ b/tests/codegen/placement-new.rs @@ -9,9 +9,9 @@ use std::sync::Arc; // CHECK-LABEL: @box_default_inplace #[no_mangle] pub fn box_default_inplace() -> Box<(String, String)> { - // CHECK: [[ALLOCA:%.*]] = alloca + // CHECK-NOT: alloca // CHECK: [[BOX:%.*]] = {{.*}}call {{.*}}__rust_alloc( - // CHECK: call void @llvm.memcpy{{.*}}(ptr {{.*}}[[BOX]], ptr {{.*}}[[ALLOCA]] + // CHECK-NOT: call void @llvm.memcpy // CHECK: ret ptr [[BOX]] Box::default() } |
