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

Skip intermediate PtrAdds. #1468

Closed
wants to merge 1 commit into from
Closed
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
39 changes: 39 additions & 0 deletions ykrt/src/compile/jitc_yk/opt/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,21 @@ impl Opt {
Operand::Var(op_iidx) => {
if x.off() == 0 {
self.m.replace(iidx, Inst::Copy(op_iidx));
} else {
// Skip intermediate `PtrAdds`. For example given:
// %7: ptr = ptr_add %0, 2
// %8: ptr = ptr_add %7, 3
// we can replace the latter with:
// %8: ptr = ptr_add %0, 5
if let Some(Inst::PtrAdd(back_inst)) = self.m.inst_nocopy(op_iidx) {
self.m.replace(
iidx,
Inst::PtrAdd(PtrAddInst::new(
back_inst.ptr(&self.m),
back_inst.off().checked_add(x.off()).unwrap(),
)),
);
}
}
}
},
Expand Down Expand Up @@ -1022,4 +1037,28 @@ mod test {
",
);
}

#[test]
fn opt_ptr_adds() {
Module::assert_ir_transform_eq(
"
entry:
%0: ptr = load_ti 0
%1: ptr = ptr_add %0, 2
%2: ptr = ptr_add %1, 3
black_box %1
black_box %2
",
|m| opt(m).unwrap(),
"
...
entry:
%0: ptr = load_ti ...
%1: ptr = ptr_add %0, 2
%2: ptr = ptr_add %0, 5
black_box %1
black_box %2
",
);
}
}