diff --git a/mlir/examples/CMakeLists.txt b/mlir/examples/CMakeLists.txt --- a/mlir/examples/CMakeLists.txt +++ b/mlir/examples/CMakeLists.txt @@ -1 +1 @@ -add_subdirectory(toy) +add_subdirectory(toy) \ No newline at end of file diff --git a/mlir/examples/standalone/lib/Standalone/CMakeLists.txt b/mlir/examples/standalone/lib/Standalone/CMakeLists.txt --- a/mlir/examples/standalone/lib/Standalone/CMakeLists.txt +++ b/mlir/examples/standalone/lib/Standalone/CMakeLists.txt @@ -11,8 +11,10 @@ MLIRStandaloneOpsIncGen MLIRStandalonePassesIncGen - LINK_LIBS PUBLIC - MLIRIR + LINK_LIBS PUBLIC + MLIRIR + MLIRPass + MLIRTransforms MLIRInferTypeOpInterface MLIRFuncDialect - ) + ) diff --git a/mlir/examples/standalone/standalone-plugin/CMakeLists.txt b/mlir/examples/standalone/standalone-plugin/CMakeLists.txt --- a/mlir/examples/standalone/standalone-plugin/CMakeLists.txt +++ b/mlir/examples/standalone/standalone-plugin/CMakeLists.txt @@ -1,22 +1,16 @@ -get_property(dialect_libs GLOBAL PROPERTY MLIR_DIALECT_LIBS) -get_property(conversion_libs GLOBAL PROPERTY MLIR_CONVERSION_LIBS) -set(LIBS - MLIRIR - MLIRPass - MLIRPluginsLib - MLIRStandalone - MLIRTransformUtils - ) - -add_mlir_dialect_library(StandalonePlugin +add_mlir_library(StandalonePlugin SHARED standalone-plugin.cpp - DEPENDS - MLIRStandalone + ADDITIONAL_HEADER_DIRS + ${PROJECT_SOURCE_DIR}/include/Standalone ) llvm_update_compile_flags(StandalonePlugin) -target_link_libraries(StandalonePlugin PRIVATE ${LIBS}) +target_link_libraries(StandalonePlugin + PRIVATE + "$<$:-Wl,-undefined,dynamic_lookup>" + "$<$:-Wl,--unresolved-symbols=ignore-in-object-files>" + ) -mlir_check_all_link_libraries(StandalonePlugin) +mlir_check_all_link_libraries(StandalonePlugin) \ No newline at end of file diff --git a/mlir/examples/standalone/standalone-plugin/standalone-plugin.cpp b/mlir/examples/standalone/standalone-plugin/standalone-plugin.cpp --- a/mlir/examples/standalone/standalone-plugin/standalone-plugin.cpp +++ b/mlir/examples/standalone/standalone-plugin/standalone-plugin.cpp @@ -13,27 +13,51 @@ #include "mlir/InitAllPasses.h" #include "mlir/Pass/Pass.h" #include "mlir/Tools/Plugins/DialectPlugin.h" - -#include "Standalone/StandaloneDialect.h" -#include "Standalone/StandalonePasses.h" +#include "mlir/Transforms/GreedyPatternRewriteDriver.h" using namespace mlir; -/// Dialect plugin registration mechanism. -/// Observe that it also allows to register passes. -/// Necessary symbol to register the dialect plugin. -extern "C" LLVM_ATTRIBUTE_WEAK DialectPluginLibraryInfo -mlirGetDialectPluginInfo() { - return {MLIR_PLUGIN_API_VERSION, "Standalone", LLVM_VERSION_STRING, - [](DialectRegistry *registry) { - registry->insert(); - mlir::standalone::registerPasses(); - }}; +namespace { + +class StandaloneSwitchFooBarRewriter : public OpRewritePattern { +public: + using OpRewritePattern::OpRewritePattern; + LogicalResult matchAndRewrite(func::FuncOp op, + PatternRewriter &rewriter) const final { + if (op.getSymName() == "foo") { + rewriter.updateRootInPlace(op, [&op]() { op.setSymName("bar"); }); + return success(); + } + return failure(); + } +}; + +struct StandaloneSwitchFooBar + : public PassWrapper> { + MLIR_DEFINE_EXPLICIT_INTERNAL_INLINE_TYPE_ID(StandaloneSwitchFooBar) + StringRef getArgument() const final { return "standalone-switch-foo-bar"; } + void runOnOperation() override { + RewritePatternSet patterns(&getContext()); + patterns.add(&getContext()); + FrozenRewritePatternSet patternSet(std::move(patterns)); + if (failed(applyPatternsAndFoldGreedily(getOperation(), patternSet))) + signalPassFailure(); + } +}; + +} // namespace + +namespace mlir { +namespace standalone { +void registerStandaloneSwitchFooBar() { + PassRegistration(); } +} // namespace standalone +} // namespace mlir /// Pass plugin registration mechanism. /// Necessary symbol to register the pass plugin. extern "C" LLVM_ATTRIBUTE_WEAK PassPluginLibraryInfo mlirGetPassPluginInfo() { return {MLIR_PLUGIN_API_VERSION, "StandalonePasses", LLVM_VERSION_STRING, - []() { mlir::standalone::registerPasses(); }}; + []() { mlir::standalone::registerStandaloneSwitchFooBar(); }}; } diff --git a/mlir/examples/standalone/test/Standalone/standalone-opt.mlir b/mlir/examples/standalone/test/Standalone/standalone-opt.mlir --- a/mlir/examples/standalone/test/Standalone/standalone-opt.mlir +++ b/mlir/examples/standalone/test/Standalone/standalone-opt.mlir @@ -1,3 +1,14 @@ -// RUN: standalone-opt --show-dialects | FileCheck %s -// CHECK: Available Dialects: -// CHECK-SAME: standalone +// RUN: standalone-opt %s --pass-pipeline="builtin.module(standalone-switch-bar-foo)" | FileCheck %s + +module { + // CHECK-LABEL: func @foo() + func.func @bar() { + return + } + + // CHECK-LABEL: func @abar() + func.func @abar() { + return + } +} + diff --git a/mlir/examples/standalone/test/Standalone/standalone-pass-plugin.mlir b/mlir/examples/standalone/test/Standalone/standalone-pass-plugin.mlir --- a/mlir/examples/standalone/test/Standalone/standalone-pass-plugin.mlir +++ b/mlir/examples/standalone/test/Standalone/standalone-pass-plugin.mlir @@ -1,8 +1,8 @@ -// RUN: mlir-opt %s --load-pass-plugin=%standalone_libs/libStandalonePlugin.so --pass-pipeline="builtin.module(standalone-switch-bar-foo)" | FileCheck %s +// RUN: mlir-opt %s --load-pass-plugin=libStandalonePlugin%shlibext --pass-pipeline="builtin.module(standalone-switch-foo-bar)" | FileCheck %s module { - // CHECK-LABEL: func @foo() - func.func @bar() { + // CHECK-LABEL: func @bar() + func.func @foo() { return } diff --git a/mlir/examples/standalone/test/Standalone/standalone-plugin.mlir b/mlir/examples/standalone/test/Standalone/standalone-plugin.mlir deleted file mode 100644 --- a/mlir/examples/standalone/test/Standalone/standalone-plugin.mlir +++ /dev/null @@ -1,13 +0,0 @@ -// RUN: mlir-opt %s --load-dialect-plugin=%standalone_libs/libStandalonePlugin.so --pass-pipeline="builtin.module(standalone-switch-bar-foo)" | FileCheck %s - -module { - // CHECK-LABEL: func @foo() - func.func @bar() { - return - } - - // CHECK-LABEL: func @standalone_types(%arg0: !standalone.custom<"10">) - func.func @standalone_types(%arg0: !standalone.custom<"10">) { - return - } -} diff --git a/mlir/examples/standalone/test/lit.cfg.py b/mlir/examples/standalone/test/lit.cfg.py --- a/mlir/examples/standalone/test/lit.cfg.py +++ b/mlir/examples/standalone/test/lit.cfg.py @@ -30,6 +30,7 @@ config.test_exec_root = os.path.join(config.standalone_obj_root, 'test') config.substitutions.append(('%PATH%', config.environment['PATH'])) +config.substitutions.append(('%shlibext', config.llvm_shlib_ext)) llvm_config.with_system_environment( ['HOME', 'INCLUDE', 'LIB', 'TMP', 'TEMP']) diff --git a/mlir/examples/standalone/test/lit.site.cfg.py.in b/mlir/examples/standalone/test/lit.site.cfg.py.in --- a/mlir/examples/standalone/test/lit.site.cfg.py.in +++ b/mlir/examples/standalone/test/lit.site.cfg.py.in @@ -4,6 +4,7 @@ config.mlir_obj_dir = "@MLIR_BINARY_DIR@" config.enable_bindings_python = @MLIR_ENABLE_BINDINGS_PYTHON@ config.standalone_obj_root = "@STANDALONE_BINARY_DIR@" +config.llvm_shlib_ext = "@SHLIBEXT@" import lit.llvm lit.llvm.initialize(lit_config, config) diff --git a/mlir/test/Examples/standalone/lit.local.cfg b/mlir/test/Examples/standalone/lit.local.cfg --- a/mlir/test/Examples/standalone/lit.local.cfg +++ b/mlir/test/Examples/standalone/lit.local.cfg @@ -11,3 +11,4 @@ config.substitutions.append( ("%mlir_cmake_dir", config.mlir_cmake_dir)) config.substitutions.append(("%llvm_use_linker", config.llvm_use_linker)) +config.substitutions.append(("%mlir_lib_dir", config.mlir_lib_dir)) diff --git a/mlir/test/Examples/standalone/test.toy b/mlir/test/Examples/standalone/test.toy --- a/mlir/test/Examples/standalone/test.toy +++ b/mlir/test/Examples/standalone/test.toy @@ -1,9 +1,10 @@ -# RUN: %cmake_exe %mlir_src_root/examples/standalone -G "%cmake_generator" \ +# RUN: "%cmake_exe" "%mlir_src_root/examples/standalone" -G "%cmake_generator" \ # RUN: -DCMAKE_CXX_COMPILER=%host_cxx -DCMAKE_C_COMPILER=%host_cc \ -# RUN: -DLLVM_ENABLE_LIBCXX=%enable_libcxx -DMLIR_DIR=%mlir_cmake_dir \ +# RUN: -DLLVM_ENABLE_LIBCXX=%enable_libcxx -DMLIR_DIR="%mlir_cmake_dir" \ # RUN: -DLLVM_USE_LINKER=%llvm_use_linker \ -# RUN: -DPython3_EXECUTABLE=%python -# RUN: %cmake_exe --build . --target check-standalone | tee %t | FileCheck %s +# RUN: -DPython3_EXECUTABLE="%python" -DCMAKE_INSTALL_PREFIX="%mlir_lib_dir/.." +# RUN: "%cmake_exe" --build . --target install-StandalonePlugin | tee %t +# RUN: "%cmake_exe" --build . --target check-standalone | tee %t | FileCheck %s # Note: The number of checked tests is not important. The command will fail # if any fail.