diff options
| author | Alex Zinenko <zinenko@google.com> | 2019-11-28 11:50:47 -0800 |
|---|---|---|
| committer | A. Unique TensorFlower <gardener@tensorflow.org> | 2019-11-28 11:51:23 -0800 |
| commit | 2f16bf7ac9de4d525fda2916e801e23f456d8b82 (patch) | |
| tree | 7362f50f7dddb56b46493405c467e71c220d06c1 /mlir | |
| parent | 0494ef60f71abc09dbeed568ef0c5ee09d292bbb (diff) | |
| download | bcm5719-llvm-2f16bf7ac9de4d525fda2916e801e23f456d8b82.tar.gz bcm5719-llvm-2f16bf7ac9de4d525fda2916e801e23f456d8b82.zip | |
Split out FunctionLike printing/parsing into FunctionImplementation.{h,cpp}
Helper utilies for parsing and printing FunctionLike Ops are only relevant to
the implementation of the Op, not its definition. They depend on
OpImplementation.h and increase the inclusion footprint of FunctionSupport.h,
and do so only to provide some utilities in the "impl" namespace. Move them to
a separate files, similarly to OpDefinition/OpImplementation distinction, and
make only Op implementations use them while keeping headers cleaner. NFC.
PiperOrigin-RevId: 282964556
Diffstat (limited to 'mlir')
| -rw-r--r-- | mlir/include/mlir/IR/FunctionImplementation.h | 109 | ||||
| -rw-r--r-- | mlir/include/mlir/IR/FunctionSupport.h | 73 | ||||
| -rw-r--r-- | mlir/lib/Dialect/GPU/IR/GPUDialect.cpp | 1 | ||||
| -rw-r--r-- | mlir/lib/Dialect/LLVMIR/IR/LLVMDialect.cpp | 1 | ||||
| -rw-r--r-- | mlir/lib/IR/Function.cpp | 1 | ||||
| -rw-r--r-- | mlir/lib/IR/FunctionImplementation.cpp (renamed from mlir/lib/IR/FunctionSupport.cpp) | 6 |
6 files changed, 116 insertions, 75 deletions
diff --git a/mlir/include/mlir/IR/FunctionImplementation.h b/mlir/include/mlir/IR/FunctionImplementation.h new file mode 100644 index 00000000000..241d5615acf --- /dev/null +++ b/mlir/include/mlir/IR/FunctionImplementation.h @@ -0,0 +1,109 @@ +//===- FunctionImplementation.h - Function-like Op utilities ----*- C++ -*-===// +// +// Copyright 2019 The MLIR Authors. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// ============================================================================= +// +// This file provides utility functions for implementing function-like +// operations, in particular, parsing, printing and verification components +// common to function-like operations. +// +//===----------------------------------------------------------------------===// + +#ifndef MLIR_IR_FUNCTIONIMPLEMENTATION_H_ +#define MLIR_IR_FUNCTIONIMPLEMENTATION_H_ + +#include "mlir/IR/FunctionSupport.h" +#include "mlir/IR/OpImplementation.h" + +namespace mlir { + +namespace impl { + +/// A named class for passing around the variadic flag. +class VariadicFlag { +public: + explicit VariadicFlag(bool variadic) : variadic(variadic) {} + bool isVariadic() const { return variadic; } + +private: + /// Underlying storage. + bool variadic; +}; + +/// Adds argument and result attributes, provided as `argAttrs` and +/// `resultAttrs` arguments, to the list of operation attributes in `result`. +/// Internally, argument and result attributes are stored as dict attributes +/// with special names given by getResultAttrName, getArgumentAttrName. +void addArgAndResultAttrs(Builder &builder, OperationState &result, + ArrayRef<SmallVector<NamedAttribute, 2>> argAttrs, + ArrayRef<SmallVector<NamedAttribute, 2>> resultAttrs); + +/// Callback type for `parseFunctionLikeOp`, the callback should produce the +/// type that will be associated with a function-like operation from lists of +/// function arguments and results, VariadicFlag indicates whether the function +/// should have variadic arguments; in case of error, it may populate the last +/// argument with a message. +using FuncTypeBuilder = llvm::function_ref<Type( + Builder &, ArrayRef<Type>, ArrayRef<Type>, VariadicFlag, std::string &)>; + +/// Parses a function signature using `parser`. The `allowVariadic` argument +/// indicates whether functions with variadic arguments are supported. The +/// trailing arguments are populated by this function with names, types and +/// attributes of the arguments and those of the results. +ParseResult parseFunctionSignature( + OpAsmParser &parser, bool allowVariadic, + SmallVectorImpl<OpAsmParser::OperandType> &argNames, + SmallVectorImpl<Type> &argTypes, + SmallVectorImpl<SmallVector<NamedAttribute, 2>> &argAttrs, bool &isVariadic, + SmallVectorImpl<Type> &resultTypes, + SmallVectorImpl<SmallVector<NamedAttribute, 2>> &resultAttrs); + +/// Parser implementation for function-like operations. Uses +/// `funcTypeBuilder` to construct the custom function type given lists of +/// input and output types. If `allowVariadic` is set, the parser will accept +/// trailing ellipsis in the function signature and indicate to the builder +/// whether the function is variadic. If the builder returns a null type, +/// `result` will not contain the `type` attribute. The caller can then add a +/// type, report the error or delegate the reporting to the op's verifier. +ParseResult parseFunctionLikeOp(OpAsmParser &parser, OperationState &result, + bool allowVariadic, + FuncTypeBuilder funcTypeBuilder); + +/// Printer implementation for function-like operations. Accepts lists of +/// argument and result types to use while printing. +void printFunctionLikeOp(OpAsmPrinter &p, Operation *op, + ArrayRef<Type> argTypes, bool isVariadic, + ArrayRef<Type> resultTypes); + +/// Prints the signature of the function-like operation `op`. Assumes `op` has +/// the FunctionLike trait and passed the verification. +void printFunctionSignature(OpAsmPrinter &p, Operation *op, + ArrayRef<Type> argTypes, bool isVariadic, + ArrayRef<Type> resultTypes); + +/// Prints the list of function prefixed with the "attributes" keyword. The +/// attributes with names listed in "elided" as well as those used by the +/// function-like operation internally are not printed. Nothing is printed +/// if all attributes are elided. Assumes `op` has the `FunctionLike` trait and +/// passed the verification. +void printFunctionAttributes(OpAsmPrinter &p, Operation *op, unsigned numInputs, + unsigned numResults, + ArrayRef<StringRef> elided = {}); + +} // namespace impl + +} // namespace mlir + +#endif // MLIR_IR_FUNCTIONIMPLEMENTATION_H_ diff --git a/mlir/include/mlir/IR/FunctionSupport.h b/mlir/include/mlir/IR/FunctionSupport.h index 38e406e8f08..4656c35a9c2 100644 --- a/mlir/include/mlir/IR/FunctionSupport.h +++ b/mlir/include/mlir/IR/FunctionSupport.h @@ -24,12 +24,12 @@ #define MLIR_IR_FUNCTIONSUPPORT_H #include "mlir/IR/OpDefinition.h" -#include "mlir/IR/OpImplementation.h" #include "llvm/ADT/SmallString.h" namespace mlir { namespace impl { + /// Return the name of the attribute used for function types. inline StringRef getTypeAttrName() { return "type"; } @@ -73,77 +73,6 @@ inline ArrayRef<NamedAttribute> getResultAttrs(Operation *op, unsigned index) { return resultDict ? resultDict.getValue() : llvm::None; } -/// A named class for passing around the variadic flag. -class VariadicFlag { -public: - explicit VariadicFlag(bool variadic) : variadic(variadic) {} - bool isVariadic() const { return variadic; } - -private: - /// Underlying storage. - bool variadic; -}; - -/// Adds argument and result attributes, provided as `argAttrs` and -/// `resultAttrs` arguments, to the list of operation attributes in `result`. -/// Internally, argument and result attributes are stored as dict attributes -/// with special names given by getResultAttrName, getArgumentAttrName. -void addArgAndResultAttrs(Builder &builder, OperationState &result, - ArrayRef<SmallVector<NamedAttribute, 2>> argAttrs, - ArrayRef<SmallVector<NamedAttribute, 2>> resultAttrs); - -/// Callback type for `parseFunctionLikeOp`, the callback should produce the -/// type that will be associated with a function-like operation from lists of -/// function arguments and results, VariadicFlag indicates whether the function -/// should have variadic arguments; in case of error, it may populate the last -/// argument with a message. -using FuncTypeBuilder = llvm::function_ref<Type( - Builder &, ArrayRef<Type>, ArrayRef<Type>, VariadicFlag, std::string &)>; - -/// Parses a function signature using `parser`. The `allowVariadic` argument -/// indicates whether functions with variadic arguments are supported. The -/// trailing arguments are populated by this function with names, types and -/// attributes of the arguments and those of the results. -ParseResult parseFunctionSignature( - OpAsmParser &parser, bool allowVariadic, - SmallVectorImpl<OpAsmParser::OperandType> &argNames, - SmallVectorImpl<Type> &argTypes, - SmallVectorImpl<SmallVector<NamedAttribute, 2>> &argAttrs, bool &isVariadic, - SmallVectorImpl<Type> &resultTypes, - SmallVectorImpl<SmallVector<NamedAttribute, 2>> &resultAttrs); - -/// Parser implementation for function-like operations. Uses -/// `funcTypeBuilder` to construct the custom function type given lists of -/// input and output types. If `allowVariadic` is set, the parser will accept -/// trailing ellipsis in the function signature and indicate to the builder -/// whether the function is variadic. If the builder returns a null type, -/// `result` will not contain the `type` attribute. The caller can then add a -/// type, report the error or delegate the reporting to the op's verifier. -ParseResult parseFunctionLikeOp(OpAsmParser &parser, OperationState &result, - bool allowVariadic, - FuncTypeBuilder funcTypeBuilder); - -/// Printer implementation for function-like operations. Accepts lists of -/// argument and result types to use while printing. -void printFunctionLikeOp(OpAsmPrinter &p, Operation *op, - ArrayRef<Type> argTypes, bool isVariadic, - ArrayRef<Type> resultTypes); - -/// Prints the signature of the function-like operation `op`. Assumes `op` has -/// the FunctionLike trait and passed the verification. -void printFunctionSignature(OpAsmPrinter &p, Operation *op, - ArrayRef<Type> argTypes, bool isVariadic, - ArrayRef<Type> resultTypes); - -/// Prints the list of function prefixed with the "attributes" keyword. The -/// attributes with names listed in "elided" as well as those used by the -/// function-like operation internally are not printed. Nothing is printed -/// if all attributes are elided. Assumes `op` has the `FunctionLike` trait and -/// passed the verification. -void printFunctionAttributes(OpAsmPrinter &p, Operation *op, unsigned numInputs, - unsigned numResults, - ArrayRef<StringRef> elided = {}); - } // namespace impl namespace OpTrait { diff --git a/mlir/lib/Dialect/GPU/IR/GPUDialect.cpp b/mlir/lib/Dialect/GPU/IR/GPUDialect.cpp index 5fc1cade760..8d84fadae8a 100644 --- a/mlir/lib/Dialect/GPU/IR/GPUDialect.cpp +++ b/mlir/lib/Dialect/GPU/IR/GPUDialect.cpp @@ -24,6 +24,7 @@ #include "mlir/Dialect/StandardOps/Ops.h" #include "mlir/IR/Builders.h" #include "mlir/IR/Function.h" +#include "mlir/IR/FunctionImplementation.h" #include "mlir/IR/Module.h" #include "mlir/IR/OpImplementation.h" #include "mlir/IR/PatternMatch.h" diff --git a/mlir/lib/Dialect/LLVMIR/IR/LLVMDialect.cpp b/mlir/lib/Dialect/LLVMIR/IR/LLVMDialect.cpp index ca71db5fd8d..66a9bc0ae9f 100644 --- a/mlir/lib/Dialect/LLVMIR/IR/LLVMDialect.cpp +++ b/mlir/lib/Dialect/LLVMIR/IR/LLVMDialect.cpp @@ -22,6 +22,7 @@ #include "mlir/Dialect/LLVMIR/LLVMDialect.h" #include "mlir/IR/Builders.h" #include "mlir/IR/DialectImplementation.h" +#include "mlir/IR/FunctionImplementation.h" #include "mlir/IR/MLIRContext.h" #include "mlir/IR/Module.h" #include "mlir/IR/StandardTypes.h" diff --git a/mlir/lib/IR/Function.cpp b/mlir/lib/IR/Function.cpp index 4e103508af0..e5e854260f3 100644 --- a/mlir/lib/IR/Function.cpp +++ b/mlir/lib/IR/Function.cpp @@ -20,6 +20,7 @@ #include "mlir/IR/Builders.h" #include "mlir/IR/Diagnostics.h" #include "mlir/IR/Dialect.h" +#include "mlir/IR/FunctionImplementation.h" #include "mlir/IR/MLIRContext.h" #include "mlir/IR/Module.h" #include "mlir/IR/OpImplementation.h" diff --git a/mlir/lib/IR/FunctionSupport.cpp b/mlir/lib/IR/FunctionImplementation.cpp index c6f2673ef2a..a1fc21e11ea 100644 --- a/mlir/lib/IR/FunctionSupport.cpp +++ b/mlir/lib/IR/FunctionImplementation.cpp @@ -1,4 +1,4 @@ -//===- FunctionSupport.cpp - Utility types for function-like ops ----------===// +//===- FunctionImplementation.cpp - Utilities for function-like ops -------===// // // Copyright 2019 The MLIR Authors. // @@ -15,9 +15,9 @@ // limitations under the License. // ============================================================================= -#include "mlir/IR/FunctionSupport.h" +#include "mlir/IR/FunctionImplementation.h" #include "mlir/IR/Builders.h" -#include "mlir/IR/OpImplementation.h" +#include "mlir/IR/FunctionSupport.h" #include "mlir/IR/SymbolTable.h" using namespace mlir; |

