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

How to replace one instruction with a block #217

Open
chenyan2002 opened this issue Aug 3, 2021 · 0 comments
Open

How to replace one instruction with a block #217

chenyan2002 opened this issue Aug 3, 2021 · 0 comments
Labels
question Further information is requested

Comments

@chenyan2002
Copy link

chenyan2002 commented Aug 3, 2021

Summary

We can use visit_instr_mut to easily replace one instruction with another instruction. But how to replace the instruction with a block? It seems there is no direct way of creating InstrSeqBuilder, and I need to use FunctionBuilder and dangling_instr_seq. The following code compiles, but got a runtime error. Any help?

thread 'main' panicked at 'assertion failed: `(left == right)`
  left: `30`,
 right: `387`', /.../walrus-0.19.0/src/module/functions/local_function/mod.rs:109:14

[Update] I can create a new function, and replace the instruction to call the new function. Is this an expected pattern, or there is a better way?

Additional Details

    struct Replacer<'a> {
        types: &'a mut ModuleTypes,
        func_id: FunctionId,
    }
    impl<'a> ir::VisitorMut for Replacer<'a> {
        fn visit_instr_mut(&mut self, instr: &mut ir::Instr, _instr_loc: &mut ir::InstrLocId) {
            if let ir::Instr::Call(ir::Call { func }) = instr {
                if *func == self.func_id {
                    let mut builder = FunctionBuilder::new(self.types, &[], &[]);
                    let mut seq = builder.dangling_instr_seq(None);
                    seq.drop().i32_const(...).call(...);
                    *instr = ir::Block { seq: seq.id() }.into();
                }
            }
        }
    }
@chenyan2002 chenyan2002 added the question Further information is requested label Aug 3, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

1 participant