Skip to content

Commit c04c0b9

Browse files
read_target_uint => read_target_ptr (nfc)
Most public callers of read_target_uint are using it for the purpose of acquiring a pointer-sized uint. Using read_target_ptr instead makes this usage more clear, and exploits the knowledge of the caller to make the function into a clear "table lookup". This enables making read_target_uint private, and is intended to help simplify refactoring the interpreter in rustc_middle. In cg_clif, one usage of read_target_uint was to obtain a u32, which has been specialized to match the exact numeric width.
1 parent e00196d commit c04c0b9

File tree

5 files changed

+40
-20
lines changed

5 files changed

+40
-20
lines changed

compiler/rustc_codegen_cranelift/src/constant.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use rustc_data_structures::fx::FxHashSet;
66
use rustc_errors::ErrorReported;
77
use rustc_middle::middle::codegen_fn_attrs::CodegenFnAttrFlags;
88
use rustc_middle::mir::interpret::{
9-
read_target_uint, AllocId, Allocation, ConstValue, ErrorHandled, GlobalAlloc, Pointer, Scalar,
9+
read_target_ptr, AllocId, Allocation, ConstValue, ErrorHandled, GlobalAlloc, Pointer, Scalar,
1010
};
1111
use rustc_middle::ty::ConstKind;
1212

@@ -379,11 +379,11 @@ fn define_all_allocs(tcx: TyCtxt<'_>, module: &mut dyn Module, cx: &mut Constant
379379
let addend = {
380380
let endianness = tcx.data_layout.endian;
381381
let offset = offset.bytes() as usize;
382-
let ptr_size = tcx.data_layout.pointer_size;
382+
let ptr_size = tcx.data_layout.pointer_size.bytes() as usize;
383383
let bytes = &alloc.inspect_with_uninit_and_ptr_outside_interpreter(
384-
offset..offset + ptr_size.bytes() as usize,
384+
offset..offset + ptr_size
385385
);
386-
read_target_uint(endianness, bytes).unwrap()
386+
read_target_ptr(endianness, ptr_size, bytes)
387387
};
388388

389389
let reloc_target_alloc = tcx.get_global_alloc(reloc).unwrap();

compiler/rustc_codegen_cranelift/src/intrinsics/simd.rs

+7-4
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,8 @@ pub(super) fn codegen_simd_intrinsic_call<'tcx>(
8383

8484
let indexes = {
8585
use rustc_middle::mir::interpret::*;
86+
use rustc_target::abi::Endian;
87+
8688
let idx_const = crate::constant::mir_operand_get_const_val(fx, idx).expect("simd_shuffle* idx not const");
8789

8890
let idx_bytes = match idx_const {
@@ -96,10 +98,11 @@ pub(super) fn codegen_simd_intrinsic_call<'tcx>(
9698

9799
(0..ret_lane_count).map(|i| {
98100
let i = usize::try_from(i).unwrap();
99-
let idx = rustc_middle::mir::interpret::read_target_uint(
100-
fx.tcx.data_layout.endian,
101-
&idx_bytes[4*i.. 4*i + 4],
102-
).expect("read_target_uint");
101+
let idx = match (fx.tcx.data_layout.endian, &idx_bytes[4*i.. 4*i + 4]) {
102+
(Endian::Little, &[a, b, c, d]) => u32::from_le_bytes([a, b, c, d]),
103+
(Endian::Big, &[a, b, c, d]) => u32::from_be_bytes([a, b, c, d]),
104+
(_, _) => panic!("cg_clif SIMD: can't get idx")
105+
};
103106
u16::try_from(idx).expect("try_from u32")
104107
}).collect::<Vec<u16>>()
105108
};

compiler/rustc_codegen_llvm/src/consts.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use rustc_codegen_ssa::traits::*;
1111
use rustc_hir::def_id::DefId;
1212
use rustc_middle::middle::codegen_fn_attrs::{CodegenFnAttrFlags, CodegenFnAttrs};
1313
use rustc_middle::mir::interpret::{
14-
read_target_uint, Allocation, ErrorHandled, GlobalAlloc, Pointer,
14+
read_target_ptr, Allocation, ErrorHandled, GlobalAlloc, Pointer,
1515
};
1616
use rustc_middle::mir::mono::MonoItem;
1717
use rustc_middle::ty::{self, Instance, Ty};
@@ -22,6 +22,7 @@ use tracing::debug;
2222
pub fn const_alloc_to_llvm(cx: &CodegenCx<'ll, '_>, alloc: &Allocation) -> &'ll Value {
2323
let mut llvals = Vec::with_capacity(alloc.relocations().len() + 1);
2424
let dl = cx.data_layout();
25+
let endian = dl.endian;
2526
let pointer_size = dl.pointer_size.bytes() as usize;
2627

2728
let mut next_offset = 0;
@@ -39,16 +40,15 @@ pub fn const_alloc_to_llvm(cx: &CodegenCx<'ll, '_>, alloc: &Allocation) -> &'ll
3940
let bytes = alloc.inspect_with_uninit_and_ptr_outside_interpreter(next_offset..offset);
4041
llvals.push(cx.const_bytes(bytes));
4142
}
42-
let ptr_offset = read_target_uint(
43-
dl.endian,
43+
44+
let ptr_offset = read_target_ptr(
45+
endian,
46+
pointer_size,
4447
// This `inspect` is okay since it is within the bounds of the allocation, it doesn't
4548
// affect interpreter execution (we inspect the result after interpreter execution),
4649
// and we properly interpret the relocation as a relocation pointer offset.
4750
alloc.inspect_with_uninit_and_ptr_outside_interpreter(offset..(offset + pointer_size)),
48-
)
49-
.expect("const_alloc_to_llvm: could not read relocation pointer")
50-
as u64;
51-
51+
);
5252
let address_space = match cx.tcx.global_alloc(alloc_id) {
5353
GlobalAlloc::Function(..) => cx.data_layout().instruction_address_space,
5454
GlobalAlloc::Static(..) | GlobalAlloc::Memory(..) => AddressSpace::DATA,

compiler/rustc_middle/src/mir/interpret/mod.rs

+18-1
Original file line numberDiff line numberDiff line change
@@ -571,7 +571,7 @@ fn write_target_uint(
571571
}
572572

573573
#[inline]
574-
pub fn read_target_uint(endianness: Endian, mut source: &[u8]) -> Result<u128, io::Error> {
574+
fn read_target_uint(endianness: Endian, mut source: &[u8]) -> Result<u128, io::Error> {
575575
// This u128 holds an "any-size uint" (since smaller uints can fits in it)
576576
let mut buf = [0u8; std::mem::size_of::<u128>()];
577577
// So we do not read exactly 16 bytes into the u128, just the "payload".
@@ -588,3 +588,20 @@ pub fn read_target_uint(endianness: Endian, mut source: &[u8]) -> Result<u128, i
588588
debug_assert!(source.len() == 0); // We should have consumed the source buffer.
589589
uint
590590
}
591+
592+
/// Read a pointer-sized thing from a target, so it will fit in a u64
593+
/// as the modules using this don't support wider pointer values
594+
/// FIXME(jubilee): Move this out of here and closer to the things using it!
595+
#[inline]
596+
pub fn read_target_ptr(endian: Endian, ptr_size: usize, bytes: &[u8]) -> u64 {
597+
use core::convert::TryInto;
598+
match (ptr_size, endian) {
599+
(2, Endian::Little) => u16::from_le_bytes(bytes.try_into().unwrap()) as u64,
600+
(2, Endian::Big) => u16::from_be_bytes(bytes.try_into().unwrap()) as u64,
601+
(4, Endian::Little) => u32::from_le_bytes(bytes.try_into().unwrap()) as u64,
602+
(4, Endian::Big) => u32::from_be_bytes(bytes.try_into().unwrap()) as u64,
603+
(8, Endian::Little) => u64::from_le_bytes(bytes.try_into().unwrap()),
604+
(8, Endian::Big) => u64::from_be_bytes(bytes.try_into().unwrap()),
605+
(_, _) => panic!("unknown pointer size and endianness combination"),
606+
}
607+
}

compiler/rustc_mir/src/util/pretty.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use rustc_data_structures::fx::FxHashMap;
1313
use rustc_hir::def_id::{DefId, LOCAL_CRATE};
1414
use rustc_index::vec::Idx;
1515
use rustc_middle::mir::interpret::{
16-
read_target_uint, AllocId, Allocation, ConstValue, GlobalAlloc, Pointer,
16+
read_target_ptr, AllocId, Allocation, ConstValue, GlobalAlloc, Pointer,
1717
};
1818
use rustc_middle::mir::visit::Visitor;
1919
use rustc_middle::mir::*;
@@ -833,6 +833,7 @@ fn write_allocation_bytes<Tag: Copy + Debug, Extra>(
833833
let mut line_start = Size::ZERO;
834834

835835
let ptr_size = tcx.data_layout.pointer_size;
836+
let ptr_bytesize = ptr_size.bytes_usize();
836837

837838
let mut ascii = String::new();
838839

@@ -852,9 +853,8 @@ fn write_allocation_bytes<Tag: Copy + Debug, Extra>(
852853
if let Some(&(tag, target_id)) = alloc.relocations().get(&i) {
853854
// Memory with a relocation must be defined
854855
let j = i.bytes_usize();
855-
let offset = alloc
856-
.inspect_with_uninit_and_ptr_outside_interpreter(j..j + ptr_size.bytes_usize());
857-
let offset = read_target_uint(tcx.data_layout.endian, offset).unwrap();
856+
let offset = alloc.inspect_with_uninit_and_ptr_outside_interpreter(j..j + ptr_bytesize);
857+
let offset = read_target_ptr(tcx.data_layout.endian, ptr_bytesize, offset);
858858
let offset = Size::from_bytes(offset);
859859
let relocation_width = |bytes| bytes * 3;
860860
let ptr = Pointer::new_with_tag(target_id, offset, tag);

0 commit comments

Comments
 (0)