forked from rust-lang/rust
-
Notifications
You must be signed in to change notification settings - Fork 1
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#121668 - erikdesjardins:commonprim, r=scottmc…
…m,oli-obk Represent `Result<usize, Box<T>>` as ScalarPair(i64, ptr) This allows types like `Result<usize, std::io::Error>` (and integers of differing sign, e.g. `Result<u64, i64>`) to be passed in a pair of registers instead of through memory, like `Result<u64, u64>` or `Result<Box<T>, Box<U>>` are today. Fixes rust-lang#97540. r? `@ghost`
- Loading branch information
Showing
7 changed files
with
222 additions
and
19 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
//@ compile-flags: -O | ||
|
||
#![crate_type = "lib"] | ||
#![feature(core_intrinsics)] | ||
|
||
// Tests that codegen works properly when enums like `Result<usize, Box<()>>` | ||
// are represented as `{ u64, ptr }`, i.e., for `Ok(123)`, `123` is stored | ||
// as a pointer. | ||
|
||
// CHECK-LABEL: @insert_int | ||
#[no_mangle] | ||
pub fn insert_int(x: usize) -> Result<usize, Box<()>> { | ||
// CHECK: start: | ||
// CHECK-NEXT: inttoptr i{{[0-9]+}} %x to ptr | ||
// CHECK-NEXT: insertvalue | ||
// CHECK-NEXT: ret { i{{[0-9]+}}, ptr } | ||
Ok(x) | ||
} | ||
|
||
// CHECK-LABEL: @insert_box | ||
#[no_mangle] | ||
pub fn insert_box(x: Box<()>) -> Result<usize, Box<()>> { | ||
// CHECK: start: | ||
// CHECK-NEXT: insertvalue { i{{[0-9]+}}, ptr } | ||
// CHECK-NEXT: ret | ||
Err(x) | ||
} | ||
|
||
// CHECK-LABEL: @extract_int | ||
// CHECK-NOT: nonnull | ||
// CHECK-SAME: (i{{[0-9]+}} {{[^,]+}} [[DISCRIMINANT:%[0-9]+]], ptr {{[^,]+}} [[PAYLOAD:%[0-9]+]]) | ||
#[no_mangle] | ||
pub unsafe fn extract_int(x: Result<usize, Box<()>>) -> usize { | ||
// CHECK: [[TEMP:%.+]] = ptrtoint ptr [[PAYLOAD]] to [[USIZE:i[0-9]+]] | ||
// CHECK: ret [[USIZE]] [[TEMP]] | ||
match x { | ||
Ok(v) => v, | ||
Err(_) => std::intrinsics::unreachable(), | ||
} | ||
} | ||
|
||
// CHECK-LABEL: @extract_box | ||
// CHECK-SAME: (i{{[0-9]+}} {{[^,]+}} [[DISCRIMINANT:%[0-9]+]], ptr {{[^,]+}} [[PAYLOAD:%[0-9]+]]) | ||
#[no_mangle] | ||
pub unsafe fn extract_box(x: Result<usize, Box<i32>>) -> Box<i32> { | ||
// CHECK: ret ptr [[PAYLOAD]] | ||
match x { | ||
Ok(_) => std::intrinsics::unreachable(), | ||
Err(e) => e, | ||
} | ||
} |
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,24 @@ | ||
//@ normalize-stderr-test "pref: Align\([1-8] bytes\)" -> "pref: $$PREF_ALIGN" | ||
//@ normalize-stderr-test "Int\(I[0-9]+," -> "Int(I?," | ||
//@ normalize-stderr-test "valid_range: 0..=[0-9]+" -> "valid_range: $$VALID_RANGE" | ||
|
||
//! Enum layout tests related to scalar pairs with an int/ptr common primitive. | ||
|
||
#![feature(rustc_attrs)] | ||
#![feature(never_type)] | ||
#![crate_type = "lib"] | ||
|
||
#[rustc_layout(abi)] | ||
enum ScalarPairPointerWithInt { //~ERROR: abi: ScalarPair | ||
A(usize), | ||
B(Box<()>), | ||
} | ||
|
||
// Negative test--ensure that pointers are not commoned with integers | ||
// of a different size. (Assumes that no target has 8 bit pointers, which | ||
// feels pretty safe.) | ||
#[rustc_layout(abi)] | ||
enum NotScalarPairPointerWithSmallerInt { //~ERROR: abi: Aggregate | ||
A(u8), | ||
B(Box<()>), | ||
} |
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,14 @@ | ||
error: abi: ScalarPair(Initialized { value: Int(I?, false), valid_range: $VALID_RANGE }, Initialized { value: Pointer(AddressSpace(0)), valid_range: $VALID_RANGE }) | ||
--> $DIR/enum-scalar-pair-int-ptr.rs:12:1 | ||
| | ||
LL | enum ScalarPairPointerWithInt { | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
|
||
error: abi: Aggregate { sized: true } | ||
--> $DIR/enum-scalar-pair-int-ptr.rs:21:1 | ||
| | ||
LL | enum NotScalarPairPointerWithSmallerInt { | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
|
||
error: aborting due to 2 previous errors | ||
|
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