14 #ifndef MLIR_IR_FUNCTIONSUPPORT_H 15 #define MLIR_IR_FUNCTIONSUPPORT_H 18 #include "llvm/ADT/SmallString.h" 30 return (
"arg" + Twine(arg)).toStringRef(out);
36 return (
"result" + Twine(arg)).toStringRef(out);
58 return argDict ? argDict.getValue() :
llvm::None;
64 return resultDict ? resultDict.getValue() :
llvm::None;
99 template <
typename ConcreteType>
116 getBody().dropAllReferences();
117 getBody().getBlocks().clear();
133 bool empty() {
return getBody().empty(); }
153 return this->getOperation()->template getAttrOfType<TypeAttr>(
158 auto typeAttr = getTypeAttr();
161 return typeAttr.getValue() !=
Type{};
169 return static_cast<ConcreteType *
>(
this)->getNumFuncArguments();
173 return static_cast<ConcreteType *
>(
this)->getNumFuncResults();
178 return getBlocks().front().getArgument(idx);
186 return {args_begin(), args_end()};
207 for (
unsigned i = 0, e = getNumArguments(); i != e; ++i)
215 return argDict ? argDict.get(name) :
nullptr;
219 return argDict ? argDict.get(name) :
nullptr;
222 template <
typename AttrClass>
224 return getArgAttr(index, name).template dyn_cast_or_null<AttrClass>();
226 template <
typename AttrClass>
228 return getArgAttr(index, name).template dyn_cast_or_null<AttrClass>();
235 assert(attributes.size() == getNumArguments());
236 for (
unsigned i = 0, e = attributes.size(); i != e; ++i)
237 setArgAttrs(i, attributes[i]);
244 setArgAttr(index,
Identifier::get(name, this->getOperation()->getContext()),
270 for (
unsigned i = 0, e = getNumResults(); i != e; ++i)
278 return argDict ? argDict.get(name) :
nullptr;
282 return argDict ? argDict.get(name) :
nullptr;
285 template <
typename AttrClass>
287 return getResultAttr(index, name).template dyn_cast_or_null<AttrClass>();
289 template <
typename AttrClass>
291 return getResultAttr(index, name).template dyn_cast_or_null<AttrClass>();
298 assert(attributes.size() == getNumResults());
299 for (
unsigned i = 0, e = attributes.size(); i != e; ++i)
300 setResultAttrs(i, attributes[i]);
327 assert(index < getNumArguments() &&
"invalid argument number");
342 assert(index < getNumResults() &&
"invalid result number");
353 template <
typename ConcreteType>
355 auto funcOp = cast<ConcreteType>(this->getOperation());
357 if (funcOp.isExternal())
360 unsigned numArguments = funcOp.getNumArguments();
361 if (funcOp.front().getNumArguments() != numArguments)
362 return funcOp.emitOpError(
"entry block must have ")
363 << numArguments <<
" arguments to match function signature";
368 template <
typename ConcreteType>
371 auto funcOp = cast<ConcreteType>(op);
373 if (!funcOp.isTypeAttrValid())
374 return funcOp.emitOpError(
"requires a type attribute '")
377 if (
failed(funcOp.verifyType()))
380 for (
unsigned i = 0, e = funcOp.getNumArguments(); i != e; ++i) {
384 for (
auto attr : funcOp.getArgAttrs(i)) {
385 if (!attr.first.strref().contains(
'.'))
386 return funcOp.emitOpError(
"arguments may only have dialect attributes");
387 auto dialectNamePair = attr.first.strref().split(
'.');
389 if (
failed(dialect->verifyRegionArgAttribute(op, 0,
396 for (
unsigned i = 0, e = funcOp.getNumResults(); i != e; ++i) {
400 for (
auto attr : funcOp.getResultAttrs(i)) {
401 if (!attr.first.strref().contains(
'.'))
402 return funcOp.emitOpError(
"results may only have dialect attributes");
403 auto dialectNamePair = attr.first.strref().split(
'.');
405 if (
failed(dialect->verifyRegionResultAttribute(op, 0,
415 return funcOp.emitOpError(
"expects one region");
417 return funcOp.verifyBody();
425 template <
typename ConcreteType>
428 assert(index < getNumArguments() &&
"invalid argument number");
432 if (attributes.empty())
433 return (
void)
static_cast<ConcreteType *
>(
this)->removeAttr(nameOut);
438 template <
typename ConcreteType>
441 assert(index < getNumArguments() &&
"invalid argument number");
444 return this->getOperation()->setAttr(
getArgAttrName(index, nameOut),
446 static_cast<ConcreteType *
>(
this)->removeAttr(
getArgAttrName(index, nameOut));
451 template <
typename ConcreteType>
456 attrList.
set(name, value);
460 setArgAttrs(index, attrList);
464 template <
typename ConcreteType>
469 auto result = attrList.
remove(name);
473 setArgAttrs(index, attrList);
482 template <
typename ConcreteType>
485 assert(index < getNumResults() &&
"invalid result number");
489 if (attributes.empty())
490 return (
void)
static_cast<ConcreteType *
>(
this)->removeAttr(nameOut);
495 template <
typename ConcreteType>
498 assert(index < getNumResults() &&
"invalid result number");
503 static_cast<ConcreteType *
>(
this)->removeAttr(
509 template <
typename ConcreteType>
514 attrList.
set(name, value);
518 setResultAttrs(index, attrList);
522 template <
typename ConcreteType>
527 auto result = attrList.
remove(name);
531 setResultAttrs(index, attrList);
539 #endif // MLIR_IR_FUNCTIONSUPPORT_H void setAllResultAttrs(ArrayRef< NamedAttributeList > attributes)
Definition: FunctionSupport.h:297
AttrClass getAttrOfType(Identifier name)
Definition: Operation.h:289
Definition: InferTypeOpInterface.cpp:20
static StringRef getTypeAttrName()
Return the name of the attribute used for function types.
Definition: FunctionSupport.h:150
void setAttr(Identifier name, Attribute value)
Definition: Operation.h:299
StringRef getResultAttrName(unsigned arg, SmallVectorImpl< char > &out)
Return the name of the attribute used for function results.
Definition: FunctionSupport.h:34
Attribute getArgAttr(unsigned index, Identifier name)
Definition: FunctionSupport.h:213
reverse_iterator rbegin()
Definition: FunctionSupport.h:130
Definition: Operation.h:27
BlockArgument getArgument(unsigned idx)
Gets argument.
Definition: FunctionSupport.h:177
Operation & back()
Definition: Block.h:119
unsigned getNumRegions()
Returns the number of regions held by this operation.
Definition: Operation.h:360
Definition: Attributes.h:519
Block represents an ordered list of Operations.
Definition: Block.h:21
LogicalResult verifyType()
Definition: FunctionSupport.h:348
iterator_range< args_iterator > getArguments()
Definition: FunctionSupport.h:185
DictionaryAttr getResultAttrDict(unsigned index)
Definition: FunctionSupport.h:341
void getAllResultAttrs(SmallVectorImpl< NamedAttributeList > &result)
Return all result attributes of this function.
Definition: FunctionSupport.h:269
AttrClass getResultAttrOfType(unsigned index, Identifier name)
Definition: FunctionSupport.h:286
bool failed(LogicalResult result)
Definition: LogicalResult.h:45
static Identifier get(StringRef str, MLIRContext *context)
Return an identifier for the specified string.
Definition: MLIRContext.cpp:426
Definition: Identifier.h:26
static DictionaryAttr get(ArrayRef< NamedAttribute > value, MLIRContext *context)
Definition: Attributes.cpp:88
AttrClass getArgAttrOfType(unsigned index, Identifier name)
Definition: FunctionSupport.h:223
Definition: Attributes.h:269
Operation & front()
Definition: Block.h:120
Dialect * getRegisteredDialect(StringRef name)
Definition: MLIRContext.cpp:315
MLIRContext * getContext()
Return the context this operation is associated with.
Definition: Operation.cpp:248
BlockListType & getBlocks()
Definition: FunctionSupport.h:122
args_iterator args_end()
Definition: FunctionSupport.h:184
BlockArgListType::iterator args_iterator
Definition: Block.h:70
ArrayRef< NamedAttribute > getResultAttrs(unsigned index)
Return all of the attributes for the result at 'index'.
Definition: FunctionSupport.h:264
AttrClass getArgAttrOfType(unsigned index, StringRef name)
Definition: FunctionSupport.h:227
LogicalResult success(bool isSuccess=true)
Definition: LogicalResult.h:25
RemoveResult remove(Identifier name)
Definition: Attributes.cpp:1082
Definition: LogicalResult.h:18
LogicalResult failure(bool isFailure=true)
Definition: LogicalResult.h:32
DictionaryAttr getArgAttrDict(unsigned index)
Definition: FunctionSupport.h:326
static StringRef getResultAttrName(unsigned index, SmallVectorImpl< char > &out)
Definition: FunctionSupport.h:333
bool isExternal()
Returns true if this function is external, i.e. it has no body.
Definition: FunctionSupport.h:110
DictionaryAttr getDictionary() const
Definition: Attributes.h:1389
TypeAttr getTypeAttr()
Definition: FunctionSupport.h:152
iterator end()
Definition: FunctionSupport.h:129
Definition: Attributes.h:53
Block & front()
Definition: FunctionSupport.h:138
Attribute getResultAttr(unsigned index, Identifier name)
Definition: FunctionSupport.h:276
Block::args_iterator args_iterator
Definition: FunctionSupport.h:182
unsigned getNumArguments()
Definition: FunctionSupport.h:168
void setArgAttr(unsigned index, StringRef name, Attribute value)
Definition: FunctionSupport.h:243
ArrayRef< NamedAttribute > getArgAttrs(unsigned index)
Return all of the attributes for the argument at 'index'.
Definition: FunctionSupport.h:201
Definition: Attributes.h:1374
bool empty()
Definition: FunctionSupport.h:133
Block arguments are values.
Definition: Value.h:235
args_iterator args_begin()
Definition: FunctionSupport.h:183
ArrayRef< NamedAttribute > getResultAttrs(Operation *op, unsigned index)
Return all of the attributes for the result at 'index'.
Definition: FunctionSupport.h:62
DictionaryAttr getArgAttrDict(Operation *op, unsigned index)
Definition: FunctionSupport.h:42
Region & getBody()
Definition: FunctionSupport.h:112
reverse_iterator rend()
Definition: FunctionSupport.h:131
void eraseBody()
Delete all blocks from this function.
Definition: FunctionSupport.h:115
StringRef getTypeAttrName()
Return the name of the attribute used for function types.
Definition: FunctionSupport.h:25
Definition: OpDefinition.h:386
void setResultAttr(unsigned index, StringRef name, Attribute value)
Definition: FunctionSupport.h:306
ArrayRef< NamedAttribute > getArgAttrs(Operation *op, unsigned index)
Return all of the attributes for the argument at 'index'.
Definition: FunctionSupport.h:56
Attribute getResultAttr(unsigned index, StringRef name)
Definition: FunctionSupport.h:280
BlockListType::iterator iterator
Definition: FunctionSupport.h:125
StringRef getArgAttrName(unsigned arg, SmallVectorImpl< char > &out)
Return the name of the attribute used for function arguments.
Definition: FunctionSupport.h:28
AttrClass getResultAttrOfType(unsigned index, StringRef name)
Definition: FunctionSupport.h:290
Definition: MLIRContext.h:34
static StringRef getArgAttrName(unsigned index, SmallVectorImpl< char > &out)
Definition: FunctionSupport.h:319
void push_back(Block *block)
Definition: FunctionSupport.h:134
Block & back()
Definition: FunctionSupport.h:137
void setAllArgAttrs(ArrayRef< NamedAttributeList > attributes)
Definition: FunctionSupport.h:234
bool isTypeAttrValid()
Definition: FunctionSupport.h:157
Definition: StandardTypes.h:63
Region::BlockListType BlockListType
This is the list of blocks in the function.
Definition: FunctionSupport.h:121
RemoveResult
Definition: Attributes.h:1405
Attribute getArgAttr(unsigned index, StringRef name)
Definition: FunctionSupport.h:217
unsigned getNumResults()
Definition: FunctionSupport.h:172
DictionaryAttr getResultAttrDict(Operation *op, unsigned index)
Definition: FunctionSupport.h:50
iterator begin()
Definition: FunctionSupport.h:128
void set(Identifier name, Attribute value)
Definition: Attributes.cpp:1062
Definition: FunctionSupport.h:100
BlockListType::reverse_iterator reverse_iterator
Definition: FunctionSupport.h:126
void push_front(Block *block)
Definition: FunctionSupport.h:135
llvm::iplist< Block > BlockListType
Definition: Region.h:37
void getAllArgAttrs(SmallVectorImpl< NamedAttributeList > &result)
Return all argument attributes of this function.
Definition: FunctionSupport.h:206