Skip to content

Running wasm-opt with -Os leaves NOP / dead control flow behind #8077

@mkustermann

Description

@mkustermann

See attached flute.tar.gz

We start with an unoptimized flute.wasm file and run wasm-opt

% wasm-opt -g --enable-gc --enable-reference-types --enable-multivalue --enable-exception-handling --enable-nontrapping-float-to-int --enable-sign-ext --enable-bulk-memory --enable-threads '--no-inline=*<noInline>*' --traps-never-happen \
    -Os \
    flute.wasm \
    -o flute.Os.wasm

This -Os leaves NOP blocks behind:

 (func $_InterfaceType.toString ...
  ...
    block $label0 (result i32)
      local.get $var0
      ref.cast $_InterfaceType
      local.tee $var7
      struct.get $_InterfaceType $classId
      br $label0
    end $label0
  ...
)

It also doesn't prune branches with unreachable (some variants of this):

(func $_FunctionType.toString
  ...
            if (result (ref $JSStringImpl))
              local.get $var6
              unreachable
            else
              local.get $var6
              block $label5 (result (ref $JSStringImpl))
                local.get $var9
                i32.const 0
                global.get $"C8 DiagnosticLevel"
                ref.null none
                local.get $var9
                struct.get $_Type $field0
                i32.const 3066
                i32.add
                call_indirect (param (ref $#Top) i32 (ref $DiagnosticLevel) (ref null $TextTreeConfiguration)) (result (ref $JSStringImpl))
                br $label5
              end $label5
              call $JSStringImpl.+
            end
  ...  
)

If we run another -Os pass then those issues disappear

% wasm-opt -g --enable-gc --enable-reference-types --enable-multivalue --enable-exception-handling --enable-nontrapping-float-to-int --enable-sign-ext --enable-bulk-memory --enable-threads '--no-inline=*<noInline>*' --traps-never-happen \
    -Os \
    flute.Os.wasm \
    -o flute.Os.Os.wasm

From a users point of view, I wouldn't expect an -Os to leave these code patterns behind. We can of course run multiple -Os rounds, but that a) may do more work than needed and b) raises the question of how many -Os passes one may need to run to cleanup things left by previous -Os passes.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions