| 1 | #include "mlir/Remark/RemarkStreamer.h" |
| 2 | #include "mlir/IR/Diagnostics.h" |
| 3 | #include "mlir/IR/MLIRContext.h" |
| 4 | #include "mlir/IR/Remarks.h" |
| 5 | #include "mlir/Support/FileUtilities.h" |
| 6 | |
| 7 | #include "llvm/Remarks/RemarkSerializer.h" |
| 8 | #include "llvm/Remarks/RemarkStreamer.h" |
| 9 | #include "llvm/Support/Error.h" |
| 10 | #include "llvm/Support/FileSystem.h" |
| 11 | #include "llvm/Support/ToolOutputFile.h" |
| 12 | |
| 13 | namespace mlir::remark::detail { |
| 14 | llvm::Expected<std::unique_ptr<MLIRRemarkStreamerBase>> |
| 15 | LLVMRemarkStreamer::(llvm::StringRef path, |
| 16 | llvm::remarks::Format fmt) { |
| 17 | std::string errorMessage; |
| 18 | std::unique_ptr<llvm::ToolOutputFile> f = openOutputFile(outputFilename: path, errorMessage: &errorMessage); |
| 19 | if (!f) |
| 20 | return llvm::createStringError(S: errorMessage); |
| 21 | |
| 22 | auto serOr = llvm::remarks::createRemarkSerializer(RemarksFormat: fmt, OS&: f->os()); |
| 23 | if (!serOr) |
| 24 | return llvm::createFileError(F: path, E: serOr.takeError()); |
| 25 | |
| 26 | auto rs = |
| 27 | std::make_unique<llvm::remarks::RemarkStreamer>(args: std::move(*serOr), args&: path); |
| 28 | |
| 29 | auto impl = std::unique_ptr<LLVMRemarkStreamer>(new LLVMRemarkStreamer()); |
| 30 | impl->remarkStreamer = std::move(rs); |
| 31 | impl->file = std::move(f); |
| 32 | return std::unique_ptr<MLIRRemarkStreamerBase>(std::move(impl)); |
| 33 | } |
| 34 | |
| 35 | void LLVMRemarkStreamer::(const Remark &) { |
| 36 | std::scoped_lock<std::mutex> lock(mutex); |
| 37 | if (!remarkStreamer->matchesFilter(Str: remark.getCategoryName())) |
| 38 | return; |
| 39 | |
| 40 | // First, convert the diagnostic to a remark. |
| 41 | llvm::remarks::Remark r = remark.generateRemark(); |
| 42 | // Then, emit the remark through the serializer. |
| 43 | remarkStreamer->getSerializer().emit(Remark: r); |
| 44 | } |
| 45 | |
| 46 | LLVMRemarkStreamer::() { |
| 47 | if (file && remarkStreamer) |
| 48 | file->keep(); |
| 49 | } |
| 50 | |
| 51 | void LLVMRemarkStreamer::() { |
| 52 | if (!remarkStreamer) |
| 53 | return; |
| 54 | remarkStreamer->releaseSerializer(); |
| 55 | } |
| 56 | } // namespace mlir::remark::detail |
| 57 | |
| 58 | namespace mlir::remark { |
| 59 | LogicalResult ( |
| 60 | MLIRContext &ctx, StringRef path, llvm::remarks::Format fmt, |
| 61 | std::unique_ptr<detail::RemarkEmittingPolicyBase> , |
| 62 | const RemarkCategories &cat, bool ) { |
| 63 | |
| 64 | llvm::Expected<std::unique_ptr<detail::MLIRRemarkStreamerBase>> sOr = |
| 65 | detail::LLVMRemarkStreamer::createToFile(path, fmt); |
| 66 | if (!sOr) |
| 67 | return emitError(loc: UnknownLoc::get(context: &ctx)) << llvm::toString(E: sOr.takeError()); |
| 68 | |
| 69 | return remark::enableOptimizationRemarks(ctx, streamer: std::move(*sOr), |
| 70 | remarkEmittingPolicy: std::move(remarkEmittingPolicy), cats: cat, |
| 71 | printAsEmitRemarks); |
| 72 | } |
| 73 | |
| 74 | } // namespace mlir::remark |
| 75 | |