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

Changeset 249109 in webkit


Ignore:
Timestamp:
Aug 26, 2019, 11:58:00 AM (7 years ago)
Author:
weinig@apple.com
Message:

[WHLSL] TypeNamer can be simplified by replacing BaseTypeNameNode with uniqued AST::UnnamedTypes
https://bugs.webkit.org/show_bug.cgi?id=200632

Reviewed by Saam Barati.

There is no longer a reason to keep a parallel tree of the UnnamedType-like objects
BaseTypeNameNodes. Instead, we can store a single HashMap mapping from UnnamedTypeKeys
to MangledTypeName, and use the the UnnamedType stored in the UnnamedTypeKey while
emitting the metal code. This removes the parallel BaseTypeNameNode type hierarchy
and removes an extra allocation for each UnnamedType.

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

Define HashTraits and DefaultHash specializations for UnnamedTypeKey to simplify
uses of UnnamedTypeKey as a key in HashMap/HashSet.

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

(WebCore::WHLSL::Metal::TypeNamer::insert): Deleted.
(WebCore::WHLSL::Metal::TypeNamer::generateUniquedTypeName):
Replace old insert function with generateUniquedTypeName, which uniques and generates
names for the UnnamedType and any 'parent' UnnamedTypes.

(WebCore::WHLSL::Metal::BaseTypeNameNode): Deleted.
Remove BaseTypeNameNode and subclasses.

(WebCore::WHLSL::Metal::TypeNamer::find): Deleted.
(WebCore::WHLSL::Metal::TypeNamer::createNameNode): Deleted.
We no longer need the find or createNameNode functions, as the UnnamedTypes can be now be
used directly everywhere.

(WebCore::WHLSL::Metal::TypeNamer::emitUnnamedTypeDefinition):
Switch to directly using the UnnamedType and always have the caller pass in the mangled
name, since in the main emit loop, we always have access to the them. Also, inline the
the recursive calls to emitNamedTypeDefinition for 'parent' types to avoid unnecessary
extra switch over the kind getting the parent, and avoid it entirely for TypeReference
which never has a parent.

(WebCore::WHLSL::Metal::TypeNamer::emitNamedTypeDefinition):
Switches to now passing in the neighbors, since they are always available in the main
emit loop. Also move to a switch statement rather than ifs for consistency.

(WebCore::WHLSL::Metal::TypeNamer::emitMetalTypeDefinitions):
Pass keys and values into the emit functions to avoid double lookups.

(WebCore::WHLSL::Metal::TypeNamer::mangledNameForType):
Update to hash lookup.

  • Modules/webgpu/WHLSL/WHLSLSynthesizeConstructors.cpp:

Take advantage of default HashTraits and DefaultHash for UnnamedTypeKey.

Location:
trunk/Source/WebCore
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • trunk/Source/WebCore/ChangeLog

    r249106 r249109  
     12019-08-26  Sam Weinig  <weinig@apple.com>
     2
     3        [WHLSL] TypeNamer can be simplified by replacing BaseTypeNameNode with uniqued AST::UnnamedTypes
     4        https://bugs.webkit.org/show_bug.cgi?id=200632
     5
     6        Reviewed by Saam Barati.
     7
     8        There is no longer a reason to keep a parallel tree of the UnnamedType-like objects
     9        BaseTypeNameNodes. Instead, we can store a single HashMap mapping from UnnamedTypeKeys
     10        to MangledTypeName, and use the the UnnamedType stored in the UnnamedTypeKey while
     11        emitting the metal code. This removes the parallel BaseTypeNameNode type hierarchy
     12        and removes an extra allocation for each UnnamedType.
     13
     14        * Modules/webgpu/WHLSL/AST/WHLSLUnnamedTypeHash.h:
     15        Define HashTraits and DefaultHash specializations for UnnamedTypeKey to simplify
     16        uses of UnnamedTypeKey as a key in HashMap/HashSet.
     17
     18        * Modules/webgpu/WHLSL/Metal/WHLSLTypeNamer.h:
     19        * Modules/webgpu/WHLSL/Metal/WHLSLTypeNamer.cpp:
     20        (WebCore::WHLSL::Metal::TypeNamer::insert): Deleted.
     21        (WebCore::WHLSL::Metal::TypeNamer::generateUniquedTypeName):
     22        Replace old insert function with generateUniquedTypeName, which uniques and generates
     23        names for the UnnamedType and any 'parent' UnnamedTypes.
     24
     25        (WebCore::WHLSL::Metal::BaseTypeNameNode): Deleted.
     26        Remove BaseTypeNameNode and subclasses.
     27
     28        (WebCore::WHLSL::Metal::TypeNamer::find): Deleted.
     29        (WebCore::WHLSL::Metal::TypeNamer::createNameNode): Deleted.
     30        We no longer need the find or createNameNode functions, as the UnnamedTypes can be now be
     31        used directly everywhere.
     32
     33        (WebCore::WHLSL::Metal::TypeNamer::emitUnnamedTypeDefinition):
     34        Switch to directly using the UnnamedType and always have the caller pass in the mangled
     35        name, since in the main emit loop, we always have access to the them. Also, inline the
     36        the recursive calls to emitNamedTypeDefinition for 'parent' types to avoid unnecessary
     37        extra switch over the kind getting the parent, and avoid it entirely for TypeReference
     38        which never has a parent.
     39       
     40        (WebCore::WHLSL::Metal::TypeNamer::emitNamedTypeDefinition):
     41        Switches to now passing in the neighbors, since they are always available in the main
     42        emit loop. Also move to a switch statement rather than ifs for consistency.
     43       
     44        (WebCore::WHLSL::Metal::TypeNamer::emitMetalTypeDefinitions):
     45        Pass keys and values into the emit functions to avoid double lookups.
     46
     47        (WebCore::WHLSL::Metal::TypeNamer::mangledNameForType):
     48        Update to hash lookup.
     49
     50        * Modules/webgpu/WHLSL/WHLSLSynthesizeConstructors.cpp:
     51        Take advantage of default HashTraits and DefaultHash for UnnamedTypeKey.
     52
    1532019-08-26  Peng Liu  <peng.liu6@apple.com>
    254
  • trunk/Source/WebCore/Modules/webgpu/WHLSL/AST/WHLSLUnnamedTypeHash.h

    r248395 r249109  
    7575}
    7676
     77namespace WTF {
     78
     79template<> struct HashTraits<WebCore::WHLSL::UnnamedTypeKey> : WebCore::WHLSL::UnnamedTypeKey::Traits { };
     80template<> struct DefaultHash<WebCore::WHLSL::UnnamedTypeKey> {
     81    typedef WebCore::WHLSL::UnnamedTypeKey::Hash Hash;
     82};
     83
     84} // namespace WTF
     85
    7786#endif
  • trunk/Source/WebCore/Modules/webgpu/WHLSL/Metal/WHLSLTypeNamer.cpp

    r248892 r249109  
    5959namespace Metal {
    6060
    61 // FIXME: Look into replacing BaseTypeNameNode with a simple struct { RefPtr<UnnamedType> parent; MangledTypeName; } that UnnamedTypeKeys map to.
    62 class BaseTypeNameNode {
    63     WTF_MAKE_FAST_ALLOCATED;
    64 public:
    65     BaseTypeNameNode(BaseTypeNameNode* parent, MangledTypeName&& mangledName, AST::UnnamedType::Kind kind)
    66         : m_parent(parent)
    67         , m_mangledName(mangledName)
    68         , m_kind(kind)
    69     {
    70     }
    71     virtual ~BaseTypeNameNode() = default;
    72    
    73     AST::UnnamedType::Kind kind() { return m_kind; }
    74     bool isReferenceTypeNameNode() const { return m_kind == AST::UnnamedType::Kind::TypeReference; }
    75     bool isPointerTypeNameNode() const { return m_kind == AST::UnnamedType::Kind::Pointer; }
    76     bool isArrayReferenceTypeNameNode() const { return m_kind == AST::UnnamedType::Kind::ArrayReference; }
    77     bool isArrayTypeNameNode() const { return m_kind == AST::UnnamedType::Kind::Array; }
    78 
    79     BaseTypeNameNode* parent() { return m_parent; }
    80     MangledTypeName mangledName() const { return m_mangledName; }
    81 
    82 private:
    83     BaseTypeNameNode* m_parent;
    84     MangledTypeName m_mangledName;
    85     AST::UnnamedType::Kind m_kind;
    86 };
    87 
    88 class ArrayTypeNameNode final : public BaseTypeNameNode {
    89     WTF_MAKE_FAST_ALLOCATED;
    90 public:
    91     ArrayTypeNameNode(BaseTypeNameNode* parent, MangledTypeName&& mangledName, unsigned numElements)
    92         : BaseTypeNameNode(parent, WTFMove(mangledName), AST::UnnamedType::Kind::Array)
    93         , m_numElements(numElements)
    94     {
    95     }
    96     virtual ~ArrayTypeNameNode() = default;
    97     unsigned numElements() const { return m_numElements; }
    98 
    99 private:
    100     unsigned m_numElements;
    101 };
    102 
    103 class ArrayReferenceTypeNameNode final : public BaseTypeNameNode {
    104     WTF_MAKE_FAST_ALLOCATED;
    105 public:
    106     ArrayReferenceTypeNameNode(BaseTypeNameNode* parent, MangledTypeName&& mangledName, AST::AddressSpace addressSpace)
    107         : BaseTypeNameNode(parent, WTFMove(mangledName), AST::UnnamedType::Kind::ArrayReference)
    108         , m_addressSpace(addressSpace)
    109     {
    110     }
    111     virtual ~ArrayReferenceTypeNameNode() = default;
    112     AST::AddressSpace addressSpace() const { return m_addressSpace; }
    113 
    114 private:
    115     AST::AddressSpace m_addressSpace;
    116 };
    117 
    118 class PointerTypeNameNode final : public BaseTypeNameNode {
    119     WTF_MAKE_FAST_ALLOCATED;
    120 public:
    121     PointerTypeNameNode(BaseTypeNameNode* parent, MangledTypeName&& mangledName, AST::AddressSpace addressSpace)
    122         : BaseTypeNameNode(parent, WTFMove(mangledName), AST::UnnamedType::Kind::Pointer)
    123         , m_addressSpace(addressSpace)
    124     {
    125     }
    126     virtual ~PointerTypeNameNode() = default;
    127     AST::AddressSpace addressSpace() const { return m_addressSpace; }
    128 
    129 private:
    130     AST::AddressSpace m_addressSpace;
    131 };
    132 
    133 class ReferenceTypeNameNode final : public BaseTypeNameNode {
    134     WTF_MAKE_FAST_ALLOCATED;
    135 public:
    136     ReferenceTypeNameNode(BaseTypeNameNode* parent, MangledTypeName&& mangledName, AST::NamedType& namedType)
    137         : BaseTypeNameNode(parent, WTFMove(mangledName), AST::UnnamedType::Kind::TypeReference)
    138         , m_namedType(namedType)
    139     {
    140     }
    141     virtual ~ReferenceTypeNameNode() = default;
    142     AST::NamedType& namedType() { return m_namedType; }
    143 
    144 private:
    145     AST::NamedType& m_namedType;
    146 };
    147 
    148 }
    149 
    150 }
    151 
    152 }
    153 
    154 #define SPECIALIZE_TYPE_TRAITS_WHLSL_BASE_TYPE_NAMED_NODE(ToValueTypeName, predicate) \
    155 SPECIALIZE_TYPE_TRAITS_BEGIN(WebCore::WHLSL::Metal::ToValueTypeName) \
    156     static bool isType(const WebCore::WHLSL::Metal::BaseTypeNameNode& type) { return type.predicate; } \
    157 SPECIALIZE_TYPE_TRAITS_END()
    158 
    159 SPECIALIZE_TYPE_TRAITS_WHLSL_BASE_TYPE_NAMED_NODE(ArrayTypeNameNode, isArrayTypeNameNode())
    160 
    161 SPECIALIZE_TYPE_TRAITS_WHLSL_BASE_TYPE_NAMED_NODE(ArrayReferenceTypeNameNode, isArrayReferenceTypeNameNode())
    162 
    163 SPECIALIZE_TYPE_TRAITS_WHLSL_BASE_TYPE_NAMED_NODE(PointerTypeNameNode, isPointerTypeNameNode())
    164 
    165 SPECIALIZE_TYPE_TRAITS_WHLSL_BASE_TYPE_NAMED_NODE(ReferenceTypeNameNode, isReferenceTypeNameNode())
    166 
    167 namespace WebCore {
    168 
    169 namespace WHLSL {
    170 
    171 namespace Metal {
    172 
    17361TypeNamer::TypeNamer(Program& program)
    17462    : m_program(program)
     
    18068void TypeNamer::visit(AST::UnnamedType& unnamedType)
    18169{
    182     insert(unnamedType);
     70    generateUniquedTypeName(unnamedType);
    18371}
    18472
     
    19886
    19987    {
    200         Vector<std::reference_wrapper<BaseTypeNameNode>> neighbors = { find(enumerationDefinition.type()) };
    201         auto addResult = m_dependencyGraph.add(&enumerationDefinition, WTFMove(neighbors));
     88        auto addResult = m_dependencyGraph.add(&enumerationDefinition, Vector<std::reference_wrapper<AST::UnnamedType>> { enumerationDefinition.type() });
    20289        ASSERT_UNUSED(addResult, addResult.isNewEntry);
    20390    }
     
    20794{
    20895    // Native type declarations already have names, and are already declared in Metal.
    209     auto addResult = m_dependencyGraph.add(&nativeTypeDeclaration, Vector<std::reference_wrapper<BaseTypeNameNode>>());
     96    auto addResult = m_dependencyGraph.add(&nativeTypeDeclaration, Vector<std::reference_wrapper<AST::UnnamedType>> { });
    21097    ASSERT_UNUSED(addResult, addResult.isNewEntry);
    21198}
     
    219106    Visitor::visit(structureDefinition);
    220107    {
    221         Vector<std::reference_wrapper<BaseTypeNameNode>> neighbors;
     108        Vector<std::reference_wrapper<AST::UnnamedType>> neighbors;
    222109        for (auto& structureElement : structureDefinition.structureElements()) {
    223110            auto addResult = m_structureElementMapping.add(&structureElement, generateNextStructureElementName());
    224111            ASSERT_UNUSED(addResult, addResult.isNewEntry);
    225             neighbors.append(find(structureElement.type()));
     112            neighbors.append(structureElement.type());
    226113        }
    227114        auto addResult = m_dependencyGraph.add(&structureDefinition, WTFMove(neighbors));
     
    238125    Visitor::visit(typeDefinition);
    239126    {
    240         Vector<std::reference_wrapper<BaseTypeNameNode>> neighbors = { find(typeDefinition.type()) };
    241         auto addResult = m_dependencyGraph.add(&typeDefinition, WTFMove(neighbors));
     127        auto addResult = m_dependencyGraph.add(&typeDefinition, Vector<std::reference_wrapper<AST::UnnamedType>> { typeDefinition.type() });
    242128        ASSERT_UNUSED(addResult, addResult.isNewEntry);
    243129    }
     
    246132void TypeNamer::visit(AST::Expression& expression)
    247133{
    248     insert(expression.resolvedType());
     134    generateUniquedTypeName(expression.resolvedType());
    249135    Visitor::visit(expression);
    250136}
     
    259145{
    260146    return writeNativeType(nativeTypeDeclaration);
    261 }
    262 
    263 BaseTypeNameNode& TypeNamer::find(AST::UnnamedType& unnamedType)
    264 {
    265     auto iterator = m_unnamedTypesUniquingMap.find(unnamedType);
    266     ASSERT(iterator != m_unnamedTypesUniquingMap.end());
    267     return *iterator->value;
    268 }
    269 
    270 std::unique_ptr<BaseTypeNameNode> TypeNamer::createNameNode(AST::UnnamedType& unnamedType, BaseTypeNameNode* parent)
    271 {
    272     switch (unnamedType.kind()) {
    273     case AST::UnnamedType::Kind::TypeReference: {
    274         auto& typeReference = downcast<AST::TypeReference>(unnamedType);
    275         return makeUnique<ReferenceTypeNameNode>(parent, generateNextTypeName(), typeReference.resolvedType());
    276     }
    277     case AST::UnnamedType::Kind::Pointer: {
    278         auto& pointerType = downcast<AST::PointerType>(unnamedType);
    279         return makeUnique<PointerTypeNameNode>(parent, generateNextTypeName(), pointerType.addressSpace());
    280     }
    281     case AST::UnnamedType::Kind::ArrayReference: {
    282         auto& arrayReferenceType = downcast<AST::ArrayReferenceType>(unnamedType);
    283         return makeUnique<ArrayReferenceTypeNameNode>(parent, generateNextTypeName(), arrayReferenceType.addressSpace());
    284     }
    285     case AST::UnnamedType::Kind::Array: {
    286         auto& arrayType = downcast<AST::ArrayType>(unnamedType);
    287         return makeUnique<ArrayTypeNameNode>(parent, generateNextTypeName(), arrayType.numElements());
    288     }
    289     default:
    290         RELEASE_ASSERT_NOT_REACHED();
    291     }
    292147}
    293148
     
    308163}
    309164
    310 BaseTypeNameNode* TypeNamer::insert(AST::UnnamedType& unnamedType)
    311 {
    312     if (auto* result = m_unnamedTypeMapping.get(&unnamedType))
    313         return result;
    314 
     165void TypeNamer::generateUniquedTypeName(AST::UnnamedType& unnamedType)
     166{
    315167    auto* parentUnnamedType = parent(unnamedType);
    316     BaseTypeNameNode* parentNode = parentUnnamedType ? insert(*parentUnnamedType) : nullptr;
    317 
    318     auto addResult = m_unnamedTypesUniquingMap.ensure(UnnamedTypeKey { unnamedType }, [&] {
    319         return createNameNode(unnamedType, parentNode);
     168    if (parentUnnamedType)
     169        generateUniquedTypeName(*parentUnnamedType);
     170
     171    m_unnamedTypeMapping.ensure(UnnamedTypeKey { unnamedType }, [&] {
     172        return generateNextTypeName();
    320173    });
    321 
    322     m_unnamedTypeMapping.add(&unnamedType, addResult.iterator->value.get());
    323     return addResult.iterator->value.get();
    324174}
    325175
     
    351201}
    352202
    353 void TypeNamer::emitUnnamedTypeDefinition(StringBuilder& stringBuilder, BaseTypeNameNode& baseTypeNameNode, HashSet<AST::NamedType*>& emittedNamedTypes, HashSet<BaseTypeNameNode*>& emittedUnnamedTypes)
    354 {
    355     if (emittedUnnamedTypes.contains(&baseTypeNameNode))
     203void TypeNamer::emitUnnamedTypeDefinition(StringBuilder& stringBuilder, AST::UnnamedType& unnamedType, MangledTypeName mangledName, HashSet<AST::NamedType*>& emittedNamedTypes, HashSet<UnnamedTypeKey>& emittedUnnamedTypes)
     204{
     205    if (emittedUnnamedTypes.contains(UnnamedTypeKey { unnamedType }))
    356206        return;
    357207
    358     if (baseTypeNameNode.parent())
    359         emitUnnamedTypeDefinition(stringBuilder, *baseTypeNameNode.parent(), emittedNamedTypes, emittedUnnamedTypes);
    360    
    361     switch (baseTypeNameNode.kind()) {
     208    switch (unnamedType.kind()) {
    362209    case AST::UnnamedType::Kind::TypeReference: {
    363         auto& namedType = downcast<ReferenceTypeNameNode>(baseTypeNameNode).namedType();
    364         emitNamedTypeDefinition(stringBuilder, namedType, emittedNamedTypes, emittedUnnamedTypes);
    365         stringBuilder.append("typedef ", mangledNameForType(namedType), ' ', baseTypeNameNode.mangledName(), ";\n");
     210        auto& typeReference = downcast<AST::TypeReference>(unnamedType);
     211
     212        auto& parent = typeReference.resolvedType();
     213        auto parentMangledName = mangledNameForType(typeReference.resolvedType());
     214        auto iterator = m_dependencyGraph.find(&parent);
     215        ASSERT(iterator != m_dependencyGraph.end());
     216        emitNamedTypeDefinition(stringBuilder, parent, iterator->value, emittedNamedTypes, emittedUnnamedTypes);
     217
     218        stringBuilder.append("typedef ", parentMangledName, ' ', mangledName, ";\n");
    366219        break;
    367220    }
    368221    case AST::UnnamedType::Kind::Pointer: {
    369         auto& pointerType = downcast<PointerTypeNameNode>(baseTypeNameNode);
    370         ASSERT(baseTypeNameNode.parent());
    371         stringBuilder.append("typedef ", toString(pointerType.addressSpace()), ' ', pointerType.parent()->mangledName(), "* ", pointerType.mangledName(), ";\n");
     222        auto& pointerType = downcast<AST::PointerType>(unnamedType);
     223
     224        auto& parent = pointerType.elementType();
     225        auto parentMangledName = mangledNameForType(parent);
     226        emitUnnamedTypeDefinition(stringBuilder, parent, parentMangledName, emittedNamedTypes, emittedUnnamedTypes);
     227
     228        stringBuilder.append("typedef ", toString(pointerType.addressSpace()), ' ', parentMangledName, "* ", mangledName, ";\n");
    372229        break;
    373230    }
    374231    case AST::UnnamedType::Kind::ArrayReference: {
    375         auto& arrayReferenceType = downcast<ArrayReferenceTypeNameNode>(baseTypeNameNode);
    376         ASSERT(baseTypeNameNode.parent());
     232        auto& arrayReferenceType = downcast<AST::ArrayReferenceType>(unnamedType);
     233
     234        auto& parent = arrayReferenceType.elementType();
     235        auto parentMangledName = mangledNameForType(parent);
     236        emitUnnamedTypeDefinition(stringBuilder, parent, parentMangledName, emittedNamedTypes, emittedUnnamedTypes);
     237
    377238        stringBuilder.append(
    378             "struct ", arrayReferenceType.mangledName(), " {\n"
    379             "    ", toString(arrayReferenceType.addressSpace()), ' ', arrayReferenceType.parent()->mangledName(), "* pointer;\n"
     239            "struct ", mangledName, " {\n"
     240            "    ", toString(arrayReferenceType.addressSpace()), ' ', parentMangledName, "* pointer;\n"
    380241            "    uint32_t length;\n"
    381242            "};\n"
     
    384245    }
    385246    case AST::UnnamedType::Kind::Array: {
    386         auto& arrayType = downcast<ArrayTypeNameNode>(baseTypeNameNode);
    387         ASSERT(baseTypeNameNode.parent());
    388         stringBuilder.append("typedef array<", arrayType.parent()->mangledName(), ", ", arrayType.numElements(), "> ", arrayType.mangledName(), ";\n");
     247        auto& arrayType = downcast<AST::ArrayType>(unnamedType);
     248
     249        auto& parent = arrayType.type();
     250        auto parentMangledName = mangledNameForType(parent);
     251        emitUnnamedTypeDefinition(stringBuilder, parent, parentMangledName, emittedNamedTypes, emittedUnnamedTypes);
     252
     253        stringBuilder.append("typedef array<", parentMangledName, ", ", arrayType.numElements(), "> ", mangledName, ";\n");
    389254        break;
    390255    }
     
    393258    }
    394259
    395     emittedUnnamedTypes.add(&baseTypeNameNode);
    396 }
    397 
    398 void TypeNamer::emitNamedTypeDefinition(StringBuilder& stringBuilder, AST::NamedType& namedType, HashSet<AST::NamedType*>& emittedNamedTypes, HashSet<BaseTypeNameNode*>& emittedUnnamedTypes)
     260    emittedUnnamedTypes.add(UnnamedTypeKey { unnamedType });
     261}
     262
     263void TypeNamer::emitNamedTypeDefinition(StringBuilder& stringBuilder, AST::NamedType& namedType, Vector<std::reference_wrapper<AST::UnnamedType>>& neighbors, HashSet<AST::NamedType*>& emittedNamedTypes, HashSet<UnnamedTypeKey>& emittedUnnamedTypes)
    399264{
    400265    if (emittedNamedTypes.contains(&namedType))
    401266        return;
    402     auto iterator = m_dependencyGraph.find(&namedType);
    403     ASSERT(iterator != m_dependencyGraph.end());
    404     for (auto& baseTypeNameNode : iterator->value)
    405         emitUnnamedTypeDefinition(stringBuilder, baseTypeNameNode, emittedNamedTypes, emittedUnnamedTypes);
    406     if (is<AST::EnumerationDefinition>(namedType)) {
     267
     268    for (auto& unnameType : neighbors)
     269        emitUnnamedTypeDefinition(stringBuilder, unnameType, mangledNameForType(unnameType), emittedNamedTypes, emittedUnnamedTypes);
     270
     271    switch (namedType.kind()) {
     272    case AST::NamedType::Kind::EnumerationDefinition: {
    407273        auto& enumerationDefinition = downcast<AST::EnumerationDefinition>(namedType);
    408274        auto& baseType = enumerationDefinition.type().unifyNode();
     275
    409276        stringBuilder.append("enum class ", mangledNameForType(enumerationDefinition), " : ", mangledNameForType(downcast<AST::NamedType>(baseType)), " {\n");
    410277        for (auto& enumerationMember : enumerationDefinition.enumerationMembers())
    411278            stringBuilder.append("    ", mangledNameForEnumerationMember(enumerationMember), " = ", enumerationMember.get().value(), ",\n");
    412279        stringBuilder.append("};\n");
    413     } else if (is<AST::NativeTypeDeclaration>(namedType)) {
     280        break;
     281    }
     282    case AST::NamedType::Kind::NativeTypeDeclaration: {
    414283        // Native types already have definitions. There's nothing to do.
    415     } else if (is<AST::StructureDefinition>(namedType)) {
     284        break;
     285    }
     286    case AST::NamedType::Kind::StructureDefinition: {
    416287        auto& structureDefinition = downcast<AST::StructureDefinition>(namedType);
     288
    417289        stringBuilder.append("struct ", mangledNameForType(structureDefinition), " {\n");
    418290        for (auto& structureElement : structureDefinition.structureElements())
    419291            stringBuilder.append("    ", mangledNameForType(structureElement.type()), ' ', mangledNameForStructureElement(structureElement), ";\n");
    420292        stringBuilder.append("};\n");
    421     } else {
     293        break;
     294    }
     295    case AST::NamedType::Kind::TypeDefinition: {
    422296        auto& typeDefinition = downcast<AST::TypeDefinition>(namedType);
     297
    423298        stringBuilder.append("typedef ", mangledNameForType(typeDefinition.type()), ' ', mangledNameForType(typeDefinition), ";\n");
    424     }
     299        break;
     300    }
     301    default:
     302        RELEASE_ASSERT_NOT_REACHED();
     303    }
     304
    425305    emittedNamedTypes.add(&namedType);
    426306}
     
    429309{
    430310    HashSet<AST::NamedType*> emittedNamedTypes;
    431     HashSet<BaseTypeNameNode*> emittedUnnamedTypes;
    432     for (auto& namedType : m_dependencyGraph.keys())
    433         emitNamedTypeDefinition(stringBuilder, *namedType, emittedNamedTypes, emittedUnnamedTypes);
    434     for (auto& node : m_unnamedTypesUniquingMap.values())
    435         emitUnnamedTypeDefinition(stringBuilder, *node, emittedNamedTypes, emittedUnnamedTypes);
     311    HashSet<UnnamedTypeKey> emittedUnnamedTypes;
     312    for (auto& [namedType, neighbors] : m_dependencyGraph)
     313        emitNamedTypeDefinition(stringBuilder, *namedType, neighbors, emittedNamedTypes, emittedUnnamedTypes);
     314    for (auto& [unnamedTypeKey, mangledName] : m_unnamedTypeMapping)
     315        emitUnnamedTypeDefinition(stringBuilder, unnamedTypeKey.unnamedType(), mangledName, emittedNamedTypes, emittedUnnamedTypes);
    436316}
    437317
    438318MangledTypeName TypeNamer::mangledNameForType(AST::UnnamedType& unnamedType)
    439319{
    440     return find(unnamedType).mangledName();
     320    auto iterator = m_unnamedTypeMapping.find(UnnamedTypeKey { unnamedType });
     321    ASSERT(iterator != m_unnamedTypeMapping.end());
     322    return iterator->value;
    441323}
    442324
     
    450332}
    451333
    452 
    453334MangledEnumerationMemberName TypeNamer::mangledNameForEnumerationMember(AST::EnumerationMember& enumerationMember)
    454335{
  • trunk/Source/WebCore/Modules/webgpu/WHLSL/Metal/WHLSLTypeNamer.h

    r248395 r249109  
    6363    void emitMetalTypes(StringBuilder&);
    6464
    65     // Must be called after calling metalTypes().
     65    // Must be called after calling emitMetalTypes().
    6666    String mangledNameForType(AST::NativeTypeDeclaration&);
    6767    MangledTypeName mangledNameForType(AST::UnnamedType&);
     
    8484    MangledEnumerationMemberName generateNextEnumerationMemberName() { return { m_enumerationMemberCount++ }; }
    8585
    86     void emitNamedTypeDefinition(StringBuilder&, AST::NamedType&, HashSet<AST::NamedType*>& emittedNamedTypes, HashSet<BaseTypeNameNode*>& emittedUnnamedTypes);
    87     void emitUnnamedTypeDefinition(StringBuilder&, BaseTypeNameNode&, HashSet<AST::NamedType*>& emittedNamedTypes, HashSet<BaseTypeNameNode*>& emittedUnnamedTypes);
     86    void emitNamedTypeDefinition(StringBuilder&, AST::NamedType&, Vector<std::reference_wrapper<AST::UnnamedType>>&, HashSet<AST::NamedType*>& emittedNamedTypes, HashSet<UnnamedTypeKey>& emittedUnnamedTypes);
     87    void emitUnnamedTypeDefinition(StringBuilder&, AST::UnnamedType&, MangledTypeName, HashSet<AST::NamedType*>& emittedNamedTypes, HashSet<UnnamedTypeKey>& emittedUnnamedTypes);
    8888    void emitMetalTypeDeclarations(StringBuilder&);
    8989    void emitMetalTypeDefinitions(StringBuilder&);
    9090
    91     std::unique_ptr<BaseTypeNameNode> createNameNode(AST::UnnamedType&, BaseTypeNameNode* parent);
    92     BaseTypeNameNode* insert(AST::UnnamedType&);
    93     BaseTypeNameNode& find(AST::UnnamedType&);
     91    void generateUniquedTypeName(AST::UnnamedType&);
    9492
    9593    Program& m_program;
    96     HashMap<UnnamedTypeKey, std::unique_ptr<BaseTypeNameNode>, UnnamedTypeKey::Hash, UnnamedTypeKey::Traits> m_unnamedTypesUniquingMap;
    97     HashMap<AST::UnnamedType*, BaseTypeNameNode*> m_unnamedTypeMapping;
     94    HashMap<UnnamedTypeKey, MangledTypeName> m_unnamedTypeMapping;
    9895    HashMap<AST::NamedType*, MangledTypeName> m_namedTypeMapping;
    99     HashMap<AST::NamedType*, Vector<std::reference_wrapper<BaseTypeNameNode>>> m_dependencyGraph;
     96    HashMap<AST::NamedType*, Vector<std::reference_wrapper<AST::UnnamedType>>> m_dependencyGraph;
    10097    HashMap<AST::EnumerationMember*, MangledEnumerationMemberName> m_enumerationMemberMapping;
    10198    HashMap<AST::StructureElement*, MangledStructureElementName> m_structureElementMapping;
  • trunk/Source/WebCore/Modules/webgpu/WHLSL/WHLSLSynthesizeConstructors.cpp

    r248395 r249109  
    8787    }
    8888
    89     HashSet<UnnamedTypeKey, UnnamedTypeKey::Hash, UnnamedTypeKey::Traits> takeUnnamedTypes()
     89    HashSet<UnnamedTypeKey> takeUnnamedTypes()
    9090    {
    9191        return WTFMove(m_unnamedTypes);
     
    108108    }
    109109
    110     HashSet<UnnamedTypeKey, UnnamedTypeKey::Hash, UnnamedTypeKey::Traits> m_unnamedTypes;
     110    HashSet<UnnamedTypeKey> m_unnamedTypes;
    111111    Vector<std::reference_wrapper<AST::NamedType>> m_namedTypes;
    112112};
Note: See TracChangeset for help on using the changeset viewer.