From c567781fa3ecef324f7d311a6222cf75250f2173 Mon Sep 17 00:00:00 2001 From: Byron Changuion <7623249+byronChanguion@users.noreply.github.com> Date: Sun, 19 Apr 2020 07:11:24 -0700 Subject: [PATCH] Add support for Windows using Visual Studio Compiler (#86) * Move to more recent LLVM commit ID * Update LLVM cache version from V9 to V10 * Update to latest LLVM commit id from master, roll back conditions in util scripts * Update circlci LLVM cache tag to ensure ci updates builds with latest LLVM commit id * Update README.md to have matching LLVM commit id * Update doc/Dialtects/onnx.md * Enable onnx-mlir for VS builds on Windows * Update README to include lit * Update build command for Windows to include config * Update build instructions, add cmd files for windows, enable single source of truth for MLIR commit-id (clone-mlir.sh) * Add Visual Studio workload info * Update ONNX op definitions * Revert onnx submodule back to previous commit, disable warnings in CMakeLists to work around build issues with MSVC * Update environment for path to PDcurses on Windows * Fix directory strings to be compatible with Windows or Linux style slashes * Fix install-mlir.sh so it works when sourced * Ensure README and cmd files match and have correct paths * Properly quote ONNX_MLIR_SRC_DIR * Address PR feedback: Use llvm_unreachable to indicate failure to convert attribute proto to name/value pair Co-authored-by: Tian Jin --- CMakeLists.txt | 6 + MLIR.cmake | 92 ++++++++++++-- README.md | 114 +++++++++++++++++- docs/README.md | 114 +++++++++++++++++- src/Builder/FrontendDialectTransformer.cpp | 2 + .../ONNXToKrnl/Math/Elementwise.cpp | 2 + .../ONNXToKrnl/ONNXToKrnlCommon.cpp | 2 +- src/Dialect/Krnl/KrnlOps.cpp | 3 + src/Dialect/ONNX/ONNXOps.cpp | 2 +- test/mlir/lit.site.cfg.py.in | 17 +-- utils/build-mlir.cmd | 16 +++ utils/build-mlir.sh | 12 ++ utils/clone-mlir.sh | 3 + utils/install-mlir.cmd | 8 ++ utils/install-mlir.sh | 17 +-- utils/install-onnx-mlir.cmd | 16 +++ 16 files changed, 380 insertions(+), 46 deletions(-) create mode 100644 utils/build-mlir.cmd create mode 100644 utils/build-mlir.sh create mode 100644 utils/clone-mlir.sh create mode 100644 utils/install-mlir.cmd create mode 100644 utils/install-onnx-mlir.cmd diff --git a/CMakeLists.txt b/CMakeLists.txt index f2f7dee..e3b474d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -13,12 +13,18 @@ set(CMAKE_CXX_FLAGS_RELEASE "-O2 -DNDEBUG") set(ONNX_MLIR_SRC_ROOT "${CMAKE_CURRENT_SOURCE_DIR}") set(ONNX_MLIR_BIN_ROOT "${CMAKE_CURRENT_BINARY_DIR}") + set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib) set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib) set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin) include(MLIR.cmake) +if (MSVC) + # Disable warnings that prevent building of dependencies + # like onnx + set(CMAKE_CXX_FLAGS "-wd4244 -wd4267 -wd4530 -wd4624") +endif() add_subdirectory(third_party/onnx) add_subdirectory(third_party/benchmark) add_subdirectory(third_party/pybind11) diff --git a/MLIR.cmake b/MLIR.cmake index 873c480..ba3fc12 100644 --- a/MLIR.cmake +++ b/MLIR.cmake @@ -25,16 +25,55 @@ else() endif() # LLVM project lib folder -set(LLVM_PROJECT_LIB ${LLVM_PROJ_BUILD}/lib) +if (ENV{LLVM_PROJECT_LIB}) + set(LLVM_PROJECT_LIB $ENV{LLVM_PROJECT_LIB}) +else() + if(MSVC) + if (CMAKE_BUILD_TYPE) + set(LLVM_PROJECT_LIB ${LLVM_PROJ_BUILD}/${CMAKE_BUILD_TYPE}/lib) + else() + set(LLVM_PROJECT_LIB ${LLVM_PROJ_BUILD}/release/lib) + endif() + else() + set(LLVM_PROJECT_LIB ${LLVM_PROJ_BUILD}/lib) + endif() +endif() +message(STATUS "LLVM_PROJECT_LIB:" ${LLVM_PROJECT_LIB}) + +# LLVM project bin folder +if (ENV{LLVM_PROJ_BIN}) + set(LLVM_PROJ_BIN $ENV{LLVM_PROJ_BIN}) +else() + if(MSVC) + if (CMAKE_BUILD_TYPE) + set(LLVM_PROJ_BIN ${LLVM_PROJ_BUILD}/${CMAKE_BUILD_TYPE}/bin) + else() + set(LLVM_PROJ_BIN ${LLVM_PROJ_BUILD}/Release/bin) + endif() + else() + set(LLVM_PROJ_BIN ${LLVM_PROJ_BUILD}/bin) + endif() +endif() +message(STATUS "LLVM_PROJ_BIN:" ${LLVM_PROJ_BIN}) # Include paths for MLIR 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(MLIR_TOOLS_DIR ${LLVM_PROJ_BIN}) -set(ONNX_MLIR_TOOLS_DIR ${ONNX_MLIR_BIN_ROOT}/bin) +# ONNX-MLIR tools folder +if(MSVC) + if (CMAKE_BUILD_TYPE) + set(ONNX_MLIR_TOOLS_DIR ${CMAKE_BINARY_DIR}/bin/${CMAKE_BUILD_TYPE}) + else() + set(ONNX_MLIR_TOOLS_DIR ${CMAKE_BINARY_DIR}/bin/Release) + endif() +else() + set(ONNX_MLIR_TOOLS_DIR ${CMAKE_BINARY_DIR}/bin) +endif() +message(STATUS "ONNX_MLIR_TOOLS_DIR:" ${ONNX_MLIR_TOOLS_DIR}) set(ONNX_MLIR_LIT_TEST_SRC_DIR ${ONNX_MLIR_SRC_ROOT}/test/mlir) set(ONNX_MLIR_LIT_TEST_BUILD_DIR ${CMAKE_BINARY_DIR}/test/mlir) @@ -53,8 +92,35 @@ message(STATUS "BUILD_SHARED_LIBS ${BUILD_SHARED_LIBS}") # Threading libraries required due to parallel pass execution. find_package(Threads REQUIRED) # libcurses and libz required by libLLVMSupport -find_package(Curses REQUIRED) -find_package(ZLIB REQUIRED) +if(MSVC) + if(DEFINED ENV{CURSES_LIB_PATH}) + find_library(CURSES_LIBRARIES + NAMES pdcurses + PATHS $ENV{CURSES_LIB_PATH} + NO_DEFAULT_PATH) + if(CURSES_LIBRARIES) + message(STATUS "CURSES_LIBRARIES: ${CURSES_LIBRARIES}") + else() + message(FATAL_ERROR "Could not find curses library at $ENV{CURSES_LIB_PATH}") + endif() + else() + message(FATAL_ERROR "Expected CURSES_LIB_PATH environment variable to be set to location of pdcurses.lib") + endif() +else() + find_package(Curses REQUIRED) + find_package(ZLIB REQUIRED) +endif() + +# Set output library path +if(MSVC) + if (CMAKE_BUILD_TYPE) + set(ONNX_MLIR_LIB_DIR ${CMAKE_LIBRARY_OUTPUT_DIRECTORY}/${CMAKE_BUILD_TYPE}) + else() + set(ONNX_MLIR_LIB_DIR ${CMAKE_LIBRARY_OUTPUT_DIRECTORY}/Release) + endif() +else() + set(ONNX_MLIR_LIB_DIR ${CMAKE_LIBRARY_OUTPUT_DIRECTORY}) +endif() function(find_mlir_lib lib) find_library(${lib} @@ -171,8 +237,8 @@ set(MLIRLibs ${LLVMSupport} ${LLVMDemangle} ${CMAKE_THREAD_LIBS_INIT} - ${CURSES_LIBRARIES} - ${ZLIB_LIBRARIES}) + ${CURSES_LIBRARIES} + ${ZLIB_LIBRARIES}) # MLIR libraries that must be linked with --whole-archive for static build or # must be specified on LD_PRELOAD for shared build. @@ -211,7 +277,7 @@ function(whole_archive_link target lib_dir) endforeach(LIB) elseif(MSVC) foreach(LIB ${ARGN}) - string(CONCAT link_flags ${link_flags} "/WHOLEARCHIVE:${LIB} ") + string(CONCAT link_flags ${link_flags} "/WHOLEARCHIVE:${lib_dir}/${LIB} ") endforeach(LIB) else() set(link_flags "${link_flags} -L${lib_dir} -Wl,--whole-archive,") @@ -241,9 +307,9 @@ endfunction(ld_preload_libs) function(whole_archive_link_mlir target) if(BUILD_SHARED_LIBS) - ld_preload_libs(${target} ${LLVM_PROJ_BUILD}/lib ${ARGN}) + ld_preload_libs(${target} ${LLVM_PROJECT_LIB} ${ARGN}) else() - whole_archive_link(${target} ${LLVM_PROJ_BUILD}/lib ${ARGN}) + whole_archive_link(${target} ${LLVM_PROJECT_LIB} ${ARGN}) endif() endfunction(whole_archive_link_mlir) @@ -252,9 +318,9 @@ function(whole_archive_link_onnx_mlir target) add_dependencies(${target} ${lib_target}) endforeach(lib_target) if(BUILD_SHARED_LIBS) - ld_preload_libs(${target} ${CMAKE_BINARY_DIR}/lib ${ARGN}) + ld_preload_libs(${target} ${ONNX_MLIR_LIB_DIR} ${ARGN}) else() - whole_archive_link(${target} ${CMAKE_BINARY_DIR}/lib ${ARGN}) + whole_archive_link(${target} ${ONNX_MLIR_LIB_DIR} ${ARGN}) endif() endfunction(whole_archive_link_onnx_mlir) @@ -281,7 +347,7 @@ 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_PROJ_BUILD}/bin/mlir-tblgen) + PROPERTY IMPORTED_LOCATION ${LLVM_PROJ_BIN}/mlir-tblgen) set(MLIR_TABLEGEN_EXE mlir-tblgen) # Add a dialect used by ONNX MLIR and copy the generated operation diff --git a/README.md b/README.md index bc4f421..7e12e65 100644 --- a/README.md +++ b/README.md @@ -11,15 +11,20 @@ libprotoc >= 3.11.0 cmake >= 3.15.4 ``` -## Installation +## Installation on UNIX +#### MLIR Firstly, install MLIR (as a part of LLVM-Project): -[same-as-file]: <> (utils/install-mlir.sh) +[same-as-file]: <> (utils/clone-mlir.sh) ``` bash git clone https://github.com/llvm/llvm-project.git # Check out a specific branch that is known to work with ONNX MLIR. cd llvm-project && git checkout 07e462526d0cbae40b320e1a4307ce11e197fb0a && cd .. +``` + +[same-as-file]: <> (utils/build-mlir.sh) +``` bash mkdir llvm-project/build cd llvm-project/build cmake -G Ninja ../llvm \ @@ -34,6 +39,7 @@ cmake --build . --target -- ${MAKEFLAGS} cmake --build . --target check-mlir ``` +#### ONNX-MLIR (this project) Two environment variables need to be set: - LLVM_PROJ_SRC should point to the llvm-project src directory (e.g., llvm-project/). - LLVM_PROJ_BUILD should point to the llvm-project build directory (e.g., llvm-project/build). @@ -41,7 +47,7 @@ Two environment variables need to be set: To build ONNX-MLIR, use the following command: [same-as-file]: <> ({"ref": "utils/install-onnx-mlir.sh", "skip-doc": 2}) -``` +```bash git clone --recursive https://github.com/onnx/onnx-mlir.git # Export environment variables pointing to LLVM-Projects. @@ -59,7 +65,107 @@ cmake --build . --target check-onnx-lit After the above commands succeed, an `onnx-mlir` executable should appear in the `bin` directory. -## Using ONNX MLIR +## Installation on Windows +Building onnx-mlir on Windows requires building some additional prerequisites that are not available by default. + +Note that the instructions in this file assume you are using [Visual Studio 2019 Community Edition](https://visualstudio.microsoft.com/downloads/). It is recommended that you have the **Desktop development with C++** and **Linux development with C++** workloads installed. This ensures you have all toolchains and libraries needed to compile this project and its dependencies on Windows. + +Run all the commands from a shell started from **"Developer Command Prompt for VS 2019"**. + +#### Protobuf +Build protobuf as a static library. + +```shell +set root_dir=%cd% +git clone --recurse-submodules https://github.com/protocolbuffers/protobuf.git +cd protobuf +cd cmake +cmake -G "Visual Studio 16 2019" -A x64 -T host=x64 -DCMAKE_BUILD_TYPE=Release -Dprotobuf_MSVC_STATIC_RUNTIME=OFF -Dprotobuf_BUILD_TESTS=OFF -Dprotobuf_BUILD_EXAMPLES=OFF -Dprotobuf_WITH_ZLIB=OFF -DCMAKE_INSTALL_PREFIX="%root_dir%\protobuf\install" +call msbuild protobuf.sln /m /p:Configuration=Release +call msbuild INSTALL.vcxproj /p:Configuration=Release +``` + +Before running CMake for onnx-mlir, ensure that the bin directory to this protobuf is before any others in your PATH: +```shell +set PATH=%root_dir%\protobuf\install\bin;%PATH% +``` + +#### PDCurses +Build a local version of the curses library, used by various commandline tools in onnx-mlir. These instructions assume you use [Public Domain Curses](https://pdcurses.org/). + +Run this from a Visual Studio developer command prompt since you will need access to the appropriate version of Visual Studio's nmake tool. + +```shell +set root_dir=%cd% +git clone https://github.com/wmcbrine/PDCurses.git +set PDCURSES_SRCDIR=%root_dir%/PDCurses +cd PDCurses +call nmake -f wincon/Makefile.vc +``` + +#### MLIR +Install MLIR (as a part of LLVM-Project): + +[same-as-file]: <> (utils/clone-mlir.sh) +```shell +git clone https://github.com/llvm/llvm-project.git +# Check out a specific branch that is known to work with ONNX MLIR. +cd llvm-project && git checkout 07e462526d0cbae40b320e1a4307ce11e197fb0a && cd .. +``` + +[same-as-file]: <> (utils/build-mlir.cmd) +```shell +set root_dir=%cd% +md llvm-project\build +cd llvm-project\build +call cmake -G "Visual Studio 16 2019" -A x64 -T host=x64 ..\llvm ^ + -DCMAKE_INSTALL_PREFIX="%root_dir%\llvm-project\build\install" ^ + -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 ^ + -DLLVM_ENABLE_ZLIB=OFF + +call cmake --build . --config Release --target -- /m +call cmake --build . --config Release --target install +call cmake --build . --config Release --target check-mlir +``` + +#### ONNX-MLIR (this project) +The following environment variables need to be set before building onnx-mlir: +- CURSES_LIB_PATH: Path to curses library (e.g. c:/repos/PDCurses) +- LLVM_PROJ_BUILD: Path to the build directory for LLVM (e.g. c:/repos/llvm-project/build) +- LLVM_PROJ_SRC: Path to the source directory for LLVM (e.g. c:/repos/llvm-project) + +This project uses lit ([LLVM's Integrated Tester](http://llvm.org/docs/CommandGuide/lit.html)) for unit tests. When running CMake, we will also specify the path to the lit tool from LLVM using the LLVM_EXTERNAL_LIT define. + +To build ONNX MLIR, use the following command: + +[same-as-file]: <> (utils/install-onnx-mlir.cmd) +```shell +git clone --recursive https://github.com/onnx/onnx-mlir.git + +REM Export environment variables pointing to LLVM-Projects. +set root_dir=%cd% +set CURSES_LIB_PATH=%root_dir%/PDCurses +set LLVM_PROJ_BUILD=%root_dir%/llvm-project/build +set LLVM_PROJ_SRC=%root_dir%/llvm-project + +md onnx-mlir\build +cd onnx-mlir\build +call cmake -G "Visual Studio 16 2019" -A x64 -T host=x64 -DLLVM_EXTERNAL_LIT="%root_dir%\llvm-project\build\Release\bin\llvm-lit.py" -DCMAKE_BUILD_TYPE=Release .. +call cmake --build . --config Release --target onnx-mlir -- /m + +REM Run FileCheck tests +set LIT_OPTS=-v +call cmake --build . --config Release --target check-onnx-lit +``` + +After the above commands succeed, an `onnx-mlir` executable should appear in the `bin` directory. + +## Using ONNX-MLIR The usage of `onnx-mlir` is as such: ``` diff --git a/docs/README.md b/docs/README.md index bc4f421..7e12e65 100644 --- a/docs/README.md +++ b/docs/README.md @@ -11,15 +11,20 @@ libprotoc >= 3.11.0 cmake >= 3.15.4 ``` -## Installation +## Installation on UNIX +#### MLIR Firstly, install MLIR (as a part of LLVM-Project): -[same-as-file]: <> (utils/install-mlir.sh) +[same-as-file]: <> (utils/clone-mlir.sh) ``` bash git clone https://github.com/llvm/llvm-project.git # Check out a specific branch that is known to work with ONNX MLIR. cd llvm-project && git checkout 07e462526d0cbae40b320e1a4307ce11e197fb0a && cd .. +``` + +[same-as-file]: <> (utils/build-mlir.sh) +``` bash mkdir llvm-project/build cd llvm-project/build cmake -G Ninja ../llvm \ @@ -34,6 +39,7 @@ cmake --build . --target -- ${MAKEFLAGS} cmake --build . --target check-mlir ``` +#### ONNX-MLIR (this project) Two environment variables need to be set: - LLVM_PROJ_SRC should point to the llvm-project src directory (e.g., llvm-project/). - LLVM_PROJ_BUILD should point to the llvm-project build directory (e.g., llvm-project/build). @@ -41,7 +47,7 @@ Two environment variables need to be set: To build ONNX-MLIR, use the following command: [same-as-file]: <> ({"ref": "utils/install-onnx-mlir.sh", "skip-doc": 2}) -``` +```bash git clone --recursive https://github.com/onnx/onnx-mlir.git # Export environment variables pointing to LLVM-Projects. @@ -59,7 +65,107 @@ cmake --build . --target check-onnx-lit After the above commands succeed, an `onnx-mlir` executable should appear in the `bin` directory. -## Using ONNX MLIR +## Installation on Windows +Building onnx-mlir on Windows requires building some additional prerequisites that are not available by default. + +Note that the instructions in this file assume you are using [Visual Studio 2019 Community Edition](https://visualstudio.microsoft.com/downloads/). It is recommended that you have the **Desktop development with C++** and **Linux development with C++** workloads installed. This ensures you have all toolchains and libraries needed to compile this project and its dependencies on Windows. + +Run all the commands from a shell started from **"Developer Command Prompt for VS 2019"**. + +#### Protobuf +Build protobuf as a static library. + +```shell +set root_dir=%cd% +git clone --recurse-submodules https://github.com/protocolbuffers/protobuf.git +cd protobuf +cd cmake +cmake -G "Visual Studio 16 2019" -A x64 -T host=x64 -DCMAKE_BUILD_TYPE=Release -Dprotobuf_MSVC_STATIC_RUNTIME=OFF -Dprotobuf_BUILD_TESTS=OFF -Dprotobuf_BUILD_EXAMPLES=OFF -Dprotobuf_WITH_ZLIB=OFF -DCMAKE_INSTALL_PREFIX="%root_dir%\protobuf\install" +call msbuild protobuf.sln /m /p:Configuration=Release +call msbuild INSTALL.vcxproj /p:Configuration=Release +``` + +Before running CMake for onnx-mlir, ensure that the bin directory to this protobuf is before any others in your PATH: +```shell +set PATH=%root_dir%\protobuf\install\bin;%PATH% +``` + +#### PDCurses +Build a local version of the curses library, used by various commandline tools in onnx-mlir. These instructions assume you use [Public Domain Curses](https://pdcurses.org/). + +Run this from a Visual Studio developer command prompt since you will need access to the appropriate version of Visual Studio's nmake tool. + +```shell +set root_dir=%cd% +git clone https://github.com/wmcbrine/PDCurses.git +set PDCURSES_SRCDIR=%root_dir%/PDCurses +cd PDCurses +call nmake -f wincon/Makefile.vc +``` + +#### MLIR +Install MLIR (as a part of LLVM-Project): + +[same-as-file]: <> (utils/clone-mlir.sh) +```shell +git clone https://github.com/llvm/llvm-project.git +# Check out a specific branch that is known to work with ONNX MLIR. +cd llvm-project && git checkout 07e462526d0cbae40b320e1a4307ce11e197fb0a && cd .. +``` + +[same-as-file]: <> (utils/build-mlir.cmd) +```shell +set root_dir=%cd% +md llvm-project\build +cd llvm-project\build +call cmake -G "Visual Studio 16 2019" -A x64 -T host=x64 ..\llvm ^ + -DCMAKE_INSTALL_PREFIX="%root_dir%\llvm-project\build\install" ^ + -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 ^ + -DLLVM_ENABLE_ZLIB=OFF + +call cmake --build . --config Release --target -- /m +call cmake --build . --config Release --target install +call cmake --build . --config Release --target check-mlir +``` + +#### ONNX-MLIR (this project) +The following environment variables need to be set before building onnx-mlir: +- CURSES_LIB_PATH: Path to curses library (e.g. c:/repos/PDCurses) +- LLVM_PROJ_BUILD: Path to the build directory for LLVM (e.g. c:/repos/llvm-project/build) +- LLVM_PROJ_SRC: Path to the source directory for LLVM (e.g. c:/repos/llvm-project) + +This project uses lit ([LLVM's Integrated Tester](http://llvm.org/docs/CommandGuide/lit.html)) for unit tests. When running CMake, we will also specify the path to the lit tool from LLVM using the LLVM_EXTERNAL_LIT define. + +To build ONNX MLIR, use the following command: + +[same-as-file]: <> (utils/install-onnx-mlir.cmd) +```shell +git clone --recursive https://github.com/onnx/onnx-mlir.git + +REM Export environment variables pointing to LLVM-Projects. +set root_dir=%cd% +set CURSES_LIB_PATH=%root_dir%/PDCurses +set LLVM_PROJ_BUILD=%root_dir%/llvm-project/build +set LLVM_PROJ_SRC=%root_dir%/llvm-project + +md onnx-mlir\build +cd onnx-mlir\build +call cmake -G "Visual Studio 16 2019" -A x64 -T host=x64 -DLLVM_EXTERNAL_LIT="%root_dir%\llvm-project\build\Release\bin\llvm-lit.py" -DCMAKE_BUILD_TYPE=Release .. +call cmake --build . --config Release --target onnx-mlir -- /m + +REM Run FileCheck tests +set LIT_OPTS=-v +call cmake --build . --config Release --target check-onnx-lit +``` + +After the above commands succeed, an `onnx-mlir` executable should appear in the `bin` directory. + +## Using ONNX-MLIR The usage of `onnx-mlir` is as such: ``` diff --git a/src/Builder/FrontendDialectTransformer.cpp b/src/Builder/FrontendDialectTransformer.cpp index cb79974..f094761 100644 --- a/src/Builder/FrontendDialectTransformer.cpp +++ b/src/Builder/FrontendDialectTransformer.cpp @@ -80,6 +80,7 @@ private: case onnx::TensorProto_DataType::TensorProto_DataType_COMPLEX64: case onnx::TensorProto_DataType::TensorProto_DataType_COMPLEX128: case onnx::TensorProto_DataType::TensorProto_DataType_UNDEFINED: + default: assert(false && "Unsupported data type encountered."); return nullptr; } @@ -209,6 +210,7 @@ private: assert(false && "datatype for attribute is not implemented"); break; } + llvm_unreachable("Failed to convert attribute proto to name/value pair"); } std::vector diff --git a/src/Conversion/ONNXToKrnl/Math/Elementwise.cpp b/src/Conversion/ONNXToKrnl/Math/Elementwise.cpp index be9d031..f8adb20 100644 --- a/src/Conversion/ONNXToKrnl/Math/Elementwise.cpp +++ b/src/Conversion/ONNXToKrnl/Math/Elementwise.cpp @@ -412,6 +412,7 @@ Value emitScalarOpFor(ConversionPatternRewriter &rewriter, return result; } else { emitError(loc, "unsupported element type"); + return {}; } } @@ -469,6 +470,7 @@ Value emitScalarOpFor(ConversionPatternRewriter &rewriter, loc, lessThanZero, negativeOperand, operand); } else { emitError(loc, "unsupported element type"); + return {}; } } diff --git a/src/Conversion/ONNXToKrnl/ONNXToKrnlCommon.cpp b/src/Conversion/ONNXToKrnl/ONNXToKrnlCommon.cpp index d79490b..5a7ae4e 100644 --- a/src/Conversion/ONNXToKrnl/ONNXToKrnlCommon.cpp +++ b/src/Conversion/ONNXToKrnl/ONNXToKrnlCommon.cpp @@ -273,7 +273,7 @@ getBroadcastedDimInfo(Location loc, ConversionPatternRewriter &rewriter, auto shape = operands[i].getType().cast().getShape(); int size = shape.size(); for (int j = 0; j < shape.size(); ++j) { - if (shape[j] < 0 and sharedDimCount[rank - size + j] > 1) { + if (shape[j] < 0 && sharedDimCount[rank - size + j] > 1) { auto dim = rewriter.create(loc, operands[i], j).getResult(); auto one = rewriter.create(loc, 1); auto isBroadcasted = diff --git a/src/Dialect/Krnl/KrnlOps.cpp b/src/Dialect/Krnl/KrnlOps.cpp index fbe0c34..6142701 100644 --- a/src/Dialect/Krnl/KrnlOps.cpp +++ b/src/Dialect/Krnl/KrnlOps.cpp @@ -75,6 +75,7 @@ ParseResult parseKrnlDefineLoopsOp( numLoops.getValue().getSExtValue(), LoopType::get(builder.getContext())); if (parser.addTypesToList(loopTypes, result.types)) return failure(); + return success(); } //===----------------------------------------------------------------------===// @@ -288,6 +289,7 @@ ParseResult parseKrnlIterateOp(OpAsmParser &parser, OperationState &result) { builder.getConstantAffineMap(integerAttr.getValue().getSExtValue()); boundMaps.emplace_back(AffineMapAttr::get(map)); } + return success(); }; while (failed(parser.parseOptionalRParen())) { @@ -335,6 +337,7 @@ ParseResult parseKrnlIterateOp(OpAsmParser &parser, OperationState &result) { static LogicalResult verify(KrnlIterateOp op) { // TODO: Verify number of induction variable bounds matches the number of // input loops. + return success(); } //===----------------------------------------------------------------------===// diff --git a/src/Dialect/ONNX/ONNXOps.cpp b/src/Dialect/ONNX/ONNXOps.cpp index 5da613f..b407c85 100644 --- a/src/Dialect/ONNX/ONNXOps.cpp +++ b/src/Dialect/ONNX/ONNXOps.cpp @@ -852,7 +852,7 @@ bool ONNXGemmOp::inferShapes() { N = (transB() == 0) ? rhsTy.getShape()[1] : rhsTy.getShape()[0]; K_B = (transB() == 0) ? rhsTy.getShape()[0] : rhsTy.getShape()[1]; - if ((K_A != -1) and (K_B != -1) and (K_A != K_B)) { + if ((K_A != -1) && (K_B != -1) && (K_A != K_B)) { emitError("Tensor shapes mismatched"); } diff --git a/test/mlir/lit.site.cfg.py.in b/test/mlir/lit.site.cfg.py.in index d81581c..28df6b2 100644 --- a/test/mlir/lit.site.cfg.py.in +++ b/test/mlir/lit.site.cfg.py.in @@ -2,18 +2,19 @@ import lit.llvm if '@BUILD_SHARED_LIBS@' == 'ON': - config.environment['LD_PRELOAD'] = "@ONNX_MLIR_LD_PRELOAD_onnx-mlir-opt@" + config.environment['LD_PRELOAD'] = r"@ONNX_MLIR_LD_PRELOAD_onnx-mlir-opt@" -config.llvm_tools_dir = "@MLIR_TOOLS_DIR@" -config.mlir_obj_root = "@LLVM_PROJ_BUILD@" -config.mlir_tools_dir = "@MLIR_TOOLS_DIR@" +config.llvm_tools_dir = r"@MLIR_TOOLS_DIR@" +config.mlir_obj_root = r"@LLVM_PROJ_BUILD@" +config.mlir_tools_dir = r"@MLIR_TOOLS_DIR@" config.suffixes = ['.mlir'] +config.lit_tools_dir = config.llvm_tools_dir -config.onnx_mlir_tools_dir = "@ONNX_MLIR_TOOLS_DIR@" -config.onnx_mlir_test_src_dir = "@ONNX_MLIR_LIT_TEST_SRC_DIR@" -config.onnx_mlir_test_build_dir = "@ONNX_MLIR_LIT_TEST_BUILD_DIR@" +config.onnx_mlir_tools_dir = r"@ONNX_MLIR_TOOLS_DIR@" +config.onnx_mlir_test_src_dir = r"@ONNX_MLIR_LIT_TEST_SRC_DIR@" +config.onnx_mlir_test_build_dir = r"@ONNX_MLIR_LIT_TEST_BUILD_DIR@" lit.llvm.initialize(lit_config, config) # Let the main config do the real work. -lit_config.load_config(config, "@ONNX_MLIR_LIT_TEST_SRC_DIR@/lit.cfg.py") +lit_config.load_config(config, r"@ONNX_MLIR_LIT_TEST_SRC_DIR@/lit.cfg.py") diff --git a/utils/build-mlir.cmd b/utils/build-mlir.cmd new file mode 100644 index 0000000..49edbfa --- /dev/null +++ b/utils/build-mlir.cmd @@ -0,0 +1,16 @@ +set root_dir=%cd% +md llvm-project\build +cd llvm-project\build +call cmake -G "Visual Studio 16 2019" -A x64 -T host=x64 ..\llvm ^ + -DCMAKE_INSTALL_PREFIX="%root_dir%\llvm-project\build\install" ^ + -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 ^ + -DLLVM_ENABLE_ZLIB=OFF + +call cmake --build . --config Release --target -- /m +call cmake --build . --config Release --target install +call cmake --build . --config Release --target check-mlir diff --git a/utils/build-mlir.sh b/utils/build-mlir.sh new file mode 100644 index 0000000..6de6ea0 --- /dev/null +++ b/utils/build-mlir.sh @@ -0,0 +1,12 @@ +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 -- ${MAKEFLAGS} +cmake --build . --target check-mlir \ No newline at end of file diff --git a/utils/clone-mlir.sh b/utils/clone-mlir.sh new file mode 100644 index 0000000..b47c69c --- /dev/null +++ b/utils/clone-mlir.sh @@ -0,0 +1,3 @@ +git clone https://github.com/llvm/llvm-project.git +# Check out a specific branch that is known to work with ONNX MLIR. +cd llvm-project && git checkout 07e462526d0cbae40b320e1a4307ce11e197fb0a && cd .. diff --git a/utils/install-mlir.cmd b/utils/install-mlir.cmd new file mode 100644 index 0000000..b13568d --- /dev/null +++ b/utils/install-mlir.cmd @@ -0,0 +1,8 @@ +REM Clone the repo to the appropriate commit id +copy %~dp0\clone-mlir.sh %~dp0\clone-mlir.cmd +call %~dp0\clone-mlir.cmd +del %~dp0\clone-mlir.cmd + +REM Build the repo +call %~dp0\build-mlir.cmd + diff --git a/utils/install-mlir.sh b/utils/install-mlir.sh index 12ad8c6..670200b 100755 --- a/utils/install-mlir.sh +++ b/utils/install-mlir.sh @@ -1,15 +1,2 @@ -git clone https://github.com/llvm/llvm-project.git -# Check out a specific branch that is known to work with ONNX MLIR. -cd llvm-project && git checkout 07e462526d0cbae40b320e1a4307ce11e197fb0a && cd .. -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 -- ${MAKEFLAGS} -cmake --build . --target check-mlir \ No newline at end of file +. $(dirname "${BASH_SOURCE[0]}")/clone-mlir.sh +. $(dirname "${BASH_SOURCE[0]}")/build-mlir.sh diff --git a/utils/install-onnx-mlir.cmd b/utils/install-onnx-mlir.cmd new file mode 100644 index 0000000..29f73d4 --- /dev/null +++ b/utils/install-onnx-mlir.cmd @@ -0,0 +1,16 @@ +git clone --recursive https://github.com/onnx/onnx-mlir.git + +REM Export environment variables pointing to LLVM-Projects. +set root_dir=%cd% +set CURSES_LIB_PATH=%root_dir%/PDCurses +set LLVM_PROJ_BUILD=%root_dir%/llvm-project/build +set LLVM_PROJ_SRC=%root_dir%/llvm-project + +md onnx-mlir\build +cd onnx-mlir\build +call cmake -G "Visual Studio 16 2019" -A x64 -T host=x64 -DLLVM_EXTERNAL_LIT="%root_dir%\llvm-project\build\Release\bin\llvm-lit.py" -DCMAKE_BUILD_TYPE=Release .. +call cmake --build . --config Release --target onnx-mlir -- /m + +REM Run FileCheck tests +set LIT_OPTS=-v +call cmake --build . --config Release --target check-onnx-lit