Stream: git-wasmtime

Topic: wasmtime / issue #10994 Cranelift: missed DCE / constant ...


view this post on Zulip Wasmtime GitHub notifications bot (Jun 10 2025 at 08:57):

Simoffsky opened issue #10994:

Cranelift fails to eliminate a dead conditional branch in a case where the condition is statically known to be false due to constant folding.

godbolt link (look at $process_rows).

Input WASM getted by using C++ Wasmtime API

(module
  (func $process_rows (param i32 i32) (result i32)
    block
      i32.const 0
      i32.eqz
      br_if 0
      local.get 0
      i32.const 1
      i32.shl
      local.get 0
      i32.const 1
      i32.add
      local.get 0
      i32.const 2
      i32.gt_s
      select
      local.get 0
      i32.add
      local.set 0
    end
    local.get 0
    i32.const 1
    i32.add))

Expected behavior

Since i32.const 0 is always equal to zero, the result of i32.eqz is always 1, and br_if 0 is guaranteed to be taken unconditionally. Therefore, all code after the branch inside the block is dead and should be removed.

xor esi, esi
test    esi, esi
je  <after_block>       ; this is always false, should not exist
mov rax, rdx            ; dead code
shl eax, 0x1
lea edi, [rdx + 0x1]
cmp edx, 0x2
cmovg   edi, eax
add edx, edi
lea eax, [rdx + 0x1]

Versions and Environment

Cranelift version or commit: Wasmtime 22.0.0

Operating system: macos, linux (debian)

Architecture: (arm, x64)

view this post on Zulip Wasmtime GitHub notifications bot (Jun 10 2025 at 08:57):

Simoffsky added the bug label to Issue #10994.

view this post on Zulip Wasmtime GitHub notifications bot (Jun 10 2025 at 08:57):

Simoffsky added the cranelift label to Issue #10994.

view this post on Zulip Wasmtime GitHub notifications bot (Jun 10 2025 at 09:14):

Simoffsky edited issue #10994:

Cranelift fails to eliminate a dead conditional branch in a case where the condition is statically known to be false due to constant folding.

godbolt link (look at $process_rows).

Input WASM

(module
  (func $process_rows (param i32 i32) (result i32)
    block
      i32.const 0
      i32.eqz
      br_if 0
      local.get 0
      i32.const 1
      i32.shl
      local.get 0
      i32.const 1
      i32.add
      local.get 0
      i32.const 2
      i32.gt_s
      select
      local.get 0
      i32.add
      local.set 0
    end
    local.get 0
    i32.const 1
    i32.add))

Expected behavior

Since i32.const 0 is always equal to zero, the result of i32.eqz is always 1, and br_if 0 is guaranteed to be taken unconditionally. Therefore, all code after the branch inside the block is dead and should be removed.

xor esi, esi
test    esi, esi
je  <after_block>       ; this is always false, should not exist
mov rax, rdx            ; dead code
shl eax, 0x1
lea edi, [rdx + 0x1]
cmp edx, 0x2
cmovg   edi, eax
add edx, edi
lea eax, [rdx + 0x1]

Versions and Environment

Cranelift version or commit: Wasmtime 22.0.0

Operating system: macos, linux (debian)

Architecture: (arm, x64)

view this post on Zulip Wasmtime GitHub notifications bot (Jun 10 2025 at 09:34):

cfallin closed issue #10994:

Cranelift fails to eliminate a dead conditional branch in a case where the condition is statically known to be false due to constant folding.

godbolt link (look at $process_rows).

Input WASM

(module
  (func $process_rows (param i32 i32) (result i32)
    block
      i32.const 0
      i32.eqz
      br_if 0
      local.get 0
      i32.const 1
      i32.shl
      local.get 0
      i32.const 1
      i32.add
      local.get 0
      i32.const 2
      i32.gt_s
      select
      local.get 0
      i32.add
      local.set 0
    end
    local.get 0
    i32.const 1
    i32.add))

Expected behavior

Since i32.const 0 is always equal to zero, the result of i32.eqz is always 1, and br_if 0 is guaranteed to be taken unconditionally. Therefore, all code after the branch inside the block is dead and should be removed.

xor esi, esi
test    esi, esi
je  <after_block>       ; this is always false, should not exist
mov rax, rdx            ; dead code
shl eax, 0x1
lea edi, [rdx + 0x1]
cmp edx, 0x2
cmovg   edi, eax
add edx, edi
lea eax, [rdx + 0x1]

Versions and Environment

Cranelift version or commit: Wasmtime 22.0.0

Operating system: macos, linux (debian)

Architecture: (arm, x64)

view this post on Zulip Wasmtime GitHub notifications bot (Jun 10 2025 at 09:34):

cfallin commented on issue #10994:

We don't have any branch-folding optimizations at the moment, so this result is expected. #6106 covers tracking this possible enhancement, I believe, so I'll close as a duplicate of that tracking issue.

view this post on Zulip Wasmtime GitHub notifications bot (Jun 10 2025 at 09:35):

cfallin closed issue #10994:

Cranelift fails to eliminate a dead conditional branch in a case where the condition is statically known to be false due to constant folding.

godbolt link (look at $process_rows).

Input WASM

(module
  (func $process_rows (param i32 i32) (result i32)
    block
      i32.const 0
      i32.eqz
      br_if 0
      local.get 0
      i32.const 1
      i32.shl
      local.get 0
      i32.const 1
      i32.add
      local.get 0
      i32.const 2
      i32.gt_s
      select
      local.get 0
      i32.add
      local.set 0
    end
    local.get 0
    i32.const 1
    i32.add))

Expected behavior

Since i32.const 0 is always equal to zero, the result of i32.eqz is always 1, and br_if 0 is guaranteed to be taken unconditionally. Therefore, all code after the branch inside the block is dead and should be removed.

xor esi, esi
test    esi, esi
je  <after_block>       ; this is always false, should not exist
mov rax, rdx            ; dead code
shl eax, 0x1
lea edi, [rdx + 0x1]
cmp edx, 0x2
cmovg   edi, eax
add edx, edi
lea eax, [rdx + 0x1]

Versions and Environment

Cranelift version or commit: Wasmtime 22.0.0

Operating system: macos, linux (debian)

Architecture: (arm, x64)


Last updated: Dec 06 2025 at 06:05 UTC