Skip to content

[RFC] Change builtin implicit eq/ne/truish behaviour for v128 types to more explicit #2152

Open
@MaxGraey

Description

@MaxGraey

I think we shouldn't implicitly lower v128 to a truish via v128.any_true. And use explicit built-in operation like some/every or v128.any_true. For select ternary operations we could use v128.bitselect if cond, ifTrue and ifFalse return same v128 type. Otherwise throw an error. That's how work C++ for example:
https://godbolt.org/z/ca4vvGrqK

Advantages

  • Explicit behaviour without hidden costs;
  • By default ternary will lower to faster bitselect instead any_true + select.

Disadvantages

  • Breaking change;
  • Required any(vX == vY) ? vX : vZ instead vX == vY ? vX : vZ if you need an exact selection (not component-wise).

Current code:

<bool>i64x2(1, 0) == true

const x = i64x2(1, 2);
const y = i64x2(3, 2);
const z = x == y ? x : y;

should be rewritten to:

v128.any_true(i64x2(1, 0)) == true

const x = i64x2(1, 2);
const y = i64x2(3, 2);
const z = v128.any_true(x == y) ? x : y;

or

some(i64x2(1, 0)) == true

const x = i64x2(1, 2);
const y = i64x2(3, 2);
const z = some(x == y) ? x : y;

Bonus:
New some and every builtins could be also useful for tuples and declared as:

declare function some(value: v128 | unknown[]): bool;

instead some and every we could extends true by all and any methods:

if (true.any(x == y)) {...}  // equiv to v128.any_true
if (true.all(x == y)) {...}  // equiv to i64x2.all_true (when we have concrete types like `f64x2` and etc)

Alternative variant

Remove truish lowering but allow implicit logical not operation ! which will use v128.any_true under the hood. In this case we could use double negation !! instead some for same proposes:

!!i64x2(1, 0) == true

const x = i64x2(1, 2);
const y = i64x2(3, 2);
const z = !!(x == y) ? x : y;

so

const z = !!(x == y) ? x : y; // select(x, y, v128.any_true(v128.eq(x, y))
const z = x == y ? x : y;     // v128.bitselect(x, y, v128.eq(x, y))

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