Skip to content

wasm: i8x16.swizzle with a constant value should be optimized to i8x16.shuffle #169058

@valadaptive

Description

@valadaptive

A SIMD variable permute (like i8x16.swizzle) should be optimizable to a constant permute (like i8x16.shuffle) if the input is constant. This type of optimization is already performed for x86 vector operations, but not for WebAssembly vector operations.

This optimization is particularly useful for architecture-generic SIMD, where exposing constant permutes might not be possible depending on the type system.

Take a look at this example code (also on https://godbolt.org/z/6esax48Kr):

#include <wasm_simd128.h>

v128_t reverse(v128_t n) {
    v128_t indices = wasm_i8x16_make(
        15,
        14,
        13,
        12,
        11,
        10,
        9,
        8,
        7,
        6,
        5,
        4,
        3,
        2,
        1,
        0
    );
    return wasm_i8x16_swizzle(n, indices);
}

I would expect this code to be optimized to:

reverse:
        local.get       0
        local.get       0
        i8x16.shuffle   15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0
        end_function

But instead, we get:

reverse:
        local.get       0
        v128.const      15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0
        i8x16.swizzle
        end_function

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions