14 #include "llvm/ADT/MapVector.h"
15 #include "llvm/ADT/TypeSwitch.h"
20 #include "mlir/Dialect/Async/IR/AsyncOpsDialect.cpp.inc"
22 constexpr StringRef AsyncDialect::kAllowedToBlockAttrName;
24 void AsyncDialect::initialize() {
27 #include "mlir/Dialect/Async/IR/AsyncOps.cpp.inc"
30 #define GET_TYPEDEF_LIST
31 #include "mlir/Dialect/Async/IR/AsyncOpsTypes.cpp.inc"
42 assert(point == getBodyRegion() &&
"invalid region index");
43 return getBodyOperands();
46 bool ExecuteOp::areTypesCompatible(
Type lhs,
Type rhs) {
47 const auto getValueOrTokenType = [](
Type type) {
48 if (
auto value = llvm::dyn_cast<ValueType>(type))
49 return value.getValueType();
52 return getValueOrTokenType(lhs) == getValueOrTokenType(rhs);
58 if (point == getBodyRegion()) {
70 ValueRange operands, BodyBuilderFn bodyBuilder) {
76 int32_t numDependencies = dependencies.size();
77 int32_t numOperands = operands.size();
78 auto operandSegmentSizes =
85 for (
Type type : resultTypes)
91 for (
Value operand : operands) {
92 auto valueType = llvm::dyn_cast<ValueType>(operand.getType());
93 bodyBlock->
addArgument(valueType ? valueType.getValueType()
101 if (resultTypes.empty() && !bodyBuilder) {
103 }
else if (bodyBuilder) {
110 if (!getDependencies().empty())
111 p <<
" [" << getDependencies() <<
"]";
114 if (!getBodyOperands().empty()) {
116 Block *entry = getBodyRegion().
empty() ? nullptr : &getBodyRegion().front();
117 llvm::interleaveComma(
118 getBodyOperands(), p, [&, n = 0](
Value operand)
mutable {
120 p << operand <<
" as " << argument <<
": " << operand.
getType();
128 {kOperandSegmentSizesAttr});
137 int32_t numDependencies = 0;
149 numDependencies = tokenArgs.size();
158 auto parseAsyncValueArg = [&]() -> ParseResult {
165 auto valueTy = llvm::dyn_cast<ValueType>(valueTypes.back());
166 unwrappedArgs.back().type = valueTy ? valueTy.getValueType() :
Type();
172 parseAsyncValueArg) ||
176 int32_t numOperands = valueArgs.size();
179 auto operandSegmentSizes =
201 LogicalResult ExecuteOp::verifyRegions() {
203 auto unwrappedTypes = llvm::map_range(getBodyOperands(), [](
Value operand) {
204 return llvm::cast<ValueType>(operand.
getType()).getValueType();
208 if (getBodyRegion().getArgumentTypes() != unwrappedTypes)
209 return emitOpError(
"async body region argument types do not match the "
210 "execute operation arguments types");
219 LogicalResult CreateGroupOp::canonicalize(CreateGroupOp op,
224 auto isAwaitAll = [&](
Operation *op) ->
bool {
225 if (AwaitAllOp awaitAll = dyn_cast<AwaitAllOp>(op)) {
226 awaitAllUsers.push_back(awaitAll);
233 if (!llvm::all_of(op->getUsers(), isAwaitAll))
238 for (AwaitAllOp awaitAll : awaitAllUsers)
255 if (
auto valueType = llvm::dyn_cast<ValueType>(operand.
getType()))
256 result.
addTypes(valueType.getValueType());
265 if (
auto valueType = llvm::dyn_cast<ValueType>(operandType))
266 resultType = valueType.getValueType();
272 Type operandType,
Type resultType) {
277 Type argType = getOperand().getType();
280 if (llvm::isa<TokenType>(argType) && !getResultTypes().empty())
281 return emitOpError(
"awaiting on a token must have empty result");
284 if (
auto value = llvm::dyn_cast<ValueType>(argType)) {
285 if (*getResultType() != value.getValueType())
286 return emitOpError() <<
"result type " << *getResultType()
287 <<
" does not match async value type "
288 << value.getValueType();
303 state.addAttribute(getFunctionTypeAttrName(state.name),
TypeAttr::get(type));
305 state.attributes.append(attrs.begin(), attrs.end());
308 if (argAttrs.empty())
310 assert(type.getNumInputs() == argAttrs.size());
312 builder, state, argAttrs, std::nullopt,
313 getArgAttrsAttrName(state.name), getResAttrsAttrName(state.name));
323 parser, result,
false,
324 getFunctionTypeAttrName(result.
name), buildFuncType,
325 getArgAttrsAttrName(result.
name), getResAttrsAttrName(result.
name));
330 p, *
this,
false, getFunctionTypeAttrName(),
331 getArgAttrsAttrName(), getResAttrsAttrName());
337 auto resultTypes = getResultTypes();
338 if (resultTypes.empty())
340 <<
"result is expected to be at least of size 1, but got "
341 << resultTypes.size();
343 for (
unsigned i = 0, e = resultTypes.size(); i != e; ++i) {
344 auto type = resultTypes[i];
345 if (!llvm::isa<TokenType>(type) && !llvm::isa<ValueType>(type))
346 return emitOpError() <<
"result type must be async value type or async "
347 "token type, but got "
350 if (llvm::isa<TokenType>(type) && i != 0) {
352 <<
" results' (optional) async token type is expected "
353 "to appear as the 1st return value, but got "
369 return emitOpError(
"requires a 'callee' symbol reference attribute");
372 return emitOpError() <<
"'" << fnAttr.getValue()
373 <<
"' does not reference a valid async function";
376 auto fnType = fn.getFunctionType();
377 if (fnType.getNumInputs() != getNumOperands())
378 return emitOpError(
"incorrect number of operands for callee");
380 for (
unsigned i = 0, e = fnType.getNumInputs(); i != e; ++i)
381 if (getOperand(i).
getType() != fnType.getInput(i))
382 return emitOpError(
"operand type mismatch: expected operand type ")
383 << fnType.getInput(i) <<
", but provided "
384 << getOperand(i).getType() <<
" for operand number " << i;
386 if (fnType.getNumResults() != getNumResults())
387 return emitOpError(
"incorrect number of results for callee");
389 for (
unsigned i = 0, e = fnType.getNumResults(); i != e; ++i)
390 if (getResult(i).
getType() != fnType.getResult(i)) {
391 auto diag = emitOpError(
"result type mismatch at index ") << i;
392 diag.attachNote() <<
" op result types: " << getResultTypes();
393 diag.attachNote() <<
"function result types: " << fnType.getResults();
400 FunctionType CallOp::getCalleeType() {
409 auto funcOp = (*this)->getParentOfType<FuncOp>();
411 ? funcOp.getResultTypes().drop_front()
412 : funcOp.getResultTypes();
415 auto types = llvm::map_range(resultTypes, [](
const Type &result) {
416 return llvm::cast<ValueType>(result).getValueType();
419 if (getOperandTypes() != types)
420 return emitOpError(
"operand types do not match the types returned from "
421 "the parent FuncOp");
430 #define GET_OP_CLASSES
431 #include "mlir/Dialect/Async/IR/AsyncOps.cpp.inc"
437 #define GET_TYPEDEF_CLASSES
438 #include "mlir/Dialect/Async/IR/AsyncOpsTypes.cpp.inc"
446 Type ValueType::parse(mlir::AsmParser &parser) {
448 if (parser.parseLess() || parser.parseType(ty) || parser.parseGreater()) {
449 parser.emitError(parser.getNameLoc(), "failed to parse async value type");
452 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 MLIRContext * getContext(OpFoldResult val)
static std::string diag(const llvm::Value &value)
static void print(spirv::VerCapExtAttr triple, DialectAsmPrinter &printer)
@ OptionalParen
Parens supporting zero or more operands, 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 Builder & getBuilder() const =0
Return a builder which provides useful access to MLIRContext, global objects like types and attribute...
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 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 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)
A symbol reference with a reference path containing a single element.
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=std::nullopt, ArrayRef< Location > locs=std::nullopt)
Add new block with 'argTypes' arguments and set the insertion point to the end of it.
Operation * create(const OperationState &state)
Creates an operation given the fields represented as an OperationState.
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.
This class represents a successor of a region.
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.
Operation * lookupNearestSymbolFrom(Operation *from, StringAttr symbol)
Returns the operation registered with the given symbol name within the closest parent operation of,...
static StringRef getSymbolAttrName()
Return the name of the attribute used for symbol names.
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.
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.
QueryRef parse(llvm::StringRef line, const QuerySession &qs)
Include the generated interface declarations.
Type getType(OpFoldResult ofr)
Returns the int type of the integer in ofr.
auto get(MLIRContext *context, Ts &&...params)
Helper method that injects context only if needed, this helps unify some of the attribute constructio...
LogicalResult verify(Operation *op, bool verifyRecursively=true)
Perform (potentially expensive) checks of invariants, used to detect compiler bugs,...
This represents an operation in an abstracted form, suitable for use with the builder APIs.
SmallVector< Value, 4 > operands
void addOperands(ValueRange newOperands)
void addAttributes(ArrayRef< NamedAttribute > newAttributes)
Add an array of named attributes.
void addAttribute(StringRef name, Attribute attr)
Add an attribute with the specified name.
void addTypes(ArrayRef< Type > newTypes)
MLIRContext * getContext() const
Get the context held by this operation state.
SmallVector< Type, 4 > types
Types of the results of this operation.
Region * addRegion()
Create a region that should be attached to the operation.