1 /* Copyright 2019 The TensorFlow Authors. All Rights Reserved. 2 3 Licensed under the Apache License, Version 2.0 (the "License"); 4 you may not use this file except in compliance with the License. 5 You may obtain a copy of the License at 6 7 http://www.apache.org/licenses/LICENSE-2.0 8 9 Unless required by applicable law or agreed to in writing, software 10 distributed under the License is distributed on an "AS IS" BASIS, 11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 See the License for the specific language governing permissions and 13 limitations under the License. 14 ==============================================================================*/ 15 16 // This file defines the operations used in the MHLO dialect. 17 18 #ifndef MLIR_HLO_DIALECT_MHLO_IR_HLO_OPS_H 19 #define MLIR_HLO_DIALECT_MHLO_IR_HLO_OPS_H 20 21 #include "llvm/ADT/StringRef.h" 22 #include "mlir-hlo/Dialect/mhlo/IR/hlo_ops_base.h" 23 #include "mlir/Dialect/Quant/QuantTypes.h" 24 #include "mlir/Dialect/Shape/IR/Shape.h" 25 #include "mlir/IR/Attributes.h" 26 #include "mlir/IR/BuiltinTypes.h" 27 #include "mlir/IR/Dialect.h" 28 #include "mlir/IR/DialectImplementation.h" 29 #include "mlir/IR/Location.h" 30 #include "mlir/IR/MLIRContext.h" 31 #include "mlir/IR/OpDefinition.h" 32 #include "mlir/IR/Operation.h" 33 #include "mlir/IR/TypeUtilities.h" 34 #include "mlir/IR/Types.h" 35 #include "mlir/Interfaces/InferTypeOpInterface.h" 36 #include "mlir/Interfaces/SideEffectInterfaces.h" 37 38 namespace mlir { 39 class OpBuilder; 40 41 namespace mhlo { 42 43 class MhloDialect : public Dialect { 44 public: 45 explicit MhloDialect(MLIRContext *context); getDialectNamespace()46 static StringRef getDialectNamespace() { return "mhlo"; } 47 48 // Registered hook to materialize a constant operation from a given attribute 49 // value with the desired resultant type. 50 Operation *materializeConstant(OpBuilder &builder, Attribute value, Type type, 51 Location loc) override; 52 53 // Registered hook to verify region arg attributes on operations. 54 LogicalResult verifyRegionArgAttribute(mlir::Operation *op, 55 unsigned regionIndex, 56 unsigned argIndex, 57 mlir::NamedAttribute attr) override; 58 59 // Registered hook to verify an attribute from this dialect on operations. 60 LogicalResult verifyOperationAttribute(mlir::Operation *op, 61 mlir::NamedAttribute attr) override; 62 63 // Parses a type registered to this dialect. 64 Type parseType(DialectAsmParser &parser) const override; 65 66 // Prints a type registered to this dialect. 67 void printType(Type type, DialectAsmPrinter &os) const override; 68 69 // Parses an attribute registered to this dialect. 70 Attribute parseAttribute(DialectAsmParser &parser, Type type) const override; 71 72 // Prints an attribute registered to this dialect. 73 void printAttribute(Attribute attr, DialectAsmPrinter &os) const override; 74 }; 75 76 class TokenType : public Type::TypeBase<TokenType, Type, TypeStorage> { 77 public: 78 using Base::Base; 79 }; 80 81 // Returns true if the given types are the same for the purposes of MHLO type 82 // inference, accounting for special properties of quantization and sparsity. 83 bool isCompatibleForMhloTypeInference(Type tp1, Type tp2); 84 85 // Shape derivation function that computes the shape of the result based on an 86 // operand. For a 2-dimensional input tensor, this produces IR of the form 87 // 88 // %0 = dim %arg0, 0 : memref<?x?xf32> 89 // %1 = index_cast %0 : index to i64 90 // %2 = dim %arg0, 1 : memref<?x?xf32> 91 // %3 = index_cast %2 : index to i64 92 // %4 = "mhlo.scalars_to_dimension_tensor"(%1, %3) 93 // : (i64, i64) -> tensor<2xi64> 94 // 95 // and returns %4 as the shape value. 96 LogicalResult deriveShapeFromOperand( 97 OpBuilder *builder, Operation *op, Value operand, 98 SmallVectorImpl<Value> *reifiedReturnShapes); 99 100 // Type derivation function that returns a tensor type with a new element type. 101 TensorType getSameShapeTensorType(TensorType tensorType, Type elementType); 102 103 void printConvolutionDimensions(AsmPrinter &p, ConvDimensionNumbersAttr dnums); 104 void printConvolutionDimensions(AsmPrinter &p, Operation *, 105 ConvDimensionNumbersAttr dnums); 106 ParseResult parseConvolutionDimensions(AsmParser &parser, 107 ConvDimensionNumbersAttr &dnums); 108 109 } // end namespace mhlo 110 } // end namespace mlir 111 112 #define GET_OP_CLASSES 113 #include "mlir-hlo/Dialect/mhlo/IR/hlo_ops.h.inc" 114 115 namespace mlir { 116 namespace mhlo { 117 118 SortOp createSortOp(PatternRewriter *rewriter, const Location &loc, 119 const llvm::ArrayRef<Value> &operands, 120 const llvm::ArrayRef<Type> &elementTypes, int64_t dimension, 121 bool isStable, ComparisonDirection direction); 122 123 } // end namespace mhlo 124 } // end namespace mlir 125 126 #endif // MLIR_HLO_DIALECT_MHLO_IR_HLO_OPS_H 127