mlir-hlo/tools/mlir-tblgen-builder/Builder/BuilderImpl.h

115 lines
3.8 KiB
C
Raw Normal View History

2021-08-04 20:24:07 +08:00
#ifndef BUILDER_BUILDERIMPL_
#define BUILDER_BUILDERIMPL_
2021-08-17 11:28:17 +08:00
#include "Attribute.h"
#include "AttributeImpl.h"
2021-08-04 20:24:07 +08:00
#include "Builder.h"
2021-08-17 11:28:17 +08:00
#include "OpImpl.h"
2021-08-04 20:24:07 +08:00
#include "llvm/Support/Casting.h"
2021-08-16 15:35:37 +08:00
// #include "llvm/Support/InitLLVM.h"
2021-08-17 11:28:17 +08:00
#include "mlir-hlo/Dialect/mhlo/IR/hlo_ops.h"
#include "mlir-hlo/Dialect/mhlo/IR/lhlo_ops.h"
#include "mlir-hlo/Dialect/mhlo/transforms/register_passes.h"
2021-08-04 20:24:07 +08:00
#include "mlir/IR/Attributes.h"
2021-08-13 15:05:10 +08:00
#include "mlir/IR/Block.h"
2021-08-04 20:24:07 +08:00
#include "mlir/IR/Builders.h"
2021-08-13 15:05:10 +08:00
#include "mlir/IR/BuiltinOps.h"
#include "mlir/IR/BuiltinTypes.h"
2021-08-04 20:24:07 +08:00
#include "mlir/IR/MLIRContext.h"
#include "mlir/IR/Operation.h"
#include "mlir/IR/Types.h"
#include "mlir/IR/Value.h"
2021-08-17 11:28:17 +08:00
#include "mlir/InitAllDialects.h"
#include "mlir/InitAllPasses.h"
2021-08-04 20:24:07 +08:00
namespace builder {
class Builder::Impl {
public:
2021-08-13 15:05:10 +08:00
Impl() : builder_(&context_) {
2021-08-16 15:35:37 +08:00
// llvm::InitLLVM y(argc, argv);
// llvm::InitializeNativeTarget();
// llvm::InitializeNativeTargetAsmPrinter();
// llvm::InitializeNativeTargetAsmParser();
// mlir::initializeLLVMPasses();
// Register any command line options.
// registerAsmPrinterCLOptions();
// registerMLIRContextCLOptions();
// registerPassManagerCLOptions();
// registerDefaultTimingManagerCLOptions();
// DebugCounter::registerCLOptions();
2021-08-17 11:28:17 +08:00
mlir::registerAllPasses();
2021-08-16 15:35:37 +08:00
mlir::mhlo::registerAllMhloPasses();
2021-08-17 11:28:17 +08:00
// mlir::lmhlo::registerAllLmhloPasses();
// mlir::disc_ral::registerAllDiscRalPasses();
2021-08-16 15:35:37 +08:00
mlir::DialectRegistry registry;
// mlir::registerAllToLLVMIRTranslations(registry);
2021-08-17 11:28:17 +08:00
mlir::registerAllDialects(registry);
2021-08-16 15:35:37 +08:00
registry.insert<mlir::mhlo::MhloDialect>();
// registry.insert<mlir::chlo::HloClientDialect>();
// registry.insert<mlir::lmhlo::LmhloDialect>();
// registry.insert<mlir::lmhlo_gpu::LmhloGpuDialect>();
// registry.insert<mlir::disc_ral::RalDialect>();
context_.appendDialectRegistry(registry);
context_.loadAllAvailableDialects();
2021-08-13 15:05:10 +08:00
module_ = mlir::ModuleOp::create(mlir::UnknownLoc::get(&context_));
llvm::SmallVector<mlir::Type, 4> arg_types;
// Create the main function.
mlir::FunctionType funcType = builder_.getFunctionType(arg_types, {});
main_func_ = mlir::FuncOp::create(builder_.getUnknownLoc(), "main",
funcType, /* attrs = */ {});
entry_block_ = main_func_.addEntryBlock();
builder_.setInsertionPointToStart(entry_block_);
module_.push_back(main_func_);
}
mlir::Location GetLoc() { return builder_.getUnknownLoc(); }
mlir::OpBuilder GetBuilder() { return builder_; }
mlir::MLIRContext* GetContext() { return &context_; }
2021-08-16 15:35:37 +08:00
void DumpModule() { module_.dump(); }
2021-08-04 20:24:07 +08:00
2021-08-17 11:28:17 +08:00
builder::Op CreateInput(const builder::Type& type) {
mlir::BlockArgument arg =
entry_block_->addArgument(type.GetImpl()->GetMlirType(&context_));
builder::Op op;
op.GetImpl()->SetValue(arg);
return op;
}
void SetOutput(const std::vector<builder::Op>& outputs) {
llvm::SmallVector<mlir::Type, 4> arg_types;
int arg_num = entry_block_->getNumArguments();
for (int i = 0; i < arg_num; ++i) {
arg_types.push_back(entry_block_->getArgument(i).getType());
}
llvm::SmallVector<mlir::Type, 4> ret_types;
llvm::SmallVector<mlir::Value, 4> ret_vals;
for (auto& out : outputs) {
mlir::Value v = out.GetImpl()->GetResult();
ret_types.push_back(v.getType());
ret_vals.push_back(v);
}
// return all output tensors.
builder_.create<mlir::ReturnOp>(builder_.getUnknownLoc(), ret_vals);
// Update main function input/output type
mlir::FunctionType funcType =
builder_.getFunctionType(arg_types, ret_types);
main_func_.setType(funcType);
}
2021-08-04 20:24:07 +08:00
private:
2021-08-13 15:05:10 +08:00
mlir::MLIRContext context_;
mlir::ModuleOp module_;
mlir::OpBuilder builder_;
mlir::FuncOp main_func_;
mlir::Block* entry_block_;
2021-08-04 20:24:07 +08:00
};
} // namespace builder
#endif