Skip to content

Commit

Permalink
[WGSL] Remove ASTTypeName
Browse files Browse the repository at this point in the history
https://bugs.webkit.org/show_bug.cgi?id=261207
rdar://115060930

Reviewed by Mike Wyrzykowski.

Currently, the AST treats type nodes as unrelated from expression nodes.
This doesn't match the reference grammar, and causes issues since expressions
are allowed as arguments to elaborated types.

* Source/WebGPU/WGSL/AST/AST.h:
* Source/WebGPU/WGSL/AST/ASTArrayTypeExpression.h: Copied from Source/WebGPU/WGSL/AST/ASTBitcastExpression.h.
(WGSL::AST::ArrayTypeExpression::maybeElementType const):
(WGSL::AST::ArrayTypeExpression::maybeElementCount const):
(WGSL::AST::ArrayTypeExpression::ArrayTypeExpression):
* Source/WebGPU/WGSL/AST/ASTBitcastExpression.h:
* Source/WebGPU/WGSL/AST/ASTCallExpression.h:
* Source/WebGPU/WGSL/AST/ASTElaboratedExpression.h: Copied from Source/WebGPU/WGSL/AST/ASTBitcastExpression.h.
(WGSL::AST::ElaboratedExpression::base):
(WGSL::AST::ElaboratedExpression::arguments):
(WGSL::AST::ElaboratedExpression::ElaboratedExpression):
* Source/WebGPU/WGSL/AST/ASTExpression.h:
* Source/WebGPU/WGSL/AST/ASTForward.h:
* Source/WebGPU/WGSL/AST/ASTFunction.h:
* Source/WebGPU/WGSL/AST/ASTNode.h:
* Source/WebGPU/WGSL/AST/ASTParameter.h:
* Source/WebGPU/WGSL/AST/ASTReferenceTypeExpression.h: Copied from Source/WebGPU/WGSL/AST/ASTBitcastExpression.h.
* Source/WebGPU/WGSL/AST/ASTStringDumper.cpp:
(WGSL::AST::StringDumper::visit):
* Source/WebGPU/WGSL/AST/ASTStringDumper.h:
* Source/WebGPU/WGSL/AST/ASTStructureMember.h:
* Source/WebGPU/WGSL/AST/ASTTypeName.h: Removed.
* Source/WebGPU/WGSL/AST/ASTVariable.h:
* Source/WebGPU/WGSL/AST/ASTVisitor.cpp:
(WGSL::AST::Visitor::visit):
* Source/WebGPU/WGSL/AST/ASTVisitor.h:
* Source/WebGPU/WGSL/CallGraph.cpp:
(WGSL::CallGraphBuilder::visit):
* Source/WebGPU/WGSL/ConstantRewriter.cpp:
(WGSL::ConstantRewriter::visit):
* Source/WebGPU/WGSL/EntryPointRewriter.cpp:
(WGSL::EntryPointRewriter::rewrite):
(WGSL::EntryPointRewriter::checkReturnType):
(WGSL::EntryPointRewriter::visit):
* Source/WebGPU/WGSL/GlobalSorting.cpp:
* Source/WebGPU/WGSL/GlobalVariableRewriter.cpp:
(WGSL::RewriteGlobalVariables::pack):
(WGSL::RewriteGlobalVariables::packResource):
(WGSL::RewriteGlobalVariables::packStructResource):
(WGSL::RewriteGlobalVariables::packArrayResource):
(WGSL::RewriteGlobalVariables::updateReference):
(WGSL::bindingMemberForGlobal):
(WGSL::RewriteGlobalVariables::insertParameters):
* Source/WebGPU/WGSL/MangleNames.cpp:
* Source/WebGPU/WGSL/Metal/MetalFunctionWriter.cpp:
(WGSL::Metal::FunctionDefinitionWriter::visit):
(WGSL::Metal::FunctionDefinitionWriter::visitArgumentBufferParameter):
* Source/WebGPU/WGSL/Parser.cpp:
(WGSL::Parser<Lexer>::parseTypeName):
(WGSL::Parser<Lexer>::parseTypeNameAfterIdentifier):
(WGSL::Parser<Lexer>::parseArrayType):
(WGSL::Parser<Lexer>::parseVariableWithAttributes):
(WGSL::Parser<Lexer>::parseFunction):
* Source/WebGPU/WGSL/ParserPrivate.h:
* Source/WebGPU/WGSL/TypeCheck.cpp:
(WGSL::TypeChecker::visitVariable):
(WGSL::TypeChecker::visit):
(WGSL::TypeChecker::resolve):
(WGSL::TypeChecker::allocateSimpleConstructor):
* Source/WebGPU/WGSL/TypeStore.cpp:
(WGSL::TypeStore::typeConstructorType):
* Source/WebGPU/WGSL/TypeStore.h:
* Source/WebGPU/WGSL/Types.h:
* Source/WebGPU/WebGPU.xcodeproj/project.pbxproj:

Canonical link: https://commits.webkit.org/267724@main
  • Loading branch information
tadeuzagallo committed Sep 7, 2023
1 parent 939ec68 commit f5b0fa4
Show file tree
Hide file tree
Showing 33 changed files with 364 additions and 424 deletions.
4 changes: 3 additions & 1 deletion Source/WebGPU/WGSL/AST/AST.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#include "ASTAbstractFloatLiteral.h"
#include "ASTAbstractIntegerLiteral.h"
#include "ASTAlignAttribute.h"
#include "ASTArrayTypeExpression.h"
#include "ASTAssignmentStatement.h"
#include "ASTAttribute.h"
#include "ASTBinaryExpression.h"
Expand All @@ -44,6 +45,7 @@
#include "ASTDecrementIncrementStatement.h"
#include "ASTDirective.h"
#include "ASTDiscardStatement.h"
#include "ASTElaboratedTypeExpression.h"
#include "ASTExpression.h"
#include "ASTFieldAccessExpression.h"
#include "ASTFloat32Literal.h"
Expand All @@ -64,6 +66,7 @@
#include "ASTParameter.h"
#include "ASTPhonyStatement.h"
#include "ASTPointerDereference.h"
#include "ASTReferenceTypeExpression.h"
#include "ASTReturnStatement.h"
#include "ASTSigned32Literal.h"
#include "ASTSizeAttribute.h"
Expand All @@ -73,7 +76,6 @@
#include "ASTStructure.h"
#include "ASTStructureMember.h"
#include "ASTSwitchStatement.h"
#include "ASTTypeName.h"
#include "ASTUnaryExpression.h"
#include "ASTUnsigned32Literal.h"
#include "ASTVariable.h"
Expand Down
54 changes: 54 additions & 0 deletions Source/WebGPU/WGSL/AST/ASTArrayTypeExpression.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* Copyright (C) 2023 Apple Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
* THE POSSIBILITY OF SUCH DAMAGE.
*/

#pragma once

#include "ASTExpression.h"

namespace WGSL::AST {

class ArrayTypeExpression : public Expression {
WGSL_AST_BUILDER_NODE(ArrayTypeExpression);

public:
NodeKind kind() const override;

Expression* maybeElementType() const { return m_elementType; }
Expression* maybeElementCount() const { return m_elementCount; }

private:
ArrayTypeExpression(SourceSpan span, Expression::Ptr elementType, Expression::Ptr elementCount)
: Expression(span)
, m_elementType(elementType)
, m_elementCount(elementCount)
{ }

Expression::Ptr m_elementType;
Expression::Ptr m_elementCount;
};

} // namespace WGSL::AST

SPECIALIZE_TYPE_TRAITS_WGSL_AST(ArrayTypeExpression)
5 changes: 2 additions & 3 deletions Source/WebGPU/WGSL/AST/ASTBitcastExpression.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
#pragma once

#include "ASTExpression.h"
#include "ASTTypeName.h"

namespace WGSL::AST {

Expand All @@ -37,13 +36,13 @@ class BitcastExpression final : public Expression {
Expression& expression() { return m_expression.get(); }

private:
BitcastExpression(SourceSpan span, TypeName::Ref&& castToType, Expression::Ref&& expression)
BitcastExpression(SourceSpan span, Expression::Ref&& castToType, Expression::Ref&& expression)
: Expression(span)
, m_castToType(WTFMove(castToType))
, m_expression(WTFMove(expression))
{ }

TypeName::Ref m_castToType;
Expression::Ref m_castToType;
Expression::Ref m_expression;
};

Expand Down
7 changes: 3 additions & 4 deletions Source/WebGPU/WGSL/AST/ASTCallExpression.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
#pragma once

#include "ASTExpression.h"
#include "ASTTypeName.h"

namespace WGSL::AST {

Expand All @@ -38,11 +37,11 @@ class CallExpression final : public Expression {
WGSL_AST_BUILDER_NODE(CallExpression);
public:
NodeKind kind() const override;
TypeName& target() { return m_target.get(); }
Expression& target() { return m_target.get(); }
Expression::List& arguments() { return m_arguments; }

private:
CallExpression(SourceSpan span, TypeName::Ref&& target, Expression::List&& arguments)
CallExpression(SourceSpan span, Expression::Ref&& target, Expression::List&& arguments)
: Expression(span)
, m_target(WTFMove(target))
, m_arguments(WTFMove(arguments))
Expand All @@ -52,7 +51,7 @@ class CallExpression final : public Expression {
// * Type that does not accept parameters (bool, i32, u32, ...)
// * Identifier that refers to a type alias.
// * Identifier that refers to a function.
TypeName::Ref m_target;
Expression::Ref m_target;
Expression::List m_arguments;
};

Expand Down
53 changes: 53 additions & 0 deletions Source/WebGPU/WGSL/AST/ASTElaboratedTypeExpression.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* Copyright (C) 2023 Apple Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
* THE POSSIBILITY OF SUCH DAMAGE.
*/

#pragma once

#include "ASTExpression.h"

namespace WGSL::AST {

class ElaboratedTypeExpression : public Expression {
WGSL_AST_BUILDER_NODE(ElaboratedTypeExpression);

public:
NodeKind kind() const override;
Identifier& base() { return m_base; }
Expression::List& arguments() { return m_arguments; }

private:
ElaboratedTypeExpression(SourceSpan span, Identifier&& base, Expression::List&& arguments)
: Expression(span)
, m_base(WTFMove(base))
, m_arguments(WTFMove(arguments))
{ }

Identifier m_base;
Expression::List m_arguments;
};

} // namespace WGSL::AST

SPECIALIZE_TYPE_TRAITS_WGSL_AST(ElaboratedTypeExpression)
2 changes: 2 additions & 0 deletions Source/WebGPU/WGSL/AST/ASTExpression.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@

namespace WGSL {
class ConstantRewriter;
class EntryPointRewriter;
class RewriteGlobalVariables;
class TypeChecker;
struct Type;
Expand All @@ -40,6 +41,7 @@ namespace AST {
class Expression : public Node {
WGSL_AST_BUILDER_NODE(Expression);
friend ConstantRewriter;
friend EntryPointRewriter;
friend RewriteGlobalVariables;
friend TypeChecker;

Expand Down
8 changes: 3 additions & 5 deletions Source/WebGPU/WGSL/AST/ASTForward.h
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,9 @@ class StructureMember;

class TypeAlias;

class TypeName;
class ArrayTypeName;
class NamedTypeName;
class ParameterizedTypeName;
class ReferenceTypeName;
class ArrayTypeExpression;
class ElaboratedTypeExpression;
class ReferenceTypeExpression;

class Variable;
class VariableQualifier;
Expand Down
9 changes: 4 additions & 5 deletions Source/WebGPU/WGSL/AST/ASTFunction.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
#include "ASTCompoundStatement.h"
#include "ASTDeclaration.h"
#include "ASTParameter.h"
#include "ASTTypeName.h"

#include <wtf/UniqueRefVector.h>

Expand All @@ -46,17 +45,17 @@ class Function final : public Declaration {
Parameter::List& parameters() { return m_parameters; }
Attribute::List& attributes() { return m_attributes; }
Attribute::List& returnAttributes() { return m_returnAttributes; }
TypeName* maybeReturnType() { return m_returnType; }
Expression* maybeReturnType() { return m_returnType; }
CompoundStatement& body() { return m_body.get(); }
const Identifier& name() const { return m_name; }
const Parameter::List& parameters() const { return m_parameters; }
const Attribute::List& attributes() const { return m_attributes; }
const Attribute::List& returnAttributes() const { return m_returnAttributes; }
const TypeName* maybeReturnType() const { return m_returnType; }
const Expression* maybeReturnType() const { return m_returnType; }
const CompoundStatement& body() const { return m_body.get(); }

private:
Function(SourceSpan span, Identifier&& name, Parameter::List&& parameters, TypeName::Ptr returnType, CompoundStatement::Ref&& body, Attribute::List&& attributes, Attribute::List&& returnAttributes)
Function(SourceSpan span, Identifier&& name, Parameter::List&& parameters, Expression::Ptr returnType, CompoundStatement::Ref&& body, Attribute::List&& attributes, Attribute::List&& returnAttributes)
: Declaration(span)
, m_name(WTFMove(name))
, m_parameters(WTFMove(parameters))
Expand All @@ -70,7 +69,7 @@ class Function final : public Declaration {
Parameter::List m_parameters;
Attribute::List m_attributes;
Attribute::List m_returnAttributes;
TypeName::Ptr m_returnType;
Expression::Ptr m_returnType;
CompoundStatement::Ref m_body;
};

Expand Down
8 changes: 3 additions & 5 deletions Source/WebGPU/WGSL/AST/ASTNode.h
Original file line number Diff line number Diff line change
Expand Up @@ -100,11 +100,9 @@ enum class NodeKind : uint8_t {

TypeAlias,

// TypeName
ArrayTypeName,
NamedTypeName,
ParameterizedTypeName,
ReferenceTypeName,
ArrayTypeExpression,
ElaboratedTypeExpression,
ReferenceTypeExpression,

Variable,

Expand Down
8 changes: 4 additions & 4 deletions Source/WebGPU/WGSL/AST/ASTParameter.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@

#include "ASTAttribute.h"
#include "ASTBuilder.h"
#include "ASTExpression.h"
#include "ASTIdentifier.h"
#include "ASTTypeName.h"
#include <wtf/ReferenceWrapperVector.h>

namespace WGSL::AST {
Expand All @@ -46,12 +46,12 @@ class Parameter final : public Node {

NodeKind kind() const override;
Identifier& name() { return m_name; }
TypeName& typeName() { return m_typeName.get(); }
Expression& typeName() { return m_typeName.get(); }
Attribute::List& attributes() { return m_attributes; }
ParameterRole role() { return m_role; }

private:
Parameter(SourceSpan span, Identifier&& name, TypeName::Ref&& typeName, Attribute::List&& attributes, ParameterRole role)
Parameter(SourceSpan span, Identifier&& name, Expression::Ref&& typeName, Attribute::List&& attributes, ParameterRole role)
: Node(span)
, m_role(role)
, m_name(WTFMove(name))
Expand All @@ -61,7 +61,7 @@ class Parameter final : public Node {

ParameterRole m_role;
Identifier m_name;
TypeName::Ref m_typeName;
Expression::Ref m_typeName;
Attribute::List m_attributes;
};

Expand Down
50 changes: 50 additions & 0 deletions Source/WebGPU/WGSL/AST/ASTReferenceTypeExpression.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* Copyright (C) 2023 Apple Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
* THE POSSIBILITY OF SUCH DAMAGE.
*/

#pragma once

#include "ASTExpression.h"

namespace WGSL::AST {

class ReferenceTypeExpression final : public Expression {
WGSL_AST_BUILDER_NODE(ReferenceTypeExpression);

public:
NodeKind kind() const override;
Expression& type() const { return m_type.get(); }

private:
ReferenceTypeExpression(SourceSpan span, Expression::Ref&& type)
: Expression(span)
, m_type(WTFMove(type))
{ }

Expression::Ref m_type;
};

} // namespace WGSL::AST

SPECIALIZE_TYPE_TRAITS_WGSL_AST(ReferenceTypeExpression)
11 changes: 3 additions & 8 deletions Source/WebGPU/WGSL/AST/ASTStringDumper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -396,7 +396,7 @@ void StringDumper::visit(ForStatement& statement)
}

// Types
void StringDumper::visit(ArrayTypeName& type)
void StringDumper::visit(ArrayTypeExpression& type)
{
m_out.print("array");
if (type.maybeElementType()) {
Expand All @@ -410,12 +410,7 @@ void StringDumper::visit(ArrayTypeName& type)
}
}

void StringDumper::visit(NamedTypeName& type)
{
m_out.print(type.name());
}

void StringDumper::visit(ParameterizedTypeName& type)
void StringDumper::visit(ElaboratedTypeExpression& type)
{
m_out.print(type.base(), "<");
bool first = true;
Expand All @@ -428,7 +423,7 @@ void StringDumper::visit(ParameterizedTypeName& type)
m_out.print(">");
}

void StringDumper::visit(ReferenceTypeName& type)
void StringDumper::visit(ReferenceTypeExpression& type)
{
visit(type.type());
m_out.print("&");
Expand Down
Loading

0 comments on commit f5b0fa4

Please sign in to comment.