Skip to content

Commit 5dd2d41

Browse files
authored
[NFC] Use HeapTypeDef more widely (#7446)
Update `TypeBuilder::build` to return a vector of `HeapTypeDef`. Although `HeapTypeDef` is implicitly convertible to `HeapType`, containers of `HeapTypeDef` are not implicitly convertible to containers of `HeapType`, so this change requires several other utilities and passes to start using `HeapTypeDef` directly and transitively.
1 parent 5ad90cb commit 5dd2d41

27 files changed

+199
-181
lines changed

src/ir/module-utils.cpp

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -347,15 +347,15 @@ namespace {
347347

348348
// Helper for collecting HeapTypes and their frequencies.
349349
struct TypeInfos {
350-
InsertOrderedMap<HeapType, HeapTypeInfo> info;
350+
InsertOrderedMap<HeapTypeDef, HeapTypeInfo> info;
351351

352352
// Multivalue control flow structures need a function type, but the identity
353353
// of the function type (i.e. what recursion group it is in or whether it is
354354
// final) doesn't matter. Save them for the end to see if we can re-use an
355355
// existing function type with the necessary signature.
356356
InsertOrderedMap<Signature, size_t> controlFlowSignatures;
357357

358-
void note(HeapType type) {
358+
void note(HeapTypeDef type) {
359359
if (!type.isBasic()) {
360360
++info[type].useCount;
361361
}
@@ -366,7 +366,7 @@ struct TypeInfos {
366366
}
367367
}
368368
// Ensure a type is included without increasing its count.
369-
void include(HeapType type) {
369+
void include(HeapTypeDef type) {
370370
if (!type.isBasic()) {
371371
info[type];
372372
}
@@ -388,7 +388,7 @@ struct TypeInfos {
388388
note(sig.results);
389389
}
390390
}
391-
bool contains(HeapType type) { return info.count(type); }
391+
bool contains(HeapTypeDef type) { return info.count(type); }
392392
};
393393

394394
struct CodeScanner
@@ -468,11 +468,11 @@ struct CodeScanner
468468
};
469469

470470
void classifyTypeVisibility(Module& wasm,
471-
InsertOrderedMap<HeapType, HeapTypeInfo>& types);
471+
InsertOrderedMap<HeapTypeDef, HeapTypeInfo>& types);
472472

473473
} // anonymous namespace
474474

475-
InsertOrderedMap<HeapType, HeapTypeInfo> collectHeapTypeInfo(
475+
InsertOrderedMap<HeapTypeDef, HeapTypeInfo> collectHeapTypeInfo(
476476
Module& wasm, TypeInclusion inclusion, VisibilityHandling visibility) {
477477
// Collect module-level info.
478478
TypeInfos info;
@@ -523,9 +523,9 @@ InsertOrderedMap<HeapType, HeapTypeInfo> collectHeapTypeInfo(
523523
// track which recursion groups we've already processed to avoid quadratic
524524
// behavior when there is a single large group.
525525
// TODO: Use a vector here, since we never try to add the same type twice.
526-
UniqueNonrepeatingDeferredQueue<HeapType> newTypes;
527-
std::unordered_map<Signature, HeapType> seenSigs;
528-
auto noteNewType = [&](HeapType type) {
526+
UniqueNonrepeatingDeferredQueue<HeapTypeDef> newTypes;
527+
std::unordered_map<Signature, HeapTypeDef> seenSigs;
528+
auto noteNewType = [&](HeapTypeDef type) {
529529
newTypes.push(type);
530530
if (type.isSignature()) {
531531
seenSigs.insert({type.getSignature(), type});
@@ -588,14 +588,14 @@ InsertOrderedMap<HeapType, HeapTypeInfo> collectHeapTypeInfo(
588588

589589
namespace {
590590

591-
void classifyTypeVisibility(Module& wasm,
592-
InsertOrderedMap<HeapType, HeapTypeInfo>& types) {
591+
void classifyTypeVisibility(
592+
Module& wasm, InsertOrderedMap<HeapTypeDef, HeapTypeInfo>& types) {
593593
// We will need to traverse the types used by public types and mark them
594594
// public as well.
595-
std::vector<HeapType> workList;
595+
std::vector<HeapTypeDef> workList;
596596
std::unordered_set<RecGroup> publicGroups;
597597

598-
auto notePublic = [&](HeapType type) {
598+
auto notePublic = [&](HeapTypeDef type) {
599599
if (type.isBasic()) {
600600
return;
601601
}
@@ -694,20 +694,20 @@ void setIndices(IndexedHeapTypes& indexedTypes) {
694694

695695
} // anonymous namespace
696696

697-
std::vector<HeapType> collectHeapTypes(Module& wasm) {
697+
std::vector<HeapTypeDef> collectHeapTypes(Module& wasm) {
698698
auto info = collectHeapTypeInfo(wasm);
699-
std::vector<HeapType> types;
699+
std::vector<HeapTypeDef> types;
700700
types.reserve(info.size());
701701
for (auto& [type, _] : info) {
702702
types.push_back(type);
703703
}
704704
return types;
705705
}
706706

707-
std::vector<HeapType> getPublicHeapTypes(Module& wasm) {
707+
std::vector<HeapTypeDef> getPublicHeapTypes(Module& wasm) {
708708
auto info = collectHeapTypeInfo(
709709
wasm, TypeInclusion::BinaryTypes, VisibilityHandling::FindVisibility);
710-
std::vector<HeapType> types;
710+
std::vector<HeapTypeDef> types;
711711
types.reserve(info.size());
712712
for (auto& [type, typeInfo] : info) {
713713
if (typeInfo.visibility == Visibility::Public) {
@@ -717,10 +717,10 @@ std::vector<HeapType> getPublicHeapTypes(Module& wasm) {
717717
return types;
718718
}
719719

720-
std::vector<HeapType> getPrivateHeapTypes(Module& wasm) {
720+
std::vector<HeapTypeDef> getPrivateHeapTypes(Module& wasm) {
721721
auto info = collectHeapTypeInfo(
722722
wasm, TypeInclusion::UsedIRTypes, VisibilityHandling::FindVisibility);
723-
std::vector<HeapType> types;
723+
std::vector<HeapTypeDef> types;
724724
types.reserve(info.size());
725725
for (auto& [type, typeInfo] : info) {
726726
if (typeInfo.visibility == Visibility::Private) {

src/ir/module-utils.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -470,26 +470,26 @@ struct HeapTypeInfo {
470470
Visibility visibility = Visibility::Unknown;
471471
};
472472

473-
InsertOrderedMap<HeapType, HeapTypeInfo> collectHeapTypeInfo(
473+
InsertOrderedMap<HeapTypeDef, HeapTypeInfo> collectHeapTypeInfo(
474474
Module& wasm,
475475
TypeInclusion inclusion = TypeInclusion::AllTypes,
476476
VisibilityHandling visibility = VisibilityHandling::NoVisibility);
477477

478478
// Helper function for collecting all the non-basic heap types used in the
479479
// module, i.e. the types that would appear in the type section.
480-
std::vector<HeapType> collectHeapTypes(Module& wasm);
480+
std::vector<HeapTypeDef> collectHeapTypes(Module& wasm);
481481

482482
// Collect all the heap types visible on the module boundary that cannot be
483483
// changed. TODO: For open world use cases, this needs to include all subtypes
484484
// of public types as well.
485-
std::vector<HeapType> getPublicHeapTypes(Module& wasm);
485+
std::vector<HeapTypeDef> getPublicHeapTypes(Module& wasm);
486486

487487
// getHeapTypes - getPublicHeapTypes
488-
std::vector<HeapType> getPrivateHeapTypes(Module& wasm);
488+
std::vector<HeapTypeDef> getPrivateHeapTypes(Module& wasm);
489489

490490
struct IndexedHeapTypes {
491-
std::vector<HeapType> types;
492-
std::unordered_map<HeapType, Index> indices;
491+
std::vector<HeapTypeDef> types;
492+
std::unordered_map<HeapTypeDef, Index> indices;
493493
};
494494

495495
// Similar to `collectHeapTypes`, but provides fast lookup of the index for each

src/ir/subtypes.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ namespace wasm {
2828
//
2929
// This only scans user types, and not basic types like HeapType::eq.
3030
struct SubTypes {
31-
SubTypes(const std::vector<HeapType>& types) : types(types) {
31+
SubTypes(const std::vector<HeapTypeDef>& types) : types(types) {
3232
for (auto type : types) {
3333
note(type);
3434
}
@@ -198,11 +198,11 @@ struct SubTypes {
198198

199199
// All the types in the program. This is computed here anyhow, and can be
200200
// useful for callers to iterate on, so it is public.
201-
std::vector<HeapType> types;
201+
std::vector<HeapTypeDef> types;
202202

203203
private:
204204
// Add a type to the graph.
205-
void note(HeapType type) {
205+
void note(HeapTypeDef type) {
206206
if (auto super = type.getDeclaredSuperType()) {
207207
typeSubTypes[*super].push_back(type);
208208
}

src/parser/contexts.h

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1179,18 +1179,19 @@ struct ParseImplicitTypeDefsCtx : TypeParserCtx<ParseImplicitTypeDefsCtx> {
11791179
Lexer in;
11801180

11811181
// Types parsed so far.
1182-
std::vector<HeapType>& types;
1182+
std::vector<HeapTypeDef>& types;
11831183

11841184
// Map typeuse positions without an explicit type to the correct type.
1185-
std::unordered_map<Index, HeapType>& implicitTypes;
1185+
std::unordered_map<Index, HeapTypeDef>& implicitTypes;
11861186

11871187
// Map signatures to the first defined heap type they match.
1188-
std::unordered_map<Signature, HeapType> sigTypes;
1188+
std::unordered_map<Signature, HeapTypeDef> sigTypes;
11891189

1190-
ParseImplicitTypeDefsCtx(Lexer& in,
1191-
std::vector<HeapType>& types,
1192-
std::unordered_map<Index, HeapType>& implicitTypes,
1193-
const IndexMap& typeIndices)
1190+
ParseImplicitTypeDefsCtx(
1191+
Lexer& in,
1192+
std::vector<HeapTypeDef>& types,
1193+
std::unordered_map<Index, HeapTypeDef>& implicitTypes,
1194+
const IndexMap& typeIndices)
11941195
: TypeParserCtx<ParseImplicitTypeDefsCtx>(typeIndices), in(in),
11951196
types(types), implicitTypes(implicitTypes) {
11961197
for (auto type : types) {
@@ -1231,7 +1232,7 @@ struct ParseImplicitTypeDefsCtx : TypeParserCtx<ParseImplicitTypeDefsCtx> {
12311232
}
12321233

12331234
auto sig = Signature(Type(paramTypes), Type(resultTypes));
1234-
auto [it, inserted] = sigTypes.insert({sig, HeapType::func});
1235+
auto [it, inserted] = sigTypes.insert({sig, HeapType(HeapType::func)});
12351236
if (inserted) {
12361237
auto type = HeapType(sig);
12371238
it->second = type;
@@ -1259,8 +1260,8 @@ struct ParseModuleTypesCtx : TypeParserCtx<ParseModuleTypesCtx>,
12591260

12601261
Module& wasm;
12611262

1262-
const std::vector<HeapType>& types;
1263-
const std::unordered_map<Index, HeapType>& implicitTypes;
1263+
const std::vector<HeapTypeDef>& types;
1264+
const std::unordered_map<Index, HeapTypeDef>& implicitTypes;
12641265
const std::unordered_map<Index, Index>& implicitElemIndices;
12651266

12661267
// The index of the current type.
@@ -1269,8 +1270,8 @@ struct ParseModuleTypesCtx : TypeParserCtx<ParseModuleTypesCtx>,
12691270
ParseModuleTypesCtx(
12701271
Lexer& in,
12711272
Module& wasm,
1272-
const std::vector<HeapType>& types,
1273-
const std::unordered_map<Index, HeapType>& implicitTypes,
1273+
const std::vector<HeapTypeDef>& types,
1274+
const std::unordered_map<Index, HeapTypeDef>& implicitTypes,
12741275
const std::unordered_map<Index, Index>& implicitElemIndices,
12751276
const IndexMap& typeIndices)
12761277
: TypeParserCtx<ParseModuleTypesCtx>(typeIndices), in(in), wasm(wasm),
@@ -1308,7 +1309,7 @@ struct ParseModuleTypesCtx : TypeParserCtx<ParseModuleTypesCtx>,
13081309
return TypeUse{it->second, ids};
13091310
}
13101311

1311-
Result<HeapType> getBlockTypeFromTypeUse(Index pos, TypeUse use) {
1312+
Result<HeapTypeDef> getBlockTypeFromTypeUse(Index pos, TypeUse use) {
13121313
return use.type;
13131314
}
13141315

@@ -1448,9 +1449,9 @@ struct ParseDefsCtx : TypeParserCtx<ParseDefsCtx> {
14481449
Module& wasm;
14491450
Builder builder;
14501451

1451-
const std::vector<HeapType>& types;
1452-
const std::unordered_map<Index, HeapType>& implicitTypes;
1453-
const std::unordered_map<HeapType, std::unordered_map<Name, Index>>&
1452+
const std::vector<HeapTypeDef>& types;
1453+
const std::unordered_map<Index, HeapTypeDef>& implicitTypes;
1454+
const std::unordered_map<HeapTypeDef, std::unordered_map<Name, Index>>&
14541455
typeNames;
14551456
const std::unordered_map<Index, Index>& implicitElemIndices;
14561457

@@ -1475,9 +1476,9 @@ struct ParseDefsCtx : TypeParserCtx<ParseDefsCtx> {
14751476
ParseDefsCtx(
14761477
Lexer& in,
14771478
Module& wasm,
1478-
const std::vector<HeapType>& types,
1479-
const std::unordered_map<Index, HeapType>& implicitTypes,
1480-
const std::unordered_map<HeapType, std::unordered_map<Name, Index>>&
1479+
const std::vector<HeapTypeDef>& types,
1480+
const std::unordered_map<Index, HeapTypeDef>& implicitTypes,
1481+
const std::unordered_map<HeapTypeDef, std::unordered_map<Name, Index>>&
14811482
typeNames,
14821483
const std::unordered_map<Index, Index>& implicitElemIndices,
14831484
const IndexMap& typeIndices)
@@ -1501,7 +1502,7 @@ struct ParseDefsCtx : TypeParserCtx<ParseDefsCtx> {
15011502
return HeapType(Signature(Type::none, results[0]));
15021503
}
15031504

1504-
Result<HeapType> getBlockTypeFromTypeUse(Index pos, HeapType type) {
1505+
Result<HeapTypeDef> getBlockTypeFromTypeUse(Index pos, HeapType type) {
15051506
assert(type.isSignature());
15061507
// TODO: Error if block parameters are named
15071508
return type;

src/parser/parse-2-typedefs.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ Result<> parseTypeDefs(
2222
ParseDeclsCtx& decls,
2323
Lexer& input,
2424
IndexMap& typeIndices,
25-
std::vector<HeapType>& types,
26-
std::unordered_map<HeapType, std::unordered_map<Name, Index>>& typeNames) {
25+
std::vector<HeapTypeDef>& types,
26+
std::unordered_map<HeapTypeDef, std::unordered_map<Name, Index>>& typeNames) {
2727
TypeBuilder builder(decls.typeDefs.size());
2828
ParseTypeDefsCtx ctx(input, builder, typeIndices);
2929
for (auto& recType : decls.recTypeDefs) {

src/parser/parse-3-implicit-types.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ Result<>
2222
parseImplicitTypeDefs(ParseDeclsCtx& decls,
2323
Lexer& input,
2424
IndexMap& typeIndices,
25-
std::vector<HeapType>& types,
26-
std::unordered_map<Index, HeapType>& implicitTypes) {
25+
std::vector<HeapTypeDef>& types,
26+
std::unordered_map<Index, HeapTypeDef>& implicitTypes) {
2727
ParseImplicitTypeDefsCtx ctx(input, types, implicitTypes, typeIndices);
2828
for (Index pos : decls.implicitTypeDefs) {
2929
WithPosition with(ctx, pos);

src/parser/parse-4-module-types.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,12 @@
1818

1919
namespace wasm::WATParser {
2020

21-
Result<> parseModuleTypes(ParseDeclsCtx& decls,
22-
Lexer& input,
23-
IndexMap& typeIndices,
24-
std::vector<HeapType>& types,
25-
std::unordered_map<Index, HeapType>& implicitTypes) {
21+
Result<>
22+
parseModuleTypes(ParseDeclsCtx& decls,
23+
Lexer& input,
24+
IndexMap& typeIndices,
25+
std::vector<HeapTypeDef>& types,
26+
std::unordered_map<Index, HeapTypeDef>& implicitTypes) {
2627
ParseModuleTypesCtx ctx(input,
2728
decls.wasm,
2829
types,

src/parser/parse-5-defs.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@ Result<> parseDefinitions(
2222
ParseDeclsCtx& decls,
2323
Lexer& input,
2424
IndexMap& typeIndices,
25-
std::vector<HeapType>& types,
26-
std::unordered_map<Index, HeapType>& implicitTypes,
27-
std::unordered_map<HeapType, std::unordered_map<Name, Index>>& typeNames) {
25+
std::vector<HeapTypeDef>& types,
26+
std::unordered_map<Index, HeapTypeDef>& implicitTypes,
27+
std::unordered_map<HeapTypeDef, std::unordered_map<Name, Index>>& typeNames) {
2828
// Parse definitions.
2929
// TODO: Parallelize this.
3030
ParseDefsCtx ctx(input,

src/parser/wat-parser-internal.h

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -28,29 +28,30 @@ Result<> parseTypeDefs(
2828
ParseDeclsCtx& decls,
2929
Lexer& input,
3030
IndexMap& typeIndices,
31-
std::vector<HeapType>& types,
32-
std::unordered_map<HeapType, std::unordered_map<Name, Index>>& typeNames);
31+
std::vector<HeapTypeDef>& types,
32+
std::unordered_map<HeapTypeDef, std::unordered_map<Name, Index>>& typeNames);
3333

3434
Result<>
3535
parseImplicitTypeDefs(ParseDeclsCtx& decls,
3636
Lexer& input,
3737
IndexMap& typeIndices,
38-
std::vector<HeapType>& types,
39-
std::unordered_map<Index, HeapType>& implicitTypes);
38+
std::vector<HeapTypeDef>& types,
39+
std::unordered_map<Index, HeapTypeDef>& implicitTypes);
4040

41-
Result<> parseModuleTypes(ParseDeclsCtx& decls,
42-
Lexer& input,
43-
IndexMap& typeIndices,
44-
std::vector<HeapType>& types,
45-
std::unordered_map<Index, HeapType>& implicitTypes);
41+
Result<>
42+
parseModuleTypes(ParseDeclsCtx& decls,
43+
Lexer& input,
44+
IndexMap& typeIndices,
45+
std::vector<HeapTypeDef>& types,
46+
std::unordered_map<Index, HeapTypeDef>& implicitTypes);
4647

4748
Result<> parseDefinitions(
4849
ParseDeclsCtx& decls,
4950
Lexer& input,
5051
IndexMap& typeIndices,
51-
std::vector<HeapType>& types,
52-
std::unordered_map<Index, HeapType>& implicitTypes,
53-
std::unordered_map<HeapType, std::unordered_map<Name, Index>>& typeNames);
52+
std::vector<HeapTypeDef>& types,
53+
std::unordered_map<Index, HeapTypeDef>& implicitTypes,
54+
std::unordered_map<HeapTypeDef, std::unordered_map<Name, Index>>& typeNames);
5455

5556
// RAII utility for temporarily changing the parsing position of a parsing
5657
// context.

src/parser/wat-parser.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -100,11 +100,11 @@ Result<> doParseModule(Module& wasm, Lexer& input, bool allowExtra) {
100100
auto typeIndices = createIndexMap(decls.in, decls.typeDefs);
101101
CHECK_ERR(typeIndices);
102102

103-
std::vector<HeapType> types;
104-
std::unordered_map<HeapType, std::unordered_map<Name, Index>> typeNames;
103+
std::vector<HeapTypeDef> types;
104+
std::unordered_map<HeapTypeDef, std::unordered_map<Name, Index>> typeNames;
105105
CHECK_ERR(parseTypeDefs(decls, input, *typeIndices, types, typeNames));
106106

107-
std::unordered_map<Index, HeapType> implicitTypes;
107+
std::unordered_map<Index, HeapTypeDef> implicitTypes;
108108
CHECK_ERR(
109109
parseImplicitTypeDefs(decls, input, *typeIndices, types, implicitTypes));
110110

0 commit comments

Comments
 (0)