Skip to content

Commit

Permalink
Implement the 32 bit case of llvm.ctpop.
Browse files Browse the repository at this point in the history
  • Loading branch information
ltratt committed Jan 27, 2025
1 parent e3f549d commit d35acf3
Showing 1 changed file with 40 additions and 0 deletions.
40 changes: 40 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.ctpop") => {
let [op] = args.try_into().unwrap();
self.cg_ctpop(iidx, op);
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);
Expand Down Expand Up @@ -1692,6 +1697,22 @@ impl<'a> Assemble<'a> {
}
}

fn cg_ctpop(&mut self, iidx: InstIdx, op: Operand) {
let bitw = op.bitw(self.m);
let [in_reg, out_reg] = self.ra.assign_gp_regs(
&mut self.asm,
iidx,
[
RegConstraint::Input(op.clone()),
RegConstraint::OutputCanBeSameAsInput(op),
],
);
match bitw {
32 => dynasm!(self.asm; popcnt Rd(out_reg.code()), Rd(in_reg.code())),
x => todo!("{x}"),
}
}

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);
Expand Down Expand Up @@ -3923,6 +3944,25 @@ mod tests {
);
}

#[test]
fn cg_call_ctpop() {
codegen_and_test(
"
func_decl llvm.ctpop.i32 (i32) -> i32
entry:
%0: i32 = param 0
%1: i32 = call @llvm.ctpop.i32(%0)
black_box %1
",
"
...
; %1: i32 = call @llvm.ctpop.i32(%0)
popcnt r.32._, r.32._
",
false,
);
}

#[test]
fn cg_call_smax() {
codegen_and_test(
Expand Down

0 comments on commit d35acf3

Please sign in to comment.