Skip to content

Commit

Permalink
Implement codegen for 64 bit llvm.smax.
Browse files Browse the repository at this point in the history
This is effectively an IR intrinsic, but is encoded as a call.
  • Loading branch information
ltratt committed Jan 26, 2025
1 parent c42bc99 commit e3f549d
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 2 deletions.
45 changes: 45 additions & 0 deletions ykrt/src/compile/jitc_yk/codegen/x64/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1503,6 +1503,11 @@ impl<'a> Assemble<'a> {
"llvm.assume" => Ok(()),
"llvm.lifetime.start.p0" => Ok(()),
"llvm.lifetime.end.p0" => Ok(()),
x if x.starts_with("llvm.smax") => {
let [lhs_op, rhs_op] = args.try_into().unwrap();
self.cg_smax(iidx, lhs_op, rhs_op);
Ok(())
}
x => {
let va = symbol_to_ptr(x).map_err(|e| CompilationError::General(e.to_string()))?;
self.emit_call(iidx, fty, Some(va), None, &args)
Expand Down Expand Up @@ -1687,6 +1692,25 @@ impl<'a> Assemble<'a> {
}
}

fn cg_smax(&mut self, iidx: InstIdx, lhs: Operand, rhs: Operand) {
assert_eq!(lhs.bitw(self.m), rhs.bitw(self.m));
let bitw = lhs.bitw(self.m);
let [lhs_reg, rhs_reg] = self.ra.assign_gp_regs(
&mut self.asm,
iidx,
[RegConstraint::InputOutput(lhs), RegConstraint::Input(rhs)],
);
match bitw {
64 => {
dynasm!(self.asm
; cmp Rq(lhs_reg.code()), Rq(rhs_reg.code())
; cmovl Rq(lhs_reg.code()), Rq(rhs_reg.code())
);
}
x => todo!("{x}"),
}
}

/// Return the [VarLocation] an [Operand] relates to.
fn op_to_var_location(&self, op: Operand) -> VarLocation {
match op {
Expand Down Expand Up @@ -3899,6 +3923,27 @@ mod tests {
);
}

#[test]
fn cg_call_smax() {
codegen_and_test(
"
func_decl llvm.smax.i64 (i64, i64) -> i64
entry:
%0: i64 = param 0
%1: i64 = param 1
%2: i64 = call @llvm.smax.i64(%0, %1)
black_box %2
",
"
...
; %2: i64 = call @llvm.smax.i64(%0, %1)
cmp r.64.a, r.64.b
cmovl r.64.a, r.64.b
",
false,
);
}

#[test]
fn cg_eq() {
codegen_and_test(
Expand Down
4 changes: 2 additions & 2 deletions ykrt/src/compile/jitc_yk/jit_ir/jit_ir.l
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
%%
@[a-zA-Z_.][a-zA-Z_0-9]* "GLOBAL"
@[a-zA-Z_.][a-zA-Z_0-9.]* "GLOBAL"
%[0-9]+ "LOCAL_OPERAND"
i[0-9]+ "INT_TYPE"
float "FLOAT_TYPE"
Expand Down Expand Up @@ -80,7 +80,7 @@ f_true "F_TRUE"
urem "UREM"
xor "XOR"
[a-zA_Z_]+: "LABEL"
[a-zA_Z_][a-zA_Z_0-9]* "ID"
[a-zA_Z_][a-zA_Z_0-9.]* "ID"
volatile "VOLATILE"
\< "<"
\> ">"
Expand Down

0 comments on commit e3f549d

Please sign in to comment.