MLIR 24.0.0git
RemarkStreamer.cpp
Go to the documentation of this file.
4#include "mlir/IR/Remarks.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
13namespace mlir::remark::detail {
14llvm::Expected<std::unique_ptr<MLIRRemarkStreamerBase>>
16 llvm::remarks::Format fmt) {
17 std::string errorMessage;
18 std::unique_ptr<llvm::ToolOutputFile> f = openOutputFile(path, &errorMessage);
19 if (!f)
20 return llvm::createStringError(errorMessage);
21
22 auto serOr = llvm::remarks::createRemarkSerializer(fmt, f->os());
23 if (!serOr)
24 return llvm::createFileError(path, serOr.takeError());
25
26 auto rs =
27 std::make_unique<llvm::remarks::RemarkStreamer>(std::move(*serOr), 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
36 if (!remarkStreamer->matchesFilter(remark.getCategoryName()))
37 return;
38
39 // First, convert the diagnostic to a remark.
40 llvm::remarks::Remark r = remark.generateRemark();
41 // Then, emit the remark through the serializer.
42 remarkStreamer->getSerializer().emit(r);
43}
44
46 if (file && remarkStreamer)
47 file->keep();
48}
49
51 if (!remarkStreamer)
52 return;
53 remarkStreamer->releaseSerializer();
54}
55} // namespace mlir::remark::detail
56
57namespace mlir::remark {
59 MLIRContext &ctx, StringRef path, llvm::remarks::Format fmt,
60 std::unique_ptr<detail::RemarkEmittingPolicyBase> remarkEmittingPolicy,
61 const RemarkCategories &cat, bool printAsEmitRemarks) {
62
65 if (!sOr)
66 return emitError(UnknownLoc::get(&ctx)) << llvm::toString(sOr.takeError());
67
68 return remark::enableOptimizationRemarks(ctx, std::move(*sOr),
69 std::move(remarkEmittingPolicy), cat,
70 printAsEmitRemarks);
71}
72
73} // namespace mlir::remark
MLIRContext is the top-level object for a collection of MLIR operations.
Definition MLIRContext.h:64
void streamOptimizationRemark(const Remark &remark) override
Stream an optimization remark to the underlying remark streamer.
static llvm::Expected< std::unique_ptr< MLIRRemarkStreamerBase > > createToFile(llvm::StringRef path, llvm::remarks::Format fmt)
Open path and create the serializer for fmt.
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.
std::unique_ptr< llvm::ToolOutputFile > openOutputFile(llvm::StringRef outputFilename, std::string *errorMessage=nullptr)
Open the file specified by its name for writing.
InFlightDiagnostic emitError(Location loc)
Utility method to emit an error message using this location.
Define an the set of categories to accept.
Definition Remarks.h:64