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 18 #include "mlir/IR/FunctionInterfaces.h" 19 #include "mlir/IR/OpImplementation.h" 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 void addArgAndResultAttrs(Builder &builder, OperationState &result, 44 ArrayRef<NamedAttrList> argAttrs, 45 ArrayRef<NamedAttrList> resultAttrs); 46 47 /// Callback type for `parseFunctionOp`, the callback should produce the 48 /// type that will be associated with a function-like operation from lists of 49 /// function arguments and results, VariadicFlag indicates whether the function 50 /// should have variadic arguments; in case of error, it may populate the last 51 /// argument with a message. 52 using FuncTypeBuilder = function_ref<Type( 53 Builder &, ArrayRef<Type>, ArrayRef<Type>, VariadicFlag, std::string &)>; 54 55 /// Parses function arguments using `parser`. The `allowVariadic` argument 56 /// indicates whether functions with variadic arguments are supported. The 57 /// trailing arguments are populated by this function with names, types, 58 /// attributes and locations of the arguments. 59 ParseResult parseFunctionArgumentList( 60 OpAsmParser &parser, bool allowAttributes, bool allowVariadic, 61 SmallVectorImpl<OpAsmParser::UnresolvedOperand> &argNames, 62 SmallVectorImpl<Type> &argTypes, SmallVectorImpl<NamedAttrList> &argAttrs, 63 SmallVectorImpl<Location> &argLocations, bool &isVariadic); 64 65 /// Parses a function signature using `parser`. The `allowVariadic` argument 66 /// indicates whether functions with variadic arguments are supported. The 67 /// trailing arguments are populated by this function with names, types, 68 /// attributes and locations of the arguments and those of the results. 69 ParseResult parseFunctionSignature( 70 OpAsmParser &parser, bool allowVariadic, 71 SmallVectorImpl<OpAsmParser::UnresolvedOperand> &argNames, 72 SmallVectorImpl<Type> &argTypes, SmallVectorImpl<NamedAttrList> &argAttrs, 73 SmallVectorImpl<Location> &argLocations, bool &isVariadic, 74 SmallVectorImpl<Type> &resultTypes, 75 SmallVectorImpl<NamedAttrList> &resultAttrs); 76 77 /// Parser implementation for function-like operations. Uses 78 /// `funcTypeBuilder` to construct the custom function type given lists of 79 /// input and output types. If `allowVariadic` is set, the parser will accept 80 /// trailing ellipsis in the function signature and indicate to the builder 81 /// whether the function is variadic. If the builder returns a null type, 82 /// `result` will not contain the `type` attribute. The caller can then add a 83 /// type, report the error or delegate the reporting to the op's verifier. 84 ParseResult parseFunctionOp(OpAsmParser &parser, OperationState &result, 85 bool allowVariadic, 86 FuncTypeBuilder funcTypeBuilder); 87 88 /// Printer implementation for function-like operations. 89 void printFunctionOp(OpAsmPrinter &p, FunctionOpInterface op, bool isVariadic); 90 91 /// Prints the signature of the function-like operation `op`. Assumes `op` has 92 /// is a FunctionOpInterface and has passed verification. 93 void printFunctionSignature(OpAsmPrinter &p, Operation *op, 94 ArrayRef<Type> argTypes, bool isVariadic, 95 ArrayRef<Type> resultTypes); 96 97 /// Prints the list of function prefixed with the "attributes" keyword. The 98 /// attributes with names listed in "elided" as well as those used by the 99 /// function-like operation internally are not printed. Nothing is printed 100 /// if all attributes are elided. Assumes `op` is a FunctionOpInterface and 101 /// has passed verification. 102 void printFunctionAttributes(OpAsmPrinter &p, Operation *op, unsigned numInputs, 103 unsigned numResults, 104 ArrayRef<StringRef> elided = {}); 105 106 } // namespace function_interface_impl 107 108 } // namespace mlir 109 110 #endif // MLIR_IR_FUNCTIONIMPLEMENTATION_H_ 111