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(
24  fmt, llvm::remarks::SerializerMode::Separate, f->os());
25  if (!serOr) {
26  llvm::consumeError(serOr.takeError());
27  return failure();
28  }
29 
30  auto rs =
31  std::make_unique<llvm::remarks::RemarkStreamer>(std::move(*serOr), path);
32 
33  auto impl = std::unique_ptr<LLVMRemarkStreamer>(new LLVMRemarkStreamer());
34  impl->remarkStreamer = std::move(rs);
35  impl->file = std::move(f);
36  return std::unique_ptr<MLIRRemarkStreamerBase>(std::move(impl));
37 }
38 
40  if (!remarkStreamer->matchesFilter(remark.getCategoryName()))
41  return;
42 
43  // First, convert the diagnostic to a remark.
44  llvm::remarks::Remark r = remark.generateRemark();
45  // Then, emit the remark through the serializer.
46  remarkStreamer->getSerializer().emit(r);
47 }
48 
50  if (file && remarkStreamer)
51  file->keep();
52 }
53 } // namespace mlir::remark::detail
54 
55 namespace mlir::remark {
57  MLIRContext &ctx, StringRef path, llvm::remarks::Format fmt,
58  const RemarkCategories &cat, bool printAsEmitRemarks) {
59 
60  FailureOr<std::unique_ptr<detail::MLIRRemarkStreamerBase>> sOr =
62  if (failed(sOr))
63  return failure();
64 
65  return remark::enableOptimizationRemarks(ctx, std::move(*sOr), cat,
66  printAsEmitRemarks);
67 }
68 
69 } // 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