1 //===- llvm/IR/LLVMRemarkStreamer.h - Streamer for LLVM remarks--*- C++ -*-===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 // 9 // This file implements the conversion between IR Diagnostics and 10 // serializable remarks::Remark objects. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #ifndef LLVM_IR_LLVMREMARKSTREAMER_H 15 #define LLVM_IR_LLVMREMARKSTREAMER_H 16 17 #include "llvm/Remarks/Remark.h" 18 #include "llvm/Support/Error.h" 19 #include <memory> 20 #include <optional> 21 #include <string> 22 23 namespace llvm { 24 25 class DiagnosticInfoOptimizationBase; 26 class LLVMContext; 27 class ToolOutputFile; 28 namespace remarks { 29 class RemarkStreamer; 30 } 31 32 /// Streamer for LLVM remarks which has logic for dealing with DiagnosticInfo 33 /// objects. 34 class LLVMRemarkStreamer { 35 remarks::RemarkStreamer &RS; 36 /// Convert diagnostics into remark objects. 37 /// The lifetime of the members of the result is bound to the lifetime of 38 /// the LLVM diagnostics. 39 remarks::Remark toRemark(const DiagnosticInfoOptimizationBase &Diag) const; 40 41 public: LLVMRemarkStreamer(remarks::RemarkStreamer & RS)42 LLVMRemarkStreamer(remarks::RemarkStreamer &RS) : RS(RS) {} 43 /// Emit a diagnostic through the streamer. 44 void emit(const DiagnosticInfoOptimizationBase &Diag); 45 }; 46 47 template <typename ThisError> 48 struct LLVMRemarkSetupErrorInfo : public ErrorInfo<ThisError> { 49 std::string Msg; 50 std::error_code EC; 51 LLVMRemarkSetupErrorInfoLLVMRemarkSetupErrorInfo52 LLVMRemarkSetupErrorInfo(Error E) { 53 handleAllErrors(std::move(E), [&](const ErrorInfoBase &EIB) { 54 Msg = EIB.message(); 55 EC = EIB.convertToErrorCode(); 56 }); 57 } 58 logLLVMRemarkSetupErrorInfo59 void log(raw_ostream &OS) const override { OS << Msg; } convertToErrorCodeLLVMRemarkSetupErrorInfo60 std::error_code convertToErrorCode() const override { return EC; } 61 }; 62 63 struct LLVMRemarkSetupFileError 64 : LLVMRemarkSetupErrorInfo<LLVMRemarkSetupFileError> { 65 static char ID; 66 using LLVMRemarkSetupErrorInfo< 67 LLVMRemarkSetupFileError>::LLVMRemarkSetupErrorInfo; 68 }; 69 70 struct LLVMRemarkSetupPatternError 71 : LLVMRemarkSetupErrorInfo<LLVMRemarkSetupPatternError> { 72 static char ID; 73 using LLVMRemarkSetupErrorInfo< 74 LLVMRemarkSetupPatternError>::LLVMRemarkSetupErrorInfo; 75 }; 76 77 struct LLVMRemarkSetupFormatError 78 : LLVMRemarkSetupErrorInfo<LLVMRemarkSetupFormatError> { 79 static char ID; 80 using LLVMRemarkSetupErrorInfo< 81 LLVMRemarkSetupFormatError>::LLVMRemarkSetupErrorInfo; 82 }; 83 84 /// Setup optimization remarks that output to a file. 85 Expected<std::unique_ptr<ToolOutputFile>> 86 setupLLVMOptimizationRemarks(LLVMContext &Context, StringRef RemarksFilename, 87 StringRef RemarksPasses, StringRef RemarksFormat, 88 bool RemarksWithHotness, 89 std::optional<uint64_t> RemarksHotnessThreshold = 0); 90 91 /// Setup optimization remarks that output directly to a raw_ostream. 92 /// \p OS is managed by the caller and should be open for writing as long as \p 93 /// Context is streaming remarks to it. 94 Error setupLLVMOptimizationRemarks( 95 LLVMContext &Context, raw_ostream &OS, StringRef RemarksPasses, 96 StringRef RemarksFormat, bool RemarksWithHotness, 97 std::optional<uint64_t> RemarksHotnessThreshold = 0); 98 99 } // end namespace llvm 100 101 #endif // LLVM_IR_LLVMREMARKSTREAMER_H 102