From a08dc357ac3246e53fadc23daeba9da0d8d0f8a6 Mon Sep 17 00:00:00 2001 From: github-actions Date: Fri, 12 Apr 2024 15:11:04 +0000 Subject: [PATCH] Update to LLVM commit c13b7485b87909fcf739f62cfa382b55407433c0 --- .env | 4 +- CMakeLists.txt | 48 ++++++++++++------- README.md | 16 ++++++- include/Standalone/CMakeLists.txt | 4 ++ include/Standalone/StandaloneDialect.h | 1 + include/Standalone/StandaloneDialect.td | 5 ++ include/Standalone/StandaloneOps.td | 2 +- include/Standalone/StandalonePasses.h | 26 ++++++++++ include/Standalone/StandalonePasses.td | 30 ++++++++++++ include/Standalone/StandaloneTypes.h | 17 +++++++ include/Standalone/StandaloneTypes.td | 31 ++++++++++++ lib/Standalone/CMakeLists.txt | 10 ++-- lib/Standalone/StandaloneDialect.cpp | 2 + lib/Standalone/StandaloneOps.cpp | 1 - lib/Standalone/StandalonePasses.cpp | 48 +++++++++++++++++++ lib/Standalone/StandaloneTypes.cpp | 26 ++++++++++ python/StandaloneExtension.cpp | 1 - .../mlir_standalone/dialects/StandaloneOps.td | 1 - standalone-opt/standalone-opt.cpp | 10 +--- standalone-plugin/CMakeLists.txt | 18 +++++++ standalone-plugin/standalone-plugin.cpp | 38 +++++++++++++++ standalone-translate/standalone-translate.cpp | 3 ++ test/CAPI/lit.local.cfg | 2 +- test/CMakeLists.txt | 1 + test/Standalone/dummy.mlir | 5 ++ test/Standalone/standalone-opt.mlir | 2 +- test/Standalone/standalone-pass-plugin.mlir | 13 +++++ test/Standalone/standalone-plugin.mlir | 13 +++++ test/lit.cfg.py | 40 +++++++++------- test/lit.site.cfg.py.in | 5 +- test/python/lit.local.cfg | 4 +- test/python/smoketest.py | 19 ++++---- 32 files changed, 378 insertions(+), 68 deletions(-) create mode 100644 include/Standalone/StandalonePasses.h create mode 100644 include/Standalone/StandalonePasses.td create mode 100644 include/Standalone/StandaloneTypes.h create mode 100644 include/Standalone/StandaloneTypes.td create mode 100644 lib/Standalone/StandalonePasses.cpp create mode 100644 lib/Standalone/StandaloneTypes.cpp create mode 100644 standalone-plugin/CMakeLists.txt create mode 100644 standalone-plugin/standalone-plugin.cpp create mode 100644 test/Standalone/standalone-pass-plugin.mlir create mode 100644 test/Standalone/standalone-plugin.mlir diff --git a/.env b/.env index ee615bf..417c522 100644 --- a/.env +++ b/.env @@ -1,3 +1,3 @@ -LLVM_COMMIT=18ddebe1a1a9bde349441631365f0472e9693520 +LLVM_COMMIT=c13b7485b87909fcf739f62cfa382b55407433c0 CMAKE_FLAGS='-DCMAKE_LINKER=lld -DCMAKE_C_COMPILER=clang -DCMAKE_CXX_COMPILER=clang++ -DMLIR_DIR=$GITHUB_WORKSPACE/llvm-project/prefix/lib/cmake/mlir/ -DLLVM_DIR=$GITHUB_WORKSPACE/llvm-project/prefix/lib/cmake/llvm/ -DLLVM_EXTERNAL_LIT=$GITHUB_WORKSPACE/llvm-project/build/bin/llvm-lit' -LLVM_REF=refs/tags/llvmorg-16.0.2 +LLVM_REF=refs/tags/llvmorg-18.1.3 diff --git a/CMakeLists.txt b/CMakeLists.txt index 84bce58..038242b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,35 +1,46 @@ -cmake_minimum_required(VERSION 3.13.4) +cmake_minimum_required(VERSION 3.20.0) project(standalone-dialect LANGUAGES CXX C) set(CMAKE_BUILD_WITH_INSTALL_NAME_DIR ON) set(CMAKE_CXX_STANDARD 17 CACHE STRING "C++ standard to conform to") -find_package(MLIR REQUIRED CONFIG) - -message(STATUS "Using MLIRConfig.cmake in: ${MLIR_DIR}") -message(STATUS "Using LLVMConfig.cmake in: ${LLVM_DIR}") - -set(LLVM_RUNTIME_OUTPUT_INTDIR ${CMAKE_BINARY_DIR}/bin) -set(LLVM_LIBRARY_OUTPUT_INTDIR ${CMAKE_BINARY_DIR}/lib) -set(MLIR_BINARY_DIR ${CMAKE_BINARY_DIR}) - -list(APPEND CMAKE_MODULE_PATH "${MLIR_CMAKE_DIR}") -list(APPEND CMAKE_MODULE_PATH "${LLVM_CMAKE_DIR}") -include(TableGen) -include(AddLLVM) -include(AddMLIR) -include(HandleLLVMOptions) +if(CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR) + find_package(MLIR REQUIRED CONFIG) + + message(STATUS "Using MLIRConfig.cmake in: ${MLIR_DIR}") + message(STATUS "Using LLVMConfig.cmake in: ${LLVM_DIR}") + + set(LLVM_RUNTIME_OUTPUT_INTDIR ${CMAKE_BINARY_DIR}/bin) + set(LLVM_LIBRARY_OUTPUT_INTDIR ${CMAKE_BINARY_DIR}/lib) + set(MLIR_BINARY_DIR ${CMAKE_BINARY_DIR}) + + list(APPEND CMAKE_MODULE_PATH "${MLIR_CMAKE_DIR}") + list(APPEND CMAKE_MODULE_PATH "${LLVM_CMAKE_DIR}") + + include(TableGen) + include(AddLLVM) + include(AddMLIR) + include(HandleLLVMOptions) +else() + # Build via external projects mechanism + set(MLIR_MAIN_SRC_DIR ${LLVM_MAIN_SRC_DIR}/../mlir) + set(MLIR_INCLUDE_DIR ${MLIR_MAIN_SRC_DIR}/include) + set(MLIR_GENERATED_INCLUDE_DIR ${LLVM_BINARY_DIR}/tools/mlir/include) + set(MLIR_INCLUDE_DIRS "${MLIR_INCLUDE_DIR};${MLIR_GENERATED_INCLUDE_DIR}") +endif() if(MLIR_ENABLE_BINDINGS_PYTHON) include(MLIRDetectPythonEnv) mlir_configure_python_dev_packages() endif() +set(STANDALONE_SOURCE_DIR ${PROJECT_SOURCE_DIR}) +set(STANDALONE_BINARY_DIR ${PROJECT_BINARY_DIR}) include_directories(${LLVM_INCLUDE_DIRS}) include_directories(${MLIR_INCLUDE_DIRS}) -include_directories(${PROJECT_SOURCE_DIR}/include) -include_directories(${PROJECT_BINARY_DIR}/include) +include_directories(${STANDALONE_SOURCE_DIR}/include) +include_directories(${STANDALONE_BINARY_DIR}/include) link_directories(${LLVM_BUILD_LIBRARY_DIR}) add_definitions(${LLVM_DEFINITIONS}) @@ -41,4 +52,5 @@ if(MLIR_ENABLE_BINDINGS_PYTHON) endif() add_subdirectory(test) add_subdirectory(standalone-opt) +add_subdirectory(standalone-plugin) add_subdirectory(standalone-translate) diff --git a/README.md b/README.md index b1ca627..6615f66 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,7 @@ This is an example of an out-of-tree [MLIR](https://mlir.llvm.org/) dialect along with a standalone `opt`-like tool to operate on that dialect. -## Building +## Building - Component Build This setup assumes that you have built LLVM and MLIR in `$BUILD_DIR` and installed them to `$PREFIX`. To build and launch the tests, run ```sh @@ -16,3 +16,17 @@ cmake --build . --target mlir-doc ``` **Note**: Make sure to pass `-DLLVM_INSTALL_UTILS=ON` when building LLVM with CMake in order to install `FileCheck` to the chosen installation prefix. +## Building - Monolithic Build + +This setup assumes that you build the project as part of a monolithic LLVM build via the `LLVM_EXTERNAL_PROJECTS` mechanism. +To build LLVM, MLIR, the example and launch the tests run +```sh +mkdir build && cd build +cmake -G Ninja `$LLVM_SRC_DIR/llvm` \ + -DCMAKE_BUILD_TYPE=Release \ + -DLLVM_TARGETS_TO_BUILD=host \ + -DLLVM_ENABLE_PROJECTS=mlir \ + -DLLVM_EXTERNAL_PROJECTS=standalone-dialect -DLLVM_EXTERNAL_STANDALONE_DIALECT_SOURCE_DIR=../ +cmake --build . --target check-standalone +``` +Here, `$LLVM_SRC_DIR` needs to point to the root of the monorepo. diff --git a/include/Standalone/CMakeLists.txt b/include/Standalone/CMakeLists.txt index 8acf640..975a4ff 100644 --- a/include/Standalone/CMakeLists.txt +++ b/include/Standalone/CMakeLists.txt @@ -1,3 +1,7 @@ add_mlir_dialect(StandaloneOps standalone) add_mlir_doc(StandaloneDialect StandaloneDialect Standalone/ -gen-dialect-doc) add_mlir_doc(StandaloneOps StandaloneOps Standalone/ -gen-op-doc) + +set(LLVM_TARGET_DEFINITIONS StandalonePasses.td) +mlir_tablegen(StandalonePasses.h.inc --gen-pass-decls) +add_public_tablegen_target(MLIRStandalonePassesIncGen) diff --git a/include/Standalone/StandaloneDialect.h b/include/Standalone/StandaloneDialect.h index d3eb24c..71bbefb 100644 --- a/include/Standalone/StandaloneDialect.h +++ b/include/Standalone/StandaloneDialect.h @@ -9,6 +9,7 @@ #ifndef STANDALONE_STANDALONEDIALECT_H #define STANDALONE_STANDALONEDIALECT_H +#include "mlir/Bytecode/BytecodeOpInterface.h" #include "mlir/IR/Dialect.h" #include "Standalone/StandaloneOpsDialect.h.inc" diff --git a/include/Standalone/StandaloneDialect.td b/include/Standalone/StandaloneDialect.td index 1588601..598a8df 100644 --- a/include/Standalone/StandaloneDialect.td +++ b/include/Standalone/StandaloneDialect.td @@ -24,6 +24,11 @@ def Standalone_Dialect : Dialect { working inside of the LLVM source tree. }]; let cppNamespace = "::mlir::standalone"; + + let useDefaultTypePrinterParser = 1; + let extraClassDeclaration = [{ + void registerTypes(); + }]; } //===----------------------------------------------------------------------===// diff --git a/include/Standalone/StandaloneOps.td b/include/Standalone/StandaloneOps.td index 6c9da31..9882a25 100644 --- a/include/Standalone/StandaloneOps.td +++ b/include/Standalone/StandaloneOps.td @@ -9,7 +9,7 @@ #ifndef STANDALONE_OPS #define STANDALONE_OPS -include "Standalone/StandaloneDialect.td" +include "Standalone/StandaloneTypes.td" include "mlir/Interfaces/InferTypeOpInterface.td" include "mlir/Interfaces/SideEffectInterfaces.td" diff --git a/include/Standalone/StandalonePasses.h b/include/Standalone/StandalonePasses.h new file mode 100644 index 0000000..75546d6 --- /dev/null +++ b/include/Standalone/StandalonePasses.h @@ -0,0 +1,26 @@ +//===- StandalonePasses.h - Standalone passes ------------------*- C++ -*-===// +// +// This file is licensed under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// +#ifndef STANDALONE_STANDALONEPASSES_H +#define STANDALONE_STANDALONEPASSES_H + +#include "Standalone/StandaloneDialect.h" +#include "Standalone/StandaloneOps.h" +#include "mlir/Pass/Pass.h" +#include + +namespace mlir { +namespace standalone { +#define GEN_PASS_DECL +#include "Standalone/StandalonePasses.h.inc" + +#define GEN_PASS_REGISTRATION +#include "Standalone/StandalonePasses.h.inc" +} // namespace standalone +} // namespace mlir + +#endif diff --git a/include/Standalone/StandalonePasses.td b/include/Standalone/StandalonePasses.td new file mode 100644 index 0000000..4cb2be0 --- /dev/null +++ b/include/Standalone/StandalonePasses.td @@ -0,0 +1,30 @@ +//===- StandalonePsss.td - Standalone dialect passes -------*- tablegen -*-===// +// +// This file is licensed under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#ifndef STANDALONE_PASS +#define STANDALONE_PASS + +include "mlir/Pass/PassBase.td" + +def StandaloneSwitchBarFoo: Pass<"standalone-switch-bar-foo", "::mlir::ModuleOp"> { + let summary = "Switches the name of a FuncOp named `bar` to `foo` and folds."; + let description = [{ + Switches the name of a FuncOp named `bar` to `foo` and folds. + ``` + func.func @bar() { + return + } + // Gets transformed to: + func.func @foo() { + return + } + ``` + }]; +} + +#endif // STANDALONE_PASS diff --git a/include/Standalone/StandaloneTypes.h b/include/Standalone/StandaloneTypes.h new file mode 100644 index 0000000..681bb5e --- /dev/null +++ b/include/Standalone/StandaloneTypes.h @@ -0,0 +1,17 @@ +//===- StandaloneTypes.h - Standalone dialect types -------------*- C++ -*-===// +// +// This file is licensed under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#ifndef STANDALONE_STANDALONETYPES_H +#define STANDALONE_STANDALONETYPES_H + +#include "mlir/IR/BuiltinTypes.h" + +#define GET_TYPEDEF_CLASSES +#include "Standalone/StandaloneOpsTypes.h.inc" + +#endif // STANDALONE_STANDALONETYPES_H diff --git a/include/Standalone/StandaloneTypes.td b/include/Standalone/StandaloneTypes.td new file mode 100644 index 0000000..05b4c45 --- /dev/null +++ b/include/Standalone/StandaloneTypes.td @@ -0,0 +1,31 @@ +//===- StandaloneTypes.td - Standalone dialect types -------*- tablegen -*-===// +// +// This file is licensed under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#ifndef STANDALONE_TYPES +#define STANDALONE_TYPES + +include "mlir/IR/AttrTypeBase.td" +include "Standalone/StandaloneDialect.td" + +//===----------------------------------------------------------------------===// +// Standalone type definitions +//===----------------------------------------------------------------------===// + +class Standalone_Type traits = []> + : TypeDef { + let mnemonic = typeMnemonic; +} + +def Standalone_CustomType : Standalone_Type<"Custom", "custom"> { + let summary = "Standalone custom type"; + let description = "Custom type in standalone dialect"; + let parameters = (ins StringRefParameter<"the custom value">:$value); + let assemblyFormat = "`<` $value `>`"; +} + +#endif // STANDALONE_TYPES diff --git a/lib/Standalone/CMakeLists.txt b/lib/Standalone/CMakeLists.txt index eadc695..0f1705a 100644 --- a/lib/Standalone/CMakeLists.txt +++ b/lib/Standalone/CMakeLists.txt @@ -1,14 +1,18 @@ add_mlir_dialect_library(MLIRStandalone + StandaloneTypes.cpp StandaloneDialect.cpp StandaloneOps.cpp + StandalonePasses.cpp ADDITIONAL_HEADER_DIRS ${PROJECT_SOURCE_DIR}/include/Standalone DEPENDS MLIRStandaloneOpsIncGen + MLIRStandalonePassesIncGen - LINK_LIBS PUBLIC - MLIRIR + LINK_LIBS PUBLIC + MLIRIR MLIRInferTypeOpInterface - ) + MLIRFuncDialect + ) diff --git a/lib/Standalone/StandaloneDialect.cpp b/lib/Standalone/StandaloneDialect.cpp index cdd9337..1ea69f9 100644 --- a/lib/Standalone/StandaloneDialect.cpp +++ b/lib/Standalone/StandaloneDialect.cpp @@ -8,6 +8,7 @@ #include "Standalone/StandaloneDialect.h" #include "Standalone/StandaloneOps.h" +#include "Standalone/StandaloneTypes.h" using namespace mlir; using namespace mlir::standalone; @@ -23,4 +24,5 @@ void StandaloneDialect::initialize() { #define GET_OP_LIST #include "Standalone/StandaloneOps.cpp.inc" >(); + registerTypes(); } diff --git a/lib/Standalone/StandaloneOps.cpp b/lib/Standalone/StandaloneOps.cpp index 497eb98..55b66b5 100644 --- a/lib/Standalone/StandaloneOps.cpp +++ b/lib/Standalone/StandaloneOps.cpp @@ -8,7 +8,6 @@ #include "Standalone/StandaloneOps.h" #include "Standalone/StandaloneDialect.h" -#include "mlir/IR/OpImplementation.h" #define GET_OP_CLASSES #include "Standalone/StandaloneOps.cpp.inc" diff --git a/lib/Standalone/StandalonePasses.cpp b/lib/Standalone/StandalonePasses.cpp new file mode 100644 index 0000000..a23d042 --- /dev/null +++ b/lib/Standalone/StandalonePasses.cpp @@ -0,0 +1,48 @@ +//===- StandalonePasses.cpp - Standalone passes -----------------*- C++ -*-===// +// +// This file is licensed under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// +#include "mlir/Dialect/Func/IR/FuncOps.h" +#include "mlir/IR/PatternMatch.h" +#include "mlir/Rewrite/FrozenRewritePatternSet.h" +#include "mlir/Support/LogicalResult.h" +#include "mlir/Transforms/GreedyPatternRewriteDriver.h" + +#include "Standalone/StandalonePasses.h" + +namespace mlir::standalone { +#define GEN_PASS_DEF_STANDALONESWITCHBARFOO +#include "Standalone/StandalonePasses.h.inc" + +namespace { +class StandaloneSwitchBarFooRewriter : public OpRewritePattern { +public: + using OpRewritePattern::OpRewritePattern; + LogicalResult matchAndRewrite(func::FuncOp op, + PatternRewriter &rewriter) const final { + if (op.getSymName() == "bar") { + rewriter.modifyOpInPlace(op, [&op]() { op.setSymName("foo"); }); + return success(); + } + return failure(); + } +}; + +class StandaloneSwitchBarFoo + : public impl::StandaloneSwitchBarFooBase { +public: + using impl::StandaloneSwitchBarFooBase< + StandaloneSwitchBarFoo>::StandaloneSwitchBarFooBase; + void runOnOperation() final { + RewritePatternSet patterns(&getContext()); + patterns.add(&getContext()); + FrozenRewritePatternSet patternSet(std::move(patterns)); + if (failed(applyPatternsAndFoldGreedily(getOperation(), patternSet))) + signalPassFailure(); + } +}; +} // namespace +} // namespace mlir::standalone diff --git a/lib/Standalone/StandaloneTypes.cpp b/lib/Standalone/StandaloneTypes.cpp new file mode 100644 index 0000000..4ce4e00 --- /dev/null +++ b/lib/Standalone/StandaloneTypes.cpp @@ -0,0 +1,26 @@ +//===- StandaloneTypes.cpp - Standalone dialect types -----------*- C++ -*-===// +// +// This file is licensed under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#include "Standalone/StandaloneTypes.h" + +#include "Standalone/StandaloneDialect.h" +#include "mlir/IR/Builders.h" +#include "mlir/IR/DialectImplementation.h" +#include "llvm/ADT/TypeSwitch.h" + +using namespace mlir::standalone; + +#define GET_TYPEDEF_CLASSES +#include "Standalone/StandaloneOpsTypes.cpp.inc" + +void StandaloneDialect::registerTypes() { + addTypes< +#define GET_TYPEDEF_LIST +#include "Standalone/StandaloneOpsTypes.cpp.inc" + >(); +} diff --git a/python/StandaloneExtension.cpp b/python/StandaloneExtension.cpp index e8f574f..5e83060 100644 --- a/python/StandaloneExtension.cpp +++ b/python/StandaloneExtension.cpp @@ -9,7 +9,6 @@ #include "Standalone-c/Dialects.h" #include "mlir/Bindings/Python/PybindAdaptors.h" -namespace py = pybind11; using namespace mlir::python::adaptors; PYBIND11_MODULE(_standaloneDialects, m) { diff --git a/python/mlir_standalone/dialects/StandaloneOps.td b/python/mlir_standalone/dialects/StandaloneOps.td index 6cfa6b7..7246582 100644 --- a/python/mlir_standalone/dialects/StandaloneOps.td +++ b/python/mlir_standalone/dialects/StandaloneOps.td @@ -9,7 +9,6 @@ #ifndef PYTHON_BINDINGS_STANDALONE_OPS #define PYTHON_BINDINGS_STANDALONE_OPS -include "mlir/Bindings/Python/Attributes.td" include "Standalone/StandaloneOps.td" #endif diff --git a/standalone-opt/standalone-opt.cpp b/standalone-opt/standalone-opt.cpp index 4cf7e15..e39fa96 100644 --- a/standalone-opt/standalone-opt.cpp +++ b/standalone-opt/standalone-opt.cpp @@ -6,24 +6,18 @@ // //===----------------------------------------------------------------------===// -#include "mlir/Dialect/Arith/IR/Arith.h" -#include "mlir/IR/Dialect.h" #include "mlir/IR/MLIRContext.h" #include "mlir/InitAllDialects.h" #include "mlir/InitAllPasses.h" -#include "mlir/Pass/Pass.h" -#include "mlir/Pass/PassManager.h" #include "mlir/Support/FileUtilities.h" #include "mlir/Tools/mlir-opt/MlirOptMain.h" -#include "llvm/Support/CommandLine.h" -#include "llvm/Support/InitLLVM.h" -#include "llvm/Support/SourceMgr.h" -#include "llvm/Support/ToolOutputFile.h" #include "Standalone/StandaloneDialect.h" +#include "Standalone/StandalonePasses.h" int main(int argc, char **argv) { mlir::registerAllPasses(); + mlir::standalone::registerPasses(); // TODO: Register standalone passes here. mlir::DialectRegistry registry; diff --git a/standalone-plugin/CMakeLists.txt b/standalone-plugin/CMakeLists.txt new file mode 100644 index 0000000..16ac0cc --- /dev/null +++ b/standalone-plugin/CMakeLists.txt @@ -0,0 +1,18 @@ +add_llvm_library(StandalonePlugin + # BUILDTREE_ONLY is only for testing purposes + MODULE BUILDTREE_ONLY + standalone-plugin.cpp + + DEPENDS + MLIRStandalone + PLUGIN_TOOL + mlir-opt + + LINK_LIBS + MLIRStandalone + ) +target_include_directories( + StandalonePlugin + PRIVATE + "${STANDALONE_BINARY_DIR}/include" +) diff --git a/standalone-plugin/standalone-plugin.cpp b/standalone-plugin/standalone-plugin.cpp new file mode 100644 index 0000000..d2dcdc9 --- /dev/null +++ b/standalone-plugin/standalone-plugin.cpp @@ -0,0 +1,38 @@ +//===- standalone-plugin.cpp ------------------------------------*- C++ -*-===// +// +// This file is licensed under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#include "mlir/IR/MLIRContext.h" +#include "mlir/InitAllDialects.h" +#include "mlir/Tools/Plugins/DialectPlugin.h" + +#include "Standalone/StandaloneDialect.h" +#include "Standalone/StandalonePasses.h" +#include "mlir/Tools/Plugins/PassPlugin.h" +#include "llvm/Config/llvm-config.h" +#include "llvm/Support/Compiler.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(); + }}; +} + +/// 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(); }}; +} diff --git a/standalone-translate/standalone-translate.cpp b/standalone-translate/standalone-translate.cpp index 94277c1..a4da328 100644 --- a/standalone-translate/standalone-translate.cpp +++ b/standalone-translate/standalone-translate.cpp @@ -12,10 +12,13 @@ //===----------------------------------------------------------------------===// #include "Standalone/StandaloneDialect.h" +#include "mlir/IR/DialectRegistry.h" +#include "mlir/IR/Operation.h" #include "mlir/InitAllTranslations.h" #include "mlir/Support/LogicalResult.h" #include "mlir/Tools/mlir-translate/MlirTranslateMain.h" #include "mlir/Tools/mlir-translate/Translation.h" +#include "llvm/Support/raw_ostream.h" int main(int argc, char **argv) { mlir::registerAllTranslations(); diff --git a/test/CAPI/lit.local.cfg b/test/CAPI/lit.local.cfg index f08a0de..bb0c17c 100644 --- a/test/CAPI/lit.local.cfg +++ b/test/CAPI/lit.local.cfg @@ -1 +1 @@ -config.suffixes.add('.c') +config.suffixes.add(".c") diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index d47ba93..fdde159 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -14,6 +14,7 @@ set(STANDALONE_TEST_DEPENDS standalone-capi-test standalone-opt standalone-translate + StandalonePlugin ) if(MLIR_ENABLE_BINDINGS_PYTHON) list(APPEND STANDALONE_TEST_DEPENDS StandalonePythonModules) diff --git a/test/Standalone/dummy.mlir b/test/Standalone/dummy.mlir index cb688d2..60b169e 100644 --- a/test/Standalone/dummy.mlir +++ b/test/Standalone/dummy.mlir @@ -8,4 +8,9 @@ module { %res = standalone.foo %0 : i32 return } + + // CHECK-LABEL: func @standalone_types(%arg0: !standalone.custom<"10">) + func.func @standalone_types(%arg0: !standalone.custom<"10">) { + return + } } diff --git a/test/Standalone/standalone-opt.mlir b/test/Standalone/standalone-opt.mlir index 1a78a9d..101f901 100644 --- a/test/Standalone/standalone-opt.mlir +++ b/test/Standalone/standalone-opt.mlir @@ -1,3 +1,3 @@ // RUN: standalone-opt --show-dialects | FileCheck %s // CHECK: Available Dialects: -// CHECK: standalone +// CHECK-SAME: standalone diff --git a/test/Standalone/standalone-pass-plugin.mlir b/test/Standalone/standalone-pass-plugin.mlir new file mode 100644 index 0000000..1d652dc --- /dev/null +++ b/test/Standalone/standalone-pass-plugin.mlir @@ -0,0 +1,13 @@ +// RUN: mlir-opt %s --load-pass-plugin=%standalone_libs/StandalonePlugin%shlibext --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/test/Standalone/standalone-plugin.mlir b/test/Standalone/standalone-plugin.mlir new file mode 100644 index 0000000..468932b --- /dev/null +++ b/test/Standalone/standalone-plugin.mlir @@ -0,0 +1,13 @@ +// RUN: mlir-opt %s --load-dialect-plugin=%standalone_libs/StandalonePlugin%shlibext --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/test/lit.cfg.py b/test/lit.cfg.py index a6a3d24..e27dddd 100644 --- a/test/lit.cfg.py +++ b/test/lit.cfg.py @@ -16,47 +16,55 @@ # Configuration file for the 'lit' test runner. # name: The name of this test suite. -config.name = 'STANDALONE' +config.name = "STANDALONE" config.test_format = lit.formats.ShTest(not llvm_config.use_lit_shell) # suffixes: A list of file extensions to treat as test files. -config.suffixes = ['.mlir'] +config.suffixes = [".mlir"] # test_source_root: The root path where tests are located. config.test_source_root = os.path.dirname(__file__) # test_exec_root: The root path where tests should be run. -config.test_exec_root = os.path.join(config.standalone_obj_root, 'test') +config.test_exec_root = os.path.join(config.standalone_obj_root, "test") -config.substitutions.append(('%PATH%', config.environment['PATH'])) +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']) +llvm_config.with_system_environment(["HOME", "INCLUDE", "LIB", "TMP", "TEMP"]) llvm_config.use_default_substitutions() # excludes: A list of directories to exclude from the testsuite. The 'Inputs' # subdirectories contain auxiliary inputs for various tests in their parent # directories. -config.excludes = ['Inputs', 'Examples', 'CMakeLists.txt', 'README.txt', 'LICENSE.txt'] +config.excludes = ["Inputs", "Examples", "CMakeLists.txt", "README.txt", "LICENSE.txt"] # test_exec_root: The root path where tests should be run. -config.test_exec_root = os.path.join(config.standalone_obj_root, 'test') -config.standalone_tools_dir = os.path.join(config.standalone_obj_root, 'bin') +config.test_exec_root = os.path.join(config.standalone_obj_root, "test") +config.standalone_tools_dir = os.path.join(config.standalone_obj_root, "bin") +config.standalone_libs_dir = os.path.join(config.standalone_obj_root, "lib") + +config.substitutions.append(("%standalone_libs", config.standalone_libs_dir)) # Tweak the PATH to include the tools dir. -llvm_config.with_environment('PATH', config.llvm_tools_dir, append_path=True) +llvm_config.with_environment("PATH", config.llvm_tools_dir, append_path=True) tool_dirs = [config.standalone_tools_dir, config.llvm_tools_dir] tools = [ - 'standalone-capi-test', - 'standalone-opt', - 'standalone-translate', + "mlir-opt", + "standalone-capi-test", + "standalone-opt", + "standalone-translate", ] llvm_config.add_tool_substitutions(tools, tool_dirs) -llvm_config.with_environment('PYTHONPATH', [ - os.path.join(config.mlir_obj_dir, 'python_packages', 'standalone'), -], append_path=True) +llvm_config.with_environment( + "PYTHONPATH", + [ + os.path.join(config.mlir_obj_dir, "python_packages", "standalone"), + ], + append_path=True, +) diff --git a/test/lit.site.cfg.py.in b/test/lit.site.cfg.py.in index 8f9e557..eae93d0 100644 --- a/test/lit.site.cfg.py.in +++ b/test/lit.site.cfg.py.in @@ -3,10 +3,11 @@ config.llvm_tools_dir = lit_config.substitute("@LLVM_TOOLS_DIR@") config.mlir_obj_dir = "@MLIR_BINARY_DIR@" config.enable_bindings_python = @MLIR_ENABLE_BINDINGS_PYTHON@ -config.standalone_obj_root = "@CMAKE_BINARY_DIR@" +config.standalone_obj_root = "@STANDALONE_BINARY_DIR@" +config.llvm_shlib_ext = "@SHLIBEXT@" import lit.llvm lit.llvm.initialize(lit_config, config) # Let the main config do the real work. -lit_config.load_config(config, "@CMAKE_SOURCE_DIR@/test/lit.cfg.py") +lit_config.load_config(config, "@STANDALONE_SOURCE_DIR@/test/lit.cfg.py") diff --git a/test/python/lit.local.cfg b/test/python/lit.local.cfg index b70b9d7..3394f18 100644 --- a/test/python/lit.local.cfg +++ b/test/python/lit.local.cfg @@ -1,4 +1,4 @@ -config.suffixes.add('.py') +config.suffixes.add(".py") if not config.enable_bindings_python: - config.unsupported = True + config.unsupported = True diff --git a/test/python/smoketest.py b/test/python/smoketest.py index 0d8f41c..08e08cb 100644 --- a/test/python/smoketest.py +++ b/test/python/smoketest.py @@ -1,17 +1,16 @@ # RUN: %python %s | FileCheck %s from mlir_standalone.ir import * -from mlir_standalone.dialects import ( - builtin as builtin_d, - standalone as standalone_d -) +from mlir_standalone.dialects import builtin as builtin_d, standalone as standalone_d with Context(): - standalone_d.register_dialect() - module = Module.parse(""" + standalone_d.register_dialect() + module = Module.parse( + """ %0 = arith.constant 2 : i32 %1 = standalone.foo %0 : i32 - """) - # CHECK: %[[C:.*]] = arith.constant 2 : i32 - # CHECK: standalone.foo %[[C]] : i32 - print(str(module)) + """ + ) + # CHECK: %[[C:.*]] = arith.constant 2 : i32 + # CHECK: standalone.foo %[[C]] : i32 + print(str(module))