Skip to content

Commit 8cb0b6c

Browse files
Apply noundef attribute to &T, &mut T, Box<T>, bool
This doesn't handle `char` because it's a bit awkward to distinguish it from u32 at this point in codegen. Note that for some types (like `&Struct` and `&mut Struct`), we already apply `dereferenceable`, which implies `noundef`, so the IR does not change.
1 parent 71226d7 commit 8cb0b6c

File tree

10 files changed

+56
-22
lines changed

10 files changed

+56
-22
lines changed

compiler/rustc_codegen_llvm/src/abi.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ impl ArgAttributeExt for ArgAttribute {
3737
where
3838
F: FnMut(llvm::Attribute),
3939
{
40-
for_each_kind!(self, f, NoAlias, NoCapture, NonNull, ReadOnly, InReg)
40+
for_each_kind!(self, f, NoAlias, NoCapture, NonNull, ReadOnly, InReg, NoUndef)
4141
}
4242
}
4343

@@ -69,7 +69,9 @@ impl ArgAttributesExt for ArgAttributes {
6969
} else {
7070
llvm::LLVMRustAddDereferenceableOrNullAttr(llfn, idx.as_uint(), deref);
7171
}
72+
// dereferenceable implies nonnull noundef; dereferenceable_or_null implies noundef
7273
regular -= ArgAttribute::NonNull;
74+
regular -= ArgAttribute::NoUndef;
7375
}
7476
if let Some(align) = self.pointee_align {
7577
llvm::LLVMRustAddAlignmentAttr(llfn, idx.as_uint(), align.bytes() as u32);
@@ -109,7 +111,9 @@ impl ArgAttributesExt for ArgAttributes {
109111
deref,
110112
);
111113
}
114+
// dereferenceable implies nonnull noundef; dereferenceable_or_null implies noundef
112115
regular -= ArgAttribute::NonNull;
116+
regular -= ArgAttribute::NoUndef;
113117
}
114118
if let Some(align) = self.pointee_align {
115119
llvm::LLVMRustAddAlignmentCallSiteAttr(

compiler/rustc_codegen_llvm/src/llvm/ffi.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,7 @@ pub enum Attribute {
189189
StackProtectReq = 30,
190190
StackProtectStrong = 31,
191191
StackProtect = 32,
192+
NoUndef = 33,
192193
}
193194

194195
/// LLVMIntPredicate

compiler/rustc_llvm/llvm-wrapper/LLVMWrapper.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ enum LLVMRustAttribute {
8282
StackProtectReq = 30,
8383
StackProtectStrong = 31,
8484
StackProtect = 32,
85+
NoUndef = 33,
8586
};
8687

8788
typedef struct OpaqueRustString *RustStringRef;

compiler/rustc_llvm/llvm-wrapper/RustWrapper.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,8 @@ static Attribute::AttrKind fromRust(LLVMRustAttribute Kind) {
224224
return Attribute::StackProtectStrong;
225225
case StackProtect:
226226
return Attribute::StackProtect;
227+
case NoUndef:
228+
return Attribute::NoUndef;
227229
}
228230
report_fatal_error("bad AttributeKind");
229231
}

compiler/rustc_middle/src/ty/layout.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3048,9 +3048,10 @@ impl<'tcx> LayoutCx<'tcx, TyCtxt<'tcx>> {
30483048
layout: TyAndLayout<'tcx>,
30493049
offset: Size,
30503050
is_return: bool| {
3051-
// Booleans are always an i1 that needs to be zero-extended.
3051+
// Booleans are always a noundef i1 that needs to be zero-extended.
30523052
if scalar.is_bool() {
30533053
attrs.ext(ArgExtension::Zext);
3054+
attrs.set(ArgAttribute::NoUndef);
30543055
return;
30553056
}
30563057

@@ -3075,6 +3076,11 @@ impl<'tcx> LayoutCx<'tcx, TyCtxt<'tcx>> {
30753076
_ => pointee.size,
30763077
};
30773078

3079+
// `Box`, `&T`, and `&mut T` cannot be undef.
3080+
// Note that this only applies to the value of the pointer itself;
3081+
// this attribute doesn't make it UB for the pointed-to data to be undef.
3082+
attrs.set(ArgAttribute::NoUndef);
3083+
30783084
// `Box` pointer parameters never alias because ownership is transferred
30793085
// `&mut` pointer parameters never alias other parameters,
30803086
// or mutable global data

compiler/rustc_target/src/abi/call/mod.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ mod attr_impl {
7373
// or not to actually emit the attribute. It can also be controlled
7474
// with the `-Zmutable-noalias` debugging option.
7575
const NoAliasMutRef = 1 << 6;
76+
const NoUndef = 1 << 7;
7677
}
7778
}
7879
}
@@ -494,7 +495,11 @@ impl<'a, Ty> ArgAbi<'a, Ty> {
494495
// For non-immediate arguments the callee gets its own copy of
495496
// the value on the stack, so there are no aliases. It's also
496497
// program-invisible so can't possibly capture
497-
attrs.set(ArgAttribute::NoAlias).set(ArgAttribute::NoCapture).set(ArgAttribute::NonNull);
498+
attrs
499+
.set(ArgAttribute::NoAlias)
500+
.set(ArgAttribute::NoCapture)
501+
.set(ArgAttribute::NonNull)
502+
.set(ArgAttribute::NoUndef);
498503
attrs.pointee_size = layout.size;
499504
// FIXME(eddyb) We should be doing this, but at least on
500505
// i686-pc-windows-msvc, it results in wrong stack offsets.

src/test/codegen/function-arguments.rs

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ pub struct UnsafeInner {
1111
_field: std::cell::UnsafeCell<i16>,
1212
}
1313

14-
// CHECK: zeroext i1 @boolean(i1 zeroext %x)
14+
// CHECK: noundef zeroext i1 @boolean(i1 noundef zeroext %x)
1515
#[no_mangle]
1616
pub fn boolean(x: bool) -> bool {
1717
x
@@ -64,9 +64,14 @@ pub fn indirect_struct(_: S) {
6464
pub fn borrowed_struct(_: &S) {
6565
}
6666

67+
// CHECK: @raw_struct(%S* %_1)
68+
#[no_mangle]
69+
pub fn raw_struct(_: *const S) {
70+
}
71+
6772
// `Box` can get deallocated during execution of the function, so it should
6873
// not get `dereferenceable`.
69-
// CHECK: noalias nonnull align 4 i32* @_box(i32* noalias nonnull align 4 %x)
74+
// CHECK: noalias noundef nonnull align 4 i32* @_box(i32* noalias noundef nonnull align 4 %x)
7075
#[no_mangle]
7176
pub fn _box(x: Box<i32>) -> Box<i32> {
7277
x
@@ -86,48 +91,58 @@ pub fn struct_return() -> S {
8691
pub fn helper(_: usize) {
8792
}
8893

89-
// CHECK: @slice([0 x i8]* noalias nonnull readonly align 1 %_1.0, [[USIZE]] %_1.1)
94+
// CHECK: @slice([0 x i8]* noalias noundef nonnull readonly align 1 %_1.0, [[USIZE]] %_1.1)
9095
// FIXME #25759 This should also have `nocapture`
9196
#[no_mangle]
9297
pub fn slice(_: &[u8]) {
9398
}
9499

95-
// CHECK: @mutable_slice([0 x i8]* noalias nonnull align 1 %_1.0, [[USIZE]] %_1.1)
100+
// CHECK: @mutable_slice([0 x i8]* noalias noundef nonnull align 1 %_1.0, [[USIZE]] %_1.1)
96101
// FIXME #25759 This should also have `nocapture`
97102
#[no_mangle]
98103
pub fn mutable_slice(_: &mut [u8]) {
99104
}
100105

101-
// CHECK: @unsafe_slice([0 x i16]* nonnull align 2 %_1.0, [[USIZE]] %_1.1)
106+
// CHECK: @unsafe_slice([0 x i16]* noundef nonnull align 2 %_1.0, [[USIZE]] %_1.1)
102107
// unsafe interior means this isn't actually readonly and there may be aliases ...
103108
#[no_mangle]
104109
pub fn unsafe_slice(_: &[UnsafeInner]) {
105110
}
106111

107-
// CHECK: @str([0 x i8]* noalias nonnull readonly align 1 %_1.0, [[USIZE]] %_1.1)
112+
// CHECK: @raw_slice([0 x i8]* %_1.0, [[USIZE]] %_1.1)
113+
#[no_mangle]
114+
pub fn raw_slice(_: *const [u8]) {
115+
}
116+
117+
// CHECK: @str([0 x i8]* noalias noundef nonnull readonly align 1 %_1.0, [[USIZE]] %_1.1)
108118
// FIXME #25759 This should also have `nocapture`
109119
#[no_mangle]
110120
pub fn str(_: &[u8]) {
111121
}
112122

113-
// CHECK: @trait_borrow({}* nonnull align 1 %_1.0, [3 x [[USIZE]]]* noalias readonly align {{.*}} dereferenceable({{.*}}) %_1.1)
123+
// CHECK: @trait_borrow({}* noundef nonnull align 1 %_1.0, [3 x [[USIZE]]]* noalias readonly align {{.*}} dereferenceable({{.*}}) %_1.1)
114124
// FIXME #25759 This should also have `nocapture`
115125
#[no_mangle]
116126
pub fn trait_borrow(_: &Drop) {
117127
}
118128

119-
// CHECK: @trait_box({}* noalias nonnull align 1{{( %0)?}}, [3 x [[USIZE]]]* noalias readonly align {{.*}} dereferenceable({{.*}}){{( %1)?}})
129+
// CHECK: @trait_raw({}* %_1.0, [3 x [[USIZE]]]* noalias readonly align {{.*}} dereferenceable({{.*}}) %_1.1)
130+
#[no_mangle]
131+
pub fn trait_raw(_: *const Drop) {
132+
}
133+
134+
// CHECK: @trait_box({}* noalias noundef nonnull align 1{{( %0)?}}, [3 x [[USIZE]]]* noalias readonly align {{.*}} dereferenceable({{.*}}){{( %1)?}})
120135
#[no_mangle]
121136
pub fn trait_box(_: Box<Drop>) {
122137
}
123138

124-
// CHECK: { i8*, i8* } @trait_option(i8* noalias align 1 %x.0, i8* %x.1)
139+
// CHECK: { i8*, i8* } @trait_option(i8* noalias noundef align 1 %x.0, i8* %x.1)
125140
#[no_mangle]
126141
pub fn trait_option(x: Option<Box<Drop>>) -> Option<Box<Drop>> {
127142
x
128143
}
129144

130-
// CHECK: { [0 x i16]*, [[USIZE]] } @return_slice([0 x i16]* noalias nonnull readonly align 2 %x.0, [[USIZE]] %x.1)
145+
// CHECK: { [0 x i16]*, [[USIZE]] } @return_slice([0 x i16]* noalias noundef nonnull readonly align 2 %x.0, [[USIZE]] %x.1)
131146
#[no_mangle]
132147
pub fn return_slice(x: &[u16]) -> &[u16] {
133148
x
@@ -139,7 +154,7 @@ pub fn enum_id_1(x: Option<Result<u16, u16>>) -> Option<Result<u16, u16>> {
139154
x
140155
}
141156

142-
// CHECK: { i8, i8 } @enum_id_2(i1 zeroext %x.0, i8 %x.1)
157+
// CHECK: { i8, i8 } @enum_id_2(i1 noundef zeroext %x.0, i8 %x.1)
143158
#[no_mangle]
144159
pub fn enum_id_2(x: Option<u8>) -> Option<u8> {
145160
x

src/test/codegen/scalar-pair-bool.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,25 @@
22

33
#![crate_type = "lib"]
44

5-
// CHECK: define{{.*}}{ i8, i8 } @pair_bool_bool(i1 zeroext %pair.0, i1 zeroext %pair.1)
5+
// CHECK: define{{.*}}{ i8, i8 } @pair_bool_bool(i1 noundef zeroext %pair.0, i1 noundef zeroext %pair.1)
66
#[no_mangle]
77
pub fn pair_bool_bool(pair: (bool, bool)) -> (bool, bool) {
88
pair
99
}
1010

11-
// CHECK: define{{.*}}{ i8, i32 } @pair_bool_i32(i1 zeroext %pair.0, i32 %pair.1)
11+
// CHECK: define{{.*}}{ i8, i32 } @pair_bool_i32(i1 noundef zeroext %pair.0, i32 %pair.1)
1212
#[no_mangle]
1313
pub fn pair_bool_i32(pair: (bool, i32)) -> (bool, i32) {
1414
pair
1515
}
1616

17-
// CHECK: define{{.*}}{ i32, i8 } @pair_i32_bool(i32 %pair.0, i1 zeroext %pair.1)
17+
// CHECK: define{{.*}}{ i32, i8 } @pair_i32_bool(i32 %pair.0, i1 noundef zeroext %pair.1)
1818
#[no_mangle]
1919
pub fn pair_i32_bool(pair: (i32, bool)) -> (i32, bool) {
2020
pair
2121
}
2222

23-
// CHECK: define{{.*}}{ i8, i8 } @pair_and_or(i1 zeroext %_1.0, i1 zeroext %_1.1)
23+
// CHECK: define{{.*}}{ i8, i8 } @pair_and_or(i1 noundef zeroext %_1.0, i1 noundef zeroext %_1.1)
2424
#[no_mangle]
2525
pub fn pair_and_or((a, b): (bool, bool)) -> (bool, bool) {
2626
// Make sure it can operate directly on the unpacked args
@@ -30,7 +30,7 @@ pub fn pair_and_or((a, b): (bool, bool)) -> (bool, bool) {
3030
(a && b, a || b)
3131
}
3232

33-
// CHECK: define{{.*}}void @pair_branches(i1 zeroext %_1.0, i1 zeroext %_1.1)
33+
// CHECK: define{{.*}}void @pair_branches(i1 noundef zeroext %_1.0, i1 noundef zeroext %_1.1)
3434
#[no_mangle]
3535
pub fn pair_branches((a, b): (bool, bool)) {
3636
// Make sure it can branch directly on the unpacked bool args

src/test/codegen/transmute-scalar.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ pub fn f32_to_bits(x: f32) -> u32 {
1515
unsafe { std::mem::transmute(x) }
1616
}
1717

18-
// CHECK-LABEL: define{{.*}}i8 @bool_to_byte(i1 zeroext %b)
18+
// CHECK-LABEL: define{{.*}}i8 @bool_to_byte(i1 noundef zeroext %b)
1919
// CHECK: %1 = zext i1 %b to i8
2020
// CHECK-NEXT: store i8 %1, i8* %0
2121
// CHECK-NEXT: %2 = load i8, i8* %0
@@ -25,7 +25,7 @@ pub fn bool_to_byte(b: bool) -> u8 {
2525
unsafe { std::mem::transmute(b) }
2626
}
2727

28-
// CHECK-LABEL: define{{.*}}zeroext i1 @byte_to_bool(i8 %byte)
28+
// CHECK-LABEL: define{{.*}}noundef zeroext i1 @byte_to_bool(i8 %byte)
2929
// CHECK: %1 = trunc i8 %byte to i1
3030
// CHECK-NEXT: %2 = zext i1 %1 to i8
3131
// CHECK-NEXT: store i8 %2, i8* %0

src/test/codegen/union-abi.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ pub union CUnionU128x2{a:(u128, u128)}
7575
pub fn test_CUnionU128x2(_: CUnionU128x2) { loop {} }
7676

7777
pub union UnionBool { b:bool }
78-
// CHECK: define zeroext i1 @test_UnionBool(i8 %b)
78+
// CHECK: define noundef zeroext i1 @test_UnionBool(i8 %b)
7979
#[no_mangle]
8080
pub fn test_UnionBool(b: UnionBool) -> bool { unsafe { b.b } }
8181
// CHECK: %0 = trunc i8 %b to i1

0 commit comments

Comments
 (0)