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:
@@ -8,25 +8,23 @@ use generated_code::{Context, MInst};
|
||||
// Types that the generated ISLE code uses via `use super::*`.
|
||||
use super::{writable_zero_reg, zero_reg};
|
||||
use crate::isa::riscv64::abi::Riscv64ABICaller;
|
||||
use crate::isa::riscv64::settings::Flags as IsaFlags;
|
||||
use crate::isa::riscv64::Riscv64Backend;
|
||||
use crate::machinst::Reg;
|
||||
use crate::machinst::{isle::*, MachInst, SmallInstVec};
|
||||
use crate::machinst::{VCodeConstant, VCodeConstantData};
|
||||
use crate::settings::Flags;
|
||||
use crate::{
|
||||
ir::{
|
||||
immediates::*, types::*, AtomicRmwOp, ExternalName, Inst, InstructionData, MemFlags,
|
||||
StackSlot, TrapCode, Value, ValueList,
|
||||
},
|
||||
isa::riscv64::inst::*,
|
||||
machinst::{ArgPair, InsnOutput, Lower},
|
||||
machinst::{ArgPair, InstOutput, Lower},
|
||||
};
|
||||
use crate::{isle_common_prelude_methods, isle_lower_prelude_methods};
|
||||
use regalloc2::PReg;
|
||||
use std::boxed::Box;
|
||||
use std::convert::TryFrom;
|
||||
use std::vec::Vec;
|
||||
use target_lexicon::Triple;
|
||||
|
||||
type BoxCallInfo = Box<CallInfo>;
|
||||
type BoxCallIndInfo = Box<CallIndInfo>;
|
||||
@@ -38,28 +36,20 @@ use crate::machinst::valueregs;
|
||||
/// The main entry point for lowering with ISLE.
|
||||
pub(crate) fn lower(
|
||||
lower_ctx: &mut Lower<MInst>,
|
||||
flags: &Flags,
|
||||
triple: &Triple,
|
||||
isa_flags: &IsaFlags,
|
||||
outputs: &[InsnOutput],
|
||||
backend: &Riscv64Backend,
|
||||
inst: Inst,
|
||||
) -> Result<(), ()> {
|
||||
lower_common(
|
||||
lower_ctx,
|
||||
triple,
|
||||
flags,
|
||||
isa_flags,
|
||||
outputs,
|
||||
inst,
|
||||
|cx, insn| generated_code::constructor_lower(cx, insn),
|
||||
)
|
||||
) -> Option<InstOutput> {
|
||||
// TODO: reuse the ISLE context across lowerings so we can reuse its
|
||||
// internal heap allocations.
|
||||
let mut isle_ctx = IsleContext { lower_ctx, backend };
|
||||
generated_code::constructor_lower(&mut isle_ctx, inst)
|
||||
}
|
||||
|
||||
impl IsleContext<'_, '_, MInst, Flags, IsaFlags, 6> {
|
||||
impl IsleContext<'_, '_, MInst, Riscv64Backend> {
|
||||
isle_prelude_method_helpers!(Riscv64ABICaller);
|
||||
}
|
||||
|
||||
impl generated_code::Context for IsleContext<'_, '_, MInst, Flags, IsaFlags, 6> {
|
||||
impl generated_code::Context for IsleContext<'_, '_, MInst, Riscv64Backend> {
|
||||
isle_lower_prelude_methods!();
|
||||
isle_prelude_caller_methods!(Riscv64MachineDeps, Riscv64ABICaller);
|
||||
|
||||
@@ -134,7 +124,7 @@ impl generated_code::Context for IsleContext<'_, '_, MInst, Flags, IsaFlags, 6>
|
||||
InstOutput::default()
|
||||
}
|
||||
fn load_ra(&mut self) -> Reg {
|
||||
if self.flags.preserve_frame_pointers() {
|
||||
if self.backend.flags.preserve_frame_pointers() {
|
||||
let tmp = self.temp_writable_reg(I64);
|
||||
self.emit(&MInst::Load {
|
||||
rd: tmp,
|
||||
@@ -198,8 +188,13 @@ impl generated_code::Context for IsleContext<'_, '_, MInst, Flags, IsaFlags, 6>
|
||||
|
||||
fn imm(&mut self, ty: Type, val: u64) -> Reg {
|
||||
let tmp = self.temp_writable_reg(ty);
|
||||
let insts = &MInst::load_constant_u64(tmp, val, &mut |ty| self.temp_writable_reg(ty));
|
||||
self.emit_list(insts);
|
||||
let alloc_tmp = &mut |ty| self.temp_writable_reg(ty);
|
||||
let insts = match ty {
|
||||
F32 => MInst::load_fp_constant32(tmp, val as u32, alloc_tmp),
|
||||
F64 => MInst::load_fp_constant64(tmp, val, alloc_tmp),
|
||||
_ => MInst::load_constant_u64(tmp, val, alloc_tmp),
|
||||
};
|
||||
self.emit_list(&insts);
|
||||
tmp.to_reg()
|
||||
}
|
||||
#[inline]
|
||||
@@ -309,10 +304,10 @@ impl generated_code::Context for IsleContext<'_, '_, MInst, Flags, IsaFlags, 6>
|
||||
}
|
||||
|
||||
fn has_b(&mut self) -> bool {
|
||||
self.isa_flags.has_b()
|
||||
self.backend.isa_flags.has_b()
|
||||
}
|
||||
fn has_zbkb(&mut self) -> bool {
|
||||
self.isa_flags.has_zbkb()
|
||||
self.backend.isa_flags.has_zbkb()
|
||||
}
|
||||
|
||||
fn inst_output_get(&mut self, x: InstOutput, index: u8) -> ValueRegs {
|
||||
@@ -436,7 +431,7 @@ impl generated_code::Context for IsleContext<'_, '_, MInst, Flags, IsaFlags, 6>
|
||||
}
|
||||
}
|
||||
|
||||
impl IsleContext<'_, '_, MInst, Flags, IsaFlags, 6> {
|
||||
impl IsleContext<'_, '_, MInst, Riscv64Backend> {
|
||||
#[inline]
|
||||
fn emit_list(&mut self, list: &SmallInstVec<MInst>) {
|
||||
for i in list {
|
||||
@@ -448,21 +443,14 @@ impl IsleContext<'_, '_, MInst, Flags, IsaFlags, 6> {
|
||||
/// The main entry point for branch lowering with ISLE.
|
||||
pub(crate) fn lower_branch(
|
||||
lower_ctx: &mut Lower<MInst>,
|
||||
triple: &Triple,
|
||||
flags: &Flags,
|
||||
isa_flags: &IsaFlags,
|
||||
backend: &Riscv64Backend,
|
||||
branch: Inst,
|
||||
targets: &[MachLabel],
|
||||
) -> Result<(), ()> {
|
||||
lower_common(
|
||||
lower_ctx,
|
||||
triple,
|
||||
flags,
|
||||
isa_flags,
|
||||
&[],
|
||||
branch,
|
||||
|cx, insn| generated_code::constructor_lower_branch(cx, insn, &targets.to_vec()),
|
||||
)
|
||||
) -> Option<InstOutput> {
|
||||
// TODO: reuse the ISLE context across lowerings so we can reuse its
|
||||
// internal heap allocations.
|
||||
let mut isle_ctx = IsleContext { lower_ctx, backend };
|
||||
generated_code::constructor_lower_branch(&mut isle_ctx, branch, &targets.to_vec())
|
||||
}
|
||||
|
||||
/// construct destination according to ty.
|
||||
|
||||
Reference in New Issue
Block a user