Skip to content

Commit 767c96f

Browse files
committed
Add setupLLVMOptimizationRemarks
1 parent ee57eed commit 767c96f

File tree

1 file changed

+70
-1
lines changed

1 file changed

+70
-1
lines changed

lib/IRGen/IRGen.cpp

Lines changed: 70 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,11 @@
9494
#include "llvm/Transforms/Scalar.h"
9595
#include "llvm/Transforms/Scalar/DCE.h"
9696

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+
97102
#include <thread>
98103

99104
#if HAVE_UNISTD_H
@@ -598,6 +603,38 @@ static void countStatsPostIRGen(UnifiedStatsReporter &Stats,
598603
}
599604
}
600605

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+
601638
/// Run the LLVM passes. In multi-threaded compilation this will be done for
602639
/// multiple LLVM modules in parallel.
603640
bool swift::performLLVM(const IRGenOptions &Opts,
@@ -666,6 +703,32 @@ bool swift::performLLVM(const IRGenOptions &Opts,
666703
assert(Opts.OutputKind == IRGenOutputKind::Module && "no output specified");
667704
}
668705

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+
669732
performLLVMOptimizations(Opts, Diags, DiagMutex, Module, TargetMachine,
670733
OutputFile ? &OutputFile->getOS() : nullptr);
671734

@@ -695,9 +758,15 @@ bool swift::performLLVM(const IRGenOptions &Opts,
695758
}
696759
}
697760

698-
return compileAndWriteLLVM(Module, TargetMachine, Opts, Stats, Diags,
761+
auto res = compileAndWriteLLVM(Module, TargetMachine, Opts, Stats, Diags,
699762
*OutputFile, DiagMutex,
700763
CASIDFile ? CASIDFile.get() : nullptr);
764+
if (OptRecordFile)
765+
OptRecordFile->keep();
766+
767+
Ctxt.setDiagnosticHandler(std::move(OldDiagnosticHandler));
768+
769+
return res;
701770
}
702771

703772
bool swift::compileAndWriteLLVM(

0 commit comments

Comments
 (0)