Skip to content

Commit 704c224

Browse files
[mlir][sparse] Clean up parser (#72571)
Remove unused functions in parser.
1 parent 2fd343e commit 704c224

File tree

4 files changed

+1
-326
lines changed

4 files changed

+1
-326
lines changed

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

Lines changed: 0 additions & 240 deletions
Original file line numberDiff line numberDiff line change
@@ -16,21 +16,6 @@ using namespace mlir::sparse_tensor::ir_detail;
1616
// `DimLvlExpr` implementation.
1717
//===----------------------------------------------------------------------===//
1818

19-
Var DimLvlExpr::castAnyVar() const {
20-
assert(expr && "uninitialized DimLvlExpr");
21-
const auto var = dyn_castAnyVar();
22-
assert(var && "expected DimLvlExpr to be a Var");
23-
return *var;
24-
}
25-
26-
std::optional<Var> DimLvlExpr::dyn_castAnyVar() const {
27-
if (const auto s = dyn_cast_or_null<AffineSymbolExpr>(expr))
28-
return SymVar(s);
29-
if (const auto x = dyn_cast_or_null<AffineDimExpr>(expr))
30-
return Var(getAllowedVarKind(), x);
31-
return std::nullopt;
32-
}
33-
3419
SymVar DimLvlExpr::castSymVar() const {
3520
return SymVar(llvm::cast<AffineSymbolExpr>(expr));
3621
}
@@ -51,30 +36,6 @@ std::optional<Var> DimLvlExpr::dyn_castDimLvlVar() const {
5136
return std::nullopt;
5237
}
5338

54-
int64_t DimLvlExpr::castConstantValue() const {
55-
return llvm::cast<AffineConstantExpr>(expr).getValue();
56-
}
57-
58-
std::optional<int64_t> DimLvlExpr::dyn_castConstantValue() const {
59-
const auto k = dyn_cast_or_null<AffineConstantExpr>(expr);
60-
return k ? std::make_optional(k.getValue()) : std::nullopt;
61-
}
62-
63-
bool DimLvlExpr::hasConstantValue(int64_t val) const {
64-
const auto k = dyn_cast_or_null<AffineConstantExpr>(expr);
65-
return k && k.getValue() == val;
66-
}
67-
68-
DimLvlExpr DimLvlExpr::getLHS() const {
69-
const auto binop = dyn_cast_or_null<AffineBinaryOpExpr>(expr);
70-
return DimLvlExpr(kind, binop ? binop.getLHS() : nullptr);
71-
}
72-
73-
DimLvlExpr DimLvlExpr::getRHS() const {
74-
const auto binop = dyn_cast_or_null<AffineBinaryOpExpr>(expr);
75-
return DimLvlExpr(kind, binop ? binop.getRHS() : nullptr);
76-
}
77-
7839
std::tuple<DimLvlExpr, AffineExprKind, DimLvlExpr>
7940
DimLvlExpr::unpackBinop() const {
8041
const auto ak = getAffineKind();
@@ -84,115 +45,6 @@ DimLvlExpr::unpackBinop() const {
8445
return {lhs, ak, rhs};
8546
}
8647

87-
void DimLvlExpr::dump() const {
88-
print(llvm::errs());
89-
llvm::errs() << "\n";
90-
}
91-
std::string DimLvlExpr::str() const {
92-
std::string str;
93-
llvm::raw_string_ostream os(str);
94-
print(os);
95-
return os.str();
96-
}
97-
void DimLvlExpr::print(AsmPrinter &printer) const {
98-
print(printer.getStream());
99-
}
100-
void DimLvlExpr::print(llvm::raw_ostream &os) const {
101-
if (!expr)
102-
os << "<<NULL AFFINE EXPR>>";
103-
else
104-
printWeak(os);
105-
}
106-
107-
namespace {
108-
struct MatchNeg final : public std::pair<DimLvlExpr, int64_t> {
109-
using Base = std::pair<DimLvlExpr, int64_t>;
110-
using Base::Base;
111-
constexpr DimLvlExpr getLHS() const { return first; }
112-
constexpr int64_t getRHS() const { return second; }
113-
};
114-
} // namespace
115-
116-
static std::optional<MatchNeg> matchNeg(DimLvlExpr expr) {
117-
const auto [lhs, op, rhs] = expr.unpackBinop();
118-
if (op == AffineExprKind::Constant) {
119-
const auto val = expr.castConstantValue();
120-
if (val < 0)
121-
return MatchNeg{DimLvlExpr{expr.getExprKind(), AffineExpr()}, val};
122-
}
123-
if (op == AffineExprKind::Mul)
124-
if (const auto rval = rhs.dyn_castConstantValue(); rval && *rval < 0)
125-
return MatchNeg{lhs, *rval};
126-
return std::nullopt;
127-
}
128-
129-
// A heavily revised version of `AsmPrinter::Impl::printAffineExprInternal`.
130-
void DimLvlExpr::printAffineExprInternal(
131-
llvm::raw_ostream &os, BindingStrength enclosingTightness) const {
132-
const char *binopSpelling = nullptr;
133-
switch (getAffineKind()) {
134-
case AffineExprKind::SymbolId:
135-
os << castSymVar();
136-
return;
137-
case AffineExprKind::DimId:
138-
os << castDimLvlVar();
139-
return;
140-
case AffineExprKind::Constant:
141-
os << castConstantValue();
142-
return;
143-
case AffineExprKind::Add:
144-
binopSpelling = " + "; // N.B., this is unused
145-
break;
146-
case AffineExprKind::Mul:
147-
binopSpelling = " * ";
148-
break;
149-
case AffineExprKind::FloorDiv:
150-
binopSpelling = " floordiv ";
151-
break;
152-
case AffineExprKind::CeilDiv:
153-
binopSpelling = " ceildiv ";
154-
break;
155-
case AffineExprKind::Mod:
156-
binopSpelling = " mod ";
157-
break;
158-
}
159-
160-
if (enclosingTightness == BindingStrength::Strong)
161-
os << '(';
162-
163-
const auto [lhs, op, rhs] = unpackBinop();
164-
if (op == AffineExprKind::Mul && rhs.hasConstantValue(-1)) {
165-
// Pretty print `(lhs * -1)` as "-lhs".
166-
os << '-';
167-
lhs.printStrong(os);
168-
} else if (op != AffineExprKind::Add) {
169-
// Default rule for tightly binding binary operators.
170-
// (Including `Mul` that didn't match the previous rule.)
171-
lhs.printStrong(os);
172-
os << binopSpelling;
173-
rhs.printStrong(os);
174-
} else {
175-
// Combination of all the special rules for addition/subtraction.
176-
lhs.printWeak(os);
177-
const auto rx = matchNeg(rhs);
178-
os << (rx ? " - " : " + ");
179-
const auto &rlhs = rx ? rx->getLHS() : rhs;
180-
const auto rrhs = rx ? rx->getRHS() : -1; // value irrelevant when `!rx`
181-
const bool nonunit = rrhs != -1; // value irrelevant when `!rx`
182-
const bool isStrong =
183-
rx && rlhs && (nonunit || rlhs.getAffineKind() == AffineExprKind::Add);
184-
if (rlhs)
185-
rlhs.printAffineExprInternal(os, BindingStrength{isStrong});
186-
if (rx && rlhs && nonunit)
187-
os << " * ";
188-
if (rx && (!rlhs || nonunit))
189-
os << -rrhs;
190-
}
191-
192-
if (enclosingTightness == BindingStrength::Strong)
193-
os << ')';
194-
}
195-
19648
//===----------------------------------------------------------------------===//
19749
// `DimSpec` implementation.
19850
//===----------------------------------------------------------------------===//
@@ -206,31 +58,6 @@ bool DimSpec::isValid(Ranks const &ranks) const {
20658
return ranks.isValid(var) && (!expr || ranks.isValid(expr));
20759
}
20860

209-
void DimSpec::dump() const {
210-
print(llvm::errs(), /*wantElision=*/false);
211-
llvm::errs() << "\n";
212-
}
213-
std::string DimSpec::str(bool wantElision) const {
214-
std::string str;
215-
llvm::raw_string_ostream os(str);
216-
print(os, wantElision);
217-
return os.str();
218-
}
219-
void DimSpec::print(AsmPrinter &printer, bool wantElision) const {
220-
print(printer.getStream(), wantElision);
221-
}
222-
void DimSpec::print(llvm::raw_ostream &os, bool wantElision) const {
223-
os << var;
224-
if (expr && (!wantElision || !elideExpr))
225-
os << " = " << expr;
226-
if (slice) {
227-
os << " : ";
228-
// Call `SparseTensorDimSliceAttr::print` directly, to avoid
229-
// printing the mnemonic.
230-
slice.print(os);
231-
}
232-
}
233-
23461
//===----------------------------------------------------------------------===//
23562
// `LvlSpec` implementation.
23663
//===----------------------------------------------------------------------===//
@@ -246,26 +73,6 @@ bool LvlSpec::isValid(Ranks const &ranks) const {
24673
return ranks.isValid(var) && ranks.isValid(expr);
24774
}
24875

249-
void LvlSpec::dump() const {
250-
print(llvm::errs(), /*wantElision=*/false);
251-
llvm::errs() << "\n";
252-
}
253-
std::string LvlSpec::str(bool wantElision) const {
254-
std::string str;
255-
llvm::raw_string_ostream os(str);
256-
print(os, wantElision);
257-
return os.str();
258-
}
259-
void LvlSpec::print(AsmPrinter &printer, bool wantElision) const {
260-
print(printer.getStream(), wantElision);
261-
}
262-
void LvlSpec::print(llvm::raw_ostream &os, bool wantElision) const {
263-
if (!wantElision || !elideVar)
264-
os << var << " = ";
265-
os << expr;
266-
os << ": " << toMLIRString(type);
267-
}
268-
26976
//===----------------------------------------------------------------------===//
27077
// `DimLvlMap` implementation.
27178
//===----------------------------------------------------------------------===//
@@ -334,51 +141,4 @@ AffineMap DimLvlMap::getLvlToDimMap(MLIRContext *context) const {
334141
return map;
335142
}
336143

337-
void DimLvlMap::dump() const {
338-
print(llvm::errs(), /*wantElision=*/false);
339-
llvm::errs() << "\n";
340-
}
341-
std::string DimLvlMap::str(bool wantElision) const {
342-
std::string str;
343-
llvm::raw_string_ostream os(str);
344-
print(os, wantElision);
345-
return os.str();
346-
}
347-
void DimLvlMap::print(AsmPrinter &printer, bool wantElision) const {
348-
print(printer.getStream(), wantElision);
349-
}
350-
void DimLvlMap::print(llvm::raw_ostream &os, bool wantElision) const {
351-
// Symbolic identifiers.
352-
// NOTE: Unlike `AffineMap` we place the SymVar bindings before the DimVar
353-
// bindings, since the SymVars may occur within DimExprs and thus this
354-
// ordering helps reduce potential user confusion about the scope of bidings
355-
// (since it means SymVars and DimVars both bind-forward in the usual way,
356-
// whereas only LvlVars have different binding rules).
357-
if (symRank != 0) {
358-
os << "[s0";
359-
for (unsigned i = 1; i < symRank; ++i)
360-
os << ", s" << i;
361-
os << ']';
362-
}
363-
364-
// LvlVar forward-declarations.
365-
if (mustPrintLvlVars) {
366-
os << '{';
367-
llvm::interleaveComma(
368-
lvlSpecs, os, [&](LvlSpec const &spec) { os << spec.getBoundVar(); });
369-
os << "} ";
370-
}
371-
372-
// Dimension specifiers.
373-
os << '(';
374-
llvm::interleaveComma(
375-
dimSpecs, os, [&](DimSpec const &spec) { spec.print(os, wantElision); });
376-
os << ") -> (";
377-
// Level specifiers.
378-
wantElision = wantElision && !mustPrintLvlVars;
379-
llvm::interleaveComma(
380-
lvlSpecs, os, [&](LvlSpec const &spec) { spec.print(os, wantElision); });
381-
os << ')';
382-
}
383-
384144
//===----------------------------------------------------------------------===//

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

Lines changed: 0 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -77,40 +77,19 @@ class DimLvlExpr {
7777
//
7878
// Getters for handling `AffineExpr` subclasses.
7979
//
80-
Var castAnyVar() const;
81-
std::optional<Var> dyn_castAnyVar() const;
8280
SymVar castSymVar() const;
8381
std::optional<SymVar> dyn_castSymVar() const;
8482
Var castDimLvlVar() const;
8583
std::optional<Var> dyn_castDimLvlVar() const;
86-
int64_t castConstantValue() const;
87-
std::optional<int64_t> dyn_castConstantValue() const;
88-
bool hasConstantValue(int64_t val) const;
89-
DimLvlExpr getLHS() const;
90-
DimLvlExpr getRHS() const;
9184
std::tuple<DimLvlExpr, AffineExprKind, DimLvlExpr> unpackBinop() const;
9285

9386
/// Checks whether the variables bound/used by this spec are valid
9487
/// with respect to the given ranks.
9588
[[nodiscard]] bool isValid(Ranks const &ranks) const;
9689

97-
std::string str() const;
98-
void print(llvm::raw_ostream &os) const;
99-
void print(AsmPrinter &printer) const;
100-
void dump() const;
101-
10290
protected:
10391
// Variant of `mlir::AsmPrinter::Impl::BindingStrength`
10492
enum class BindingStrength : bool { Weak = false, Strong = true };
105-
106-
void printAffineExprInternal(llvm::raw_ostream &os,
107-
BindingStrength enclosingTightness) const;
108-
void printStrong(llvm::raw_ostream &os) const {
109-
printAffineExprInternal(os, BindingStrength::Strong);
110-
}
111-
void printWeak(llvm::raw_ostream &os) const {
112-
printAffineExprInternal(os, BindingStrength::Weak);
113-
}
11493
};
11594
static_assert(IsZeroCostAbstraction<DimLvlExpr>);
11695

@@ -208,11 +187,6 @@ class DimSpec final {
208187
/// to be vacuously valid, and therefore calling `setExpr` invalidates
209188
/// the result of this predicate.
210189
[[nodiscard]] bool isValid(Ranks const &ranks) const;
211-
212-
std::string str(bool wantElision = true) const;
213-
void print(llvm::raw_ostream &os, bool wantElision = true) const;
214-
void print(AsmPrinter &printer, bool wantElision = true) const;
215-
void dump() const;
216190
};
217191

218192
static_assert(IsZeroCostAbstraction<DimSpec>);
@@ -248,11 +222,6 @@ class LvlSpec final {
248222
/// Checks whether the variables bound/used by this spec are valid
249223
/// with respect to the given ranks.
250224
[[nodiscard]] bool isValid(Ranks const &ranks) const;
251-
252-
std::string str(bool wantElision = true) const;
253-
void print(llvm::raw_ostream &os, bool wantElision = true) const;
254-
void print(AsmPrinter &printer, bool wantElision = true) const;
255-
void dump() const;
256225
};
257226

258227
static_assert(IsZeroCostAbstraction<LvlSpec>);
@@ -282,11 +251,6 @@ class DimLvlMap final {
282251
AffineMap getDimToLvlMap(MLIRContext *context) const;
283252
AffineMap getLvlToDimMap(MLIRContext *context) const;
284253

285-
std::string str(bool wantElision = true) const;
286-
void print(llvm::raw_ostream &os, bool wantElision = true) const;
287-
void print(AsmPrinter &printer, bool wantElision = true) const;
288-
void dump() const;
289-
290254
private:
291255
/// Checks for integrity of variable-binding structure.
292256
/// This is already called by the ctor.

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

Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -84,36 +84,6 @@ bool VarSet::contains(Var var) const {
8484
return num < bits.size() && bits[num];
8585
}
8686

87-
bool VarSet::occursIn(VarSet const &other) const {
88-
for (const auto vk : everyVarKind)
89-
if (impl[vk].anyCommon(other.impl[vk]))
90-
return true;
91-
return false;
92-
}
93-
94-
bool VarSet::occursIn(DimLvlExpr expr) const {
95-
if (!expr)
96-
return false;
97-
switch (expr.getAffineKind()) {
98-
case AffineExprKind::Constant:
99-
return false;
100-
case AffineExprKind::SymbolId:
101-
return contains(expr.castSymVar());
102-
case AffineExprKind::DimId:
103-
return contains(expr.castDimLvlVar());
104-
case AffineExprKind::Add:
105-
case AffineExprKind::Mul:
106-
case AffineExprKind::Mod:
107-
case AffineExprKind::FloorDiv:
108-
case AffineExprKind::CeilDiv: {
109-
const auto [lhs, op, rhs] = expr.unpackBinop();
110-
(void)op;
111-
return occursIn(lhs) || occursIn(rhs);
112-
}
113-
}
114-
llvm_unreachable("unknown AffineExprKind");
115-
}
116-
11787
void VarSet::add(Var var) {
11888
// NOTE: `SmallBitVector::operator[]` will raise assertion errors for OOB.
11989
impl[var.getKind()][var.getNum()] = true;

0 commit comments

Comments
 (0)