From 0034f32d6d85d7f21cf04c82083860cdb2fe1ddb Mon Sep 17 00:00:00 2001 From: Yann Herklotz Date: Mon, 26 Sep 2022 19:08:01 +0100 Subject: Update MLIR files and include LLVM directory --- test/CAPI/CMakeLists.txt | 20 ++++++++++++++++ test/CAPI/lit.local.cfg | 1 + test/CAPI/standalone-capi-test.c | 49 ++++++++++++++++++++++++++++++++++++++++ test/CMakeLists.txt | 10 ++++++++ test/Standalone/dummy.mlir | 4 ++-- test/lit.cfg.py | 11 +++++---- test/lit.site.cfg.py.in | 45 +++--------------------------------- test/python/lit.local.cfg | 4 ++++ test/python/smoketest.py | 17 ++++++++++++++ 9 files changed, 112 insertions(+), 49 deletions(-) create mode 100644 test/CAPI/CMakeLists.txt create mode 100644 test/CAPI/lit.local.cfg create mode 100644 test/CAPI/standalone-capi-test.c create mode 100644 test/python/lit.local.cfg create mode 100644 test/python/smoketest.py (limited to 'test') diff --git a/test/CAPI/CMakeLists.txt b/test/CAPI/CMakeLists.txt new file mode 100644 index 0000000..eaa6cfc --- /dev/null +++ b/test/CAPI/CMakeLists.txt @@ -0,0 +1,20 @@ +# Here we create a single aggregate shared library with the parts of the CAPI +# that we want to bundle together. Then we link a simple C executable +# against it to demonstrate that it does have the fully self contained +# core MLIR library and our own standalone dialect. +add_mlir_aggregate(StandaloneCAPITestLib + SHARED + EMBED_LIBS + MLIRCAPIIR + # TODO: Remove this in favor of showing fine grained dialect registration + # (once available). + MLIRCAPIRegisterEverything + StandaloneCAPI +) + +add_llvm_executable(standalone-capi-test + standalone-capi-test.c +) +llvm_update_compile_flags(standalone-capi-test) +target_link_libraries(standalone-capi-test + PRIVATE StandaloneCAPITestLib) diff --git a/test/CAPI/lit.local.cfg b/test/CAPI/lit.local.cfg new file mode 100644 index 0000000..f08a0de --- /dev/null +++ b/test/CAPI/lit.local.cfg @@ -0,0 +1 @@ +config.suffixes.add('.c') diff --git a/test/CAPI/standalone-capi-test.c b/test/CAPI/standalone-capi-test.c new file mode 100644 index 0000000..54f3ca7 --- /dev/null +++ b/test/CAPI/standalone-capi-test.c @@ -0,0 +1,49 @@ +//===- standalone-cap-demo.c - Simple demo of C-API -----------------------===// +// +// Part of the LLVM Project, 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 +// +//===----------------------------------------------------------------------===// + +// RUN: standalone-capi-test 2>&1 | FileCheck %s + +#include + +#include "Standalone-c/Dialects.h" +#include "mlir-c/IR.h" +#include "mlir-c/RegisterEverything.h" + +static void registerAllUpstreamDialects(MlirContext ctx) { + MlirDialectRegistry registry = mlirDialectRegistryCreate(); + mlirRegisterAllDialects(registry); + mlirContextAppendDialectRegistry(ctx, registry); + mlirDialectRegistryDestroy(registry); +} + +int main(int argc, char **argv) { + MlirContext ctx = mlirContextCreate(); + // TODO: Create the dialect handles for the builtin dialects and avoid this. + // This adds dozens of MB of binary size over just the standalone dialect. + registerAllUpstreamDialects(ctx); + mlirDialectHandleRegisterDialect(mlirGetDialectHandle__standalone__(), ctx); + + MlirModule module = mlirModuleCreateParse( + ctx, mlirStringRefCreateFromCString("%0 = arith.constant 2 : i32\n" + "%1 = standalone.foo %0 : i32\n")); + if (mlirModuleIsNull(module)) { + printf("ERROR: Could not parse.\n"); + mlirContextDestroy(ctx); + return 1; + } + MlirOperation op = mlirModuleGetOperation(module); + + // CHECK: %[[C:.*]] = arith.constant 2 : i32 + // CHECK: standalone.foo %[[C]] : i32 + mlirOperationDump(op); + + mlirModuleDestroy(module); + mlirContextDestroy(ctx); + return 0; +} diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 29da2b8..d47ba93 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -1,3 +1,7 @@ +llvm_canonicalize_cmake_booleans( + MLIR_ENABLE_BINDINGS_PYTHON +) + configure_lit_site_cfg( ${CMAKE_CURRENT_SOURCE_DIR}/lit.site.cfg.py.in ${CMAKE_CURRENT_BINARY_DIR}/lit.site.cfg.py @@ -7,9 +11,13 @@ configure_lit_site_cfg( set(STANDALONE_TEST_DEPENDS FileCheck count not + standalone-capi-test standalone-opt standalone-translate ) +if(MLIR_ENABLE_BINDINGS_PYTHON) + list(APPEND STANDALONE_TEST_DEPENDS StandalonePythonModules) +endif() add_lit_testsuite(check-standalone "Running the standalone regression tests" ${CMAKE_CURRENT_BINARY_DIR} @@ -18,3 +26,5 @@ add_lit_testsuite(check-standalone "Running the standalone regression tests" set_target_properties(check-standalone PROPERTIES FOLDER "Tests") add_lit_testsuites(STANDALONE ${CMAKE_CURRENT_SOURCE_DIR} DEPENDS ${STANDALONE_TEST_DEPENDS}) + +add_subdirectory(CAPI) diff --git a/test/Standalone/dummy.mlir b/test/Standalone/dummy.mlir index ada4280..cb688d2 100644 --- a/test/Standalone/dummy.mlir +++ b/test/Standalone/dummy.mlir @@ -2,8 +2,8 @@ module { // CHECK-LABEL: func @bar() - func @bar() { - %0 = constant 1 : i32 + func.func @bar() { + %0 = arith.constant 1 : i32 // CHECK: %{{.*}} = standalone.foo %{{.*}} : i32 %res = standalone.foo %0 : i32 return diff --git a/test/lit.cfg.py b/test/lit.cfg.py index 9fb5b54..a6a3d24 100644 --- a/test/lit.cfg.py +++ b/test/lit.cfg.py @@ -30,7 +30,6 @@ config.test_source_root = os.path.dirname(__file__) 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']) @@ -42,9 +41,6 @@ llvm_config.use_default_substitutions() # directories. config.excludes = ['Inputs', 'Examples', 'CMakeLists.txt', 'README.txt', 'LICENSE.txt'] -# 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.standalone_tools_dir = os.path.join(config.standalone_obj_root, 'bin') @@ -54,8 +50,13 @@ 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' + '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) diff --git a/test/lit.site.cfg.py.in b/test/lit.site.cfg.py.in index 26fb8aa..8f9e557 100644 --- a/test/lit.site.cfg.py.in +++ b/test/lit.site.cfg.py.in @@ -1,49 +1,10 @@ @LIT_SITE_CFG_IN_HEADER@ -import sys - -config.host_triple = "@LLVM_HOST_TRIPLE@" -config.target_triple = "@TARGET_TRIPLE@" -config.llvm_src_root = "@LLVM_SOURCE_DIR@" -config.llvm_obj_root = "@LLVM_BINARY_DIR@" -config.llvm_tools_dir = "@LLVM_TOOLS_DIR@" -config.llvm_lib_dir = "@LLVM_LIBS_DIR@" -config.llvm_shlib_dir = "@SHLIBDIR@" -config.llvm_shlib_ext = "@SHLIBEXT@" -config.llvm_exe_ext = "@EXEEXT@" -config.lit_tools_dir = "@LLVM_LIT_TOOLS_DIR@" -config.python_executable = "@PYTHON_EXECUTABLE@" -config.gold_executable = "@GOLD_EXECUTABLE@" -config.ld64_executable = "@LD64_EXECUTABLE@" -config.enable_shared = @ENABLE_SHARED@ -config.enable_assertions = @ENABLE_ASSERTIONS@ -config.targets_to_build = "@TARGETS_TO_BUILD@" -config.native_target = "@LLVM_NATIVE_ARCH@" -config.llvm_bindings = "@LLVM_BINDINGS@".split(' ') -config.host_os = "@HOST_OS@" -config.host_cc = "@HOST_CC@" -config.host_cxx = "@HOST_CXX@" -config.enable_libcxx = "@LLVM_ENABLE_LIBCXX@" -# Note: ldflags can contain double-quoted paths, so must use single quotes here. -config.host_ldflags = '@HOST_LDFLAGS@' -config.llvm_use_sanitizer = "@LLVM_USE_SANITIZER@" -config.llvm_host_triple = '@LLVM_HOST_TRIPLE@' -config.host_arch = "@HOST_ARCH@" -config.standalone_src_root = "@CMAKE_SOURCE_DIR@" +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@" -# Support substitution of the tools_dir with user parameters. This is -# used when we can't determine the tool dir at configuration time. -try: - config.llvm_tools_dir = config.llvm_tools_dir % lit_config.params - config.llvm_lib_dir = config.llvm_lib_dir % lit_config.params - config.llvm_shlib_dir = config.llvm_shlib_dir % lit_config.params -except KeyError: - e = sys.exc_info()[1] - key, = e.args - lit_config.fatal("unable to find %r parameter, use '--param=%s=VALUE'" % (key,key)) - - import lit.llvm lit.llvm.initialize(lit_config, config) diff --git a/test/python/lit.local.cfg b/test/python/lit.local.cfg new file mode 100644 index 0000000..b70b9d7 --- /dev/null +++ b/test/python/lit.local.cfg @@ -0,0 +1,4 @@ +config.suffixes.add('.py') + +if not config.enable_bindings_python: + config.unsupported = True diff --git a/test/python/smoketest.py b/test/python/smoketest.py new file mode 100644 index 0000000..0d8f41c --- /dev/null +++ b/test/python/smoketest.py @@ -0,0 +1,17 @@ +# RUN: %python %s | FileCheck %s + +from mlir_standalone.ir import * +from mlir_standalone.dialects import ( + builtin as builtin_d, + standalone as standalone_d +) + +with Context(): + 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)) -- cgit