1 //===- FunctionImplementation.h - Function-like Op utilities ----*- C++ -*-===//
2 //
3 // Part of the MLIR 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 
18 #include "mlir/IR/FunctionSupport.h"
19 #include "mlir/IR/OpImplementation.h"
20 
21 namespace mlir {
22 
23 namespace impl {
24 
25 /// A named class for passing around the variadic flag.
26 class VariadicFlag {
27 public:
VariadicFlag(bool variadic)28   explicit VariadicFlag(bool variadic) : variadic(variadic) {}
isVariadic()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<SmallVector<NamedAttribute, 2>> argAttrs,
42                           ArrayRef<SmallVector<NamedAttribute, 2>> resultAttrs);
43 
44 /// Callback type for `parseFunctionLikeOp`, the callback should produce the
45 /// type that will be associated with a function-like operation from lists of
46 /// function arguments and results, VariadicFlag indicates whether the function
47 /// should have variadic arguments; in case of error, it may populate the last
48 /// argument with a message.
49 using FuncTypeBuilder = function_ref<Type(
50     Builder &, ArrayRef<Type>, ArrayRef<Type>, VariadicFlag, std::string &)>;
51 
52 /// Parses a function signature using `parser`. The `allowVariadic` argument
53 /// indicates whether functions with variadic arguments are supported. The
54 /// trailing arguments are populated by this function with names, types and
55 /// attributes of the arguments and those of the results.
56 ParseResult parseFunctionSignature(
57     OpAsmParser &parser, bool allowVariadic,
58     SmallVectorImpl<OpAsmParser::OperandType> &argNames,
59     SmallVectorImpl<Type> &argTypes,
60     SmallVectorImpl<SmallVector<NamedAttribute, 2>> &argAttrs, bool &isVariadic,
61     SmallVectorImpl<Type> &resultTypes,
62     SmallVectorImpl<SmallVector<NamedAttribute, 2>> &resultAttrs);
63 
64 /// Parser implementation for function-like operations.  Uses
65 /// `funcTypeBuilder` to construct the custom function type given lists of
66 /// input and output types.  If `allowVariadic` is set, the parser will accept
67 /// trailing ellipsis in the function signature and indicate to the builder
68 /// whether the function is variadic.  If the builder returns a null type,
69 /// `result` will not contain the `type` attribute.  The caller can then add a
70 /// type, report the error or delegate the reporting to the op's verifier.
71 ParseResult parseFunctionLikeOp(OpAsmParser &parser, OperationState &result,
72                                 bool allowVariadic,
73                                 FuncTypeBuilder funcTypeBuilder);
74 
75 /// Printer implementation for function-like operations.  Accepts lists of
76 /// argument and result types to use while printing.
77 void printFunctionLikeOp(OpAsmPrinter &p, Operation *op,
78                          ArrayRef<Type> argTypes, bool isVariadic,
79                          ArrayRef<Type> resultTypes);
80 
81 /// Prints the signature of the function-like operation `op`.  Assumes `op` has
82 /// the FunctionLike trait and passed the verification.
83 void printFunctionSignature(OpAsmPrinter &p, Operation *op,
84                             ArrayRef<Type> argTypes, bool isVariadic,
85                             ArrayRef<Type> resultTypes);
86 
87 /// Prints the list of function prefixed with the "attributes" keyword. The
88 /// attributes with names listed in "elided" as well as those used by the
89 /// function-like operation internally are not printed. Nothing is printed
90 /// if all attributes are elided. Assumes `op` has the `FunctionLike` trait and
91 /// passed the verification.
92 void printFunctionAttributes(OpAsmPrinter &p, Operation *op, unsigned numInputs,
93                              unsigned numResults,
94                              ArrayRef<StringRef> elided = {});
95 
96 } // namespace impl
97 
98 } // namespace mlir
99 
100 #endif // MLIR_IR_FUNCTIONIMPLEMENTATION_H_
101