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
9 changes: 9 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ members = [
"crates/celox-frontend-sdk",
"crates/celox-frontend-sv",
"crates/celox-frontend-veryl",
"crates/celox-heap",
"crates/celox-sir",
"crates/celox-sir-opt",
"crates/celox-slt",
Expand Down Expand Up @@ -65,6 +66,7 @@ celox-frontend-core = { version = "=0.11.0", path = "crates/celox-frontend-c
celox-frontend-sdk = { version = "=0.11.0", path = "crates/celox-frontend-sdk" } # x-release-please-version
celox-frontend-sv = { version = "=0.11.0", path = "crates/celox-frontend-sv" } # x-release-please-version
celox-frontend-veryl = { version = "=0.11.0", path = "crates/celox-frontend-veryl" } # x-release-please-version
celox-heap = { version = "=0.11.0", path = "crates/celox-heap" } # x-release-please-version
celox-macros = { version = "=0.11.0", path = "crates/celox-macros" } # x-release-please-version
celox-napi = { version = "=0.11.0", path = "crates/celox-napi" } # x-release-please-version
celox-runtime = { version = "=0.11.0", path = "crates/celox-runtime" } # x-release-please-version
Expand Down
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)));
}
}
Loading
Loading