Skip to content

LocalCSE: Handle idempotency precisely - #9409

Merged
kripken merged 16 commits into
WebAssembly:mainfrom
kripken:localcse.idempotent
Nov 21, 2026
Merged

kripken merged 16 commits into
WebAssembly:mainfrom
kripken:localcse.idempotent

Conversation

@kripken

@kripken kripken commented Nov 20, 2026

Copy link
Copy Markdown
Member

Consider

(call $use1
  (call $idempotent
    (local.get $x)
  )
)
(call $use2
  (call $idempotent
    (local.get $x)
  )
)

We execute idempotant, use1, idempotent, use2. It is valid for us to
deduplicate the idempotent calls:

(call $use1
  (local.tee $temp
    (call $idempotent
      (local.get $x)
    )
  )
)
(call $use2
  (local.get $temp)
)

This is because the second idempotent call has no effects, by the
definition of idempotency, even given use1 there. Previously we
did 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.get was effectful)
separately from its "idempotent effects", i.e., the effects of
calls that are idempotent (call $idempotent, above). The
latter only matter for the original appearance, not copies
(due to idempotency: those effects happen on the first call
and never again).

@kripken
kripken requested a review from tlively November 20, 2026 00:10
Comment on lines +713 to +714
;; i32, to make it appear optimizable), we do not treat it as an optimizable
;; idempotent call.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not? What would go wrong if we happened to optimize this?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread src/passes/LocalCSE.cpp
Comment on lines +482 to +484
if (!idempotentCallEffects) {
idempotentCallEffects.emplace(options, *getModule());
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of passing an optional idempotentCallEffects that may need to be initialized, can we just pass a non-optional but empty idempotentCallEffects?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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>.

Comment thread src/passes/LocalCSE.cpp Outdated
// shallow effects into |idempotentCallEffects| and put all other shallow
// effects (operands and enclosing expressions) into |copyEffects|.
void
collectCopyEffects(Expression* expr,

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might as well call this collectEffects since it collects both kinds of effects.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea, done.

;; CHECK-NEXT: (local.get $1)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
(func $idem-effects-interact-2

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Comment thread src/passes/LocalCSE.cpp Resolved
Comment on lines +783 to +785
;; 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).

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread src/passes/LocalCSE.cpp
EffectAnalyzer& copyEffects,
std::optional<EffectAnalyzer>& idempotentCallEffects) {
SmallVector<Expression*, 16> worklist{expr};
while (!worklist.empty()) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@kripken
kripken merged commit e7a4b90 into WebAssembly:main Nov 21, 2026
18 checks passed
@kripken
kripken deleted the localcse.idempotent branch November 21, 2026 00:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants