Skip to content

Commit ee0e5a4

Browse files
committed
Add range attribute to scalar function results and arguments
1 parent 66e5852 commit ee0e5a4

File tree

9 files changed

+115
-14
lines changed

9 files changed

+115
-14
lines changed

compiler/rustc_codegen_llvm/src/abi.rs

+43-9
Original file line numberDiff line numberDiff line change
@@ -415,9 +415,36 @@ impl<'ll, 'tcx> FnAbiLlvmExt<'ll, 'tcx> for FnAbi<'tcx, Ty<'tcx>> {
415415
i += 1;
416416
i - 1
417417
};
418+
419+
let apply_range_attr = |idx: AttributePlace, abi: rustc_target::abi::Abi| {
420+
if cx.sess().opts.optimize != config::OptLevel::No
421+
&& llvm_util::get_version() >= (19, 0, 0)
422+
{
423+
if let abi::Abi::Scalar(scalar) = abi {
424+
// If the value is a boolean, the range is 0..2 and that ultimately
425+
// become 0..0 when the type becomes i1, which would be rejected
426+
// by the LLVM verifier.
427+
if let Int(..) = scalar.primitive() {
428+
if !scalar.is_bool() && !scalar.is_always_valid(cx) {
429+
attributes::apply_to_llfn(
430+
llfn,
431+
idx,
432+
&[llvm::CreateRangeAttr(
433+
cx.llcx,
434+
scalar.size(cx),
435+
scalar.valid_range(cx),
436+
)],
437+
);
438+
}
439+
}
440+
}
441+
}
442+
};
443+
418444
match &self.ret.mode {
419445
PassMode::Direct(attrs) => {
420446
attrs.apply_attrs_to_llfn(llvm::AttributePlace::ReturnValue, cx, llfn);
447+
apply_range_attr(llvm::AttributePlace::ReturnValue, self.ret.layout.abi);
421448
}
422449
PassMode::Indirect { attrs, meta_attrs: _, on_stack } => {
423450
assert!(!on_stack);
@@ -456,8 +483,11 @@ impl<'ll, 'tcx> FnAbiLlvmExt<'ll, 'tcx> for FnAbi<'tcx, Ty<'tcx>> {
456483
);
457484
attributes::apply_to_llfn(llfn, llvm::AttributePlace::Argument(i), &[byval]);
458485
}
459-
PassMode::Direct(attrs)
460-
| PassMode::Indirect { attrs, meta_attrs: None, on_stack: false } => {
486+
PassMode::Direct(attrs) => {
487+
let i = apply(attrs);
488+
apply_range_attr(llvm::AttributePlace::Argument(i), arg.layout.abi);
489+
}
490+
PassMode::Indirect { attrs, meta_attrs: None, on_stack: false } => {
461491
apply(attrs);
462492
}
463493
PassMode::Indirect { attrs, meta_attrs: Some(meta_attrs), on_stack } => {
@@ -517,13 +547,17 @@ impl<'ll, 'tcx> FnAbiLlvmExt<'ll, 'tcx> for FnAbi<'tcx, Ty<'tcx>> {
517547
}
518548
_ => {}
519549
}
520-
if let abi::Abi::Scalar(scalar) = self.ret.layout.abi {
521-
// If the value is a boolean, the range is 0..2 and that ultimately
522-
// become 0..0 when the type becomes i1, which would be rejected
523-
// by the LLVM verifier.
524-
if let Int(..) = scalar.primitive() {
525-
if !scalar.is_bool() && !scalar.is_always_valid(bx) {
526-
bx.range_metadata(callsite, scalar.valid_range(bx));
550+
if bx.cx.sess().opts.optimize != config::OptLevel::No
551+
&& llvm_util::get_version() < (19, 0, 0)
552+
{
553+
if let abi::Abi::Scalar(scalar) = self.ret.layout.abi {
554+
// If the value is a boolean, the range is 0..2 and that ultimately
555+
// become 0..0 when the type becomes i1, which would be rejected
556+
// by the LLVM verifier.
557+
if let Int(..) = scalar.primitive() {
558+
if !scalar.is_bool() && !scalar.is_always_valid(bx) {
559+
bx.range_metadata(callsite, scalar.valid_range(bx));
560+
}
527561
}
528562
}
529563
}

compiler/rustc_codegen_llvm/src/llvm/ffi.rs

+6
Original file line numberDiff line numberDiff line change
@@ -1574,6 +1574,12 @@ extern "C" {
15741574
pub fn LLVMRustCreateAllocSizeAttr(C: &Context, size_arg: u32) -> &Attribute;
15751575
pub fn LLVMRustCreateAllocKindAttr(C: &Context, size_arg: u64) -> &Attribute;
15761576
pub fn LLVMRustCreateMemoryEffectsAttr(C: &Context, effects: MemoryEffects) -> &Attribute;
1577+
pub fn LLVMRustCreateRangeAttribute(
1578+
C: &Context,
1579+
num_bits: c_uint,
1580+
lower_words: *const u64,
1581+
upper_words: *const u64,
1582+
) -> &Attribute;
15771583

15781584
// Operations on functions
15791585
pub fn LLVMRustGetOrInsertFunction<'a>(

compiler/rustc_codegen_llvm/src/llvm/mod.rs

+16-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use std::string::FromUtf8Error;
88
use libc::c_uint;
99
use rustc_data_structures::small_c_str::SmallCStr;
1010
use rustc_llvm::RustString;
11-
use rustc_target::abi::Align;
11+
use rustc_target::abi::{Align, Size, WrappingRange};
1212

1313
pub use self::AtomicRmwBinOp::*;
1414
pub use self::CallConv::*;
@@ -105,6 +105,21 @@ pub fn CreateAllocKindAttr(llcx: &Context, kind_arg: AllocKindFlags) -> &Attribu
105105
unsafe { LLVMRustCreateAllocKindAttr(llcx, kind_arg.bits()) }
106106
}
107107

108+
pub fn CreateRangeAttr(llcx: &Context, size: Size, range: WrappingRange) -> &Attribute {
109+
let lower = range.start;
110+
let upper = range.end.wrapping_add(1);
111+
let lower_words = [lower as u64, (lower >> 64) as u64];
112+
let upper_words = [upper as u64, (upper >> 64) as u64];
113+
unsafe {
114+
LLVMRustCreateRangeAttribute(
115+
llcx,
116+
size.bits().try_into().unwrap(),
117+
lower_words.as_ptr(),
118+
upper_words.as_ptr(),
119+
)
120+
}
121+
}
122+
108123
#[derive(Copy, Clone)]
109124
pub enum AttributePlace {
110125
ReturnValue,

compiler/rustc_llvm/llvm-wrapper/RustWrapper.cpp

+12
Original file line numberDiff line numberDiff line change
@@ -397,6 +397,18 @@ LLVMRustCreateAllocSizeAttr(LLVMContextRef C, uint32_t ElementSizeArg) {
397397
std::nullopt));
398398
}
399399

400+
extern "C" LLVMAttributeRef
401+
LLVMRustCreateRangeAttribute(LLVMContextRef C, unsigned NumBits,
402+
const uint64_t LowerWords[],
403+
const uint64_t UpperWords[]) {
404+
#if LLVM_VERSION_GE(19, 0)
405+
return LLVMCreateConstantRangeAttribute(C, Attribute::Range, NumBits,
406+
LowerWords, UpperWords);
407+
#else
408+
report_fatal_error("LLVM 19.0 is required for Range Attribute");
409+
#endif
410+
}
411+
400412
// These values **must** match ffi::AllocKindFlags.
401413
// It _happens_ to match the LLVM values of llvm::AllocFnKind,
402414
// but that's happenstance and we do explicit conversions before

tests/codegen/call-metadata.rs

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// scalar value.
33

44
//@ compile-flags: -O -C no-prepopulate-passes
5+
//@ ignore-llvm-version: 19 - 99
56

67
#![crate_type = "lib"]
78

tests/codegen/enum/enum-match.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ pub enum Enum1 {
3434

3535
// CHECK: define noundef{{( range\(i8 [0-9]+, [0-9]+\))?}} i8 @match1{{.*}}
3636
// CHECK-NEXT: start:
37-
// CHECK-NEXT: %1 = add i8 %0, -2
37+
// CHECK-NEXT: %1 = add{{( nsw)?}} i8 %0, -2
3838
// CHECK-NEXT: %2 = zext i8 %1 to i64
3939
// CHECK-NEXT: %3 = icmp ult i8 %1, 2
4040
// CHECK-NEXT: %4 = add nuw nsw i64 %2, 1

tests/codegen/function-arguments.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ pub fn maybeuninit_enum_bool(x: MaybeUninit<MyBool>) -> MaybeUninit<MyBool> {
5050
x
5151
}
5252

53-
// CHECK: noundef i32 @char(i32 noundef %x)
53+
// CHECK: noundef{{( range\(i32 0, 1114112\))?}} i32 @char(i32 noundef{{( range\(i32 0, 1114112\))?}} %x)
5454
#[no_mangle]
5555
pub fn char(x: char) -> char {
5656
x
@@ -68,7 +68,7 @@ pub fn int(x: u64) -> u64 {
6868
x
6969
}
7070

71-
// CHECK: noundef i64 @nonzero_int(i64 noundef %x)
71+
// CHECK: noundef{{( range\(i64 1, 0\))?}} i64 @nonzero_int(i64 noundef{{( range\(i64 1, 0\))?}} %x)
7272
#[no_mangle]
7373
pub fn nonzero_int(x: NonZero<u64>) -> NonZero<u64> {
7474
x

tests/codegen/range-attribute.rs

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// Checks that range metadata gets emitted on functions result and arguments
2+
// with scalar value.
3+
4+
//@ compile-flags: -O -C no-prepopulate-passes
5+
//@ min-llvm-version: 19
6+
7+
#![crate_type = "lib"]
8+
9+
use std::num::NonZero;
10+
11+
// CHECK: noundef range(i64 1, 0) i64 @nonzero_int(i64 noundef range(i64 1, 0) %x)
12+
#[no_mangle]
13+
pub fn nonzero_int(x: NonZero<u64>) -> NonZero<u64> {
14+
x
15+
}
16+
17+
// CHECK: noundef range(i8 0, 3) i8 @optional_bool(i8 noundef range(i8 0, 3) %x)
18+
#[no_mangle]
19+
fn optional_bool(x: Option<bool>) -> Option<bool> {
20+
x
21+
}
22+
23+
pub enum Enum0 {
24+
A(bool),
25+
B,
26+
C,
27+
}
28+
29+
// CHECK: noundef range(i8 0, 4) i8 @enum_value(i8 noundef range(i8 0, 4) %x)
30+
#[no_mangle]
31+
fn enum_value(x: Enum0) -> Enum0 {
32+
x
33+
}

tests/codegen/repr/transparent.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ pub enum Bool {
7474
FileNotFound,
7575
}
7676

77-
// CHECK: define{{( dso_local)?}} noundef{{( zeroext)?}} i8 @test_Gpz(i8 noundef{{( zeroext)?}} %_1)
77+
// CHECK: define{{( dso_local)?}} noundef{{( zeroext)?( range\(i8 0, 3\))?}} i8 @test_Gpz(i8 noundef{{( zeroext)?( range\(i8 0, 3\))?}} %_1)
7878
#[no_mangle]
7979
pub extern "C" fn test_Gpz(_: GenericPlusZst<Bool>) -> GenericPlusZst<Bool> {
8080
loop {}

0 commit comments

Comments
 (0)