16 #include "llvm/ADT/ArrayRef.h"
17 #include "llvm/ADT/STLExtras.h"
18 #include "llvm/ADT/StringExtras.h"
19 #include "llvm/ADT/StringMap.h"
20 #include "llvm/ADT/iterator.h"
21 #include "llvm/Support/ErrorHandling.h"
92 for (
auto &it : *opAndUseMapIt.second) {
95 opAndUseMapIt.first, cast<SymbolRefAttr>(it.first), symbolOps)))
99 for (
const auto &symIt : llvm::zip(symbolOps, useRange)) {
102 operations[opIt->second]->symbolUses.push_back(std::get<1>(symIt));
113 AsmParserState::AsmParserState() :
impl(std::make_unique<
Impl>()) {}
116 impl = std::move(other.impl);
129 auto it =
impl->blocksToIdx.find(block);
130 return it ==
impl->blocksToIdx.end() ? nullptr : &*
impl->blocks[it->second];
139 auto it =
impl->operationToIdx.find(op);
140 return it ==
impl->operationToIdx.end() ? nullptr
141 : &*
impl->operations[it->second];
146 return llvm::make_pointee_range(
ArrayRef(
impl->attrAliases));
151 auto it =
impl->attrAliasToIdx.find(name);
152 return it ==
impl->attrAliasToIdx.end() ? nullptr
153 : &*
impl->attrAliases[it->second];
158 return llvm::make_pointee_range(
ArrayRef(
impl->typeAliases));
163 auto it =
impl->typeAliasToIdx.find(name);
164 return it ==
impl->typeAliasToIdx.end() ? nullptr
165 : &*
impl->typeAliases[it->second];
172 while (
char c = *curPtr++) {
174 if (StringRef(
"\"\n\v\f").
contains(c))
180 if (*curPtr ==
'"' || *curPtr ==
'\\' || *curPtr ==
'n' || *curPtr ==
't')
182 else if (llvm::isHexDigit(*curPtr) && llvm::isHexDigit(curPtr[1]))
197 const char *curPtr = loc.getPointer();
200 if (*curPtr ==
'"') {
206 auto isIdentifierChar = [](
char c) {
207 return isalnum(c) || c ==
'$' || c ==
'.' || c ==
'_' || c ==
'-';
210 while (*curPtr && isIdentifierChar(*(++curPtr)))
214 return SMRange(loc, SMLoc::getFromPointer(curPtr));
230 assert(!
impl->partialOperations.empty() &&
231 "expected valid partial operation definition");
236 impl->symbolTableOperations.emplace_back(
239 impl->resolveSymbolUses();
243 impl->partialOperations.emplace_back(opName);
247 Operation *op, SMRange nameLoc, SMLoc endLoc,
248 ArrayRef<std::pair<unsigned, SMLoc>> resultGroups) {
249 assert(!
impl->partialOperations.empty() &&
250 "expected valid partial operation definition");
254 std::unique_ptr<OperationDefinition> def =
255 std::make_unique<OperationDefinition>(op, nameLoc, endLoc);
256 for (
auto &resultGroup : resultGroups)
257 def->resultGroups.emplace_back(resultGroup.first,
259 impl->operationToIdx.try_emplace(op,
impl->operations.size());
260 impl->operations.emplace_back(std::move(def));
264 impl->symbolTableOperations.emplace_back(
270 assert(!
impl->partialOperations.empty() &&
271 "expected valid partial operation definition");
281 assert(!
impl->partialOperations.empty() &&
282 "expected valid partial operation definition");
288 impl->symbolUseScopes.pop_back();
292 auto [it, inserted] =
293 impl->blocksToIdx.try_emplace(block,
impl->blocks.size());
295 impl->blocks.emplace_back(std::make_unique<BlockDefinition>(
307 assert(it !=
impl->blocksToIdx.end() &&
308 "expected owner block to have an entry");
319 auto [it, inserted] =
320 impl->attrAliasToIdx.try_emplace(name,
impl->attrAliases.size());
323 impl->attrAliases.push_back(
324 std::make_unique<AttributeAliasDefinition>(name, location, value));
334 [[maybe_unused]]
auto [it, inserted] =
335 impl->typeAliasToIdx.try_emplace(name,
impl->typeAliases.size());
336 assert(inserted &&
"unexpected attribute alias redefinition");
337 impl->typeAliases.push_back(
338 std::make_unique<TypeAliasDefinition>(name, location, value));
343 if (
OpResult result = dyn_cast<OpResult>(value)) {
348 auto existingIt =
impl->operationToIdx.find(parentOp);
349 if (existingIt ==
impl->operationToIdx.end()) {
350 impl->placeholderValueUses[value].append(locations.begin(),
358 unsigned resultNo = result.getResultNumber();
360 for (
auto &resultGroup : llvm::reverse(def.
resultGroups)) {
361 if (resultNo >= resultGroup.startIndex) {
362 for (SMLoc loc : locations)
367 llvm_unreachable(
"expected valid result group for value use");
372 auto existingIt =
impl->blocksToIdx.find(arg.
getOwner());
373 assert(existingIt !=
impl->blocksToIdx.end() &&
374 "expected valid block definition for block argument");
377 for (SMLoc loc : locations)
382 auto [it, inserted] =
383 impl->blocksToIdx.try_emplace(block,
impl->blocks.size());
385 impl->blocks.emplace_back(std::make_unique<BlockDefinition>(block));
388 for (SMLoc loc : locations)
395 if (
impl->symbolUseScopes.empty())
398 assert((refAttr.getNestedReferences().size() + 1) == locations.size() &&
399 "expected the same number of references as provided locations");
400 (*
impl->symbolUseScopes.back())[refAttr].emplace_back(locations.begin(),
405 auto it =
impl->attrAliasToIdx.find(name);
407 if (it ==
impl->attrAliasToIdx.end()) {
408 it =
impl->attrAliasToIdx.try_emplace(name,
impl->attrAliases.size()).first;
409 impl->attrAliases.push_back(
410 std::make_unique<AttributeAliasDefinition>(name));
417 auto it =
impl->typeAliasToIdx.find(name);
419 assert(it !=
impl->typeAliasToIdx.end() &&
420 "expected valid type alias definition");
426 auto it =
impl->placeholderValueUses.find(oldValue);
427 assert(it !=
impl->placeholderValueUses.end() &&
428 "expected `oldValue` to be a placeholder");
430 impl->placeholderValueUses.erase(oldValue);
static const char * lexLocStringTok(const char *curPtr)
Lex a string token whose contents start at the given curPtr.
static bool contains(SMRange range, SMLoc loc)
Returns true if the given range contains the given source location.
This class represents state from a parsed MLIR textual format string.
void startRegionDefinition()
Start a definition for a region nested under the current operation.
iterator_range< AttributeDefIterator > getAttributeAliasDefs() const
Return a range of the AttributeAliasDefinitions held by the current parser state.
void startOperationDefinition(const OperationName &opName)
Start a definition for an operation with the given name.
llvm::pointee_iterator< ArrayRef< std::unique_ptr< OperationDefinition > >::iterator > OperationDefIterator
void addTypeAliasUses(StringRef name, SMRange locations)
llvm::pointee_iterator< ArrayRef< std::unique_ptr< TypeAliasDefinition > >::iterator > TypeDefIterator
void finalizeOperationDefinition(Operation *op, SMRange nameLoc, SMLoc endLoc, ArrayRef< std::pair< unsigned, SMLoc >> resultGroups=std::nullopt)
Finalize the most recently started operation definition.
iterator_range< BlockDefIterator > getBlockDefs() const
Return a range of the BlockDefinitions held by the current parser state.
const OperationDefinition * getOpDef(Operation *op) const
Return the definition for the given operation, or nullptr if the given operation does not have a defi...
void initialize(Operation *topLevelOp)
Initialize the state in preparation for populating more parser state under the given top-level operat...
void addAttrAliasUses(StringRef name, SMRange locations)
const AttributeAliasDefinition * getAttributeAliasDef(StringRef name) const
Return the definition for the given attribute alias, or nullptr if the given alias does not have a de...
const BlockDefinition * getBlockDef(Block *block) const
Return the definition for the given block, or nullptr if the given block does not have a definition.
const TypeAliasDefinition * getTypeAliasDef(StringRef name) const
Return the definition for the given type alias, or nullptr if the given alias does not have a definit...
void addAttrAliasDefinition(StringRef name, SMRange location, Attribute value)
void finalize(Operation *topLevelOp)
Finalize any in-progress parser state under the given top-level operation.
static SMRange convertIdLocToRange(SMLoc loc)
Returns (heuristically) the range of an identifier given a SMLoc corresponding to the start of an ide...
void addUses(Value value, ArrayRef< SMLoc > locations)
Add a source uses of the given value.
void refineDefinition(Value oldValue, Value newValue)
Refine the oldValue to the newValue.
iterator_range< OperationDefIterator > getOpDefs() const
Return a range of the OperationDefinitions held by the current parser state.
void finalizeRegionDefinition()
Finalize the most recently started region definition.
iterator_range< TypeDefIterator > getTypeAliasDefs() const
Return a range of the TypeAliasDefinitions held by the current parser state.
llvm::pointee_iterator< ArrayRef< std::unique_ptr< AttributeAliasDefinition > >::iterator > AttributeDefIterator
llvm::pointee_iterator< ArrayRef< std::unique_ptr< BlockDefinition > >::iterator > BlockDefIterator
void addTypeAliasDefinition(StringRef name, SMRange location, Type value)
AsmParserState & operator=(AsmParserState &&other)
void addDefinition(Block *block, SMLoc location)
Add a definition of the given entity.
Attributes are known-constant values of operations.
This class represents an argument of a Block.
Block * getOwner() const
Returns the block that owns this argument.
unsigned getArgNumber() const
Returns the number of this argument.
Block represents an ordered list of Operations.
This is a value defined by a result of an operation.
A trait used to provide symbol table functionalities to a region operation.
bool hasTrait() const
Returns true if the operation was registered with a particular trait, e.g.
Operation is the basic unit of execution within MLIR.
OperationName getName()
The name of an operation is the key identifier for it.
This class represents a collection of SymbolTables.
Operation * lookupSymbolIn(Operation *symbolTableOp, StringAttr symbol)
Look up a symbol with the specified name within the specified symbol table operation,...
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...
Include the generated interface declarations.
bool isSymbolTable() const
Return if this operation is a symbol table.
std::unique_ptr< SymbolUseMap > symbolTable
If this operation is a symbol table, the following contains symbol uses within this operation.
PartialOpDef(const OperationName &opName)
SymbolTableCollection symbolTable
A symbol table containing all of the symbol table operations in the IR.
DenseMap< Value, SmallVector< SMLoc > > placeholderValueUses
A set of value definitions that are placeholders for forward references.
void resolveSymbolUses()
Resolve any symbol table uses in the IR.
llvm::StringMap< unsigned > typeAliasToIdx
SmallVector< std::unique_ptr< OperationDefinition > > operations
A mapping from operations in the input source file to their parser state.
SmallVector< std::unique_ptr< AttributeAliasDefinition > > attrAliases
A mapping from aliases in the input source file to their parser state.
DenseMap< Operation *, unsigned > operationToIdx
SmallVector< std::unique_ptr< TypeAliasDefinition > > typeAliases
SmallVector< PartialOpDef > partialOperations
A stack of partial operation definitions that have been started but not yet finalized.
SmallVector< std::unique_ptr< BlockDefinition > > blocks
A mapping from blocks in the input source file to their parser state.
llvm::StringMap< unsigned > attrAliasToIdx
DenseMap< Block *, unsigned > blocksToIdx
SmallVector< std::pair< Operation *, std::unique_ptr< SymbolUseMap > > > symbolTableOperations
The symbol table operations within the IR.
SmallVector< SymbolUseMap * > symbolUseScopes
A stack of symbol use scopes.
This class represents the information for an attribute alias definition within the input file.
SMDefinition definition
The source location for the alias.
Attribute value
The value of the alias.
This class represents the information for a block definition within the input file.
SmallVector< SMDefinition > arguments
Source definitions for any arguments of this block.
SMDefinition definition
The source location for the block, i.e.
This class represents the information for an operation definition within an input file.
SmallVector< ResultGroupDefinition > resultGroups
Source definitions for any result groups of this operation.
This class represents a definition within the source manager, containing it's defining location and l...
SmallVector< SMRange > uses
The source location of all uses of the definition.
SMRange loc
The source location of the definition.
This class represents the information for type definition within the input file.
SMDefinition definition
The source location for the alias.