Skip to content

Generalize "trivial call" flag to other simple instructions #7670

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

Draft
wants to merge 18 commits into
base: main
Choose a base branch
from

Conversation

osa1
Copy link
Contributor

@osa1 osa1 commented Jun 19, 2025

Note: this is based on #7669. I can't create a PR to that branch as the branch lives in my my fork.

I can merge this to #7669 if that's more helpful than a separate PR?


A "trivial call" function is a function that just calls another with local.gets as arguments.

These functions can sometimes be inlined when optimizing for size.

With this PR we generate "trivial call" flag to "trivial instruction", to cover binary instructions like i32.add and unary instructions like i32.eqz.

With this PR wasm-opt now inlines trivial functions in dart2wasm outputs like:

(func $IntWasmInstructions|ltU (;40;) (param $var0 i64) (param $var1 i64) (result i32)
  local.get $var0
  local.get $var1
  i64.lt_u
)

(func $IntWasmInstructions|leU (;75;) (param $var0 i64) (param $var1 i64) (result i32)
  local.get $var0
  local.get $var1
  i64.le_u
)

(func $IntWasmInstructions|shrS (;77;) (param $var0 i64) (param $var1 i64) (result i64)
  local.get $var0
  local.get $var1
  i64.shr_s
)

Note: dart2wasm doesn't directly generate these functions, these become "trivial" after wasm-opt passes.

osa1 added 13 commits June 19, 2025 12:52
A "trivial call" is a function that just calls another function.

    (func $foo ...
      (call $target ...))

Currently we inline these functions always only when not optimizing for
code size.

When optimizing for code size, these functions can always be inlined
when

1. The arguments to `$target` are all function argument locals.
2. The locals are not used more than once
3. The locals are used in the same order they appear in the function
   arguments.

When these hold, inlining `$foo` never increases code size as it doesn't
cause introducing more locals at call sites.

Improve `FunctionInfo` type and `FunctionInfoScanner` to annotate
functions with "trivial call" information that also contains whether
inlining shrinks code size.

If a function shrinks when inlined always inline it even with `-Os`.

Otherwise inline it as before, i.e. when not optimizing for code size.
@kripken
Copy link
Member

kripken commented Jun 20, 2025

Note: this is based on #7669. I can't create a PR to that branch as the branch lives in my my fork.

An option is to create a PR in your fork, and ping people there. That's what I usually do. (Then, after the parent branch lands in upstream, you need to create a new PR there, so there is a downside...)

Though, perhaps it makes sense to focus on the first PR for now?

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