Skip to content

Commit b872552

Browse files
authoredApr 10, 2023
Rollup merge of #110021 - scottmcm:fix-110005, r=compiler-errors
Fix a couple ICEs in the new `CastKind::Transmute` code Check the sizes of the immediates, rather than the overall types, when deciding whether we can convert types without going through memory. Fixes #110005 Fixes #109992 Fixes #110032 cc `@matthiaskrgr`
2 parents c30d7e9 + d757c4b commit b872552

File tree

4 files changed

+261
-38
lines changed

4 files changed

+261
-38
lines changed
 

Diff for: ‎compiler/rustc_codegen_ssa/src/mir/operand.rs

+25
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,31 @@ impl<'a, 'tcx, V: CodegenObject> OperandRef<'tcx, V> {
259259
}
260260

261261
impl<'a, 'tcx, V: CodegenObject> OperandValue<V> {
262+
/// Returns an `OperandValue` that's generally UB to use in any way.
263+
///
264+
/// Depending on the `layout`, returns an `Immediate` or `Pair` containing
265+
/// poison value(s), or a `Ref` containing a poison pointer.
266+
///
267+
/// Supports sized types only.
268+
pub fn poison<Bx: BuilderMethods<'a, 'tcx, Value = V>>(
269+
bx: &mut Bx,
270+
layout: TyAndLayout<'tcx>,
271+
) -> OperandValue<V> {
272+
assert!(layout.is_sized());
273+
if bx.cx().is_backend_immediate(layout) {
274+
let ibty = bx.cx().immediate_backend_type(layout);
275+
OperandValue::Immediate(bx.const_poison(ibty))
276+
} else if bx.cx().is_backend_scalar_pair(layout) {
277+
let ibty0 = bx.cx().scalar_pair_element_backend_type(layout, 0, true);
278+
let ibty1 = bx.cx().scalar_pair_element_backend_type(layout, 1, true);
279+
OperandValue::Pair(bx.const_poison(ibty0), bx.const_poison(ibty1))
280+
} else {
281+
let bty = bx.cx().backend_type(layout);
282+
let ptr_bty = bx.cx().type_ptr_to(bty);
283+
OperandValue::Ref(bx.const_poison(ptr_bty), None, layout.align.abi)
284+
}
285+
}
286+
262287
pub fn store<Bx: BuilderMethods<'a, 'tcx, Value = V>>(
263288
self,
264289
bx: &mut Bx,

Diff for: ‎compiler/rustc_codegen_ssa/src/mir/rvalue.rs

+71-36
Original file line numberDiff line numberDiff line change
@@ -158,17 +158,6 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
158158
debug_assert!(src.layout.is_sized());
159159
debug_assert!(dst.layout.is_sized());
160160

161-
if src.layout.size != dst.layout.size
162-
|| src.layout.abi.is_uninhabited()
163-
|| dst.layout.abi.is_uninhabited()
164-
{
165-
// In all of these cases it's UB to run this transmute, but that's
166-
// known statically so might as well trap for it, rather than just
167-
// making it unreachable.
168-
bx.abort();
169-
return;
170-
}
171-
172161
if let Some(val) = self.codegen_transmute_operand(bx, src, dst.layout) {
173162
val.store(bx, dst);
174163
return;
@@ -202,8 +191,21 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
202191
operand: OperandRef<'tcx, Bx::Value>,
203192
cast: TyAndLayout<'tcx>,
204193
) -> Option<OperandValue<Bx::Value>> {
205-
// Callers already checked that the layout sizes match
206-
debug_assert_eq!(operand.layout.size, cast.size);
194+
// Check for transmutes that are always UB.
195+
if operand.layout.size != cast.size
196+
|| operand.layout.abi.is_uninhabited()
197+
|| cast.abi.is_uninhabited()
198+
{
199+
if !operand.layout.abi.is_uninhabited() {
200+
// Since this is known statically and the input could have existed
201+
// without already having hit UB, might as well trap for it.
202+
bx.abort();
203+
}
204+
205+
// Because this transmute is UB, return something easy to generate,
206+
// since it's fine that later uses of the value are probably UB.
207+
return Some(OperandValue::poison(bx, cast));
208+
}
207209

208210
let operand_kind = self.value_kind(operand.layout);
209211
let cast_kind = self.value_kind(cast);
@@ -222,10 +224,20 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
222224
bug!("Found {operand_kind:?} for operand {operand:?}");
223225
};
224226
if let OperandValueKind::Immediate(out_scalar) = cast_kind {
225-
let cast_bty = bx.backend_type(cast);
226-
Some(OperandValue::Immediate(Self::transmute_immediate(
227-
bx, imm, in_scalar, out_scalar, cast_bty,
228-
)))
227+
match (in_scalar, out_scalar) {
228+
(ScalarOrZst::Zst, ScalarOrZst::Zst) => {
229+
Some(OperandRef::new_zst(bx, cast).val)
230+
}
231+
(ScalarOrZst::Scalar(in_scalar), ScalarOrZst::Scalar(out_scalar))
232+
if in_scalar.size(self.cx) == out_scalar.size(self.cx) =>
233+
{
234+
let cast_bty = bx.backend_type(cast);
235+
Some(OperandValue::Immediate(
236+
self.transmute_immediate(bx, imm, in_scalar, out_scalar, cast_bty),
237+
))
238+
}
239+
_ => None,
240+
}
229241
} else {
230242
None
231243
}
@@ -234,12 +246,15 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
234246
let OperandValueKind::Pair(in_a, in_b) = operand_kind else {
235247
bug!("Found {operand_kind:?} for operand {operand:?}");
236248
};
237-
if let OperandValueKind::Pair(out_a, out_b) = cast_kind {
249+
if let OperandValueKind::Pair(out_a, out_b) = cast_kind
250+
&& in_a.size(self.cx) == out_a.size(self.cx)
251+
&& in_b.size(self.cx) == out_b.size(self.cx)
252+
{
238253
let out_a_ibty = bx.scalar_pair_element_backend_type(cast, 0, false);
239254
let out_b_ibty = bx.scalar_pair_element_backend_type(cast, 1, false);
240255
Some(OperandValue::Pair(
241-
Self::transmute_immediate(bx, imm_a, in_a, out_a, out_a_ibty),
242-
Self::transmute_immediate(bx, imm_b, in_b, out_b, out_b_ibty),
256+
self.transmute_immediate(bx, imm_a, in_a, out_a, out_a_ibty),
257+
self.transmute_immediate(bx, imm_b, in_b, out_b, out_b_ibty),
243258
))
244259
} else {
245260
None
@@ -254,12 +269,15 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
254269
/// `to_backend_ty` must be the *non*-immediate backend type (so it will be
255270
/// `i8`, not `i1`, for `bool`-like types.)
256271
fn transmute_immediate(
272+
&self,
257273
bx: &mut Bx,
258274
mut imm: Bx::Value,
259275
from_scalar: abi::Scalar,
260276
to_scalar: abi::Scalar,
261277
to_backend_ty: Bx::Type,
262278
) -> Bx::Value {
279+
debug_assert_eq!(from_scalar.size(self.cx), to_scalar.size(self.cx));
280+
263281
use abi::Primitive::*;
264282
imm = bx.from_immediate(imm);
265283
imm = match (from_scalar.primitive(), to_scalar.primitive()) {
@@ -831,14 +849,6 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
831849
let operand_ty = operand.ty(self.mir, self.cx.tcx());
832850
let cast_layout = self.cx.layout_of(self.monomorphize(cast_ty));
833851
let operand_layout = self.cx.layout_of(self.monomorphize(operand_ty));
834-
if operand_layout.size != cast_layout.size
835-
|| operand_layout.abi.is_uninhabited()
836-
|| cast_layout.abi.is_uninhabited()
837-
{
838-
// Send UB cases to the full form so the operand version can
839-
// `bitcast` without worrying about malformed IR.
840-
return false;
841-
}
842852

843853
match (self.value_kind(operand_layout), self.value_kind(cast_layout)) {
844854
// Can always load from a pointer as needed
@@ -847,9 +857,12 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
847857
// Need to generate an `alloc` to get a pointer from an immediate
848858
(OperandValueKind::Immediate(..) | OperandValueKind::Pair(..), OperandValueKind::Ref) => false,
849859

850-
// When we have scalar immediates, we can convert them as needed
851-
(OperandValueKind::Immediate(..), OperandValueKind::Immediate(..)) |
852-
(OperandValueKind::Pair(..), OperandValueKind::Pair(..)) => true,
860+
// When we have scalar immediates, we can only convert things
861+
// where the sizes match, to avoid endianness questions.
862+
(OperandValueKind::Immediate(a), OperandValueKind::Immediate(b)) =>
863+
a.size(self.cx) == b.size(self.cx),
864+
(OperandValueKind::Pair(a0, a1), OperandValueKind::Pair(b0, b1)) =>
865+
a0.size(self.cx) == b0.size(self.cx) && a1.size(self.cx) == b1.size(self.cx),
853866

854867
// Send mixings between scalars and pairs through the memory route
855868
// FIXME: Maybe this could use insertvalue/extractvalue instead?
@@ -887,13 +900,18 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
887900
if self.cx.is_backend_immediate(layout) {
888901
debug_assert!(!self.cx.is_backend_scalar_pair(layout));
889902
OperandValueKind::Immediate(match layout.abi {
890-
abi::Abi::Scalar(s) => s,
891-
abi::Abi::Vector { element, .. } => element,
892-
x => bug!("Couldn't translate {x:?} as backend immediate"),
903+
abi::Abi::Scalar(s) => ScalarOrZst::Scalar(s),
904+
abi::Abi::Vector { element, .. } => ScalarOrZst::Scalar(element),
905+
_ if layout.is_zst() => ScalarOrZst::Zst,
906+
x => span_bug!(self.mir.span, "Couldn't translate {x:?} as backend immediate"),
893907
})
894908
} else if self.cx.is_backend_scalar_pair(layout) {
895909
let abi::Abi::ScalarPair(s1, s2) = layout.abi else {
896-
bug!("Couldn't translate {:?} as backend scalar pair", layout.abi)
910+
span_bug!(
911+
self.mir.span,
912+
"Couldn't translate {:?} as backend scalar pair",
913+
layout.abi,
914+
);
897915
};
898916
OperandValueKind::Pair(s1, s2)
899917
} else {
@@ -902,9 +920,26 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
902920
}
903921
}
904922

923+
/// The variants of this match [`OperandValue`], giving details about the
924+
/// backend values that will be held in that other type.
905925
#[derive(Debug, Copy, Clone)]
906926
enum OperandValueKind {
907927
Ref,
908-
Immediate(abi::Scalar),
928+
Immediate(ScalarOrZst),
909929
Pair(abi::Scalar, abi::Scalar),
910930
}
931+
932+
#[derive(Debug, Copy, Clone)]
933+
enum ScalarOrZst {
934+
Zst,
935+
Scalar(abi::Scalar),
936+
}
937+
938+
impl ScalarOrZst {
939+
pub fn size(self, cx: &impl abi::HasDataLayout) -> abi::Size {
940+
match self {
941+
ScalarOrZst::Zst => abi::Size::ZERO,
942+
ScalarOrZst::Scalar(s) => s.size(cx),
943+
}
944+
}
945+
}

Diff for: ‎tests/codegen/intrinsics/transmute-x64.rs

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// compile-flags: -O -C no-prepopulate-passes
2+
// only-x86_64 (it's using arch-specific types)
3+
// min-llvm-version: 15.0 # this test assumes `ptr`s
4+
5+
#![crate_type = "lib"]
6+
7+
use std::arch::x86_64::{__m128, __m128i, __m256i};
8+
use std::mem::transmute;
9+
10+
// CHECK-LABEL: @check_sse_float_to_int(
11+
#[no_mangle]
12+
pub unsafe fn check_sse_float_to_int(x: __m128) -> __m128i {
13+
// CHECK-NOT: alloca
14+
// CHECK: %1 = load <4 x float>, ptr %x, align 16
15+
// CHECK: store <4 x float> %1, ptr %0, align 16
16+
transmute(x)
17+
}
18+
19+
// CHECK-LABEL: @check_sse_pair_to_avx(
20+
#[no_mangle]
21+
pub unsafe fn check_sse_pair_to_avx(x: (__m128i, __m128i)) -> __m256i {
22+
// CHECK-NOT: alloca
23+
// CHECK: %1 = load <4 x i64>, ptr %x, align 16
24+
// CHECK: store <4 x i64> %1, ptr %0, align 32
25+
transmute(x)
26+
}
27+
28+
// CHECK-LABEL: @check_sse_pair_from_avx(
29+
#[no_mangle]
30+
pub unsafe fn check_sse_pair_from_avx(x: __m256i) -> (__m128i, __m128i) {
31+
// CHECK-NOT: alloca
32+
// CHECK: %1 = load <4 x i64>, ptr %x, align 32
33+
// CHECK: store <4 x i64> %1, ptr %0, align 16
34+
transmute(x)
35+
}

Diff for: ‎tests/codegen/intrinsics/transmute.rs

+130-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
#![feature(inline_const)]
99
#![allow(unreachable_code)]
1010

11-
use std::mem::transmute;
11+
use std::mem::{transmute, MaybeUninit};
1212

1313
// Some of the cases here are statically rejected by `mem::transmute`, so
1414
// we need to generate custom MIR for those cases to get to codegen.
@@ -54,6 +54,32 @@ pub unsafe fn check_smaller_size(x: u32) -> u16 {
5454
}
5555
}
5656

57+
// CHECK-LABEL: @check_smaller_array(
58+
#[no_mangle]
59+
#[custom_mir(dialect = "runtime", phase = "initial")]
60+
pub unsafe fn check_smaller_array(x: [u32; 7]) -> [u32; 3] {
61+
// CHECK: call void @llvm.trap
62+
mir!{
63+
{
64+
RET = CastTransmute(x);
65+
Return()
66+
}
67+
}
68+
}
69+
70+
// CHECK-LABEL: @check_bigger_array(
71+
#[no_mangle]
72+
#[custom_mir(dialect = "runtime", phase = "initial")]
73+
pub unsafe fn check_bigger_array(x: [u32; 3]) -> [u32; 7] {
74+
// CHECK: call void @llvm.trap
75+
mir!{
76+
{
77+
RET = CastTransmute(x);
78+
Return()
79+
}
80+
}
81+
}
82+
5783
// CHECK-LABEL: @check_to_uninhabited(
5884
#[no_mangle]
5985
#[custom_mir(dialect = "runtime", phase = "initial")]
@@ -71,7 +97,7 @@ pub unsafe fn check_to_uninhabited(x: u16) -> BigNever {
7197
#[no_mangle]
7298
#[custom_mir(dialect = "runtime", phase = "initial")]
7399
pub unsafe fn check_from_uninhabited(x: BigNever) -> u16 {
74-
// CHECK: call void @llvm.trap
100+
// CHECK: ret i16 poison
75101
mir!{
76102
{
77103
RET = CastTransmute(x);
@@ -301,3 +327,105 @@ pub unsafe fn check_pair_to_array(x: (i64, u64)) -> [u8; 16] {
301327
// CHECK: store i64 %x.1, ptr %{{.+}}, align 1
302328
transmute(x)
303329
}
330+
331+
// CHECK-LABEL: @check_heterogeneous_integer_pair(
332+
#[no_mangle]
333+
pub unsafe fn check_heterogeneous_integer_pair(x: (i32, bool)) -> (bool, u32) {
334+
// CHECK: store i32 %x.0
335+
// CHECK: %[[WIDER:.+]] = zext i1 %x.1 to i8
336+
// CHECK: store i8 %[[WIDER]]
337+
338+
// CHECK: %[[BYTE:.+]] = load i8
339+
// CHECK: trunc i8 %[[BYTE:.+]] to i1
340+
// CHECK: load i32
341+
transmute(x)
342+
}
343+
344+
// CHECK-LABEL: @check_heterogeneous_float_pair(
345+
#[no_mangle]
346+
pub unsafe fn check_heterogeneous_float_pair(x: (f64, f32)) -> (f32, f64) {
347+
// CHECK: store double %x.0
348+
// CHECK: store float %x.1
349+
// CHECK: %[[A:.+]] = load float
350+
// CHECK: %[[B:.+]] = load double
351+
// CHECK: %[[P:.+]] = insertvalue { float, double } poison, float %[[A]], 0
352+
// CHECK: insertvalue { float, double } %[[P]], double %[[B]], 1
353+
transmute(x)
354+
}
355+
356+
// CHECK-LABEL: @check_issue_110005(
357+
#[no_mangle]
358+
pub unsafe fn check_issue_110005(x: (usize, bool)) -> Option<Box<[u8]>> {
359+
// CHECK: store i64 %x.0
360+
// CHECK: %[[WIDER:.+]] = zext i1 %x.1 to i8
361+
// CHECK: store i8 %[[WIDER]]
362+
// CHECK: load ptr
363+
// CHECK: load i64
364+
transmute(x)
365+
}
366+
367+
// CHECK-LABEL: @check_pair_to_dst_ref(
368+
#[no_mangle]
369+
pub unsafe fn check_pair_to_dst_ref<'a>(x: (usize, usize)) -> &'a [u8] {
370+
// CHECK: %0 = inttoptr i64 %x.0 to ptr
371+
// CHECK: %1 = insertvalue { ptr, i64 } poison, ptr %0, 0
372+
// CHECK: %2 = insertvalue { ptr, i64 } %1, i64 %x.1, 1
373+
// CHECK: ret { ptr, i64 } %2
374+
transmute(x)
375+
}
376+
377+
// CHECK-LABEL: @check_issue_109992(
378+
#[no_mangle]
379+
#[custom_mir(dialect = "runtime", phase = "optimized")]
380+
pub unsafe fn check_issue_109992(x: ()) -> [(); 1] {
381+
// This uses custom MIR to avoid MIR optimizations having removed ZST ops.
382+
383+
// CHECK: start
384+
// CHECK-NEXT: ret void
385+
mir!{
386+
{
387+
RET = CastTransmute(x);
388+
Return()
389+
}
390+
}
391+
}
392+
393+
// CHECK-LABEL: @check_maybe_uninit_pair(i16 %x.0, i64 %x.1)
394+
#[no_mangle]
395+
pub unsafe fn check_maybe_uninit_pair(
396+
x: (MaybeUninit<u16>, MaybeUninit<u64>),
397+
) -> (MaybeUninit<i64>, MaybeUninit<i16>) {
398+
// Thanks to `MaybeUninit` this is actually defined behaviour,
399+
// unlike the examples above with pairs of primitives.
400+
401+
// CHECK: store i16 %x.0
402+
// CHECK: store i64 %x.1
403+
// CHECK: load i64
404+
// CHECK-NOT: noundef
405+
// CHECK: load i16
406+
// CHECK-NOT: noundef
407+
// CHECK: ret { i64, i16 }
408+
transmute(x)
409+
}
410+
411+
#[repr(align(8))]
412+
pub struct HighAlignScalar(u8);
413+
414+
// CHECK-LABEL: @check_to_overalign(
415+
#[no_mangle]
416+
pub unsafe fn check_to_overalign(x: u64) -> HighAlignScalar {
417+
// CHECK: %0 = alloca %HighAlignScalar, align 8
418+
// CHECK: store i64 %x, ptr %0, align 8
419+
// CHECK: %1 = load i64, ptr %0, align 8
420+
// CHECK: ret i64 %1
421+
transmute(x)
422+
}
423+
424+
// CHECK-LABEL: @check_from_overalign(
425+
#[no_mangle]
426+
pub unsafe fn check_from_overalign(x: HighAlignScalar) -> u64 {
427+
// CHECK: %x = alloca %HighAlignScalar, align 8
428+
// CHECK: %[[VAL:.+]] = load i64, ptr %x, align 8
429+
// CHECK: ret i64 %[[VAL]]
430+
transmute(x)
431+
}

0 commit comments

Comments
 (0)
Please sign in to comment.