Skip to content

Commit d9720ea

Browse files
tekknolagik0kubun
authored andcommitted
Add constant folding pass (Shopify/zjit#74)
Add a constant folding pass for HIR. This pass uses the types left by `infer_types` to fold away: * `GuardType(val, ty)` where `val` is already `ty` * `FixnumAdd(l, r)` where `l` and `r` are constants * `FixnumLt(l, r)` where `l` and `r` are constants * `FixnumEq(l, r)` where `l` and `r` are constants * `Test(v)`, where `v` is a constant * `IfTrue(v)` and `IfFalse(v)` where `v` is a constant It does this by using the union-find data structure in the IR to avoid a complicated manual find-and-replace process for all the uses. Right now, for branch instructions that it deletes, we remove from the block completely. This is safe only because we have ensured that nothing refers to branch instructions---they produce no values. However, if we want to instead replace them with Nops that get ignored during codegen or removed by a later DCE pass, that works for me as well.
1 parent 97a478f commit d9720ea

File tree

3 files changed

+340
-27
lines changed

3 files changed

+340
-27
lines changed

zjit/src/codegen.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,13 +80,14 @@ fn iseq_gen_entry_point(iseq: IseqPtr) -> *const u8 {
8080
// with_vm_lock() does nothing if the program doesn't use Ractors.
8181
with_vm_lock(src_loc!(), || {
8282
// Compile ISEQ into High-level IR
83-
let ssa = match hir::iseq_to_hir(iseq) {
83+
let mut ssa = match hir::iseq_to_hir(iseq) {
8484
Ok(ssa) => ssa,
8585
Err(err) => {
8686
debug!("ZJIT: iseq_to_hir: {:?}", err);
8787
return std::ptr::null();
8888
}
8989
};
90+
ssa.optimize();
9091

9192
// Compile High-level IR into machine code
9293
let cb = ZJITState::get_code_block();

0 commit comments

Comments
 (0)