Skip to content

Commit d74f5b1

Browse files
committed
Allow multiple noinline instances with different arguments.
1 parent 765c614 commit d74f5b1

File tree

4 files changed

+54
-14
lines changed

4 files changed

+54
-14
lines changed

src/pass.h

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,11 @@ struct PassRegistry {
3838

3939
using Creator = std::function<Pass*()>;
4040

41-
void registerPass(const char* name, const char* description, Creator create);
41+
void registerPass(const char* name,
42+
const char* description,
43+
Creator create,
44+
bool allowMultipleInstancesWithArgs = false);
45+
;
4246
// Register a pass that's used for internal testing. These passes do not show
4347
// up in --help.
4448
void
@@ -47,6 +51,7 @@ struct PassRegistry {
4751
std::vector<std::string> getRegisteredNames();
4852
std::string getPassDescription(std::string name);
4953
bool isPassHidden(std::string name);
54+
bool doesPassAllowMultipleInstancesWithArgs(std::string name);
5055

5156
private:
5257
void registerPasses();
@@ -55,9 +60,14 @@ struct PassRegistry {
5560
std::string description;
5661
Creator create;
5762
bool hidden;
63+
bool allowMultipleInstancesWithArgs;
5864
PassInfo() = default;
59-
PassInfo(std::string description, Creator create, bool hidden = false)
60-
: description(description), create(create), hidden(hidden) {}
65+
PassInfo(std::string description,
66+
Creator create,
67+
bool hidden = false,
68+
bool allowMultipleInstancesWithArgs = false)
69+
: description(description), create(create), hidden(hidden),
70+
allowMultipleInstancesWithArgs(allowMultipleInstancesWithArgs) {}
6171
};
6272
std::map<std::string, PassInfo> passInfos;
6373
};
@@ -322,9 +332,8 @@ struct PassRunner {
322332
}
323333

324334
// Add a pass using its name.
325-
void add(std::string passName) {
326-
doAdd(PassRegistry::get()->createPass(passName));
327-
}
335+
void add(std::string passName,
336+
std::optional<std::string> passArg = std::optional<std::string>());
328337

329338
// Add a pass given an instance.
330339
void add(std::unique_ptr<Pass> pass) { doAdd(std::move(pass)); }
@@ -486,6 +495,8 @@ class Pass {
486495
// to imports must override this to return true.
487496
virtual bool addsEffects() { return false; }
488497

498+
void setPassArg(std::string value) { passArg = value; }
499+
489500
std::string name;
490501

491502
PassRunner* getPassRunner() { return runner; }
@@ -497,6 +508,8 @@ class Pass {
497508
PassOptions& getPassOptions() { return runner->options; }
498509

499510
protected:
511+
std::optional<std::string> passArg;
512+
500513
Pass() = default;
501514
Pass(const Pass&) = default;
502515
Pass(Pass&&) = default;

src/passes/NoInline.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,10 @@ struct NoInline : public Pass {
4848
NoInline(NoInlineMode mode) : mode(mode) {}
4949

5050
void run(Module* module) override {
51-
std::string pattern = getPassOptions().getArgument(
52-
name, "Usage usage: wasm-opt --" + name + "=WILDCARD");
51+
std::string pattern =
52+
passArg ? *passArg
53+
: getPassOptions().getArgument(
54+
name, "Usage usage: wasm-opt --" + name + "=WILDCARD");
5355

5456
for (auto& func : module->functions) {
5557
if (!String::wildcardMatch(pattern, func->name.toString())) {

src/passes/pass.cpp

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,11 @@ PassRegistry* PassRegistry::get() { return &singleton; }
4343

4444
void PassRegistry::registerPass(const char* name,
4545
const char* description,
46-
Creator create) {
46+
Creator create,
47+
bool allowMultipleInstancesWithArgs) {
4748
assert(passInfos.find(name) == passInfos.end());
48-
passInfos[name] = PassInfo(description, create);
49+
passInfos[name] =
50+
PassInfo(description, create, false, allowMultipleInstancesWithArgs);
4951
}
5052

5153
void PassRegistry::registerTestPass(const char* name,
@@ -83,6 +85,12 @@ bool PassRegistry::isPassHidden(std::string name) {
8385
return passInfos[name].hidden;
8486
}
8587

88+
bool PassRegistry::doesPassAllowMultipleInstancesWithArgs(std::string name) {
89+
assert(passInfos.find(name) != passInfos.end());
90+
91+
return passInfos[name].allowMultipleInstancesWithArgs;
92+
}
93+
8694
// PassRunner
8795

8896
void PassRegistry::registerPasses() {
@@ -300,7 +308,8 @@ void PassRegistry::registerPasses() {
300308
createMultiMemoryLoweringWithBoundsChecksPass);
301309
registerPass("nm", "name list", createNameListPass);
302310
registerPass("name-types", "(re)name all heap types", createNameTypesPass);
303-
registerPass("no-inline", "mark functions as no-inline", createNoInlinePass);
311+
registerPass(
312+
"no-inline", "mark functions as no-inline", createNoInlinePass, true);
304313
registerPass("no-full-inline",
305314
"mark functions as no-inline (for full inlining only)",
306315
createNoFullInlinePass);
@@ -708,6 +717,15 @@ void PassRunner::addDefaultGlobalOptimizationPrePasses() {
708717
// which can lead to more opportunities for global effects to matter.
709718
}
710719

720+
void PassRunner::add(std::string passName, std::optional<std::string> passArg) {
721+
auto pass = PassRegistry::get()->createPass(passName);
722+
if (passArg) {
723+
pass->setPassArg(*passArg);
724+
}
725+
726+
doAdd(std::move(pass));
727+
}
728+
711729
void PassRunner::addDefaultGlobalOptimizationPostPasses() {
712730
if (options.optimizeLevel >= 2 || options.shrinkLevel >= 1) {
713731
addIfNoDWARFIssues("dae-optimizing");

src/tools/optimization-options.h

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ struct OptimizationOptions : public ToolOptions {
4646
// The name of the pass to run.
4747
std::string name;
4848

49+
std::optional<std::string> argument;
50+
4951
// The optimize and shrink levels to run the pass with, if specified. If not
5052
// specified then the defaults are used.
5153
std::optional<int> optimizeLevel;
@@ -315,13 +317,18 @@ struct OptimizationOptions : public ToolOptions {
315317
// --foo --pass-arg=foo@ARG
316318
Options::Arguments::Optional,
317319
[this, p](Options*, const std::string& arg) {
320+
PassInfo info(p);
318321
if (!arg.empty()) {
319-
if (passOptions.arguments.count(p)) {
322+
if (passOptions.arguments.count(p) &&
323+
!PassRegistry::get()->doesPassAllowMultipleInstancesWithArgs(
324+
p)) {
320325
Fatal() << "Cannot pass multiple pass arguments to " << p;
321326
}
322327
passOptions.arguments[p] = arg;
328+
info.argument = arg;
323329
}
324-
passes.push_back(p);
330+
331+
passes.push_back(info);
325332
},
326333
PassRegistry::get()->isPassHidden(p));
327334
}
@@ -378,7 +385,7 @@ struct OptimizationOptions : public ToolOptions {
378385
passRunner.options.shrinkLevel = passOptions.shrinkLevel;
379386
} else {
380387
// This is a normal pass. Add it to the queue for execution.
381-
passRunner.add(pass.name);
388+
passRunner.add(pass.name, pass.argument);
382389

383390
// Normal passes do not set their own optimize/shrinkLevels.
384391
assert(!pass.optimizeLevel);

0 commit comments

Comments
 (0)