Skip to content

Commit

Permalink
[WGSL] Add support for constant multiplication
Browse files Browse the repository at this point in the history
https://bugs.webkit.org/show_bug.cgi?id=262561
rdar://116413844

Reviewed by Mike Wyrzykowski.

Implement the constant function for multiplication. The function is mostly complete,
it can handle all overloads for scalars and vectors, but since we don't yet support
constant matrices I couldn't support the overloads that operate on them.

* Source/WebGPU/WGSL/ConstantFunctions.h:
(WGSL::constantMultiply):
* Source/WebGPU/WGSL/TypeCheck.cpp:
(WGSL::TypeChecker::TypeChecker):
* Source/WebGPU/WGSL/tests/valid/constants.wgsl:

Canonical link: https://commits.webkit.org/268834@main
  • Loading branch information
tadeuzagallo committed Oct 4, 2023
1 parent fa9c962 commit d765150
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 0 deletions.
16 changes: 16 additions & 0 deletions Source/WebGPU/WGSL/ConstantFunctions.h
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,22 @@ static ConstantValue constantMinus(const Type*, const FixedVector<ConstantValue>
return binaryMinus();
}

static ConstantValue constantMultiply(const Type*, const FixedVector<ConstantValue>& arguments)
{
ASSERT(arguments.size() == 2);

auto& lhs = arguments[0];
auto& rhs = arguments[1];

// FIXME: handle constant matrices

return scalarOrVector([&](const auto& left, auto& right) -> ConstantValue {
if (left.isInt() && right.isInt())
return left.toInt() * right.toInt();
return left.toDouble() * right.toDouble();
}, lhs, rhs);
}

static ConstantValue zeroValue(const Type* type)
{
return WTF::switchOn(*type,
Expand Down
1 change: 1 addition & 0 deletions Source/WebGPU/WGSL/TypeCheck.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,7 @@ TypeChecker::TypeChecker(ShaderModule& shaderModule)

m_constantFunctions.add("pow"_s, constantPow);
m_constantFunctions.add("-"_s, constantMinus);
m_constantFunctions.add("*"_s, constantMultiply);
m_constantFunctions.add("vec2"_s, constantVector2);
m_constantFunctions.add("vec2f"_s, constantVector2);
m_constantFunctions.add("vec2i"_s, constantVector2);
Expand Down
10 changes: 10 additions & 0 deletions Source/WebGPU/WGSL/tests/valid/constants.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,16 @@ fn testArrayConstants() -> i32
return 0;
}

fn testConstantMultiplication()
{
const x1 = 2 * 2;
const x2 = 2 * 2.0;
const x3 = 2.0 * 2.0;
const x4 = 2.0 * vec2(2.0);
const x5 = vec2(2.0) * vec2(2.0);
const x6 = vec2(2.0) * 2.0;
}

@compute @workgroup_size(1)
fn testVectorConstants() -> i32
{
Expand Down

0 comments on commit d765150

Please sign in to comment.