Skip to content

Commit

Permalink
Also preserve valid scalar ranges for newtyped ScalarPair's.
Browse files Browse the repository at this point in the history
  • Loading branch information
luqmana committed May 31, 2022
1 parent 8477376 commit 34a80f0
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 8 deletions.
15 changes: 8 additions & 7 deletions compiler/rustc_codegen_ssa/src/mir/place.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,13 +136,14 @@ impl<'a, 'tcx, V: CodegenObject> PlaceRef<'tcx, V> {
};

// If this field is a primitive type of a newtype, propogate the scalar valid range
let scalar_valid_range = if let (0, Variants::Single { .. }, Abi::Scalar(_)) =
(offset.bytes(), &self.layout.variants, field.abi)
{
self.scalar_valid_range
} else {
field.abi.scalar_valid_range(bx)
};
let scalar_valid_range =
if let (0, Variants::Single { .. }, Abi::Scalar(_) | Abi::ScalarPair(..)) =
(offset.bytes(), &self.layout.variants, field.abi)
{
self.scalar_valid_range
} else {
field.abi.scalar_valid_range(bx)
};

PlaceRef {
// HACK(eddyb): have to bitcast pointers until LLVM removes pointee types.
Expand Down
14 changes: 13 additions & 1 deletion src/test/codegen/scalar-ranges-newtypes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,14 @@ pub struct NonNull3 {
ptr: *const (),
}

trait Foo {}

#[rustc_layout_scalar_valid_range_start(1)]
pub struct NonNull4(*const dyn Foo);

// CHECK: define void @test_nonnull_load
#[no_mangle]
pub fn test_nonnull_load(p1: &NonNull1, p2: &NonNull2, p3: &NonNull3) {
pub fn test_nonnull_load(p1: &NonNull1, p2: &NonNull2, p3: &NonNull3, p4: &NonNull4) {
// CHECK: %[[P1:[0-9]+]] = bitcast i8** %p1 to {}**
// CHECK: load {}*, {}** %[[P1]], align 8, !nonnull
std::hint::black_box(p1.0);
Expand All @@ -29,6 +34,13 @@ pub fn test_nonnull_load(p1: &NonNull1, p2: &NonNull2, p3: &NonNull3) {
// CHECK: %[[P3:[0-9]+]] = bitcast i8** %p3 to {}**
// CHECK: load {}*, {}** %[[P3]], align 8, !nonnull
std::hint::black_box(p3.ptr);

// CHECK: %[[P4_PTR:[0-9]+]] = bitcast { i8*, i64* }* %p4 to {}**
// CHECK: load {}*, {}** %[[P4_PTR]], align 8, !nonnull
// CHECK: %[[P4_VTABLE:[0-9]+]] = getelementptr inbounds { i8*, i64* }, { i8*, i64* }* %p4, i64 0, i32 1
// CHECK: %[[P4_VTABLE_PTR:[0-9]+]] = bitcast i64** %[[P4_VTABLE]] to [3 x i64]**
// CHECK: load [3 x i64]*, [3 x i64]** %[[P4_VTABLE_PTR]], align 8, !nonnull
std::hint::black_box(p4.0);
}

#[rustc_layout_scalar_valid_range_start(16)]
Expand Down

0 comments on commit 34a80f0

Please sign in to comment.