MLIR: lib/IR/Remarks.cpp Source File (original) (raw)

1

2

3

4

5

6

7

8

10

14

15#include "llvm/ADT/StringExtras.h"

16#include "llvm/ADT/StringRef.h"

17

20

21

22

23

25 llvm::raw_string_ostream os(val);

26 os << v;

27}

28

30 llvm::raw_string_ostream os(val);

31 os << t;

32}

33

35 llvm::raw_string_ostream os(val);

36 os << a;

37}

38

41

42

44 if (args.empty())

45 return;

46

49 return a.key < b.key;

50 });

51

52 for (size_t i = 0; i < sorted.size(); ++i) {

53 const auto &a = sorted[i];

54 os << a.key << "=";

55

56 llvm::StringRef val(a.val);

57 bool needsQuote = val.contains(' ') || val.contains(',') ||

58 val.contains('{') || val.contains('}');

59 if (needsQuote)

60 os << '"' << val << '"';

61 else

62 os << val;

63

64 if (i + 1 < sorted.size())

65 os << ", ";

66 }

67}

68

69

70

71

72

73

74

75void Remark::print(llvm::raw_ostream &os, bool printLocation) const {

76

80

81 os << '[' << type << "] ";

82 os << name << " | ";

86 os << "Function=" << getFunction() << " | ";

87

88 if (printLocation) {

89 if (auto flc = mlir::dyn_castmlir::FileLineColLoc(getLocation())) {

90 os << " @" << flc.getFilename() << ":" << flc.getLine() << ":"

91 << flc.getColumn();

92 }

93 }

94

96}

97

99 std::string s;

100 llvm::raw_string_ostream os(s);

102 os.flush();

103 return s;

104}

105

109 return "Unknown";

111 return "Passed";

113 return "Missed";

115 return "Failure";

117 return "Analysis";

118 }

119 llvm_unreachable("Unknown remark kind");

120}

121

125 return llvm::remarks::Type::Unknown;

127 return llvm::remarks::Type::Passed;

129 return llvm::remarks::Type::Missed;

131 return llvm::remarks::Type::Failure;

133 return llvm::remarks::Type::Analysis;

134 }

135 llvm_unreachable("Unknown remark kind");

136}

137

139 auto locLambda = [&]() -> llvm::remarks::RemarkLocation {

140 if (auto flc = dyn_cast(getLocation()))

141 return {flc.getFilename(), flc.getLine(), flc.getColumn()};

142 return {"", 0, 0};

143 };

144

145 llvm::remarks::Remark r;

148

151 r.Loc = locLambda();

153 r.Args.emplace_back();

154 r.Args.back().Key = arg.key;

155 r.Args.back().Val = arg.val;

156 }

157 return r;

158}

159

160

161

162

163

165 if (remark && owner)

166 owner->report(std::move(*remark));

167 owner = nullptr;

168}

169

170

171

172

173

174template <typename RemarkT, typename... Args>

176 static_assert(std::is_base_of_v<Remark, RemarkT>,

177 "RemarkT must derive from Remark");

179 std::make_unique(std::forward(args)...));

180}

181

182template

185 bool (RemarkEngine::*isEnabled)(StringRef) const) {

186 return (this->*isEnabled)(opts.categoryName) ? makeRemark(loc, opts)

188}

189

190bool RemarkEngine::isMissedOptRemarkEnabled(StringRef categoryName) const {

191 return missFilter && missFilter->match(categoryName);

192}

193

194bool RemarkEngine::isPassedOptRemarkEnabled(StringRef categoryName) const {

195 return passedFilter && passedFilter->match(categoryName);

196}

197

198bool RemarkEngine::isAnalysisOptRemarkEnabled(StringRef categoryName) const {

199 return analysisFilter && analysisFilter->match(categoryName);

200}

201

202bool RemarkEngine::isFailedOptRemarkEnabled(StringRef categoryName) const {

203 return failedFilter && failedFilter->match(categoryName);

204}

205

208 return emitIfEnabled(loc, opts,

209 &RemarkEngine::isPassedOptRemarkEnabled);

210}

211

214 return emitIfEnabled(

215 loc, opts, &RemarkEngine::isMissedOptRemarkEnabled);

216}

217

220 return emitIfEnabled(

221 loc, opts, &RemarkEngine::isFailedOptRemarkEnabled);

222}

223

226 return emitIfEnabled(

227 loc, opts, &RemarkEngine::isAnalysisOptRemarkEnabled);

228}

229

230

231

232

233

234void RemarkEngine::reportImpl(const Remark &remark) {

235

236 if (remarkStreamer) {

237 remarkStreamer->streamOptimizationRemark(remark);

238 }

239

240

241 if (printAsEmitRemarks)

243}

244

246 if (remarkEmittingPolicy)

247 remarkEmittingPolicy->reportRemark(remark);

248}

249

251 if (remarkEmittingPolicy)

252 remarkEmittingPolicy->finalize();

253

254 if (remarkStreamer)

255 remarkStreamer->finalize();

256}

257

259 std::unique_ptr streamer,

260 std::unique_ptr remarkEmittingPolicy,

261 std::string *errMsg) {

262

263 remarkStreamer = std::move(streamer);

264

265 auto reportFunc =

266 std::bind(&RemarkEngine::reportImpl, this, std::placeholders::_1);

267 remarkEmittingPolicy->initialize(ReportFn(std::move(reportFunc)));

268

269 this->remarkEmittingPolicy = std::move(remarkEmittingPolicy);

271}

272

273

275 s = s.trim();

276 return s.starts_with("^") && s.ends_with("$");

277}

278

279

280static std::string anchorWhole(llvm::StringRef filter) {

282 return filter.str();

283 return (llvm::Twine("^(") + filter + ")$").str();

284}

285

286

287

288

289static std::optionalllvm::Regex

291 const std::optionalstd::string &specific) {

293 if (cats.all && !cats.all->empty())

294 parts.emplace_back(*cats.all);

295 if (specific && !specific->empty())

296 parts.emplace_back(*specific);

297

298 if (parts.empty())

299 return std::nullopt;

300

301 std::string joined = llvm::join(parts, "|");

302 std::string anchored = anchorWhole(joined);

303

304 llvm::Regex rx(anchored);

305 std::string err;

306 if (!rx.isValid(err))

307 return std::nullopt;

308

309 return std::make_optionalllvm::Regex(std::move(rx));

310}

311

314 : printAsEmitRemarks(printAsEmitRemarks) {

323}

324

326 MLIRContext &ctx, std::unique_ptrdetail::MLIRRemarkStreamerBase streamer,

327 std::unique_ptrdetail::RemarkEmittingPolicyBase remarkEmittingPolicy,

329 auto engine =

330 std::make_uniquedetail::RemarkEngine(printAsEmitRemarks, cats);

331

332 std::string errMsg;

333 if (failed(engine->initialize(std::move(streamer),

334 std::move(remarkEmittingPolicy), &errMsg))) {

335 llvm::report_fatal_error(

336 llvm::Twine("Failed to initialize remark engine. Error: ") + errMsg);

337 }

339

341}

342

343

344

345

346

350}

b

Return true if permutation is a valid permutation of the outer_dims_perm (case OuterOrInnerPerm::Oute...

Attributes are known-constant values of operations.

This class defines the main interface for locations in MLIR and acts as a non-nullable wrapper around...

MLIRContext is the top-level object for a collection of MLIR operations.

void setRemarkEngine(std::unique_ptr< remark::detail::RemarkEngine > engine)

Set the remark engine for this context.

Instances of the Type class are uniqued, have an immutable identifier and an optional mutable compone...

This class represents an instance of an SSA value in the MLIR system, representing a computable value...

InFlightDiagnostic emitRemark(Location loc)

Utility method to emit a remark message using this location.