MLIR  22.0.0git
RemarkStreamer.cpp
Go to the documentation of this file.
2 #include "mlir/IR/MLIRContext.h"
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 
11 namespace mlir::remark::detail {
12 
13 FailureOr<std::unique_ptr<MLIRRemarkStreamerBase>>
14 LLVMRemarkStreamer::createToFile(llvm::StringRef path,
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 
60 namespace mlir::remark {
62  MLIRContext &ctx, StringRef path, llvm::remarks::Format fmt,
63  const RemarkCategories &cat, bool printAsEmitRemarks) {
64 
65  FailureOr<std::unique_ptr<detail::MLIRRemarkStreamerBase>> sOr =
67  if (failed(sOr))
68  return failure();
69 
70  return remark::enableOptimizationRemarks(ctx, std::move(*sOr), cat,
71  printAsEmitRemarks);
72 }
73 
74 } // namespace mlir::remark
MLIRContext is the top-level object for a collection of MLIR operations.
Definition: MLIRContext.h:63
Concrete streamer that writes LLVM optimization remarks to a file (YAML or Bitstream).
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.
llvm::StringRef getCategoryName() const
Definition: Remarks.h:145
llvm::remarks::Remark generateRemark() const
Diagnostic -> Remark.
Definition: Remarks.cpp:132
LogicalResult enableOptimizationRemarksWithLLVMStreamer(MLIRContext &ctx, StringRef filePath, llvm::remarks::Format fmt, const RemarkCategories &cat, bool printAsEmitRemarks=false)
Enable optimization remarks to a file with the given path and format.
detail::InFlightRemark failed(Location loc, RemarkOpts opts)
Report an optimization remark that failed.
Definition: Remarks.h:491
LogicalResult enableOptimizationRemarks(MLIRContext &ctx, std::unique_ptr< remark::detail::MLIRRemarkStreamerBase > streamer, const remark::RemarkCategories &cats, bool printAsEmitRemarks=false)
Setup remarks for the context.
Define an the set of categories to accept.
Definition: Remarks.h:31