Changeset 248395 in webkit


Ignore:
Timestamp:
Aug 7, 2019 4:47:49 PM (5 years ago)
Author:
weinig@apple.com
Message:

[WHLSL] Metal code generation takes a long time uniquing UnnamedTypes
https://bugs.webkit.org/show_bug.cgi?id=200512

Reviewed by Saam Barati.

Instead of using a trie for unnamed type uniquing, use the same technique used
in SynthesizeConstructors and use a HashMap of UnnamedTypeKeys. To make this
profitable, we also need to devirtualize the hash and equality functions on
UnnamedType, instead using an enum + switch. While this change only devirtualizes
the UnnamedType subtree, we should probably do it for the entire AST in a future
change.

  • Modules/webgpu/WHLSL/AST/WHLSLArrayReferenceType.h:
  • Modules/webgpu/WHLSL/AST/WHLSLArrayType.h:
  • Modules/webgpu/WHLSL/AST/WHLSLPointerType.h:
  • Modules/webgpu/WHLSL/AST/WHLSLReferenceType.h:
  • Modules/webgpu/WHLSL/AST/WHLSLTypeReference.h:
  • Modules/webgpu/WHLSL/AST/WHLSLUnnamedType.cpp: Added.

(WebCore::WHLSL::AST::UnnamedType::hash const):
(WebCore::WHLSL::AST::UnnamedType::operator== const):

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

Devirtualize hash, operator== and type predicates.

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

(WebCore::WHLSL::UnnamedTypeKey::UnnamedTypeKey):

  • Modules/webgpu/WHLSL/WHLSLSynthesizeConstructors.cpp:

(WebCore::WHLSL::UnnamedTypeKey::UnnamedTypeKey): Deleted.
Moved UnnamedTypeKey into it's own header from WHLSLSynthesizeConstructors.cpp

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

(WebCore::WHLSL::Metal::BaseTypeNameNode::BaseTypeNameNode):
(WebCore::WHLSL::Metal::BaseTypeNameNode::kind):
(WebCore::WHLSL::Metal::BaseTypeNameNode::isReferenceTypeNameNode const):
(WebCore::WHLSL::Metal::BaseTypeNameNode::isPointerTypeNameNode const):
(WebCore::WHLSL::Metal::BaseTypeNameNode::isArrayReferenceTypeNameNode const):
(WebCore::WHLSL::Metal::BaseTypeNameNode::isArrayTypeNameNode const):
Devirtualize BaseTypeNameNode as well. In a future change, we should consider removing
this class entirely and instead mapping directly to a parent/mangled name pair.

(WebCore::WHLSL::Metal::TypeNamer::visit):
(WebCore::WHLSL::Metal::TypeNamer::find):
(WebCore::WHLSL::Metal::TypeNamer::createNameNode):
(WebCore::WHLSL::Metal::parent):
(WebCore::WHLSL::Metal::TypeNamer::insert):
(WebCore::WHLSL::Metal::TypeNamer::emitUnnamedTypeDefinition):
(WebCore::WHLSL::Metal::TypeNamer::emitMetalTypeDefinitions):
(WebCore::WHLSL::Metal::TypeNamer::mangledNameForType):
(WebCore::WHLSL::Metal::findInVector): Deleted.
(WebCore::WHLSL::Metal::find): Deleted.
(WebCore::WHLSL::Metal::TypeNamer::emitAllUnnamedTypeDefinitions): Deleted.

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

Switch from a Vector based trie to a HashMap for UnnamedType uniquing. Also
use UnnamedType::Kind where possible with switch statements to clarify code.

  • WebCore.xcodeproj/project.pbxproj:

Add new files.

Location:
trunk/Source/WebCore
Files:
2 added
11 edited

Legend:

Unmodified
Added
Removed
  • trunk/Source/WebCore/ChangeLog

    r248389 r248395  
     12019-08-07  Sam Weinig  <weinig@apple.com>
     2
     3        [WHLSL] Metal code generation takes a long time uniquing UnnamedTypes
     4        https://bugs.webkit.org/show_bug.cgi?id=200512
     5
     6        Reviewed by Saam Barati.
     7
     8        Instead of using a trie for unnamed type uniquing, use the same technique used
     9        in SynthesizeConstructors and use a HashMap of UnnamedTypeKeys. To make this
     10        profitable, we also need to devirtualize the hash and equality functions on
     11        UnnamedType, instead using an enum + switch. While this change only devirtualizes
     12        the UnnamedType subtree, we should probably do it for the entire AST in a future
     13        change.
     14
     15        * Modules/webgpu/WHLSL/AST/WHLSLArrayReferenceType.h:
     16        * Modules/webgpu/WHLSL/AST/WHLSLArrayType.h:
     17        * Modules/webgpu/WHLSL/AST/WHLSLPointerType.h:
     18        * Modules/webgpu/WHLSL/AST/WHLSLReferenceType.h:
     19        * Modules/webgpu/WHLSL/AST/WHLSLTypeReference.h:
     20        * Modules/webgpu/WHLSL/AST/WHLSLUnnamedType.cpp: Added.
     21        (WebCore::WHLSL::AST::UnnamedType::hash const):
     22        (WebCore::WHLSL::AST::UnnamedType::operator== const):
     23         * Modules/webgpu/WHLSL/AST/WHLSLUnnamedType.h:
     24        Devirtualize hash, operator== and type predicates.
     25
     26        * Modules/webgpu/WHLSL/AST/WHLSLUnnamedTypeHash.h: Added.
     27        (WebCore::WHLSL::UnnamedTypeKey::UnnamedTypeKey):
     28        * Modules/webgpu/WHLSL/WHLSLSynthesizeConstructors.cpp:
     29        (WebCore::WHLSL::UnnamedTypeKey::UnnamedTypeKey): Deleted.
     30        Moved UnnamedTypeKey into it's own header from WHLSLSynthesizeConstructors.cpp
     31       
     32        * Modules/webgpu/WHLSL/Metal/WHLSLTypeNamer.cpp:
     33        (WebCore::WHLSL::Metal::BaseTypeNameNode::BaseTypeNameNode):
     34        (WebCore::WHLSL::Metal::BaseTypeNameNode::kind):
     35        (WebCore::WHLSL::Metal::BaseTypeNameNode::isReferenceTypeNameNode const):
     36        (WebCore::WHLSL::Metal::BaseTypeNameNode::isPointerTypeNameNode const):
     37        (WebCore::WHLSL::Metal::BaseTypeNameNode::isArrayReferenceTypeNameNode const):
     38        (WebCore::WHLSL::Metal::BaseTypeNameNode::isArrayTypeNameNode const):
     39        Devirtualize BaseTypeNameNode as well. In a future change, we should consider removing
     40        this class entirely and instead mapping directly to a parent/mangled name pair.
     41 
     42        (WebCore::WHLSL::Metal::TypeNamer::visit):
     43        (WebCore::WHLSL::Metal::TypeNamer::find):
     44        (WebCore::WHLSL::Metal::TypeNamer::createNameNode):
     45        (WebCore::WHLSL::Metal::parent):
     46        (WebCore::WHLSL::Metal::TypeNamer::insert):
     47        (WebCore::WHLSL::Metal::TypeNamer::emitUnnamedTypeDefinition):
     48        (WebCore::WHLSL::Metal::TypeNamer::emitMetalTypeDefinitions):
     49        (WebCore::WHLSL::Metal::TypeNamer::mangledNameForType):
     50        (WebCore::WHLSL::Metal::findInVector): Deleted.
     51        (WebCore::WHLSL::Metal::find): Deleted.
     52        (WebCore::WHLSL::Metal::TypeNamer::emitAllUnnamedTypeDefinitions): Deleted.
     53        * Modules/webgpu/WHLSL/Metal/WHLSLTypeNamer.h:
     54        Switch from a Vector based trie to a HashMap for UnnamedType uniquing. Also
     55        use UnnamedType::Kind where possible with switch statements to clarify code.
     56
     57        * WebCore.xcodeproj/project.pbxproj:
     58        Add new files.
     59
    1602019-08-07  Devin Rousso  <drousso@apple.com>
    261
  • trunk/Source/WebCore/Modules/webgpu/WHLSL/AST/WHLSLArrayReferenceType.h

    r248083 r248395  
    4141namespace AST {
    4242
    43 class ArrayReferenceType : public ReferenceType {
     43class ArrayReferenceType final : public ReferenceType {
    4444    WTF_MAKE_FAST_ALLOCATED;
    4545    WTF_MAKE_NONCOPYABLE(ArrayReferenceType);
     
    4747
    4848    ArrayReferenceType(CodeLocation location, AddressSpace addressSpace, Ref<UnnamedType> elementType)
    49         : Base(location, addressSpace, WTFMove(elementType))
     49        : Base(location, addressSpace, WTFMove(elementType), Kind::ArrayReferenceType)
    5050    {
    5151    }
     
    5858    virtual ~ArrayReferenceType() = default;
    5959
    60     bool isArrayReferenceType() const override { return true; }
    61 
    62     unsigned hash() const override
     60    unsigned hash() const
    6361    {
    6462        return this->Base::hash() ^ StringHasher::computeLiteralHash("array");
    6563    }
    6664
    67     bool operator==(const UnnamedType& other) const override
     65    bool operator==(const ArrayReferenceType& other) const
    6866    {
    69         if (!is<ArrayReferenceType>(other))
    70             return false;
    71 
    72         return addressSpace() == downcast<ArrayReferenceType>(other).addressSpace()
    73             && elementType() == downcast<ArrayReferenceType>(other).elementType();
     67        return addressSpace() == other.addressSpace() && elementType() == other.elementType();
    7468    }
    7569
     
    7872        return makeString(elementType().toString(), "[]");
    7973    }
    80 
    81 private:
    8274};
    8375
  • trunk/Source/WebCore/Modules/webgpu/WHLSL/AST/WHLSLArrayType.h

    r248083 r248395  
    3434#include <wtf/Noncopyable.h>
    3535#include <wtf/UniqueRef.h>
     36#include <wtf/text/StringConcatenateNumbers.h>
    3637#include <wtf/text/WTFString.h>
    3738
     
    4647    WTF_MAKE_NONCOPYABLE(ArrayType);
    4748    ArrayType(CodeLocation location, Ref<UnnamedType> elementType, unsigned numElements)
    48         : UnnamedType(location)
     49        : UnnamedType(location, Kind::ArrayType)
    4950        , m_elementType(WTFMove(elementType))
    5051        , m_numElements(numElements)
     
    6061    virtual ~ArrayType() = default;
    6162
    62     bool isArrayType() const override { return true; }
    63 
    6463    const UnnamedType& type() const { return m_elementType; }
    6564    UnnamedType& type() { return m_elementType; }
    6665    unsigned numElements() const { return m_numElements; }
    6766
    68     unsigned hash() const override
     67    unsigned hash() const
    6968    {
    7069        return WTF::IntHash<unsigned>::hash(m_numElements) ^ m_elementType->hash();
    7170    }
    7271
    73     bool operator==(const UnnamedType& other) const override
     72    bool operator==(const ArrayType& other) const
    7473    {
    75         if (!is<ArrayType>(other))
    76             return false;
    77 
    78         return numElements() == downcast<ArrayType>(other).numElements()
    79             && type() == downcast<ArrayType>(other).type();
     74        return numElements() == other.numElements() && type() == other.type();
    8075    }
    8176
  • trunk/Source/WebCore/Modules/webgpu/WHLSL/AST/WHLSLPointerType.h

    r248307 r248395  
    4747
    4848    PointerType(CodeLocation location, AddressSpace addressSpace, Ref<UnnamedType> elementType)
    49         : Base(location, addressSpace, WTFMove(elementType))
     49        : Base(location, addressSpace, WTFMove(elementType), Kind::PointerType)
    5050    {
    5151    }
     
    5959    virtual ~PointerType() = default;
    6060
    61     bool isPointerType() const override { return true; }
    62 
    63     unsigned hash() const override
     61    unsigned hash() const
    6462    {
    6563        return this->Base::hash() ^ StringHasher::computeLiteralHash("pointer");
    6664    }
    6765
    68     bool operator==(const UnnamedType& other) const override
     66    bool operator==(const PointerType& other) const
    6967    {
    70         if (!is<PointerType>(other))
    71             return false;
    72 
    73         return addressSpace() == downcast<PointerType>(other).addressSpace()
    74             && elementType() == downcast<PointerType>(other).elementType();
     68        return addressSpace() == other.addressSpace() && elementType() == other.elementType();
    7569    }
    7670
  • trunk/Source/WebCore/Modules/webgpu/WHLSL/AST/WHLSLReferenceType.h

    r248083 r248395  
    4545    WTF_MAKE_NONCOPYABLE(ReferenceType);
    4646protected:
    47     ReferenceType(CodeLocation location, AddressSpace addressSpace, Ref<UnnamedType> elementType)
    48         : UnnamedType(location)
     47    ReferenceType(CodeLocation location, AddressSpace addressSpace, Ref<UnnamedType> elementType, Kind kind)
     48        : UnnamedType(location, kind)
    4949        , m_addressSpace(addressSpace)
    5050        , m_elementType(WTFMove(elementType))
     
    5252    }
    5353public:
    54 
    5554    virtual ~ReferenceType() = default;
    56 
    57     bool isReferenceType() const override { return true; }
    5855
    5956    AddressSpace addressSpace() const { return m_addressSpace; }
     
    6158    UnnamedType& elementType() { return m_elementType; }
    6259
    63     unsigned hash() const override
     60    unsigned hash() const
    6461    {
    6562        return ~m_elementType->hash();
  • trunk/Source/WebCore/Modules/webgpu/WHLSL/AST/WHLSLTypeReference.h

    r248083 r248395  
    4949    WTF_MAKE_NONCOPYABLE(TypeReference);
    5050    TypeReference(CodeLocation location, String&& name, TypeArguments&& typeArguments)
    51         : UnnamedType(location)
     51        : UnnamedType(location, Kind::TypeReference)
    5252        , m_name(WTFMove(name))
    5353        , m_typeArguments(WTFMove(typeArguments))
     
    6363
    6464    static Ref<TypeReference> wrap(CodeLocation, NamedType& resolvedType);
    65 
    66     bool isTypeReference() const override { return true; }
    6765
    6866    String& name() { return m_name; }
     
    9290    }
    9391
    94     unsigned hash() const override
     92    unsigned hash() const
    9593    {
    9694        // Currently, we only use this function after the name resolver runs.
     
    10098    }
    10199
    102     bool operator==(const UnnamedType& other) const override
     100    bool operator==(const TypeReference& other) const
    103101    {
    104102        ASSERT(m_resolvedType);
    105         if (!is<TypeReference>(other))
    106             return false;
    107 
    108         return &unifyNode() == &downcast<TypeReference>(other).unifyNode();
     103        return &unifyNode() == &other.unifyNode();
    109104    }
    110105
  • trunk/Source/WebCore/Modules/webgpu/WHLSL/AST/WHLSLUnnamedType.h

    r248083 r248395  
    4646    WTF_MAKE_NONCOPYABLE(UnnamedType);
    4747public:
    48     UnnamedType(CodeLocation location)
     48    enum class Kind {
     49        TypeReference,
     50        PointerType,
     51        ArrayReferenceType,
     52        ArrayType
     53    };
     54
     55    UnnamedType(CodeLocation location, Kind kind)
    4956        : m_codeLocation(location)
     57        , m_kind(kind)
    5058    {
    5159    }
     
    5462
    5563    bool isUnnamedType() const override { return true; }
    56     virtual bool isTypeReference() const { return false; }
    57     virtual bool isPointerType() const { return false; }
    58     virtual bool isArrayReferenceType() const { return false; }
    59     virtual bool isArrayType() const { return false; }
    60     virtual bool isReferenceType() const { return false; }
     64
     65    Kind kind() const { return m_kind; }
     66    bool isTypeReference() const { return m_kind == Kind::TypeReference; }
     67    bool isPointerType() const { return m_kind == Kind::PointerType; }
     68    bool isArrayReferenceType() const { return m_kind == Kind::ArrayReferenceType; }
     69    bool isArrayType() const { return m_kind == Kind::ArrayType; }
     70    bool isReferenceType() const { return isPointerType() || isArrayReferenceType(); }
    6171
    6272    virtual const Type& unifyNode() const { return *this; }
    6373    virtual Type& unifyNode() { return *this; }
    6474
    65     virtual unsigned hash() const = 0;
    66     virtual bool operator==(const UnnamedType&) const = 0;
     75    unsigned hash() const;
     76    bool operator==(const UnnamedType&) const;
    6777
    6878    virtual String toString() const = 0;
     
    7282private:
    7383    CodeLocation m_codeLocation;
     84    Kind m_kind;
    7485};
    7586
  • trunk/Source/WebCore/Modules/webgpu/WHLSL/Metal/WHLSLTypeNamer.cpp

    r248307 r248395  
    5959namespace Metal {
    6060
     61// FIXME: Look into replacing BaseTypeNameNode with a simple struct { RefPtr<UnnamedType> parent; MangledTypeName; } that UnnamedTypeKeys map to.
    6162class BaseTypeNameNode {
    6263    WTF_MAKE_FAST_ALLOCATED;
    6364public:
    64     BaseTypeNameNode(BaseTypeNameNode* parent, MangledTypeName&& mangledName)
     65    BaseTypeNameNode(BaseTypeNameNode* parent, MangledTypeName&& mangledName, AST::UnnamedType::Kind kind)
    6566        : m_parent(parent)
    6667        , m_mangledName(mangledName)
     68        , m_kind(kind)
    6769    {
    6870    }
    6971    virtual ~BaseTypeNameNode() = default;
    70     virtual bool isArrayTypeNameNode() const { return false; }
    71     virtual bool isArrayReferenceTypeNameNode() const { return false; }
    72     virtual bool isPointerTypeNameNode() const { return false; }
    73     virtual bool isReferenceTypeNameNode() const { return false; }
    74     Vector<UniqueRef<BaseTypeNameNode>>& children() { return m_children; }
    75     void append(UniqueRef<BaseTypeNameNode>&& child)
    76     {
    77         m_children.append(WTFMove(child));
    78     }
     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::PointerType; }
     76    bool isArrayReferenceTypeNameNode() const { return m_kind == AST::UnnamedType::Kind::ArrayReferenceType; }
     77    bool isArrayTypeNameNode() const { return m_kind == AST::UnnamedType::Kind::ArrayType; }
     78
    7979    BaseTypeNameNode* parent() { return m_parent; }
    8080    MangledTypeName mangledName() const { return m_mangledName; }
    8181
    8282private:
    83     Vector<UniqueRef<BaseTypeNameNode>> m_children;
    8483    BaseTypeNameNode* m_parent;
    8584    MangledTypeName m_mangledName;
    86 };
    87 
    88 class ArrayTypeNameNode : public BaseTypeNameNode {
     85    AST::UnnamedType::Kind m_kind;
     86};
     87
     88class ArrayTypeNameNode final : public BaseTypeNameNode {
    8989    WTF_MAKE_FAST_ALLOCATED;
    9090public:
    9191    ArrayTypeNameNode(BaseTypeNameNode* parent, MangledTypeName&& mangledName, unsigned numElements)
    92         : BaseTypeNameNode(parent, WTFMove(mangledName))
     92        : BaseTypeNameNode(parent, WTFMove(mangledName), AST::UnnamedType::Kind::ArrayType)
    9393        , m_numElements(numElements)
    9494    {
    9595    }
    9696    virtual ~ArrayTypeNameNode() = default;
    97     bool isArrayTypeNameNode() const override { return true; }
    9897    unsigned numElements() const { return m_numElements; }
    9998
     
    102101};
    103102
    104 class ArrayReferenceTypeNameNode : public BaseTypeNameNode {
     103class ArrayReferenceTypeNameNode final : public BaseTypeNameNode {
    105104    WTF_MAKE_FAST_ALLOCATED;
    106105public:
    107106    ArrayReferenceTypeNameNode(BaseTypeNameNode* parent, MangledTypeName&& mangledName, AST::AddressSpace addressSpace)
    108         : BaseTypeNameNode(parent, WTFMove(mangledName))
     107        : BaseTypeNameNode(parent, WTFMove(mangledName), AST::UnnamedType::Kind::ArrayReferenceType)
    109108        , m_addressSpace(addressSpace)
    110109    {
    111110    }
    112111    virtual ~ArrayReferenceTypeNameNode() = default;
    113     bool isArrayReferenceTypeNameNode() const override { return true; }
    114112    AST::AddressSpace addressSpace() const { return m_addressSpace; }
    115113
     
    118116};
    119117
    120 class PointerTypeNameNode : public BaseTypeNameNode {
     118class PointerTypeNameNode final : public BaseTypeNameNode {
    121119    WTF_MAKE_FAST_ALLOCATED;
    122120public:
    123121    PointerTypeNameNode(BaseTypeNameNode* parent, MangledTypeName&& mangledName, AST::AddressSpace addressSpace)
    124         : BaseTypeNameNode(parent, WTFMove(mangledName))
     122        : BaseTypeNameNode(parent, WTFMove(mangledName), AST::UnnamedType::Kind::PointerType)
    125123        , m_addressSpace(addressSpace)
    126124    {
    127125    }
    128126    virtual ~PointerTypeNameNode() = default;
    129     bool isPointerTypeNameNode() const override { return true; }
    130127    AST::AddressSpace addressSpace() const { return m_addressSpace; }
    131128
     
    134131};
    135132
    136 class ReferenceTypeNameNode : public BaseTypeNameNode {
     133class ReferenceTypeNameNode final : public BaseTypeNameNode {
    137134    WTF_MAKE_FAST_ALLOCATED;
    138135public:
    139136    ReferenceTypeNameNode(BaseTypeNameNode* parent, MangledTypeName&& mangledName, AST::NamedType& namedType)
    140         : BaseTypeNameNode(parent, WTFMove(mangledName))
     137        : BaseTypeNameNode(parent, WTFMove(mangledName), AST::UnnamedType::Kind::TypeReference)
    141138        , m_namedType(namedType)
    142139    {
    143140    }
    144141    virtual ~ReferenceTypeNameNode() = default;
    145     bool isReferenceTypeNameNode() const override { return true; }
    146142    AST::NamedType& namedType() { return m_namedType; }
    147143
     
    182178TypeNamer::~TypeNamer() = default;
    183179
    184 static Vector<UniqueRef<BaseTypeNameNode>>::iterator findInVector(AST::UnnamedType& unnamedType, Vector<UniqueRef<BaseTypeNameNode>>& types)
    185 {
    186     return std::find_if(types.begin(), types.end(), [&](BaseTypeNameNode& baseTypeNameNode) -> bool {
    187         if (is<AST::TypeReference>(unnamedType) && is<ReferenceTypeNameNode>(baseTypeNameNode)) {
    188             auto& resolvedType = downcast<AST::TypeReference>(unnamedType).resolvedType();
    189             return &resolvedType == &downcast<ReferenceTypeNameNode>(baseTypeNameNode).namedType();
    190         }
    191         if (is<AST::PointerType>(unnamedType) && is<PointerTypeNameNode>(baseTypeNameNode))
    192             return downcast<AST::PointerType>(unnamedType).addressSpace() == downcast<PointerTypeNameNode>(baseTypeNameNode).addressSpace();
    193         if (is<AST::ArrayReferenceType>(unnamedType) && is<ArrayReferenceTypeNameNode>(baseTypeNameNode))
    194             return downcast<AST::ArrayReferenceType>(unnamedType).addressSpace() == downcast<ArrayReferenceTypeNameNode>(baseTypeNameNode).addressSpace();
    195         if (is<AST::ArrayType>(unnamedType) && is<ArrayTypeNameNode>(baseTypeNameNode))
    196             return downcast<AST::ArrayType>(unnamedType).numElements() == downcast<ArrayTypeNameNode>(baseTypeNameNode).numElements();
    197         return false;
    198     });
    199 }
    200 
    201 static BaseTypeNameNode& find(AST::UnnamedType& unnamedType, Vector<UniqueRef<BaseTypeNameNode>>& types)
    202 {
    203     auto& vectorToSearch = ([&]() -> Vector<UniqueRef<BaseTypeNameNode>>& {
    204         if (is<AST::TypeReference>(unnamedType))
    205             return types;
    206         if (is<AST::PointerType>(unnamedType))
    207             return find(downcast<AST::PointerType>(unnamedType).elementType(), types).children();
    208         if (is<AST::ArrayReferenceType>(unnamedType))
    209             return find(downcast<AST::ArrayReferenceType>(unnamedType).elementType(), types).children();
    210         return find(downcast<AST::ArrayType>(unnamedType).type(), types).children();
    211     })();
    212     auto iterator = findInVector(unnamedType, vectorToSearch);
    213     ASSERT(iterator != types.end());
    214     return *iterator;
    215 }
    216 
    217180void TypeNamer::visit(AST::UnnamedType& unnamedType)
    218181{
    219     insert(unnamedType, m_trie);
     182    insert(unnamedType);
    220183}
    221184
     
    235198
    236199    {
    237         Vector<std::reference_wrapper<BaseTypeNameNode>> neighbors = { find(enumerationDefinition.type(), m_trie) };
     200        Vector<std::reference_wrapper<BaseTypeNameNode>> neighbors = { find(enumerationDefinition.type()) };
    238201        auto addResult = m_dependencyGraph.add(&enumerationDefinition, WTFMove(neighbors));
    239202        ASSERT_UNUSED(addResult, addResult.isNewEntry);
     
    260223            auto addResult = m_structureElementMapping.add(&structureElement, generateNextStructureElementName());
    261224            ASSERT_UNUSED(addResult, addResult.isNewEntry);
    262             neighbors.append(find(structureElement.type(), m_trie));
     225            neighbors.append(find(structureElement.type()));
    263226        }
    264227        auto addResult = m_dependencyGraph.add(&structureDefinition, WTFMove(neighbors));
     
    275238    Visitor::visit(typeDefinition);
    276239    {
    277         Vector<std::reference_wrapper<BaseTypeNameNode>> neighbors = { find(typeDefinition.type(), m_trie) };
     240        Vector<std::reference_wrapper<BaseTypeNameNode>> neighbors = { find(typeDefinition.type()) };
    278241        auto addResult = m_dependencyGraph.add(&typeDefinition, WTFMove(neighbors));
    279242        ASSERT_UNUSED(addResult, addResult.isNewEntry);
     
    283246void TypeNamer::visit(AST::Expression& expression)
    284247{
    285     insert(expression.resolvedType(), m_trie);
     248    insert(expression.resolvedType());
    286249    Visitor::visit(expression);
    287250}
     
    298261}
    299262
    300 UniqueRef<BaseTypeNameNode> TypeNamer::createNameNode(AST::UnnamedType& unnamedType, BaseTypeNameNode* parent)
    301 {
    302     if (is<AST::TypeReference>(unnamedType)) {
     263BaseTypeNameNode& 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
     270std::unique_ptr<BaseTypeNameNode> TypeNamer::createNameNode(AST::UnnamedType& unnamedType, BaseTypeNameNode* parent)
     271{
     272    switch (unnamedType.kind()) {
     273    case AST::UnnamedType::Kind::TypeReference: {
    303274        auto& typeReference = downcast<AST::TypeReference>(unnamedType);
    304         return makeUniqueRef<ReferenceTypeNameNode>(parent, generateNextTypeName(), typeReference.resolvedType());
    305     }
    306     if (is<AST::PointerType>(unnamedType)) {
     275        return std::make_unique<ReferenceTypeNameNode>(parent, generateNextTypeName(), typeReference.resolvedType());
     276    }
     277    case AST::UnnamedType::Kind::PointerType: {
    307278        auto& pointerType = downcast<AST::PointerType>(unnamedType);
    308         return makeUniqueRef<PointerTypeNameNode>(parent, generateNextTypeName(), pointerType.addressSpace());
    309     }
    310     if (is<AST::ArrayReferenceType>(unnamedType)) {
     279        return std::make_unique<PointerTypeNameNode>(parent, generateNextTypeName(), pointerType.addressSpace());
     280    }
     281    case AST::UnnamedType::Kind::ArrayReferenceType: {
    311282        auto& arrayReferenceType = downcast<AST::ArrayReferenceType>(unnamedType);
    312         return makeUniqueRef<ArrayReferenceTypeNameNode>(parent, generateNextTypeName(), arrayReferenceType.addressSpace());
    313     }
    314     auto& arrayType = downcast<AST::ArrayType>(unnamedType);
    315     return makeUniqueRef<ArrayTypeNameNode>(parent, generateNextTypeName(), arrayType.numElements());
    316 }
    317 
    318 BaseTypeNameNode* TypeNamer::insert(AST::UnnamedType& unnamedType, Vector<UniqueRef<BaseTypeNameNode>>& types)
     283        return std::make_unique<ArrayReferenceTypeNameNode>(parent, generateNextTypeName(), arrayReferenceType.addressSpace());
     284    }
     285    case AST::UnnamedType::Kind::ArrayType: {
     286        auto& arrayType = downcast<AST::ArrayType>(unnamedType);
     287        return std::make_unique<ArrayTypeNameNode>(parent, generateNextTypeName(), arrayType.numElements());
     288    }
     289    }
     290}
     291
     292static AST::UnnamedType* parent(AST::UnnamedType& unnamedType)
     293{
     294    switch (unnamedType.kind()) {
     295    case AST::UnnamedType::Kind::TypeReference:
     296        return nullptr;
     297    case AST::UnnamedType::Kind::PointerType:
     298        return &downcast<AST::PointerType>(unnamedType).elementType();
     299    case AST::UnnamedType::Kind::ArrayReferenceType:
     300        return &downcast<AST::ArrayReferenceType>(unnamedType).elementType();
     301    case AST::UnnamedType::Kind::ArrayType:
     302        return &downcast<AST::ArrayType>(unnamedType).type();
     303    }
     304}
     305
     306BaseTypeNameNode* TypeNamer::insert(AST::UnnamedType& unnamedType)
    319307{
    320308    if (auto* result = m_unnamedTypeMapping.get(&unnamedType))
    321309        return result;
    322310
    323     Vector<UniqueRef<BaseTypeNameNode>>* vectorToInsertInto { nullptr };
    324     BaseTypeNameNode* parent { nullptr };
    325     if (is<AST::TypeReference>(unnamedType)) {
    326         vectorToInsertInto = &types;
    327         parent = nullptr;
    328     } else if (is<AST::PointerType>(unnamedType)) {
    329         parent = insert(downcast<AST::PointerType>(unnamedType).elementType(), types);
    330         vectorToInsertInto = &parent->children();
    331     } else if (is<AST::ArrayReferenceType>(unnamedType)) {
    332         parent = insert(downcast<AST::ArrayReferenceType>(unnamedType).elementType(), types);
    333         vectorToInsertInto = &parent->children();
    334     } else {
    335         parent = insert(downcast<AST::ArrayType>(unnamedType).type(), types);
    336         vectorToInsertInto = &parent->children();
    337     }
    338     ASSERT(vectorToInsertInto);
    339 
    340     auto iterator = findInVector(unnamedType, *vectorToInsertInto);
    341     if (iterator == vectorToInsertInto->end()) {
    342         auto result = createNameNode(unnamedType, parent);
    343         {
    344             auto addResult = m_unnamedTypeMapping.add(&unnamedType, &result);
    345             ASSERT_UNUSED(addResult, addResult.isNewEntry);
    346         }
    347         vectorToInsertInto->append(WTFMove(result));
    348         return &vectorToInsertInto->last().get();
    349     }
    350     m_unnamedTypeMapping.add(&unnamedType, &*iterator);
    351     return &*iterator;
     311    auto* parentUnnamedType = parent(unnamedType);
     312    BaseTypeNameNode* parentNode = parentUnnamedType ? insert(*parentUnnamedType) : nullptr;
     313
     314    auto addResult = m_unnamedTypesUniquingMap.ensure(UnnamedTypeKey { unnamedType }, [&] {
     315        return createNameNode(unnamedType, parentNode);
     316    });
     317
     318    m_unnamedTypeMapping.add(&unnamedType, addResult.iterator->value.get());
     319    return addResult.iterator->value.get();
    352320}
    353321
     
    383351    if (emittedUnnamedTypes.contains(&baseTypeNameNode))
    384352        return;
     353
    385354    if (baseTypeNameNode.parent())
    386355        emitUnnamedTypeDefinition(stringBuilder, *baseTypeNameNode.parent(), emittedNamedTypes, emittedUnnamedTypes);
    387     if (is<ReferenceTypeNameNode>(baseTypeNameNode)) {
     356   
     357    switch (baseTypeNameNode.kind()) {
     358    case AST::UnnamedType::Kind::TypeReference: {
    388359        auto& namedType = downcast<ReferenceTypeNameNode>(baseTypeNameNode).namedType();
    389360        emitNamedTypeDefinition(stringBuilder, namedType, emittedNamedTypes, emittedUnnamedTypes);
    390361        stringBuilder.flexibleAppend("typedef ", mangledNameForType(namedType), ' ', baseTypeNameNode.mangledName(), ";\n");
    391     } else if (is<PointerTypeNameNode>(baseTypeNameNode)) {
     362        break;
     363    }
     364    case AST::UnnamedType::Kind::PointerType: {
    392365        auto& pointerType = downcast<PointerTypeNameNode>(baseTypeNameNode);
    393366        ASSERT(baseTypeNameNode.parent());
    394367        stringBuilder.flexibleAppend("typedef ", toString(pointerType.addressSpace()), ' ', pointerType.parent()->mangledName(), "* ", pointerType.mangledName(), ";\n");
    395     } else if (is<ArrayReferenceTypeNameNode>(baseTypeNameNode)) {
     368        break;
     369    }
     370    case AST::UnnamedType::Kind::ArrayReferenceType: {
    396371        auto& arrayReferenceType = downcast<ArrayReferenceTypeNameNode>(baseTypeNameNode);
    397372        ASSERT(baseTypeNameNode.parent());
     
    402377            "};\n"
    403378        );
    404     } else {
     379        break;
     380    }
     381    case AST::UnnamedType::Kind::ArrayType: {
    405382        auto& arrayType = downcast<ArrayTypeNameNode>(baseTypeNameNode);
    406383        ASSERT(baseTypeNameNode.parent());
    407384        stringBuilder.flexibleAppend("typedef array<", arrayType.parent()->mangledName(), ", ", arrayType.numElements(), "> ", arrayType.mangledName(), ";\n");
    408     }
     385        break;
     386    }
     387    }
     388
    409389    emittedUnnamedTypes.add(&baseTypeNameNode);
    410390}
     
    440420}
    441421
    442 void TypeNamer::emitAllUnnamedTypeDefinitions(StringBuilder& stringBuilder, Vector<UniqueRef<BaseTypeNameNode>>& nodes, HashSet<AST::NamedType*>& emittedNamedTypes, HashSet<BaseTypeNameNode*>& emittedUnnamedTypes)
    443 {
    444     for (auto& node : nodes) {
    445         emitUnnamedTypeDefinition(stringBuilder, node, emittedNamedTypes, emittedUnnamedTypes);
    446         emitAllUnnamedTypeDefinitions(stringBuilder, node->children(), emittedNamedTypes, emittedUnnamedTypes);
    447     }
    448 }
    449 
    450422void TypeNamer::emitMetalTypeDefinitions(StringBuilder& stringBuilder)
    451423{
    452424    HashSet<AST::NamedType*> emittedNamedTypes;
    453425    HashSet<BaseTypeNameNode*> emittedUnnamedTypes;
    454     for (auto& keyValuePair : m_dependencyGraph)
    455         emitNamedTypeDefinition(stringBuilder, *keyValuePair.key, emittedNamedTypes, emittedUnnamedTypes);
    456     emitAllUnnamedTypeDefinitions(stringBuilder, m_trie, emittedNamedTypes, emittedUnnamedTypes);
     426    for (auto& namedType : m_dependencyGraph.keys())
     427        emitNamedTypeDefinition(stringBuilder, *namedType, emittedNamedTypes, emittedUnnamedTypes);
     428    for (auto& node : m_unnamedTypesUniquingMap.values())
     429        emitUnnamedTypeDefinition(stringBuilder, *node, emittedNamedTypes, emittedUnnamedTypes);
    457430}
    458431
    459432MangledTypeName TypeNamer::mangledNameForType(AST::UnnamedType& unnamedType)
    460433{
    461     return find(unnamedType, m_trie).mangledName();
     434    return find(unnamedType).mangledName();
    462435}
    463436
  • trunk/Source/WebCore/Modules/webgpu/WHLSL/Metal/WHLSLTypeNamer.h

    r248307 r248395  
    2929
    3030#include "WHLSLMangledNames.h"
     31#include "WHLSLUnnamedTypeHash.h"
    3132#include "WHLSLVisitor.h"
    3233#include <wtf/HashMap.h>
     
    8586    void emitNamedTypeDefinition(StringBuilder&, AST::NamedType&, HashSet<AST::NamedType*>& emittedNamedTypes, HashSet<BaseTypeNameNode*>& emittedUnnamedTypes);
    8687    void emitUnnamedTypeDefinition(StringBuilder&, BaseTypeNameNode&, HashSet<AST::NamedType*>& emittedNamedTypes, HashSet<BaseTypeNameNode*>& emittedUnnamedTypes);
    87     void emitAllUnnamedTypeDefinitions(StringBuilder&, Vector<UniqueRef<BaseTypeNameNode>>&, HashSet<AST::NamedType*>& emittedNamedTypes, HashSet<BaseTypeNameNode*>& emittedUnnamedTypes);
    8888    void emitMetalTypeDeclarations(StringBuilder&);
    8989    void emitMetalTypeDefinitions(StringBuilder&);
    9090
    91     UniqueRef<BaseTypeNameNode> createNameNode(AST::UnnamedType&, BaseTypeNameNode* parent);
    92     BaseTypeNameNode* insert(AST::UnnamedType&, Vector<UniqueRef<BaseTypeNameNode>>&);
     91    std::unique_ptr<BaseTypeNameNode> createNameNode(AST::UnnamedType&, BaseTypeNameNode* parent);
     92    BaseTypeNameNode* insert(AST::UnnamedType&);
     93    BaseTypeNameNode& find(AST::UnnamedType&);
    9394
    9495    Program& m_program;
    95     Vector<UniqueRef<BaseTypeNameNode>> m_trie;
     96    HashMap<UnnamedTypeKey, std::unique_ptr<BaseTypeNameNode>, UnnamedTypeKey::Hash, UnnamedTypeKey::Traits> m_unnamedTypesUniquingMap;
    9697    HashMap<AST::UnnamedType*, BaseTypeNameNode*> m_unnamedTypeMapping;
    9798    HashMap<AST::NamedType*, MangledTypeName> m_namedTypeMapping;
  • trunk/Source/WebCore/Modules/webgpu/WHLSL/WHLSLSynthesizeConstructors.cpp

    r248303 r248395  
    3939#include "WHLSLStructureDefinition.h"
    4040#include "WHLSLTypeReference.h"
     41#include "WHLSLUnnamedTypeHash.h"
    4142#include "WHLSLVariableDeclaration.h"
    4243#include "WHLSLVisitor.h"
     
    4546
    4647namespace WHLSL {
    47 
    48 class UnnamedTypeKey {
    49 public:
    50     UnnamedTypeKey() = default;
    51     UnnamedTypeKey(WTF::HashTableDeletedValueType)
    52     {
    53         m_type = bitwise_cast<AST::UnnamedType*>(static_cast<uintptr_t>(1));
    54     }
    55 
    56     UnnamedTypeKey(AST::UnnamedType& type)
    57         : m_type(&type)
    58     { }
    59 
    60     bool isEmptyValue() const { return !m_type; }
    61     bool isHashTableDeletedValue() const { return m_type == bitwise_cast<AST::UnnamedType*>(static_cast<uintptr_t>(1)); }
    62 
    63     unsigned hash() const { return m_type->hash(); }
    64     bool operator==(const UnnamedTypeKey& other) const { return *m_type == *other.m_type; }
    65     AST::UnnamedType& unnamedType() const { return *m_type; }
    66 
    67     struct Hash {
    68         static unsigned hash(const UnnamedTypeKey& key) { return key.hash(); }
    69         static bool equal(const UnnamedTypeKey& a, const UnnamedTypeKey& b) { return a == b; }
    70         static const bool safeToCompareToEmptyOrDeleted = false;
    71         static const bool emptyValueIsZero = true;
    72     };
    73 
    74     struct Traits : public WTF::SimpleClassHashTraits<UnnamedTypeKey> {
    75         static const bool hasIsEmptyValueFunction = true;
    76         static bool isEmptyValue(const UnnamedTypeKey& key) { return key.isEmptyValue(); }
    77     };
    78 
    79 private:
    80     AST::UnnamedType* m_type { nullptr };
    81 };
    8248
    8349class FindAllTypes : public Visitor {
  • trunk/Source/WebCore/WebCore.xcodeproj/project.pbxproj

    r248364 r248395  
    22322232                7C8139AA1ED6604B00CE26E8 /* JSDOMCastedThisErrorBehavior.h in Headers */ = {isa = PBXBuildFile; fileRef = 7C8139A91ED62DF200CE26E8 /* JSDOMCastedThisErrorBehavior.h */; settings = {ATTRIBUTES = (Private, ); }; };
    22332233                7C83DE861D04CC5D00FEBCF3 /* SpringSolver.h in Headers */ = {isa = PBXBuildFile; fileRef = 7C83DE851D04CBD400FEBCF3 /* SpringSolver.h */; };
     2234                7C85B20222FB04850030684F /* WHLSLUnnamedType.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 7C85B20122FB04850030684F /* WHLSLUnnamedType.cpp */; };
    22342235                7C8E34AD1E4A33AF0054CE23 /* JSDOMConvertAny.h in Headers */ = {isa = PBXBuildFile; fileRef = 7C8E34921E4A338E0054CE23 /* JSDOMConvertAny.h */; settings = {ATTRIBUTES = (Private, ); }; };
    22352236                7C8E34AE1E4A33AF0054CE23 /* JSDOMConvertBase.h in Headers */ = {isa = PBXBuildFile; fileRef = 7C8E34931E4A338E0054CE23 /* JSDOMConvertBase.h */; settings = {ATTRIBUTES = (Private, ); }; };
     
    97429743                7C8139A91ED62DF200CE26E8 /* JSDOMCastedThisErrorBehavior.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = JSDOMCastedThisErrorBehavior.h; sourceTree = "<group>"; };
    97439744                7C83DE851D04CBD400FEBCF3 /* SpringSolver.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SpringSolver.h; sourceTree = "<group>"; };
     9745                7C85B20122FB04850030684F /* WHLSLUnnamedType.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = WHLSLUnnamedType.cpp; sourceTree = "<group>"; };
     9746                7C85B20422FB06320030684F /* WHLSLUnnamedTypeHash.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = WHLSLUnnamedTypeHash.h; sourceTree = "<group>"; };
    97449747                7C8E34921E4A338E0054CE23 /* JSDOMConvertAny.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = JSDOMConvertAny.h; sourceTree = "<group>"; };
    97459748                7C8E34931E4A338E0054CE23 /* JSDOMConvertBase.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = JSDOMConvertBase.h; sourceTree = "<group>"; };
     
    1705917062                        isa = PBXGroup;
    1706017063                        children = (
     17064                                52914C2C22F93E5D00578150 /* WHLSLAddressEscapeMode.h */,
    1706117065                                1C840B9021EC30F900D0500D /* WHLSLAddressSpace.h */,
    17062                                 52914C2C22F93E5D00578150 /* WHLSLAddressEscapeMode.h */,
    1706317066                                C21BF72521CD89E200227979 /* WHLSLArrayReferenceType.h */,
    1706417067                                C21BF70921CD89CA00227979 /* WHLSLArrayType.h */,
     
    1713817141                                C20F4F6421DFBE5C0070C45A /* WHLSLTypeReference.cpp */,
    1713917142                                C21BF71F21CD89DC00227979 /* WHLSLTypeReference.h */,
     17143                                7C85B20122FB04850030684F /* WHLSLUnnamedType.cpp */,
    1714017144                                1C33277221CF0D2E000DC9F2 /* WHLSLUnnamedType.h */,
     17145                                7C85B20422FB06320030684F /* WHLSLUnnamedTypeHash.h */,
    1714117146                                C20F4F6721DFF3A70070C45A /* WHLSLUnsignedIntegerLiteral.cpp */,
    1714217147                                C21BF72B21CD89E800227979 /* WHLSLUnsignedIntegerLiteral.h */,
     
    3351633521                                5D21A80213ECE5DF00BB7064 /* WebVTTParser.cpp in Sources */,
    3351733522                                B10B6981140C174000BC1C26 /* WebVTTTokenizer.cpp in Sources */,
     33523                                7C85B20222FB04850030684F /* WHLSLUnnamedType.cpp in Sources */,
    3351833524                                A14832C2187F65C700DA63A6 /* WKUtilities.c in Sources */,
    3351933525                                A833C7CC0A2CF07400D57664 /* XLinkNames.cpp in Sources */,
Note: See TracChangeset for help on using the changeset viewer.