|
38 | 38 |
|
39 | 39 | #include "comgr-compiler.h"
|
40 | 40 | #include "comgr-device-libs.h"
|
| 41 | +#include "comgr-diagnostic-handler.h" |
41 | 42 | #include "comgr-env.h"
|
42 | 43 | #include "lld/Common/CommonLinkerContext.h"
|
43 | 44 | #include "lld/Common/Driver.h"
|
|
51 | 52 | #include "clang/Driver/Tool.h"
|
52 | 53 | #include "clang/Frontend/CompilerInstance.h"
|
53 | 54 | #include "clang/Frontend/FrontendDiagnostic.h"
|
| 55 | +#include "clang/Frontend/TextDiagnosticPrinter.h" |
54 | 56 | #include "clang/FrontendTool/Utils.h"
|
55 | 57 | #include "llvm/Bitcode/BitcodeWriter.h"
|
56 | 58 | #include "llvm/IR/LLVMContext.h"
|
@@ -640,6 +642,82 @@ void logArgv(raw_ostream &OS, StringRef ProgramName,
|
640 | 642 | OS << '\n';
|
641 | 643 | OS.flush();
|
642 | 644 | }
|
| 645 | + |
| 646 | +amd_comgr_status_t executeCommand(const Command &Job, raw_ostream &LogS, |
| 647 | + DiagnosticOptions &DiagOpts) { |
| 648 | + TextDiagnosticPrinter DiagClient(LogS, &DiagOpts); |
| 649 | + IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs); |
| 650 | + DiagnosticsEngine Diags(DiagID, &DiagOpts, &DiagClient, false); |
| 651 | + |
| 652 | + auto Arguments = Job.getArguments(); |
| 653 | + SmallVector<const char *, 128> Argv; |
| 654 | + initializeCommandLineArgs(Argv); |
| 655 | + Argv.append(Arguments.begin(), Arguments.end()); |
| 656 | + Argv.push_back(nullptr); |
| 657 | + |
| 658 | + // By default clang driver will ask CC1 to leak memory. |
| 659 | + auto *IT = find(Argv, StringRef("-disable-free")); |
| 660 | + if (IT != Argv.end()) { |
| 661 | + Argv.erase(IT); |
| 662 | + } |
| 663 | + |
| 664 | + clearLLVMOptions(); |
| 665 | + |
| 666 | + if (Argv[1] == StringRef("-cc1")) { |
| 667 | + if (env::shouldEmitVerboseLogs()) { |
| 668 | + logArgv(LogS, "clang", Argv); |
| 669 | + } |
| 670 | + |
| 671 | + std::unique_ptr<CompilerInstance> Clang(new CompilerInstance()); |
| 672 | + Clang->setVerboseOutputStream(LogS); |
| 673 | + if (!Argv.back()) { |
| 674 | + Argv.pop_back(); |
| 675 | + } |
| 676 | + if (!CompilerInvocation::CreateFromArgs(Clang->getInvocation(), Argv, |
| 677 | + Diags)) { |
| 678 | + return AMD_COMGR_STATUS_ERROR; |
| 679 | + } |
| 680 | + // Internally this call refers to the invocation created above, so at |
| 681 | + // this point the DiagnosticsEngine should accurately reflect all user |
| 682 | + // requested configuration from Argv. |
| 683 | + Clang->createDiagnostics(&DiagClient, /* ShouldOwnClient */ false); |
| 684 | + if (!Clang->hasDiagnostics()) { |
| 685 | + return AMD_COMGR_STATUS_ERROR; |
| 686 | + } |
| 687 | + if (!ExecuteCompilerInvocation(Clang.get())) { |
| 688 | + return AMD_COMGR_STATUS_ERROR; |
| 689 | + } |
| 690 | + } else if (Argv[1] == StringRef("-cc1as")) { |
| 691 | + if (env::shouldEmitVerboseLogs()) { |
| 692 | + logArgv(LogS, "clang", Argv); |
| 693 | + } |
| 694 | + Argv.erase(Argv.begin() + 1); |
| 695 | + if (!Argv.back()) { |
| 696 | + Argv.pop_back(); |
| 697 | + } |
| 698 | + AssemblerInvocation Asm; |
| 699 | + if (!AssemblerInvocation::createFromArgs(Asm, Argv, Diags)) { |
| 700 | + return AMD_COMGR_STATUS_ERROR; |
| 701 | + } |
| 702 | + if (auto Status = parseLLVMOptions(Asm.LLVMArgs)) { |
| 703 | + return Status; |
| 704 | + } |
| 705 | + if (executeAssembler(Asm, Diags, LogS)) { |
| 706 | + return AMD_COMGR_STATUS_ERROR; |
| 707 | + } |
| 708 | + } else if (Job.getCreator().getName() == LinkerJobName) { |
| 709 | + if (env::shouldEmitVerboseLogs()) { |
| 710 | + logArgv(LogS, "lld", Argv); |
| 711 | + } |
| 712 | + if (auto Status = linkWithLLD(Arguments, LogS, LogS)) { |
| 713 | + return Status; |
| 714 | + } |
| 715 | + } else { |
| 716 | + return AMD_COMGR_STATUS_ERROR; |
| 717 | + } |
| 718 | + return AMD_COMGR_STATUS_SUCCESS; |
| 719 | +} |
| 720 | + |
643 | 721 | } // namespace
|
644 | 722 |
|
645 | 723 | amd_comgr_status_t
|
@@ -686,76 +764,13 @@ AMDGPUCompiler::executeInProcessDriver(ArrayRef<const char *> Args) {
|
686 | 764 | }
|
687 | 765 |
|
688 | 766 | std::unique_ptr<Compilation> C(TheDriver.BuildCompilation(Args));
|
689 |
| - if (!C) { |
690 |
| - return C->containsError() ? AMD_COMGR_STATUS_ERROR |
691 |
| - : AMD_COMGR_STATUS_SUCCESS; |
| 767 | + if (!C || C->containsError()) { |
| 768 | + return AMD_COMGR_STATUS_ERROR; |
692 | 769 | }
|
693 |
| - for (auto &Job : C->getJobs()) { |
694 |
| - auto Arguments = Job.getArguments(); |
695 |
| - SmallVector<const char *, 128> Argv; |
696 |
| - initializeCommandLineArgs(Argv); |
697 |
| - Argv.append(Arguments.begin(), Arguments.end()); |
698 |
| - Argv.push_back(nullptr); |
699 | 770 |
|
700 |
| - // By default clang driver will ask CC1 to leak memory. |
701 |
| - auto *IT = find(Argv, StringRef("-disable-free")); |
702 |
| - if (IT != Argv.end()) { |
703 |
| - Argv.erase(IT); |
704 |
| - } |
705 |
| - |
706 |
| - clearLLVMOptions(); |
707 |
| - |
708 |
| - if (Argv[1] == StringRef("-cc1")) { |
709 |
| - if (env::shouldEmitVerboseLogs()) { |
710 |
| - logArgv(LogS, "clang", Argv); |
711 |
| - } |
712 |
| - |
713 |
| - std::unique_ptr<CompilerInstance> Clang(new CompilerInstance()); |
714 |
| - Clang->setVerboseOutputStream(LogS); |
715 |
| - if (!Argv.back()) { |
716 |
| - Argv.pop_back(); |
717 |
| - } |
718 |
| - if (!CompilerInvocation::CreateFromArgs(Clang->getInvocation(), Argv, |
719 |
| - Diags)) { |
720 |
| - return AMD_COMGR_STATUS_ERROR; |
721 |
| - } |
722 |
| - // Internally this call refers to the invocation created above, so at |
723 |
| - // this point the DiagnosticsEngine should accurately reflect all user |
724 |
| - // requested configuration from Argv. |
725 |
| - Clang->createDiagnostics(DiagClient, /* ShouldOwnClient */ false); |
726 |
| - if (!Clang->hasDiagnostics()) { |
727 |
| - return AMD_COMGR_STATUS_ERROR; |
728 |
| - } |
729 |
| - if (!ExecuteCompilerInvocation(Clang.get())) { |
730 |
| - return AMD_COMGR_STATUS_ERROR; |
731 |
| - } |
732 |
| - } else if (Argv[1] == StringRef("-cc1as")) { |
733 |
| - if (env::shouldEmitVerboseLogs()) { |
734 |
| - logArgv(LogS, "clang", Argv); |
735 |
| - } |
736 |
| - Argv.erase(Argv.begin() + 1); |
737 |
| - if (!Argv.back()) { |
738 |
| - Argv.pop_back(); |
739 |
| - } |
740 |
| - AssemblerInvocation Asm; |
741 |
| - if (!AssemblerInvocation::createFromArgs(Asm, Argv, Diags)) { |
742 |
| - return AMD_COMGR_STATUS_ERROR; |
743 |
| - } |
744 |
| - if (auto Status = parseLLVMOptions(Asm.LLVMArgs)) { |
745 |
| - return Status; |
746 |
| - } |
747 |
| - if (executeAssembler(Asm, Diags, LogS)) { |
748 |
| - return AMD_COMGR_STATUS_ERROR; |
749 |
| - } |
750 |
| - } else if (Job.getCreator().getName() == LinkerJobName) { |
751 |
| - if (env::shouldEmitVerboseLogs()) { |
752 |
| - logArgv(LogS, "lld", Argv); |
753 |
| - } |
754 |
| - if (auto Status = linkWithLLD(Arguments, LogS, LogS)) { |
755 |
| - return Status; |
756 |
| - } |
757 |
| - } else { |
758 |
| - return AMD_COMGR_STATUS_ERROR; |
| 771 | + for (auto &Job : C->getJobs()) { |
| 772 | + if (auto Status = executeCommand(Job, LogS, *DiagOpts)) { |
| 773 | + return Status; |
759 | 774 | }
|
760 | 775 | }
|
761 | 776 | return AMD_COMGR_STATUS_SUCCESS;
|
@@ -1353,7 +1368,7 @@ amd_comgr_status_t AMDGPUCompiler::linkBitcodeToBitcode() {
|
1353 | 1368 | SMDiagnostic SMDiag;
|
1354 | 1369 | LLVMContext Context;
|
1355 | 1370 | Context.setDiagnosticHandler(
|
1356 |
| - std::make_unique<AMDGPUCompilerDiagnosticHandler>(this), true); |
| 1371 | + std::make_unique<AMDGPUCompilerDiagnosticHandler>(this->LogS), true); |
1357 | 1372 |
|
1358 | 1373 | auto Composite = std::make_unique<llvm::Module>("llvm-link", Context);
|
1359 | 1374 | Linker L(*Composite);
|
@@ -1867,7 +1882,7 @@ amd_comgr_status_t AMDGPUCompiler::translateSpirvToBitcode() {
|
1867 | 1882 |
|
1868 | 1883 | LLVMContext Context;
|
1869 | 1884 | Context.setDiagnosticHandler(
|
1870 |
| - std::make_unique<AMDGPUCompilerDiagnosticHandler>(this), true); |
| 1885 | + std::make_unique<AMDGPUCompilerDiagnosticHandler>(this->LogS), true); |
1871 | 1886 |
|
1872 | 1887 | for (auto *Input : InSet->DataObjects) {
|
1873 | 1888 |
|
|
0 commit comments