Merge pull request #3282 from afonso360/x64-fix-brtables

cranelift: Fix `br_table` for `i64` types in x64 backend.
This commit is contained in:
Chris Fallin
2021-09-02 09:58:42 -07:00
committed by GitHub
3 changed files with 25 additions and 26 deletions

View File

@@ -7202,24 +7202,34 @@ impl LowerBackend for X64Backend {
Opcode::BrTable => {
let jt_size = targets.len() - 1;
assert!(jt_size <= u32::max_value() as usize);
assert!(jt_size <= u32::MAX as usize);
let jt_size = jt_size as u32;
let ty = ctx.input_ty(branches[0], 0);
let ext_spec = match ty {
types::I128 => panic!("BrTable unimplemented for I128"),
types::I64 => ExtSpec::ZeroExtendTo64,
_ => ExtSpec::ZeroExtendTo32,
};
let idx = extend_input_to_reg(
ctx,
InsnInput {
insn: branches[0],
input: 0,
},
ExtSpec::ZeroExtendTo32,
ext_spec,
);
// Bounds-check (compute flags from idx - jt_size) and branch to default.
ctx.emit(Inst::cmp_rmi_r(
OperandSize::Size32,
RegMemImm::imm(jt_size),
idx,
));
// We only support u32::MAX entries, but we compare the full 64 bit register
// when doing the bounds check.
let cmp_size = if ty == types::I64 {
OperandSize::Size64
} else {
OperandSize::Size32
};
ctx.emit(Inst::cmp_rmi_r(cmp_size, RegMemImm::imm(jt_size), idx));
// Emit the compound instruction that does:
//

View File

@@ -1,6 +1,7 @@
test interpret
test run
target aarch64
target x86_64 machinst
target s390x

View File

@@ -264,27 +264,22 @@ impl Switch {
}
};
let discr = if bx.func.dfg.value_type(discr).bits() > 32 {
// Check for overflow of cast to u32.
let discr = if bx.func.dfg.value_type(discr).bits() > 64 {
// Check for overflow of cast to u32. This is the max supported jump table entries.
let new_block = bx.create_block();
let bigger_than_u32 =
bx.ins()
.icmp_imm(IntCC::UnsignedGreaterThan, discr, u32::max_value() as i64);
.icmp_imm(IntCC::UnsignedGreaterThan, discr, u32::MAX as i64);
bx.ins().brnz(bigger_than_u32, otherwise, &[]);
bx.ins().jump(new_block, &[]);
bx.seal_block(new_block);
bx.switch_to_block(new_block);
// Cast to u32, as br_table is not implemented for integers bigger than 32bits.
let discr = if bx.func.dfg.value_type(discr) == types::I128 {
// Cast to u64, as br_table is not implemented for i128
bx.ins().isplit(discr).0
} else {
discr
};
bx.ins().ireduce(types::I32, discr)
} else {
discr
};
bx.ins().br_table(discr, otherwise, jump_table);
}
@@ -616,13 +611,7 @@ block0:
jump block4
block4:
v1 = icmp_imm.i64 ugt v0, 0xffff_ffff
brnz v1, block3
jump block5
block5:
v2 = ireduce.i32 v0
br_table v2, block3, jt0"
br_table.i64 v0, block3, jt0"
);
}
@@ -663,8 +652,7 @@ block4:
block5:
v2, v3 = isplit.i128 v0
v4 = ireduce.i32 v2
br_table v4, block3, jt0"
br_table v2, block3, jt0"
);
}
}