15 #define FAILURE_IF_FAILED(RES) \
26 #define FAILURE_IF_NULLOPT_OR_FAILED(RES) \
27 if (didntSucceed(RES)) { \
32 #define ERROR_IF(COND, MSG) \
34 return parser.emitError(loc, MSG); \
51 ERROR_IF(!isOptional,
"expected bare identifier")
55 if (
const auto res = env.
lookupOrCreate(creationPolicy, name, loc, vk)) {
57 didCreate = res->second;
61 switch (creationPolicy) {
63 return parser.
emitError(loc,
"use of undeclared identifier '" + name +
"'");
65 llvm_unreachable(
"got nullopt for Policy::May");
67 return parser.
emitError(loc,
"redefinition of identifier '" + name +
"'");
69 llvm_unreachable(
"unknown Policy");
72 FailureOr<VarInfo::ID> DimLvlMapParser::parseVarUsage(
VarKind vk,
76 const bool isOptional =
false;
78 const auto res = parseVar(vk, isOptional, creationPolicy,
id, didCreate);
80 assert(requireKnown ? !didCreate :
true);
84 FailureOr<VarInfo::ID> DimLvlMapParser::parseVarBinding(
VarKind vk,
89 const bool isOptional =
false;
91 const auto res = parseVar(vk, isOptional, creationPolicy,
id, didCreate);
93 assert(requireKnown ? !didCreate : didCreate);
98 FailureOr<std::pair<Var, bool>>
99 DimLvlMapParser::parseOptionalVarBinding(
VarKind vk,
bool requireKnown) {
103 const bool isOptional =
true;
105 const auto res = parseVar(vk, isOptional, creationPolicy,
id, didCreate);
106 if (res.has_value()) {
109 return std::make_pair(bindVar(loc,
id),
true);
117 const auto var = env.
bindVar(
id);
118 const auto &info = std::as_const(env).access(
id);
119 const auto name = info.getName();
120 const auto num = *info.
getNum();
121 switch (info.getKind()) {
124 dimsAndSymbols.emplace_back(name, affine);
125 lvlsAndSymbols.emplace_back(name, affine);
135 llvm_unreachable(
"unknown VarKind");
154 ParseResult DimLvlMapParser::parseSymbolBindingList() {
158 " in symbol binding list");
161 ParseResult DimLvlMapParser::parseLvlVarBindingList() {
164 [
this]() {
return ParseResult(parseVarBinding(
VarKind::Level)); },
165 " in level declaration list");
172 ParseResult DimLvlMapParser::parseDimSpecList() {
175 [
this]() -> ParseResult {
return parseDimSpec(); },
176 " in dimension-specifier list");
179 ParseResult DimLvlMapParser::parseDimSpec() {
194 SparseTensorDimSliceAttr slice;
199 slice = llvm::dyn_cast<SparseTensorDimSliceAttr>(attr);
200 ERROR_IF(!slice,
"expected SparseTensorDimSliceAttr")
203 dimSpecs.emplace_back(var, expr, slice);
211 ParseResult DimLvlMapParser::parseLvlSpecList() {
229 const bool requireLvlVarBinding = declaredLvlRank != 0;
234 [=]() -> ParseResult {
return parseLvlSpec(requireLvlVarBinding); },
235 " in level-specifier list");
237 const auto specLvlRank = lvlSpecs.size();
238 ERROR_IF(requireLvlVarBinding && specLvlRank != declaredLvlRank,
239 "Level-rank mismatch between forward-declarations and specifiers. "
241 Twine(declaredLvlRank) +
" level-variables; but got " +
242 Twine(specLvlRank) +
" level-specifiers.")
253 return Twine(n) +
"th";
258 DimLvlMapParser::parseLvlVarBinding(
bool requireLvlVarBinding) {
260 if (!requireLvlVarBinding)
272 const auto &info = std::as_const(env).access(*varID);
273 const auto var = info.getVar().
cast<
LvlVar>();
274 const auto forwardNum = var.
getNum();
275 const auto specNum = lvlSpecs.size();
277 "Level-variable ordering mismatch. The variable '" + info.getName() +
278 "' was forward-declared as the " +
nth(forwardNum) +
279 " level; but is bound by the " +
nth(specNum) +
288 const auto varRes = parseLvlVarBinding(requireLvlVarBinding);
290 const LvlVar var = *varRes;
298 const auto type = lvlTypeParser.parseLvlType(parser);
301 lvlSpecs.emplace_back(var, expr, static_cast<
LevelType>(*type));
static bool didntSucceed(OptionalParseResult res)
Helper function for FAILURE_IF_NULLOPT_OR_FAILED to avoid duplicating its RES parameter.
#define FAILURE_IF_FAILED(RES)
#define ERROR_IF(COND, MSG)
static Twine nth(Var::Num n)
#define FAILURE_IF_NULLOPT_OR_FAILED(RES)
Base type for affine expression.
@ Paren
Parens surrounding zero or more operands.
@ OptionalBraces
{} brackets surrounding zero or more operands, or nothing.
@ OptionalSquare
Square brackets supporting zero or more ops, or nothing.
virtual ParseResult parseCommaSeparatedList(Delimiter delimiter, function_ref< ParseResult()> parseElementFn, StringRef contextMessage=StringRef())=0
Parse a list of comma-separated items with an optional delimiter.
virtual ParseResult parseOptionalEqual()=0
Parse a = token if present.
virtual ParseResult parseOptionalKeyword(StringRef keyword)=0
Parse the given keyword if present.
MLIRContext * getContext() const
virtual InFlightDiagnostic emitError(SMLoc loc, const Twine &message={})=0
Emit a diagnostic at the specified location and return failure.
virtual ParseResult parseOptionalColon()=0
Parse a : token if present.
virtual SMLoc getCurrentLocation()=0
Get the location of the next token and store it into the argument.
virtual ParseResult parseColon()=0
Parse a : token.
virtual ParseResult parseArrow()=0
Parse a '->' token.
virtual ParseResult parseAffineExpr(ArrayRef< std::pair< StringRef, AffineExpr >> symbolSet, AffineExpr &expr)=0
Parse an affine expr instance into 'expr' using the already computed mapping from symbols to affine e...
virtual ParseResult parseAttribute(Attribute &result, Type type={})=0
Parse an arbitrary attribute of a given type and return it in result.
Attributes are known-constant values of operations.
This class represents a diagnostic that is inflight and set to be reported.
MLIRContext is the top-level object for a collection of MLIR operations.
This class implements Optional functionality for ParseResult.
bool has_value() const
Returns true if we contain a valid ParseResult value.
Parses the Sparse Tensor Encoding Attribute (STEA).
FailureOr< DimLvlMap > parseDimLvlMap()
constexpr unsigned getLvlRank() const
constexpr unsigned getSymRank() const
Var bindUnusedVar(VarKind vk)
Creates a new variable of the given kind and immediately binds it.
Ranks getRanks() const
Returns the current ranks of bound variables.
InFlightDiagnostic emitErrorIfAnyUnbound(AsmParser &parser) const
std::optional< std::pair< VarInfo::ID, bool > > lookupOrCreate(Policy creationPolicy, StringRef name, llvm::SMLoc loc, VarKind vk)
Looks up or creates a variable according to the given Policy.
Var getVar(VarInfo::ID id) const
Gets the Var identified by the VarInfo::ID, raising an assertion failure if the variable is not bound...
Var bindVar(VarInfo::ID id)
Binds the given variable to the next free Var::Num for its VarKind.
ID
Newtype for unique identifiers of VarInfo records, to ensure they aren't confused with Var::Num.
A concrete variable, to be used in our variant of AffineExpr.
constexpr Num getNum() const
VarKind
The three kinds of variables that Var can be.
Include the generated interface declarations.
AffineExpr getAffineDimExpr(unsigned position, MLIRContext *context)
These free functions allow clients of the API to not use classes in detail.
AffineExpr getAffineSymbolExpr(unsigned position, MLIRContext *context)
This enum defines all the sparse representations supportable by the SparseTensor dialect.