Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions compiler/rustc_codegen_llvm/src/llvm/ffi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1929,11 +1929,17 @@ unsafe extern "C" {
C: &Context,
effects: MemoryEffects,
) -> &Attribute;
/// ## Safety
/// - Each of `LowerWords` and `UpperWords` must point to an array that is
/// long enough to fully define an integer of size `NumBits`, i.e. each
/// pointer must point to `NumBits.div_ceil(64)` elements or more.
/// - The implementation will make its own copy of the pointed-to `u64`
/// values, so the pointers only need to outlive this function call.
pub(crate) fn LLVMRustCreateRangeAttribute(
C: &Context,
num_bits: c_uint,
lower_words: *const u64,
upper_words: *const u64,
NumBits: c_uint,
LowerWords: *const u64,
UpperWords: *const u64,
) -> &Attribute;

// Operations on functions
Expand Down
26 changes: 18 additions & 8 deletions compiler/rustc_codegen_llvm/src/llvm/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,16 +112,26 @@ pub(crate) fn CreateAllocKindAttr(llcx: &Context, kind_arg: AllocKindFlags) -> &

pub(crate) fn CreateRangeAttr(llcx: &Context, size: Size, range: WrappingRange) -> &Attribute {
let lower = range.start;
// LLVM treats the upper bound as exclusive, but allows wrapping.
let upper = range.end.wrapping_add(1);
let lower_words = [lower as u64, (lower >> 64) as u64];
let upper_words = [upper as u64, (upper >> 64) as u64];

// Pass each `u128` endpoint value as a `[u64; 2]` array, least-significant part first.
let as_u64_array = |x: u128| [x as u64, (x >> 64) as u64];
let lower_words: [u64; 2] = as_u64_array(lower);
let upper_words: [u64; 2] = as_u64_array(upper);

// To ensure that LLVM doesn't try to read beyond the `[u64; 2]` arrays,
// we must explicitly check that `size_bits` does not exceed 128.
let size_bits = size.bits();
assert!(size_bits <= 128);
// More robust assertions that are redundant with `size_bits <= 128` and
// should be optimized away.
assert!(size_bits.div_ceil(64) <= u64::try_from(lower_words.len()).unwrap());
assert!(size_bits.div_ceil(64) <= u64::try_from(upper_words.len()).unwrap());
let size_bits = c_uint::try_from(size_bits).unwrap();

unsafe {
LLVMRustCreateRangeAttribute(
llcx,
size.bits().try_into().unwrap(),
lower_words.as_ptr(),
upper_words.as_ptr(),
)
LLVMRustCreateRangeAttribute(llcx, size_bits, lower_words.as_ptr(), upper_words.as_ptr())
}
}

Expand Down
3 changes: 3 additions & 0 deletions compiler/rustc_llvm/llvm-wrapper/RustWrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -488,6 +488,9 @@ extern "C" LLVMAttributeRef
LLVMRustCreateRangeAttribute(LLVMContextRef C, unsigned NumBits,
const uint64_t LowerWords[],
const uint64_t UpperWords[]) {
// FIXME(Zalathar): There appears to be no stable guarantee that C++
// `AttrKind` values correspond directly to the `unsigned KindID` values
// accepted by LLVM-C API functions, though in practice they currently do.
return LLVMCreateConstantRangeAttribute(C, Attribute::Range, NumBits,
LowerWords, UpperWords);
}
Expand Down
Loading