Skip to content

Commit

Permalink
Merge pull request llvm#9 from clang-ykt/use-mlir-in-llvm-project
Browse files Browse the repository at this point in the history
Update to latest MLIR
  • Loading branch information
tjingrant authored Jan 6, 2020
2 parents 5b38938 + 38e7d2d commit 322002f
Show file tree
Hide file tree
Showing 11 changed files with 214 additions and 227 deletions.
41 changes: 11 additions & 30 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,52 +7,33 @@ jobs:
steps:
- checkout
- run:
name: "Pull Submodules"
name: Pull Submodules
command: |
git submodule update --init --recursive
- run:
name: Check current directory
command: pwd
- run:
name: Check current directory content
command: ls
- run:
name: Installing GCC
command: 'sudo apt-get update && sudo apt-get install -y gcc g++'
- run:
name: Install CMAKE
command: 'sudo apt-get update && sudo apt-get install -y cmake ninja-build'
- run:
name: Install Protobuf
command: 'sudo apt-get update && sudo apt-get install -y protobuf-compiler'
- run:
name: Check gcc version
command: gcc --version

name: Installing GCC, CMake, Ninja, Protobuf
command: sudo apt-get update && sudo apt-get install -y gcc g++ cmake ninja-build protobuf-compiler
# Use cached mlir installation if possible.
- restore_cache:
key: ONNF-MLIR-{{ arch }}
key: V2-LLVM-PROJECT-{{ arch }}
- run:
name: Install MLIR
command: |
# Check whether cache restoration succeeds by checking whether
# mlir-opt executable exists.
if [ ! -f llvm-project/build/bin/mlir-opt ]; then
git clone https://github.com/llvm/llvm-project.git
cd llvm-project && git checkout 9b6ad8466bb8b97082b705270603ad7f4559e931 && cd ..
git clone https://github.com/tensorflow/mlir llvm-project/llvm/projects/mlir
cd llvm-project/llvm/projects/mlir && git checkout 0710266d0f56cf6ab0f437badbd7416b6cecdf5f && cd ../../../..
mkdir llvm-project/build
cd llvm-project/build
cmake -G Ninja ../llvm -DLLVM_ENABLE_RTTI=ON -DLLVM_BUILD_EXAMPLES=OFF -DLLVM_TARGETS_TO_BUILD="host" -DCMAKE_BUILD_TYPE=Release
CMAKE_EXE_LINKER_FLAGS="-Wl,--reduce-memory-overheads -Wl,--hash-size=512" cmake --build . --target check-mlir -- -j 4
export MAKEFLAGS=-j4
source .circleci/install-mlir.sh
fi
- save_cache:
key: ONNF-MLIR-{{ arch }}
key: V2-LLVM-PROJECT-{{ arch }}
paths:
- llvm-project
- run:
name: Install ONNF
command: |
mkdir build && cd build
LLVM_SRC=$(pwd)/../llvm-project/llvm LLVM_BUILD=$(pwd)/../llvm-project/build cmake ..
LLVM_PROJ_SRC=$(pwd)/../llvm-project/ LLVM_PROJ_BUILD=$(pwd)/../llvm-project/build cmake ..
make all
LIT_OPTS=-v make check-mlir-lit
- run:
Expand Down
12 changes: 12 additions & 0 deletions .circleci/install-mlir.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
git clone https://github.com/llvm/llvm-project.git
mkdir llvm-project/build
cd llvm-project/build
cmake -G Ninja ../llvm \
-DLLVM_ENABLE_PROJECTS=mlir \
-DLLVM_BUILD_EXAMPLES=ON \
-DLLVM_TARGETS_TO_BUILD="host" \
-DCMAKE_BUILD_TYPE=Release \
-DLLVM_ENABLE_ASSERTIONS=ON \
-DLLVM_ENABLE_RTTI=ON

cmake --build . --target check-mlir -- ${MAKEFLAGS}
46 changes: 23 additions & 23 deletions MLIR.cmake
Original file line number Diff line number Diff line change
@@ -1,38 +1,38 @@
# Path to LLVM source folder.
if(DEFINED ENV{LLVM_SRC})
set(LLVM_SRC $ENV{LLVM_SRC})
if(EXISTS ${LLVM_SRC})
message(STATUS "LLVM_SRC " ${LLVM_SRC})
if(DEFINED ENV{LLVM_PROJ_SRC})
set(LLVM_PROJ_SRC $ENV{LLVM_PROJ_SRC})
if(EXISTS ${LLVM_PROJ_SRC})
message(STATUS "LLVM_PROJ_SRC " ${LLVM_PROJ_SRC})
else()
message(FATAL_ERROR "The path specified by LLVM_SRC does not exist: "
${LLVM_SRC})
message(FATAL_ERROR "The path specified by LLVM_PROJ_SRC does not exist: "
${LLVM_PROJ_SRC})
endif()
else()
message(FATAL_ERROR "env variable LLVM_SRC not set")
message(FATAL_ERROR "env variable LLVM_PROJ_SRC not set")
endif()

# Path to LLVM build folder
if(DEFINED ENV{LLVM_BUILD})
set(LLVM_BUILD $ENV{LLVM_BUILD})
if(EXISTS ${LLVM_BUILD})
message(STATUS "LLVM_BUILD " ${LLVM_BUILD})
if(DEFINED ENV{LLVM_PROJ_BUILD})
set(LLVM_PROJ_BUILD $ENV{LLVM_PROJ_BUILD})
if(EXISTS ${LLVM_PROJ_BUILD})
message(STATUS "LLVM_PROJ_BUILD " ${LLVM_PROJ_BUILD})
else()
message(FATAL_ERROR "The path specified by LLVM_BUILD does not exist: "
${LLVM_BUILD})
message(FATAL_ERROR "The path specified by LLVM_PROJ_BUILD does not exist: "
${LLVM_PROJ_BUILD})
endif()
else()
message(FATAL_ERROR "env variable LLVM_BUILD not set")
message(FATAL_ERROR "env variable LLVM_PROJ_BUILD not set")
endif()

# LLVM project lib folder
set(LLVM_PROJECT_LIB ${LLVM_BUILD}/lib)
set(LLVM_PROJECT_LIB ${LLVM_PROJ_BUILD}/lib)

# Include paths for MLIR
set(LLVM_SRC_INCLUDE_PATH ${LLVM_SRC}/include)
set(LLVM_BIN_INCLUDE_PATH ${LLVM_BUILD}/include)
set(MLIR_SRC_INCLUDE_PATH ${LLVM_SRC}/projects/mlir/include)
set(MLIR_BIN_INCLUDE_PATH ${LLVM_BUILD}/projects/mlir/include)
set(MLIR_TOOLS_DIR ${LLVM_BUILD}/bin)
set(LLVM_SRC_INCLUDE_PATH ${LLVM_PROJ_SRC}/llvm/include)
set(LLVM_BIN_INCLUDE_PATH ${LLVM_PROJ_BUILD}/include)
set(MLIR_SRC_INCLUDE_PATH ${LLVM_PROJ_SRC}/mlir/include)
set(MLIR_BIN_INCLUDE_PATH ${LLVM_PROJ_BUILD}/tools/mlir/include)
set(MLIR_TOOLS_DIR ${LLVM_PROJ_BUILD}/bin)

set(ONNF_TOOLS_DIR ${ONNF_BIN_ROOT}/bin)
set(ONNF_LIT_TEST_SRC_DIR ${CMAKE_SOURCE_DIR}/test/mlir)
Expand Down Expand Up @@ -173,7 +173,7 @@ function(whole_archive_link target lib_dir)
endfunction(whole_archive_link)

function(whole_archive_link_mlir target)
whole_archive_link(${target} ${LLVM_BUILD}/lib ${ARGN})
whole_archive_link(${target} ${LLVM_PROJ_BUILD}/lib ${ARGN})
endfunction(whole_archive_link_mlir)

function(whole_archive_link_onnf target)
Expand All @@ -184,7 +184,7 @@ function(whole_archive_link_onnf target)
endfunction(whole_archive_link_onnf)

set(LLVM_CMAKE_DIR
"${LLVM_BUILD}/lib/cmake/llvm"
"${LLVM_PROJ_BUILD}/lib/cmake/llvm"
CACHE PATH "Path to LLVM cmake modules")
list(APPEND CMAKE_MODULE_PATH "${LLVM_CMAKE_DIR}")
include(AddLLVM)
Expand All @@ -205,5 +205,5 @@ endfunction()
# table gen utility itself can be detected and cause re-compilation of .td file.
add_executable(mlir-tblgen IMPORTED)
set_property(TARGET mlir-tblgen
PROPERTY IMPORTED_LOCATION ${LLVM_BUILD}/bin/mlir-tblgen)
PROPERTY IMPORTED_LOCATION ${LLVM_PROJ_BUILD}/bin/mlir-tblgen)
set(MLIR_TABLEGEN_EXE mlir-tblgen)
28 changes: 14 additions & 14 deletions src/builder/frontend_dialect_transformer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ struct OnnxOnnfSymbolMapping {
* @param name onnx tensor name.
* @return onnf tensor corresponding to `name`.
*/
mlir::Value *GetTensorByOnnxName(std::string name) {
mlir::Value GetTensorByOnnxName(std::string name) {
assert(onnx_name2onnf_tensor.find(legalize_name(name)) !=
onnx_name2onnf_tensor.end() &&
"Tensor not found");
Expand All @@ -81,9 +81,9 @@ struct OnnxOnnfSymbolMapping {
/*!
* Add a new mapping from onnx tensor name to MLIR symbol.
* @param name onnx tensor name.
* @param tensor MLIR Value* pointer.
* @param tensor MLIR Value pointer.
*/
void AddMapping(std::string name, mlir::Value *tensor) {
void AddMapping(std::string name, mlir::Value tensor) {
assert(onnx_name2onnf_tensor.count(legalize_name(name)) == 0 &&
"Tensor already exists.");
onnx_name2onnf_tensor.emplace(legalize_name(name), tensor);
Expand All @@ -97,7 +97,7 @@ struct OnnxOnnfSymbolMapping {
/*!
* mapping from onnx tensor names to MLIR tensor.
*/
std::map<std::string, mlir::Value*> onnx_name2onnf_tensor;
std::map<std::string, mlir::Value> onnx_name2onnf_tensor;
};

class FrontendGenImpl {
Expand Down Expand Up @@ -192,13 +192,13 @@ class FrontendGenImpl {

/*!
* Import a input tensor symbol by recording a new entry in frontend_symbols_
* recording the mapping between legalized onnx tensor name and mlir::Value*
* recording the mapping between legalized onnx tensor name and mlir::Value
* for further lookup in computation node importing.
* @param input onnx input tensor ValueInfoProto.
* @param symbol mlir input argument.
*/
void ImportInputTensorSymbol(const onnx::ValueInfoProto &input,
mlir::Value *symbol) {
mlir::Value symbol) {
auto input_tensor_legalized_name = legalize_name(input.name());
assert(
!frontend_symbols_.ContainKey(input_tensor_legalized_name) &&
Expand Down Expand Up @@ -480,7 +480,7 @@ class FrontendGenImpl {
}

void ImportNodeGeneric(onnx::NodeProto node) {
std::vector<mlir::Value *> inputs;
std::vector<mlir::Value> inputs;
for (auto item : node.input()) {
if (frontend_symbols_.ContainKey(legalize_name(item))) {
inputs.push_back(frontend_symbols_.GetTensorByOnnxName(item));
Expand Down Expand Up @@ -515,7 +515,7 @@ class FrontendGenImpl {
onnx::NodeProto node, int nIn, int nOut,
std::initializer_list<std::tuple<std::string, std::string, std::string>>
attrs) {
std::vector<mlir::Value *> inputs;
std::vector<mlir::Value> inputs;
for (auto item : node.input()) {
if (frontend_symbols_.ContainKey(legalize_name(item))) {
inputs.push_back(frontend_symbols_.GetTensorByOnnxName(item));
Expand Down Expand Up @@ -562,7 +562,7 @@ class FrontendGenImpl {
onnx::NodeProto node, int nIn, int nOut,
std::initializer_list<std::tuple<std::string, std::string, std::string>>
attrs) {
std::vector<mlir::Value *> inputs;
std::vector<mlir::Value> inputs;
for (auto item : node.input()) {
if (frontend_symbols_.ContainKey(legalize_name(item))) {
inputs.push_back(frontend_symbols_.GetTensorByOnnxName(item));
Expand Down Expand Up @@ -633,7 +633,7 @@ class FrontendGenImpl {
}

void ImportNode(onnx::NodeProto node) {
std::vector<mlir::Value *> inputs;
std::vector<mlir::Value> inputs;
for (auto item : node.input()) {
if (frontend_symbols_.ContainKey(legalize_name(item))) {
inputs.push_back(frontend_symbols_.GetTensorByOnnxName(item));
Expand Down Expand Up @@ -662,17 +662,17 @@ class FrontendGenImpl {
* Import output tensor, by doing the following:
* - Add the type of this output tensor to a list of tensor
* types representing return types of this graph function.
* - Add this output tensor to the list of mlir::Value*
* - Add this output tensor to the list of mlir::Value
* to be returned by the function representing computation graph.
* @param output onnx output tensor ValueInfoProto.
* @param ret_types a vector of tensor types representing graph's
* output tensor types.
* @param ret_vals a vector of mlir Value* representing graph's
* @param ret_vals a vector of mlir Value representing graph's
* output tensor.
*/
void ImportOutputTensor(const onnx::ValueInfoProto &output,
llvm::SmallVectorImpl<mlir::Type> &ret_types,
llvm::SmallVectorImpl<mlir::Value *> &ret_vals) {
llvm::SmallVectorImpl<mlir::Value> &ret_vals) {
auto output_tensor_legalized_name = legalize_name(output.name());
assert(
frontend_symbols_.ContainKey(output_tensor_legalized_name) &&
Expand Down Expand Up @@ -722,7 +722,7 @@ class FrontendGenImpl {
}

llvm::SmallVector<mlir::Type, 4> ret_types;
llvm::SmallVector<mlir::Value *, 4> ret_vals;
llvm::SmallVector<mlir::Value, 4> ret_vals;
// Import the output tensors
for (const auto &output : graph.output()) {
ImportOutputTensor(output, ret_types, ret_vals);
Expand Down
19 changes: 10 additions & 9 deletions src/dialect/krnl/krnl_helper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ namespace onnf {

using namespace mlir;

ParseResult KrnlDialectOperandParser::ParseOptionalOperand(
const Type& operandType, Value*& operand) {
ParseResult
KrnlDialectOperandParser::ParseOptionalOperand(const Type &operandType,
Value &operand) {
// If operand queue is empty, parse more operands and cache them.
if (_operandRefQueue.empty()) {
// Parse operand types:
Expand All @@ -27,7 +28,7 @@ ParseResult KrnlDialectOperandParser::ParseOptionalOperand(
auto operand_ref = _operandRefQueue.front();
_operandRefQueue.pop();

llvm::SmallVector<Value*, 1> operands;
llvm::SmallVector<Value, 1> operands;
_parser.resolveOperand(operand_ref, operandType, operands);
operand = operands.front();
return success();
Expand All @@ -38,25 +39,25 @@ ParseResult KrnlDialectOperandParser::ParseOptionalOperand(
}

ParseResult KrnlDialectOperandParser::ParseOptionalOperand(
const Type& operandType, llvm::SmallVectorImpl<Value*>& operandList) {
Value* operand = nullptr;
const Type &operandType, llvm::SmallVectorImpl<Value> &operandList) {
Value operand = nullptr;
if (ParseOptionalOperand(operandType, operand))
return failure();

operandList.emplace_back(operand);
return success();
}

ParseResult KrnlDialectOperandParser::ParseOperand(
const Type& operandType, Value*& operand) {
ParseResult KrnlDialectOperandParser::ParseOperand(const Type &operandType,
Value &operand) {
if (ParseOptionalOperand(operandType, operand))
return _parser.emitError(
_parser.getCurrentLocation(), "Expecting an operand.");
return success();
}

ParseResult KrnlDialectOperandParser::ParseOperand(
const Type& operandType, llvm::SmallVectorImpl<Value*>& operandList) {
const Type &operandType, llvm::SmallVectorImpl<Value> &operandList) {
if (ParseOptionalOperand(operandType, operandList))
return _parser.emitError(
_parser.getCurrentLocation(), "Expecting an operand.");
Expand Down Expand Up @@ -129,7 +130,7 @@ void KrnlIterateOperandPack::pushConstantBound(int64_t bound) {
boundMaps.emplace_back(AffineMapAttr::get(map));
}

void KrnlIterateOperandPack::pushOperandBound(mlir::Value* operand) {
void KrnlIterateOperandPack::pushOperandBound(mlir::Value operand) {
if (boundMaps.size() % 2 == 0)
_operands.emplace_back(inputLoops[boundMaps.size() / 2]);
AffineMap map = builder.getSymbolIdentityMap();
Expand Down
Loading

0 comments on commit 322002f

Please sign in to comment.