Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(2)

Side by Side Diff: tools/pnacl-llc/pnacl-llc.cpp

Issue 196793026: Add self-scheduling to threaded translation (vs static) (Closed) Base URL: http://git.chromium.org/native_client/pnacl-llvm.git@master
Patch Set: disallow copy and assign Created 6 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 //===-- pnacl-llc.cpp - PNaCl-specific llc: pexe ---> nexe ---------------===// 1 //===-- pnacl-llc.cpp - PNaCl-specific llc: pexe ---> nexe ---------------===//
2 // 2 //
3 // The LLVM Compiler Infrastructure 3 // The LLVM Compiler Infrastructure
4 // 4 //
5 // This file is distributed under the University of Illinois Open Source 5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details. 6 // License. See LICENSE.TXT for details.
7 // 7 //
8 //===----------------------------------------------------------------------===// 8 //===----------------------------------------------------------------------===//
9 // 9 //
10 // pnacl-llc: the core of the PNaCl translator, compiling a pexe into a nexe. 10 // pnacl-llc: the core of the PNaCl translator, compiling a pexe into a nexe.
(...skipping 27 matching lines...) Expand all
38 #include "llvm/Support/PrettyStackTrace.h" 38 #include "llvm/Support/PrettyStackTrace.h"
39 #include "llvm/Support/Signals.h" 39 #include "llvm/Support/Signals.h"
40 #include "llvm/Support/SourceMgr.h" 40 #include "llvm/Support/SourceMgr.h"
41 #include "llvm/Support/StreamableMemoryObject.h" 41 #include "llvm/Support/StreamableMemoryObject.h"
42 #include "llvm/Support/TargetRegistry.h" 42 #include "llvm/Support/TargetRegistry.h"
43 #include "llvm/Support/TargetSelect.h" 43 #include "llvm/Support/TargetSelect.h"
44 #include "llvm/Support/ToolOutputFile.h" 44 #include "llvm/Support/ToolOutputFile.h"
45 #include "llvm/Target/TargetLibraryInfo.h" 45 #include "llvm/Target/TargetLibraryInfo.h"
46 #include "llvm/Target/TargetMachine.h" 46 #include "llvm/Target/TargetMachine.h"
47 #include "llvm/Transforms/NaCl.h" 47 #include "llvm/Transforms/NaCl.h"
48 #include "ThreadedFunctionQueue.h"
48 #include "ThreadedStreamingCache.h" 49 #include "ThreadedStreamingCache.h"
49 #include <pthread.h> 50 #include <pthread.h>
50 #include <memory> 51 #include <memory>
51 52
52
53 using namespace llvm; 53 using namespace llvm;
54 54
55 // NOTE: When __native_client__ is defined it means pnacl-llc is built as a 55 // NOTE: When __native_client__ is defined it means pnacl-llc is built as a
56 // sandboxed translator (from pnacl-llc.pexe to pnacl-llc.nexe). In this mode 56 // sandboxed translator (from pnacl-llc.pexe to pnacl-llc.nexe). In this mode
57 // it uses SRPC operations instead of direct OS intefaces. 57 // it uses SRPC operations instead of direct OS intefaces.
58 #if defined(__native_client__) 58 #if defined(__native_client__)
59 int srpc_main(int argc, char **argv); 59 int srpc_main(int argc, char **argv);
60 int getObjectFileFD(unsigned index); 60 int getObjectFileFD(unsigned index);
61 DataStreamer *getNaClBitcodeStreamer(); 61 DataStreamer *getNaClBitcodeStreamer();
62 62
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after
124 124
125 cl::opt<bool> 125 cl::opt<bool>
126 DisableSimplifyLibCalls("disable-simplify-libcalls", 126 DisableSimplifyLibCalls("disable-simplify-libcalls",
127 cl::desc("Disable simplify-libcalls"), 127 cl::desc("Disable simplify-libcalls"),
128 cl::init(false)); 128 cl::init(false));
129 129
130 cl::opt<unsigned> 130 cl::opt<unsigned>
131 SplitModuleCount("split-module", 131 SplitModuleCount("split-module",
132 cl::desc("Split PNaCl module"), cl::init(1U)); 132 cl::desc("Split PNaCl module"), cl::init(1U));
133 133
134 enum SplitModuleSchedulerKind {
135 SplitModuleDynamic,
136 SplitModuleStatic
137 };
138
139 cl::opt<SplitModuleSchedulerKind>
140 SplitModuleSched(
141 "split-module-sched",
142 cl::desc("Choose thread scheduler for split module compilation."),
143 cl::values(
144 clEnumValN(SplitModuleDynamic, "dynamic",
145 "Dynamic thread scheduling (default)"),
146 clEnumValN(SplitModuleStatic, "static",
147 "Static thread scheduling"),
148 clEnumValEnd),
149 cl::init(SplitModuleDynamic));
150
134 /// Compile the module provided to pnacl-llc. The file name for reading the 151 /// Compile the module provided to pnacl-llc. The file name for reading the
135 /// module and other options are taken from globals populated by command-line 152 /// module and other options are taken from globals populated by command-line
136 /// option parsing. 153 /// option parsing.
137 static int compileModule(StringRef ProgramName); 154 static int compileModule(StringRef ProgramName);
138 155
139 #if !defined(__native_client__) 156 #if !defined(__native_client__)
140 // GetFileNameRoot - Helper function to get the basename of a filename. 157 // GetFileNameRoot - Helper function to get the basename of a filename.
141 static std::string 158 static std::string
142 GetFileNameRoot(StringRef InputFilename) { 159 GetFileNameRoot(StringRef InputFilename) {
143 std::string IFN = InputFilename; 160 std::string IFN = InputFilename;
(...skipping 184 matching lines...) Expand 10 before | Expand all | Expand 10 after
328 // Err.print is prettier, so use it for the non-sandboxed translator. 345 // Err.print is prettier, so use it for the non-sandboxed translator.
329 Err.print(ProgramName.data(), errs()); 346 Err.print(ProgramName.data(), errs());
330 return NULL; 347 return NULL;
331 #endif 348 #endif
332 } 349 }
333 return M; 350 return M;
334 } 351 }
335 352
336 static int runCompilePasses(Module *mod, 353 static int runCompilePasses(Module *mod,
337 unsigned ModuleIndex, 354 unsigned ModuleIndex,
355 ThreadedFunctionQueue *FuncQueue,
338 const Triple &TheTriple, 356 const Triple &TheTriple,
339 TargetMachine &Target, 357 TargetMachine &Target,
340 StringRef ProgramName, 358 StringRef ProgramName,
341 formatted_raw_ostream &FOS){ 359 formatted_raw_ostream &FOS){
342 // Add declarations for external functions required by PNaCl. The 360 // Add declarations for external functions required by PNaCl. The
343 // ResolvePNaClIntrinsics function pass running during streaming 361 // ResolvePNaClIntrinsics function pass running during streaming
344 // depends on these declarations being in the module. 362 // depends on these declarations being in the module.
345 OwningPtr<ModulePass> AddPNaClExternalDeclsPass( 363 OwningPtr<ModulePass> AddPNaClExternalDeclsPass(
346 createAddPNaClExternalDeclsPass()); 364 createAddPNaClExternalDeclsPass());
347 AddPNaClExternalDeclsPass->runOnModule(*mod); 365 AddPNaClExternalDeclsPass->runOnModule(*mod);
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after
425 /* DisableVerify */ true)) { 443 /* DisableVerify */ true)) {
426 errs() << ProgramName 444 errs() << ProgramName
427 << ": target does not support generation of this file type!\n"; 445 << ": target does not support generation of this file type!\n";
428 return 1; 446 return 1;
429 } 447 }
430 448
431 if (LazyBitcode) { 449 if (LazyBitcode) {
432 FunctionPassManager* P = static_cast<FunctionPassManager*>(PM.get()); 450 FunctionPassManager* P = static_cast<FunctionPassManager*>(PM.get());
433 P->doInitialization(); 451 P->doInitialization();
434 unsigned FuncIndex = 0; 452 unsigned FuncIndex = 0;
435 for (Module::iterator I = mod->begin(), E = mod->end(); I != E; ++I) { 453 if (SplitModuleSched == SplitModuleStatic) {
JF 2014/03/19 04:47:05 It would be nicer if this were a switch on SplitMo
jvoung (off chromium) 2014/03/19 18:44:37 Done.
436 if (FuncIndex++ % SplitModuleCount == ModuleIndex) { 454 for (Module::iterator I = mod->begin(), E = mod->end(); I != E; ++I) {
437 P->run(*I); 455 if (FuncQueue->GrabFunctionStatic(FuncIndex, ModuleIndex)) {
438 CheckABIVerifyErrors(ABIErrorReporter, "Function " + I->getName()); 456 P->run(*I);
439 I->Dematerialize(); 457 CheckABIVerifyErrors(ABIErrorReporter, "Function " + I->getName());
458 I->Dematerialize();
459 }
460 ++FuncIndex;
461 }
462 } else {
463 unsigned ChunkSize = 0;
464 for (Module::iterator I = mod->begin(), E = mod->end(); I != E; ) {
465 ChunkSize = FuncQueue->RecommendedChunkSize();
466 unsigned NextIndex;
467 bool grabbed = FuncQueue->GrabFunctionDynamic(FuncIndex, ChunkSize,
468 NextIndex);
469 if (grabbed) {
470 while (FuncIndex < NextIndex && I != E) {
JF 2014/03/19 04:47:05 Can it happen that I == E? Shouldn't we have faile
jvoung (off chromium) 2014/03/19 18:44:37 It happened back when I was experimenting with Chu
jvoung (off chromium) 2014/03/20 15:50:31 Looks like not all threads agree on how many funct
471 P->run(*I);
472 CheckABIVerifyErrors(ABIErrorReporter, "Function " + I->getName());
473 I->Dematerialize();
474 ++FuncIndex;
475 ++I;
476 }
477 } else {
478 while (FuncIndex < NextIndex && I != E) {
479 ++FuncIndex;
480 ++I;
481 }
482 }
440 } 483 }
441 } 484 }
442 P->doFinalization(); 485 P->doFinalization();
443 } else { 486 } else {
444 static_cast<PassManager*>(PM.get())->run(*mod); 487 static_cast<PassManager*>(PM.get())->run(*mod);
445 } 488 }
446 return 0; 489 return 0;
447 } 490 }
448 491
449 492
450 static int compileSplitModule(const TargetOptions &Options, 493 static int compileSplitModule(const TargetOptions &Options,
451 const Triple &TheTriple, 494 const Triple &TheTriple,
452 const Target *TheTarget, 495 const Target *TheTarget,
453 const std::string &FeaturesStr, 496 const std::string &FeaturesStr,
454 CodeGenOpt::Level OLvl, 497 CodeGenOpt::Level OLvl,
455 const StringRef &ProgramName, 498 const StringRef &ProgramName,
456 Module *GlobalModule, 499 Module *GlobalModule,
457 StreamingMemoryObject *StreamingObject, 500 StreamingMemoryObject *StreamingObject,
458 unsigned ModuleIndex) { 501 unsigned ModuleIndex,
502 ThreadedFunctionQueue *FuncQueue) {
459 std::auto_ptr<TargetMachine> 503 std::auto_ptr<TargetMachine>
460 target(TheTarget->createTargetMachine(TheTriple.getTriple(), 504 target(TheTarget->createTargetMachine(TheTriple.getTriple(),
461 MCPU, FeaturesStr, Options, 505 MCPU, FeaturesStr, Options,
462 RelocModel, CMModel, OLvl)); 506 RelocModel, CMModel, OLvl));
463 assert(target.get() && "Could not allocate target machine!"); 507 assert(target.get() && "Could not allocate target machine!");
464 TargetMachine &Target = *target.get(); 508 TargetMachine &Target = *target.get();
465 // Override default to generate verbose assembly. 509 // Override default to generate verbose assembly.
466 Target.setAsmVerbosityDefault(true); 510 Target.setAsmVerbosityDefault(true);
467 if (RelaxAll) { 511 if (RelaxAll) {
468 if (FileType != TargetMachine::CGFT_ObjectFile) 512 if (FileType != TargetMachine::CGFT_ObjectFile)
(...skipping 19 matching lines...) Expand all
488 532
489 mod->setTargetTriple(Triple::normalize(UserDefinedTriple)); 533 mod->setTargetTriple(Triple::normalize(UserDefinedTriple));
490 { 534 {
491 #if !defined(__native_client__) 535 #if !defined(__native_client__)
492 // Figure out where we are going to send the output. 536 // Figure out where we are going to send the output.
493 std::string N(OutputFilename); 537 std::string N(OutputFilename);
494 raw_string_ostream OutFileName(N); 538 raw_string_ostream OutFileName(N);
495 if (ModuleIndex > 0) 539 if (ModuleIndex > 0)
496 OutFileName << ".module" << ModuleIndex; 540 OutFileName << ".module" << ModuleIndex;
497 OwningPtr<tool_output_file> Out 541 OwningPtr<tool_output_file> Out
498 (GetOutputStream(TheTarget->getName(), TheTriple.getOS(), 542 (GetOutputStream(TheTarget->getName(), TheTriple.getOS(),
499 OutFileName.str())); 543 OutFileName.str()));
500 if (!Out) return 1; 544 if (!Out) return 1;
501 formatted_raw_ostream FOS(Out->os()); 545 formatted_raw_ostream FOS(Out->os());
502 #else 546 #else
503 raw_fd_ostream ROS(getObjectFileFD(ModuleIndex), true); 547 raw_fd_ostream ROS(getObjectFileFD(ModuleIndex), true);
504 ROS.SetBufferSize(1 << 20); 548 ROS.SetBufferSize(1 << 20);
505 formatted_raw_ostream FOS(ROS); 549 formatted_raw_ostream FOS(ROS);
506 #endif 550 #endif
507 int ret = runCompilePasses(mod, ModuleIndex, TheTriple, Target, ProgramName, 551 int ret = runCompilePasses(mod, ModuleIndex, FuncQueue,
552 TheTriple, Target, ProgramName,
508 FOS); 553 FOS);
509 if (ret) 554 if (ret)
510 return ret; 555 return ret;
511 #if defined (__native_client__) 556 #if defined (__native_client__)
512 FOS.flush(); 557 FOS.flush();
513 ROS.flush(); 558 ROS.flush();
514 #else 559 #else
515 // Declare success. 560 // Declare success.
516 Out->keep(); 561 Out->keep();
517 #endif // __native_client__ 562 #endif // __native_client__
518 } 563 }
519 return 0; 564 return 0;
520 } 565 }
521 566
522 struct ThreadData { 567 struct ThreadData {
523 const TargetOptions *Options; 568 const TargetOptions *Options;
524 const Triple *TheTriple; 569 const Triple *TheTriple;
525 const Target *TheTarget; 570 const Target *TheTarget;
526 std::string FeaturesStr; 571 std::string FeaturesStr;
527 CodeGenOpt::Level OLvl; 572 CodeGenOpt::Level OLvl;
528 std::string ProgramName; 573 std::string ProgramName;
529 Module *GlobalModule; 574 Module *GlobalModule;
530 StreamingMemoryObject *StreamingObject; 575 StreamingMemoryObject *StreamingObject;
531 unsigned ModuleIndex; 576 unsigned ModuleIndex;
577 ThreadedFunctionQueue *FuncQueue;
532 }; 578 };
533 579
534 580
535 static void *runCompileThread(void *arg) { 581 static void *runCompileThread(void *arg) {
536 struct ThreadData *Data = static_cast<ThreadData *>(arg); 582 struct ThreadData *Data = static_cast<ThreadData *>(arg);
537 int ret = compileSplitModule(*Data->Options, 583 int ret = compileSplitModule(*Data->Options,
538 *Data->TheTriple, 584 *Data->TheTriple,
539 Data->TheTarget, 585 Data->TheTarget,
540 Data->FeaturesStr, 586 Data->FeaturesStr,
541 Data->OLvl, 587 Data->OLvl,
542 Data->ProgramName, 588 Data->ProgramName,
543 Data->GlobalModule, 589 Data->GlobalModule,
544 Data->StreamingObject, 590 Data->StreamingObject,
545 Data->ModuleIndex); 591 Data->ModuleIndex,
592 Data->FuncQueue);
546 return reinterpret_cast<void *>(static_cast<intptr_t>(ret)); 593 return reinterpret_cast<void *>(static_cast<intptr_t>(ret));
547 } 594 }
548 595
549 static int compileModule(StringRef ProgramName) { 596 static int compileModule(StringRef ProgramName) {
550 // Use a new context instead of the global context for the main module. It mus t 597 // Use a new context instead of the global context for the main module. It mus t
551 // outlive the module object, declared below. We do this because 598 // outlive the module object, declared below. We do this because
552 // lib/CodeGen/PseudoSourceValue.cpp gets a type from the global context and 599 // lib/CodeGen/PseudoSourceValue.cpp gets a type from the global context and
553 // races with any other use of the context. Rather than doing an invasive 600 // races with any other use of the context. Rather than doing an invasive
554 // plumbing change to fix it, we work around it by using a new context here 601 // plumbing change to fix it, we work around it by using a new context here
555 // and leaving PseudoSourceValue as the only user of the global context. 602 // and leaving PseudoSourceValue as the only user of the global context.
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after
645 return 1; 692 return 1;
646 case ' ': break; 693 case ' ': break;
647 case '0': OLvl = CodeGenOpt::None; break; 694 case '0': OLvl = CodeGenOpt::None; break;
648 case '1': OLvl = CodeGenOpt::Less; break; 695 case '1': OLvl = CodeGenOpt::Less; break;
649 case '2': OLvl = CodeGenOpt::Default; break; 696 case '2': OLvl = CodeGenOpt::Default; break;
650 case '3': OLvl = CodeGenOpt::Aggressive; break; 697 case '3': OLvl = CodeGenOpt::Aggressive; break;
651 } 698 }
652 699
653 SmallVector<pthread_t, 4> Pthreads(SplitModuleCount); 700 SmallVector<pthread_t, 4> Pthreads(SplitModuleCount);
654 SmallVector<ThreadData, 4> ThreadDatas(SplitModuleCount); 701 SmallVector<ThreadData, 4> ThreadDatas(SplitModuleCount);
702 ThreadedFunctionQueue FuncQueue(mod.get(), SplitModuleCount);
655 703
656 if (SplitModuleCount == 1) { 704 if (SplitModuleCount == 1) {
705 // No need for dynamic scheduling with one thread.
706 SplitModuleSched = SplitModuleStatic;
657 return compileSplitModule(Options, TheTriple, TheTarget, FeaturesStr, 707 return compileSplitModule(Options, TheTriple, TheTarget, FeaturesStr,
658 OLvl, ProgramName, mod.get(), NULL, 0); 708 OLvl, ProgramName, mod.get(), NULL, 0,
709 &FuncQueue);
659 } 710 }
660 711
661 for(unsigned ModuleIndex = 0; ModuleIndex < SplitModuleCount; ++ModuleIndex) { 712 for(unsigned ModuleIndex = 0; ModuleIndex < SplitModuleCount; ++ModuleIndex) {
662 ThreadDatas[ModuleIndex].Options = &Options; 713 ThreadDatas[ModuleIndex].Options = &Options;
663 ThreadDatas[ModuleIndex].TheTriple = &TheTriple; 714 ThreadDatas[ModuleIndex].TheTriple = &TheTriple;
664 ThreadDatas[ModuleIndex].TheTarget = TheTarget; 715 ThreadDatas[ModuleIndex].TheTarget = TheTarget;
665 ThreadDatas[ModuleIndex].FeaturesStr = FeaturesStr; 716 ThreadDatas[ModuleIndex].FeaturesStr = FeaturesStr;
666 ThreadDatas[ModuleIndex].OLvl = OLvl; 717 ThreadDatas[ModuleIndex].OLvl = OLvl;
667 ThreadDatas[ModuleIndex].ProgramName = ProgramName.str(); 718 ThreadDatas[ModuleIndex].ProgramName = ProgramName.str();
668 ThreadDatas[ModuleIndex].GlobalModule = mod.get(); 719 ThreadDatas[ModuleIndex].GlobalModule = mod.get();
669 ThreadDatas[ModuleIndex].StreamingObject = StreamingObject.get(); 720 ThreadDatas[ModuleIndex].StreamingObject = StreamingObject.get();
670 ThreadDatas[ModuleIndex].ModuleIndex = ModuleIndex; 721 ThreadDatas[ModuleIndex].ModuleIndex = ModuleIndex;
722 ThreadDatas[ModuleIndex].FuncQueue = &FuncQueue;
671 if (pthread_create(&Pthreads[ModuleIndex], NULL, runCompileThread, 723 if (pthread_create(&Pthreads[ModuleIndex], NULL, runCompileThread,
672 &ThreadDatas[ModuleIndex])) { 724 &ThreadDatas[ModuleIndex])) {
673 report_fatal_error("Failed to create thread"); 725 report_fatal_error("Failed to create thread");
674 } 726 }
675 } 727 }
676 for(unsigned ModuleIndex = 0; ModuleIndex < SplitModuleCount; ++ModuleIndex) { 728 for(unsigned ModuleIndex = 0; ModuleIndex < SplitModuleCount; ++ModuleIndex) {
677 void *retval; 729 void *retval;
678 if (pthread_join(Pthreads[ModuleIndex], &retval)) 730 if (pthread_join(Pthreads[ModuleIndex], &retval))
679 report_fatal_error("Failed to join thread"); 731 report_fatal_error("Failed to join thread");
680 intptr_t ret = reinterpret_cast<intptr_t>(retval); 732 intptr_t ret = reinterpret_cast<intptr_t>(retval);
681 if (ret != 0) 733 if (ret != 0)
682 report_fatal_error("Thread returned nonzero"); 734 report_fatal_error("Thread returned nonzero");
683 } 735 }
684 return 0; 736 return 0;
685 } 737 }
686 738
687 int main(int argc, char **argv) { 739 int main(int argc, char **argv) {
688 #if defined(__native_client__) 740 #if defined(__native_client__)
689 return srpc_main(argc, argv); 741 return srpc_main(argc, argv);
690 #else 742 #else
691 return llc_main(argc, argv); 743 return llc_main(argc, argv);
692 #endif // __native_client__ 744 #endif // __native_client__
693 } 745 }
OLDNEW
« tools/pnacl-llc/ThreadedFunctionQueue.h ('K') | « tools/pnacl-llc/ThreadedFunctionQueue.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698