|
94 | 94 | #include "llvm/Transforms/Scalar.h"
|
95 | 95 | #include "llvm/Transforms/Scalar/DCE.h"
|
96 | 96 |
|
| 97 | +#include "llvm/CodeGen/MachineOptimizationRemarkEmitter.h" |
| 98 | +#include "llvm/IR/DiagnosticInfo.h" |
| 99 | +#include "llvm/IR/LLVMRemarkStreamer.h" |
| 100 | +#include "llvm/Support/ToolOutputFile.h" |
| 101 | + |
97 | 102 | #include <thread>
|
98 | 103 |
|
99 | 104 | #if HAVE_UNISTD_H
|
@@ -598,6 +603,38 @@ static void countStatsPostIRGen(UnifiedStatsReporter &Stats,
|
598 | 603 | }
|
599 | 604 | }
|
600 | 605 |
|
| 606 | +namespace { |
| 607 | + class SwiftDiagnosticHandler final : public llvm::DiagnosticHandler { |
| 608 | + |
| 609 | + public: |
| 610 | + SwiftDiagnosticHandler(const IRGenOptions &Opts) : IRGenOpts(Opts) {} |
| 611 | + |
| 612 | + bool handleDiagnostics(const llvm::DiagnosticInfo &DI) override { |
| 613 | + return true; |
| 614 | + } |
| 615 | + |
| 616 | + bool isAnalysisRemarkEnabled(StringRef PassName) const override { |
| 617 | + return IRGenOpts.AnnotateCondFailMessage && |
| 618 | + PassName == "annotation-remarks"; |
| 619 | + } |
| 620 | + bool isMissedOptRemarkEnabled(StringRef PassName) const override { |
| 621 | + return IRGenOpts.AnnotateCondFailMessage && |
| 622 | + PassName == "annotation-remarks"; |
| 623 | + } |
| 624 | + bool isPassedOptRemarkEnabled(StringRef PassName) const override { |
| 625 | + return IRGenOpts.AnnotateCondFailMessage && |
| 626 | + PassName == "annotation-remarks"; |
| 627 | + } |
| 628 | + |
| 629 | + bool isAnyRemarkEnabled() const override { |
| 630 | + return IRGenOpts.AnnotateCondFailMessage; |
| 631 | + } |
| 632 | + |
| 633 | + private: |
| 634 | + const IRGenOptions &IRGenOpts; |
| 635 | + }; |
| 636 | +} |
| 637 | + |
601 | 638 | /// Run the LLVM passes. In multi-threaded compilation this will be done for
|
602 | 639 | /// multiple LLVM modules in parallel.
|
603 | 640 | bool swift::performLLVM(const IRGenOptions &Opts,
|
@@ -666,6 +703,32 @@ bool swift::performLLVM(const IRGenOptions &Opts,
|
666 | 703 | assert(Opts.OutputKind == IRGenOutputKind::Module && "no output specified");
|
667 | 704 | }
|
668 | 705 |
|
| 706 | + std::string OptRemarksRecordFile; |
| 707 | + if (Opts.AnnotateCondFailMessage && !OutputFilename.empty()) { |
| 708 | + OptRemarksRecordFile = std::string(OutputFilename); |
| 709 | + OptRemarksRecordFile.append(".opt.yaml"); |
| 710 | + } |
| 711 | + |
| 712 | + auto &Ctxt = Module->getContext(); |
| 713 | + std::unique_ptr<llvm::DiagnosticHandler> OldDiagnosticHandler = |
| 714 | + Ctxt.getDiagnosticHandler(); |
| 715 | + Ctxt.setDiagnosticHandler(std::make_unique<SwiftDiagnosticHandler>(Opts)); |
| 716 | + |
| 717 | + llvm::Expected<std::unique_ptr<llvm::ToolOutputFile>> OptRecordFileOrErr = |
| 718 | + setupLLVMOptimizationRemarks(Ctxt, OptRemarksRecordFile.c_str(), "annotation-remarks", "yaml", |
| 719 | + false/*RemarksWithHotness*/, |
| 720 | + 0/*RemarksHotnessThreshold*/); |
| 721 | + |
| 722 | + if (Error E = OptRecordFileOrErr.takeError()) { |
| 723 | + diagnoseSync(Diags, DiagMutex, SourceLoc(), diag::error_opening_output, |
| 724 | + StringRef(OptRemarksRecordFile.c_str()), |
| 725 | + toString(std::move(E))); |
| 726 | + return true; |
| 727 | + } |
| 728 | + |
| 729 | + std::unique_ptr<llvm::ToolOutputFile> OptRecordFile = |
| 730 | + std::move(*OptRecordFileOrErr); |
| 731 | + |
669 | 732 | performLLVMOptimizations(Opts, Diags, DiagMutex, Module, TargetMachine,
|
670 | 733 | OutputFile ? &OutputFile->getOS() : nullptr);
|
671 | 734 |
|
@@ -695,9 +758,15 @@ bool swift::performLLVM(const IRGenOptions &Opts,
|
695 | 758 | }
|
696 | 759 | }
|
697 | 760 |
|
698 |
| - return compileAndWriteLLVM(Module, TargetMachine, Opts, Stats, Diags, |
| 761 | + auto res = compileAndWriteLLVM(Module, TargetMachine, Opts, Stats, Diags, |
699 | 762 | *OutputFile, DiagMutex,
|
700 | 763 | CASIDFile ? CASIDFile.get() : nullptr);
|
| 764 | + if (OptRecordFile) |
| 765 | + OptRecordFile->keep(); |
| 766 | + |
| 767 | + Ctxt.setDiagnosticHandler(std::move(OldDiagnosticHandler)); |
| 768 | + |
| 769 | + return res; |
701 | 770 | }
|
702 | 771 |
|
703 | 772 | bool swift::compileAndWriteLLVM(
|
|
0 commit comments