Skip to content

Commit be56ece

Browse files
committed
[llvm-reduce] Move code to check chunk to function, to enable reuse (NFC).
This patch moves the logic to clone and check a new chunk into a new function, to allow re-use in a follow-up patch that implements parallel reductions. Reviewed By: dblaikie Differential Revision: https://reviews.llvm.org/D113856
1 parent 75a0784 commit be56ece

File tree

1 file changed

+57
-42
lines changed

1 file changed

+57
-42
lines changed

llvm/tools/llvm-reduce/deltas/Delta.cpp

Lines changed: 57 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,58 @@ static bool increaseGranularity(std::vector<Chunk> &Chunks) {
116116
}
117117
return SplitOne;
118118
}
119+
// Check if \p ChunkToCheckForUninterestingness is interesting. Returns the
120+
// modified module if the chunk resulted in a reduction.
121+
template <typename T>
122+
static std::unique_ptr<ReducerWorkItem>
123+
CheckChunk(Chunk &ChunkToCheckForUninterestingness, TestRunner &Test,
124+
function_ref<void(Oracle &, T &)> ExtractChunksFromModule,
125+
std::set<Chunk> &UninterestingChunks,
126+
std::vector<Chunk> &ChunksStillConsideredInteresting) {
127+
// Take all of ChunksStillConsideredInteresting chunks, except those we've
128+
// already deemed uninteresting (UninterestingChunks) but didn't remove
129+
// from ChunksStillConsideredInteresting yet, and additionally ignore
130+
// ChunkToCheckForUninterestingness chunk.
131+
std::vector<Chunk> CurrentChunks;
132+
CurrentChunks.reserve(ChunksStillConsideredInteresting.size() -
133+
UninterestingChunks.size() - 1);
134+
copy_if(ChunksStillConsideredInteresting, std::back_inserter(CurrentChunks),
135+
[&](const Chunk &C) {
136+
return !UninterestingChunks.count(C) &&
137+
C != ChunkToCheckForUninterestingness;
138+
});
139+
140+
// Clone module before hacking it up..
141+
std::unique_ptr<ReducerWorkItem> Clone =
142+
cloneReducerWorkItem(Test.getProgram());
143+
// Generate Module with only Targets inside Current Chunks
144+
Oracle O(CurrentChunks);
145+
ExtractChunksFromModule(O, *Clone);
146+
147+
// Some reductions may result in invalid IR. Skip such reductions.
148+
if (verifyReducerWorkItem(*Clone, &errs())) {
149+
if (AbortOnInvalidReduction) {
150+
errs() << "Invalid reduction\n";
151+
exit(1);
152+
}
153+
errs() << " **** WARNING | reduction resulted in invalid module, "
154+
"skipping\n";
155+
return nullptr;
156+
}
157+
158+
errs() << "Ignoring: ";
159+
ChunkToCheckForUninterestingness.print();
160+
for (const Chunk &C : UninterestingChunks)
161+
C.print();
162+
163+
SmallString<128> CurrentFilepath;
164+
if (!isReduced(*Clone, Test, CurrentFilepath)) {
165+
// Program became non-reduced, so this chunk appears to be interesting.
166+
errs() << "\n";
167+
return nullptr;
168+
}
169+
return Clone;
170+
}
119171

120172
/// Runs the Delta Debugging algorithm, splits the code into chunks and
121173
/// reduces the amount of chunks that are considered interesting by the
@@ -162,52 +214,15 @@ void runDeltaPassInt(
162214
std::set<Chunk> UninterestingChunks;
163215
for (Chunk &ChunkToCheckForUninterestingness :
164216
reverse(ChunksStillConsideredInteresting)) {
165-
// Take all of ChunksStillConsideredInteresting chunks, except those we've
166-
// already deemed uninteresting (UninterestingChunks) but didn't remove
167-
// from ChunksStillConsideredInteresting yet, and additionally ignore
168-
// ChunkToCheckForUninterestingness chunk.
169-
std::vector<Chunk> CurrentChunks;
170-
CurrentChunks.reserve(ChunksStillConsideredInteresting.size() -
171-
UninterestingChunks.size() - 1);
172-
copy_if(ChunksStillConsideredInteresting,
173-
std::back_inserter(CurrentChunks), [&](const Chunk &C) {
174-
return !UninterestingChunks.count(C) &&
175-
C != ChunkToCheckForUninterestingness;
176-
});
177-
178-
// Clone module before hacking it up..
179-
std::unique_ptr<ReducerWorkItem> Clone =
180-
cloneReducerWorkItem(Test.getProgram());
181-
// Generate Module with only Targets inside Current Chunks
182-
Oracle O(CurrentChunks);
183-
ExtractChunksFromModule(O, *Clone);
184-
185-
// Some reductions may result in invalid IR. Skip such reductions.
186-
if (verifyReducerWorkItem(*Clone, &errs())) {
187-
if (AbortOnInvalidReduction) {
188-
errs() << "Invalid reduction\n";
189-
exit(1);
190-
}
191-
errs() << " **** WARNING | reduction resulted in invalid module, "
192-
"skipping\n";
193-
continue;
194-
}
195-
196-
errs() << "Ignoring: ";
197-
ChunkToCheckForUninterestingness.print();
198-
for (const Chunk &C : UninterestingChunks)
199-
C.print();
200-
201-
SmallString<128> CurrentFilepath;
202-
if (!isReduced(*Clone, Test, CurrentFilepath)) {
203-
// Program became non-reduced, so this chunk appears to be interesting.
204-
errs() << "\n";
217+
std::unique_ptr<ReducerWorkItem> Result = CheckChunk(
218+
ChunkToCheckForUninterestingness, Test, ExtractChunksFromModule,
219+
UninterestingChunks, ChunksStillConsideredInteresting);
220+
if (!Result)
205221
continue;
206-
}
207222

208223
FoundAtLeastOneNewUninterestingChunkWithCurrentGranularity = true;
209224
UninterestingChunks.insert(ChunkToCheckForUninterestingness);
210-
ReducedProgram = std::move(Clone);
225+
ReducedProgram = std::move(Result);
211226
errs() << " **** SUCCESS | lines: " << getLines(CurrentFilepath) << "\n";
212227
writeOutput(*ReducedProgram, "Saved new best reduction to ");
213228
}

0 commit comments

Comments
 (0)