From 69ae4233cfdffd4269e07203406ff0ccb2671896 Mon Sep 17 00:00:00 2001 From: Erik Desjardins Date: Tue, 15 Feb 2022 23:45:09 -0500 Subject: [PATCH 1/3] Add !align metadata on loads of &/&mut/Box Note that this refers to the alignment of what the loaded value points to, _not_ the alignment of the loaded value itself. --- compiler/rustc_codegen_llvm/src/builder.rs | 38 ++++++++++++++---- compiler/rustc_codegen_llvm/src/llvm/ffi.rs | 1 + src/test/codegen/loads.rs | 44 +++++++++++++++++---- 3 files changed, 67 insertions(+), 16 deletions(-) diff --git a/compiler/rustc_codegen_llvm/src/builder.rs b/compiler/rustc_codegen_llvm/src/builder.rs index 77aaa3010f2e6..9a417c71779d3 100644 --- a/compiler/rustc_codegen_llvm/src/builder.rs +++ b/compiler/rustc_codegen_llvm/src/builder.rs @@ -478,6 +478,8 @@ impl<'a, 'll, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> { bx: &mut Builder<'a, 'll, 'tcx>, load: &'ll Value, scalar: abi::Scalar, + layout: TyAndLayout<'tcx>, + offset: Size, ) { if !scalar.is_always_valid(bx) { bx.noundef_metadata(load); @@ -489,10 +491,18 @@ impl<'a, 'll, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> { bx.range_metadata(load, scalar.valid_range); } } - abi::Pointer if !scalar.valid_range.contains(0) => { - bx.nonnull_metadata(load); + abi::Pointer => { + if !scalar.valid_range.contains(0) { + bx.nonnull_metadata(load); + } + + if let Some(pointee) = layout.pointee_info_at(bx, offset) { + if let Some(_) = pointee.safe { + bx.align_metadata(load, pointee.align); + } + } } - _ => {} + abi::F32 | abi::F64 => {} } } @@ -510,7 +520,7 @@ impl<'a, 'll, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> { let llval = const_llval.unwrap_or_else(|| { let load = self.load(place.layout.llvm_type(self), place.llval, place.align); if let abi::Abi::Scalar(scalar) = place.layout.abi { - scalar_load_metadata(self, load, scalar); + scalar_load_metadata(self, load, scalar, place.layout, Size::ZERO); } load }); @@ -519,17 +529,17 @@ impl<'a, 'll, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> { let b_offset = a.value.size(self).align_to(b.value.align(self).abi); let pair_ty = place.layout.llvm_type(self); - let mut load = |i, scalar: abi::Scalar, align| { + let mut load = |i, scalar: abi::Scalar, layout, align, offset| { let llptr = self.struct_gep(pair_ty, place.llval, i as u64); let llty = place.layout.scalar_pair_element_llvm_type(self, i, false); let load = self.load(llty, llptr, align); - scalar_load_metadata(self, load, scalar); + scalar_load_metadata(self, load, scalar, layout, offset); self.to_immediate_scalar(load, scalar) }; OperandValue::Pair( - load(0, a, place.align), - load(1, b, place.align.restrict_for_offset(b_offset)), + load(0, a, place.layout, place.align, Size::ZERO), + load(1, b, place.layout, place.align.restrict_for_offset(b_offset), b_offset), ) } else { OperandValue::Ref(place.llval, None, place.align) @@ -1219,6 +1229,18 @@ impl<'a, 'll, 'tcx> Builder<'a, 'll, 'tcx> { } } + fn align_metadata(&mut self, load: &'ll Value, align: Align) { + unsafe { + let v = [self.cx.const_u64(align.bytes())]; + + llvm::LLVMSetMetadata( + load, + llvm::MD_align as c_uint, + llvm::LLVMMDNodeInContext(self.cx.llcx, v.as_ptr(), v.len() as c_uint), + ); + } + } + fn noundef_metadata(&mut self, load: &'ll Value) { unsafe { llvm::LLVMSetMetadata( diff --git a/compiler/rustc_codegen_llvm/src/llvm/ffi.rs b/compiler/rustc_codegen_llvm/src/llvm/ffi.rs index 81ae97a80cc17..ed0825e124c45 100644 --- a/compiler/rustc_codegen_llvm/src/llvm/ffi.rs +++ b/compiler/rustc_codegen_llvm/src/llvm/ffi.rs @@ -441,6 +441,7 @@ pub enum MetadataType { MD_nontemporal = 9, MD_mem_parallel_loop_access = 10, MD_nonnull = 11, + MD_align = 17, MD_type = 19, MD_noundef = 29, } diff --git a/src/test/codegen/loads.rs b/src/test/codegen/loads.rs index 977175a4a621d..268c55561f2b8 100644 --- a/src/test/codegen/loads.rs +++ b/src/test/codegen/loads.rs @@ -18,21 +18,47 @@ pub enum MyBool { False, } +#[repr(align(16))] +pub struct Align16(u128); + +// CHECK: @ptr_alignment_helper({}** {{.*}} align [[PTR_ALIGNMENT:[0-9]+]] +#[no_mangle] +pub fn ptr_alignment_helper(x: &&()) {} + // CHECK-LABEL: @load_ref #[no_mangle] pub fn load_ref<'a>(x: &&'a i32) -> &'a i32 { -// Alignment of a reference itself is target dependent, so just match any alignment: -// the main thing we care about here is !nonnull and !noundef. -// CHECK: load i32*, i32** %x, align {{[0-9]+}}, !nonnull !{{[0-9]+}}, !noundef !{{[0-9]+}} +// CHECK: load i32*, i32** %x, align [[PTR_ALIGNMENT]], !nonnull !{{[0-9]+}}, !align ![[ALIGN_4_META:[0-9]+]], !noundef !{{[0-9]+}} + *x +} + +// CHECK-LABEL: @load_ref_higher_alignment +#[no_mangle] +pub fn load_ref_higher_alignment<'a>(x: &&'a Align16) -> &'a Align16 { +// CHECK: load %Align16*, %Align16** %x, align [[PTR_ALIGNMENT]], !nonnull !{{[0-9]+}}, !align ![[ALIGN_16_META:[0-9]+]], !noundef !{{[0-9]+}} + *x +} + +// CHECK-LABEL: @load_scalar_pair +#[no_mangle] +pub fn load_scalar_pair<'a>(x: &(&'a i32, &'a Align16)) -> (&'a i32, &'a Align16) { +// CHECK: load i32*, i32** %{{.+}}, align [[PTR_ALIGNMENT]], !nonnull !{{[0-9]+}}, !align ![[ALIGN_4_META]], !noundef !{{[0-9]+}} +// CHECK: load i64*, i64** %{{.+}}, align [[PTR_ALIGNMENT]], !nonnull !{{[0-9]+}}, !align ![[ALIGN_16_META]], !noundef !{{[0-9]+}} + *x +} + +// CHECK-LABEL: @load_raw_pointer +#[no_mangle] +pub fn load_raw_pointer<'a>(x: &*const i32) -> *const i32 { +// loaded raw pointer should not have !nonnull, !align, or !noundef metadata +// CHECK: load i32*, i32** %x, align [[PTR_ALIGNMENT]]{{$}} *x } // CHECK-LABEL: @load_box #[no_mangle] pub fn load_box<'a>(x: Box>) -> Box { -// Alignment of a box itself is target dependent, so just match any alignment: -// the main thing we care about here is !nonnull and !noundef. -// CHECK: load i32*, i32** %x, align {{[0-9]+}}, !nonnull !{{[0-9]+}}, !noundef !{{[0-9]+}} +// CHECK: load i32*, i32** %x, align [[PTR_ALIGNMENT]], !nonnull !{{[0-9]+}}, !align ![[ALIGN_4_META]], !noundef !{{[0-9]+}} *x } @@ -120,5 +146,7 @@ pub fn small_struct_alignment(x: Bytes) -> Bytes { x } -// CHECK: ![[BOOL_RANGE]] = !{i8 0, i8 2} -// CHECK: ![[NONZEROU16_RANGE]] = !{i16 1, i16 0} +// CHECK-DAG: ![[BOOL_RANGE]] = !{i8 0, i8 2} +// CHECK-DAG: ![[NONZEROU16_RANGE]] = !{i16 1, i16 0} +// CHECK-DAG: ![[ALIGN_4_META]] = !{i64 4} +// CHECK-DAG: ![[ALIGN_16_META]] = !{i64 16} From f147303434f6b26d64458a999fb6760fdd422977 Mon Sep 17 00:00:00 2001 From: Erik Desjardins Date: Wed, 2 Mar 2022 17:30:58 -0500 Subject: [PATCH 2/3] fix tests on platforms where Align16 is represented as i128 --- src/test/codegen/loads.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/codegen/loads.rs b/src/test/codegen/loads.rs index 268c55561f2b8..7952bf67a46e6 100644 --- a/src/test/codegen/loads.rs +++ b/src/test/codegen/loads.rs @@ -35,7 +35,7 @@ pub fn load_ref<'a>(x: &&'a i32) -> &'a i32 { // CHECK-LABEL: @load_ref_higher_alignment #[no_mangle] pub fn load_ref_higher_alignment<'a>(x: &&'a Align16) -> &'a Align16 { -// CHECK: load %Align16*, %Align16** %x, align [[PTR_ALIGNMENT]], !nonnull !{{[0-9]+}}, !align ![[ALIGN_16_META:[0-9]+]], !noundef !{{[0-9]+}} +// CHECK: load {{%Align16|i128}}*, {{%Align16|i128}}** %x, align [[PTR_ALIGNMENT]], !nonnull !{{[0-9]+}}, !align ![[ALIGN_16_META:[0-9]+]], !noundef !{{[0-9]+}} *x } From a381aefebb69c7fa518a20f3db6639f29096cc96 Mon Sep 17 00:00:00 2001 From: Erik Desjardins Date: Wed, 2 Mar 2022 22:46:23 -0500 Subject: [PATCH 3/3] make test work on noopt builder --- src/test/codegen/loads.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/codegen/loads.rs b/src/test/codegen/loads.rs index 7952bf67a46e6..c7444ce02fa25 100644 --- a/src/test/codegen/loads.rs +++ b/src/test/codegen/loads.rs @@ -21,7 +21,7 @@ pub enum MyBool { #[repr(align(16))] pub struct Align16(u128); -// CHECK: @ptr_alignment_helper({}** {{.*}} align [[PTR_ALIGNMENT:[0-9]+]] +// CHECK: @ptr_alignment_helper({}** {{.*}}align [[PTR_ALIGNMENT:[0-9]+]] #[no_mangle] pub fn ptr_alignment_helper(x: &&()) {}