Changeset 245706 in webkit


Ignore:
Timestamp:
May 23, 2019 12:54:53 PM (5 years ago)
Author:
sbarati@apple.com
Message:

[WHLSL] Add a helper for in-place AST mutation
https://bugs.webkit.org/show_bug.cgi?id=198175

Reviewed by Myles Maxfield.

This makes WHLSL AST mutation code a bit easier to read and write.

Code that looked like:
`
static_assert(sizeof(AST::DereferenceExpression) <= sizeof(AST::DotExpression), "Dot expressions need to be able to become dereference expressions without updating backreferences");
void* location = &dotExpression;
dotExpression.~DotExpression();
auto* dereferenceExpression = new (location) AST::DereferenceExpression(WTFMove(origin), WTFMove(callExpression));
`

Can now be:
`
auto* dereferenceExpression = AST::replaceWith<AST::DereferenceExpression>(dotExpression, WTFMove(origin), WTFMove(callExpression));
`

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

(WebCore::WHLSL::AST::replaceWith):

  • Modules/webgpu/WHLSL/WHLSLNameResolver.cpp:

(WebCore::WHLSL::NameResolver::visit):

  • Modules/webgpu/WHLSL/WHLSLPropertyResolver.cpp:

(WebCore::WHLSL::PropertyResolver::visit):
(WebCore::WHLSL::PropertyResolver::simplifyRightValue):
(WebCore::WHLSL::LeftValueSimplifier::visit):

Location:
trunk/Source/WebCore
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/Source/WebCore/ChangeLog

    r245701 r245706  
     12019-05-23  Saam barati  <sbarati@apple.com>
     2
     3        [WHLSL] Add a helper for in-place AST mutation
     4        https://bugs.webkit.org/show_bug.cgi?id=198175
     5
     6        Reviewed by Myles Maxfield.
     7
     8        This makes WHLSL AST mutation code a bit easier to read and write.
     9       
     10        Code that looked like:
     11        ```
     12        static_assert(sizeof(AST::DereferenceExpression) <= sizeof(AST::DotExpression), "Dot expressions need to be able to become dereference expressions without updating backreferences");
     13        void* location = &dotExpression;
     14        dotExpression.~DotExpression();
     15        auto* dereferenceExpression = new (location) AST::DereferenceExpression(WTFMove(origin), WTFMove(callExpression));
     16        ```
     17       
     18        Can now be:
     19        ```
     20        auto* dereferenceExpression = AST::replaceWith<AST::DereferenceExpression>(dotExpression, WTFMove(origin), WTFMove(callExpression));
     21        ```
     22
     23        * Modules/webgpu/WHLSL/AST/WHLSLNode.h:
     24        (WebCore::WHLSL::AST::replaceWith):
     25        * Modules/webgpu/WHLSL/WHLSLNameResolver.cpp:
     26        (WebCore::WHLSL::NameResolver::visit):
     27        * Modules/webgpu/WHLSL/WHLSLPropertyResolver.cpp:
     28        (WebCore::WHLSL::PropertyResolver::visit):
     29        (WebCore::WHLSL::PropertyResolver::simplifyRightValue):
     30        (WebCore::WHLSL::LeftValueSimplifier::visit):
     31
    1322019-05-23  Eric Carlson  <eric.carlson@apple.com>
    233
  • trunk/Source/WebCore/Modules/webgpu/WHLSL/AST/WHLSLNode.h

    r239844 r245706  
    4949};
    5050
     51template <typename New, typename Old, typename ...Args>
     52ALWAYS_INLINE New* replaceWith(Old& old, Args&&... args)
     53{
     54    static_assert(sizeof(New) <= sizeof(Old), "This is needed for the placement new below to not overwrite unowned memory.");
     55    void* location = &old;
     56    old.~Old();
     57    return new (location) New(std::forward<Args>(args)...);
     58}
     59
    5160} // namespace AST
    5261
  • trunk/Source/WebCore/Modules/webgpu/WHLSL/WHLSLNameResolver.cpp

    r245680 r245706  
    195195                auto memberName = dotExpression.fieldName();
    196196                if (auto* member = enumerationDefinition.memberByName(memberName)) {
    197                     static_assert(sizeof(AST::EnumerationMemberLiteral) <= sizeof(AST::DotExpression), "Dot expressions need to be able to become EnumerationMemberLiterals without updating backreferences");
    198197                    Lexer::Token origin = dotExpression.origin();
    199                     void* location = &dotExpression;
    200                     dotExpression.~DotExpression();
    201198                    auto enumerationMemberLiteral = AST::EnumerationMemberLiteral::wrap(WTFMove(origin), WTFMove(baseName), WTFMove(memberName), enumerationDefinition, *member);
    202                     new (location) AST::EnumerationMemberLiteral(WTFMove(enumerationMemberLiteral));
     199                    AST::replaceWith<AST::EnumerationMemberLiteral>(dotExpression, WTFMove(enumerationMemberLiteral));
    203200                    return;
    204201                }
  • trunk/Source/WebCore/Modules/webgpu/WHLSL/WHLSLPropertyResolver.cpp

    r245680 r245706  
    405405    simplifyLeftValue(modifyResult->innerLeftValue);
    406406
    407     static_assert(sizeof(AST::CommaExpression) <= sizeof(AST::AssignmentExpression), "Assignment expressions need to be able to become comma expressions without updating backreferences");
    408407    Lexer::Token origin = assignmentExpression.origin();
    409     void* location = &assignmentExpression;
    410     assignmentExpression.~AssignmentExpression();
    411     auto* commaExpression = new (location) AST::CommaExpression(WTFMove(origin), WTFMove(modifyResult->expressions));
     408    auto* commaExpression = AST::replaceWith<AST::CommaExpression>(assignmentExpression, WTFMove(origin), WTFMove(modifyResult->expressions));
    412409    commaExpression->setType(WTFMove(type));
    413410    commaExpression->setTypeAnnotation(AST::RightValue());
     
    514511        UniqueRef<AST::VariableDeclaration> newVariableDeclaration = readModifyWriteExpression.takeNewValue();
    515512
    516         static_assert(sizeof(AST::CommaExpression) <= sizeof(AST::ReadModifyWriteExpression), "ReadModifyWrite expressions need to be able to become comma expressions without updating backreferences");
    517513        Lexer::Token origin = readModifyWriteExpression.origin();
    518         void* location = &readModifyWriteExpression;
    519         readModifyWriteExpression.~ReadModifyWriteExpression();
    520         auto* commaExpression = new (location) AST::CommaExpression(WTFMove(origin), WTFMove(expressions));
     514        auto* commaExpression = AST::replaceWith<AST::CommaExpression>(readModifyWriteExpression, WTFMove(origin), WTFMove(expressions));
    521515        commaExpression->setType(WTFMove(type));
    522516        commaExpression->setTypeAnnotation(AST::RightValue());
     
    581575    UniqueRef<AST::VariableDeclaration> newVariableDeclaration = readModifyWriteExpression.takeNewValue();
    582576
    583     static_assert(sizeof(AST::CommaExpression) <= sizeof(AST::ReadModifyWriteExpression), "ReadModifyWrite expressions need to be able to become comma expressions without updating backreferences");
    584577    Lexer::Token origin = readModifyWriteExpression.origin();
    585     void* location = &readModifyWriteExpression;
    586     readModifyWriteExpression.~ReadModifyWriteExpression();
    587     auto* commaExpression = new (location) AST::CommaExpression(WTFMove(origin), WTFMove(modifyResult->expressions));
     578    auto* commaExpression = AST::replaceWith<AST::CommaExpression>(readModifyWriteExpression, WTFMove(origin), WTFMove(modifyResult->expressions));
    588579    commaExpression->setType(WTFMove(type));
    589580    commaExpression->setTypeAnnotation(AST::RightValue());
     
    613604            callExpression->setFunction(*anderFunction);
    614605
    615             static_assert(sizeof(AST::DereferenceExpression) <= sizeof(AST::DotExpression), "Dot expressions need to be able to become dereference expressions without updating backreferences");
    616             void* location = &dotExpression;
    617             dotExpression.~DotExpression();
    618             auto* dereferenceExpression = new (location) AST::DereferenceExpression(WTFMove(origin), WTFMove(callExpression));
     606            auto* dereferenceExpression = AST::replaceWith<AST::DereferenceExpression>(dotExpression, WTFMove(origin), WTFMove(callExpression));
    619607            dereferenceExpression->setType(downcast<AST::PointerType>(anderFunction->type()).elementType().clone());
    620608            dereferenceExpression->setTypeAnnotation(AST::LeftValue { downcast<AST::PointerType>(anderFunction->type()).addressSpace() });
     
    656644        dereferenceExpression->setTypeAnnotation(AST::LeftValue { AST::AddressSpace::Thread });
    657645
    658         static_assert(sizeof(AST::CommaExpression) <= sizeof(AST::DotExpression), "Dot expressions need to be able to become comma expressions without updating backreferences");
    659         void* location = &dotExpression;
    660         dotExpression.~DotExpression();
    661646        Vector<UniqueRef<AST::Expression>> expressions;
    662647        expressions.append(WTFMove(assignmentExpression));
    663648        expressions.append(WTFMove(dereferenceExpression));
    664         auto* commaExpression = new (location) AST::CommaExpression(WTFMove(origin), WTFMove(expressions));
     649        auto* commaExpression = AST::replaceWith<AST::CommaExpression>(dotExpression, WTFMove(origin), WTFMove(expressions));
    665650        commaExpression->setType(downcast<AST::PointerType>(anderFunction->type()).elementType().clone());
    666651        commaExpression->setTypeAnnotation(AST::LeftValue { AST::AddressSpace::Thread });
     
    670655    }
    671656
    672     static_assert(sizeof(AST::CallExpression) <= sizeof(AST::DotExpression), "Dot expressions need to be able to become call expressions without updating backreferences");
    673657    ASSERT(dotExpression.getterFunction());
    674658    auto& getterFunction = *dotExpression.getterFunction();
    675659    Vector<UniqueRef<AST::Expression>> arguments;
    676660    arguments.append(dotExpression.takeBase());
    677     void* location = &dotExpression;
    678     dotExpression.~DotExpression();
    679     auto* callExpression = new (location) AST::CallExpression(WTFMove(origin), String(getterFunction.name()), WTFMove(arguments));
     661    auto* callExpression = AST::replaceWith<AST::CallExpression>(dotExpression, WTFMove(origin), String(getterFunction.name()), WTFMove(arguments));
    680662    callExpression->setFunction(getterFunction);
    681663    callExpression->setType(getterFunction.type().clone());
     
    713695    callExpression->setFunction(*anderFunction);
    714696
    715     static_assert(sizeof(AST::DereferenceExpression) <= sizeof(AST::DotExpression), "Dot expressions need to be able to become dereference expressions without updating backreferences");
    716     void* location = &dotExpression;
    717     dotExpression.~DotExpression();
    718     auto* dereferenceExpression = new (location) AST::DereferenceExpression(WTFMove(origin), WTFMove(callExpression));
     697    auto* dereferenceExpression = AST::replaceWith<AST::DereferenceExpression>(dotExpression, WTFMove(origin), WTFMove(callExpression));
    719698    dereferenceExpression->setType(downcast<AST::PointerType>(anderFunction->type()).elementType().clone());
    720699    dereferenceExpression->setTypeAnnotation(AST::LeftValue { downcast<AST::PointerType>(anderFunction->type()).addressSpace() });
Note: See TracChangeset for help on using the changeset viewer.