Repository navigation
LocalCSE: Handle idempotency precisely - #9409
Conversation
| ;; i32, to make it appear optimizable), we do not treat it as an optimizable | ||
| ;; idempotent call. |
There was a problem hiding this comment.
Why not? What would go wrong if we happened to optimize this?
There was a problem hiding this comment.
The issue is that the return effect would be removed from the copy. It is true that the original would already return in this example, though, but not generally. I added a comment.
| if (!idempotentCallEffects) { | ||
| idempotentCallEffects.emplace(options, *getModule()); | ||
| } |
There was a problem hiding this comment.
Instead of passing an optional idempotentCallEffects that may need to be initialized, can we just pass a non-optional but empty idempotentCallEffects?
There was a problem hiding this comment.
We can, though an EffectAnalyzer is a large data structure and this is rarely needed, and it just takes a trivial if to avoid creating it here?
There was a problem hiding this comment.
Sad that C++ doesn't have a good pattern for "initialize this optional if necessary" as part of a one-liner :( Rust gets this right with the get_or_insert family of methods on std::Option<T>.
| // shallow effects into |idempotentCallEffects| and put all other shallow | ||
| // effects (operands and enclosing expressions) into |copyEffects|. | ||
| void | ||
| collectCopyEffects(Expression* expr, |
There was a problem hiding this comment.
Might as well call this collectEffects since it collects both kinds of effects.
| ;; CHECK-NEXT: (local.get $1) | ||
| ;; CHECK-NEXT: ) | ||
| ;; CHECK-NEXT: ) | ||
| (func $idem-effects-interact-2 |
There was a problem hiding this comment.
Gemini identified that a similar test case still shows room for improvement:
(module
(import "a" "b" (func $import))
(global $mutable (mut i32) (i32.const 42))
(global $immutable i32 (i32.const 1337))
(@binaryen.idempotent)
(func $idempotent (param i32) (result i32)
(call $import)
(i32.const 42)
)
(func $idem-effects-interact-enclosing
;; Just like $idem-effects-interact-2, except the idempotent call is nested
;; inside an i32.add. Both repeated i32.add expressions can be optimized.
(drop
(i32.add
(call $idempotent
(global.get $immutable)
)
(i32.const 1)
)
)
(drop
(i32.add
(global.get $mutable)
(global.get $mutable)
)
)
(drop
(i32.add
(call $idempotent
(global.get $immutable)
)
(i32.const 1)
)
)
(drop
(i32.add
(global.get $mutable)
(global.get $mutable)
)
)
)
)We currently fail to optimize the second (i32.add (global.get $mutable) (global.get $mutable)) here.
There was a problem hiding this comment.
Good catch. I don't see a simple way to handle it, and a second --local-cse does, so maybe it isn't a high priority to fix. But I added it with a TODO
Co-authored-by: Thomas Lively <tlively123@gmail.com>
| ;; return; in this testcase that does not matter, as the original returns | ||
| ;; anyhow, but if the return_call were nested in an if arm, say, then this | ||
| ;; would be a possible change in behavior). |
There was a problem hiding this comment.
Is this true? If the first return_call doesn't dominate the second return_call, then local CSE wouldn't apply in the first place AFAICT.
There was a problem hiding this comment.
Oh, yes, because we only work locally inside a basic block, then this is safe. If we could optimize an If, that would be different, but we don't. Updated.
| EffectAnalyzer& copyEffects, | ||
| std::optional<EffectAnalyzer>& idempotentCallEffects) { | ||
| SmallVector<Expression*, 16> worklist{expr}; | ||
| while (!worklist.empty()) { |
There was a problem hiding this comment.
It would be really nice if we had an iterator over expression trees so we could avoid implementing a tree search or defining a visitor every time we had to do this. Such an iterator would be much nicer if defined in terms of C++ coroutines. I bumped the discussion on #8657 to see where we are with that.
Consider
We execute
idempotant, use1, idempotent, use2. It is valid for us todeduplicate the idempotent calls:
This is because the second idempotent call has no effects, by the
definition of idempotency, even given
use1there. Previously wedid not precisely account for the effects here, which prevented this
from getting optimized.
To precisely account for the effects, we calculate an original
expression's "copy effects" - those that will happen in other
appearances of it (imagine that the
local.getwas effectful)separately from its "idempotent effects", i.e., the effects of
calls that are idempotent (
call $idempotent, above). Thelatter only matter for the original appearance, not copies
(due to idempotency: those effects happen on the first call
and never again).