File: MangleNames.cpp

package info (click to toggle)
webkit2gtk 2.42.2-1~deb11u1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 362,432 kB
  • sloc: cpp: 2,881,947; javascript: 282,447; ansic: 134,088; python: 43,789; ruby: 18,308; perl: 15,872; asm: 14,389; xml: 4,395; yacc: 2,350; sh: 2,074; java: 1,734; lex: 1,323; makefile: 296; pascal: 60
file content (255 lines) | stat: -rw-r--r-- 8,487 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
/*
 * Copyright (c) 2023 Apple Inc. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE INC. OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include "config.h"
#include "MangleNames.h"

#include "AST.h"
#include "ASTVisitor.h"
#include "CallGraph.h"
#include "ContextProviderInlines.h"
#include "WGSL.h"
#include "WGSLShaderModule.h"
#include <wtf/HashSet.h>

namespace WGSL {

struct MangledName {
    enum Kind : uint8_t {
        Type,
        Local,
        Parameter,
        Function,
        Field,
    };
    static constexpr unsigned numberOfKinds = 6;

    Kind kind;
    uint32_t index;
    String originalName;

    String toString() const
    {
        static const ASCIILiteral prefixes[] = {
            "type"_s,
            "local"_s,
            "parameter"_s,
            "function"_s,
            "field"_s,
        };
        return makeString(prefixes[WTF::enumToUnderlyingType(kind)], String::number(index));
    }
};

class NameManglerVisitor : public AST::Visitor, public ContextProvider<MangledName> {
    using ContextProvider = ContextProvider<MangledName>;

public:
    NameManglerVisitor(const CallGraph& callGraph, PrepareResult& result)
        : m_callGraph(callGraph)
        , m_result(result)
    {
    }

    void run();

    void visit(AST::Function&) override;
    void visit(AST::VariableStatement&) override;
    void visit(AST::Structure&) override;
    void visit(AST::Variable&) override;
    void visit(AST::CompoundStatement&) override;
    void visit(AST::IdentifierExpression&) override;
    void visit(AST::FieldAccessExpression&) override;
    void visit(AST::NamedTypeName&) override;

private:
    using NameMap = ContextProvider::ContextMap;

    void introduceVariable(AST::Identifier&, MangledName::Kind);
    void readVariable(AST::Identifier&) const;

    MangledName makeMangledName(const String&, MangledName::Kind);

    void visitVariableDeclaration(AST::Variable&, MangledName::Kind);
    void visitFunctionBody(AST::Function&);

    const CallGraph& m_callGraph;
    PrepareResult& m_result;
    HashMap<AST::Structure*, NameMap> m_structFieldMapping;
    uint32_t m_indexPerType[MangledName::numberOfKinds] { 0 };
};

void NameManglerVisitor::run()
{
    auto& module = m_callGraph.ast();
    for (auto& function : module.functions()) {
        String originalName = function.name();
        introduceVariable(function.name(), MangledName::Function);
        auto it = m_result.entryPoints.find(originalName);
        if (it != m_result.entryPoints.end())
            it->value.mangledName = function.name();
    }

    for (auto& structure : module.structures())
        visit(structure);

    for (auto& variable : module.variables())
        visit(variable);

    for (auto& function : module.functions())
        visitFunctionBody(function);
}

void NameManglerVisitor::visit(AST::Function& function)
{
    introduceVariable(function.name(), MangledName::Function);
}

void NameManglerVisitor::visitFunctionBody(AST::Function& function)
{
    ContextScope functionScope(this);

    for (auto& parameter : function.parameters()) {
        AST::Visitor::visit(parameter.typeName());
        introduceVariable(parameter.name(), MangledName::Parameter);
    }

    // It's important that we call the base visitor here directly, otherwise
    // our overwritten visitor will introduce a new ContextScope for the compound
    // statement, which would allow shadowing the function's parameters
    AST::Visitor::visit(function.body());

    if (function.maybeReturnType())
        AST::Visitor::visit(*function.maybeReturnType());
}

void NameManglerVisitor::visit(AST::Structure& structure)
{
    introduceVariable(structure.name(), MangledName::Type);

    NameMap fieldMap;
    for (auto& member : structure.members()) {
        AST::Visitor::visit(member.type());
        auto mangledName = makeMangledName(member.name(), MangledName::Field);
        fieldMap.add(member.name(), mangledName);
        // FIXME: need to resolve type of expressions in order to be able to replace struct fields
    }
    auto result = m_structFieldMapping.add(&structure, WTFMove(fieldMap));
    ASSERT_UNUSED(result, result.isNewEntry);
}

void NameManglerVisitor::visit(AST::Variable& variable)
{
    String originalName = variable.name();
    for (auto& attribute : variable.attributes()) {
        if (is<AST::IdAttribute>(attribute)) {
            unsigned value;
            auto& expression = downcast<AST::IdAttribute>(attribute).value();
            if (is<AST::AbstractIntegerLiteral>(expression))
                value = downcast<AST::AbstractIntegerLiteral>(expression).value();
            else if (is<AST::Signed32Literal>(expression))
                value = downcast<AST::Signed32Literal>(expression).value();
            else if (is<AST::Unsigned32Literal>(expression))
                value = downcast<AST::Unsigned32Literal>(expression).value();
            else {
                // Constants must be resolved at an earlier phase
                RELEASE_ASSERT_NOT_REACHED();
            }
            originalName = String::number(value);
            break;
        }
    }

    const String& mangledName = variable.name();

    for (auto& entry : m_result.entryPoints) {
        auto it = entry.value.specializationConstants.find(originalName);
        if (it != entry.value.specializationConstants.end())
            it->value.mangledName = mangledName;
    }
}

void NameManglerVisitor::visit(AST::VariableStatement& variable)
{
    visitVariableDeclaration(variable.variable(), MangledName::Local);
}

void NameManglerVisitor::visitVariableDeclaration(AST::Variable& variable, MangledName::Kind kind)
{
    introduceVariable(variable.name(), kind);
    AST::Visitor::visit(variable);
}

void NameManglerVisitor::visit(AST::CompoundStatement& statement)
{
    ContextScope blockScope(this);
    AST::Visitor::visit(statement);
}

void NameManglerVisitor::visit(AST::IdentifierExpression& identifier)
{
    readVariable(identifier.identifier());
}

void NameManglerVisitor::visit(AST::FieldAccessExpression& access)
{
    // FIXME: need to resolve type of expressions in order to be able to replace struct fields
    AST::Visitor::visit(access.base());
}

void NameManglerVisitor::visit(AST::NamedTypeName& type)
{
    readVariable(type.name());
}

void NameManglerVisitor::introduceVariable(AST::Identifier& name, MangledName::Kind kind)
{
    const auto* mangledName = ContextProvider::introduceVariable(name, makeMangledName(name, kind));
    ASSERT(mangledName);
    m_callGraph.ast().replace(&name, AST::Identifier::makeWithSpan(name.span(), mangledName->toString()));
}

MangledName NameManglerVisitor::makeMangledName(const String& name, MangledName::Kind kind)
{
    return MangledName {
        kind,
        m_indexPerType[WTF::enumToUnderlyingType(kind)]++,
        name
    };
}

void NameManglerVisitor::readVariable(AST::Identifier& name) const
{
    // FIXME: this should be unconditional
    if (const auto* mangledName = ContextProvider::readVariable(name))
        m_callGraph.ast().replace(&name, AST::Identifier::makeWithSpan(name.span(), mangledName->toString()));
}

void mangleNames(CallGraph& callGraph, PrepareResult& result)
{
    NameManglerVisitor(callGraph, result).run();
}

} // namespace WGSL