support select_spectre_guard and select on i128 conditions on all platforms. (#5460)

Fixes #5199.
Fixes #5200.
Fixes #5452.
Fixes #5453.

On riscv64, there is apparently an autoconversion from `ValueRegs` to
`Reg` that takes just the low register [0], and removing this conversion
causes 48 errors. As a result of this, `select` with an `i128` condition
was silently miscompiling, testing only the low 64 bits. We should
remove this autoconversion to ensure we aren't missing any other silent
truncations, but for now this PR just adds the explicit `I128` logic for
`select` / `select_spectre_guard`.

[0]
d9fdbfd50e/cranelift/codegen/src/isa/riscv64/inst.isle (L1762)
This commit is contained in:
Chris Fallin
2022-12-16 14:18:22 -08:00
committed by GitHub
parent d9fdbfd50e
commit 22439f7b39
7 changed files with 64 additions and 22 deletions

View File

@@ -1739,12 +1739,21 @@
(cmp (OperandSize.Size32) rcond (zero_reg))
(Cond.Ne) ty rn rm)))
(rule -3 (lower (has_type ty (select rcond rn rm)))
(rule -3 (lower (has_type ty (select rcond @ (value_type (fits_in_64 _)) rn rm)))
(let ((rcond Reg (put_in_reg_zext64 rcond)))
(lower_select
(cmp (OperandSize.Size64) rcond (zero_reg))
(Cond.Ne) ty rn rm)))
(rule -4 (lower (has_type ty (select rcond @ (value_type $I128) rn rm)))
(let ((c ValueRegs (put_in_regs rcond))
(c_lo Reg (value_regs_get c 0))
(c_hi Reg (value_regs_get c 1))
(rt Reg (orr $I64 c_lo c_hi)))
(lower_select
(cmp (OperandSize.Size64) rt (zero_reg))
(Cond.Ne) ty rn rm)))
;;;; Rules for `select_spectre_guard` ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(rule (lower (has_type ty
@@ -1761,12 +1770,21 @@
(_ InstOutput (side_effect (csdb))))
dst))
(rule -1 (lower (has_type ty (select_spectre_guard rcond rn rm)))
(rule -1 (lower (has_type ty (select_spectre_guard rcond @ (value_type (fits_in_64 _)) rn rm)))
(let ((rcond Reg (put_in_reg_zext64 rcond)))
(lower_select
(cmp (OperandSize.Size64) rcond (zero_reg))
(Cond.Ne) ty rn rm)))
(rule -2 (lower (has_type ty (select_spectre_guard rcond @ (value_type $I128) rn rm)))
(let ((c ValueRegs (put_in_regs rcond))
(c_lo Reg (value_regs_get c 0))
(c_hi Reg (value_regs_get c 1))
(rt Reg (orr $I64 c_lo c_hi)))
(lower_select
(cmp (OperandSize.Size64) rt (zero_reg))
(Cond.Ne) ty rn rm)))
;;;; Rules for `vconst` ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(rule (lower (has_type (ty_vec128 _) (vconst (u128_from_constant x))))

View File

@@ -1899,6 +1899,17 @@
(rule (normalize_cmp_value $I64 r) r)
(rule (normalize_cmp_value $I128 r) r)
;; Convert a truthy value, possibly of more than one register (an
;; I128), to one register. If narrower than 64 bits, must have already
;; been masked (e.g. by `normalize_cmp_value`).
(decl truthy_to_reg (Type ValueRegs) Reg)
(rule 1 (truthy_to_reg (fits_in_64 _) regs)
(value_regs_get regs 0))
(rule 0 (truthy_to_reg $I128 regs)
(let ((lo Reg (value_regs_get regs 0))
(hi Reg (value_regs_get regs 1)))
(alu_rrr (AluOPRRR.Or) lo hi)))
;;;;;
(rule
(lower_branch (brz v @ (value_type ty) _ _) targets)

View File

@@ -612,7 +612,7 @@
;;;;; Rules for `select`;;;;;;;;;
(rule
(lower (has_type ty (select c @ (value_type cty) x y)))
(gen_select ty (normalize_cmp_value cty c) x y))
(gen_select ty (truthy_to_reg cty (normalize_cmp_value cty c)) x y))
(rule 1
(lower (has_type ty (select (icmp cc a b) x y)))
@@ -843,7 +843,7 @@
(rule -1
(lower (has_type ty (select_spectre_guard c @ (value_type cty) x y)))
(gen_select ty (normalize_cmp_value cty c) x y))
(gen_select ty (truthy_to_reg cty (normalize_cmp_value cty c)) x y))
;;;;; Rules for `bmask`;;;;;;;;;
(rule

View File

@@ -1749,6 +1749,10 @@
(gpr_c Gpr (put_in_gpr c)))
(with_flags (x64_test size gpr_c gpr_c) (cmove_from_values ty (CC.NZ) x y))))
(rule -2 (lower (has_type ty (select c @ (value_type $I128) x y)))
(let ((cond_result IcmpCondResult (cmp_zero_i128 (CC.Z) c)))
(select_icmp cond_result x y)))
;; Rules for `clz` ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; If available, we can use a plain lzcnt instruction here. Note no
@@ -2954,7 +2958,11 @@
(rule -1 (lower (has_type ty (select_spectre_guard c @ (value_type (fits_in_64 a_ty)) x y)))
(let ((size OperandSize (raw_operand_size_of_type a_ty))
(gpr_c Gpr (put_in_gpr c)))
(with_flags (x64_test size gpr_c gpr_c) (cmove_from_values ty (CC.NZ) x y))))
(with_flags (x64_test size gpr_c gpr_c) (cmove_from_values ty (CC.NZ) x y))))
(rule -2 (lower (has_type ty (select_spectre_guard c @ (value_type $I128) x y)))
(let ((cond_result IcmpCondResult (cmp_zero_i128 (CC.Z) c)))
(select_icmp cond_result x y)))
;; Rules for `fcvt_from_sint` ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;