Skip to content

Commit eebae8d

Browse files
committed
aarch64: Fix incorrect encoding of large const values in icmp.
When encoding constants as immediates into an RSE Imm12 instruction we need to take special care to check if the value that we are trying to input does not overflow its type when viewed as a signed value. (i.e. iconst.i8 200) We cannot both put an immediate and sign extend it, so we need to lower it into a separate reg, and emit the sign extend into the instruction. For more details see the [cg_clif bug report](https://github.com/bjorn3/rustc_codegen_cranelift/issues/1184#issuecomment-873214796).
1 parent f2d2f3a commit eebae8d

File tree

2 files changed

+35
-1
lines changed

2 files changed

+35
-1
lines changed

cranelift/codegen/src/isa/aarch64/lower.rs

+18-1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ use crate::data_value::DataValue;
2424
use log::{debug, trace};
2525
use regalloc::{Reg, Writable};
2626
use smallvec::SmallVec;
27+
use std::cmp;
2728

2829
//============================================================================
2930
// Result enum types.
@@ -156,6 +157,14 @@ impl NarrowValueMode {
156157
NarrowValueMode::ZeroExtend64 | NarrowValueMode::SignExtend64 => false,
157158
}
158159
}
160+
161+
fn is_signed(&self) -> bool {
162+
match self {
163+
NarrowValueMode::SignExtend32 | NarrowValueMode::SignExtend64 => true,
164+
NarrowValueMode::ZeroExtend32 | NarrowValueMode::ZeroExtend64 => false,
165+
NarrowValueMode::None => false,
166+
}
167+
}
159168
}
160169

161170
/// Emits instruction(s) to generate the given constant value into newly-allocated
@@ -438,7 +447,15 @@ pub(crate) fn put_input_in_rse_imm12<C: LowerCtx<I = Inst>>(
438447
) -> ResultRSEImm12 {
439448
if let Some(imm_value) = input_to_const(ctx, input) {
440449
if let Some(i) = Imm12::maybe_from_u64(imm_value) {
441-
return ResultRSEImm12::Imm12(i);
450+
let out_ty_bits = ty_bits(ctx.input_ty(input.insn, input.input));
451+
let is_negative = (i.bits as u64) & (1 << (cmp::max(out_ty_bits, 1) - 1)) != 0;
452+
453+
// This condition can happen if we matched a value that overflows the output type of
454+
// its `iconst` when viewed as a signed value (i.e. iconst.i8 200).
455+
// When that happens we need to lower as a negative value, which we cannot do here.
456+
if !(narrow_mode.is_signed() && is_negative) {
457+
return ResultRSEImm12::Imm12(i);
458+
}
442459
}
443460
}
444461

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
test run
2+
target aarch64
3+
target s390x
4+
target x86_64 machinst
5+
6+
; This test is also a regression test for aarch64.
7+
; We were not correctly handling the fact that the rhs constant value
8+
; overflows its type when viewed as a signed value, and thus encoding the wrong
9+
; value into the resulting instruction.
10+
function %overflow_rhs_const(i8) -> b1 {
11+
block0(v0: i8):
12+
v1 = iconst.i8 192
13+
v2 = icmp sge v0, v1
14+
return v2
15+
}
16+
; run: %test(49) == true
17+
; run: %test(-65) == false

0 commit comments

Comments
 (0)