⚠ Archived content — this site is no longer maintained.   Current WebKit documentation is at docs.webkit.org.

Changeset 245945 in webkit


Ignore:
Timestamp:
May 30, 2019, 9:31:54 PM (7 years ago)
Author:
sbarati@apple.com
Message:

[WHLSL] Enforce variable lifetimes
https://bugs.webkit.org/show_bug.cgi?id=195794
<rdar://problem/50746293>

Reviewed by Myles C. Maxfield.

Source/WebCore:

In WHLSL, each variable has global lifetime. So returning a pointer to a
local variable is a legitimate and well specified thing to do. Each local
variable has a unique place in memory. So, for example:

`
thread int* ptr() { int local; return &local; }
thread int* ptrPtr() { return ptr(); }
`

In the above program, ptr() must always return the same value
as ptrPtr(). So, the following would print "42":
`
thread int* p = ptrPtr();
*ptr() = 42;
print(*p);
`

To implement these semantics, this patch introduces a new pass which does the
following transformations:

  • It notes every variable whose address is taken in the program.
  • Each such variable gets defined as a field in a struct.
  • Each function which is an entry point defines this struct.
  • Each non entry point takes a pointer to this struct as its final parameter.
  • Each call to a non-native function is rewritten to pass a pointer to the struct as the last call argument.
  • Each variable reference to "x", where "x" ends up in the struct, is modified to instead be "struct->x". We store to "struct->x" after declaring "x". If "x" is a function parameter, we store to "struct->x" as the first thing we do in the function body.

Tests: webgpu/whlsl-ensure-proper-variable-lifetime-2.html

webgpu/whlsl-ensure-proper-variable-lifetime-3.html
webgpu/whlsl-ensure-proper-variable-lifetime.html
webgpu/whlsl-return-local-variable.html

  • Modules/webgpu/WHLSL/AST/WHLSLAST.h:
  • Modules/webgpu/WHLSL/AST/WHLSLExpression.h:

(WebCore::WHLSL::AST::Expression::Expression):
(WebCore::WHLSL::AST::Expression::isGlobalVariableReference const):
(WebCore::WHLSL::AST::Expression::origin const): Deleted.

  • Modules/webgpu/WHLSL/AST/WHLSLFunctionDeclaration.h:

(WebCore::WHLSL::AST::FunctionDeclaration::origin):

  • Modules/webgpu/WHLSL/AST/WHLSLGlobalVariableReference.h: Added.

(WebCore::WHLSL::AST::GlobalVariableReference::GlobalVariableReference):
(WebCore::WHLSL::AST::GlobalVariableReference::structField):
(WebCore::WHLSL::AST::GlobalVariableReference::base):

  • Modules/webgpu/WHLSL/AST/WHLSLStatement.h:

(WebCore::WHLSL::AST::Statement::Statement):
(WebCore::WHLSL::AST::Statement::isStatementList const):
(WebCore::WHLSL::AST::Statement::isWhileLoop const):

  • Modules/webgpu/WHLSL/AST/WHLSLStatementList.h: Added.

(WebCore::WHLSL::AST::StatementList::StatementList):
(WebCore::WHLSL::AST::StatementList::statements):

  • Modules/webgpu/WHLSL/AST/WHLSLValue.h:

(WebCore::WHLSL::AST::Value::Value):
(WebCore::WHLSL::AST::Value::origin const):

  • Modules/webgpu/WHLSL/AST/WHLSLVariableDeclaration.h:

(WebCore::WHLSL::AST::VariableDeclaration::VariableDeclaration):
(WebCore::WHLSL::AST::VariableDeclaration::takeInitializer):
(WebCore::WHLSL::AST::VariableDeclaration::origin const): Deleted.

  • Modules/webgpu/WHLSL/AST/WHLSLVariableReference.h:

(WebCore::WHLSL::AST::VariableReference::wrap):

  • Modules/webgpu/WHLSL/Metal/WHLSLFunctionWriter.cpp:

(WebCore::WHLSL::Metal::FunctionDefinitionWriter::visit):

  • Modules/webgpu/WHLSL/WHLSLASTDumper.cpp:

(WebCore::WHLSL::ASTDumper::visit):

  • Modules/webgpu/WHLSL/WHLSLASTDumper.h:

(WebCore::WHLSL::dumpASTNode):
(WebCore::WHLSL::dumpAST):
(WebCore::WHLSL::toString): Deleted.

  • Modules/webgpu/WHLSL/WHLSLPrepare.cpp:

(WebCore::WHLSL::prepareShared):

  • Modules/webgpu/WHLSL/WHLSLPreserveVariableLifetimes.cpp: Added.

(WebCore::WHLSL::EscapedVariableCollector::takeEscapedVariables):
(WebCore::WHLSL::anonymousToken):
(WebCore::WHLSL::PreserveLifetimes::PreserveLifetimes):
(WebCore::WHLSL::PreserveLifetimes::makeStructVariableReference):
(WebCore::WHLSL::preserveVariableLifetimes):

  • Modules/webgpu/WHLSL/WHLSLPreserveVariableLifetimes.h: Added.
  • Modules/webgpu/WHLSL/WHLSLVisitor.cpp:

(WebCore::WHLSL::Visitor::visit):

  • Modules/webgpu/WHLSL/WHLSLVisitor.h:
  • Sources.txt:
  • WebCore.xcodeproj/project.pbxproj:

Source/WTF:

  • wtf/PrintStream.h:

LayoutTests:

  • webgpu/whlsl-ensure-proper-variable-lifetime-2-expected.html: Added.
  • webgpu/whlsl-ensure-proper-variable-lifetime-2.html: Added.
  • webgpu/whlsl-ensure-proper-variable-lifetime-3-expected.html: Added.
  • webgpu/whlsl-ensure-proper-variable-lifetime-3.html: Added.
  • webgpu/whlsl-ensure-proper-variable-lifetime-expected.html: Added.
  • webgpu/whlsl-ensure-proper-variable-lifetime.html: Added.
  • webgpu/whlsl-return-local-variable-expected.html: Added.
  • webgpu/whlsl-return-local-variable.html: Added.
Location:
trunk
Files:
9 added
19 edited
3 copied

Legend:

Unmodified
Added
Removed
  • trunk/LayoutTests/ChangeLog

    r245944 r245945  
     12019-05-30  Saam Barati  <sbarati@apple.com>
     2
     3        [WHLSL] Enforce variable lifetimes
     4        https://bugs.webkit.org/show_bug.cgi?id=195794
     5        <rdar://problem/50746293>
     6
     7        Reviewed by Myles C. Maxfield.
     8
     9        * webgpu/whlsl-ensure-proper-variable-lifetime-2-expected.html: Added.
     10        * webgpu/whlsl-ensure-proper-variable-lifetime-2.html: Added.
     11        * webgpu/whlsl-ensure-proper-variable-lifetime-3-expected.html: Added.
     12        * webgpu/whlsl-ensure-proper-variable-lifetime-3.html: Added.
     13        * webgpu/whlsl-ensure-proper-variable-lifetime-expected.html: Added.
     14        * webgpu/whlsl-ensure-proper-variable-lifetime.html: Added.
     15        * webgpu/whlsl-return-local-variable-expected.html: Added.
     16        * webgpu/whlsl-return-local-variable.html: Added.
     17
    1182019-05-30  Ryan Haddad  <ryanhaddad@apple.com>
    219
  • trunk/Source/WTF/ChangeLog

    r245898 r245945  
     12019-05-30  Saam Barati  <sbarati@apple.com>
     2
     3        [WHLSL] Enforce variable lifetimes
     4        https://bugs.webkit.org/show_bug.cgi?id=195794
     5        <rdar://problem/50746293>
     6
     7        Reviewed by Myles C. Maxfield.
     8
     9        * wtf/PrintStream.h:
     10
    1112019-05-30  Keith Rollin  <krollin@apple.com>
    212
  • trunk/Source/WTF/wtf/PrintStream.h

    r239427 r245945  
    133133    class Name {                                 \
    134134    public:                                      \
    135         Name(const Type& value)                  \
     135        Name(Type value)                         \
    136136            : m_value(value)                     \
    137137        {                                        \
  • trunk/Source/WebCore/ChangeLog

    r245944 r245945  
     12019-05-30  Saam Barati  <sbarati@apple.com>
     2
     3        [WHLSL] Enforce variable lifetimes
     4        https://bugs.webkit.org/show_bug.cgi?id=195794
     5        <rdar://problem/50746293>
     6
     7        Reviewed by Myles C. Maxfield.
     8
     9        In WHLSL, each variable has global lifetime. So returning a pointer to a
     10        local variable is a legitimate and well specified thing to do. Each local
     11        variable has a unique place in memory. So, for example:
     12       
     13        ```
     14        thread int* ptr() { int local; return &local; }
     15        thread int* ptrPtr() { return ptr(); }
     16        ```
     17       
     18        In the above program, ptr() must always return the same value
     19        as ptrPtr(). So, the following would print "42":
     20        ```
     21        thread int* p = ptrPtr();
     22        *ptr() = 42;
     23        print(*p);
     24        ```
     25       
     26        To implement these semantics, this patch introduces a new pass which does the
     27        following transformations:
     28        - It notes every variable whose address is taken in the program.
     29        - Each such variable gets defined as a field in a struct.
     30        - Each function which is an entry point defines this struct.
     31        - Each non entry point takes a pointer to this struct as its final parameter.
     32        - Each call to a non-native function is rewritten to pass a pointer to the
     33          struct as the last call argument.
     34        - Each variable reference to "x", where "x" ends up in the struct, is
     35          modified to instead be "struct->x". We store to "struct->x" after declaring
     36          "x". If "x" is a function parameter, we store to "struct->x" as the first
     37          thing we do in the function body.
     38
     39        Tests: webgpu/whlsl-ensure-proper-variable-lifetime-2.html
     40               webgpu/whlsl-ensure-proper-variable-lifetime-3.html
     41               webgpu/whlsl-ensure-proper-variable-lifetime.html
     42               webgpu/whlsl-return-local-variable.html
     43
     44        * Modules/webgpu/WHLSL/AST/WHLSLAST.h:
     45        * Modules/webgpu/WHLSL/AST/WHLSLExpression.h:
     46        (WebCore::WHLSL::AST::Expression::Expression):
     47        (WebCore::WHLSL::AST::Expression::isGlobalVariableReference const):
     48        (WebCore::WHLSL::AST::Expression::origin const): Deleted.
     49        * Modules/webgpu/WHLSL/AST/WHLSLFunctionDeclaration.h:
     50        (WebCore::WHLSL::AST::FunctionDeclaration::origin):
     51        * Modules/webgpu/WHLSL/AST/WHLSLGlobalVariableReference.h: Added.
     52        (WebCore::WHLSL::AST::GlobalVariableReference::GlobalVariableReference):
     53        (WebCore::WHLSL::AST::GlobalVariableReference::structField):
     54        (WebCore::WHLSL::AST::GlobalVariableReference::base):
     55        * Modules/webgpu/WHLSL/AST/WHLSLStatement.h:
     56        (WebCore::WHLSL::AST::Statement::Statement):
     57        (WebCore::WHLSL::AST::Statement::isStatementList const):
     58        (WebCore::WHLSL::AST::Statement::isWhileLoop const):
     59        * Modules/webgpu/WHLSL/AST/WHLSLStatementList.h: Added.
     60        (WebCore::WHLSL::AST::StatementList::StatementList):
     61        (WebCore::WHLSL::AST::StatementList::statements):
     62        * Modules/webgpu/WHLSL/AST/WHLSLValue.h:
     63        (WebCore::WHLSL::AST::Value::Value):
     64        (WebCore::WHLSL::AST::Value::origin const):
     65        * Modules/webgpu/WHLSL/AST/WHLSLVariableDeclaration.h:
     66        (WebCore::WHLSL::AST::VariableDeclaration::VariableDeclaration):
     67        (WebCore::WHLSL::AST::VariableDeclaration::takeInitializer):
     68        (WebCore::WHLSL::AST::VariableDeclaration::origin const): Deleted.
     69        * Modules/webgpu/WHLSL/AST/WHLSLVariableReference.h:
     70        (WebCore::WHLSL::AST::VariableReference::wrap):
     71        * Modules/webgpu/WHLSL/Metal/WHLSLFunctionWriter.cpp:
     72        (WebCore::WHLSL::Metal::FunctionDefinitionWriter::visit):
     73        * Modules/webgpu/WHLSL/WHLSLASTDumper.cpp:
     74        (WebCore::WHLSL::ASTDumper::visit):
     75        * Modules/webgpu/WHLSL/WHLSLASTDumper.h:
     76        (WebCore::WHLSL::dumpASTNode):
     77        (WebCore::WHLSL::dumpAST):
     78        (WebCore::WHLSL::toString): Deleted.
     79        * Modules/webgpu/WHLSL/WHLSLPrepare.cpp:
     80        (WebCore::WHLSL::prepareShared):
     81        * Modules/webgpu/WHLSL/WHLSLPreserveVariableLifetimes.cpp: Added.
     82        (WebCore::WHLSL::EscapedVariableCollector::takeEscapedVariables):
     83        (WebCore::WHLSL::anonymousToken):
     84        (WebCore::WHLSL::PreserveLifetimes::PreserveLifetimes):
     85        (WebCore::WHLSL::PreserveLifetimes::makeStructVariableReference):
     86        (WebCore::WHLSL::preserveVariableLifetimes):
     87        * Modules/webgpu/WHLSL/WHLSLPreserveVariableLifetimes.h: Added.
     88        * Modules/webgpu/WHLSL/WHLSLVisitor.cpp:
     89        (WebCore::WHLSL::Visitor::visit):
     90        * Modules/webgpu/WHLSL/WHLSLVisitor.h:
     91        * Sources.txt:
     92        * WebCore.xcodeproj/project.pbxproj:
     93
    1942019-05-30  Ryan Haddad  <ryanhaddad@apple.com>
    295
  • trunk/Source/WebCore/Modules/webgpu/WHLSL/AST/WHLSLAST.h

    r245613 r245945  
    5858#include "WHLSLFunctionDeclaration.h"
    5959#include "WHLSLFunctionDefinition.h"
     60#include "WHLSLGlobalVariableReference.h"
    6061#include "WHLSLIfStatement.h"
    6162#include "WHLSLIndexExpression.h"
     
    8586#include "WHLSLStageInOutSemantic.h"
    8687#include "WHLSLStatement.h"
     88#include "WHLSLStatementList.h"
    8789#include "WHLSLStructureDefinition.h"
    8890#include "WHLSLStructureElement.h"
  • trunk/Source/WebCore/Modules/webgpu/WHLSL/AST/WHLSLExpression.h

    r245680 r245945  
    4242
    4343class Expression : public Value {
     44    using Base = Value;
    4445public:
    4546    Expression(Lexer::Token&& origin)
    46         : m_origin(WTFMove(origin))
     47        : Base(WTFMove(origin))
    4748    {
    4849    }
     
    5556    Expression& operator=(const Expression&) = delete;
    5657    Expression& operator=(Expression&&) = default;
    57 
    58     const Lexer::Token& origin() const { return m_origin; }
    5958
    6059    UnnamedType* maybeResolvedType() { return m_type ? &*m_type : nullptr; }
     
    9291    virtual bool isDereferenceExpression() const { return false; }
    9392    virtual bool isDotExpression() const { return false; }
     93    virtual bool isGlobalVariableReference() const { return false; }
    9494    virtual bool isFloatLiteral() const { return false; }
    9595    virtual bool isIndexExpression() const { return false; }
     
    108108
    109109private:
    110     Lexer::Token m_origin;
    111110    Optional<UniqueRef<UnnamedType>> m_type;
    112111    Optional<TypeAnnotation> m_typeAnnotation;
  • trunk/Source/WebCore/Modules/webgpu/WHLSL/AST/WHLSLFunctionDeclaration.h

    r240230 r245945  
    7777    Optional<Semantic>& semantic() { return m_semantic; }
    7878    bool isOperator() const { return m_isOperator; }
     79    Lexer::Token origin() { return m_origin; }
    7980
    8081private:
  • trunk/Source/WebCore/Modules/webgpu/WHLSL/AST/WHLSLGlobalVariableReference.h

    r245944 r245945  
    2828#if ENABLE(WEBGPU)
    2929
    30 #include "WHLSLNode.h"
     30#include "WHLSLLexer.h"
     31#include "WHLSLStructureElement.h"
     32#include <wtf/UniqueRef.h>
    3133
    3234namespace WebCore {
     
    3638namespace AST {
    3739
    38 class Value : public Node {
     40class GlobalVariableReference : public Expression {
    3941public:
    40     Value()
     42    GlobalVariableReference(Lexer::Token&& origin, UniqueRef<Expression>&& base, StructureElement* structField)
     43        : Expression(WTFMove(origin))
     44        , m_base(WTFMove(base))
     45        , m_structField(*structField)
    4146    {
     47        ASSERT(structField);
    4248    }
    4349
    44     virtual ~Value() = default;
     50    virtual ~GlobalVariableReference() = default;
     51    bool isGlobalVariableReference() const override { return true; }
     52    StructureElement& structField() { return m_structField; }
    4553
    46     explicit Value(const Value&) = default;
    47     Value(Value&&) = default;
    48 
    49     Value& operator=(const Value&) = default;
    50     Value& operator=(Value&&) = default;
     54    Expression& base() { return m_base.get(); }
    5155
    5256private:
     57    UniqueRef<Expression> m_base;
     58    StructureElement& m_structField;
    5359};
    5460
     
    5965}
    6066
     67SPECIALIZE_TYPE_TRAITS_WHLSL_EXPRESSION(GlobalVariableReference, isGlobalVariableReference())
     68
    6169#endif
  • trunk/Source/WebCore/Modules/webgpu/WHLSL/AST/WHLSLStatement.h

    r239844 r245945  
    3939
    4040class Statement : public Value {
     41    using Base = Value;
    4142public:
    4243    Statement(Lexer::Token&& origin)
    43         : m_origin(WTFMove(origin))
     44        : Base(WTFMove(origin))
    4445    {
    4546    }
     
    5960    virtual bool isIfStatement() const { return false; }
    6061    virtual bool isReturn() const { return false; }
     62    virtual bool isStatementList() const { return false; }
    6163    virtual bool isSwitchCase() const { return false; }
    6264    virtual bool isSwitchStatement() const { return false; }
     
    6466    virtual bool isVariableDeclarationsStatement() const { return false; }
    6567    virtual bool isWhileLoop() const { return false; }
    66 
    67 private:
    68     Lexer::Token m_origin;
    6968};
    7069
  • trunk/Source/WebCore/Modules/webgpu/WHLSL/AST/WHLSLStatementList.h

    r245944 r245945  
    2828#if ENABLE(WEBGPU)
    2929
    30 #include "WHLSLNode.h"
     30#include "WHLSLLexer.h"
     31#include "WHLSLStatement.h"
     32#include <wtf/Vector.h>
    3133
    3234namespace WebCore {
     
    3638namespace AST {
    3739
    38 class Value : public Node {
     40class StatementList : public Statement {
     41    using Base = Statement;
    3942public:
    40     Value()
    41     {
    42     }
     43    StatementList(Lexer::Token&& origin, Statements&& statements)
     44        : Base(WTFMove(origin))
     45        , m_statements(WTFMove(statements))
     46    { }
    4347
    44     virtual ~Value() = default;
     48    virtual ~StatementList() = default;
    4549
    46     explicit Value(const Value&) = default;
    47     Value(Value&&) = default;
     50    Statements& statements() { return m_statements; }
    4851
    49     Value& operator=(const Value&) = default;
    50     Value& operator=(Value&&) = default;
     52    bool isStatementList() const override { return true; }
    5153
    5254private:
     55    Statements m_statements;
    5356};
    5457
     
    5962}
    6063
     64SPECIALIZE_TYPE_TRAITS_WHLSL_STATEMENT(StatementList, isStatementList())
     65
    6166#endif
  • trunk/Source/WebCore/Modules/webgpu/WHLSL/AST/WHLSLValue.h

    r239844 r245945  
    3838class Value : public Node {
    3939public:
    40     Value()
     40    Value(Lexer::Token&& origin)
     41        : m_origin(WTFMove(origin))
    4142    {
    4243    }
     
    5051    Value& operator=(Value&&) = default;
    5152
    52 private:
     53    Lexer::Token origin() const { return m_origin; }
     54
     55protected:
     56    Lexer::Token m_origin;
    5357};
    5458
  • trunk/Source/WebCore/Modules/webgpu/WHLSL/AST/WHLSLVariableDeclaration.h

    r245680 r245945  
    4646
    4747class VariableDeclaration : public Value {
     48    using Base = Value;
    4849public:
    4950    VariableDeclaration(Lexer::Token&& origin, Qualifiers&& qualifiers, Optional<UniqueRef<UnnamedType>>&& type, String&& name, Optional<Semantic>&& semantic, Optional<UniqueRef<Expression>>&& initializer)
    50         : m_origin(WTFMove(origin))
     51        : Base(WTFMove(origin))
    5152        , m_qualifiers(WTFMove(qualifiers))
    5253        , m_type(WTFMove(type))
     
    6263    VariableDeclaration(VariableDeclaration&&) = default;
    6364
    64     const Lexer::Token& origin() const { return m_origin; }
    6565    String& name() { return m_name; }
    6666
     
    7070    Expression* initializer() { return m_initializer ? &*m_initializer : nullptr; }
    7171    bool isAnonymous() const { return m_name.isNull(); }
     72    Optional<UniqueRef<Expression>> takeInitializer() { return WTFMove(m_initializer); }
    7273
    7374private:
    74     Lexer::Token m_origin;
    7575    Qualifiers m_qualifiers;
    7676    Optional<UniqueRef<UnnamedType>> m_type;
  • trunk/Source/WebCore/Modules/webgpu/WHLSL/AST/WHLSLVariableReference.h

    r243091 r245945  
    5656        VariableReference result(Lexer::Token(variableDeclaration.origin()));
    5757        result.m_variable = &variableDeclaration;
     58        result.m_name = variableDeclaration.name();
    5859        return result;
    5960    }
  • trunk/Source/WebCore/Modules/webgpu/WHLSL/Metal/WHLSLFunctionWriter.cpp

    r245680 r245945  
    3030
    3131#include "NotImplemented.h"
    32 #include "WHLSLArrayReferenceType.h"
    33 #include "WHLSLArrayType.h"
    34 #include "WHLSLAssignmentExpression.h"
    35 #include "WHLSLBooleanLiteral.h"
    36 #include "WHLSLBuiltInSemantic.h"
    37 #include "WHLSLCallExpression.h"
    38 #include "WHLSLCommaExpression.h"
    39 #include "WHLSLDereferenceExpression.h"
    40 #include "WHLSLDoWhileLoop.h"
    41 #include "WHLSLEffectfulExpressionStatement.h"
    42 #include "WHLSLEntryPointScaffolding.h"
    43 #include "WHLSLEntryPointType.h"
    44 #include "WHLSLFloatLiteral.h"
    45 #include "WHLSLForLoop.h"
    46 #include "WHLSLFunctionDeclaration.h"
    47 #include "WHLSLFunctionDefinition.h"
    48 #include "WHLSLIfStatement.h"
    49 #include "WHLSLIntegerLiteral.h"
    50 #include "WHLSLLogicalExpression.h"
    51 #include "WHLSLLogicalNotExpression.h"
    52 #include "WHLSLMakeArrayReferenceExpression.h"
    53 #include "WHLSLMakePointerExpression.h"
    54 #include "WHLSLNativeFunctionDeclaration.h"
     32#include "WHLSLAST.h"
    5533#include "WHLSLNativeFunctionWriter.h"
    56 #include "WHLSLNativeTypeDeclaration.h"
    57 #include "WHLSLPointerType.h"
    5834#include "WHLSLProgram.h"
    59 #include "WHLSLReturn.h"
    60 #include "WHLSLSwitchCase.h"
    61 #include "WHLSLSwitchStatement.h"
    62 #include "WHLSLTernaryExpression.h"
    6335#include "WHLSLTypeNamer.h"
    64 #include "WHLSLUnsignedIntegerLiteral.h"
    65 #include "WHLSLVariableDeclaration.h"
    66 #include "WHLSLVariableDeclarationsStatement.h"
    67 #include "WHLSLVariableReference.h"
    6836#include "WHLSLVisitor.h"
    69 #include "WHLSLWhileLoop.h"
    7037#include <wtf/HashMap.h>
    7138#include <wtf/text/StringBuilder.h>
     
    157124    void visit(AST::Expression&) override;
    158125    void visit(AST::DotExpression&) override;
     126    void visit(AST::GlobalVariableReference&) override;
    159127    void visit(AST::IndexExpression&) override;
    160128    void visit(AST::PropertyAccessExpression&) override;
     
    447415}
    448416
     417void FunctionDefinitionWriter::visit(AST::GlobalVariableReference& globalVariableReference)
     418{
     419    auto variableName = generateNextVariableName();
     420    auto mangledTypeName = m_typeNamer.mangledNameForType(globalVariableReference.resolvedType());
     421    checkErrorAndVisit(globalVariableReference.base());
     422    m_stringBuilder.append(makeString("thread ", mangledTypeName, "& ", variableName, " = ", m_stack.takeLast(), "->", m_typeNamer.mangledNameForStructureElement(globalVariableReference.structField()), ";\n"));
     423    m_stack.append(variableName);
     424}
     425
    449426void FunctionDefinitionWriter::visit(AST::IndexExpression&)
    450427{
  • trunk/Source/WebCore/Modules/webgpu/WHLSL/WHLSLASTDumper.cpp

    r245745 r245945  
    373373}
    374374
     375void ASTDumper::visit(AST::StatementList& statementList)
     376{
     377    bool once = false;
     378    for (auto& statement : statementList.statements()) {
     379        if (once)
     380            m_out.print(";\n", m_indent);
     381        once = true;
     382        visit(statement);
     383    }
     384}
     385
    375386void ASTDumper::visit(AST::Break&)
    376387{
     
    442453}
    443454
     455void ASTDumper::visit(AST::GlobalVariableReference& globalVariableReference)
     456{
     457    visit(globalVariableReference.base());
     458    m_out.print("=>", globalVariableReference.structField().name());
     459}
     460
    444461void ASTDumper::visit(AST::IndexExpression& indexExpression)
    445462{
     
    530547        m_out.print(" ");
    531548    }
    532     m_out.print(variableDeclaration.name());
     549
     550    if (variableDeclaration.name().isEmpty())
     551        m_out.print("$", RawPointer(&variableDeclaration));
     552    else
     553        m_out.print(variableDeclaration.name());
     554
    533555    if (variableDeclaration.semantic())
    534556        visit(*variableDeclaration.semantic());
  • trunk/Source/WebCore/Modules/webgpu/WHLSL/WHLSLASTDumper.h

    r245613 r245945  
    4545    String toString() { return m_out.toString(); }
    4646
    47 private:
    4847    void visit(AST::UnnamedType&) override;
    4948    void visit(AST::NamedType&) override;
     
    8382    void visit(AST::NumThreadsFunctionAttribute&) override;
    8483    void visit(AST::Block&) override;
     84    void visit(AST::StatementList&) override;
    8585    void visit(AST::Statement&) override;
    8686    void visit(AST::Break&) override;
     
    8989    void visit(AST::Expression&) override;
    9090    void visit(AST::DotExpression&) override;
     91    void visit(AST::GlobalVariableReference&) override;
    9192    void visit(AST::IndexExpression&) override;
    9293    void visit(AST::PropertyAccessExpression&) override;
     
    114115    void visit(AST::VariableReference&) override;
    115116
     117private:
    116118    struct Indent {
    117119        Indent(ASTDumper& dumper)
     
    129131};
    130132
    131 static ALWAYS_INLINE String toString(Program& program)
     133template <typename T>
     134ALWAYS_INLINE void dumpASTNode(PrintStream& out, T& value)
    132135{
    133136    ASTDumper dumper;
    134     dumper.visit(program);
    135     return dumper.toString();
     137    dumper.visit(value);
     138    out.print(dumper.toString());
    136139}
     140MAKE_PRINT_ADAPTOR(ExpressionDumper, AST::Expression&, dumpASTNode);
     141MAKE_PRINT_ADAPTOR(StatementDumper, AST::Statement&, dumpASTNode);
     142MAKE_PRINT_ADAPTOR(ProgramDumper, Program&, dumpASTNode);
     143MAKE_PRINT_ADAPTOR(StructureDefinitionDumper, AST::StructureDefinition&, dumpASTNode);
     144MAKE_PRINT_ADAPTOR(FunctionDefinitionDumper, AST::FunctionDefinition&, dumpASTNode);
     145
    137146
    138147static ALWAYS_INLINE void dumpAST(Program& program)
    139148{
    140     dataLogLn(toString(program));
     149    dataLogLn(ProgramDumper(program));
    141150}
    142151
  • trunk/Source/WebCore/Modules/webgpu/WHLSL/WHLSLPrepare.cpp

    r245680 r245945  
    3838#include "WHLSLNameResolver.h"
    3939#include "WHLSLParser.h"
     40#include "WHLSLPreserveVariableLifetimes.h"
    4041#include "WHLSLProgram.h"
    4142#include "WHLSLPropertyResolver.h"
     
    5758static constexpr bool dumpASTBeforeEachPass = false;
    5859static constexpr bool dumpASTAfterParsing = false;
    59 static constexpr bool dumpASTAtEnd = false;
     60static constexpr bool dumpASTAtEnd = true;
     61static constexpr bool alwaysDumpPassFailures = false;
     62static constexpr bool dumpPassFailure = dumpASTBeforeEachPass || dumpASTAfterParsing || dumpASTAtEnd || alwaysDumpPassFailures;
    6063
    6164static bool dumpASTIfNeeded(bool shouldDump, Program& program, const char* message)
     
    8891    do { \
    8992        dumpASTBetweenEachPassIfNeeded(program, "AST before " # pass); \
    90         if (!pass(__VA_ARGS__)) \
     93        if (!pass(__VA_ARGS__)) { \
     94            if (dumpPassFailure) \
     95                dataLogLn("failed pass: " # pass); \
    9196            return WTF::nullopt; \
     97        } \
    9298    } while (0)
    9399   
     
    124130    RUN_PASS(checkRecursion, program);
    125131    RUN_PASS(checkFunctionStages, program);
     132    preserveVariableLifetimes(program);
    126133
    127134    dumpASTAtEndIfNeeded(program);
  • trunk/Source/WebCore/Modules/webgpu/WHLSL/WHLSLPreserveVariableLifetimes.h

    r245944 r245945  
    2828#if ENABLE(WEBGPU)
    2929
    30 #include "WHLSLNode.h"
    31 
    3230namespace WebCore {
    3331
    3432namespace WHLSL {
    3533
    36 namespace AST {
     34class Program;
    3735
    38 class Value : public Node {
    39 public:
    40     Value()
    41     {
    42     }
    43 
    44     virtual ~Value() = default;
    45 
    46     explicit Value(const Value&) = default;
    47     Value(Value&&) = default;
    48 
    49     Value& operator=(const Value&) = default;
    50     Value& operator=(Value&&) = default;
    51 
    52 private:
    53 };
    54 
    55 } // namespace AST
     36void preserveVariableLifetimes(Program&);
    5637
    5738}
  • trunk/Source/WebCore/Modules/webgpu/WHLSL/WHLSLVisitor.cpp

    r245745 r245945  
    301301}
    302302
     303void Visitor::visit(AST::StatementList& statementList)
     304{
     305    for (auto& statement : statementList.statements())
     306        checkErrorAndVisit(statement);
     307}
     308
    303309void Visitor::visit(AST::Statement& statement)
    304310{
     
    321327    else if (is<AST::Return>(statement))
    322328        checkErrorAndVisit(downcast<AST::Return>(statement));
     329    else if (is<AST::StatementList>(statement))
     330        checkErrorAndVisit(downcast<AST::StatementList>(statement));
    323331    else if (is<AST::SwitchCase>(statement))
    324332        checkErrorAndVisit(downcast<AST::SwitchCase>(statement));
     
    377385    else if (is<AST::DotExpression>(expression))
    378386        checkErrorAndVisit(downcast<AST::DotExpression>(expression));
     387    else if (is<AST::GlobalVariableReference>(expression))
     388        checkErrorAndVisit(downcast<AST::GlobalVariableReference>(expression));
    379389    else if (is<AST::IndexExpression>(expression))
    380390        checkErrorAndVisit(downcast<AST::IndexExpression>(expression));
     
    398408}
    399409
     410void Visitor::visit(AST::GlobalVariableReference& globalVariableReference)
     411{
     412    checkErrorAndVisit(globalVariableReference.base());
     413}
     414
    400415void Visitor::visit(AST::IndexExpression& indexExpression)
    401416{
  • trunk/Source/WebCore/Modules/webgpu/WHLSL/WHLSLVisitor.h

    r245680 r245945  
    6767class NumThreadsFunctionAttribute;
    6868class Block;
     69class StatementList;
    6970class Statement;
    7071class Break;
     
    7374class Expression;
    7475class DotExpression;
     76class GlobalVariableReference;
    7577class IndexExpression;
    7678class PropertyAccessExpression;
     
    146148    virtual void visit(AST::NumThreadsFunctionAttribute&);
    147149    virtual void visit(AST::Block&);
     150    virtual void visit(AST::StatementList&);
    148151    virtual void visit(AST::Statement&);
    149152    virtual void visit(AST::Break&);
     
    152155    virtual void visit(AST::Expression&);
    153156    virtual void visit(AST::DotExpression&);
     157    virtual void visit(AST::GlobalVariableReference&);
    154158    virtual void visit(AST::IndexExpression&);
    155159    virtual void visit(AST::PropertyAccessExpression&);
  • trunk/Source/WebCore/Sources.txt

    r245905 r245945  
    323323Modules/webgpu/WHLSL/WHLSLNameContext.cpp
    324324Modules/webgpu/WHLSL/WHLSLNameResolver.cpp
     325Modules/webgpu/WHLSL/WHLSLPreserveVariableLifetimes.cpp
    325326Modules/webgpu/WHLSL/WHLSLResolveOverloadImpl.cpp
    326327Modules/webgpu/WHLSL/WHLSLRecursionChecker.cpp
  • trunk/Source/WebCore/WebCore.xcodeproj/project.pbxproj

    r245905 r245945  
    83378337                52131E5A1C4F15610033F802 /* VideoFullscreenInterfaceMac.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = VideoFullscreenInterfaceMac.mm; sourceTree = "<group>"; };
    83388338                5215862C229377B7005925EF /* WHLSLAST.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = WHLSLAST.h; sourceTree = "<group>"; };
     8339                522DA3D3229E1D390042D151 /* WHLSLGlobalVariableReference.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = WHLSLGlobalVariableReference.h; sourceTree = "<group>"; };
     8340                522E1A172297D6D400E5D36A /* WHLSLPreserveVariableLifetimes.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = WHLSLPreserveVariableLifetimes.cpp; sourceTree = "<group>"; };
     8341                522E1A192297D6D400E5D36A /* WHLSLPreserveVariableLifetimes.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = WHLSLPreserveVariableLifetimes.h; sourceTree = "<group>"; };
    83398342                526724F11CB2FDF60075974D /* TextTrackRepresentationCocoa.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = TextTrackRepresentationCocoa.mm; sourceTree = "<group>"; };
    83408343                526724F21CB2FDF60075974D /* TextTrackRepresentationCocoa.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = TextTrackRepresentationCocoa.h; sourceTree = "<group>"; };
     
    1706917072                                C21BF6FD21CD89C000227979 /* WHLSLFunctionDeclaration.h */,
    1707017073                                C21BF6F421CD89B300227979 /* WHLSLFunctionDefinition.h */,
     17074                                522DA3D3229E1D390042D151 /* WHLSLGlobalVariableReference.h */,
    1707117075                                C21BF6FF21CD89C200227979 /* WHLSLIfStatement.h */,
    1707217076                                C21BF6F721CD89B900227979 /* WHLSLIndexExpression.h */,
     
    2545625460                                C24A57BA21FEAFEA004C6DD1 /* WHLSLPrepare.cpp */,
    2545725461                                C24A57BB21FEAFEA004C6DD1 /* WHLSLPrepare.h */,
     25462                                522E1A172297D6D400E5D36A /* WHLSLPreserveVariableLifetimes.cpp */,
     25463                                522E1A192297D6D400E5D36A /* WHLSLPreserveVariableLifetimes.h */,
    2545825464                                C21BF73A21CD8D7000227979 /* WHLSLProgram.h */,
    2545925465                                1CAA82F62242AE0500E84BBB /* WHLSLPropertyResolver.cpp */,
Note: See TracChangeset for help on using the changeset viewer.