Skip to content
Closed
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
54 changes: 54 additions & 0 deletions crates/celox-backend-arm64/src/isel/extern_call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,3 +167,57 @@ pub(super) fn lower_float(
});
define_extern_result(ctx, block, dst, result);
}

// Native FP operations keep the existing payload register/ABI convention.
pub(super) fn lower_native_float_binary(
ctx: &mut ISelContext,
block: &mut MBlock,
dst: RegisterId,
lhs: RegisterId,
rhs: RegisterId,
op: celox_sir::FloatBinaryOp,
precision: celox_sir::FloatPrecision,
) -> bool {
if matches!(
op,
celox_sir::FloatBinaryOp::Rem | celox_sir::FloatBinaryOp::Pow
) {
return false;
}
let result = ctx.alloc_vreg(SpillDesc::transient());
block.push(MInst::FloatBinary {
dst: result,
lhs: ctx.reg_map.get(lhs),
rhs: ctx.reg_map.get(rhs),
op,
precision,
});
define_extern_result(ctx, block, dst, result);
true
}
pub(super) fn lower_native_float_unary(
ctx: &mut ISelContext,
block: &mut MBlock,
dst: RegisterId,
src: RegisterId,
op: celox_sir::FloatUnaryOp,
precision: celox_sir::FloatPrecision,
) -> bool {
if !matches!(
op,
celox_sir::FloatUnaryOp::Neg
| celox_sir::FloatUnaryOp::ToBool
| celox_sir::FloatUnaryOp::Convert { .. }
) {
return false;
}
let result = ctx.alloc_vreg(SpillDesc::transient());
block.push(MInst::FloatUnary {
dst: result,
src: ctx.reg_map.get(src),
op,
precision,
});
define_extern_result(ctx, block, dst, result);
true
}
8 changes: 8 additions & 0 deletions crates/celox-backend-arm64/src/isel/instruction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1993,6 +1993,11 @@ pub(super) fn lower_instruction(

SIRInstruction::Binary(dst, lhs, op, rhs) => {
if let BinaryOp::Float { op, precision } = op {
if extern_call::lower_native_float_binary(
ctx, block, *dst, *lhs, *rhs, *op, *precision,
) {
return;
}
extern_call::lower_float(
ctx,
block,
Expand Down Expand Up @@ -2793,6 +2798,9 @@ pub(super) fn lower_instruction(

SIRInstruction::Unary(dst, op, src) => {
if let UnaryOp::Float { op, precision } = op {
if extern_call::lower_native_float_unary(ctx, block, *dst, *src, *op, *precision) {
return;
}
extern_call::lower_float(
ctx,
block,
Expand Down
1 change: 1 addition & 0 deletions crates/celox-backend-arm64/src/isel/tests.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
mod floating;
mod partial_load;
mod reductions;
mod shifts;
Expand Down
223 changes: 223 additions & 0 deletions crates/celox-backend-arm64/src/isel/tests/floating.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,223 @@
use super::*;
use celox_sir::{FloatBinaryOp as F, FloatPrecision as P, FloatUnaryOp as U};

fn float_unit(
op: SIRInstruction<RegionedAbsoluteAddr>,
precision: P,
) -> (ExecutionUnit<RegionedAbsoluteAddr>, MemoryLayout) {
let absolute = AbsoluteAddr {
instance_id: InstanceId(0),
var_id: StateObjectId::default(),
};
let address = RegionedAbsoluteAddr::from_absolute_addr(STABLE_REGION, absolute);
let mut unit = scalar_store_unit(address, false);
let width = match precision {
P::F32 => 32,
P::F64 => 64,
};
for ty in unit.register_map.values_mut() {
*ty = RegisterType::Bit {
width,
signed: false,
};
}
let result_width = match &op {
SIRInstruction::Binary(
_,
_,
BinaryOp::Float {
op: F::Eq | F::Ne | F::Lt | F::Le | F::Gt | F::Ge,
..
},
_,
)
| SIRInstruction::Unary(_, UnaryOp::Float { op: U::ToBool, .. }, _) => 1,
SIRInstruction::Unary(
_,
UnaryOp::Float {
op: U::Convert { to: P::F32 },
..
},
_,
) => 32,
SIRInstruction::Unary(
_,
UnaryOp::Float {
op: U::Convert { to: P::F64 },
..
},
_,
) => 64,
_ => width,
};
unit.register_map.insert(
RegisterId(2),
RegisterType::Bit {
width: result_width,
signed: false,
},
);
let block = unit.blocks.get_mut(&SirBlockId(0)).unwrap();
block.instructions[0] = SIRInstruction::Imm(
RegisterId(0),
SIRValue::new(match precision {
P::F32 => 1.5f32.to_bits() as u64,
P::F64 => 1.5f64.to_bits(),
}),
);
block.instructions[1] = SIRInstruction::Imm(
RegisterId(1),
SIRValue::new(match precision {
P::F32 => 2.0f32.to_bits() as u64,
P::F64 => 2.0f64.to_bits(),
}),
);
block.instructions[2] = op;
block.instructions[3] = SIRInstruction::Store(
address,
SIROffset::Static(0),
result_width,
RegisterId(2),
Vec::new(),
Vec::new(),
);
(unit, layout_for(absolute, 64, false))
}

#[test]
fn basic_float_operations_select_native_mir_and_emit_without_helpers() {
for precision in [P::F32, P::F64] {
for op in [
F::Add,
F::Sub,
F::Mul,
F::Div,
F::Eq,
F::Ne,
F::Lt,
F::Le,
F::Gt,
F::Ge,
] {
let (unit, layout) = float_unit(
SIRInstruction::Binary(
RegisterId(2),
RegisterId(0),
BinaryOp::Float { op, precision },
RegisterId(1),
),
precision,
);
let mir = lower_execution_unit(&unit, &layout, false);
assert!(
mir.blocks.iter().flat_map(|b| &b.insts).any(
|i| matches!(i, MInst::FloatBinary { op: selected, .. } if *selected == op)
)
);
assert!(
!mir.blocks
.iter()
.flat_map(|b| &b.insts)
.any(|i| matches!(i, MInst::CallExtern { .. }))
);
let code = crate::scalar::emit_prepared_eu(
&unit,
&layout,
false,
"eval_comb",
false,
None,
|| false,
)
.unwrap();
let opcode = match op {
F::Add => 0x1e21_2800,
F::Sub => 0x1e21_3800,
F::Mul => 0x1e21_0800,
F::Div => 0x1e21_1800,
_ => 0x1e21_2000,
} | if precision == P::F64 { 0x0040_0000 } else { 0 };
assert!(
code.code
.as_chunks::<4>()
.0
.iter()
.any(|word| u32::from_le_bytes(*word) == opcode),
"missing {op:?}.{precision:?} opcode"
);
#[cfg(all(target_arch = "aarch64", target_os = "linux"))]
{
let jit = crate::jit_mem::JitCode::new(&code.code).unwrap();
let mut state = vec![0u8; layout.merged_total_size];
assert_eq!(unsafe { jit.call(&mut state) }, 0);
let a = if precision == P::F32 {
1.5f32.to_bits() as u64
} else {
1.5f64.to_bits()
};
let b = if precision == P::F32 {
2.0f32.to_bits() as u64
} else {
2.0f64.to_bits()
};
assert_eq!(
u64::from_le_bytes(state[..8].try_into().unwrap()),
celox_design::eval_float_binary(op, precision, a, b)
);
}
}
for op in [
U::Neg,
U::ToBool,
U::Convert { to: P::F32 },
U::Convert { to: P::F64 },
] {
let (unit, layout) = float_unit(
SIRInstruction::Unary(
RegisterId(2),
UnaryOp::Float { op, precision },
RegisterId(0),
),
precision,
);
let mir = lower_execution_unit(&unit, &layout, false);
assert!(
mir.blocks
.iter()
.flat_map(|b| &b.insts)
.any(|i| matches!(i, MInst::FloatUnary { .. }))
);
let code = crate::scalar::emit_prepared_eu(
&unit,
&layout,
false,
"eval_comb",
false,
None,
|| false,
)
.unwrap();
assert!(!code.code.is_empty());
}
}
}

#[test]
fn complex_float_operations_keep_helper_calls() {
for op in [F::Rem, F::Pow] {
let (unit, layout) = float_unit(
SIRInstruction::Binary(
RegisterId(2),
RegisterId(0),
BinaryOp::Float {
op,
precision: P::F64,
},
RegisterId(1),
),
P::F64,
);
let mir = lower_execution_unit(&unit, &layout, false);
assert!(mir.blocks.iter().flat_map(|b| &b.insts).any(|i| matches!(i, MInst::CallExtern { func, .. } if *func == celox_sir::float::FLOAT_HELPER_ID)));
}
}
35 changes: 29 additions & 6 deletions crates/celox-backend-arm64/src/mir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,21 @@ impl SparseCommitDescriptor {
/// Word-level AArch64 operations before physical-register substitution.
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) enum MInst {
/// IEEE payloads use ordinary virtual registers; emission stages operands
/// through caller-clobbered SIMD scratch registers for native FP execution.
FloatBinary {
dst: VReg,
lhs: VReg,
rhs: VReg,
op: celox_sir::FloatBinaryOp,
precision: celox_sir::FloatPrecision,
},
FloatUnary {
dst: VReg,
src: VReg,
op: celox_sir::FloatUnaryOp,
precision: celox_sir::FloatPrecision,
},
Mov {
dst: VReg,
src: VReg,
Expand Down Expand Up @@ -686,7 +701,9 @@ pub(crate) enum MInst {
impl MInst {
pub(crate) fn def(&self) -> Option<VReg> {
match self {
Self::Mov { dst, .. }
Self::FloatBinary { dst, .. }
| Self::FloatUnary { dst, .. }
| Self::Mov { dst, .. }
| Self::Mov32 { dst, .. }
| Self::LoadImm { dst, .. }
| Self::BitExtract { dst, .. }
Expand Down Expand Up @@ -771,7 +788,9 @@ impl MInst {
pub(crate) fn def_mut(&mut self) -> Option<&mut VReg> {
match self {
Self::CallExtern { dst, .. } => dst.as_mut(),
Self::Mov { dst, .. }
Self::FloatBinary { dst, .. }
| Self::FloatUnary { dst, .. }
| Self::Mov { dst, .. }
| Self::Mov32 { dst, .. }
| Self::LoadImm { dst, .. }
| Self::BitExtract { dst, .. }
Expand Down Expand Up @@ -883,7 +902,8 @@ impl MInst {
| Self::ReleaseStorePtrIndexed {
ptr, index, src, ..
} => vec![*ptr, *index, *src],
Self::Add { lhs, rhs, .. }
Self::FloatBinary { lhs, rhs, .. }
| Self::Add { lhs, rhs, .. }
| Self::Add32 { lhs, rhs, .. }
| Self::Sub { lhs, rhs, .. }
| Self::Sub32 { lhs, rhs, .. }
Expand All @@ -905,7 +925,8 @@ impl MInst {
| Self::URem { lhs, rhs, .. }
| Self::SDiv { lhs, rhs, .. }
| Self::SRem { lhs, rhs, .. } => vec![*lhs, *rhs],
Self::BitExtract { src, .. }
Self::FloatUnary { src, .. }
| Self::BitExtract { src, .. }
| Self::AndImm { src, .. }
| Self::AndImm32 { src, .. }
| Self::OrImm { src, .. }
Expand Down Expand Up @@ -1019,7 +1040,8 @@ impl MInst {
rewrite(index);
rewrite(src);
}
Self::Add { lhs, rhs, .. }
Self::FloatBinary { lhs, rhs, .. }
| Self::Add { lhs, rhs, .. }
| Self::Add32 { lhs, rhs, .. }
| Self::Sub { lhs, rhs, .. }
| Self::Sub32 { lhs, rhs, .. }
Expand All @@ -1044,7 +1066,8 @@ impl MInst {
rewrite(lhs);
rewrite(rhs);
}
Self::BitExtract { src, .. }
Self::FloatUnary { src, .. }
| Self::BitExtract { src, .. }
| Self::AndImm { src, .. }
| Self::AndImm32 { src, .. }
| Self::OrImm { src, .. }
Expand Down
Loading
Loading