MLIR: lib/AsmParser/AsmParserState.cpp Source File (original) (raw)

1

2

3

4

5

6

7

8

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"

22#include

23#include

24#include

25#include

26

27using namespace mlir;

28

29

30

31

32

34

37

41 symbolTable = std::make_unique();

42 }

43

44

46

47

48

50 };

51

52

54

55

58

59

62

63

68

69

70

72

73

76

77

78

80

81

82

84

85

87};

88

92 for (auto &it : *opAndUseMapIt.second) {

93 symbolOps.clear();

95 opAndUseMapIt.first, cast(it.first), symbolOps)))

96 continue;

97

99 for (const auto &symIt : llvm::zip(symbolOps, useRange)) {

102 operations[opIt->second]->symbolUses.push_back(std::get<1>(symIt));

103 }

104 }

105 }

106 }

107}

108

109

110

111

112

116 impl = std::move(other.impl);

117 return *this;

118}

119

120

121

122

123

127

130 auto it = impl->blocksToIdx.find(block);

131 return it == impl->blocksToIdx.end() ? nullptr : &*impl->blocks[it->second];

132}

133

137

140 auto it = impl->operationToIdx.find(op);

141 return it == impl->operationToIdx.end() ? nullptr

142 : &*impl->operations[it->second];

143}

144

147 return llvm::make_pointee_range(ArrayRef(impl->attrAliases));

148}

149

152 auto it = impl->attrAliasToIdx.find(name);

153 return it == impl->attrAliasToIdx.end() ? nullptr

154 : &*impl->attrAliases[it->second];

155}

156

159 return llvm::make_pointee_range(ArrayRef(impl->typeAliases));

160}

161

164 auto it = impl->typeAliasToIdx.find(name);

165 return it == impl->typeAliasToIdx.end() ? nullptr

166 : &*impl->typeAliases[it->second];

167}

168

169

170

171

173 while (char c = *curPtr++) {

174

175 if (StringRef("\"\n\v\f").contains(c))

176 return curPtr;

177

178

179 if (c == '\\') {

180

181 if (*curPtr == '"' || *curPtr == '\\' || *curPtr == 'n' || *curPtr == 't')

182 ++curPtr;

183 else if (llvm::isHexDigit(*curPtr) && llvm::isHexDigit(curPtr[1]))

184 curPtr += 2;

185 else

186 return curPtr;

187 }

188 }

189

190

191

192 return curPtr - 1;

193}

194

196 if (!loc.isValid())

197 return SMRange();

198 const char *curPtr = loc.getPointer();

199

200

201 if (*curPtr == '"') {

203

204

205 } else {

206

207 auto isIdentifierChar = [](char c) {

208 return isalnum(c) || c == '$' || c == '.' || c == '_' || c == '-';

209 };

210

211 while (*curPtr && isIdentifierChar(*(++curPtr)))

212 continue;

213 }

214

215 return SMRange(loc, SMLoc::getFromPointer(curPtr));

216}

217

218

219

220

221

224

225

228 impl->symbolUseScopes.push_back(partialOpDef.symbolTable.get());

229}

230

232 assert(impl->partialOperations.empty() &&

233 "expected valid partial operation definition");

235

236

238 impl->symbolTableOperations.emplace_back(

239 topLevelOp, std::move(partialOpDef.symbolTable));

240 }

241 impl->resolveSymbolUses();

242}

243

245 impl->partialOperations.emplace_back(opName);

246}

247

249 Operation *op, SMRange nameLoc, SMLoc endLoc,

250 ArrayRef<std::pair<unsigned, SMLoc>> resultGroups) {

251 assert(impl->partialOperations.empty() &&

252 "expected valid partial operation definition");

254

255

256 std::unique_ptr def =

257 std::make_unique(op, nameLoc, endLoc);

258 for (auto &resultGroup : resultGroups)

259 def->resultGroups.emplace_back(resultGroup.first,

261 impl->operationToIdx.try_emplace(op, impl->operations.size());

262 impl->operations.emplace_back(std::move(def));

263

264

266 impl->symbolTableOperations.emplace_back(

267 op, std::move(partialOpDef.symbolTable));

268 }

269}

270

272 assert(impl->partialOperations.empty() &&

273 "expected valid partial operation definition");

274

275

276

279 impl->symbolUseScopes.push_back(partialOpDef.symbolTable.get());

280}

281

283 assert(impl->partialOperations.empty() &&

284 "expected valid partial operation definition");

285

286

287

290 impl->symbolUseScopes.pop_back();

291}

292

295 impl->blocksToIdx.try_emplace(block, impl->blocks.size());

297 impl->blocks.emplace_back(std::make_unique(

299 return;

300 }

301

302

303

305}

306

308 auto it = impl->blocksToIdx.find(blockArg.getOwner());

309 assert(it != impl->blocksToIdx.end() &&

310 "expected owner block to have an entry");

313

314 if (def.arguments.size() <= argIdx)

315 def.arguments.resize(argIdx + 1);

317}

318

322 impl->attrAliasToIdx.try_emplace(name, impl->attrAliases.size());

323

325 impl->attrAliases.push_back(

326 std::make_unique(name, location, value));

327 } else {

330 attr.value = value;

331 }

332}

333

335 Type value) {

336 [[maybe_unused]] auto [it, inserted] =

337 impl->typeAliasToIdx.try_emplace(name, impl->typeAliases.size());

338 assert(inserted && "unexpected attribute alias redefinition");

339 impl->typeAliases.push_back(

340 std::make_unique(name, location, value));

341}

342

344

346

347

348

350 auto existingIt = impl->operationToIdx.find(parentOp);

351 if (existingIt == impl->operationToIdx.end()) {

352 impl->placeholderValueUses[value].append(locations.begin(),

353 locations.end());

354 return;

355 }

356

357

358

359

360 unsigned resultNo = result.getResultNumber();

362 for (auto &resultGroup : llvm::reverse(def.resultGroups)) {

363 if (resultNo >= resultGroup.startIndex) {

364 for (SMLoc loc : locations)

366 return;

367 }

368 }

369 llvm_unreachable("expected valid result group for value use");

370 }

371

372

374 auto existingIt = impl->blocksToIdx.find(arg.getOwner());

375 assert(existingIt != impl->blocksToIdx.end() &&

376 "expected valid block definition for block argument");

379 for (SMLoc loc : locations)

381}

382

385 impl->blocksToIdx.try_emplace(block, impl->blocks.size());

387 impl->blocks.emplace_back(std::make_unique(block));

388

390 for (SMLoc loc : locations)

392}

393

396

397 if (impl->symbolUseScopes.empty())

398 return;

399

400 assert((refAttr.getNestedReferences().size() + 1) == locations.size() &&

401 "expected the same number of references as provided locations");

402 (*impl->symbolUseScopes.back())[refAttr].emplace_back(locations.begin(),

403 locations.end());

404}

405

407 auto it = impl->attrAliasToIdx.find(name);

408

409 if (it == impl->attrAliasToIdx.end()) {

410 it = impl->attrAliasToIdx.try_emplace(name, impl->attrAliases.size()).first;

411 impl->attrAliases.push_back(

412 std::make_unique(name));

413 }

416}

417

419 auto it = impl->typeAliasToIdx.find(name);

420

421 assert(it != impl->typeAliasToIdx.end() &&

422 "expected valid type alias definition");

425}

426

428 auto it = impl->placeholderValueUses.find(oldValue);

429 assert(it != impl->placeholderValueUses.end() &&

430 "expected `oldValue` to be a placeholder");

431 addUses(newValue, it->second);

432 impl->placeholderValueUses.erase(oldValue);

433}

static const char * lexLocStringTok(const char *curPtr)

Lex a string token whose contents start at the given curPtr.

Definition AsmParserState.cpp:172

*if copies could not be generated due to yet unimplemented cases *copyInPlacementStart and copyOutPlacementStart in copyPlacementBlock *specify the insertion points where the incoming copies and outgoing should be inserted(the insertion happens right before the *insertion point). Since `begin` can itself be invalidated due to the memref *rewriting done from this method

static bool contains(SMRange range, SMLoc loc)

Returns true if the given range contains the given source location.

llvm::pointee_iterator< ArrayRef< std::unique_ptr< AttributeAliasDefinition > >::iterator > AttributeDefIterator

void startRegionDefinition()

Start a definition for a region nested under the current operation.

Definition AsmParserState.cpp:271

iterator_range< AttributeDefIterator > getAttributeAliasDefs() const

Return a range of the AttributeAliasDefinitions held by the current parser state.

Definition AsmParserState.cpp:145

void startOperationDefinition(const OperationName &opName)

Start a definition for an operation with the given name.

Definition AsmParserState.cpp:244

AsmParserState()

Definition AsmParserState.cpp:113

void addTypeAliasUses(StringRef name, SMRange locations)

Definition AsmParserState.cpp:418

iterator_range< BlockDefIterator > getBlockDefs() const

Return a range of the BlockDefinitions held by the current parser state.

Definition AsmParserState.cpp:124

llvm::pointee_iterator< ArrayRef< std::unique_ptr< TypeAliasDefinition > >::iterator > TypeDefIterator

const OperationDefinition * getOpDef(Operation *op) const

Return the definition for the given operation, or nullptr if the given operation does not have a defi...

Definition AsmParserState.cpp:138

llvm::pointee_iterator< ArrayRef< std::unique_ptr< OperationDefinition > >::iterator > OperationDefIterator

void initialize(Operation *topLevelOp)

Initialize the state in preparation for populating more parser state under the given top-level operat...

Definition AsmParserState.cpp:222

void finalizeOperationDefinition(Operation *op, SMRange nameLoc, SMLoc endLoc, ArrayRef< std::pair< unsigned, SMLoc > > resultGroups={})

Finalize the most recently started operation definition.

Definition AsmParserState.cpp:248

void addAttrAliasUses(StringRef name, SMRange locations)

Definition AsmParserState.cpp:406

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...

Definition AsmParserState.cpp:150

llvm::pointee_iterator< ArrayRef< std::unique_ptr< BlockDefinition > >::iterator > BlockDefIterator

const BlockDefinition * getBlockDef(Block *block) const

Return the definition for the given block, or nullptr if the given block does not have a definition.

Definition AsmParserState.cpp:128

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...

Definition AsmParserState.cpp:162

void addAttrAliasDefinition(StringRef name, SMRange location, Attribute value)

Definition AsmParserState.cpp:319

void finalize(Operation *topLevelOp)

Finalize any in-progress parser state under the given top-level operation.

Definition AsmParserState.cpp:231

static SMRange convertIdLocToRange(SMLoc loc)

Returns (heuristically) the range of an identifier given a SMLoc corresponding to the start of an ide...

Definition AsmParserState.cpp:195

void addUses(Value value, ArrayRef< SMLoc > locations)

Add a source uses of the given value.

Definition AsmParserState.cpp:343

void refineDefinition(Value oldValue, Value newValue)

Refine the oldValue to the newValue.

Definition AsmParserState.cpp:427

iterator_range< OperationDefIterator > getOpDefs() const

Return a range of the OperationDefinitions held by the current parser state.

Definition AsmParserState.cpp:134

void finalizeRegionDefinition()

Finalize the most recently started region definition.

Definition AsmParserState.cpp:282

iterator_range< TypeDefIterator > getTypeAliasDefs() const

Return a range of the TypeAliasDefinitions held by the current parser state.

Definition AsmParserState.cpp:157

void addTypeAliasDefinition(StringRef name, SMRange location, Type value)

Definition AsmParserState.cpp:334

AsmParserState & operator=(AsmParserState &&other)

Definition AsmParserState.cpp:115

void addDefinition(Block *block, SMLoc location)

Add a definition of the given entity.

Definition AsmParserState.cpp:293

Attributes are known-constant values of operations.

This class represents an argument of a Block.

unsigned getArgNumber() const

Returns the number of this argument.

Block * getOwner() const

Returns the block that owns 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.

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.

llvm::DenseMap< KeyT, ValueT, KeyInfoT, BucketT > DenseMap

Definition AsmParserState.cpp:38

bool isSymbolTable() const

Return if this operation is a symbol table.

Definition AsmParserState.cpp:45

std::unique_ptr< SymbolUseMap > symbolTable

If this operation is a symbol table, the following contains symbol uses within this operation.

Definition AsmParserState.cpp:49

PartialOpDef(const OperationName &opName)

Definition AsmParserState.cpp:39

Definition AsmParserState.cpp:33

SymbolTableCollection symbolTable

A symbol table containing all of the symbol table operations in the IR.

Definition AsmParserState.cpp:86

DenseMap< Value, SmallVector< SMLoc > > placeholderValueUses

A set of value definitions that are placeholders for forward references.

Definition AsmParserState.cpp:71

DenseMap< Attribute, SmallVector< SmallVector< SMRange >, 0 > > SymbolUseMap

A map from a SymbolRefAttr to a range of uses.

Definition AsmParserState.cpp:35

void resolveSymbolUses()

Resolve any symbol table uses in the IR.

Definition AsmParserState.cpp:89

llvm::StringMap< unsigned > typeAliasToIdx

Definition AsmParserState.cpp:67

SmallVector< std::unique_ptr< OperationDefinition > > operations

A mapping from operations in the input source file to their parser state.

Definition AsmParserState.cpp:56

SmallVector< std::unique_ptr< AttributeAliasDefinition > > attrAliases

A mapping from aliases in the input source file to their parser state.

Definition AsmParserState.cpp:64

DenseMap< Operation *, unsigned > operationToIdx

Definition AsmParserState.cpp:57

SmallVector< std::unique_ptr< TypeAliasDefinition > > typeAliases

Definition AsmParserState.cpp:65

SmallVector< PartialOpDef > partialOperations

A stack of partial operation definitions that have been started but not yet finalized.

Definition AsmParserState.cpp:79

SmallVector< std::unique_ptr< BlockDefinition > > blocks

A mapping from blocks in the input source file to their parser state.

Definition AsmParserState.cpp:60

llvm::StringMap< unsigned > attrAliasToIdx

Definition AsmParserState.cpp:66

DenseMap< Block *, unsigned > blocksToIdx

Definition AsmParserState.cpp:61

SmallVector< std::pair< Operation *, std::unique_ptr< SymbolUseMap > > > symbolTableOperations

The symbol table operations within the IR.

Definition AsmParserState.cpp:75

SmallVector< SymbolUseMap * > symbolUseScopes

A stack of symbol use scopes.

Definition AsmParserState.cpp:83

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.