Is it possible to share values between blocks without block parameters? Like if I have a Value created in one block, can I use it in another block without needing block parameters?
Also, the documentation on block parameters seems to be contradictory. For instance: https://docs.rs/cranelift-frontend/latest/cranelift_frontend/struct.FunctionBuilder.html#method.block_params
claims that it
Retrieves all the parameters for a
Blockcurrently inferred from the jump instructions inserted that target it and the SSA construction.
But, the documentation comment for the switch_to_block method on the same page claims that
You must declare the types of the
Blockarguments you will use here
Additionally, bjorn3 said:
If you pass a slice of values as block args, you need to call
append_block_paraman equivalent amount of times on the target block
so what is actually the case? Can the block parameters be inferred by the jump instructions inserted that target it?
Also, the Switch API does not seem to allow us to define the arguments to pass to the blocks that are the cases of the switch statement. Is there a way to do this?
You should be able to use a value in any block so long as the defining block dominates it
Block params are only necessary for non-dominating definitions or where the value in a block differs depending on control flow
Thanks, that makes sense. Unfortunately in my case, the values are different depending on control flow. For my other question, Is it actually true that can the block parameters be inferred by the jump instructions inserted that target it? Or do we need to manually specify block parameter count/types? Also is there a way to pass block parameters in a Switch?
There's no inference, the jump and the block have to have the same number of params. Cranelift doesn't have a "Switch" instruction so I don't know what you're referring to
Alex Crichton said:
Cranelift doesn't have a "Switch" instruction so I don't know what you're referring to
I was referring to this API: https://docs.rs/cranelift-frontend/latest/cranelift_frontend/struct.Switch.html
Alex Crichton said:
There's no inference, the jump and the block have to have the same number of params
I see, so that means we must manually define the block parameters for all blocks right?
Also, could you please explain what it means on this page then: https://docs.rs/cranelift-frontend/latest/cranelift_frontend/struct.FunctionBuilder.html#method.block_params
Where it says that "Retrieves all the parameters for a Block currently inferred from the jump instructions inserted that target it and the SSA construction."
oh sorry, misunderstood! I don't know what that Switch is, so someone else will have to chime in on that.
As for those docs, I fear someone else may also need to weigh in on that. That may be oudated documentation, or that may mean I'm just flat out wrong. Unsure
Got it, I appreciate your help. Hopefully someone has some more information about this, because I am quite confused :sweat_smile:
Is it actually true that can the block parameters be inferred by the jump instructions inserted that target it? Or do we need to manually specify block parameter count/types?
cranelift-frontend will automatically insert block parameters as necessary provided that you define/use variables rather than SSA values. See the crate documentation of cranelift-frontend.
Yes, I think the confusion here is related to the frontend / SSA builder vs. the core cranelift-codegen API. In the latter, CLIF is very explicit: you define everything, and the block-call args in terminators (jumps) must match blockparams.
Unfortunately in my case, the values are different depending on control flow.
FWIW, in case it helps thinking about this: the entire point of SSA is to surface these join-points and make them explicit. May seem a little annoying at first when you're constructing the IR, but it makes optimization and reasoning about the IR's semantics extremely straightforward vs. the classical "overwrite a variable at any point" IR design
I see, thank you for the explanation. I am using SSA Values (not cranelift-frontend Variables), so I am manually setting the block parameters (with append_block_param), and it seems to be working now. Thanks!
Last updated: Jun 01 2026 at 09:49 UTC