Skip to content

Commit

Permalink
[WGSL] no matching overload for operator >>(i32, u32)
Browse files Browse the repository at this point in the history
https://bugs.webkit.org/show_bug.cgi?id=263947
rdar://117720916

Reviewed by Mike Wyrzykowski.

The type declaration for `<<` and `>>` was wrong: it specified that both lhs and
rhs, should have the same type, either i32, u32 or a vector of either. However,
the spec[1] specifies that rhs must always be u32, and only lhs can be i32 (or u32).
This also required changed the constant function, as the BINARY_OPERATOR macro assumes
that both arguments will have the same type.

* Source/WebGPU/WGSL/ConstantFunctions.h:
(WGSL::constantBitwiseShiftLeft):
(WGSL::constantBitwiseShiftRight):
* Source/WebGPU/WGSL/TypeDeclarations.rb:
* Source/WebGPU/WGSL/tests/valid/overload.wgsl:

Canonical link: https://commits.webkit.org/270060@main
  • Loading branch information
tadeuzagallo committed Nov 1, 2023
1 parent 219eb0b commit 0d56517
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 6 deletions.
40 changes: 38 additions & 2 deletions Source/WebGPU/WGSL/ConstantFunctions.h
Original file line number Diff line number Diff line change
Expand Up @@ -547,8 +547,44 @@ static ConstantValue constantBitwiseAnd(const Type*, const FixedVector<ConstantV

UNARY_OPERATION(BitwiseNot, Integer, [&](auto arg) { return ~arg; })
BINARY_OPERATION(BitwiseXor, Integer, [&](auto left, auto right) { return left ^ right; })
BINARY_OPERATION(BitwiseShiftLeft, Integer, [&](auto left, auto right) { return left << right; })
BINARY_OPERATION(BitwiseShiftRight, Integer, [&](auto left, auto right) { return left >> right; })

static ConstantValue constantBitwiseShiftLeft(const Type*, const FixedVector<ConstantValue>& arguments)
{
// We can't use a BINARY_OPERATION here since the arguments might not all have the same type
// i.e. we accept (u32, u32) as well as (i32, u32)
ASSERT(arguments.size() == 2);
const auto& shift = [&]<typename T>(T left, uint32_t right) {
return left << right;
};

return scalarOrVector([&](const auto& left, const auto& rightValue) -> ConstantValue {
auto right = std::get<uint32_t>(rightValue);
if (auto* i32 = std::get_if<int32_t>(&left))
return shift(*i32, right);
if (auto* u32 = std::get_if<uint32_t>(&left))
return shift(*u32, right);
RELEASE_ASSERT_NOT_REACHED();
}, arguments[0], arguments[1]);
}

static ConstantValue constantBitwiseShiftRight(const Type*, const FixedVector<ConstantValue>& arguments)
{
// We can't use a BINARY_OPERATION here since the arguments might not all have the same type
// i.e. we accept (u32, u32) as well as (i32, u32)
ASSERT(arguments.size() == 2);
const auto& shift = [&]<typename T>(T left, uint32_t right) {
return left >> right;
};

return scalarOrVector([&](const auto& left, const auto& rightValue) -> ConstantValue {
auto right = std::get<uint32_t>(rightValue);
if (auto* i32 = std::get_if<int32_t>(&left))
return shift(*i32, right);
if (auto* u32 = std::get_if<uint32_t>(&left))
return shift(*u32, right);
RELEASE_ASSERT_NOT_REACHED();
}, arguments[0], arguments[1]);
}


// Constructors
Expand Down
15 changes: 13 additions & 2 deletions Source/WebGPU/WGSL/TypeDeclarations.rb
Original file line number Diff line number Diff line change
Expand Up @@ -187,8 +187,6 @@
"|" => 'constantBitwiseOr',
"&" => 'constantBitwiseAnd',
"^" => 'constantBitwiseXor',
"<<" => 'constantBitwiseShiftLeft',
">>" => 'constantBitwiseShiftRight',
}.each do |op, const_function|
operator :"#{op}", {
must_use: true,
Expand All @@ -199,6 +197,19 @@
}
end

{
"<<" => 'constantBitwiseShiftLeft',
">>" => 'constantBitwiseShiftRight',
}.each do |op, const_function|
operator :"#{op}", {
must_use: true,
const: const_function,

[S < ConcreteInteger].(S, u32) => S,
[S < ConcreteInteger, N].(vec[N][S], vec[N][u32]) => vec[N][S],
}
end

# 16.1. Constructor Built-in Functions

# 16.1.1. Zero Value Built-in Functions
Expand Down
10 changes: 8 additions & 2 deletions Source/WebGPU/WGSL/tests/valid/overload.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -317,14 +317,20 @@ fn testBitwise()

{
_ = 1 << 2;
_ = 1i << 2i;
_ = 1i << 2u;
_ = 1u << 2u;
_ = vec2(1) << vec2(2);
_ = vec2(1i) << vec2(2u);
_ = vec2(1u) << vec2(2u);
}

{
_ = 1 >> 2;
_ = 1i >> 2i;
_ = 1i >> 2u;
_ = 1u >> 2u;
_ = vec2(1) >> vec2(2);
_ = vec2(1i) >> vec2(2u);
_ = vec2(1u) >> vec2(2u);
}
}

Expand Down

0 comments on commit 0d56517

Please sign in to comment.