infrt_base.h 2.7 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
// Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved.
//
// 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.

#pragma once

#include <mlir/IR/Builders.h>
#include <mlir/IR/Dialect.h>
#include <mlir/IR/DialectImplementation.h>
#include <mlir/IR/MLIRContext.h>
#include <mlir/IR/TypeUtilities.h>
#include <mlir/IR/Types.h>

#include "paddle/infrt/dialect/infrt_base.hpp.inc"

26 27
namespace infrt {
namespace dialect {
28

29 30 31 32
class INFRTDialect : public mlir::Dialect {
  explicit INFRTDialect(mlir::MLIRContext *context)
      : mlir::Dialect(
            getDialectNamespace(), context, mlir::TypeID::get<INFRTDialect>()) {
Y
Yan Chunwei 已提交
33 34 35 36 37 38 39 40 41 42
    initialize();
  }

  // parse types registered to the dialect.
  mlir::Type parseType(mlir::DialectAsmParser &parser) const override;
  // print types registered to the dialect.
  void printType(mlir::Type type,
                 mlir::DialectAsmPrinter &printer) const override;

  void initialize();
43
  friend class mlir::MLIRContext;
Y
Yan Chunwei 已提交
44 45

 public:
46
  static ::llvm::StringRef getDialectNamespace() { return "Infrt"; }
Y
Yan Chunwei 已提交
47
};
48
}  // namespace dialect
Y
Yan Chunwei 已提交
49 50 51 52 53 54 55 56

template <typename T>
static mlir::IntegerAttr createI32Attr(mlir::OpBuilder &b,  // NOLINT
                                       mlir::Location loc,
                                       T constant) {
  return b.getIntegerAttr(b.getI32Type(), constant);
}

57 58 59 60 61 62 63 64 65 66 67 68 69 70
template <typename T>
static mlir::IntegerAttr createSI32Attr(mlir::OpBuilder &b,  // NOLINT
                                        mlir::Location loc,
                                        T constant) {
  return b.getSI32IntegerAttr(constant);
}

template <typename T>
static mlir::FloatAttr createF32Attr(mlir::OpBuilder &b,  // NOLINT
                                     mlir::Location loc,
                                     T constant) {
  return b.getF32FloatAttr(constant);
}

71
static mlir::SmallVector<mlir::Value, 4> cvtValueToValueRange(
72
    const mlir::Value &operand) {
73
  return mlir::SmallVector<mlir::Value, 4>(1, operand);
Y
Yan Chunwei 已提交
74 75
}

76
static mlir::SmallVector<mlir::Value, 4> concatTwoValueRange(
77
    mlir::ValueRange operand_0, mlir::ValueRange operand_1) {
78
  mlir::SmallVector<mlir::Value, 4> operands;
Y
Yan Chunwei 已提交
79 80 81 82
  operands.append(operand_0.begin(), operand_0.end());
  operands.append(operand_1.begin(), operand_1.end());
  return operands;
}
83
}  // namespace infrt