-
Notifications
You must be signed in to change notification settings - Fork 746
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[wasm-split] Add a multi-split mode #6943
Changes from all commits
af0128d
202dce6
230388c
8ae07d3
8406aa7
a9a7f91
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -362,6 +362,87 @@ void splitModule(const WasmSplitOptions& options) { | |
writeModule(*secondary, options.secondaryOutput, options); | ||
} | ||
|
||
void multiSplitModule(const WasmSplitOptions& options) { | ||
if (options.manifestFile.empty()) { | ||
Fatal() << "--multi-split requires --manifest"; | ||
} | ||
if (options.output.empty()) { | ||
Fatal() << "--multi-split requires --output"; | ||
} | ||
|
||
std::ifstream manifest(options.manifestFile); | ||
if (!manifest.is_open()) { | ||
Fatal() << "File not found: " << options.manifestFile; | ||
} | ||
|
||
Module wasm; | ||
parseInput(wasm, options); | ||
|
||
// Map module names to the functions that should be in the modules. | ||
std::map<std::string, std::unordered_set<std::string>> moduleFuncs; | ||
// The module for which we are currently parsing a set of functions. | ||
std::string currModule; | ||
// The set of functions we are currently inserting into. | ||
std::unordered_set<std::string>* currFuncs = nullptr; | ||
// Map functions to their modules to ensure no function is assigned to | ||
// multiple modules. | ||
std::unordered_map<std::string, std::string> funcModules; | ||
|
||
std::string line; | ||
bool newSection = true; | ||
while (std::getline(manifest, line)) { | ||
if (line.empty()) { | ||
newSection = true; | ||
continue; | ||
} | ||
if (newSection) { | ||
currModule = line; | ||
currFuncs = &moduleFuncs[line]; | ||
newSection = false; | ||
continue; | ||
} | ||
assert(currFuncs); | ||
currFuncs->insert(line); | ||
auto [it, inserted] = funcModules.insert({line, currModule}); | ||
if (!inserted && it->second != currModule) { | ||
Fatal() << "Function " << line << "cannot be assigned to module " | ||
<< currModule << "; it is already assigned to module " | ||
<< it->second << '\n'; | ||
} | ||
if (inserted && !options.quiet && !wasm.getFunctionOrNull(line)) { | ||
std::cerr << "warning: Function " << line << " does not exist\n"; | ||
} | ||
} | ||
|
||
ModuleSplitting::Config config; | ||
config.usePlaceholders = false; | ||
config.importNamespace = ""; | ||
config.minimizeNewExportNames = true; | ||
for (auto& func : wasm.functions) { | ||
config.primaryFuncs.insert(func->name); | ||
} | ||
for (auto& [mod, funcs] : moduleFuncs) { | ||
if (options.verbose) { | ||
std::cerr << "Splitting module " << mod << '\n'; | ||
} | ||
if (!options.quiet && funcs.empty()) { | ||
std::cerr << "warning: Module " << mod << " will be empty\n"; | ||
} | ||
for (auto& func : funcs) { | ||
config.primaryFuncs.erase(Name(func)); | ||
} | ||
auto splitResults = ModuleSplitting::splitFunctions(wasm, config); | ||
// TODO: symbolMap, placeholderMap, emitModuleNames | ||
// TODO: Support --emit-text and use .wast in that case. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe error on these for now? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Our special option validation that checks that each flag is allowed in the selected mode already takes care of that. |
||
auto moduleName = options.outPrefix + mod + ".wasm"; | ||
PassRunner runner(&*splitResults.secondary); | ||
runner.add("remove-unused-module-elements"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why is this needed here but not in single-split? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should probably add it to the normal split mode as well. In experiments with hundreds of relatively small modules, this was obviously very necessary. I guess it's harder to tell by visual inspection that it is necessary for larger secondary modules. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Makes sense. Maybe add a TODO for the normal mode? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just uploaded #6945 to do this for all secondary modules. |
||
runner.run(); | ||
writeModule(*splitResults.secondary, moduleName, options); | ||
} | ||
writeModule(wasm, options.output, options); | ||
} | ||
|
||
void mergeProfiles(const WasmSplitOptions& options) { | ||
// Read the initial profile. We will merge other profiles into this one. | ||
ProfileData data = readProfile(options.inputFiles[0]); | ||
|
@@ -503,6 +584,9 @@ int main(int argc, const char* argv[]) { | |
case WasmSplitOptions::Mode::Split: | ||
splitModule(options); | ||
break; | ||
case WasmSplitOptions::Mode::MultiSplit: | ||
multiSplitModule(options); | ||
break; | ||
case WasmSplitOptions::Mode::Instrument: | ||
instrumentModule(options); | ||
break; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not hard to figure these out, but some comments would be nice I think.