@@ -667,37 +667,6 @@ Error LLJITBuilderState::prepareForConstruction() {
667
667
return JTMBOrErr.takeError ();
668
668
}
669
669
670
- if ((ES || EPC) && NumCompileThreads)
671
- return make_error<StringError>(
672
- " NumCompileThreads cannot be used with a custom ExecutionSession or "
673
- " ExecutorProcessControl" ,
674
- inconvertibleErrorCode ());
675
-
676
- #if !LLVM_ENABLE_THREADS
677
- if (NumCompileThreads)
678
- return make_error<StringError>(
679
- " LLJIT num-compile-threads is " + Twine (NumCompileThreads) +
680
- " but LLVM was compiled with LLVM_ENABLE_THREADS=Off" ,
681
- inconvertibleErrorCode ());
682
- #endif // !LLVM_ENABLE_THREADS
683
-
684
- bool ConcurrentCompilationSettingDefaulted = !SupportConcurrentCompilation;
685
- if (!SupportConcurrentCompilation) {
686
- #if LLVM_ENABLE_THREADS
687
- SupportConcurrentCompilation = NumCompileThreads || ES || EPC;
688
- #else
689
- SupportConcurrentCompilation = false ;
690
- #endif // LLVM_ENABLE_THREADS
691
- } else {
692
- #if !LLVM_ENABLE_THREADS
693
- if (*SupportConcurrentCompilation)
694
- return make_error<StringError>(
695
- " LLJIT concurrent compilation support requested, but LLVM was built "
696
- " with LLVM_ENABLE_THREADS=Off" ,
697
- inconvertibleErrorCode ());
698
- #endif // !LLVM_ENABLE_THREADS
699
- }
700
-
701
670
LLVM_DEBUG ({
702
671
dbgs () << " JITTargetMachineBuilder is "
703
672
<< JITTargetMachineBuilderPrinter (*JTMB, " " )
@@ -715,13 +684,11 @@ Error LLJITBuilderState::prepareForConstruction() {
715
684
<< (CreateCompileFunction ? " Yes" : " No" ) << " \n "
716
685
<< " Custom platform-setup function: "
717
686
<< (SetUpPlatform ? " Yes" : " No" ) << " \n "
718
- << " Support concurrent compilation: "
719
- << (*SupportConcurrentCompilation ? " Yes" : " No" );
720
- if (ConcurrentCompilationSettingDefaulted)
721
- dbgs () << " (defaulted based on ES / EPC)\n " ;
687
+ << " Number of compile threads: " << NumCompileThreads;
688
+ if (!NumCompileThreads)
689
+ dbgs () << " (code will be compiled on the execution thread)\n " ;
722
690
else
723
691
dbgs () << " \n " ;
724
- dbgs () << " Number of compile threads: " << NumCompileThreads << " \n " ;
725
692
});
726
693
727
694
// Create DL if not specified.
@@ -738,19 +705,7 @@ Error LLJITBuilderState::prepareForConstruction() {
738
705
dbgs () << " ExecutorProcessControl not specified, "
739
706
" Creating SelfExecutorProcessControl instance\n " ;
740
707
});
741
-
742
- std::unique_ptr<TaskDispatcher> D = nullptr ;
743
- #if LLVM_ENABLE_THREADS
744
- if (*SupportConcurrentCompilation) {
745
- std::optional<size_t > NumThreads = std ::nullopt ;
746
- if (NumCompileThreads)
747
- NumThreads = NumCompileThreads;
748
- D = std::make_unique<DynamicThreadPoolTaskDispatcher>(NumThreads);
749
- } else
750
- D = std::make_unique<InPlaceTaskDispatcher>();
751
- #endif // LLVM_ENABLE_THREADS
752
- if (auto EPCOrErr =
753
- SelfExecutorProcessControl::Create (nullptr , std::move (D), nullptr ))
708
+ if (auto EPCOrErr = SelfExecutorProcessControl::Create ())
754
709
EPC = std::move (*EPCOrErr);
755
710
else
756
711
return EPCOrErr.takeError ();
@@ -835,6 +790,8 @@ Error LLJITBuilderState::prepareForConstruction() {
835
790
}
836
791
837
792
LLJIT::~LLJIT () {
793
+ if (CompileThreads)
794
+ CompileThreads->wait ();
838
795
if (auto Err = ES->endSession ())
839
796
ES->reportError (std::move (Err));
840
797
}
@@ -959,8 +916,9 @@ LLJIT::createCompileFunction(LLJITBuilderState &S,
959
916
if (S.CreateCompileFunction )
960
917
return S.CreateCompileFunction (std::move (JTMB));
961
918
962
- // If using a custom EPC then use a ConcurrentIRCompiler by default.
963
- if (*S.SupportConcurrentCompilation )
919
+ // Otherwise default to creating a SimpleCompiler, or ConcurrentIRCompiler,
920
+ // depending on the number of threads requested.
921
+ if (S.NumCompileThreads > 0 )
964
922
return std::make_unique<ConcurrentIRCompiler>(std::move (JTMB));
965
923
966
924
auto TM = JTMB.createTargetMachine ();
@@ -1012,8 +970,21 @@ LLJIT::LLJIT(LLJITBuilderState &S, Error &Err)
1012
970
std::make_unique<IRTransformLayer>(*ES, *TransformLayer);
1013
971
}
1014
972
1015
- if (*S. SupportConcurrentCompilation )
973
+ if (S. NumCompileThreads > 0 ) {
1016
974
InitHelperTransformLayer->setCloneToNewContextOnEmit (true );
975
+ CompileThreads = std::make_unique<DefaultThreadPool>(
976
+ hardware_concurrency (S.NumCompileThreads ));
977
+ ES->setDispatchTask ([this ](std::unique_ptr<Task> T) {
978
+ // FIXME: We should be able to use move-capture here, but ThreadPool's
979
+ // AsyncTaskTys are std::functions rather than unique_functions
980
+ // (because MSVC's std::packaged_tasks don't support move-only types).
981
+ // Fix this when all the above gets sorted out.
982
+ CompileThreads->async ([UnownedT = T.release ()]() mutable {
983
+ std::unique_ptr<Task> T (UnownedT);
984
+ T->run ();
985
+ });
986
+ });
987
+ }
1017
988
1018
989
if (S.SetupProcessSymbolsJITDylib ) {
1019
990
if (auto ProcSymsJD = S.SetupProcessSymbolsJITDylib (*this )) {
@@ -1269,7 +1240,7 @@ LLLazyJIT::LLLazyJIT(LLLazyJITBuilderState &S, Error &Err) : LLJIT(S, Err) {
1269
1240
CODLayer = std::make_unique<CompileOnDemandLayer>(
1270
1241
*ES, *InitHelperTransformLayer, *LCTMgr, std::move (ISMBuilder));
1271
1242
1272
- if (*S. SupportConcurrentCompilation )
1243
+ if (S. NumCompileThreads > 0 )
1273
1244
CODLayer->setCloneToNewContextOnEmit (true );
1274
1245
}
1275
1246
0 commit comments