Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

riscv64: Add vconst lowerings #6324

Merged
merged 14 commits into from
May 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions cranelift/codegen/src/isa/riscv64/inst.isle
Original file line number Diff line number Diff line change
Expand Up @@ -1988,6 +1988,10 @@
(decl gen_amode (Reg Offset32 Type) AMode)
(extern constructor gen_amode gen_amode)

;; Generates a AMode that points to a constant in the constant pool.
(decl gen_const_amode (VCodeConstant) AMode)
(extern constructor gen_const_amode gen_const_amode)

(decl offset32_imm (i32) Offset32)
(extern constructor offset32_imm offset32_imm)

Expand Down
69 changes: 44 additions & 25 deletions cranelift/codegen/src/isa/riscv64/inst/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,19 +35,47 @@ pub enum AMode {
/// clobber pushes. See the diagram in the documentation for
/// [crate::isa::riscv64::abi](the ABI module) for more details.
NominalSPOffset(i64, Type),

/// A reference to a constant which is placed outside of the function's
/// body, typically at the end.
Const(VCodeConstant),

/// A reference to a label.
Label(MachLabel),
}

impl AMode {
pub(crate) fn reg_offset(reg: Reg, imm: i64, ty: Type) -> AMode {
AMode::RegOffset(reg, imm, ty)
pub(crate) fn with_allocs(self, allocs: &mut AllocationConsumer<'_>) -> Self {
match self {
AMode::RegOffset(reg, offset, ty) => AMode::RegOffset(allocs.next(reg), offset, ty),
AMode::SPOffset(..)
| AMode::FPOffset(..)
| AMode::NominalSPOffset(..)
| AMode::Const(..)
| AMode::Label(..) => self,
}
}

/// Returns the registers that known to the register allocator.
/// Keep this in sync with `with_allocs`.
pub(crate) fn get_allocatable_register(&self) -> Option<Reg> {
match self {
AMode::RegOffset(reg, ..) => Some(*reg),
AMode::SPOffset(..)
| AMode::FPOffset(..)
| AMode::NominalSPOffset(..)
| AMode::Const(..)
| AMode::Label(..) => None,
}
}

pub(crate) fn get_base_register(&self) -> Reg {
pub(crate) fn get_base_register(&self) -> Option<Reg> {
match self {
&AMode::RegOffset(reg, ..) => reg,
&AMode::SPOffset(..) => stack_reg(),
&AMode::FPOffset(..) => fp_reg(),
&AMode::NominalSPOffset(..) => stack_reg(),
&AMode::RegOffset(reg, ..) => Some(reg),
&AMode::SPOffset(..) => Some(stack_reg()),
&AMode::FPOffset(..) => Some(fp_reg()),
&AMode::NominalSPOffset(..) => Some(stack_reg()),
&AMode::Const(..) | AMode::Label(..) => None,
}
}

Expand All @@ -64,35 +92,20 @@ impl AMode {
&AMode::SPOffset(offset, _) => offset,
&AMode::FPOffset(offset, _) => offset,
&AMode::NominalSPOffset(offset, _) => offset,
&AMode::Const(_) | &AMode::Label(_) => 0,
}
}

pub(crate) fn to_string_with_alloc(&self, allocs: &mut AllocationConsumer<'_>) -> String {
let reg = self.get_base_register();
let next = allocs.next(reg);
let offset = self.get_offset();
match self {
&AMode::NominalSPOffset(..) => format!("{}", self),
_ => format!("{}({})", offset, reg_name(next),),
}
}

pub(crate) fn to_addr(&self, allocs: &mut AllocationConsumer<'_>) -> String {
let reg = self.get_base_register();
let next = allocs.next(reg);
let offset = self.get_offset();
match self {
&AMode::NominalSPOffset(..) => format!("nsp{:+}", offset),
_ => format!("{}{:+}", reg_name(next), offset),
}
format!("{}", self.clone().with_allocs(allocs))
}
}

impl Display for AMode {
fn fmt(&self, f: &mut Formatter<'_>) -> Result {
match self {
&AMode::RegOffset(r, offset, ..) => {
write!(f, "{}({:?})", offset, r)
write!(f, "{}({})", offset, reg_name(r))
}
&AMode::SPOffset(offset, ..) => {
write!(f, "{}(sp)", offset)
Expand All @@ -103,6 +116,12 @@ impl Display for AMode {
&AMode::FPOffset(offset, ..) => {
write!(f, "{}(fp)", offset)
}
&AMode::Const(addr, ..) => {
write!(f, "[const({})]", addr.as_u32())
}
&AMode::Label(label) => {
write!(f, "[label{}]", label.as_u32())
}
}
}
}
Expand Down
Loading