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 TENSORFLOW_COMPILER_MLIR_HLO_INCLUDE_MLIR_HLO_DIALECT_MHLO_IR_HLO_OPS_H_
19 #define TENSORFLOW_COMPILER_MLIR_HLO_INCLUDE_MLIR_HLO_DIALECT_MHLO_IR_HLO_OPS_H_
20 
21 #include "llvm/ADT/StringRef.h"
22 #include "mlir/Dialect/StandardOps/IR/Ops.h"
23 #include "mlir/IR/Attributes.h"
24 #include "mlir/IR/BuiltinTypes.h"
25 #include "mlir/IR/Dialect.h"
26 #include "mlir/IR/DialectImplementation.h"
27 #include "mlir/IR/Location.h"
28 #include "mlir/IR/MLIRContext.h"
29 #include "mlir/IR/OpDefinition.h"
30 #include "mlir/IR/Operation.h"
31 #include "mlir/IR/Types.h"
32 #include "mlir/Interfaces/InferTypeOpInterface.h"
33 #include "mlir/Interfaces/SideEffectInterfaces.h"
34 
35 // clang-format off
36 #include "mlir-hlo/Dialect/mhlo/IR/hlo_ops_base_structs.h"
37 #include "mlir-hlo/Dialect/mhlo/IR/hlo_ops_base_enums.h"
38 #include "mlir-hlo/Dialect/mhlo/IR/infer_fusibility_op_interface.h"
39 // clang-format on
40 
41 namespace mlir {
42 class OpBuilder;
43 
44 namespace mhlo {
45 
46 class MhloDialect : public Dialect {
47  public:
48   explicit MhloDialect(MLIRContext *context);
getDialectNamespace()49   static StringRef getDialectNamespace() { return "mhlo"; }
50 
51   // Registered hook to materialize a constant operation from a given attribute
52   // value with the desired resultant type.
53   Operation *materializeConstant(OpBuilder &builder, Attribute value, Type type,
54                                  Location loc) override;
55 
56   // Parses a type registered to this dialect.
57   Type parseType(DialectAsmParser &parser) const override;
58 
59   // Prints a type registered to this dialect.
60   void printType(Type type, DialectAsmPrinter &os) const override;
61 };
62 
63 class TokenType : public Type::TypeBase<TokenType, Type, TypeStorage> {
64  public:
65   using Base::Base;
66 };
67 
68 // Shape derivation function that computes the shape of the result based on
69 // the first argument. For a 2-dimensional input tensor, this produces IR of
70 // the form
71 //
72 //  %0 = dim %arg0, 0 : memref<?x?xf32>
73 //  %1 = index_cast %0 : index to i64
74 //  %2 = dim %arg0, 1 : memref<?x?xf32>
75 //  %3 = index_cast %2 : index to i64
76 //  %4 = "mhlo.scalars_to_dimension_tensor"(%1, %3)
77 //    : (i64, i64) -> tensor<2xi64>
78 //
79 // and returns %4 as the shape value.
80 LogicalResult deriveShapeFromFirstOperand(
81     OpBuilder *builder, Operation *op,
82     SmallVectorImpl<Value> *reifiedReturnShapes);
83 
84 // Type derivation function that returns a tensor type with a new element type.
85 TensorType getSameShapeTensorType(TensorType tensor_type, Type element_type);
86 
87 }  // end namespace mhlo
88 }  // end namespace mlir
89 
90 #define GET_OP_CLASSES
91 #include "mlir-hlo/Dialect/mhlo/IR/hlo_ops.h.inc"
92 
93 #endif  //  TENSORFLOW_COMPILER_MLIR_HLO_INCLUDE_MLIR_HLO_DIALECT_MHLO_IR_HLO_OPS_H_
94