Skip to content

Commit

Permalink
riscv64: Remove support for fixed offset jumps from Jump instructions (
Browse files Browse the repository at this point in the history
…#6988)

* riscv64: Use `MachLabel` for Jal

* riscv64: Use `Inst::gen_jump` for jumps

This is mostly a personal preference. It emits the exact same code.

* riscv64: Use VecMachLabel on BrTable

* riscv64: Remove `BranchTarget::Offset` arm

Replaces it with `Fallthrough` which works the same way with
a fixed offset of 0.

* riscv64: Rename `BranchTarget`

It is now only used for `CondBr`

* riscv64: Panic on fallthrough taken target in condbr
  • Loading branch information
afonso360 authored Sep 11, 2023
1 parent 2186668 commit 4dbc1f6
Show file tree
Hide file tree
Showing 4 changed files with 145 additions and 270 deletions.
52 changes: 23 additions & 29 deletions cranelift/codegen/src/isa/riscv64/inst.isle
Original file line number Diff line number Diff line change
Expand Up @@ -132,11 +132,11 @@

(Jal
;; (rd WritableReg) don't use
(dest BranchTarget))
(label MachLabel))

(CondBr
(taken BranchTarget)
(not_taken BranchTarget)
(taken CondBrTarget)
(not_taken CondBrTarget)
(kind IntegerCompare))

;; Load an inline symbol reference.
Expand Down Expand Up @@ -227,7 +227,7 @@
(index Reg)
(tmp1 WritableReg)
(tmp2 WritableReg)
(targets VecBranchTarget))
(targets VecMachLabel))

;; atomic compare and set operation
(AtomicCas
Expand Down Expand Up @@ -763,7 +763,6 @@
;;;; lowest four bit are used.
(type FenceReq (primitive u8))

(type VecBranchTarget (primitive VecBranchTarget))
(type BoxCallInfo (primitive BoxCallInfo))
(type BoxCallIndInfo (primitive BoxCallIndInfo))
(type BoxReturnCallInfo (primitive BoxReturnCallInfo))
Expand All @@ -777,7 +776,7 @@
(type Imm5 (primitive Imm5))
(type Imm20 (primitive Imm20))
(type Imm3 (primitive Imm3))
(type BranchTarget (primitive BranchTarget))
(type CondBrTarget (primitive CondBrTarget))
(type OptionFloatRoundingMode (primitive OptionFloatRoundingMode))
(type VecU8 (primitive VecU8))
(type AMO (primitive AMO))
Expand Down Expand Up @@ -2660,21 +2659,6 @@
(test XReg (rv_srli sum (imm12_const (ty_bits ty)))))
(value_regs sum test)))

(decl label_to_br_target (MachLabel) BranchTarget)
(extern constructor label_to_br_target label_to_br_target)

(decl gen_jump (MachLabel) MInst)
(rule
(gen_jump v)
(MInst.Jal (label_to_br_target v)))

(decl vec_label_get (VecMachLabel u8) MachLabel )
(extern constructor vec_label_get vec_label_get)

(decl partial lower_branch (Inst VecMachLabel) Unit)
(rule (lower_branch (jump _) targets )
(emit_side_effect (SideEffectNoResult.Inst (gen_jump (vec_label_get targets 0)))))

;;; cc a b targets Type
(decl lower_br_icmp (IntCC ValueRegs ValueRegs VecMachLabel Type) Unit)
(extern constructor lower_br_icmp lower_br_icmp)
Expand Down Expand Up @@ -2718,6 +2702,17 @@
(hi XReg (value_regs_get regs 1)))
(rv_or lo hi)))


(decl label_to_br_target (MachLabel) CondBrTarget)
(extern constructor label_to_br_target label_to_br_target)

(decl vec_label_get (VecMachLabel u8) MachLabel)
(extern constructor vec_label_get vec_label_get)

(decl partial lower_branch (Inst VecMachLabel) Unit)
(rule (lower_branch (jump _) targets )
(emit_side_effect (SideEffectNoResult.Inst (MInst.Jal (vec_label_get targets 0)))))

;; Default behavior for branching based on an input value.
(rule
(lower_branch (brif v @ (value_type ty) _ _) targets)
Expand All @@ -2739,23 +2734,22 @@
(rule 1
(lower_branch (brif (maybe_uextend (fcmp cc a @ (value_type ty) b)) _ _) targets)
(if-let $true (floatcc_unordered cc))
(let ((then BranchTarget (label_to_br_target (vec_label_get targets 0)))
(else BranchTarget (label_to_br_target (vec_label_get targets 1))))
(let ((then CondBrTarget (label_to_br_target (vec_label_get targets 0)))
(else CondBrTarget (label_to_br_target (vec_label_get targets 1))))
(emit_side_effect (cond_br (emit_fcmp (floatcc_complement cc) ty a b) else then))))

(rule 1
(lower_branch (brif (maybe_uextend (fcmp cc a @ (value_type ty) b)) _ _) targets)
(if-let $false (floatcc_unordered cc))
(let ((then BranchTarget (label_to_br_target (vec_label_get targets 0)))
(else BranchTarget (label_to_br_target (vec_label_get targets 1))))
(let ((then CondBrTarget (label_to_br_target (vec_label_get targets 0)))
(else CondBrTarget (label_to_br_target (vec_label_get targets 1))))
(emit_side_effect (cond_br (emit_fcmp cc ty a b) then else))))

;;;

(decl lower_br_table (Reg VecMachLabel) Unit)
(extern constructor lower_br_table lower_br_table)

(rule
(lower_branch (br_table index _) targets)
(rule (lower_branch (br_table index _) targets)
(lower_br_table index targets))

(decl load_ra () Reg)
Expand Down Expand Up @@ -2983,7 +2977,7 @@
(rule (cmp_result_invert result) (CmpResult.Result result $true))

;; Consume a CmpResult, producing a branch on its result.
(decl cond_br (CmpResult BranchTarget BranchTarget) SideEffectNoResult)
(decl cond_br (CmpResult CondBrTarget CondBrTarget) SideEffectNoResult)
(rule (cond_br cmp then else)
(SideEffectNoResult.Inst
(MInst.CondBr then else (cmp_integer_compare cmp))))
Expand Down
Loading

0 comments on commit 4dbc1f6

Please sign in to comment.