From f3207be96326113e73680871a5d7b9cfaa9b4209 Mon Sep 17 00:00:00 2001 From: TONG CHEN Date: Mon, 30 Sep 2019 10:29:15 -0400 Subject: [PATCH] add MLIR: compile/link/run (#342) * add MLIR: compile/link/run * change LLVMPROJECT to LLVM_PROJECT and do not allow linking with system LLVM libraries --- src/builder/CMakeLists.txt | 86 +++++++++++++++++++++++++++++++++++ src/builder/onnx_importer.cpp | 5 ++ src/builder/sgir.cpp | 69 ++++++++++++++++++++++++++++ src/builder/sgir.hpp | 26 +++++++++++ 4 files changed, 186 insertions(+) create mode 100644 src/builder/CMakeLists.txt create mode 100644 src/builder/onnx_importer.cpp create mode 100644 src/builder/sgir.cpp create mode 100644 src/builder/sgir.hpp diff --git a/src/builder/CMakeLists.txt b/src/builder/CMakeLists.txt new file mode 100644 index 0000000..eb4a7b4 --- /dev/null +++ b/src/builder/CMakeLists.txt @@ -0,0 +1,86 @@ +add_definitions(-DBOOST_LOG_DYN_LINK) + +add_library(builder + sgir.cpp + ) + +#Flags to link with LLVM/MLIR libraries +if(DEFINED ENV{LLVM_PROJECT_ROOT}) + set(LLVM_PROJECT_ROOT $ENV{LLVM_PROJECT_ROOT}) + if (EXISTS ${LLVM_PROJECT_ROOT}) + message(STATUS "LLVM_PROJECT_ROOT " ${LLVM_PROJECT_ROOT}) + else() + message(FATAL_ERROR "The path specified by LLVM_PROJECT_ROOT does not exist: " ${LLVM_PROJECT_ROOT}) + endif() +else() + message(FATAL_ERROR "env variable LLVM_PROJECT_ROOT not set") +endif() + +if(DEFINED ENV{LLVM_PROJECT_LIB}) + set(LLVM_PROJECT_LIB $ENV{LLVM_PROJECT_LIB}) +else() + set(LLVM_PROJECT_LIB $ENV{LLVM_PROJECT_ROOT}/build/lib) +endif() +if (EXISTS ${LLVM_PROJECT_LIB}) + message(STATUS "LLVM_PROJECT_LIB " ${LLVM_PROJECT_LIB}) +else() + message(FATAL_ERROR "The path specified by LLVM_PROJECT_LIB does not exist: " ${LLVM_PROJECT_LIB}) +endif() + +#include path +include_directories(${LLVM_PROJECT_ROOT}/llvm/projects/mlir/include) +include_directories(${LLVM_PROJECT_ROOT}/llvm/include) +include_directories(${LLVM_PROJECT_ROOT}/build/include) +include_directories(${LLVM_PROJECT_ROOT}/build/projects/mlir/include) + +find_library(MLIRLIBANALYSIS + NAMES MLIRAnalysis + PATHS ${LLVM_PROJECT_LIB} + NO_DEFAULT_PATH) + +find_library(MLIRLIBIR + NAMES MLIRIR + PATHS ${LLVM_PROJECT_LIB} + NO_DEFAULT_PATH) + +find_library(MLIRLIBPARSER + NAMES MLIRParser + PATHS ${LLVM_PROJECT_LIB} + NO_DEFAULT_PATH) + +find_library(MLIRLIBTRANSFORMS + NAMES MLIRTransforms + PATHS ${LLVM_PROJECT_LIB} + NO_DEFAULT_PATH) + +find_library(MLIRLIBVECTOROPS + NAMES MLIRVectorOps + PATHS ${LLVM_PROJECT_LIB} + NO_DEFAULT_PATH) + +find_library(MLIRLIBSUPPORT + NAMES MLIRSupport + PATHS ${LLVM_PROJECT_LIB} + NO_DEFAULT_PATH) + +find_library(MLIRLIBSTANDARDOPS + NAMES MLIRStandardOps + PATHS ${LLVM_PROJECT_LIB} + NO_DEFAULT_PATH) + +find_library(LLVMLIBSUPPORT + NAMES LLVMSupport + PATHS ${LLVM_PROJECT_LIB} + NO_DEFAULT_PATH) + +#libraries are set according to toy/Ch2 +set(MLIRLIBS ${MLIRLIBANALYSIS} ${MLIRLIBIR} ${MLIRLIBPARSER} ${MLIRLIBTRANSFORMS} + ${MLIRLIBANALYSIS} ${MLIRLIBVECTOROPS} ${MLIRLIBIR} ${MLIRLIBSUPPORT} ${MLIRLIBSTANDARDOPS} + ${LLVMLIBSUPPORT}) + + +target_link_libraries(builder compiler op onnx ${MLIRLIBS} curses) +target_include_directories(builder + PRIVATE + ${CMAKE_SOURCE_DIR}/third_party/onnx + ${CMAKE_SOURCE_DIR}) diff --git a/src/builder/onnx_importer.cpp b/src/builder/onnx_importer.cpp new file mode 100644 index 0000000..3d2205d --- /dev/null +++ b/src/builder/onnx_importer.cpp @@ -0,0 +1,5 @@ +#include "sgir.hpp" + +//empty module for MLIR is generated to test the compile/link of MLIR code +onnf::SGIRTest(); + diff --git a/src/builder/sgir.cpp b/src/builder/sgir.cpp new file mode 100644 index 0000000..50b31a0 --- /dev/null +++ b/src/builder/sgir.cpp @@ -0,0 +1,69 @@ +//===----------------------------------------------------------------------===// +// +// Copyright 2019 The IBM Research Authors. +// +// ============================================================================= +// +//===----------------------------------------------------------------------===// + +#include + +#include "mlir/Analysis/Verifier.h" +#include "mlir/Dialect/StandardOps/Ops.h" +#include "mlir/IR/Attributes.h" +#include "mlir/IR/Builders.h" +#include "mlir/IR/Function.h" +#include "mlir/IR/Location.h" +#include "mlir/IR/MLIRContext.h" +#include "mlir/IR/Module.h" +#include "mlir/IR/StandardTypes.h" +#include "mlir/IR/Types.h" +#include "llvm/ADT/STLExtras.h" +#include "llvm/ADT/ScopedHashTable.h" +#include "llvm/Support/raw_ostream.h" + +#include "sgir.hpp" + +using llvm::cast; +using llvm::dyn_cast; +using llvm::isa; +using llvm::ScopedHashTableScope; +using llvm::SmallVector; +using llvm::StringRef; +using llvm::Twine; + +namespace { + +class SGIRGenImpl { +public : + SGIRGenImpl(mlir::MLIRContext &context) + : context(context), builder(&context) {} + + mlir::ModuleOp mlirGen() { + theModule = mlir::ModuleOp::create(mlir::UnknownLoc::get(&context)); + return theModule; + } + +private: + mlir::MLIRContext &context; + mlir::ModuleOp theModule; + mlir::OpBuilder builder; + +} ; + +} //namespace + +namespace onnf { + +int SGIRTest() { + mlir::MLIRContext context; + + mlir::OwningModuleRef module = SGIRGenImpl(context).mlirGen(); + if (!module) + return 1; + module->dump(); + return 0; +} + +} //namespace onnf + diff --git a/src/builder/sgir.hpp b/src/builder/sgir.hpp new file mode 100644 index 0000000..2503220 --- /dev/null +++ b/src/builder/sgir.hpp @@ -0,0 +1,26 @@ +//===----------------------------------------------------------------------===// +// +// Copyright 2019 The IBM Research Authors. +// +// ============================================================================= +// +//===----------------------------------------------------------------------===// + +#pragma once + +#include + +namespace mlir { +class MLIRContext; +class OwningModuleRef; +} // namespace mlir + +namespace onnf { + /*! + * Test dummy + * @return status, 0 for success, otherwise failure + **/ + int SGIRTest(); + +} //namespace onnf +