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

Deal properly with undefined bits #1554

Merged
merged 2 commits into from
Jan 20, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
18 changes: 13 additions & 5 deletions ykrt/src/compile/jitc_yk/codegen/x64/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2492,8 +2492,16 @@ impl<'a> Assemble<'a> {
RegConstraint::Input(inst.falseval(self.m)),
],
);
dynasm!(self.asm ; cmp Rb(cond_reg.code()), 0);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree that this one is comparing undefined bits too!

dynasm!(self.asm ; cmove Rq(true_reg.code()), Rq(false_reg.code()));
debug_assert_eq!(
self.m
.type_(inst.cond(self.m).tyidx(self.m))
.bit_size()
.unwrap(),
1
);

dynasm!(self.asm ; bt Rq(cond_reg.code()), 0);
dynasm!(self.asm ; cmovnc Rq(true_reg.code()), Rq(false_reg.code()));
}

fn guard_to_deopt(&mut self, inst: &jit_ir::GuardInst) -> DynamicLabel {
Expand Down Expand Up @@ -4422,7 +4430,7 @@ mod tests {
codegen_and_test(
"
entry:
%0: i32 = param 0
%0: i1 = param 0
%1: i32 = %0 ? 1i32 : 2i32
black_box %1
",
Expand All @@ -4431,8 +4439,8 @@ mod tests {
; %1: i32 = %0 ? 1i32 : 2i32
mov r.64.x, 0x01
mov r.64.y, 0x02
cmp r.8.z, 0x00
cmovz r.64.x, r.64.y
bt r.64.z, 0x00
cmovnb r.64.x, r.64.y
...
",
false,
Expand Down
41 changes: 41 additions & 0 deletions ykrt/src/compile/jitc_yk/jit_ir/well_formed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,23 @@ impl Module {
);
}
}
Inst::Select(x) => {
let Ty::Integer(bitsize) = self.type_(x.cond(self).tyidx(self)) else {
panic!();
};
if *bitsize != 1 {
panic!(
"Instruction at position {iidx} trying to select on a non-i1\n {}",
self.inst(iidx).display(self, iidx)
);
}
if x.trueval(self).tyidx(self) != x.falseval(self).tyidx(self) {
panic!(
"Instruction at position {iidx} has incompatible true/false types\n {}",
self.inst(iidx).display(self, iidx)
);
}
}
Inst::SExt(x) => {
let Ty::Integer(val_bitsize) = self.type_(x.val(self).tyidx(self)) else {
panic!();
Expand Down Expand Up @@ -395,6 +412,30 @@ mod tests {
);
}

#[test]
#[should_panic(expected = "Instruction at position 1 trying to select on a non-i1")]
fn select_bad_width() {
Module::from_str(
"
entry:
%0: i32 = param 0
%1: i32 = %0 ? 1i32 : 2i32
",
);
}

#[test]
#[should_panic(expected = "Instruction at position 1 has incompatible true/false types")]
fn select_incompatible_types() {
Module::from_str(
"
entry:
%0: i1 = param 0
%1: i32 = %0 ? 1i16 : 2i32
",
);
}

#[test]
#[should_panic(
expected = "Instruction at position 1 trying to sign extend from an equal-or-larger-than integer type"
Expand Down
Loading