Changeset 246875 in webkit


Ignore:
Timestamp:
Jun 27, 2019 1:19:54 AM (5 years ago)
Author:
sbarati@apple.com
Message:

[WHLSL] Implement arrays and MakeArrayReference
https://bugs.webkit.org/show_bug.cgi?id=198414

Reviewed by Myles C. Maxfield.

Source/WebCore:

This patch implements WHLSL arrays. The main implementation detail is that
arrays get compiled to use Metal's array type. To make everything work, this
patch also fixes a few bugs:

  • The checker now allows "operator.length" to be called on arrays. Prior to

this patch, it was just allowed on array references.

  • The preserve variable lifetimes pass now looks at MakeArrayReference nodes.

Prior to this patch, it just looked at MakePointerExpression.

  • We were producing the wrong type for ander arguments for indexed accesses

on array types. We were saying the argument that was produced was a reference
to an array instead of an array reference to the element type.

  • The trie we compose for the reverse type hierarchy was inserting elements

into the wrong "children" vector. We were always inserting things into the
top level vector. This is wrong when we have a nesting of types > 1.

I also found a bug with having arrays of pointers when writing this patch.
Work on this will take place in a follow up: https://bugs.webkit.org/show_bug.cgi?id=199197

Tests: webgpu/whlsl-huge-array.html

webgpu/whlsl-make-array-reference.html
webgpu/whlsl-simple-arrays.html
webgpu/whlsl-two-dimensional-array.html

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

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

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

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

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

(WebCore::WHLSL::Metal::TypeNamer::insert):
(WebCore::WHLSL::Metal::TypeNamer::emitUnnamedTypeDefinition):

  • Modules/webgpu/WHLSL/Metal/WHLSLTypeNamer.h:
  • Modules/webgpu/WHLSL/WHLSLChecker.cpp:

(WebCore::WHLSL::resolveByInstantiation):
(WebCore::WHLSL::Checker::visit):

  • Modules/webgpu/WHLSL/WHLSLPreserveVariableLifetimes.cpp:

(WebCore::WHLSL::EscapedVariableCollector::escapeVariableUse):

  • Modules/webgpu/WHLSL/WHLSLPropertyResolver.cpp:

(WebCore::WHLSL::wrapAnderCallArgument):
(WebCore::WHLSL::anderCallArgument):

  • Modules/webgpu/WHLSL/WHLSLStandardLibrary.txt:

LayoutTests:

  • webgpu/whlsl-huge-array-expected.txt: Added.
  • webgpu/whlsl-huge-array.html: Added.
  • webgpu/whlsl-make-array-reference-expected.txt: Added.
  • webgpu/whlsl-make-array-reference.html: Added.
  • webgpu/whlsl-simple-arrays-expected.txt: Added.
  • webgpu/whlsl-simple-arrays.html: Added.
  • webgpu/whlsl-two-dimensional-array-expected.txt: Added.
  • webgpu/whlsl-two-dimensional-array.html: Added.
Location:
trunk
Files:
8 added
10 edited

Legend:

Unmodified
Added
Removed
  • trunk/LayoutTests/ChangeLog

    r246869 r246875  
     12019-06-27  Saam Barati  <sbarati@apple.com>
     2
     3        [WHLSL] Implement arrays and MakeArrayReference
     4        https://bugs.webkit.org/show_bug.cgi?id=198414
     5
     6        Reviewed by Myles C. Maxfield.
     7
     8        * webgpu/whlsl-huge-array-expected.txt: Added.
     9        * webgpu/whlsl-huge-array.html: Added.
     10        * webgpu/whlsl-make-array-reference-expected.txt: Added.
     11        * webgpu/whlsl-make-array-reference.html: Added.
     12        * webgpu/whlsl-simple-arrays-expected.txt: Added.
     13        * webgpu/whlsl-simple-arrays.html: Added.
     14        * webgpu/whlsl-two-dimensional-array-expected.txt: Added.
     15        * webgpu/whlsl-two-dimensional-array.html: Added.
     16
    1172019-06-26  Simon Fraser  <simon.fraser@apple.com>
    218
  • trunk/Source/WebCore/ChangeLog

    r246874 r246875  
     12019-06-27  Saam Barati  <sbarati@apple.com>
     2
     3        [WHLSL] Implement arrays and MakeArrayReference
     4        https://bugs.webkit.org/show_bug.cgi?id=198414
     5
     6        Reviewed by Myles C. Maxfield.
     7
     8        This patch implements WHLSL arrays. The main implementation detail is that
     9        arrays get compiled to use Metal's array type. To make everything work, this
     10        patch also fixes a few bugs:
     11        - The checker now allows "operator.length" to be called on arrays. Prior to
     12        this patch, it was just allowed on array references.
     13       
     14        - The preserve variable lifetimes pass now looks at MakeArrayReference nodes.
     15        Prior to this patch, it just looked at MakePointerExpression.
     16       
     17        - We were producing the wrong type for ander arguments for indexed accesses
     18        on array types. We were saying the argument that was produced was a reference
     19        to an array instead of an array reference to the element type.
     20       
     21        - The trie we compose for the reverse type hierarchy was inserting elements
     22        into the wrong "children" vector. We were always inserting things into the
     23        top level vector. This is wrong when we have a nesting of types > 1.
     24       
     25        I also found a bug with having arrays of pointers when writing this patch.
     26        Work on this will take place in a follow up: https://bugs.webkit.org/show_bug.cgi?id=199197
     27
     28        Tests: webgpu/whlsl-huge-array.html
     29               webgpu/whlsl-make-array-reference.html
     30               webgpu/whlsl-simple-arrays.html
     31               webgpu/whlsl-two-dimensional-array.html
     32
     33        * Modules/webgpu/WHLSL/Metal/WHLSLFunctionWriter.cpp:
     34        (WebCore::WHLSL::Metal::FunctionDefinitionWriter::visit):
     35        * Modules/webgpu/WHLSL/Metal/WHLSLNativeFunctionWriter.cpp:
     36        (WebCore::WHLSL::Metal::writeNativeFunction):
     37        * Modules/webgpu/WHLSL/Metal/WHLSLTypeNamer.cpp:
     38        (WebCore::WHLSL::Metal::TypeNamer::insert):
     39        (WebCore::WHLSL::Metal::TypeNamer::emitUnnamedTypeDefinition):
     40        * Modules/webgpu/WHLSL/Metal/WHLSLTypeNamer.h:
     41        * Modules/webgpu/WHLSL/WHLSLChecker.cpp:
     42        (WebCore::WHLSL::resolveByInstantiation):
     43        (WebCore::WHLSL::Checker::visit):
     44        * Modules/webgpu/WHLSL/WHLSLPreserveVariableLifetimes.cpp:
     45        (WebCore::WHLSL::EscapedVariableCollector::escapeVariableUse):
     46        * Modules/webgpu/WHLSL/WHLSLPropertyResolver.cpp:
     47        (WebCore::WHLSL::wrapAnderCallArgument):
     48        (WebCore::WHLSL::anderCallArgument):
     49        * Modules/webgpu/WHLSL/WHLSLStandardLibrary.txt:
     50
    1512019-06-27  Fujii Hironori  <Hironori.Fujii@sony.com>
    252
  • trunk/Source/WebCore/Modules/webgpu/WHLSL/Metal/WHLSLFunctionWriter.cpp

    r246631 r246875  
    629629    // FIXME: This needs to be made to work. It probably should be using the last leftValue too.
    630630    // https://bugs.webkit.org/show_bug.cgi?id=198838
    631     auto lValue = takeLastValue();
    632     auto variableName = generateNextVariableName();
     631    auto variableName = generateNextVariableName();
     632
    633633    auto mangledTypeName = m_typeNamer.mangledNameForType(makeArrayReferenceExpression.resolvedType());
    634     if (is<AST::PointerType>(makeArrayReferenceExpression.resolvedType()))
     634    if (is<AST::PointerType>(makeArrayReferenceExpression.leftValue().resolvedType())) {
     635        auto ptrValue = takeLastValue();
     636        m_stringBuilder.append(makeString(mangledTypeName, ' ', variableName, ";\n"));
     637        m_stringBuilder.append(makeString("if (", ptrValue, ") ", variableName, " = { ", ptrValue, ", 1};\n"));
     638        m_stringBuilder.append(makeString("else ", variableName, " = { nullptr, 0 };\n"));
     639    } else if (is<AST::ArrayType>(makeArrayReferenceExpression.leftValue().resolvedType())) {
     640        auto lValue = takeLastLeftValue();
     641        auto& arrayType = downcast<AST::ArrayType>(makeArrayReferenceExpression.leftValue().resolvedType());
     642        m_stringBuilder.append(makeString(mangledTypeName, ' ', variableName, " = { ", lValue, "->data(), ", arrayType.numElements(), " };\n"));
     643    } else {
     644        auto lValue = takeLastLeftValue();
    635645        m_stringBuilder.append(makeString(mangledTypeName, ' ', variableName, " = { ", lValue, ", 1 };\n"));
    636     else if (is<AST::ArrayType>(makeArrayReferenceExpression.resolvedType())) {
    637         auto& arrayType = downcast<AST::ArrayType>(makeArrayReferenceExpression.resolvedType());
    638         m_stringBuilder.append(makeString(mangledTypeName, ' ', variableName, " = { &(", lValue, "[0]), ", arrayType.numElements(), " };\n"));
    639     } else
    640         m_stringBuilder.append(makeString(mangledTypeName, ' ', variableName, " = { &", lValue, ", 1 };\n"));
     646    }
    641647    appendRightValue(makeArrayReferenceExpression, variableName);
    642648}
  • trunk/Source/WebCore/Modules/webgpu/WHLSL/Metal/WHLSLNativeFunctionWriter.cpp

    r246643 r246875  
    175175        if (is<AST::ArrayType>(unnamedParameterType)) {
    176176            auto& arrayParameterType = downcast<AST::ArrayType>(unnamedParameterType);
    177             stringBuilder.append(makeString("uint ", outputFunctionName, '(', metalParameterName, " v) {\n"));
    178             stringBuilder.append(makeString("    return ", arrayParameterType.numElements(), "u;\n"));
     177            stringBuilder.append(makeString("uint ", outputFunctionName, '(', metalParameterName, ") {\n"));
     178            stringBuilder.append(makeString("    return ", arrayParameterType.numElements(), ";\n"));
    179179            stringBuilder.append("}\n");
    180180            return stringBuilder.toString();
     
    258258        auto metalReturnName = typeNamer.mangledNameForType(nativeFunctionDeclaration.type());
    259259        stringBuilder.append(makeString(metalReturnName, ' ', outputFunctionName, '(', metalParameter1Name, " v, ", metalParameter2Name, " n) {\n"));
     260        ASSERT(is<AST::ArrayReferenceType>(*nativeFunctionDeclaration.parameters()[0]->type()));
    260261        stringBuilder.append("    if (n < v.length) return &(v.pointer[n]);\n");
    261262        stringBuilder.append("    return nullptr;\n");
  • trunk/Source/WebCore/Modules/webgpu/WHLSL/Metal/WHLSLTypeNamer.cpp

    r246438 r246875  
    310310}
    311311
    312 size_t TypeNamer::insert(AST::UnnamedType& unnamedType, Vector<UniqueRef<BaseTypeNameNode>>& types)
     312BaseTypeNameNode* TypeNamer::insert(AST::UnnamedType& unnamedType, Vector<UniqueRef<BaseTypeNameNode>>& types)
    313313{
    314314    Vector<UniqueRef<BaseTypeNameNode>>* vectorToInsertInto { nullptr };
     
    318318        parent = nullptr;
    319319    } else if (is<AST::PointerType>(unnamedType)) {
    320         auto& item = types[insert(downcast<AST::PointerType>(unnamedType).elementType(), types)];
    321         vectorToInsertInto = &item->children();
    322         parent = &item;
     320        parent = insert(downcast<AST::PointerType>(unnamedType).elementType(), types);
     321        vectorToInsertInto = &parent->children();
    323322    } else if (is<AST::ArrayReferenceType>(unnamedType)) {
    324         auto& item = types[insert(downcast<AST::ArrayReferenceType>(unnamedType).elementType(), types)];
    325         vectorToInsertInto = &item->children();
    326         parent = &item;
     323        parent = insert(downcast<AST::ArrayReferenceType>(unnamedType).elementType(), types);
     324        vectorToInsertInto = &parent->children();
    327325    } else {
    328         auto& item = types[insert(downcast<AST::ArrayType>(unnamedType).type(), types)];
    329         vectorToInsertInto = &item->children();
    330         parent = &item;
     326        parent = insert(downcast<AST::ArrayType>(unnamedType).type(), types);
     327        vectorToInsertInto = &parent->children();
    331328    }
    332329    ASSERT(vectorToInsertInto);
     
    340337        }
    341338        vectorToInsertInto->append(WTFMove(result));
    342         return vectorToInsertInto->size() - 1;
     339        return &vectorToInsertInto->last().get();
    343340    }
    344341    auto addResult = m_unnamedTypeMapping.add(&unnamedType, &*iterator);
    345342    ASSERT_UNUSED(addResult, addResult.isNewEntry);
    346     return iterator - vectorToInsertInto->begin();
     343    return &*iterator;
    347344}
    348345
     
    399396        auto& arrayType = downcast<ArrayTypeNameNode>(baseTypeNameNode);
    400397        ASSERT(baseTypeNameNode.parent());
    401         stringBuilder.append(makeString("typedef Array<", arrayType.parent()->mangledName(), ", ", arrayType.numElements(), "> ", arrayType.mangledName(), ";\n"));
     398        stringBuilder.append(makeString("typedef array<", arrayType.parent()->mangledName(), ", ", arrayType.numElements(), "> ", arrayType.mangledName(), ";\n"));
    402399    }
    403400    emittedUnnamedTypes.add(&baseTypeNameNode);
  • trunk/Source/WebCore/Modules/webgpu/WHLSL/Metal/WHLSLTypeNamer.h

    r243091 r246875  
    101101
    102102    UniqueRef<BaseTypeNameNode> createNameNode(AST::UnnamedType&, BaseTypeNameNode* parent);
    103     size_t insert(AST::UnnamedType&, Vector<UniqueRef<BaseTypeNameNode>>&);
     103    BaseTypeNameNode* insert(AST::UnnamedType&, Vector<UniqueRef<BaseTypeNameNode>>&);
    104104
    105105    Program& m_program;
  • trunk/Source/WebCore/Modules/webgpu/WHLSL/WHLSLChecker.cpp

    r246631 r246875  
    184184    } else if (name == "operator.length" && types.size() == 1) {
    185185        auto* firstArgumentReference = types[0].get().visit(WTF::makeVisitor([](UniqueRef<AST::UnnamedType>& unnamedType) -> AST::UnnamedType* {
    186             if (is<AST::ArrayReferenceType>(static_cast<AST::UnnamedType&>(unnamedType)))
     186            if (is<AST::ArrayReferenceType>(static_cast<AST::UnnamedType&>(unnamedType)) || is<AST::ArrayType>(static_cast<AST::UnnamedType&>(unnamedType)))
    187187                return &unnamedType;
    188188            return nullptr;
     
    11331133    ASSERT(variableReference.variable()->type());
    11341134   
    1135     AST::TypeAnnotation typeAnnotation = AST::RightValue();
    1136     typeAnnotation = AST::LeftValue { AST::AddressSpace::Thread };
    1137     assignType(variableReference, variableReference.variable()->type()->clone(), WTFMove(typeAnnotation));
     1135    assignType(variableReference, variableReference.variable()->type()->clone(), AST::LeftValue { AST::AddressSpace::Thread });
    11381136}
    11391137
  • trunk/Source/WebCore/Modules/webgpu/WHLSL/WHLSLPreserveVariableLifetimes.cpp

    r246438 r246875  
    5656    using Base = Visitor;
    5757public:
    58     void visit(AST::MakePointerExpression& makePointerExpression) override
    59     {
    60         if (!is<AST::VariableReference>(makePointerExpression.leftValue())) {
     58
     59    void escapeVariableUse(AST::Expression& expression)
     60    {
     61        if (!is<AST::VariableReference>(expression)) {
    6162            // FIXME: Are we missing any interesting productions here?
    6263            // https://bugs.webkit.org/show_bug.cgi?id=198311
    63             Base::visit(makePointerExpression.leftValue());
     64            Base::visit(expression);
    6465            return;
    6566        }
    6667
    67         auto& variableReference = downcast<AST::VariableReference>(makePointerExpression.leftValue());
    68         auto* variable = variableReference.variable();
     68        auto* variable = downcast<AST::VariableReference>(expression).variable();
    6969        ASSERT(variable);
    7070        // FIXME: We could skip this if we mark all internal variables with a bit, since we
     
    7272        // https://bugs.webkit.org/show_bug.cgi?id=198383
    7373        m_escapedVariables.add(variable, makeString("_", variable->name(), "_", m_count++));
     74    }
     75
     76    void visit(AST::MakePointerExpression& makePointerExpression) override
     77    {
     78        escapeVariableUse(makePointerExpression.leftValue());
     79    }
     80
     81    void visit(AST::MakeArrayReferenceExpression& makeArrayReferenceExpression) override
     82    {
     83        escapeVariableUse(makeArrayReferenceExpression.leftValue());
    7484    }
    7585
  • trunk/Source/WebCore/Modules/webgpu/WHLSL/WHLSLPropertyResolver.cpp

    r246649 r246875  
    9696
    9797template <typename ExpressionConstructor, typename TypeConstructor>
    98 static Optional<AnderCallArgumentResult> wrapAnderCallArgument(UniqueRef<AST::Expression>& expression, bool anderFunction, bool threadAnderFunction)
     98static Optional<AnderCallArgumentResult> wrapAnderCallArgument(UniqueRef<AST::Expression>& expression, UniqueRef<AST::UnnamedType> baseType, bool anderFunction, bool threadAnderFunction)
    9999{
    100100    if (auto addressSpace = expression->typeAnnotation().leftAddressSpace()) {
     
    102102            return WTF::nullopt;
    103103        auto origin = expression->origin();
    104         auto baseType = expression->resolvedType().clone();
    105104        auto makeArrayReference = makeUniqueRef<ExpressionConstructor>(Lexer::Token(origin), WTFMove(expression));
    106105        makeArrayReference->setType(makeUniqueRef<TypeConstructor>(WTFMove(origin), *addressSpace, WTFMove(baseType)));
     
    110109    if (threadAnderFunction) {
    111110        auto origin = expression->origin();
    112         auto baseType = expression->resolvedType().clone();
    113111        auto variableDeclaration = makeUniqueRef<AST::VariableDeclaration>(Lexer::Token(origin), AST::Qualifiers(), baseType->clone(), String(), WTF::nullopt, WTF::nullopt);
    114112
     
    152150            return {{ WTFMove(expression), WTF::nullopt, WhichAnder::Ander }};
    153151        if (is<AST::ArrayType>(unnamedType))
    154             return wrapAnderCallArgument<AST::MakeArrayReferenceExpression, AST::ArrayReferenceType>(expression, anderFunction, threadAnderFunction);
    155     }
    156     return wrapAnderCallArgument<AST::MakePointerExpression, AST::PointerType>(expression, anderFunction, threadAnderFunction);
     152            return wrapAnderCallArgument<AST::MakeArrayReferenceExpression, AST::ArrayReferenceType>(expression, downcast<AST::ArrayType>(unnamedType).type().clone(), anderFunction, threadAnderFunction);
     153    }
     154    return wrapAnderCallArgument<AST::MakePointerExpression, AST::PointerType>(expression, expression->resolvedType().clone(), anderFunction, threadAnderFunction);
    157155}
    158156
  • trunk/Source/WebCore/Modules/webgpu/WHLSL/WHLSLStandardLibrary.txt

    r246681 r246875  
    791791}
    792792int operator--(int value) {
     793    return value - 1;
     794}
     795uint operator++(uint value) {
     796    return value + 1;
     797}
     798uint operator--(uint value) {
    793799    return value - 1;
    794800}
Note: See TracChangeset for help on using the changeset viewer.