Skip to content

Commit 782277c

Browse files
committed
[SYCL] Enable parallel execution of llvm-foreach commands under an option
This improvement should speed up toolchain execution by launching underlying llvm-foreach commands in parallel. The feature can be enabled by passing '--parallel-exec' to the command line of llvm-foreach.
1 parent 282d1de commit 782277c

File tree

3 files changed

+50
-13
lines changed

3 files changed

+50
-13
lines changed

llvm/test/tools/llvm-foreach/llvm-foreach-lin.ll

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,17 @@
66
; RUN: echo "%t2.tgt" >> %t.list
77
; RUN: llvm-foreach --in-replace="{}" --in-file-list=%t.list -- echo "{}" > %t.res
88
; RUN: FileCheck < %t.res %s
9-
; CHECK: [[FIRST:.+1.tgt]]
10-
; CHECK: [[SECOND:.+2.tgt]]
9+
; RUN: llvm-foreach --parallel-exec --in-replace="{}" --in-file-list=%t.list -- echo "{}" > %t.res
10+
; RUN: FileCheck < %t.res %s
11+
; CHECK-DAG: [[FIRST:.+1.tgt]]
12+
; CHECK-DAG: [[SECOND:.+2.tgt]]
1113
;
1214
; RUN: llvm-foreach --in-replace="{}" --out-replace=%t --out-ext=out --in-file-list=%t.list --out-file-list=%t.out.list -- cp "{}" %t
1315
; RUN: FileCheck < %t.out.list %s --check-prefix=CHECK-LIST
14-
; CHECK-LIST: [[FIRST:.+\.out]]
15-
; CHECK-LIST: [[SECOND:.+\.out]]
16+
; RUN: llvm-foreach --parallel-exec --in-replace="{}" --out-replace=%t --out-ext=out --in-file-list=%t.list --out-file-list=%t.out.list -- cp "{}" %t
17+
; RUN: FileCheck < %t.out.list %s --check-prefix=CHECK-LIST
18+
; CHECK-LIST-DAG: [[FIRST:.+\.out]]
19+
; CHECK-LIST-DAG: [[SECOND:.+\.out]]
1620
; RUN: llvm-foreach --in-replace="{}" --in-file-list=%t.out.list -- FileCheck --input-file="{}" %s --check-prefix=CHECK-CONTENT
1721
; CHECK-CONTENT: Content of
1822

@@ -22,5 +26,7 @@
2226
; RUN: echo "%t4.tgt" >> %t1.list
2327
; RUN: llvm-foreach --in-replace="{}" --in-replace="inrep" --in-file-list=%t.list --in-file-list=%t1.list --out-increment="%t_out.prj" -- echo -first-part-of-arg={}.out -first-part-of-arg=inrep.out -another-arg=%t_out.prj > %t1.res
2428
; RUN: FileCheck < %t1.res %s --check-prefix=CHECK-DOUBLE-LISTS
25-
; CHECK-DOUBLE-LISTS: -first-part-of-arg=[[FIRST:.+1.tgt.out]] -first-part-of-arg=[[THIRD:.+3.tgt.out]] -another-arg={{.+}}_out.prj
26-
; CHECK-DOUBLE-LISTS: -first-part-of-arg=[[SECOND:.+2.tgt.out]] -first-part-of-arg=[[FOURTH:.+4.tgt.out]] -another-arg={{.+}}_out.prj_1
29+
; RUN: llvm-foreach --parallel-exec --in-replace="{}" --in-replace="inrep" --in-file-list=%t.list --in-file-list=%t1.list --out-increment="%t_out.prj" -- echo -first-part-of-arg={}.out -first-part-of-arg=inrep.out -another-arg=%t_out.prj > %t1.res
30+
; RUN: FileCheck < %t1.res %s --check-prefix=CHECK-DOUBLE-LISTS
31+
; CHECK-DOUBLE-LISTS-DAG: -first-part-of-arg=[[FIRST:.+1.tgt.out]] -first-part-of-arg=[[THIRD:.+3.tgt.out]] -another-arg={{.+}}_out.prj
32+
; CHECK-DOUBLE-LISTS-DAG: -first-part-of-arg=[[SECOND:.+2.tgt.out]] -first-part-of-arg=[[FOURTH:.+4.tgt.out]] -another-arg={{.+}}_out.prj_1

llvm/test/tools/llvm-foreach/llvm-foreach-win.ll

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,17 @@
66
; RUN: echo "%t2.tgt" >> %t.list
77
; RUN: llvm-foreach --in-replace="{}" --in-file-list=%t.list -- echo "{}" > %t.res
88
; RUN: FileCheck < %t.res %s
9-
; CHECK: [[FIRST:.+1.tgt]]
10-
; CHECK: [[SECOND:.+2.tgt]]
9+
; RUN: llvm-foreach --parallel-exec --in-replace="{}" --in-file-list=%t.list -- echo "{}" > %t.res
10+
; RUN: FileCheck < %t.res %s
11+
; CHECK-DAG: [[FIRST:.+1.tgt]]
12+
; CHECK-DAG: [[SECOND:.+2.tgt]]
1113
;
1214
; RUN: llvm-foreach --in-replace="{}" --out-replace=%t --out-ext=out --in-file-list=%t.list --out-file-list=%t.out.list -- xcopy /y "{}" %t
1315
; RUN: FileCheck < %t.out.list %s --check-prefix=CHECK-LIST
14-
; CHECK-LIST: [[FIRST:.+\.out]]
15-
; CHECK-LIST: [[SECOND:.+\.out]]
16+
; RUN: llvm-foreach --parallel-exec --in-replace="{}" --out-replace=%t --out-ext=out --in-file-list=%t.list --out-file-list=%t.out.list -- xcopy /y "{}" %t
17+
; RUN: FileCheck < %t.out.list %s --check-prefix=CHECK-LIST
18+
; CHECK-LIST-DAG: [[FIRST:.+\.out]]
19+
; CHECK-LIST-DAG: [[SECOND:.+\.out]]
1620
; RUN: llvm-foreach --in-replace="{}" --in-file-list=%t.out.list -- FileCheck --input-file="{}" %s --check-prefix=CHECK-CONTENT
1721
; CHECK-CONTENT: Content of
1822

@@ -22,5 +26,7 @@
2226
; RUN: echo "%t4.tgt" >> %t1.list
2327
; RUN: llvm-foreach --in-replace="{}" --in-replace="inrep" --in-file-list=%t.list --in-file-list=%t1.list --out-increment=%t_out.prj -- echo -first-part-of-arg={}.out -first-part-of-arg=inrep.out -another-arg=%t_out.prj > %t1.res
2428
; RUN: FileCheck < %t1.res %s --check-prefix=CHECK-DOUBLE-LISTS
25-
; CHECK-DOUBLE-LISTS: -first-part-of-arg=[[FIRST:.+1.tgt.out]] -first-part-of-arg=[[THIRD:.+3.tgt.out]] -another-arg={{.+}}_out.prj
26-
; CHECK-DOUBLE-LISTS: -first-part-of-arg=[[SECOND:.+2.tgt.out]] -first-part-of-arg=[[FOURTH:.+4.tgt.out]] -another-arg={{.+}}_out.prj_1
29+
; RUN: llvm-foreach --parallel-exec --in-replace="{}" --in-replace="inrep" --in-file-list=%t.list --in-file-list=%t1.list --out-increment=%t_out.prj -- echo -first-part-of-arg={}.out -first-part-of-arg=inrep.out -another-arg=%t_out.prj > %t1.res
30+
; RUN: FileCheck < %t1.res %s --check-prefix=CHECK-DOUBLE-LISTS
31+
; CHECK-DOUBLE-LISTS-DAG: -first-part-of-arg=[[FIRST:.+1.tgt.out]] -first-part-of-arg=[[THIRD:.+3.tgt.out]] -another-arg={{.+}}_out.prj
32+
; CHECK-DOUBLE-LISTS-DAG: -first-part-of-arg=[[SECOND:.+2.tgt.out]] -first-part-of-arg=[[FOURTH:.+4.tgt.out]] -another-arg={{.+}}_out.prj_1

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

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include "llvm/Support/Program.h"
2020
#include "llvm/Support/SystemUtils.h"
2121

22+
#include <list>
2223
#include <vector>
2324

2425
using namespace llvm;
@@ -69,6 +70,11 @@ static cl::opt<std::string> OutIncrement{
6970
"pass."),
7071
cl::init(""), cl::value_desc("R")};
7172

73+
static cl::opt<bool> ExecuteInParallel{
74+
"parallel-exec",
75+
cl::desc("Enable launching input commands in parallel mode"),
76+
cl::init(false)};
77+
7278
static void error(const Twine &Msg) {
7379
errs() << "llvm-foreach: " << Msg << '\n';
7480
exit(1);
@@ -170,6 +176,7 @@ int main(int argc, char **argv) {
170176
std::string IncOutArg;
171177
std::vector<std::string> ResInArgs(InReplaceArgs.size());
172178
std::string ResFileList = "";
179+
std::list<sys::ProcessInfo> CommandsStarted;
173180
for (size_t j = 0; j != FileLists[0].size(); ++j) {
174181
for (size_t i = 0; i < InReplaceArgs.size(); ++i) {
175182
ArgumentReplace CurReplace = InReplaceArgs[i];
@@ -222,7 +229,12 @@ int main(int argc, char **argv) {
222229
}
223230

224231
std::string ErrMsg;
225-
// TODO: Add possibility to execute commands in parallel.
232+
if (ExecuteInParallel) {
233+
CommandsStarted.emplace_back(sys::ExecuteNoWait(
234+
Prog, Args, /*Env=*/None, /*Redirects=*/None, /*MemoryLimit=*/0));
235+
continue;
236+
}
237+
226238
int Result =
227239
sys::ExecuteAndWait(Prog, Args, /*Env=*/None, /*Redirects=*/None,
228240
/*SecondsToWait=*/0, /*MemoryLimit=*/0, &ErrMsg);
@@ -232,6 +244,19 @@ int main(int argc, char **argv) {
232244
}
233245
}
234246

247+
// Wait for all commands to be executed.
248+
std::string ErrMsg;
249+
auto It = CommandsStarted.begin();
250+
while (It != CommandsStarted.end()) {
251+
sys::ProcessInfo WaitResult =
252+
sys::Wait(*It, 0, /*WaitUntilTerminates*/ true, &ErrMsg);
253+
if (WaitResult.ReturnCode != 0) {
254+
errs() << "llvm-foreach: " << ErrMsg << '\n';
255+
Res = WaitResult.ReturnCode;
256+
}
257+
It = CommandsStarted.erase(It);
258+
}
259+
235260
if (!OutputFileList.empty()) {
236261
OS.close();
237262
}

0 commit comments

Comments
 (0)