Skip to content

Commit 2d610d1

Browse files
authored
[llvm-foreach] Avoid running llvm-foreach when waiting for child processes (#17260)
This resolves #15177 Currently, we have a 'while' loop that keeps checking for child processes to complete. This causes llvm-foreach to run and consume resources when its child processes are active. Instead, we can use blocking waits to wait for child processes. During blocking waits. parent process is idle and does not consume resources. I tested this on a fairly large program compilation (with AOT) and it seems to work as expected. No new tests are needed. Thanks --------- Signed-off-by: Sudarsanam, Arvind <[email protected]>
1 parent 7276c55 commit 2d610d1

File tree

1 file changed

+8
-22
lines changed

1 file changed

+8
-22
lines changed

llvm/tools/llvm-foreach/llvm-foreach.cpp

Lines changed: 8 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -92,25 +92,14 @@ static void error(std::error_code EC, const Twine &Prefix) {
9292
error(Prefix + ": " + EC.message());
9393
}
9494

95-
// With BlockingWait=false this function just goes through the all
96-
// submitted jobs to check if some of them have finished.
97-
int checkIfJobsAreFinished(std::list<sys::ProcessInfo> &JobsSubmitted,
98-
bool BlockingWait = true) {
95+
// This function returns 0 if all jobs have successfully completed..
96+
int checkIfJobsAreFinished(std::list<sys::ProcessInfo> &JobsSubmitted) {
9997
std::string ErrMsg;
10098
auto It = JobsSubmitted.begin();
10199
while (It != JobsSubmitted.end()) {
102-
sys::ProcessInfo WaitResult = sys::Wait(
103-
*It, /*SecondsToWait*/ BlockingWait ? std::nullopt : std::optional(0),
104-
&ErrMsg);
105-
106-
// Check if the job has finished (PID will be 0 if it's not).
107-
if (!BlockingWait && !WaitResult.Pid) {
108-
It++;
109-
continue;
110-
}
111-
assert(BlockingWait || WaitResult.Pid);
100+
sys::ProcessInfo WaitResult = sys::Wait(*It, std::nullopt, &ErrMsg);
101+
assert(WaitResult.Pid);
112102
It = JobsSubmitted.erase(It);
113-
114103
if (WaitResult.ReturnCode != 0) {
115104
errs() << "llvm-foreach: " << ErrMsg << '\n';
116105
return WaitResult.ReturnCode;
@@ -271,12 +260,10 @@ int main(int argc, char **argv) {
271260
IncOutArg += ("_" + Twine(j)).str();
272261
Args[OutIncrementArg.ArgNum] = IncOutArg;
273262
}
274-
275263
// Do not start execution of a new job until previous one(s) are finished,
276264
// if the maximum number of parallel workers is reached.
277-
while (JobsSubmitted.size() == JobsInParallel)
278-
if (int Result =
279-
checkIfJobsAreFinished(JobsSubmitted, /*BlockingWait*/ false))
265+
if (JobsSubmitted.size() == JobsInParallel)
266+
if (int Result = checkIfJobsAreFinished(JobsSubmitted))
280267
Res = Result;
281268

282269
JobsSubmitted.emplace_back(
@@ -285,9 +272,8 @@ int main(int argc, char **argv) {
285272
}
286273

287274
// Wait for all commands to be executed.
288-
while (!JobsSubmitted.empty())
289-
if (int Result =
290-
checkIfJobsAreFinished(JobsSubmitted, /*BlockingWait*/ true))
275+
if (!JobsSubmitted.empty())
276+
if (int Result = checkIfJobsAreFinished(JobsSubmitted))
291277
Res = Result;
292278

293279
if (!OutputFileList.empty()) {

0 commit comments

Comments
 (0)