Skip to content

Commit d7a3c03

Browse files
committed
address comments
1 parent fa75d31 commit d7a3c03

File tree

5 files changed

+168
-152
lines changed

5 files changed

+168
-152
lines changed

mlir/include/mlir/IR/MLIRContext.h

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include "mlir/Support/LLVM.h"
1313
#include "mlir/Support/TypeID.h"
1414
#include "llvm/ADT/ArrayRef.h"
15+
#include "llvm/Remarks/RemarkFormat.h"
1516
#include <functional>
1617
#include <memory>
1718
#include <vector>
@@ -214,7 +215,7 @@ class MLIRContext {
214215
DiagnosticEngine &getDiagEngine();
215216

216217
/// Returns the remark engine for this context.
217-
std::unique_ptr<RemarkEngine> &getRemarkEngine();
218+
RemarkEngine *getRemarkEngine();
218219

219220
/// Returns the storage uniquer used for creating affine constructs.
220221
StorageUniquer &getAffineUniquer();
@@ -249,11 +250,12 @@ class MLIRContext {
249250
/// (attributes, operations, types, etc.).
250251
llvm::hash_code getRegistryHash();
251252

252-
/// Set up optimization remarks for the context.
253+
/// Setup optimization remarks for the context.
253254
void setupOptimizationRemarks(
254-
StringRef outputPath, StringRef outputFormat, bool printAsEmitRemarks,
255-
const std::optional<std::string> &categoryPassName = std::nullopt,
256-
const std::optional<std::string> &categoryMissName = std::nullopt,
255+
StringRef outputPath, llvm::remarks::Format outputFormat,
256+
bool printAsEmitRemarks,
257+
const std::optional<std::string> &categoryPassedName = std::nullopt,
258+
const std::optional<std::string> &categoryMissedName = std::nullopt,
257259
const std::optional<std::string> &categoryAnalysisName = std::nullopt,
258260
const std::optional<std::string> &categoryFailedName = std::nullopt);
259261

mlir/include/mlir/IR/Remarks.h

Lines changed: 34 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ namespace mlir {
2727
/// Defines different remark kinds that can be used to categorize remarks.
2828
enum class RemarkKind {
2929
OptimizationRemarkUnknown = 0,
30-
OptimizationRemarkPass,
30+
OptimizationRemarkPassed,
3131
OptimizationRemarkMissed,
3232
OptimizationRemarkFailure,
3333
OptimizationRemarkAnalysis,
@@ -143,7 +143,7 @@ class RemarkBase : public llvm::DiagnosticInfo {
143143
switch (remarkKind) {
144144
case RemarkKind::OptimizationRemarkUnknown:
145145
return llvm::DiagnosticKind::DK_Generic;
146-
case RemarkKind::OptimizationRemarkPass:
146+
case RemarkKind::OptimizationRemarkPassed:
147147
return llvm::DiagnosticKind::DK_OptimizationRemark;
148148
case RemarkKind::OptimizationRemarkMissed:
149149
return llvm::DiagnosticKind::DK_OptimizationRemarkMissed;
@@ -156,51 +156,19 @@ class RemarkBase : public llvm::DiagnosticInfo {
156156
}
157157
};
158158

159-
// clang-format off
160-
template <class RemarkT>
161-
decltype(auto) operator<<(
162-
RemarkT &&r,
163-
std::enable_if_t<std::is_base_of_v<RemarkBase,
164-
std::remove_reference_t<RemarkT>>,
165-
StringRef>
166-
s) {
159+
inline RemarkBase &operator<<(RemarkBase &r, StringRef s) {
167160
r.insert(s);
168-
return std::forward<RemarkT>(r);
169-
}
170-
171-
template <class RemarkT>
172-
decltype(auto) operator<<(
173-
RemarkT &&r,
174-
std::enable_if_t<std::is_base_of_v<RemarkBase,
175-
std::remove_reference_t<RemarkT>>,
176-
RemarkBase::RemarkKeyValue>
177-
a) {
178-
r.insert(std::move(a));
179-
return std::forward<RemarkT>(r);
161+
return r;
180162
}
181-
182-
template <class RemarkT>
183-
decltype(auto) operator<<(
184-
RemarkT &&r,
185-
std::enable_if_t<std::is_base_of_v<RemarkBase,
186-
std::remove_reference_t<RemarkT>>,
187-
RemarkBase::SetIsVerbose>
188-
v) {
189-
r.insert(v);
190-
return std::forward<RemarkT>(r);
163+
inline RemarkBase &&operator<<(RemarkBase &&r, StringRef s) {
164+
r.insert(s);
165+
return std::move(r);
191166
}
192-
193-
template <class RemarkT>
194-
decltype(auto) operator<<(
195-
RemarkT &&r,
196-
std::enable_if_t<std::is_base_of_v<RemarkBase,
197-
std::remove_reference_t<RemarkT>>,
198-
RemarkBase::SetExtraArgs>
199-
ea) {
200-
r.insert(ea);
201-
return std::forward<RemarkT>(r);
167+
inline RemarkBase &operator<<(RemarkBase &r,
168+
const RemarkBase::RemarkKeyValue &kv) {
169+
r.insert(kv);
170+
return r;
202171
}
203-
// clang-format on
204172

205173
//===----------------------------------------------------------------------===//
206174
// Shorthand aliases for different kinds of remarks.
@@ -209,16 +177,17 @@ decltype(auto) operator<<(
209177
template <RemarkKind K, DiagnosticSeverity S>
210178
class OptRemarkBase final : public RemarkBase {
211179
public:
212-
explicit OptRemarkBase(Location loc, StringRef passName, StringRef remarkName)
213-
: RemarkBase(K, S, passName.data(), remarkName, loc) {}
180+
explicit OptRemarkBase(Location loc, StringRef passName,
181+
StringRef categoryName)
182+
: RemarkBase(K, S, passName.data(), categoryName, loc) {}
214183

215184
bool isEnabled() const override { return true; }
216185
};
217186

218187
using OptRemarkAnalysis = OptRemarkBase<RemarkKind::OptimizationRemarkAnalysis,
219188
DiagnosticSeverity::Remark>;
220189

221-
using OptRemarkPass = OptRemarkBase<RemarkKind::OptimizationRemarkPass,
190+
using OptRemarkPass = OptRemarkBase<RemarkKind::OptimizationRemarkPassed,
222191
DiagnosticSeverity::Remark>;
223192

224193
using OptRemarkMissed = OptRemarkBase<RemarkKind::OptimizationRemarkMissed,
@@ -241,8 +210,8 @@ class RemarkEngine;
241210
class InFlightRemark {
242211
public:
243212
explicit InFlightRemark(RemarkBase *diag) : remark(diag) {}
244-
InFlightRemark(RemarkEngine &eng, RemarkBase *diag)
245-
: owner(&eng), remark(diag) {}
213+
InFlightRemark(RemarkEngine &eng, std::unique_ptr<RemarkBase> diag)
214+
: owner(&eng), remark(std::move(diag)) {}
246215

247216
InFlightRemark() = default; // empty ctor
248217

@@ -287,7 +256,8 @@ class MLIRRemarkStreamer {
287256

288257
class RemarkEngine {
289258
private:
290-
/// The category for missed optimization remarks.
259+
/// Regex that filters missed optimization remarks: only matching one are
260+
/// reported.
291261
std::optional<llvm::Regex> missFilter;
292262
/// The category for passed optimization remarks.
293263
std::optional<llvm::Regex> passFilter;
@@ -299,16 +269,17 @@ class RemarkEngine {
299269
std::unique_ptr<llvm::ToolOutputFile> remarksFile;
300270
/// The MLIR remark streamer that will be used to emit the remarks.
301271
std::unique_ptr<MLIRRemarkStreamer> remarkStreamer;
272+
/// The LLVM remark streamer that will be used to emit the remarks.
302273
std::unique_ptr<llvm::remarks::RemarkStreamer> llvmRemarkStreamer;
303274
/// When is enabled, engine also prints remarks as mlir::emitRemarks.
304-
bool printAsEmitRemarks;
275+
bool printAsEmitRemarks = false;
276+
305277
/// The main MLIR remark streamer that will be used to emit the remarks.
306278
MLIRRemarkStreamer *getLLVMRemarkStreamer() { return remarkStreamer.get(); }
307279
const MLIRRemarkStreamer *getLLVMRemarkStreamer() const {
308280
return remarkStreamer.get();
309281
}
310-
void
311-
setLLVMRemarkStreamer(std::unique_ptr<MLIRRemarkStreamer> remarkStreamer) {
282+
void setRemarkStreamer(std::unique_ptr<MLIRRemarkStreamer> remarkStreamer) {
312283
this->remarkStreamer = std::move(remarkStreamer);
313284
}
314285

@@ -361,8 +332,12 @@ class RemarkEngine {
361332
bool (RemarkEngine::*isEnabled)(StringRef) const);
362333

363334
public:
335+
/// Default constructor is deleted, use the other constructor.
364336
RemarkEngine() = delete;
365-
/// Constructs Remark engine with optional category names
337+
338+
/// Constructs Remark engine with optional category names. If a category
339+
/// name is not provided, it is not enabled. The category names are used to
340+
/// filter the remarks that are emitted.
366341
RemarkEngine(bool printAsEmitRemarks,
367342
std::optional<std::string> categoryPassName = std::nullopt,
368343
std::optional<std::string> categoryMissName = std::nullopt,
@@ -374,7 +349,8 @@ class RemarkEngine {
374349
~RemarkEngine();
375350

376351
/// Setup the remark engine with the given output path and format.
377-
llvm::Error initialize(StringRef outputPath, StringRef outputFormat);
352+
LogicalResult initialize(StringRef outputPath, llvm::remarks::Format fmt,
353+
std::string *errMsg);
378354

379355
/// Report a diagnostic remark.
380356
void report(const RemarkBase &&diag);
@@ -406,14 +382,13 @@ using Suggestion = RemarkBase::RemarkKeyValue;
406382
inline Suggestion suggest(StringRef txt) { return {"Suggestion", txt}; }
407383

408384
template <typename Fn, typename... Args>
409-
[[nodiscard]] inline InFlightRemark withEngine(Fn fn, Location loc,
410-
Args &&...args) {
385+
inline InFlightRemark withEngine(Fn fn, Location loc, Args &&...args) {
411386
MLIRContext *ctx = loc->getContext();
412387

413-
auto &enginePtr = ctx->getRemarkEngine();
388+
RemarkEngine *enginePtr = ctx->getRemarkEngine();
414389

415-
if (RemarkEngine *eng = enginePtr.get())
416-
return (eng->*fn)(loc, std::forward<Args>(args)...);
390+
if (enginePtr)
391+
return (enginePtr->*fn)(loc, std::forward<Args>(args)...);
417392

418393
return {};
419394
}

mlir/lib/IR/MLIRContext.cpp

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -403,22 +403,25 @@ void MLIRContext::setRemarkEngine(std::unique_ptr<RemarkEngine> engine) {
403403
getImpl().remarkEngine = std::move(engine);
404404
}
405405

406-
std::unique_ptr<RemarkEngine> &MLIRContext::getRemarkEngine() {
407-
return getImpl().remarkEngine;
406+
RemarkEngine *MLIRContext::getRemarkEngine() {
407+
return getImpl().remarkEngine.get();
408408
}
409409

410410
void MLIRContext::setupOptimizationRemarks(
411-
StringRef outputPath, StringRef outputFormat, bool printAsEmitRemarks,
412-
const std::optional<std::string> &categoryPassName,
413-
const std::optional<std::string> &categoryMissName,
411+
StringRef outputPath, llvm::remarks::Format outputFormat,
412+
bool printAsEmitRemarks,
413+
const std::optional<std::string> &categoryPassedName,
414+
const std::optional<std::string> &categoryMissedName,
414415
const std::optional<std::string> &categoryAnalysisName,
415416
const std::optional<std::string> &categoryFailedName) {
416417
auto engine = std::make_unique<RemarkEngine>(
417-
printAsEmitRemarks, categoryPassName, categoryMissName,
418+
printAsEmitRemarks, categoryPassedName, categoryMissedName,
418419
categoryAnalysisName, categoryFailedName);
419-
llvm::Error e = engine->initialize(outputPath, outputFormat);
420-
if (e)
421-
llvm::report_fatal_error("Failed to initialize remark engine");
420+
std::string errMsg;
421+
if (failed(engine->initialize(outputPath, outputFormat, &errMsg))) {
422+
llvm::report_fatal_error(
423+
llvm::Twine("Failed to initialize remark engine. Error: ") + errMsg);
424+
}
422425
setRemarkEngine(std::move(engine));
423426
}
424427

0 commit comments

Comments
 (0)