forked from rust-lang/rust
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Auto merge of rust-lang#123886 - scottmcm:more-rvalue-operands, r=<try>
Avoid `alloca`s in codegen for simple pairs and simple transparent structs Even something simple like constructing a ```rust #[repr(transparent)] struct Foo(u32); ``` forces an `alloca` to be generated in nightly right now. Certainly LLVM can optimize that away, but it would be nice if it didn't have to. Quick example: ```rust #[repr(transparent)] pub struct Transparent32(u32); #[no_mangle] pub fn make_transparent(x: u32) -> Transparent32 { let a = Transparent32(x); a } ``` on nightly we produce <https://rust.godbolt.org/z/zcvoM79ae> ```llvm define noundef i32 `@make_transparent(i32` noundef %x) unnamed_addr #0 { %a = alloca i32, align 4 store i32 %x, ptr %a, align 4 %0 = load i32, ptr %a, align 4, !noundef !3 ret i32 %0 } ``` but after this PR we produce ```llvm define noundef i32 `@make_transparent(i32` noundef %x) unnamed_addr #0 { start: ret i32 %x } ``` (even before the optimizer runs).
- Loading branch information
Showing
6 changed files
with
146 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
//@ compile-flags: -O -C no-prepopulate-passes | ||
|
||
#![crate_type = "lib"] | ||
|
||
#[repr(transparent)] | ||
pub struct Transparent32(u32); | ||
|
||
// CHECK: i32 @make_transparent(i32 noundef %x) | ||
#[no_mangle] | ||
pub fn make_transparent(x: u32) -> Transparent32 { | ||
// CHECK-NOT: alloca | ||
// CHECK: ret i32 %x | ||
let a = Transparent32(x); | ||
a | ||
} | ||
|
||
#[repr(transparent)] | ||
pub struct TransparentPair((), (u16, u16), ()); | ||
|
||
// CHECK: { i16, i16 } @make_transparent_pair(i16 noundef %x.0, i16 noundef %x.1) | ||
#[no_mangle] | ||
pub fn make_transparent_pair(x: (u16, u16)) -> TransparentPair { | ||
// CHECK-NOT: alloca | ||
// CHECK: %[[TEMP0:.+]] = insertvalue { i16, i16 } poison, i16 %x.0, 0 | ||
// CHECK: %[[TEMP1:.+]] = insertvalue { i16, i16 } %[[TEMP0]], i16 %x.1, 1 | ||
// CHECK: ret { i16, i16 } %[[TEMP1]] | ||
let a = TransparentPair((), x, ()); | ||
a | ||
} | ||
|
||
// CHECK-LABEL: { i32, i32 } @make_2_tuple(i32 noundef %x) | ||
#[no_mangle] | ||
pub fn make_2_tuple(x: u32) -> (u32, u32) { | ||
// CHECK-NOT: alloca | ||
// CHECK: %[[TEMP0:.+]] = insertvalue { i32, i32 } poison, i32 %x, 0 | ||
// CHECK: %[[TEMP1:.+]] = insertvalue { i32, i32 } %[[TEMP0]], i32 %x, 1 | ||
// CHECK: ret { i32, i32 } %[[TEMP1]] | ||
let pair = (x, x); | ||
pair | ||
} | ||
|
||
// CHECK-LABEL: i8 @make_cell_of_bool(i1 noundef zeroext %b) | ||
#[no_mangle] | ||
pub fn make_cell_of_bool(b: bool) -> std::cell::Cell<bool> { | ||
// CHECK: %[[BYTE:.+]] = zext i1 %b to i8 | ||
// CHECK: ret i8 %[[BYTE]] | ||
std::cell::Cell::new(b) | ||
} | ||
|
||
// CHECK-LABLE: { i8, i16 } @make_cell_of_bool_and_short(i1 noundef zeroext %b, i16 noundef %s) | ||
#[no_mangle] | ||
pub fn make_cell_of_bool_and_short(b: bool, s: u16) -> std::cell::Cell<(bool, u16)> { | ||
// CHECK-NOT: alloca | ||
// CHECK: %[[BYTE:.+]] = zext i1 %b to i8 | ||
// CHECK: %[[TEMP0:.+]] = insertvalue { i8, i16 } poison, i8 %[[BYTE]], 0 | ||
// CHECK: %[[TEMP1:.+]] = insertvalue { i8, i16 } %[[TEMP0]], i16 %s, 1 | ||
// CHECK: ret { i8, i16 } %[[TEMP1]] | ||
std::cell::Cell::new((b, s)) | ||
} | ||
|
||
// CHECK-LABEL: { i1, i1 } @make_tuple_of_bools(i1 noundef zeroext %a, i1 noundef zeroext %b) | ||
#[no_mangle] | ||
pub fn make_tuple_of_bools(a: bool, b: bool) -> (bool, bool) { | ||
// CHECK-NOT: alloca | ||
// CHECK: %[[TEMP0:.+]] = insertvalue { i1, i1 } poison, i1 %a, 0 | ||
// CHECK: %[[TEMP1:.+]] = insertvalue { i1, i1 } %[[TEMP0]], i1 %b, 1 | ||
// CHECK: ret { i1, i1 } %[[TEMP1]] | ||
(a, b) | ||
} |