From 2f16bf7ac9de4d525fda2916e801e23f456d8b82 Mon Sep 17 00:00:00 2001 From: Alex Zinenko Date: Thu, 28 Nov 2019 11:50:47 -0800 Subject: [PATCH] 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 --- mlir/include/mlir/IR/FunctionImplementation.h | 109 +++++++++++++++++++++ mlir/include/mlir/IR/FunctionSupport.h | 73 +------------- mlir/lib/Dialect/GPU/IR/GPUDialect.cpp | 1 + mlir/lib/Dialect/LLVMIR/IR/LLVMDialect.cpp | 1 + mlir/lib/IR/Function.cpp | 1 + ...ctionSupport.cpp => FunctionImplementation.cpp} | 6 +- 6 files changed, 116 insertions(+), 75 deletions(-) create mode 100644 mlir/include/mlir/IR/FunctionImplementation.h rename mlir/lib/IR/{FunctionSupport.cpp => FunctionImplementation.cpp} (99%) diff --git a/mlir/include/mlir/IR/FunctionImplementation.h b/mlir/include/mlir/IR/FunctionImplementation.h new file mode 100644 index 0000000..241d561 --- /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> argAttrs, + ArrayRef> 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, ArrayRef, 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 &argNames, + SmallVectorImpl &argTypes, + SmallVectorImpl> &argAttrs, bool &isVariadic, + SmallVectorImpl &resultTypes, + SmallVectorImpl> &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 argTypes, bool isVariadic, + ArrayRef 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 argTypes, bool isVariadic, + ArrayRef 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 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 38e406e..4656c35 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 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> argAttrs, - ArrayRef> 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, ArrayRef, 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 &argNames, - SmallVectorImpl &argTypes, - SmallVectorImpl> &argAttrs, bool &isVariadic, - SmallVectorImpl &resultTypes, - SmallVectorImpl> &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 argTypes, bool isVariadic, - ArrayRef 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 argTypes, bool isVariadic, - ArrayRef 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 elided = {}); - } // namespace impl namespace OpTrait { diff --git a/mlir/lib/Dialect/GPU/IR/GPUDialect.cpp b/mlir/lib/Dialect/GPU/IR/GPUDialect.cpp index 5fc1cad..8d84fad 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 ca71db5..66a9bc0 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 4e10350..e5e85426 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 similarity index 99% rename from mlir/lib/IR/FunctionSupport.cpp rename to mlir/lib/IR/FunctionImplementation.cpp index c6f2673..a1fc21e 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; -- 2.7.4