Changeset 246273 in webkit


Ignore:
Timestamp:
Jun 10, 2019 12:58:19 PM (5 years ago)
Author:
sbarati@apple.com
Message:

[WHLSL] Auto initialize local variables
https://bugs.webkit.org/show_bug.cgi?id=198426

Reviewed by Myles Maxfield.

Source/WebCore:

This patch implements zero-filling for local variables in two parts:

  1. We add a new pass, autoInitializeVariables, which makes any variable declaration

without an initializer call the default constructor for the variable's type.
Since we auto generate the default constructor, it's a native function whose
implementation we control.

  1. Each native constructor is implemented as a memset(&value, sizeof(value), 0).

This memset is an inlined loop in each constructor. The reason this turns
everything into zero is that for every primitive type, the "zero" value is
represented as all zeroes in memory: float, int, pointers, etc.

Since our ability to test some of this is limited, I opened a follow-up bug to
test this more:
https://bugs.webkit.org/show_bug.cgi?id=198413

Tests: webgpu/whlsl-zero-initialize-values-2.html

webgpu/whlsl-zero-initialize-values.html

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

(WebCore::WHLSL::AST::VariableDeclaration::setInitializer):

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

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

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

(WebCore::WHLSL::Metal::writeNativeFunction):

  • Modules/webgpu/WHLSL/WHLSLASTDumper.h:
  • Modules/webgpu/WHLSL/WHLSLAutoInitializeVariables.cpp: Added.

(WebCore::WHLSL::AutoInitialize::AutoInitialize):
(WebCore::WHLSL::AutoInitialize::visit):
(WebCore::WHLSL::autoInitializeVariables):

  • Modules/webgpu/WHLSL/WHLSLAutoInitializeVariables.h: Added.
  • Modules/webgpu/WHLSL/WHLSLChecker.cpp:

(WebCore::WHLSL::checkOperatorOverload):
(WebCore::WHLSL::Checker::visit):

  • Modules/webgpu/WHLSL/WHLSLInferTypes.cpp:

(WebCore::WHLSL::inferTypesForCallImpl):
(WebCore::WHLSL::inferTypesForCall):

  • Modules/webgpu/WHLSL/WHLSLInferTypes.h:
  • Modules/webgpu/WHLSL/WHLSLPrepare.cpp:

(WebCore::WHLSL::prepareShared):

  • Modules/webgpu/WHLSL/WHLSLResolveOverloadImpl.cpp:

(WebCore::WHLSL::resolveFunctionOverloadImpl):
(WebCore::WHLSL::resolveFunctionOverload):

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

LayoutTests:

  • webgpu/whlsl-zero-initialize-values-2-expected.html: Added.
  • webgpu/whlsl-zero-initialize-values-2.html: Added.
  • webgpu/whlsl-zero-initialize-values-expected.html: Added.
  • webgpu/whlsl-zero-initialize-values.html: Added.
Location:
trunk
Files:
5 added
14 edited
1 copied

Legend:

Unmodified
Added
Removed
  • trunk/LayoutTests/ChangeLog

    r246270 r246273  
     12019-06-10  Saam Barati  <sbarati@apple.com>
     2
     3        [WHLSL] Auto initialize local variables
     4        https://bugs.webkit.org/show_bug.cgi?id=198426
     5
     6        Reviewed by Myles Maxfield.
     7
     8        * webgpu/whlsl-zero-initialize-values-2-expected.html: Added.
     9        * webgpu/whlsl-zero-initialize-values-2.html: Added.
     10        * webgpu/whlsl-zero-initialize-values-expected.html: Added.
     11        * webgpu/whlsl-zero-initialize-values.html: Added.
     12
    1132019-06-10  Timothy Hatcher  <timothy@apple.com>
    214
  • trunk/Source/WebCore/ChangeLog

    r246270 r246273  
     12019-06-10  Saam Barati  <sbarati@apple.com>
     2
     3        [WHLSL] Auto initialize local variables
     4        https://bugs.webkit.org/show_bug.cgi?id=198426
     5
     6        Reviewed by Myles Maxfield.
     7
     8        This patch implements zero-filling for local variables in two parts:
     9        1. We add a new pass, autoInitializeVariables, which makes any variable declaration
     10        without an initializer call the default constructor for the variable's type.
     11        Since we auto generate the default constructor, it's a native function whose
     12        implementation we control.
     13       
     14        2. Each native constructor is implemented as a memset(&value, sizeof(value), 0).
     15        This memset is an inlined loop in each constructor. The reason this turns
     16        everything into zero is that for every primitive type, the "zero" value is
     17        represented as all zeroes in memory: float, int, pointers, etc.
     18       
     19        Since our ability to test some of this is limited, I opened a follow-up bug to
     20        test this more:
     21        https://bugs.webkit.org/show_bug.cgi?id=198413
     22
     23        Tests: webgpu/whlsl-zero-initialize-values-2.html
     24               webgpu/whlsl-zero-initialize-values.html
     25
     26        * Modules/webgpu/WHLSL/AST/WHLSLVariableDeclaration.h:
     27        (WebCore::WHLSL::AST::VariableDeclaration::setInitializer):
     28        * Modules/webgpu/WHLSL/Metal/WHLSLFunctionWriter.cpp:
     29        (WebCore::WHLSL::Metal::FunctionDefinitionWriter::visit):
     30        * Modules/webgpu/WHLSL/Metal/WHLSLNativeFunctionWriter.cpp:
     31        (WebCore::WHLSL::Metal::writeNativeFunction):
     32        * Modules/webgpu/WHLSL/WHLSLASTDumper.h:
     33        * Modules/webgpu/WHLSL/WHLSLAutoInitializeVariables.cpp: Added.
     34        (WebCore::WHLSL::AutoInitialize::AutoInitialize):
     35        (WebCore::WHLSL::AutoInitialize::visit):
     36        (WebCore::WHLSL::autoInitializeVariables):
     37        * Modules/webgpu/WHLSL/WHLSLAutoInitializeVariables.h: Added.
     38        * Modules/webgpu/WHLSL/WHLSLChecker.cpp:
     39        (WebCore::WHLSL::checkOperatorOverload):
     40        (WebCore::WHLSL::Checker::visit):
     41        * Modules/webgpu/WHLSL/WHLSLInferTypes.cpp:
     42        (WebCore::WHLSL::inferTypesForCallImpl):
     43        (WebCore::WHLSL::inferTypesForCall):
     44        * Modules/webgpu/WHLSL/WHLSLInferTypes.h:
     45        * Modules/webgpu/WHLSL/WHLSLPrepare.cpp:
     46        (WebCore::WHLSL::prepareShared):
     47        * Modules/webgpu/WHLSL/WHLSLResolveOverloadImpl.cpp:
     48        (WebCore::WHLSL::resolveFunctionOverloadImpl):
     49        (WebCore::WHLSL::resolveFunctionOverload):
     50        * Modules/webgpu/WHLSL/WHLSLResolveOverloadImpl.h:
     51        * Sources.txt:
     52        * WebCore.xcodeproj/project.pbxproj:
     53
    1542019-06-10  Timothy Hatcher  <timothy@apple.com>
    255
  • trunk/Source/WebCore/Modules/webgpu/WHLSL/AST/WHLSLVariableDeclaration.h

    r245945 r246273  
    7171    bool isAnonymous() const { return m_name.isNull(); }
    7272    Optional<UniqueRef<Expression>> takeInitializer() { return WTFMove(m_initializer); }
     73    void setInitializer(UniqueRef<Expression> expression)
     74    {
     75        ASSERT(!initializer());
     76        m_initializer = WTFMove(expression);
     77    }
    7378
    7479private:
  • trunk/Source/WebCore/Modules/webgpu/WHLSL/Metal/WHLSLFunctionWriter.cpp

    r246121 r246273  
    478478        checkErrorAndVisit(*variableDeclaration.initializer());
    479479        m_stringBuilder.append(makeString(m_typeNamer.mangledNameForType(*variableDeclaration.type()), ' ', variableName, " = ", m_stack.takeLast(), ";\n"));
    480     } else {
    481         // FIXME: https://bugs.webkit.org/show_bug.cgi?id=195771 Zero-fill the variable.
     480    } else
    482481        m_stringBuilder.append(makeString(m_typeNamer.mangledNameForType(*variableDeclaration.type()), ' ', variableName, ";\n"));
    483     }
    484482}
    485483
  • trunk/Source/WebCore/Modules/webgpu/WHLSL/Metal/WHLSLNativeFunctionWriter.cpp

    r246225 r246273  
    105105            stringBuilder.append(makeString(metalReturnName, ' ', outputFunctionName, "() {\n"));
    106106            stringBuilder.append(makeString("    ", metalReturnName, " x;\n"));
    107             // FIXME: https://bugs.webkit.org/show_bug.cgi?id=195771 Zero-fill
     107            stringBuilder.append("    thread char* ptr = static_cast<thread char*>(static_cast<thread void*>(&x));\n");
     108            stringBuilder.append(makeString("    for (size_t i = 0; i < sizeof(", metalReturnName, "); ++i) ptr[i] = 0;\n"));
    108109            stringBuilder.append("    return x;\n");
    109110            stringBuilder.append("}\n");
  • trunk/Source/WebCore/Modules/webgpu/WHLSL/WHLSLASTDumper.h

    r245945 r246273  
    143143MAKE_PRINT_ADAPTOR(StructureDefinitionDumper, AST::StructureDefinition&, dumpASTNode);
    144144MAKE_PRINT_ADAPTOR(FunctionDefinitionDumper, AST::FunctionDefinition&, dumpASTNode);
     145MAKE_PRINT_ADAPTOR(TypeDumper, AST::UnnamedType&, dumpASTNode);
    145146
    146147
  • trunk/Source/WebCore/Modules/webgpu/WHLSL/WHLSLAutoInitializeVariables.h

    r246272 r246273  
    2828#if ENABLE(WEBGPU)
    2929
    30 #include "WHLSLResolvingType.h"
    31 #include "WHLSLTypeArgument.h"
    32 #include <functional>
    33 #include <wtf/Vector.h>
    34 
    3530namespace WebCore {
    3631
    3732namespace WHLSL {
    3833
    39 namespace AST {
     34class Program;
    4035
    41 class FunctionDeclaration;
    42 class NamedType;
    43 
    44 }
    45 
    46 AST::FunctionDeclaration* resolveFunctionOverloadImpl(Vector<std::reference_wrapper<AST::FunctionDeclaration>, 1>& possibleFunctions, Vector<std::reference_wrapper<ResolvingType>>& argumentTypes, AST::NamedType* castReturnType);
    47 AST::NamedType* resolveTypeOverloadImpl(Vector<std::reference_wrapper<AST::NamedType>, 1>&, AST::TypeArguments&);
     36void autoInitializeVariables(Program&);
    4837
    4938}
  • trunk/Source/WebCore/Modules/webgpu/WHLSL/WHLSLChecker.cpp

    r246225 r246273  
    358358        for (auto& argumentType : argumentTypes)
    359359            argumentTypeReferences.append(argumentType);
    360         auto* overload = resolveFunctionOverloadImpl(*getterFuncs, argumentTypeReferences, nullptr);
     360        auto* overload = resolveFunctionOverload(*getterFuncs, argumentTypeReferences);
    361361        if (!overload)
    362362            return false;
     
    980980    {
    981981        Vector<std::reference_wrapper<ResolvingType>> getterArgumentTypes { baseInfo->resolvingType };
    982         getterFunction = resolveFunctionOverloadImpl(dotExpression.possibleGetterOverloads(), getterArgumentTypes, nullptr);
     982        getterFunction = resolveFunctionOverload(dotExpression.possibleGetterOverloads(), getterArgumentTypes);
    983983        if (getterFunction)
    984984            getterReturnType = &getterFunction->type();
     
    990990        auto argumentType = makeUniqueRef<AST::PointerType>(Lexer::Token(dotExpression.origin()), *leftAddressSpace, baseUnnamedType->get().clone());
    991991        Vector<std::reference_wrapper<ResolvingType>> anderArgumentTypes { baseInfo->resolvingType };
    992         anderFunction = resolveFunctionOverloadImpl(dotExpression.possibleAnderOverloads(), anderArgumentTypes, nullptr);
     992        anderFunction = resolveFunctionOverload(dotExpression.possibleAnderOverloads(), anderArgumentTypes);
    993993        if (anderFunction)
    994994            anderReturnType = &downcast<AST::PointerType>(anderFunction->type()).elementType(); // FIXME: https://bugs.webkit.org/show_bug.cgi?id=198164 Enforce the return of anders will always be a pointer
     
    10001000        auto argumentType = makeUniqueRef<AST::PointerType>(Lexer::Token(dotExpression.origin()), AST::AddressSpace::Thread, baseUnnamedType->get().clone());
    10011001        Vector<std::reference_wrapper<ResolvingType>> threadAnderArgumentTypes { baseInfo->resolvingType };
    1002         threadAnderFunction = resolveFunctionOverloadImpl(dotExpression.possibleAnderOverloads(), threadAnderArgumentTypes, nullptr);
     1002        threadAnderFunction = resolveFunctionOverload(dotExpression.possibleAnderOverloads(), threadAnderArgumentTypes);
    10031003        if (threadAnderFunction)
    10041004            threadAnderReturnType = &downcast<AST::PointerType>(threadAnderFunction->type()).elementType(); // FIXME: https://bugs.webkit.org/show_bug.cgi?id=198164 Enforce the return of anders will always be a pointer
     
    10251025        ResolvingType fieldResolvingType(fieldType->clone());
    10261026        Vector<std::reference_wrapper<ResolvingType>> setterArgumentTypes { baseInfo->resolvingType, fieldResolvingType };
    1027         setterFunction = resolveFunctionOverloadImpl(dotExpression.possibleSetterOverloads(), setterArgumentTypes, nullptr);
     1027        setterFunction = resolveFunctionOverload(dotExpression.possibleSetterOverloads(), setterArgumentTypes);
    10281028        if (setterFunction)
    10291029            setterReturnType = &setterFunction->type();
     
    13981398
    13991399    ASSERT(callExpression.hasOverloads());
    1400     auto* function = resolveFunctionOverloadImpl(*callExpression.overloads(), types, callExpression.castReturnType());
     1400    auto* function = resolveFunctionOverload(*callExpression.overloads(), types, callExpression.castReturnType());
    14011401    if (!function) {
    14021402        if (auto newFunction = resolveByInstantiation(callExpression, types, m_intrinsics)) {
  • trunk/Source/WebCore/Modules/webgpu/WHLSL/WHLSLInferTypes.cpp

    r245680 r246273  
    222222}
    223223
    224 bool inferTypesForCall(AST::FunctionDeclaration& possibleFunction, Vector<std::reference_wrapper<ResolvingType>>& argumentTypes, const AST::NamedType* castReturnType)
     224template <typename TypeKind>
     225ALWAYS_INLINE bool inferTypesForCallImpl(AST::FunctionDeclaration& possibleFunction, Vector<std::reference_wrapper<ResolvingType>>& argumentTypes, const TypeKind* castReturnType)
    225226{
    226227    if (possibleFunction.parameters().size() != argumentTypes.size())
     
    240241}
    241242
     243bool inferTypesForCall(AST::FunctionDeclaration& possibleFunction, Vector<std::reference_wrapper<ResolvingType>>& argumentTypes, const AST::NamedType* castReturnType)
     244{
     245    return inferTypesForCallImpl(possibleFunction, argumentTypes, castReturnType);
     246}
     247
     248bool inferTypesForCall(AST::FunctionDeclaration& possibleFunction, Vector<std::reference_wrapper<ResolvingType>>& argumentTypes, const AST::UnnamedType* castReturnType)
     249{
     250    return inferTypesForCallImpl(possibleFunction, argumentTypes, castReturnType);
     251}
     252
    242253} // namespace WHLSL
    243254
  • trunk/Source/WebCore/Modules/webgpu/WHLSL/WHLSLInferTypes.h

    r246225 r246273  
    5555bool inferTypesForTypeArguments(AST::NamedType& possibleType, AST::TypeArguments&);
    5656bool inferTypesForCall(AST::FunctionDeclaration& possibleFunction, Vector<std::reference_wrapper<ResolvingType>>& argumentTypes, const AST::NamedType* castReturnType);
     57bool inferTypesForCall(AST::FunctionDeclaration& possibleFunction, Vector<std::reference_wrapper<ResolvingType>>& argumentTypes, const AST::UnnamedType* castReturnType);
    5758
    5859}
  • trunk/Source/WebCore/Modules/webgpu/WHLSL/WHLSLPrepare.cpp

    r246135 r246273  
    3030
    3131#include "WHLSLASTDumper.h"
     32#include "WHLSLAutoInitializeVariables.h"
    3233#include "WHLSLCheckDuplicateFunctions.h"
    3334#include "WHLSLChecker.h"
     
    5960static constexpr bool dumpASTAfterParsing = false;
    6061static constexpr bool dumpASTAtEnd = false;
    61 static constexpr bool alwaysDumpPassFailures = true;
     62static constexpr bool alwaysDumpPassFailures = false;
    6263static constexpr bool dumpPassFailure = dumpASTBeforeEachPass || dumpASTAfterParsing || dumpASTAtEnd || alwaysDumpPassFailures;
    6364
     
    131132
    132133    checkLiteralTypes(program);
     134    autoInitializeVariables(program);
    133135    resolveProperties(program);
    134136    findHighZombies(program);
  • trunk/Source/WebCore/Modules/webgpu/WHLSL/WHLSLResolveOverloadImpl.cpp

    r245680 r246273  
    5252}
    5353
    54 AST::FunctionDeclaration* resolveFunctionOverloadImpl(Vector<std::reference_wrapper<AST::FunctionDeclaration>, 1>& possibleFunctions, Vector<std::reference_wrapper<ResolvingType>>& argumentTypes, AST::NamedType* castReturnType)
     54template <typename TypeKind>
     55AST::FunctionDeclaration* resolveFunctionOverloadImpl(Vector<std::reference_wrapper<AST::FunctionDeclaration>, 1>& possibleFunctions, Vector<std::reference_wrapper<ResolvingType>>& argumentTypes, TypeKind* castReturnType)
    5556{
    5657    Vector<std::reference_wrapper<AST::FunctionDeclaration>, 1> candidates;
     
    7778}
    7879
     80AST::FunctionDeclaration* resolveFunctionOverload(Vector<std::reference_wrapper<AST::FunctionDeclaration>, 1>& possibleFunctions, Vector<std::reference_wrapper<ResolvingType>>& argumentTypes)
     81{
     82    return resolveFunctionOverloadImpl(possibleFunctions, argumentTypes, static_cast<AST::NamedType*>(nullptr));
     83}
     84
     85AST::FunctionDeclaration* resolveFunctionOverload(Vector<std::reference_wrapper<AST::FunctionDeclaration>, 1>& possibleFunctions, Vector<std::reference_wrapper<ResolvingType>>& argumentTypes, AST::NamedType* castReturnType)
     86{
     87    return resolveFunctionOverloadImpl(possibleFunctions, argumentTypes, castReturnType);
     88}
     89
     90AST::FunctionDeclaration* resolveFunctionOverload(Vector<std::reference_wrapper<AST::FunctionDeclaration>, 1>& possibleFunctions, Vector<std::reference_wrapper<ResolvingType>>& argumentTypes, AST::UnnamedType* castReturnType)
     91{
     92    return resolveFunctionOverloadImpl(possibleFunctions, argumentTypes, castReturnType);
     93}
     94
    7995AST::NamedType* resolveTypeOverloadImpl(Vector<std::reference_wrapper<AST::NamedType>, 1>& possibleTypes, AST::TypeArguments& typeArguments)
    8096{
  • trunk/Source/WebCore/Modules/webgpu/WHLSL/WHLSLResolveOverloadImpl.h

    r243091 r246273  
    4444}
    4545
    46 AST::FunctionDeclaration* resolveFunctionOverloadImpl(Vector<std::reference_wrapper<AST::FunctionDeclaration>, 1>& possibleFunctions, Vector<std::reference_wrapper<ResolvingType>>& argumentTypes, AST::NamedType* castReturnType);
     46AST::FunctionDeclaration* resolveFunctionOverload(Vector<std::reference_wrapper<AST::FunctionDeclaration>, 1>& possibleFunctions, Vector<std::reference_wrapper<ResolvingType>>& argumentTypes);
     47AST::FunctionDeclaration* resolveFunctionOverload(Vector<std::reference_wrapper<AST::FunctionDeclaration>, 1>& possibleFunctions, Vector<std::reference_wrapper<ResolvingType>>& argumentTypes, AST::NamedType* castReturnType);
     48AST::FunctionDeclaration* resolveFunctionOverload(Vector<std::reference_wrapper<AST::FunctionDeclaration>, 1>& possibleFunctions, Vector<std::reference_wrapper<ResolvingType>>& argumentTypes, AST::UnnamedType* castReturnType);
    4749AST::NamedType* resolveTypeOverloadImpl(Vector<std::reference_wrapper<AST::NamedType>, 1>&, AST::TypeArguments&);
    4850
  • trunk/Source/WebCore/Sources.txt

    r246070 r246273  
    308308Modules/webgpu/NavigatorGPU.cpp
    309309Modules/webgpu/WHLSL/WHLSLASTDumper.cpp
     310Modules/webgpu/WHLSL/WHLSLAutoInitializeVariables.cpp
    310311Modules/webgpu/WHLSL/WHLSLInferTypes.cpp
    311312Modules/webgpu/WHLSL/WHLSLLexer.cpp
  • trunk/Source/WebCore/WebCore.xcodeproj/project.pbxproj

    r246270 r246273  
    83518351                52B0D4BF1C57FD660077CE53 /* VideoFullscreenChangeObserver.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = VideoFullscreenChangeObserver.h; sourceTree = "<group>"; };
    83528352                52B0D4C11C57FF910077CE53 /* VideoFullscreenInterfaceMac.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = VideoFullscreenInterfaceMac.h; sourceTree = "<group>"; };
     8353                52B3434922A0752200E49389 /* WHLSLAutoInitializeVariables.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = WHLSLAutoInitializeVariables.h; sourceTree = "<group>"; };
     8354                52B3434B22A0752300E49389 /* WHLSLAutoInitializeVariables.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = WHLSLAutoInitializeVariables.cpp; sourceTree = "<group>"; };
    83538355                52D5A18D1C54590300DE34A3 /* VideoFullscreenLayerManagerObjC.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = VideoFullscreenLayerManagerObjC.mm; sourceTree = "<group>"; };
    83548356                52D5A18E1C54590300DE34A3 /* VideoFullscreenLayerManagerObjC.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = VideoFullscreenLayerManagerObjC.h; sourceTree = "<group>"; };
     
    2544625448                                C20F88AA22966B0E00D610FA /* WHLSLASTDumper.cpp */,
    2544725449                                C20F88AC22966B0F00D610FA /* WHLSLASTDumper.h */,
     25450                                52B3434B22A0752300E49389 /* WHLSLAutoInitializeVariables.cpp */,
     25451                                52B3434922A0752200E49389 /* WHLSLAutoInitializeVariables.h */,
    2544825452                                C234A9B221E92C1F003C984D /* WHLSLCheckDuplicateFunctions.cpp */,
    2544925453                                C234A9AE21E92C1A003C984D /* WHLSLCheckDuplicateFunctions.h */,
Note: See TracChangeset for help on using the changeset viewer.