Skip to content

Commit f5b974b

Browse files
wrengraartbik
authored andcommitted
[mlir][sparse] Adding {Var,DimLvlExpr,DimSpec,LvlSpec,DimLvlMap}::str methods
These methods are needed for use with `Diagnostic::operator<<` etc. The definitions follow the pattern of `Diagnostic::str` by simply wrapping the underlying `print(raw_ostream)` method. Although there is some overhead for constructing the `std::string`, this seems like the overall most-efficient option: since this overhead only occurs on the error path (under the current intended usage). An alternative approach would be to have one method construct a `Twine` directly, and then have the print method pass the twine to the stream; however, that would mean introducing the overhead of twine construction on the common/happy path of simply printing things out. Reviewed By: aartbik Differential Revision: https://reviews.llvm.org/D157643
1 parent 78921a6 commit f5b974b

File tree

4 files changed

+36
-0
lines changed

4 files changed

+36
-0
lines changed

mlir/lib/Dialect/SparseTensor/IR/Detail/DimLvlMap.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,12 @@ void DimLvlExpr::dump() const {
9898
print(llvm::errs());
9999
llvm::errs() << "\n";
100100
}
101+
std::string DimLvlExpr::str() const {
102+
std::string str;
103+
llvm::raw_string_ostream os(str);
104+
print(os);
105+
return os.str();
106+
}
101107
void DimLvlExpr::print(AsmPrinter &printer) const {
102108
print(printer.getStream());
103109
}
@@ -220,6 +226,12 @@ void DimSpec::dump() const {
220226
print(llvm::errs(), /*wantElision=*/false);
221227
llvm::errs() << "\n";
222228
}
229+
std::string DimSpec::str(bool wantElision) const {
230+
std::string str;
231+
llvm::raw_string_ostream os(str);
232+
print(os, wantElision);
233+
return os.str();
234+
}
223235
void DimSpec::print(AsmPrinter &printer, bool wantElision) const {
224236
print(printer.getStream(), wantElision);
225237
}
@@ -260,6 +272,12 @@ void LvlSpec::dump() const {
260272
print(llvm::errs(), /*wantElision=*/false);
261273
llvm::errs() << "\n";
262274
}
275+
std::string LvlSpec::str(bool wantElision) const {
276+
std::string str;
277+
llvm::raw_string_ostream os(str);
278+
print(os, wantElision);
279+
return os.str();
280+
}
263281
void LvlSpec::print(AsmPrinter &printer, bool wantElision) const {
264282
print(printer.getStream(), wantElision);
265283
}
@@ -345,6 +363,12 @@ void DimLvlMap::dump() const {
345363
print(llvm::errs(), /*wantElision=*/false);
346364
llvm::errs() << "\n";
347365
}
366+
std::string DimLvlMap::str(bool wantElision) const {
367+
std::string str;
368+
llvm::raw_string_ostream os(str);
369+
print(os, wantElision);
370+
return os.str();
371+
}
348372
void DimLvlMap::print(AsmPrinter &printer, bool wantElision) const {
349373
print(printer.getStream(), wantElision);
350374
}

mlir/lib/Dialect/SparseTensor/IR/Detail/DimLvlMap.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ class DimLvlExpr {
122122
/// with respect to the given ranks.
123123
[[nodiscard]] bool isValid(Ranks const &ranks) const;
124124

125+
std::string str() const;
125126
void print(llvm::raw_ostream &os) const;
126127
void print(AsmPrinter &printer) const;
127128
void dump() const;
@@ -251,6 +252,7 @@ class DimSpec final {
251252
bool isFunctionOf(VarSet const &vars) const;
252253
void getFreeVars(VarSet &vars) const;
253254

255+
std::string str(bool wantElision = true) const;
254256
void print(llvm::raw_ostream &os, bool wantElision = true) const;
255257
void print(AsmPrinter &printer, bool wantElision = true) const;
256258
void dump() const;
@@ -306,6 +308,7 @@ class LvlSpec final {
306308
bool isFunctionOf(VarSet const &vars) const;
307309
void getFreeVars(VarSet &vars) const;
308310

311+
std::string str(bool wantElision = true) const;
309312
void print(llvm::raw_ostream &os, bool wantElision = true) const;
310313
void print(AsmPrinter &printer, bool wantElision = true) const;
311314
void dump() const;
@@ -339,6 +342,7 @@ class DimLvlMap final {
339342
AffineMap getDimToLvlMap(MLIRContext *context) const;
340343
AffineMap getLvlToDimMap(MLIRContext *context) const;
341344

345+
std::string str(bool wantElision = true) const;
342346
void print(llvm::raw_ostream &os, bool wantElision = true) const;
343347
void print(AsmPrinter &printer, bool wantElision = true) const;
344348
void dump() const;

mlir/lib/Dialect/SparseTensor/IR/Detail/Var.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,13 @@ static constexpr const VarKind everyVarKind[] = {
2525
// `Var` implementation.
2626
//===----------------------------------------------------------------------===//
2727

28+
std::string Var::str() const {
29+
std::string str;
30+
llvm::raw_string_ostream os(str);
31+
print(os);
32+
return os.str();
33+
}
34+
2835
void Var::print(AsmPrinter &printer) const { print(printer.getStream()); }
2936

3037
void Var::print(llvm::raw_ostream &os) const {

mlir/lib/Dialect/SparseTensor/IR/Detail/Var.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,7 @@ class Var {
197197
template <typename U>
198198
constexpr std::optional<U> dyn_cast() const;
199199

200+
std::string str() const;
200201
void print(llvm::raw_ostream &os) const;
201202
void print(AsmPrinter &printer) const;
202203
void dump() const;

0 commit comments

Comments
 (0)