Skip to content

Commit 83d8edb

Browse files
authored
Prepare for type exports (#7335)
Change the type of the field `value` in class `Export` to `std::variant<Name, HeapType>`, since a type export references a heap type and not a named component.
1 parent b4bdcc3 commit 83d8edb

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+208
-194
lines changed

src/binaryen-c.cpp

Lines changed: 8 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -4908,50 +4908,35 @@ WASM_DEPRECATED BinaryenExportRef BinaryenAddExport(BinaryenModuleRef module,
49084908
BinaryenExportRef BinaryenAddFunctionExport(BinaryenModuleRef module,
49094909
const char* internalName,
49104910
const char* externalName) {
4911-
auto* ret = new Export();
4912-
ret->value = internalName;
4913-
ret->name = externalName;
4914-
ret->kind = ExternalKind::Function;
4911+
auto* ret = new Export(externalName, ExternalKind::Function, internalName);
49154912
((Module*)module)->addExport(ret);
49164913
return ret;
49174914
}
49184915
BinaryenExportRef BinaryenAddTableExport(BinaryenModuleRef module,
49194916
const char* internalName,
49204917
const char* externalName) {
4921-
auto* ret = new Export();
4922-
ret->value = internalName;
4923-
ret->name = externalName;
4924-
ret->kind = ExternalKind::Table;
4918+
auto* ret = new Export(externalName, ExternalKind::Table, internalName);
49254919
((Module*)module)->addExport(ret);
49264920
return ret;
49274921
}
49284922
BinaryenExportRef BinaryenAddMemoryExport(BinaryenModuleRef module,
49294923
const char* internalName,
49304924
const char* externalName) {
4931-
auto* ret = new Export();
4932-
ret->value = internalName;
4933-
ret->name = externalName;
4934-
ret->kind = ExternalKind::Memory;
4925+
auto* ret = new Export(externalName, ExternalKind::Memory, internalName);
49354926
((Module*)module)->addExport(ret);
49364927
return ret;
49374928
}
49384929
BinaryenExportRef BinaryenAddGlobalExport(BinaryenModuleRef module,
49394930
const char* internalName,
49404931
const char* externalName) {
4941-
auto* ret = new Export();
4942-
ret->value = internalName;
4943-
ret->name = externalName;
4944-
ret->kind = ExternalKind::Global;
4932+
auto* ret = new Export(externalName, ExternalKind::Global, internalName);
49454933
((Module*)module)->addExport(ret);
49464934
return ret;
49474935
}
49484936
BinaryenExportRef BinaryenAddTagExport(BinaryenModuleRef module,
49494937
const char* internalName,
49504938
const char* externalName) {
4951-
auto* ret = new Export();
4952-
ret->value = internalName;
4953-
ret->name = externalName;
4954-
ret->kind = ExternalKind::Tag;
4939+
auto* ret = new Export(externalName, ExternalKind::Tag, internalName);
49554940
((Module*)module)->addExport(ret);
49564941
return ret;
49574942
}
@@ -5102,11 +5087,8 @@ void BinaryenSetMemory(BinaryenModuleRef module,
51025087
memory->shared = shared;
51035088
memory->addressType = memory64 ? Type::i64 : Type::i32;
51045089
if (exportName) {
5105-
auto memoryExport = std::make_unique<Export>();
5106-
memoryExport->name = exportName;
5107-
memoryExport->value = memory->name;
5108-
memoryExport->kind = ExternalKind::Memory;
5109-
((Module*)module)->addExport(memoryExport.release());
5090+
((Module*)module)
5091+
->addExport(new Export(exportName, ExternalKind::Memory, memory->name));
51105092
}
51115093
((Module*)module)->removeDataSegments([&](DataSegment* curr) {
51125094
return true;
@@ -5940,7 +5922,7 @@ const char* BinaryenExportGetName(BinaryenExportRef export_) {
59405922
return ((Export*)export_)->name.str.data();
59415923
}
59425924
const char* BinaryenExportGetValue(BinaryenExportRef export_) {
5943-
return ((Export*)export_)->value.str.data();
5925+
return ((Export*)export_)->getInternalName()->str.data();
59445926
}
59455927

59465928
//

src/ir/export-utils.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ std::vector<T*> getExportedByKind(Module& wasm, G getModuleItem) {
2828
std::vector<T*> ret;
2929
for (auto& ex : wasm.exports) {
3030
if (ex->kind == K) {
31-
ret.push_back(getModuleItem(ex->value));
31+
// The kind is either ExternalKind::Function or ExternalKind::Global
32+
ret.push_back(getModuleItem(*ex->getInternalName()));
3233
}
3334
}
3435
return ret;

src/ir/export-utils.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ std::vector<Global*> getExportedGlobals(Module& wasm);
2727
inline bool isExported(const Module& module, const Function& func) {
2828
for (auto& exportFunc : module.exports) {
2929
if (exportFunc->kind == ExternalKind::Function &&
30-
exportFunc->value == func.name) {
30+
*exportFunc->getInternalName() == func.name) {
3131
return true;
3232
}
3333
}

src/ir/module-splitting.cpp

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -355,9 +355,9 @@ void ModuleSplitter::setupJSPI() {
355355
// Support the first version of JSPI, where the JSPI pass added the load
356356
// secondary module export.
357357
// TODO: remove this when the new JSPI API is only supported.
358-
if (primary.getExportOrNull(LOAD_SECONDARY_MODULE)) {
359-
internalLoadSecondaryModule =
360-
primary.getExport(LOAD_SECONDARY_MODULE)->value;
358+
if (auto* loadSecondary = primary.getExportOrNull(LOAD_SECONDARY_MODULE);
359+
loadSecondary && loadSecondary->kind == ExternalKind::Function) {
360+
internalLoadSecondaryModule = *loadSecondary->getInternalName();
361361
// Remove the exported LOAD_SECONDARY_MODULE function since it's only needed
362362
// internally.
363363
primary.removeExport(LOAD_SECONDARY_MODULE);
@@ -471,7 +471,7 @@ ModuleSplitter::initExportedPrimaryFuncs(const Module& primary) {
471471
std::map<Name, Name> functionExportNames;
472472
for (auto& ex : primary.exports) {
473473
if (ex->kind == ExternalKind::Function) {
474-
functionExportNames[ex->value] = ex->name;
474+
functionExportNames[*ex->getInternalName()] = ex->name;
475475
}
476476
}
477477
return functionExportNames;
@@ -527,10 +527,10 @@ void ModuleSplitter::thunkExportedSecondaryFunctions() {
527527
Builder builder(primary);
528528
for (auto& ex : primary.exports) {
529529
if (ex->kind != ExternalKind::Function ||
530-
!secondaryFuncs.count(ex->value)) {
530+
!secondaryFuncs.count(*ex->getInternalName())) {
531531
continue;
532532
}
533-
Name secondaryFunc = ex->value;
533+
Name secondaryFunc = *ex->getInternalName();
534534
if (primary.getFunctionOrNull(secondaryFunc)) {
535535
// We've already created a thunk for this function
536536
continue;
@@ -823,7 +823,9 @@ void ModuleSplitter::shareImportableItems() {
823823
std::unordered_map<std::pair<ExternalKind, Name>, Name> exports;
824824
for (auto& ex : primary.exports) {
825825
if (ex->kind != ExternalKind::Function) {
826-
exports[std::make_pair(ex->kind, ex->value)] = ex->name;
826+
if (auto* name = ex->getInternalName()) {
827+
exports[std::make_pair(ex->kind, *name)] = ex->name;
828+
}
827829
}
828830
}
829831

@@ -843,7 +845,7 @@ void ModuleSplitter::shareImportableItems() {
843845
? minified.getName()
844846
: genericExportName);
845847
Name exportName = Names::getValidExportName(primary, baseName);
846-
primary.addExport(new Export{exportName, primaryItem.name, kind});
848+
primary.addExport(new Export(exportName, kind, primaryItem.name));
847849
secondaryItem.base = exportName;
848850
}
849851
};

src/ir/module-utils.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -632,12 +632,12 @@ void classifyTypeVisibility(Module& wasm,
632632
for (auto& ex : wasm.exports) {
633633
switch (ex->kind) {
634634
case ExternalKind::Function: {
635-
auto* func = wasm.getFunction(ex->value);
635+
auto* func = wasm.getFunction(*ex->getInternalName());
636636
notePublic(func->type);
637637
continue;
638638
}
639639
case ExternalKind::Table: {
640-
auto* table = wasm.getTable(ex->value);
640+
auto* table = wasm.getTable(*ex->getInternalName());
641641
assert(table->type.isRef());
642642
notePublic(table->type.getHeapType());
643643
continue;
@@ -646,14 +646,14 @@ void classifyTypeVisibility(Module& wasm,
646646
// Never a reference type.
647647
continue;
648648
case ExternalKind::Global: {
649-
auto* global = wasm.getGlobal(ex->value);
649+
auto* global = wasm.getGlobal(*ex->getInternalName());
650650
if (global->type.isRef()) {
651651
notePublic(global->type.getHeapType());
652652
}
653653
continue;
654654
}
655655
case ExternalKind::Tag:
656-
notePublic(wasm.getTag(ex->value)->type);
656+
notePublic(wasm.getTag(*ex->getInternalName())->type);
657657
continue;
658658
case ExternalKind::Invalid:
659659
break;

src/ir/possible-contents.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2183,7 +2183,7 @@ Flower::Flower(Module& wasm, const PassOptions& options)
21832183

21842184
for (auto& ex : wasm.exports) {
21852185
if (ex->kind == ExternalKind::Table) {
2186-
shared.publicTables.insert(ex->value);
2186+
shared.publicTables.insert(*ex->getInternalName());
21872187
}
21882188
}
21892189

@@ -2295,7 +2295,7 @@ Flower::Flower(Module& wasm, const PassOptions& options)
22952295
// Exports can be modified from the outside.
22962296
for (auto& ex : wasm.exports) {
22972297
if (ex->kind == ExternalKind::Function) {
2298-
calledFromOutside(ex->value);
2298+
calledFromOutside(*ex->getInternalName());
22992299
} else if (ex->kind == ExternalKind::Table) {
23002300
// If any table is exported, assume any function in any table (including
23012301
// other tables) can be called from the outside.
@@ -2319,7 +2319,7 @@ Flower::Flower(Module& wasm, const PassOptions& options)
23192319
} else if (ex->kind == ExternalKind::Global) {
23202320
// Exported mutable globals are roots, since the outside may write any
23212321
// value to them.
2322-
auto name = ex->value;
2322+
auto name = *ex->getInternalName();
23232323
auto* global = wasm.getGlobal(name);
23242324
if (global->mutable_) {
23252325
roots[GlobalLocation{name}] = PossibleContents::fromType(global->type);
@@ -2337,7 +2337,7 @@ Flower::Flower(Module& wasm, const PassOptions& options)
23372337
}
23382338
for (auto& ex : wasm.exports) {
23392339
if (ex->kind == ExternalKind::Tag) {
2340-
publicTags.insert(ex->value);
2340+
publicTags.insert(*ex->getInternalName());
23412341
}
23422342
}
23432343
for (auto tag : publicTags) {

src/passes/Asyncify.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1668,7 +1668,7 @@ struct Asyncify : public Pass {
16681668
for (auto& theExport : module->exports) {
16691669
if (theExport->kind == ExternalKind::Memory &&
16701670
theExport->name == asyncifyMemoryValue) {
1671-
asyncifyMemory = theExport->value;
1671+
asyncifyMemory = *theExport->getInternalName();
16721672
break;
16731673
}
16741674
}
@@ -1901,7 +1901,9 @@ struct ModAsyncify
19011901
void doWalkFunction(Function* func) {
19021902
// Find the asyncify state name.
19031903
auto* unwind = this->getModule()->getExport(ASYNCIFY_STOP_UNWIND);
1904-
auto* unwindFunc = this->getModule()->getFunction(unwind->value);
1904+
auto* unwindFunc = this->getModule()->getFunction(
1905+
((unwind->kind == ExternalKind::Function)) ? *unwind->getInternalName()
1906+
: Name());
19051907
FindAll<GlobalSet> sets(unwindFunc->body);
19061908
assert(sets.list.size() == 1);
19071909
asyncifyStateName = sets.list[0]->name;

src/passes/DeadArgumentElimination.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ struct DAE : public Pass {
260260
// Exports are considered unseen calls.
261261
for (auto& curr : module->exports) {
262262
if (curr->kind == ExternalKind::Function) {
263-
hasUnseenCalls.insert(curr->value);
263+
hasUnseenCalls.insert(*curr->getInternalName());
264264
}
265265
}
266266

src/passes/Directize.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ struct Directize : public Pass {
223223

224224
for (auto& ex : module->exports) {
225225
if (ex->kind == ExternalKind::Table) {
226-
tables[ex->value].mayBeModified = true;
226+
tables[*ex->getInternalName()].mayBeModified = true;
227227
}
228228
}
229229

src/passes/EncloseWorld.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,12 @@ struct EncloseWorld : public Pass {
5252
std::vector<std::unique_ptr<Export>> newExports;
5353
for (auto& ex : module->exports) {
5454
if (ex->kind == ExternalKind::Function) {
55-
auto* func = module->getFunction(ex->value);
55+
auto* name = ex->getInternalName();
56+
auto* func = module->getFunction(*name);
5657
// If this opens up types, replace it with an enclosed stub.
5758
if (opensTypes(func)) {
5859
auto stubName = makeStubStubForExport(func, module);
59-
ex->value = stubName;
60+
*name = stubName;
6061
}
6162
}
6263
}

0 commit comments

Comments
 (0)