basic_kernels.td 4.1 KB
Newer Older
Y
Yan Chunwei 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
// Operation definitions for basic kernels.

#ifdef BASIC_OPS
#else
#define BASIC_OPS

include "paddle/infrt/dialect/infrt_base.td"
include "mlir/Interfaces/SideEffectInterfaces.td"

class INFRT_Op<string mnemonic, list<OpTrait> traits = []> : Op<INFRT_Dialect, mnemonic, !listconcat(traits, [IsolatedFromAbove])> {

  // Each registered op needs to provide all of a printer, parser and verifier.
  let printer = [{ return infrt::dialect::print(p, *this); }];
  let verifier = [{ return infrt::dialect::verify(*this); }];
  let parser = [{ return infrt::dialect::parse$cppClass(parser, result); }];
}

def CallOp : INFRT_Op<"call"> {
  let summary = "call a host operation";
  let description = [{
      The "infrt.call" operation represents a direct call to a function. The operands and result types of the call must match the specified function type.

          %2 = infrt.call @add(%0, %1) : (f32, f32) -> f32
    }];

  let arguments = (ins FlatSymbolRefAttr:$callee, Variadic<AnyType>:$operands);
  let results = (outs Variadic<AnyType>);

  let extraClassDeclaration = [{
30
      mlir::StringRef getCallee() { return callee(); }
Y
Yan Chunwei 已提交
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
      mlir::FunctionType getCalleeType();
    }];
}

class ConstantOp<string suffix, Type baseType, Attr attr>
    : INFRT_Op<"constant." # suffix, [NoSideEffect]> {
  let summary = "constant value constructor in host";

  let arguments = (ins attr:$value);
  let results = (outs baseType);
}

def ConstantI32Op : ConstantOp<"i32", I32, I32Attr>;
def ConstantI64Op : ConstantOp<"i64", I64, I64Attr>;
def ConstantF32Op : ConstantOp<"f32", F32, F32Attr>;
def ConstantF64Op : ConstantOp<"f64", F64, F64Attr>;

def ReturnOp : INFRT_Op<"return", [Terminator]> {
  let summary = "host executor return operation";
  let description = [{
      The "infrt.return" operation represents a return operation within a function.

        func @foo() : (i32, f8) {
        infrt.return %0, %1 : i32, f8
        }
    }];

  let arguments = (ins Variadic<AnyType>:$operands);

60 61
  let builders = [OpBuilder<(ins),
                  [{ build($_builder, $_state, llvm::None); }]>];
Y
Yan Chunwei 已提交
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
}

class AddOp<string suffix, Type type> : INFRT_Op<"add." # suffix, [NoSideEffect]> {
  let summary = "infrt.add operation";
  let description = [{
      An operation that takes two inputs and returns their sum as result.
    }];

  let arguments = (ins type, type);
  let results = (outs type);
  let assemblyFormat = "operands attr-dict";
  let verifier = ?;
}

def AddI32Op : AddOp<"i32", I32>;
def AddI64Op : AddOp<"i64", I64>;
def AddF32Op : AddOp<"f32", F32>;
def AddF64Op : AddOp<"f64", F64>;

class MulOp<string suffix, Type type> : INFRT_Op<"mul." # suffix, [NoSideEffect]> {
    let summary = "infrt.mul operation";
    let description = [{
        An operation that takes two inputs and returns their mul as result.
    }];

    let arguments = (ins type, type);
let results = (outs type);
let assemblyFormat = "operands attr-dict";
let verifier = ?;
}

def MulI32Op : MulOp<"i32", I32>;
def MulI64Op : MulOp<"i64", I64>;
def MulF32Op : MulOp<"f32", F32>;
def MulF64Op : MulOp<"f64", F64>;

class PrintOp<string suffix, Type type> : INFRT_Op<"print." # suffix> {
  let summary = "infrt.print operation";
  let description = [{
      An operation takes a number as input and prints to stdout.
    }];

  let arguments = (ins type);
  let assemblyFormat = "operands attr-dict";
  let verifier = ?;
}

109 110
def PrintI32Op : PrintOp<"i32", I32>;
def PrintI64Op : PrintOp<"i64", I64>;
Y
Yan Chunwei 已提交
111
def PrintF32Op : PrintOp<"f32", F32>;
112
def PrintF64Op : PrintOp<"f64", F64>;
Y
Yan Chunwei 已提交
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138

def GetStringOp : INFRT_Op<"get_string"> {
  let summary = "infrt.get_string";
  let description = [{
    Get a !infrt.string value from the given string attribute.
  }];

  let arguments = (ins StrAttr:$value);
  let results = (outs StringType);
  let assemblyFormat = "`(` $value `)` attr-dict";
  let verifier = ?;
}

def PrintStringOp : INFRT_Op<"print_string"> {
  let summary = "infrt.print_string";
  let description = [{
      An operation that prints a string.
  }];

  let arguments = (ins StringType:$input);
  let results = (outs);
  let assemblyFormat = "`(` $input `)` attr-dict";
  let verifier = ?;
}

#endif  // basic kernels