Skip to content

Commit

Permalink
[WGSL] Implement bitwise operator overload and codegen
Browse files Browse the repository at this point in the history
https://bugs.webkit.org/show_bug.cgi?id=255322
rdar://problem/107923629

Reviewed by Tadeu Zagallo.

Implement overload and codegen for operators ~, &, |, and ^.

* Source/WebGPU/WGSL/Metal/MetalFunctionWriter.cpp:
(WGSL::Metal::FunctionDefinitionWriter::visit):
* Source/WebGPU/WGSL/TypeDeclarations.rb:
* Source/WebGPU/WGSL/generator/main.rb:
* Source/WebGPU/WGSL/tests/valid/overload.wgsl:

Canonical link: https://commits.webkit.org/262906@main
  • Loading branch information
djg committed Apr 13, 2023
1 parent eb1586f commit 5429ef9
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 1 deletion.
15 changes: 14 additions & 1 deletion Source/WebGPU/WGSL/Metal/MetalFunctionWriter.cpp
Expand Up @@ -481,12 +481,14 @@ void FunctionDefinitionWriter::visit(AST::CallExpression& call)
void FunctionDefinitionWriter::visit(AST::UnaryExpression& unary)
{
switch (unary.operation()) {
case AST::UnaryOperation::Complement:
m_stringBuilder.append("~");
break;
case AST::UnaryOperation::Negate:
m_stringBuilder.append("-");
break;

case AST::UnaryOperation::AddressOf:
case AST::UnaryOperation::Complement:
case AST::UnaryOperation::Dereference:
case AST::UnaryOperation::Not:
// FIXME: Implement these
Expand All @@ -512,9 +514,20 @@ void FunctionDefinitionWriter::visit(AST::BinaryExpression& binary)

case AST::BinaryOperation::Divide:
case AST::BinaryOperation::Modulo:
// FIXME: Implement these
RELEASE_ASSERT_NOT_REACHED();
break;

case AST::BinaryOperation::And:
m_stringBuilder.append(" & ");
break;
case AST::BinaryOperation::Or:
m_stringBuilder.append(" | ");
break;
case AST::BinaryOperation::Xor:
m_stringBuilder.append(" ^ ");
break;

case AST::BinaryOperation::LeftShift:
case AST::BinaryOperation::RightShift:
// FIXME: Implement these
Expand Down
13 changes: 13 additions & 0 deletions Source/WebGPU/WGSL/TypeDeclarations.rb
Expand Up @@ -44,6 +44,19 @@
}
end

# Bitwise operations
operator :~, {
[T < Integer].(T) => T,
[T < Integer, N].(Vector[T, N]) => Vector[T, N]
}

["|", "&", "^"].each do |op|
operator :"#{op}", {
[T < Integer].(T, T) => T,
[T < Integer, N].(Vector[T, N], Vector[T, N]) => Vector[T, N]
}
end

operator :textureSample, {
[].(Texture[F32, Texture1d], Sampler, F32) => Vector[F32, 4],
[].(Texture[F32, Texture2d], Sampler, Vector[F32, 2]) => Vector[F32, 4],
Expand Down
1 change: 1 addition & 0 deletions Source/WebGPU/WGSL/generator/main.rb
Expand Up @@ -288,6 +288,7 @@ def self.prologue
R = Variable.new(:R, @NumericVariable)
Number = Constraint.new(:Number)
Integer = Constraint.new(:Integer)
Float = Constraint.new(:Float)
Scalar = Constraint.new(:Scalar)
ConcreteInteger = Constraint.new(:ConcreteInteger)
Expand Down
27 changes: 27 additions & 0 deletions Source/WebGPU/WGSL/tests/valid/overload.wgsl
Expand Up @@ -425,3 +425,30 @@ fn testComparison() {
let x9 = vec2(0.0f) <= vec2(1.0f);
}
}

fn testBitwise()
{
{
let x = ~0;
let x1 = ~0i;
let x2 = ~0u;
}

{
let x = 0 & 1;
let x1 = 0i & 1i;
let x2 = 0u & 1u;
}

{
let x = 0 | 1;
let x1 = 0i | 1i;
let x2 = 0u | 1u;
}

{
let x = 0 ^ 1;
let x1 = 0i ^ 1i;
let x2 = 0u ^ 1u;
}
}

0 comments on commit 5429ef9

Please sign in to comment.