Skip to content

Commit

Permalink
[wasm-reduce] Add an option to save all interim working files as we r…
Browse files Browse the repository at this point in the history
…educe (#7154)

With this option, each time we reduce we save a file w.wasm.17 or such,
incrementing that counter. This is useful when debugging the reducer, but
might have more uses.
  • Loading branch information
kripken authored Dec 17, 2024
1 parent aa0550e commit c930094
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
31 changes: 29 additions & 2 deletions src/tools/wasm-reduce.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,9 @@ static size_t timeout = 2;
// default of enabling all features should work in most cases.
static std::string extraFlags = "-all";

// Whether to save all intermediate working files as we go.
static bool saveAllWorkingFiles = false;

struct ProgramResult {
int code;
std::string output;
Expand Down Expand Up @@ -231,6 +234,11 @@ ProgramResult expected;
// case we may try again but much later.
static std::unordered_set<Name> functionsWeTriedToRemove;

// The index of the working file we save, when saveAllWorkingFiles. We must
// store this globally so that the difference instances of Reducer do not
// overlap.
static size_t workingFileIndex = 0;

struct Reducer
: public WalkerPass<PostWalker<Reducer, UnifiedExpressionVisitor<Reducer>>> {
std::string command, test, working;
Expand Down Expand Up @@ -322,7 +330,7 @@ struct Reducer
if (ProgramResult(command) == expected) {
std::cerr << "| command \"" << currCommand
<< "\" succeeded, reduced size to " << newSize << '\n';
copy_file(test, working);
applyTestToWorking();
more = true;
oldSize = newSize;
}
Expand All @@ -335,6 +343,16 @@ struct Reducer
}
}

// Apply the test file to the working file, after we saw that it successfully
// reduced the testcase.
void applyTestToWorking() {
copy_file(test, working);

if (saveAllWorkingFiles) {
copy_file(working, working + '.' + std::to_string(workingFileIndex++));
}
}

// does one pass of slow and destructive reduction. returns whether it
// succeeded or not
// the criterion here is a logical change in the program. this may actually
Expand Down Expand Up @@ -471,7 +489,7 @@ struct Reducer

void noteReduction(size_t amount = 1) {
reduced += amount;
copy_file(test, working);
applyTestToWorking();
}

// tests a reduction on an arbitrary child
Expand Down Expand Up @@ -1302,6 +1320,15 @@ int main(int argc, const char* argv[]) {
extraFlags = argument;
std::cout << "|applying extraFlags: " << extraFlags << "\n";
})
.add("--save-all-working",
"-saw",
"Save all intermediate working files, as $WORKING.0, .1, .2 etc",
WasmReduceOption,
Options::Arguments::Zero,
[&](Options* o, const std::string& argument) {
saveAllWorkingFiles = true;
std::cout << "|saving all intermediate working files\n";
})
.add_positional(
"INFILE",
Options::Arguments::One,
Expand Down
3 changes: 3 additions & 0 deletions test/lit/help/wasm-reduce.test
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@
;; CHECK-NEXT: wasm-opt while reducing. (default:
;; CHECK-NEXT: --enable-all)
;; CHECK-NEXT:
;; CHECK-NEXT: --save-all-working,-saw Save all intermediate working files, as
;; CHECK-NEXT: $WORKING.0, .1, .2 etc
;; CHECK-NEXT:
;; CHECK-NEXT:
;; CHECK-NEXT: Tool options:
;; CHECK-NEXT: -------------
Expand Down

0 comments on commit c930094

Please # to comment.