diff --git a/MLIR.cmake b/MLIR.cmake index 0350e4b..1a0cf76 100644 --- a/MLIR.cmake +++ b/MLIR.cmake @@ -186,6 +186,16 @@ find_mlir_lib(LLVMDemangle) find_mlir_lib(LLVMFrontendOpenMP) set(MLIRLibs + ${MLIRAffineToStandard} + ${MLIRAffineOps} + ${MLIRLLVMIR} + ${MLIRStandardOps} + ${MLIRStandardToLLVM} + ${MLIRTransforms} + ${MLIRSCFToStandard} + ${MLIRVector} + ${MLIRSCF} + ${MLIRIR} ${MLIRLLVMIR} ${MLIROptLib} ${MLIRParser} @@ -242,95 +252,6 @@ set(MLIRLibs ${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. -set(MLIRWholeArchiveLibs - MLIRAffineToStandard - MLIRAffineOps - MLIRLLVMIR - MLIRStandardOps - MLIRStandardToLLVM - MLIRTransforms - MLIRSCFToStandard - MLIRVector - MLIRSCF - MLIRIR) - -# ONNX MLIR libraries that must be linked with --whole-archive for static build or -# must be specified on LD_PRELOAD for shared build. -set(ONNXMLIRWholeArchiveLibs - OMKrnlToAffine - OMKrnlToLLVM - OMONNXToKrnl - OMONNXRewrite - OMShapeInference - OMShapeInferenceOpInterface - OMAttributePromotion - OMPromotableConstOperandsOpInterface - OMElideConstants - OMElideKrnlGlobalConstants - OMPackKrnlGlobalConstants - OMEnableMemoryPool) - -# Function to construct linkage option for the static libraries that must be -# linked with --whole-archive (or equivalent). -function(whole_archive_link target lib_dir) - get_property(link_flags TARGET ${target} PROPERTY LINK_FLAGS) - if("${CMAKE_SYSTEM_NAME}" STREQUAL "Darwin") - set(link_flags "${link_flags} -L${lib_dir} ") - foreach(LIB ${ARGN}) - string(CONCAT link_flags ${link_flags} - "-Wl,-force_load, ${lib_dir}/lib${LIB}.a ") - endforeach(LIB) - elseif(MSVC) - foreach(LIB ${ARGN}) - string(CONCAT link_flags ${link_flags} "/WHOLEARCHIVE:${lib_dir}/${LIB} ") - endforeach(LIB) - else() - set(link_flags "${link_flags} -L${lib_dir} -Wl,--whole-archive,") - foreach(LIB ${ARGN}) - string(CONCAT link_flags ${link_flags} "-l${LIB},") - endforeach(LIB) - string(CONCAT link_flags ${link_flags} "--no-whole-archive") - endif() - set_target_properties(${target} PROPERTIES LINK_FLAGS ${link_flags}) -endfunction(whole_archive_link) - -# Function to construct LD_PRELOAD value for the shared libraries whose -# static counterpart need --whole-archive linkage option. -function(ld_preload_libs target lib_dir) - foreach(lib ${ARGN}) - if("${${lib}}" STREQUAL "") - set(ONNX_MLIR_LD_PRELOAD_${target} - "${ONNX_MLIR_LD_PRELOAD_${target}}:${lib_dir}/lib${lib}.so" - CACHE STRING "" FORCE) - else() - set(ONNX_MLIR_LD_PRELOAD_${target} - "${ONNX_MLIR_LD_PRELOAD_${target}}:${${lib}}" - CACHE STRING "" FORCE) - endif() - endforeach(lib) -endfunction(ld_preload_libs) - -function(whole_archive_link_mlir target) - if(BUILD_SHARED_LIBS) - ld_preload_libs(${target} ${LLVM_PROJECT_LIB} ${ARGN}) - else() - whole_archive_link(${target} ${LLVM_PROJECT_LIB} ${ARGN}) - endif() -endfunction(whole_archive_link_mlir) - -function(whole_archive_link_onnx_mlir target) - foreach(lib_target ${ARGN}) - add_dependencies(${target} ${lib_target}) - endforeach(lib_target) - if(BUILD_SHARED_LIBS) - ld_preload_libs(${target} ${ONNX_MLIR_LIB_DIR} ${ARGN}) - else() - whole_archive_link(${target} ${ONNX_MLIR_LIB_DIR} ${ARGN}) - endif() -endfunction(whole_archive_link_onnx_mlir) - set(LLVM_CMAKE_DIR "${LLVM_PROJ_BUILD}/lib/cmake/llvm" CACHE PATH "Path to LLVM cmake modules") diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index e6ef433..03292ec 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -2,14 +2,40 @@ add_subdirectory(Interface) add_subdirectory(Dialect) add_subdirectory(Conversion) add_subdirectory(Transform) -add_subdirectory(Tool) add_subdirectory(Builder) add_subdirectory(Runtime) +# All ONNX-MLIR libraries. +set(OMLibs + OMBuilder + OMKrnlOps + OMONNXOps + OMKrnlToAffine + OMKrnlToLLVM + OMONNXToKrnl + OMONNXRewrite + OMShapeInference + OMShapeInferenceOpInterface + OMAttributePromotion + OMPromotableConstOperandsOpInterface + OMResultTypeInferenceOpInterface + OMElideConstants + OMElideKrnlGlobalConstants + OMPackKrnlGlobalConstants + OMEnableMemoryPool) +set(OMLibs ${OMLibs} PARENT_SCOPE) + +message(SATUS "OMLibs" ${OMLibs}) +add_subdirectory(Tool) + add_library(MainUtils MainUtils.hpp MainUtils.cpp) -target_link_libraries(MainUtils onnx) +target_link_libraries(MainUtils + ${OMLibs} + ${MLIRLibs} + ${CMAKE_DL_LIBS} + onnx) target_include_directories(MainUtils PRIVATE ${ONNX_MLIR_SRC_ROOT}) target_include_directories(MainUtils PRIVATE ${CMAKE_BINARY_DIR}) @@ -29,7 +55,6 @@ configure_file(${CMAKE_CURRENT_SOURCE_DIR}/ExternalUtil.hpp.in ${CMAKE_CURRENT_BINARY_DIR}/ExternalUtil.hpp) set(ONNX_MLIR_LD_PRELOAD_onnx-mlir "" CACHE STRING "" FORCE) -whole_archive_link_mlir(onnx-mlir ${MLIRWholeArchiveLibs}) if(BUILD_SHARED_LIBS) message(STATUS "To run dynamically linked onnx-mlir, you must specify:") message(STATUS "LD_PRELOAD=${ONNX_MLIR_LD_PRELOAD_onnx-mlir}") @@ -43,25 +68,6 @@ endif() # So it's better not to use target_link_libraries for the add_subdirectory # targets, or only use it for libraries that have no further dependencies # (except system libraries such as libc). -target_link_libraries(MainUtils - OMBuilder - OMKrnlOps - OMONNXOps - OMShapeInference - OMShapeInferenceOpInterface - OMAttributePromotion - OMPromotableConstOperandsOpInterface - OMResultTypeInferenceOpInterface - OMElideConstants - OMElideKrnlGlobalConstants - OMPackKrnlGlobalConstants - OMEnableMemoryPool - OMKrnlToAffine - OMKrnlToLLVM - OMONNXToKrnl - OMONNXRewrite - ${MLIRLibs} - ${CMAKE_DL_LIBS}) add_dependencies(onnx-mlir OMKrnlOpsInc OMONNXOpsInc) if (INCLUDE_ONNX_ML) diff --git a/src/Conversion/ONNXToKrnl/ConvertONNXToKrnl.cpp b/src/Conversion/ONNXToKrnl/ConvertONNXToKrnl.cpp index fac281a..b52a3dc 100644 --- a/src/Conversion/ONNXToKrnl/ConvertONNXToKrnl.cpp +++ b/src/Conversion/ONNXToKrnl/ConvertONNXToKrnl.cpp @@ -118,7 +118,4 @@ void FrontendToKrnlLoweringPass::runOnOperation() { std::unique_ptr mlir::createLowerToKrnlPass() { return std::make_unique(); -} - -static PassRegistration pass( - "lower-frontend", "Lower frontend ops to Krnl dialect."); +} \ No newline at end of file diff --git a/src/InitOMPasses.hpp b/src/InitOMPasses.hpp new file mode 100644 index 0000000..88318d2 --- /dev/null +++ b/src/InitOMPasses.hpp @@ -0,0 +1,73 @@ +#include "mlir/Pass/Pass.h" + +#include "src/Pass/Passes.hpp" + +namespace onnx_mlir { + +void initOMPasses() { + // All passes implemented within onnx-mlir should register within this + // function to make themselves available as a command-line option. + mlir::registerPass("decompose-onnx", + "Decompose ONNX operations into composition of other ONNX operations.", + []() -> std::unique_ptr { + return mlir::createDecomposeONNXToONNXPass(); + }); + + mlir::registerPass("shape-inference", + "Shape inference for frontend dialects.", + []() -> std::unique_ptr { + return mlir::createShapeInferencePass(); + }); + + mlir::registerPass("constprop-onnx", + "ConstProp ONNX operations into composition of other ONNX operations.", + []() -> std::unique_ptr { + return mlir::createConstPropONNXToONNXPass(); + }); + + mlir::registerPass("attribute-promotion", + "Promote constant operands to attributes.", + []() -> std::unique_ptr { + return mlir::createAttributePromotionPass(); + }); + + mlir::registerPass("elide-constants", "Elide values of constant operations.", + []() -> std::unique_ptr { + return mlir::createElideConstantValuePass(); + }); + + mlir::registerPass("enable-memory-pool", + "Enable a memory pool for allocating internal MemRefs.", + []() -> std::unique_ptr { + return mlir::createKrnlEnableMemoryPoolPass(); + }); + + mlir::registerPass( + "lower-krnl", "Lower Krnl dialect.", []() -> std::unique_ptr { + return mlir::createLowerKrnlPass(); + }); + + mlir::registerPass("lower-frontend", "Lower frontend ops to Krnl dialect.", + []() -> std::unique_ptr { + return mlir::createLowerToKrnlPass(); + }); + + mlir::registerPass("elide-krnl-constants", + "Elide the constant values of the Global Krnl operations.", + []() -> std::unique_ptr { + return mlir::createElideConstGlobalValuePass(); + }); + + mlir::registerPass("lower-all-llvm", + "Lower the Krnl Affine and Std dialects to LLVM.", + []() -> std::unique_ptr { + return mlir::createKrnlLowerToLLVMPass(); + }); + + mlir::registerPass("pack-krnl-constants", + "Elide the constant values of the Global Krnl operations.", + []() -> std::unique_ptr { + return mlir::createPackKrnlGlobalConstantsPass(); + }); +} +} // namespace onnx_mlir \ No newline at end of file diff --git a/src/Tool/ONNXMLIROpt/CMakeLists.txt b/src/Tool/ONNXMLIROpt/CMakeLists.txt index 8cdafef..e1d0741 100644 --- a/src/Tool/ONNXMLIROpt/CMakeLists.txt +++ b/src/Tool/ONNXMLIROpt/CMakeLists.txt @@ -5,19 +5,15 @@ target_include_directories(onnx-mlir-opt PRIVATE ${ONNX_MLIR_SRC_ROOT}) target_include_directories(onnx-mlir-opt PRIVATE ${ONNX_MLIR_BIN_ROOT}) set(ONNX_MLIR_LD_PRELOAD_onnx-mlir-opt "" CACHE STRING "" FORCE) -whole_archive_link_onnx_mlir(onnx-mlir-opt ${ONNXMLIRWholeArchiveLibs}) -whole_archive_link_mlir(onnx-mlir-opt ${MLIRWholeArchiveLibs}) if(BUILD_SHARED_LIBS) message(STATUS "To run dynamically linked onnx-mlir-opt, you must specify:") message(STATUS "LD_PRELOAD=${ONNX_MLIR_LD_PRELOAD_onnx-mlir-opt}") endif() target_link_libraries(onnx-mlir-opt - OMBuilder - OMKrnlOps - OMONNXOps - OMONNXRewrite - ${MLIRLibs}) + ${OMLibs} + ${MLIRLibs} + onnx) if (INCLUDE_ONNX_ML) target_link_libraries(onnx-mlir-opt OMMLONNXOps) diff --git a/src/Tool/ONNXMLIROpt/ONNXMLIROpt.cpp b/src/Tool/ONNXMLIROpt/ONNXMLIROpt.cpp index 7c121f2..bf8888f 100644 --- a/src/Tool/ONNXMLIROpt/ONNXMLIROpt.cpp +++ b/src/Tool/ONNXMLIROpt/ONNXMLIROpt.cpp @@ -22,6 +22,7 @@ #include "src/Dialect/Krnl/KrnlOps.hpp" #include "src/Dialect/MLONNX/MLONNXOps.hpp" #include "src/Dialect/ONNX/ONNXOps.hpp" +#include "src/InitOMPasses.hpp" #include "src/Pass/Passes.hpp" using namespace onnx_mlir; @@ -70,6 +71,7 @@ int main(int argc, char **argv) { mlir::registerDialect(); mlir::registerDialect(); mlir::registerDialect(); + initOMPasses(); mlir::registerAsmPrinterCLOptions(); mlir::registerMLIRContextCLOptions(); diff --git a/src/Transform/ElideKrnlGlobalConstants.cpp b/src/Transform/ElideKrnlGlobalConstants.cpp index 3630809..cd4703e 100644 --- a/src/Transform/ElideKrnlGlobalConstants.cpp +++ b/src/Transform/ElideKrnlGlobalConstants.cpp @@ -68,7 +68,4 @@ public: std::unique_ptr mlir::createElideConstGlobalValuePass() { return std::make_unique(); -} - -static PassRegistration pass("elide-krnl-constants", - "Elide the constant values of the Global Krnl operations."); +} \ No newline at end of file diff --git a/src/Transform/EnableMemoryPool.cpp b/src/Transform/EnableMemoryPool.cpp index 3d5bf75..94a5446 100644 --- a/src/Transform/EnableMemoryPool.cpp +++ b/src/Transform/EnableMemoryPool.cpp @@ -154,6 +154,3 @@ public: std::unique_ptr mlir::createKrnlEnableMemoryPoolPass() { return std::make_unique(); } - -static PassRegistration pass("enable-memory-pool", - "Enable a memory pool for allocating internal MemRefs."); diff --git a/src/Transform/LowerKrnl.cpp b/src/Transform/LowerKrnl.cpp index 79370d0..044b471 100644 --- a/src/Transform/LowerKrnl.cpp +++ b/src/Transform/LowerKrnl.cpp @@ -181,6 +181,3 @@ void KrnlToAffineLoweringPass::runOnFunction() { std::unique_ptr mlir::createLowerKrnlPass() { return std::make_unique(); } - -static PassRegistration pass( - "lower-krnl", "Lower Krnl dialect."); diff --git a/src/Transform/LowerToLLVM.cpp b/src/Transform/LowerToLLVM.cpp index 1b4f6cb..ccd012c 100644 --- a/src/Transform/LowerToLLVM.cpp +++ b/src/Transform/LowerToLLVM.cpp @@ -865,6 +865,3 @@ void KrnlToLLVMLoweringPass::runOnOperation() { std::unique_ptr mlir::createKrnlLowerToLLVMPass() { return std::make_unique(); } - -static PassRegistration pass( - "lower-all-llvm", "Lower the Krnl Affine and Std dialects to LLVM."); diff --git a/src/Transform/ONNX/AttributePromotion.cpp b/src/Transform/ONNX/AttributePromotion.cpp index 461c911..123e8c1 100644 --- a/src/Transform/ONNX/AttributePromotion.cpp +++ b/src/Transform/ONNX/AttributePromotion.cpp @@ -101,6 +101,3 @@ public: std::unique_ptr mlir::createAttributePromotionPass() { return std::make_unique(); } - -static PassRegistration pass( - "attribute-promotion", "Promote constant operands to attributes."); diff --git a/src/Transform/ONNX/ConstProp.cpp b/src/Transform/ONNX/ConstProp.cpp index 599c9db..a48a41b 100644 --- a/src/Transform/ONNX/ConstProp.cpp +++ b/src/Transform/ONNX/ConstProp.cpp @@ -375,6 +375,3 @@ void ConstPropONNXToONNXPass::runOnFunction() { std::unique_ptr mlir::createConstPropONNXToONNXPass() { return std::make_unique(); } - -static PassRegistration pass("constprop-onnx", - "ConstProp ONNX operations into composition of other ONNX operations."); diff --git a/src/Transform/ONNX/Decompose.cpp b/src/Transform/ONNX/Decompose.cpp index 2032344..f2f4a2b 100644 --- a/src/Transform/ONNX/Decompose.cpp +++ b/src/Transform/ONNX/Decompose.cpp @@ -61,6 +61,3 @@ void DecomposeONNXToONNXPass::runOnFunction() { std::unique_ptr mlir::createDecomposeONNXToONNXPass() { return std::make_unique(); } - -static PassRegistration pass("decompose-onnx", - "Decompose ONNX operations into composition of other ONNX operations."); diff --git a/src/Transform/ONNX/ElideConstants.cpp b/src/Transform/ONNX/ElideConstants.cpp index 71c353d..5b0b7b0 100644 --- a/src/Transform/ONNX/ElideConstants.cpp +++ b/src/Transform/ONNX/ElideConstants.cpp @@ -78,6 +78,3 @@ public: std::unique_ptr mlir::createElideConstantValuePass() { return std::make_unique(); } - -static PassRegistration pass( - "elide-constants", "Elide values of constant operations."); diff --git a/src/Transform/ONNX/ShapeInferencePass.cpp b/src/Transform/ONNX/ShapeInferencePass.cpp index b0322ff..232f005 100644 --- a/src/Transform/ONNX/ShapeInferencePass.cpp +++ b/src/Transform/ONNX/ShapeInferencePass.cpp @@ -86,7 +86,4 @@ public: */ std::unique_ptr mlir::createShapeInferencePass() { return std::make_unique(); -} - -static PassRegistration pass( - "shape-inference", "Shape inference for frontend dialects."); +} \ No newline at end of file diff --git a/test/mlir/lit.site.cfg.py.in b/test/mlir/lit.site.cfg.py.in index b820168..ecdd2e9 100644 --- a/test/mlir/lit.site.cfg.py.in +++ b/test/mlir/lit.site.cfg.py.in @@ -1,8 +1,5 @@ import lit.llvm -if '@BUILD_SHARED_LIBS@' == 'ON': - config.environment['LD_PRELOAD'] = r"@ONNX_MLIR_LD_PRELOAD_onnx-mlir-opt@" - config.llvm_tools_dir = r"@MLIR_TOOLS_DIR@" config.mlir_obj_root = r"@LLVM_PROJ_BUILD@" config.mlir_tools_dir = r"@MLIR_TOOLS_DIR@" diff --git a/test/numerical/CMakeLists.txt b/test/numerical/CMakeLists.txt index af72e58..55f1487 100644 --- a/test/numerical/CMakeLists.txt +++ b/test/numerical/CMakeLists.txt @@ -1,25 +1,13 @@ add_executable(TestConv TestConv.cpp) target_link_libraries(TestConv - OMBuilder - OMKrnlOps - OMONNXOps - OMShapeInference - OMShapeInferenceOpInterface - OMAttributePromotion - OMPromotableConstOperandsOpInterface - OMElideConstants - OMElideKrnlGlobalConstants - OMKrnlToAffine - OMKrnlToLLVM - OMONNXToKrnl - OMONNXRewrite + ${OMLibs} ${MLIRLibs} ${CMAKE_DL_LIBS} rapidcheck MainUtils ExecutionSession DynMemRefUtils) -whole_archive_link_mlir(TestConv ${MLIRWholeArchiveLibs}) + target_include_directories(TestConv PRIVATE ${ONNX_MLIR_SRC_ROOT}