16#include "mlir/Dialect/Async/IR/AsyncOpsDialect.cpp.inc"
18void AsyncDialect::initialize() {
21#include "mlir/Dialect/Async/IR/AsyncOps.cpp.inc"
24#define GET_TYPEDEF_LIST
25#include "mlir/Dialect/Async/IR/AsyncOpsTypes.cpp.inc"
37 "invalid region index");
38 return getBodyOperands();
41bool ExecuteOp::areTypesCompatible(
Type lhs,
Type rhs) {
42 const auto getValueOrTokenType = [](
Type type) {
43 if (
auto value = llvm::dyn_cast<ValueType>(type))
44 return value.getValueType();
47 return getValueOrTokenType(lhs) == getValueOrTokenType(rhs);
71 ValueRange operands, BodyBuilderFn bodyBuilder) {
73 result.addOperands(dependencies);
74 result.addOperands(operands);
77 int32_t numDependencies = dependencies.size();
78 int32_t numOperands = operands.size();
79 auto operandSegmentSizes =
85 result.addTypes({async::TokenType::get(
result.getContext())});
86 for (
Type type : resultTypes)
87 result.addTypes(ValueType::get(type));
92 for (
Value operand : operands) {
93 auto valueType = llvm::dyn_cast<ValueType>(operand.getType());
94 bodyBlock->
addArgument(valueType ? valueType.getValueType()
102 if (resultTypes.empty() && !bodyBuilder) {
104 }
else if (bodyBuilder) {
111 if (!getDependencies().empty())
112 p <<
" [" << getDependencies() <<
"]";
115 if (!getBodyOperands().empty()) {
118 llvm::interleaveComma(
119 getBodyOperands(), p, [&, n = 0](
Value operand)
mutable {
121 p << operand <<
" as " << argument <<
": " << operand.
getType();
129 (*this)->getDiscardableAttrDictionary().getValue(),
130 {kOperandSegmentSizesAttr});
139 int32_t numDependencies = 0;
141 auto tokenTy = async::TokenType::get(ctx);
151 numDependencies = tokenArgs.size();
160 auto parseAsyncValueArg = [&]() -> ParseResult {
167 auto valueTy = llvm::dyn_cast<ValueType>(valueTypes.back());
168 unwrappedArgs.back().type = valueTy ? valueTy.getValueType() :
Type();
174 parseAsyncValueArg) ||
178 int32_t numOperands = valueArgs.size();
181 auto operandSegmentSizes =
196 result.addAttributes(attrs);
203LogicalResult ExecuteOp::verifyRegions() {
205 auto unwrappedTypes = llvm::map_range(getBodyOperands(), [](
Value operand) {
206 return llvm::cast<ValueType>(operand.
getType()).getValueType();
210 if (getBodyRegion().getArgumentTypes() != unwrappedTypes)
211 return emitOpError(
"async body region argument types do not match the "
212 "execute operation arguments types");
221LogicalResult CreateGroupOp::canonicalize(CreateGroupOp op,
226 auto isAwaitAll = [&](
Operation *op) ->
bool {
227 if (AwaitAllOp awaitAll = dyn_cast<AwaitAllOp>(op)) {
228 awaitAllUsers.push_back(awaitAll);
235 if (!llvm::all_of(op->getUsers(), isAwaitAll))
240 for (AwaitAllOp awaitAll : awaitAllUsers)
253 result.addOperands({operand});
254 result.attributes.append(attrs.begin(), attrs.end());
257 if (
auto valueType = llvm::dyn_cast<ValueType>(operand.
getType()))
258 result.addTypes(valueType.getValueType());
267 if (
auto valueType = llvm::dyn_cast<ValueType>(operandType))
268 resultType = valueType.getValueType();
274 Type operandType,
Type resultType) {
278LogicalResult AwaitOp::verify() {
279 Type argType = getOperand().getType();
282 if (llvm::isa<async::TokenType>(argType) && !getResultTypes().empty())
283 return emitOpError(
"awaiting on a token must have empty result");
286 if (
auto value = llvm::dyn_cast<ValueType>(argType)) {
287 if (*getResultType() != value.getValueType())
288 return emitOpError() <<
"result type " << *getResultType()
289 <<
" does not match async value type "
290 << value.getValueType();
304 state.
addAttribute(getFunctionTypeAttrName(state.
name), TypeAttr::get(type));
309 if (argAttrs.empty())
311 assert(type.getNumInputs() == argAttrs.size());
313 builder, state, argAttrs, {},
314 getArgAttrsAttrName(state.
name), getResAttrsAttrName(state.
name));
325 getFunctionTypeAttrName(
result.name), buildFuncType,
326 getArgAttrsAttrName(
result.name), getResAttrsAttrName(
result.name));
331 p, *
this,
false, getFunctionTypeAttrName(),
332 getArgAttrsAttrName(), getResAttrsAttrName());
337LogicalResult FuncOp::verify() {
338 auto resultTypes = getResultTypes();
339 if (resultTypes.empty())
341 <<
"result is expected to be at least of size 1, but got "
342 << resultTypes.size();
344 for (
unsigned i = 0, e = resultTypes.size(); i != e; ++i) {
345 auto type = resultTypes[i];
346 if (!llvm::isa<async::TokenType>(type) && !llvm::isa<ValueType>(type))
347 return emitOpError() <<
"result type must be async value type or async "
348 "token type, but got "
351 if (llvm::isa<async::TokenType>(type) && i != 0) {
353 <<
" results' (optional) async token type is expected "
354 "to appear as the 1st return value, but got "
368 auto fnAttr = getCalleeAttr();
370 return emitOpError(
"requires a 'callee' symbol reference attribute");
373 return emitOpError() <<
"'" << fnAttr.getValue()
374 <<
"' does not reference a valid async function";
380FunctionType CallOp::getCalleeType() {
381 return FunctionType::get(
getContext(), getOperandTypes(), getResultTypes());
388LogicalResult ReturnOp::verify() {
389 auto funcOp = (*this)->getParentOfType<FuncOp>();
391 ? funcOp.getResultTypes().drop_front()
392 : funcOp.getResultTypes();
395 auto types = llvm::map_range(resultTypes, [](
const Type &
result) {
396 return llvm::cast<ValueType>(
result).getValueType();
399 if (getOperandTypes() != types)
400 return emitOpError(
"operand types do not match the types returned from "
401 "the parent FuncOp");
410#define GET_OP_CLASSES
411#include "mlir/Dialect/Async/IR/AsyncOps.cpp.inc"
417#define GET_TYPEDEF_CLASSES
418#include "mlir/Dialect/Async/IR/AsyncOpsTypes.cpp.inc"
420void ValueType::print(
AsmPrinter &printer)
const {
432 return ValueType::get(ty);
static ParseResult parseAwaitResultType(OpAsmParser &parser, Type &operandType, Type &resultType)
constexpr char kOperandSegmentSizesAttr[]
ExecuteOp.
static void printAwaitResultType(OpAsmPrinter &p, Operation *op, Type operandType, Type resultType)
static Type getValueType(Attribute attr)
This base class exposes generic asm parser hooks, usable across the various derived parsers.
@ OptionalParen
Parens supporting zero or more operands, or nothing.
virtual Builder & getBuilder() const =0
Return a builder which provides useful access to MLIRContext, global objects like types and attribute...
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 InFlightDiagnostic emitError(SMLoc loc, const Twine &message={})=0
Emit a diagnostic at the specified location and return failure.
virtual ParseResult parseRSquare()=0
Parse a ] token.
ParseResult addTypeToList(Type type, SmallVectorImpl< Type > &result)
Add the specified type to the end of the specified type list and return success.
virtual ParseResult parseLess()=0
Parse a '<' token.
virtual ParseResult parseOptionalAttrDictWithKeyword(NamedAttrList &result)=0
Parse a named dictionary into 'result' if the attributes keyword is present.
virtual ParseResult parseColonType(Type &result)=0
Parse a colon followed by a type.
virtual SMLoc getCurrentLocation()=0
Get the location of the next token and store it into the argument.
ParseResult addTypesToList(ArrayRef< Type > types, SmallVectorImpl< Type > &result)
Add the specified types to the end of the specified type list and return success.
virtual SMLoc getNameLoc() const =0
Return the location of the original name token.
virtual ParseResult parseGreater()=0
Parse a '>' token.
virtual ParseResult parseType(Type &result)=0
Parse a type.
virtual ParseResult parseOptionalArrowTypeList(SmallVectorImpl< Type > &result)=0
Parse an optional arrow followed by a type list.
ParseResult parseKeyword(StringRef keyword)
Parse a given keyword.
virtual ParseResult parseOptionalLSquare()=0
Parse a [ token if present.
This base class exposes generic asm printer hooks, usable across the various derived printers.
virtual void printType(Type type)
void printOptionalArrowTypeList(TypeRange &&types)
Print an optional arrow followed by a type list.
Block represents an ordered list of Operations.
BlockArgument getArgument(unsigned i)
BlockArgument addArgument(Type type, Location loc)
Add one value to the argument list.
BlockArgListType getArguments()
This class is a general helper class for creating context-global objects like types,...
DenseI32ArrayAttr getDenseI32ArrayAttr(ArrayRef< int32_t > values)
FunctionType getFunctionType(TypeRange inputs, TypeRange results)
StringAttr getStringAttr(const Twine &bytes)
MLIRContext is the top-level object for a collection of MLIR operations.
NamedAttrList is array of NamedAttributes that tracks whether it is sorted and does some basic work t...
void append(StringRef name, Attribute attr)
Add an attribute with the specified name.
The OpAsmParser has methods for interacting with the asm parser: parsing things from it,...
virtual ParseResult parseRegion(Region ®ion, ArrayRef< Argument > arguments={}, bool enableNameShadowing=false)=0
Parses a region.
virtual ParseResult parseArgument(Argument &result, bool allowType=false, bool allowAttrs=false)=0
Parse a single argument with the following syntax:
ParseResult resolveOperands(Operands &&operands, Type type, SmallVectorImpl< Value > &result)
Resolve a list of operands to SSA values, emitting an error on failure, or appending the results to t...
virtual ParseResult parseOperand(UnresolvedOperand &result, bool allowResultNumber=true)=0
Parse a single SSA value operand name along with a result number if allowResultNumber is true.
virtual ParseResult parseOperandList(SmallVectorImpl< UnresolvedOperand > &result, Delimiter delimiter=Delimiter::None, bool allowResultNumber=true, int requiredOperandCount=-1)=0
Parse zero or more SSA comma-separated operand references with a specified surrounding delimiter,...
This is a pure-virtual base class that exposes the asmprinter hooks necessary to implement a custom p...
virtual void printOptionalAttrDictWithKeyword(ArrayRef< NamedAttribute > attrs, ArrayRef< StringRef > elidedAttrs={})=0
If the specified operation has attributes, print out an attribute dictionary prefixed with 'attribute...
virtual void printRegion(Region &blocks, bool printEntryBlockArgs=true, bool printBlockTerminators=true, bool printEmptyBlock=false)=0
Prints a region.
RAII guard to reset the insertion point of the builder when destroyed.
This class helps build Operations.
Block * createBlock(Region *parent, Region::iterator insertPt={}, TypeRange argTypes={}, ArrayRef< Location > locs={})
Add new block with 'argTypes' arguments and set the insertion point to the end of it.
This class implements the operand iterators for the Operation class.
Operation is the basic unit of execution within MLIR.
A special type of RewriterBase that coordinates the application of a rewrite pattern on the current I...
This class represents a point being branched from in the methods of the RegionBranchOpInterface.
bool isParent() const
Returns true if branching from the parent op.
RegionBranchTerminatorOpInterface getTerminatorPredecessorOrNull() const
Returns the terminator if branching from a region.
This class represents a successor of a region.
Region * getSuccessor() const
Return the given region successor.
bool isOperation() const
Return true if the successor is an operation.
This class contains a list of basic blocks and a link to the parent operation it is attached to.
virtual void eraseOp(Operation *op)
This method erases an operation that is known to have no uses.
This class represents a collection of SymbolTables.
virtual Operation * lookupNearestSymbolFrom(Operation *from, StringAttr symbol)
Returns the operation registered with the given symbol name within the closest parent operation of,...
This class provides an abstraction over the various different ranges of value types.
Instances of the Type class are uniqued, have an immutable identifier and an optional mutable compone...
This class provides an abstraction over the different types of ranges over Values.
This class represents an instance of an SSA value in the MLIR system, representing a computable value...
Type getType() const
Return the type of this value.
A named class for passing around the variadic flag.
LogicalResult verifyCallOpInterface(CallOpInterface call, TypeRange argumentTypes, TypeRange resultTypes)
Verify that the forwarded operands and results of call are in a 1:1 relationship with the given argum...
void addArgAndResultAttrs(Builder &builder, OperationState &result, ArrayRef< DictionaryAttr > argAttrs, ArrayRef< DictionaryAttr > resultAttrs, StringAttr argAttrsName, StringAttr resAttrsName)
Adds argument and result attributes, provided as argAttrs and resultAttrs arguments,...
void printFunctionOp(OpAsmPrinter &p, FunctionOpInterface op, bool isVariadic, StringRef typeAttrName, StringAttr argAttrsName, StringAttr resAttrsName)
Printer implementation for function-like operations.
ParseResult parseFunctionOp(OpAsmParser &parser, OperationState &result, bool allowVariadic, StringAttr typeAttrName, FuncTypeBuilder funcTypeBuilder, StringAttr argAttrsName, StringAttr resAttrsName)
Parser implementation for function-like operations.
Include the generated interface declarations.
This represents an operation in an abstracted form, suitable for use with the builder APIs.
T & getOrAddProperties()
Get (or create) the properties of the provided type to be set on the operation on creation.
void addAttribute(StringRef name, Attribute attr)
Add an attribute with the specified name.
Region * addRegion()
Create a region that should be attached to the operation.