MLIR 22.0.0git
RemarkStreamer.cpp
Go to the documentation of this file.
3#include "mlir/IR/Remarks.h"
4
5#include "llvm/Remarks/RemarkSerializer.h"
6#include "llvm/Remarks/RemarkStreamer.h"
7#include "llvm/Support/Error.h"
8#include "llvm/Support/FileSystem.h"
9#include "llvm/Support/ToolOutputFile.h"
10
11namespace mlir::remark::detail {
12
13FailureOr<std::unique_ptr<MLIRRemarkStreamerBase>>
15 llvm::remarks::Format fmt) {
16 std::error_code ec;
17 // Use error_code ctor; YAML is text. (Bitstream also works fine here.)
18 auto f =
19 std::make_unique<llvm::ToolOutputFile>(path, ec, llvm::sys::fs::OF_Text);
20 if (ec)
21 return failure();
22
23 auto serOr = llvm::remarks::createRemarkSerializer(fmt, f->os());
24 if (!serOr) {
25 llvm::consumeError(serOr.takeError());
26 return failure();
27 }
28
29 auto rs =
30 std::make_unique<llvm::remarks::RemarkStreamer>(std::move(*serOr), path);
31
32 auto impl = std::unique_ptr<LLVMRemarkStreamer>(new LLVMRemarkStreamer());
33 impl->remarkStreamer = std::move(rs);
34 impl->file = std::move(f);
35 return std::unique_ptr<MLIRRemarkStreamerBase>(std::move(impl));
36}
37
39 if (!remarkStreamer->matchesFilter(remark.getCategoryName()))
40 return;
41
42 // First, convert the diagnostic to a remark.
43 llvm::remarks::Remark r = remark.generateRemark();
44 // Then, emit the remark through the serializer.
45 remarkStreamer->getSerializer().emit(r);
46}
47
49 if (file && remarkStreamer)
50 file->keep();
51}
52
54 if (!remarkStreamer)
55 return;
56 remarkStreamer->releaseSerializer();
57}
58} // namespace mlir::remark::detail
59
60namespace mlir::remark {
62 MLIRContext &ctx, StringRef path, llvm::remarks::Format fmt,
63 std::unique_ptr<detail::RemarkEmittingPolicyBase> remarkEmittingPolicy,
64 const RemarkCategories &cat, bool printAsEmitRemarks) {
65
66 FailureOr<std::unique_ptr<detail::MLIRRemarkStreamerBase>> sOr =
68 if (failed(sOr))
69 return failure();
70
71 return remark::enableOptimizationRemarks(ctx, std::move(*sOr),
72 std::move(remarkEmittingPolicy), cat,
73 printAsEmitRemarks);
74}
75
76} // namespace mlir::remark
MLIRContext is the top-level object for a collection of MLIR operations.
Definition MLIRContext.h:63
static FailureOr< std::unique_ptr< MLIRRemarkStreamerBase > > createToFile(llvm::StringRef path, llvm::remarks::Format fmt)
void streamOptimizationRemark(const Remark &remark) override
Stream an optimization remark to the underlying remark streamer.
detail::InFlightRemark failed(Location loc, RemarkOpts opts)
Report an optimization remark that failed.
Definition Remarks.h:561
LogicalResult enableOptimizationRemarks(MLIRContext &ctx, std::unique_ptr< remark::detail::MLIRRemarkStreamerBase > streamer, std::unique_ptr< remark::detail::RemarkEmittingPolicyBase > remarkEmittingPolicy, const remark::RemarkCategories &cats, bool printAsEmitRemarks=false)
Setup remarks for the context.
LogicalResult enableOptimizationRemarksWithLLVMStreamer(MLIRContext &ctx, StringRef filePath, llvm::remarks::Format fmt, std::unique_ptr< detail::RemarkEmittingPolicyBase > remarkEmittingPolicy, const RemarkCategories &cat, bool printAsEmitRemarks=false)
Enable optimization remarks to a file with the given path and format.
Define an the set of categories to accept.
Definition Remarks.h:30