ops.td 7.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/**
 * \file src/jit/impl/mlir/ir/ops.td
 * MegEngine is Licensed under the Apache License, Version 2.0 (the "License")
 *
 * Copyright (c) 2014-2020 Megvii Inc. All rights reserved.
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT ARRANTIES OR CONDITIONS OF ANY KIND, either express or
 * implied.
 */

#ifndef MGB_MLIR_OPS
#define MGB_MLIR_OPS

include "mlir/IR/OpBase.td"
include "mlir/Interfaces/SideEffectInterfaces.td"

19
include "./interfaces.td"
20
include "./predicates.td"
21 22 23 24 25 26

def Mgb_Dialect : Dialect {
  let name = "mgb";
  let cppNamespace = "mgb::jit";
}

27 28 29 30 31 32 33 34 35 36 37 38 39 40
class ElemwiseBuilderImpl {
  code ElemwiseBuilderImpl_create = [{
    static Operation* create(OpBuilder* builder, Location loc, ValueRange operands) {
      OperationState state(loc, getOperationName());
      state.addOperands(operands);
      state.addTypes(getResultType(operands));
      return builder->createOperation(state);
    }
  }];
}

class ElemwiseOp<string mnemonic, list<OpTrait> traits = [NoSideEffect]> :
  Op<Mgb_Dialect, mnemonic, !listconcat(traits, [ElemwiseOpInterface,
  GenericBuilderInterface])>, ElemwiseBuilderImpl;
41 42 43 44

class GenericOp<string mnemonic, list<OpTrait> traits = []> :
  Op<Mgb_Dialect, mnemonic, traits>;

45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
class ElemwiseUnaryOp<string mnemonic, list<OpTrait> traits = [NoSideEffect]> :
    ElemwiseOp<mnemonic, traits> {
  let arguments = (ins F32MemRef:$lhs);
  let results = (outs F32MemRef);

  let builders = [OpBuilder<
    "Builder* builder, OperationState& result, ValueRange operands", [{
      result.addOperands(operands);
      result.addTypes(getResultType(operands));
    }]>, OpBuilder <
    "OpBuilder& builder, OperationState& result, Value lhs", [{
      result.addOperands(lhs);
      result.addTypes(getResultType({lhs}));
    }]
  >];

  let extraClassDeclaration = [{
    static Type getResultType(ValueRange operands) {
      return deduce_result_type(operands);
    }
  }] # ElemwiseBuilderImpl_create;
}

def ReluOp : ElemwiseUnaryOp<"relu", [NoSideEffect]>;
def AbsOp : ElemwiseUnaryOp<"abs", [NoSideEffect]>;
def NegOp : ElemwiseUnaryOp<"negate", [NoSideEffect]>;
71 72
def AcosOp : ElemwiseUnaryOp<"acos", [NoSideEffect]>;
def AsinOp : ElemwiseUnaryOp<"asin", [NoSideEffect]>;
73 74 75 76 77 78 79 80 81 82 83 84 85
def CeilOp : ElemwiseUnaryOp<"ceil", [NoSideEffect]>;
def CosOp : ElemwiseUnaryOp<"cos", [NoSideEffect]>;
def ExpOp : ElemwiseUnaryOp<"exp", [NoSideEffect]>;
def ExpM1Op : ElemwiseUnaryOp<"expm1", [NoSideEffect]>;
def FloorOp : ElemwiseUnaryOp<"floor", [NoSideEffect]>;
def LogOp : ElemwiseUnaryOp<"log", [NoSideEffect]>;
def Log1POp : ElemwiseUnaryOp<"log1p", [NoSideEffect]>;
def SigmoidOp: ElemwiseUnaryOp<"sigmoid", [NoSideEffect]>;
def SinOp : ElemwiseUnaryOp<"sin", [NoSideEffect]>;
def TanhOp : ElemwiseUnaryOp<"tanh", [NoSideEffect]>;
def FastTanhOp : ElemwiseUnaryOp<"fast_tanh", [NoSideEffect]>;
def HswishOp : ElemwiseUnaryOp<"hswish", [NoSideEffect]>;
def RoundOp : ElemwiseUnaryOp<"round", [NoSideEffect]>;
86 87 88 89
def ErfOp : ElemwiseUnaryOp<"erf", [NoSideEffect]>;
def ErfInvOp : ElemwiseUnaryOp<"erfinv", [NoSideEffect]>;
def ErfCOp : ElemwiseUnaryOp<"erfc", [NoSideEffect]>;
def ErfCInvOp : ElemwiseUnaryOp<"erfcinv", [NoSideEffect]>;
90 91 92

class ElemwiseBinaryOp<string mnemonic, list<OpTrait> traits = [NoSideEffect]> :
  ElemwiseOp<mnemonic, traits> {
93

94
  let arguments = (ins ElemwiseFloatAny:$lhs, ElemwiseFloatAny:$rhs);
95 96
  let results = (outs F32MemRef);

97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
  let builders = [OpBuilder<
    "Builder* builder, OperationState& result, ValueRange operands", [{
      result.addOperands(operands);
      result.addTypes(getResultType(operands));
    }]
  >, OpBuilder <
    "OpBuilder& builder, OperationState& result, Value lhs, Value rhs", [{
      result.addOperands(lhs);
      result.addOperands(rhs);
      result.addTypes(getResultType({lhs, rhs}));
    }]
  >];

  let extraClassDeclaration = [{
    static Type getResultType(ValueRange operands) {
      return deduce_result_type(operands);
    }
  }] # ElemwiseBuilderImpl_create;
}

def AbsGradOp : ElemwiseBinaryOp<"abs_grad", [NoSideEffect]>;
def AddOp : ElemwiseBinaryOp<"add", [Commutative, NoSideEffect]>;
def FloorDivOp : ElemwiseBinaryOp<"floor_div", [NoSideEffect]>;
def MaxOp : ElemwiseBinaryOp<"max", [Commutative, NoSideEffect]>;
def MinOp : ElemwiseBinaryOp<"min", [Commutative, NoSideEffect]>;
def ModOp : ElemwiseBinaryOp<"mod", [NoSideEffect]>;
def MulOp : ElemwiseBinaryOp<"mul", [Commutative, NoSideEffect]>;
def SubOp : ElemwiseBinaryOp<"sub", [NoSideEffect]>;
def SigmoidGradOp :  ElemwiseBinaryOp<"sigmoid_grad", [NoSideEffect]>;
def SwishGt0Op :  ElemwiseBinaryOp<"switch_gt0", [NoSideEffect]>;
def TanhGradOp :  ElemwiseBinaryOp<"tanh_grad", [NoSideEffect]>;
def LtOp :  ElemwiseBinaryOp<"lt", [NoSideEffect]>;
def LeqOp :  ElemwiseBinaryOp<"leq", [NoSideEffect]>;
def EqOp :  ElemwiseBinaryOp<"eq", [Commutative, NoSideEffect]>;
def FuseAddReluOp :  ElemwiseBinaryOp<"fuse_add_relu", [NoSideEffect]>;
def TrueDivOp : ElemwiseBinaryOp<"true_div", [NoSideEffect]>;
133
def PowOp : ElemwiseBinaryOp<"pow", [NoSideEffect]>;
134 135 136 137 138 139
def LogSumExpOp : ElemwiseBinaryOp<"log_sum_exp", [Commutative, NoSideEffect]>;
def FuseAddTanhOp :  ElemwiseBinaryOp<"fuse_add_tanh", [NoSideEffect]>;
def FastTanhGradOp :  ElemwiseBinaryOp<"fast_tanh_grad", [NoSideEffect]>;
def FuseAddSigmoidOp :  ElemwiseBinaryOp<"fuse_add_sigmoid", [NoSideEffect]>;
def HswishGradOp :  ElemwiseBinaryOp<"hswish_grad", [NoSideEffect]>;
def FuseAddHswishOp : ElemwiseBinaryOp<"fuse_add_hswish", [NoSideEffect]>;
140
def Atan2Op : ElemwiseBinaryOp<"atan2", [NoSideEffect]>;
141 142 143 144

class ElemwiseTernaryOp<string mnemonic, list<OpTrait> traits = [NoSideEffect]> :
  ElemwiseOp<mnemonic, traits> {

145
  let arguments = (ins ElemwiseFloatAny:$x, ElemwiseFloatAny:$y, ElemwiseFloatAny:$z);
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
  let results = (outs F32MemRef);

  let builders = [OpBuilder<
    "Builder* builder, OperationState& result, ValueRange operands", [{
      result.addOperands(operands);
      result.addTypes(getResultType(operands));
    }]
  >, OpBuilder <
    "OpBuilder& builder, OperationState& result, Value x, Value y, Value z", [{
      result.addOperands(x);
      result.addOperands(y);
      result.addOperands(z);
      result.addTypes(getResultType({x, y, z}));
    }]
  >];
161

162 163 164 165 166
  let extraClassDeclaration = [{
    static Type getResultType(ValueRange operands) {
      return deduce_result_type(operands);
    }
  }] # ElemwiseBuilderImpl_create;
167 168
}

169 170 171
def CondLeqMovOp: ElemwiseTernaryOp<"cond_leq_mov", [NoSideEffect]>;
def FuseMulAdd3Op: ElemwiseTernaryOp<"fuse_mul_add3", [NoSideEffect]>;

172 173 174 175 176 177 178 179
def ReturnOp : GenericOp<"return",
    [NoSideEffect, HasParent<"FuncOp">, Terminator]> {
  let summary = "return operation";
  let description = [{
    The "return" operation represents a return operation within a function.
    The operation takes an no tensor operand and produces no results.
  }];

180 181 182 183 184 185
  // The return operation takes an optional input operand to return. This
  // value must match the return type of the enclosing function.
  let arguments = (ins);

  // The return operation only emits the input in the format if it is present.
  let assemblyFormat = "attr-dict";
186 187
}

188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206
def ConstantScalarOp: GenericOp<"sconst", [NoSideEffect]> {
  let summary = "scalar constant";
  let arguments = (ins AnyAttr:$value);
  let results = (outs F32:$result);

  let builders = [OpBuilder<
    "Builder* builder, OperationState& result, float value", [{
      result.addAttribute("value", builder->getF32FloatAttr(value));
      result.addTypes(builder->getF32Type());
    }]
  >];

  let extraClassDeclaration = [{
    Attribute getValue() { return getAttr("value"); }
    FloatAttr getFloatAttr() { return getAttrOfType<FloatAttr>("value"); }
  }];

}

207 208 209 210 211 212 213 214 215 216
def AssignOp :  GenericOp<"assign", []> {
  let summary = "assign op";
  let description = [{
    assign rhs to lhs without results
  }];

  let arguments = (ins F32MemRef:$lhs, F32MemRef:$rhs);
}

#endif