Remove MachInst::gen_constant (#5427)
* aarch64: constant generation cleanup Add support for MOVZ and MOVN generation via ISLE. Handle f32const, f64const, and nop instructions via ISLE. No longer call Inst::gen_constant from lower.rs. * riscv64: constant generation cleanup Handle f32const, f64const, and nop instructions via ISLE. * s390x: constant generation cleanup Fix rule priorities for "imm" term. Only handle 32-bit stack offsets; no longer use load_constant64. * x64: constant generation cleanup No longer call Inst::gen_constant from lower.rs or abi.rs. * Refactor LowerBackend::lower to return InstOutput No longer write to the per-insn output registers; instead, return an InstOutput vector of temp registers holding the outputs. This will allow calling LowerBackend::lower multiple times for the same instruction, e.g. to rematerialize constants. When emitting the primary copy of the instruction during lowering, writing to the per-insn registers is now done in lower_clif_block. As a result, the ISLE lower_common routine is no longer needed. In addition, the InsnOutput type and all code related to it can be removed as well. * Refactor IsleContext to hold a LowerBackend reference Remove the "triple", "flags", and "isa_flags" fields that are copied from LowerBackend to each IsleContext, and instead just hold a reference to LowerBackend in IsleContext. This will allow calling LowerBackend::lower from within callbacks in src/machinst/isle.rs, e.g. to rematerialize constants. To avoid having to pass LowerBackend references through multiple functions, eliminate the lower_insn_to_regs subroutines in those targets that still have them, and just inline into the main lower routine. This also eliminates lower_inst.rs on aarch64 and riscv64. Replace all accesses to the removed IsleContext fields by going through the LowerBackend reference. * Remove MachInst::gen_constant This addresses the problem described in issue https://github.com/bytecodealliance/wasmtime/issues/4426 that targets currently have to duplicate code to emit constants between the ISLE logic and the gen_constant callback. After the various cleanups in earlier patches in this series, the only remaining user of get_constant is put_value_in_regs in Lower. This can now be removed, and instead constant rematerialization can be performed in the put_in_regs ISLE callback by simply directly calling LowerBackend::lower on the instruction defining the constant (using a different output register). Since the check for egraph mode is now no longer performed in put_value_in_regs, the Lower::flags member becomes obsolete. Care needs to be taken that other calls directly to the Lower::put_value_in_regs routine now handle the fact that no more rematerialization is performed. All such calls in target code already historically handle constants themselves. The remaining call site in the ISLE gen_call_common helper can be redirected to the ISLE put_in_regs callback. The existing target implementations of gen_constant are then unused and can be removed. (In some target there may still be further opportunities to remove duplication between ISLE and some local Rust code - this can be left to future patches.)
This commit is contained in:
@@ -14,9 +14,9 @@ use crate::ir::{
|
||||
Type, Value, ValueDef, ValueLabelAssignments, ValueLabelStart,
|
||||
};
|
||||
use crate::machinst::{
|
||||
non_writable_value_regs, writable_value_regs, BlockIndex, BlockLoweringOrder, Callee,
|
||||
LoweredBlock, MachLabel, Reg, SigSet, VCode, VCodeBuilder, VCodeConstant, VCodeConstantData,
|
||||
VCodeConstants, VCodeInst, ValueRegs, Writable,
|
||||
writable_value_regs, BlockIndex, BlockLoweringOrder, Callee, LoweredBlock, MachLabel, Reg,
|
||||
SigSet, VCode, VCodeBuilder, VCodeConstant, VCodeConstantData, VCodeConstants, VCodeInst,
|
||||
ValueRegs, Writable,
|
||||
};
|
||||
use crate::{trace, CodegenResult};
|
||||
use alloc::vec::Vec;
|
||||
@@ -26,6 +26,9 @@ use std::fmt::Debug;
|
||||
|
||||
use super::{VCodeBuildDirection, VRegAllocator};
|
||||
|
||||
/// A vector of ValueRegs, used to represent the outputs of an instruction.
|
||||
pub type InstOutput = SmallVec<[ValueRegs<Reg>; 2]>;
|
||||
|
||||
/// An "instruction color" partitions CLIF instructions by side-effecting ops.
|
||||
/// All instructions with the same "color" are guaranteed not to be separated by
|
||||
/// any side-effecting op (for this purpose, loads are also considered
|
||||
@@ -121,7 +124,7 @@ pub trait LowerBackend {
|
||||
/// edge (block-param actuals) into registers, because the actual branch
|
||||
/// generation (`lower_branch_group()`) happens *after* any possible merged
|
||||
/// out-edge.
|
||||
fn lower(&self, ctx: &mut Lower<Self::MInst>, inst: Inst) -> CodegenResult<()>;
|
||||
fn lower(&self, ctx: &mut Lower<Self::MInst>, inst: Inst) -> CodegenResult<InstOutput>;
|
||||
|
||||
/// Lower a block-terminating group of branches (which together can be seen
|
||||
/// as one N-way branch), given a vcode MachLabel for each target.
|
||||
@@ -146,9 +149,6 @@ pub struct Lower<'func, I: VCodeInst> {
|
||||
/// The function to lower.
|
||||
f: &'func Function,
|
||||
|
||||
/// Machine-independent flags.
|
||||
flags: crate::settings::Flags,
|
||||
|
||||
/// The set of allocatable registers.
|
||||
allocatable: PRegSet,
|
||||
|
||||
@@ -324,7 +324,6 @@ impl<'func, I: VCodeInst> Lower<'func, I> {
|
||||
/// Prepare a new lowering context for the given IR function.
|
||||
pub fn new(
|
||||
f: &'func Function,
|
||||
flags: crate::settings::Flags,
|
||||
machine_env: &MachineEnv,
|
||||
abi: Callee<I::ABIMachineSpec>,
|
||||
emit_info: I::Info,
|
||||
@@ -415,7 +414,6 @@ impl<'func, I: VCodeInst> Lower<'func, I> {
|
||||
|
||||
Ok(Lower {
|
||||
f,
|
||||
flags,
|
||||
allocatable: PRegSet::from(machine_env),
|
||||
vcode,
|
||||
vregs,
|
||||
@@ -742,7 +740,27 @@ impl<'func, I: VCodeInst> Lower<'func, I> {
|
||||
// or any of its outputs its used.
|
||||
if has_side_effect || value_needed {
|
||||
trace!("lowering: inst {}: {:?}", inst, self.f.dfg[inst]);
|
||||
backend.lower(self, inst)?;
|
||||
let temp_regs = backend.lower(self, inst)?;
|
||||
|
||||
// The ISLE generated code emits its own registers to define the
|
||||
// instruction's lowered values in. However, other instructions
|
||||
// that use this SSA value will be lowered assuming that the value
|
||||
// is generated into a pre-assigned, different, register.
|
||||
//
|
||||
// To connect the two, we set up "aliases" in the VCodeBuilder
|
||||
// that apply when it is building the Operand table for the
|
||||
// regalloc to use. These aliases effectively rewrite any use of
|
||||
// the pre-assigned register to the register that was returned by
|
||||
// the ISLE lowering logic.
|
||||
debug_assert_eq!(temp_regs.len(), self.num_outputs(inst));
|
||||
for i in 0..self.num_outputs(inst) {
|
||||
let regs = temp_regs[i];
|
||||
let dsts = self.value_regs[self.f.dfg.inst_results(inst)[i]];
|
||||
debug_assert_eq!(regs.len(), dsts.len());
|
||||
for (dst, temp) in dsts.regs().iter().zip(regs.regs().iter()) {
|
||||
self.set_vreg_alias(*dst, *temp);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let loc = self.srcloc(inst);
|
||||
@@ -1249,33 +1267,6 @@ impl<'func, I: VCodeInst> Lower<'func, I> {
|
||||
assert!(!self.inst_sunk.contains(&inst));
|
||||
}
|
||||
|
||||
// If the value is a constant, then (re)materialize it at each
|
||||
// use. This lowers register pressure. (Only do this if we are
|
||||
// not using egraph-based compilation; the egraph framework
|
||||
// more efficiently rematerializes constants where needed.)
|
||||
if !self.flags.use_egraphs() {
|
||||
if let Some(c) = self
|
||||
.f
|
||||
.dfg
|
||||
.value_def(val)
|
||||
.inst()
|
||||
.and_then(|inst| self.get_constant(inst))
|
||||
{
|
||||
let ty = self.f.dfg.value_type(val);
|
||||
let regs = self.alloc_tmp(ty);
|
||||
trace!(" -> regs {:?}", regs);
|
||||
assert!(regs.is_valid());
|
||||
|
||||
let insts = I::gen_constant(regs, c.into(), ty, |ty| {
|
||||
self.alloc_tmp(ty).only_reg().unwrap()
|
||||
});
|
||||
for inst in insts {
|
||||
self.emit(inst);
|
||||
}
|
||||
return non_writable_value_regs(regs);
|
||||
}
|
||||
}
|
||||
|
||||
let regs = self.value_regs[val];
|
||||
trace!(" -> regs {:?}", regs);
|
||||
assert!(regs.is_valid());
|
||||
@@ -1284,19 +1275,6 @@ impl<'func, I: VCodeInst> Lower<'func, I> {
|
||||
|
||||
regs
|
||||
}
|
||||
|
||||
/// Get the `idx`th output register(s) of the given IR instruction.
|
||||
///
|
||||
/// When `backend.lower_inst_to_regs(ctx, inst)` is called, it is expected
|
||||
/// that the backend will write results to these output register(s). This
|
||||
/// register will always be "fresh"; it is guaranteed not to overlap with
|
||||
/// any of the inputs, and can be freely used as a scratch register within
|
||||
/// the lowered instruction sequence, as long as its final value is the
|
||||
/// result of the computation.
|
||||
pub fn get_output(&self, ir_inst: Inst, idx: usize) -> ValueRegs<Writable<Reg>> {
|
||||
let val = self.f.dfg.inst_results(ir_inst)[idx];
|
||||
writable_value_regs(self.value_regs[val])
|
||||
}
|
||||
}
|
||||
|
||||
/// Codegen primitives: allocate temps, emit instructions, set result registers,
|
||||
|
||||
Reference in New Issue
Block a user