MLIR  19.0.0git
FunctionImplementation.h
Go to the documentation of this file.
1 //===- FunctionImplementation.h - Function-like Op utilities ----*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file provides utility functions for implementing function-like
10 // operations, in particular, parsing, printing and verification components
11 // common to function-like operations.
12 //
13 //===----------------------------------------------------------------------===//
14 
15 #ifndef MLIR_IR_FUNCTIONIMPLEMENTATION_H_
16 #define MLIR_IR_FUNCTIONIMPLEMENTATION_H_
17 
20 
21 namespace mlir {
22 
23 namespace function_interface_impl {
24 
25 /// A named class for passing around the variadic flag.
26 class VariadicFlag {
27 public:
28  explicit VariadicFlag(bool variadic) : variadic(variadic) {}
29  bool isVariadic() const { return variadic; }
30 
31 private:
32  /// Underlying storage.
33  bool variadic;
34 };
35 
36 /// Adds argument and result attributes, provided as `argAttrs` and
37 /// `resultAttrs` arguments, to the list of operation attributes in `result`.
38 /// Internally, argument and result attributes are stored as dict attributes
39 /// with special names given by getResultAttrName, getArgumentAttrName.
40 void addArgAndResultAttrs(Builder &builder, OperationState &result,
41  ArrayRef<DictionaryAttr> argAttrs,
42  ArrayRef<DictionaryAttr> resultAttrs,
43  StringAttr argAttrsName, StringAttr resAttrsName);
44 void addArgAndResultAttrs(Builder &builder, OperationState &result,
46  ArrayRef<DictionaryAttr> resultAttrs,
47  StringAttr argAttrsName, StringAttr resAttrsName);
48 
49 /// Callback type for `parseFunctionOp`, the callback should produce the
50 /// type that will be associated with a function-like operation from lists of
51 /// function arguments and results, VariadicFlag indicates whether the function
52 /// should have variadic arguments; in case of error, it may populate the last
53 /// argument with a message.
55  Builder &, ArrayRef<Type>, ArrayRef<Type>, VariadicFlag, std::string &)>;
56 
57 /// Parses a function signature using `parser`. The `allowVariadic` argument
58 /// indicates whether functions with variadic arguments are supported. The
59 /// trailing arguments are populated by this function with names, types,
60 /// attributes and locations of the arguments and those of the results.
62 parseFunctionSignature(OpAsmParser &parser, bool allowVariadic,
64  bool &isVariadic, SmallVectorImpl<Type> &resultTypes,
65  SmallVectorImpl<DictionaryAttr> &resultAttrs);
66 
67 /// Parser implementation for function-like operations. Uses
68 /// `funcTypeBuilder` to construct the custom function type given lists of
69 /// input and output types. The parser sets the `typeAttrName` attribute to the
70 /// resulting function type. If `allowVariadic` is set, the parser will accept
71 /// trailing ellipsis in the function signature and indicate to the builder
72 /// whether the function is variadic. If the builder returns a null type,
73 /// `result` will not contain the `type` attribute. The caller can then add a
74 /// type, report the error or delegate the reporting to the op's verifier.
76  bool allowVariadic, StringAttr typeAttrName,
77  FuncTypeBuilder funcTypeBuilder,
78  StringAttr argAttrsName, StringAttr resAttrsName);
79 
80 /// Printer implementation for function-like operations.
81 void printFunctionOp(OpAsmPrinter &p, FunctionOpInterface op, bool isVariadic,
82  StringRef typeAttrName, StringAttr argAttrsName,
83  StringAttr resAttrsName);
84 
85 /// Prints the signature of the function-like operation `op`. Assumes `op` has
86 /// is a FunctionOpInterface and has passed verification.
87 void printFunctionSignature(OpAsmPrinter &p, FunctionOpInterface op,
88  ArrayRef<Type> argTypes, bool isVariadic,
89  ArrayRef<Type> resultTypes);
90 
91 /// Prints the list of function prefixed with the "attributes" keyword. The
92 /// attributes with names listed in "elided" as well as those used by the
93 /// function-like operation internally are not printed. Nothing is printed
94 /// if all attributes are elided. Assumes `op` is a FunctionOpInterface and
95 /// has passed verification.
97  ArrayRef<StringRef> elided = {});
98 
99 } // namespace function_interface_impl
100 
101 } // namespace mlir
102 
103 #endif // MLIR_IR_FUNCTIONIMPLEMENTATION_H_
This class is a general helper class for creating context-global objects like types,...
Definition: Builders.h:50
The OpAsmParser has methods for interacting with the asm parser: parsing things from it,...
This is a pure-virtual base class that exposes the asmprinter hooks necessary to implement a custom p...
Operation is the basic unit of execution within MLIR.
Definition: Operation.h:88
This class represents success/failure for parsing-like operations that find it important to chain tog...
Instances of the Type class are uniqued, have an immutable identifier and an optional mutable compone...
Definition: Types.h:74
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.
ParseResult parseFunctionSignature(OpAsmParser &parser, bool allowVariadic, SmallVectorImpl< OpAsmParser::Argument > &arguments, bool &isVariadic, SmallVectorImpl< Type > &resultTypes, SmallVectorImpl< DictionaryAttr > &resultAttrs)
Parses a function signature using parser.
void printFunctionAttributes(OpAsmPrinter &p, Operation *op, ArrayRef< StringRef > elided={})
Prints the list of function prefixed with the "attributes" keyword.
void printFunctionSignature(OpAsmPrinter &p, FunctionOpInterface op, ArrayRef< Type > argTypes, bool isVariadic, ArrayRef< Type > resultTypes)
Prints the signature of the function-like operation op.
Include the generated interface declarations.
This represents an operation in an abstracted form, suitable for use with the builder APIs.