infrt_base.h 2.3 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 28 29 30 31
namespace infrt {
namespace dialect {
class INFRTDialect : public mlir::Dialect {
  explicit INFRTDialect(mlir::MLIRContext *context)
      : mlir::Dialect(
            getDialectNamespace(), context, mlir::TypeID::get<INFRTDialect>()) {
Y
Yan Chunwei 已提交
32 33 34 35 36 37 38 39 40 41
    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();
42
  friend class mlir::MLIRContext;
Y
Yan Chunwei 已提交
43 44 45 46

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

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

56
static mlir::SmallVector<mlir::Value, 4> cvtValueToValueRange(
57
    const mlir::Value &operand) {
58
  return mlir::SmallVector<mlir::Value, 4>(1, operand);
Y
Yan Chunwei 已提交
59 60
}

61
static mlir::SmallVector<mlir::Value, 4> concatTwoValueRange(
62
    mlir::ValueRange operand_0, mlir::ValueRange operand_1) {
63
  mlir::SmallVector<mlir::Value, 4> operands;
Y
Yan Chunwei 已提交
64 65 66 67
  operands.append(operand_0.begin(), operand_0.end());
  operands.append(operand_1.begin(), operand_1.end());
  return operands;
}
68
}  // namespace infrt