Repository navigation
[StackIR] Support control flow input parameters - #9300
Conversation
Add a params field to StackInst on control-flow begin instructions (BlockBegin, IfBegin, LoopBegin, TryBegin, and TryTableBegin), and update ModuleUtils::collectHeapTypeInfo, BinaryInstWriter, WasmBinaryWriter, PrintSExpression, and StackIROptimizer to register, emit, print, and account for control-flow input parameter signatures. The optimizer does not yet use the input parameters, so add gtests that artificially test printing and binary writing for now. These tests will be replaced with normal lit tests in future commits that start using the block parameters in the optimizer. Using control flow input parameters in Stack IR will ultimately improve code size by up to 1.2% geomean across many interesting Wasm modules, depending on what other optimizations run first.
There was a problem hiding this comment.
Code Review
This pull request introduces support for control-flow block input parameters within StackIR, binary writing, and printing. It updates the type collection and optimization processes to account for these parameters, and adjusts the StackIR optimizer to handle control flow instructions that consume stack values. Additionally, comprehensive unit tests are added to verify these changes. The review feedback suggests a safer way to manage the state of parameter types during expression visitation by saving and restoring the previous parameter state, which prevents potential bugs from nested or recursive visits.
| void visit(Expression* curr, Type params = Type::none) { | ||
| if (func) { | ||
| parent.trackExpressionStart(curr, func); | ||
| } | ||
| currParams = params; | ||
| OverriddenVisitor<BinaryInstWriter>::visit(curr); | ||
| currParams = Type::none; | ||
| if (func) { | ||
| parent.trackExpressionEnd(curr, func); | ||
| } | ||
| } |
There was a problem hiding this comment.
To prevent potential bugs from nested or recursive visit calls, it is safer to save and restore the previous value of currParams using a local variable rather than unconditionally resetting it to Type::none at the end of the method.
| void visit(Expression* curr, Type params = Type::none) { | |
| if (func) { | |
| parent.trackExpressionStart(curr, func); | |
| } | |
| currParams = params; | |
| OverriddenVisitor<BinaryInstWriter>::visit(curr); | |
| currParams = Type::none; | |
| if (func) { | |
| parent.trackExpressionEnd(curr, func); | |
| } | |
| } | |
| void visit(Expression* curr, Type params = Type::none) { | |
| if (func) { | |
| parent.trackExpressionStart(curr, func); | |
| } | |
| auto oldParams = currParams; | |
| currParams = params; | |
| OverriddenVisitor<BinaryInstWriter>::visit(curr); | |
| currParams = oldParams; | |
| if (func) { | |
| parent.trackExpressionEnd(curr, func); | |
| } | |
| } |
|
If you'd prefer, we can drop the if and loop optimizations. |
Do we do anything like that atm in IRBuilder, or is this a new type of optimization there? How complex will it be? I do think we should avoid roundtripping getting worse (which is annoying and also bad for reduction).
This is independent, I think, of input params? Definitely useful! |
This shouldn't be any more complex than many other things already happening in IRBuilder. I imagine it will be similar to what you are currently doing to support pops nested in unnamed blocks. I can implement and land this first to prevent even temporary round-trip regressions.
Yes, this is independent. Using input parameters just means there are a few more locals that can be optimized out this way. |
|
@kripken, all the preliminary IRBuilder changes have landed, so this should be ready for another look. |
|
Did the landings so far prevent round-trip regressions as we hoped? If so, is there a test that verifies that? |
Yes! They work quite well. The tests will be in the next PR, since this one doesn't actually start emitting block parameters yet. |
| // Whether StackIR is available for the function being scanned, in which case | ||
| // control flow signatures have already been collected from StackIR and should | ||
| // not be collected again from the Binaryen IR control flow nodes. | ||
| bool hasStackIR; |
There was a problem hiding this comment.
I am having trouble understanding the model here. StackIR has never been read outside of wasm-stack-opts.cpp. In this new approach, it is..? Is this new design documented somewhere?
There was a problem hiding this comment.
In particular, Stack IR can get out of sync etc., and is thrown out in various circumstances, and it isn't clear to me how all that works in a world where Stack IR is read during the main pipeline.
There was a problem hiding this comment.
Stack IR is still not read during the main pipeline. It is newly passed here, but only from Print.cpp and wasm-binary.cpp, which already had access to Stack IR.
There was a problem hiding this comment.
But this is used in collectHeapTypeInfo which is used from many places during the main pipeline?
There was a problem hiding this comment.
Yes, but all those places pass the default nullptr for the ModuleStackIR* argument.
kripken
left a comment
There was a problem hiding this comment.
Ok, now I am starting to get the general idea here. Nice approach, to use Stack IR in this way! Some more comments as I am still reading.
| // Whether StackIR is available for the function being scanned, in which case | ||
| // control flow signatures have already been collected from StackIR and should | ||
| // not be collected again from the Binaryen IR control flow nodes. |
There was a problem hiding this comment.
This should perhaps replace "whether StackIR is available" with "whether StackIR is available and should be used" because it is possible that during the main pipeline we have StackIR, but are not using it. And add "It is only safe to use StackIR during binary generation, not the main optimization pipeline (as it may get stale)"
There was a problem hiding this comment.
Since #6568, Function and Module do not store StackIR at all; ModuleStackIR is only constructed transiently inside WasmBinaryWriter::prepare() and PrintSExpression::generateStackIR() and destroyed immediately afterward, so there is no persistent StackIR that could go stale. Tweaked this comment slightly to 'Whether StackIR is being used...' and documented the stackIR precondition on collectHeapTypeInfo in src/ir/module-utils.h.
There was a problem hiding this comment.
Oh thanks, I forgot that refactor...
| } | ||
| void visitTryTable(TryTable* curr) { | ||
| info.noteControlFlow(Signature(Type::none, curr->type)); | ||
| if (!hasStackIR) { |
There was a problem hiding this comment.
Can we do this repetitive operation inside noteControlFlow or a trampoline on the path to it? That would avoid duplication and make us safe when we add new control flow structures later.
If that doesn't work, can we assert inside noteControlFlow that !hasStackIR?
There was a problem hiding this comment.
We can't do this check (whether as an assertion or not) inside noteControlFlow because noteControlFlow is used both from the non-StackIR paths here and also from the new StackIR path below. I've factored this into a maybeNoteControlFlow helper, though.
| inst->op == StackInst::IfBegin || | ||
| inst->op == StackInst::LoopBegin || | ||
| inst->op == StackInst::TryBegin || | ||
| inst->op == StackInst::TryTableBegin) { |
There was a problem hiding this comment.
Maybe we can add a helper isControlFlowBegin to avoid hardcoding this list here?
| TypeInclusion inclusion = TypeInclusion::AllTypes, | ||
| VisibilityHandling visibility = VisibilityHandling::NoVisibility); | ||
| VisibilityHandling visibility = VisibilityHandling::NoVisibility, | ||
| const ModuleStackIR* stackIR = nullptr); |
There was a problem hiding this comment.
Please add a comment on the meaning of the new param here, which I think is less obvious than the prior ones. In particular when it is safe to pass it in, is important at the API level.
kripken
left a comment
There was a problem hiding this comment.
Great, thanks! Sorry for my confusion before but I think this PR is now in a really nice and clear place.
Add a params field to StackInst on control-flow begin instructions (BlockBegin, IfBegin, LoopBegin, TryBegin, and TryTableBegin), and update ModuleUtils::collectHeapTypeInfo, BinaryInstWriter, WasmBinaryWriter, PrintSExpression, and StackIROptimizer to register, emit, print, and account for control-flow input parameter signatures.
The optimizer does not yet use the input parameters, so add gtests that artificially test printing and binary writing for now. These tests will be replaced with normal lit tests in future commits that start using the block parameters in the optimizer.
Using control flow input parameters in Stack IR will ultimately improve code size by up to 1.2% geomean across many interesting Wasm modules, depending on what other optimizations run first.