diff --git a/.github/workflows/buildAndTest.yml b/.github/workflows/buildAndTest.yml index beac33c912..82507e0b29 100644 --- a/.github/workflows/buildAndTest.yml +++ b/.github/workflows/buildAndTest.yml @@ -168,6 +168,7 @@ jobs: cmake .. \ -GNinja \ -DCMAKE_BUILD_TYPE=Debug \ + -DCMAKE_PLATFORM_NO_VERSIONED_SONAME=ON \ -DCMAKE_VISIBILITY_INLINES_HIDDEN=ON \ -DCMAKE_C_VISIBILITY_PRESET=hidden \ -DCMAKE_CXX_VISIBILITY_PRESET=hidden \ @@ -195,6 +196,10 @@ jobs: cd build_release cmake .. \ -DCMAKE_BUILD_TYPE=Release \ + -DCMAKE_PLATFORM_NO_VERSIONED_SONAME=ON \ + -DCMAKE_VISIBILITY_INLINES_HIDDEN=ON \ + -DCMAKE_C_VISIBILITY_PRESET=hidden \ + -DCMAKE_CXX_VISIBILITY_PRESET=hidden \ -DAIE_COMPILER=NONE \ -DAIE_LINKER=NONE \ -DHOST_COMPILER=NONE \ diff --git a/CMakeLists.txt b/CMakeLists.txt index bb990fd6e4..f74481285a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -50,6 +50,11 @@ project(AIE LANGUAGES CXX C) set(CMAKE_CXX_STANDARD 17) set(CMAKE_CXX_STANDARD_REQUIRED YES) +# Disables generation of "version soname" (i.e. libFoo.so.), which +# causes pure duplication as part of Python wheels. +set(CMAKE_PLATFORM_NO_VERSIONED_SONAME ON) + + set(AIE_COMPILER "XCHESSCC" CACHE STRING "Backend compiler selection") @@ -207,10 +212,9 @@ if(NOT LLVM_INSTALL_TOOLCHAIN_ONLY) endif() endif() -add_subdirectory(cmake/modules) # Last because each of these has its own CMakeLists.txt which reloads/re-finds LLVM, thus resettings globals. add_subdirectory(reference_designs) add_subdirectory(test) add_subdirectory(tutorials) - +add_subdirectory(cmake/modules) diff --git a/python/CMakeLists.txt b/python/CMakeLists.txt index 8b3d49a520..9b3bf2b78f 100644 --- a/python/CMakeLists.txt +++ b/python/CMakeLists.txt @@ -53,20 +53,20 @@ get_property(MLIR_EXTENSION_LIBS GLOBAL PROPERTY MLIR_EXTENSION_LIBS) get_property(MLIR_TRANSLATION_LIBS GLOBAL PROPERTY MLIR_TRANSLATION_LIBS) get_property(MLIR_UPSTREAM_CAPI_LIBS GLOBAL PROPERTY MLIR_UPSTREAM_CAPI_LIBS) -get_target_property(CoreSources MLIRPythonExtension.Core INTERFACE_SOURCES) - -get_target_property(DialectsLinalgPybindSources MLIRPythonExtension.Dialects.Linalg.Pybind INTERFACE_SOURCES) -get_target_property(DialectsQuantPybindSources MLIRPythonExtension.Dialects.Quant.Pybind INTERFACE_SOURCES) -get_target_property(DialectsPDLPybindSources MLIRPythonExtension.Dialects.PDL.Pybind INTERFACE_SOURCES) -get_target_property(DialectsSparseTensorPybindSources MLIRPythonExtension.Dialects.SparseTensor.Pybind INTERFACE_SOURCES) -get_target_property(DialectsTransformPybindSources MLIRPythonExtension.Dialects.Transform.Pybind INTERFACE_SOURCES) - -get_target_property(AsyncDialectPassesSources MLIRPythonExtension.AsyncDialectPasses INTERFACE_SOURCES) -get_target_property(ExecutionEngineSources MLIRPythonExtension.ExecutionEngine INTERFACE_SOURCES) -get_target_property(GPUDialectPassesSources MLIRPythonExtension.GPUDialectPasses INTERFACE_SOURCES) -get_target_property(SparseTensorDialectPassesSources MLIRPythonExtension.SparseTensorDialectPasses INTERFACE_SOURCES) get_target_property(RegisterEverythingSources MLIRPythonExtension.RegisterEverything INTERFACE_SOURCES) +_flatten_mlir_python_targets(mlir_python_sources MLIRPythonSources) +set(_mlir_python_cpp_sources) +foreach(s ${mlir_python_sources}) + get_target_property(_sources ${s} INTERFACE_SOURCES) + list(FILTER _sources INCLUDE REGEX "(\.cpp|\.h)$") + if(_sources) + list(APPEND _mlir_python_cpp_sources ${_sources}) + endif() +endforeach() + +list(SORT _mlir_python_cpp_sources) + declare_mlir_python_extension(AIEPythonExtensions MODULE_NAME _aie ROOT_DIR "/" @@ -75,18 +75,7 @@ declare_mlir_python_extension(AIEPythonExtensions ${CMAKE_CURRENT_SOURCE_DIR}/AIEMLIRModule.cpp ${CMAKE_CURRENT_SOURCE_DIR}/PythonPass.cpp - ${CoreSources} - - ${DialectsLinalgPybindSources} - ${DialectsQuantPybindSources} - ${DialectsPDLPybindSources} - ${DialectsSparseTensorPybindSources} - ${DialectsTransformPybindSources} - - ${AsyncDialectPassesSources} - ${ExecutionEngineSources} - ${GPUDialectPassesSources} - ${SparseTensorDialectPassesSources} + ${_mlir_python_cpp_sources} ${RegisterEverythingSources} PRIVATE_LINK_LIBS @@ -104,6 +93,7 @@ list(FILTER mlir_python_sources_deps INCLUDE REGEX "^MLIRPythonSources.Dialects" foreach(t ${mlir_python_sources_deps}) set_property(TARGET ${t} PROPERTY mlir_python_DEPENDS "") endforeach() +set_property(TARGET MLIRPythonSources.ExecutionEngine PROPERTY mlir_python_DEPENDS "") add_mlir_python_modules(AIEMLIRPythonModules ROOT_PREFIX "${AIE_PYTHON_PACKAGES_DIR}/aie" @@ -111,6 +101,7 @@ add_mlir_python_modules(AIEMLIRPythonModules DECLARED_SOURCES ${mlir_python_sources_deps} MLIRPythonSources.Core.Python + MLIRPythonSources.ExecutionEngine AIEPythonExtensions ) @@ -159,3 +150,30 @@ foreach(ext ${_other_extensions}) )" ) endforeach() + +set(_runtimes + mlir_async_runtime + mlir_c_runner_utils + mlir_float16_utils + mlir_runner_utils +) + +if (TARGET mlir_rocm_runtime) + list(APPEND _runtimes mlir_rocm_runtime) +endif() + +foreach(r ${_runtimes}) + add_custom_command( + TARGET AIEPythonModules PRE_BUILD + COMMAND ${CMAKE_COMMAND} -E copy + $ + ${CMAKE_BINARY_DIR}/lib + ) +endforeach() + +install(IMPORTED_RUNTIME_ARTIFACTS + ${_runtimes} + COMPONENT aie-python + LIBRARY DESTINATION "python/aie/_mlir_libs" +) + diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 437202ba34..fc3bb17186 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -1,108 +1,3 @@ -# -# 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 -# -# (c) Copyright 2021 Xilinx Inc. - -cmake_minimum_required(VERSION 3.10) - -if(POLICY CMP0074) - cmake_policy(SET CMP0074 NEW) -endif() - -if(POLICY CMP0068) - cmake_policy(SET CMP0068 NEW) - set(CMAKE_BUILD_WITH_INSTALL_NAME_DIR ON) -endif() - -if(POLICY CMP0075) - cmake_policy(SET CMP0075 NEW) -endif() - -if(POLICY CMP0077) - cmake_policy(SET CMP0077 NEW) -endif() - -project(aie-test LANGUAGES CXX C) - -# find package AIE if running tests from AIE installation -if(NOT AIE_BINARY_DIR) - find_package(AIE REQUIRED CONFIG) - set(LibXAIE_${AIE_RUNTIME_TEST_TARGET}_DIR CACHE STRING "") #pick up libxaiengine from installation folder if no other location specified -endif() - -# default to x86_64 system architecture for runtime target -set(AIE_RUNTIME_TARGETS "x86_64" CACHE STRING "Architectures to compile the runtime libraries for.") -list(GET AIE_RUNTIME_TARGETS 0 firstRuntimeTarget) -set(AIE_RUNTIME_TEST_TARGET ${firstRuntimeTarget} CACHE STRING "Runtime architecture to test with.") - -set(CMAKE_CXX_STANDARD 14) -set(CMAKE_CXX_STANDARD_REQUIRED YES) - -option(LLVM_INCLUDE_TOOLS "Generate build targets for the LLVM tools." ON) -option(LLVM_BUILD_TOOLS "Build the LLVM tools. If OFF, just generate build targets." ON) - -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}) - -find_package(Vitis 2023.2 COMPONENTS AIE AIE2) -find_package(Python3 COMPONENTS Interpreter) - -if(Vitis_FOUND) - set(DEFAULT_ENABLE_CHESS_TESTS ON) -else() - set(DEFAULT_ENABLE_CHESS_TESTS OFF) -endif() -option(ENABLE_CHESS_TESTS "Enable backend tests using xchesscc" ${DEFAULT_ENABLE_CHESS_TESTS}) - -if(${CMAKE_HOST_SYSTEM_PROCESSOR} STREQUAL aarch64) - set(DEFAULT_ENABLE_BOARD_TESTS ON) -else() - set(DEFAULT_ENABLE_BOARD_TESTS OFF) -endif() -option(ENABLE_BOARD_TESTS "Enable board tests" ${DEFAULT_ENABLE_BOARD_TESTS}) - -# Look for LibXAIE -if (DEFINED LibXAIE_${AIE_RUNTIME_TEST_TARGET}_DIR) - message("Test using xaiengine from LibXAIE_${AIE_RUNTIME_TEST_TARGET}_DIR=${LibXAIE_${AIE_RUNTIME_TEST_TARGET}_DIR}") - set(LibXAIE_ROOT ${LibXAIE_${target}_DIR}) - find_package(LibXAIE) -else() - if(DEFINED VITIS_ROOT) - message(STATUS "Test has Vitis available, no libxaie location specified so pick up from build area") - set(LibXAIE_FOUND TRUE) - endif() -endif() - -# Define the default arguments to use with 'lit', and an option for the user to -# override. -set(LIT_ARGS_DEFAULT "-sv") -if (MSVC_IDE OR XCODE) - set(LIT_ARGS_DEFAULT "${LIT_ARGS_DEFAULT} --no-progress-bar") -endif() -set(LLVM_LIT_ARGS "${LIT_ARGS_DEFAULT}" CACHE STRING "Default options for lit") - -list(APPEND CMAKE_MODULE_PATH "${MLIR_CMAKE_DIR}") -list(APPEND CMAKE_MODULE_PATH "${LLVM_CMAKE_DIR}") - -include(TableGen) -include(AddLLVM) -include(AddMLIR) -include(HandleLLVMOptions) - -include_directories(${LLVM_INCLUDE_DIRS}) -include_directories(${MLIR_INCLUDE_DIRS}) -include_directories(${PROJECT_SOURCE_DIR}/include) -include_directories(${PROJECT_BINARY_DIR}/include) -add_definitions(${LLVM_DEFINITIONS}) - if(ENABLE_CHESS_TESTS) set(CONFIG_ENABLE_CHESS_TESTS 1) else() @@ -134,13 +29,14 @@ set(TEST_DEPENDS aie-opt aie-translate AIEPythonModules - ) +) add_lit_testsuite(check-aie "Running the aie regression tests" ${CMAKE_CURRENT_BINARY_DIR} DEPENDS ${TEST_DEPENDS} ARGS "-sv --timeout 600 --time-tests --show-unsupported" - ) +) + set_target_properties(check-aie PROPERTIES FOLDER "Tests") add_lit_testsuites(AIE ${CMAKE_CURRENT_BINARY_DIR} DEPENDS ${TEST_DEPENDS} ARGS "-sv --timeout 600 --time-tests --show-unsupported") diff --git a/test/lit.local.cfg b/test/lit.local.cfg deleted file mode 100644 index 0a1cd9b709..0000000000 --- a/test/lit.local.cfg +++ /dev/null @@ -1,8 +0,0 @@ -# -# 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 -# -# (c) Copyright 2022 Xilinx Inc. - -config.unsupported = [] diff --git a/test/lit.site.cfg.py.in b/test/lit.site.cfg.py.in index ba4496f68a..e2f89c0615 100644 --- a/test/lit.site.cfg.py.in +++ b/test/lit.site.cfg.py.in @@ -71,4 +71,4 @@ import lit.llvm lit.llvm.initialize(lit_config, config) # Let the main config do the real work. -lit_config.load_config(config, "@PROJECT_SOURCE_DIR@/lit.cfg.py") +lit_config.load_config(config, "@PROJECT_SOURCE_DIR@/test/lit.cfg.py") diff --git a/test/python/execution_engine.py b/test/python/execution_engine.py new file mode 100644 index 0000000000..88017757b6 --- /dev/null +++ b/test/python/execution_engine.py @@ -0,0 +1,161 @@ +# RUN: %PYTHON %s 2>&1 | FileCheck %s + +import gc, sys, os, tempfile +from aie.ir import * +from aie.passmanager import * +from aie.execution_engine import * +from aie.runtime import * + + +# Log everything to stderr and flush so that we have a unified stream to match +# errors/info emitted by MLIR to stderr. +def log(*args): + print(*args, file=sys.stderr) + sys.stderr.flush() + + +def run(f): + log("\nTEST:", f.__name__) + f() + gc.collect() + assert Context._get_live_count() == 0 + + +def lowerToLLVM(module): + pm = PassManager.parse( + "builtin.module(convert-complex-to-llvm,finalize-memref-to-llvm,convert-func-to-llvm,reconcile-unrealized-casts)" + ) + pm.run(module.operation) + return module + + +# Test loading of shared libraries. +# CHECK-LABEL: TEST: testSharedLibLoad +def testSharedLibLoad(): + with Context(): + module = Module.parse( + """ + module { + func.func @main(%arg0: memref<1xf32>) attributes { llvm.emit_c_interface } { + %c0 = arith.constant 0 : index + %cst42 = arith.constant 42.0 : f32 + memref.store %cst42, %arg0[%c0] : memref<1xf32> + %u_memref = memref.cast %arg0 : memref<1xf32> to memref<*xf32> + call @printMemrefF32(%u_memref) : (memref<*xf32>) -> () + return + } + func.func private @printMemrefF32(memref<*xf32>) attributes { llvm.emit_c_interface } + } """ + ) + arg0 = np.array([0.0]).astype(np.float32) + + arg0_memref_ptr = ctypes.pointer( + ctypes.pointer(get_ranked_memref_descriptor(arg0)) + ) + + if sys.platform == "win32": + shared_libs = [ + "../../bin/mlir_runner_utils.dll", + "../../bin/mlir_c_runner_utils.dll", + ] + elif sys.platform == "darwin": + shared_libs = [ + "../../lib/libmlir_runner_utils.dylib", + "../../lib/libmlir_c_runner_utils.dylib", + ] + else: + shared_libs = [ + "../../lib/libmlir_runner_utils.so", + "../../lib/libmlir_c_runner_utils.so", + ] + + execution_engine = ExecutionEngine( + lowerToLLVM(module), opt_level=3, shared_libs=shared_libs + ) + execution_engine.invoke("main", arg0_memref_ptr) + # CHECK: Unranked Memref + # CHECK-NEXT: [42] + + +run(testSharedLibLoad) + + +# Test that nano time clock is available. +# CHECK-LABEL: TEST: testNanoTime +def testNanoTime(): + with Context(): + module = Module.parse( + """ + module { + func.func @main() attributes { llvm.emit_c_interface } { + %now = call @nanoTime() : () -> i64 + %memref = memref.alloca() : memref<1xi64> + %c0 = arith.constant 0 : index + memref.store %now, %memref[%c0] : memref<1xi64> + %u_memref = memref.cast %memref : memref<1xi64> to memref<*xi64> + call @printMemrefI64(%u_memref) : (memref<*xi64>) -> () + return + } + func.func private @nanoTime() -> i64 attributes { llvm.emit_c_interface } + func.func private @printMemrefI64(memref<*xi64>) attributes { llvm.emit_c_interface } + }""" + ) + + if sys.platform == "win32": + shared_libs = [ + "../../bin/mlir_runner_utils.dll", + "../../bin/mlir_c_runner_utils.dll", + ] + else: + shared_libs = [ + "../../lib/libmlir_runner_utils.so", + "../../lib/libmlir_c_runner_utils.so", + ] + + execution_engine = ExecutionEngine( + lowerToLLVM(module), opt_level=3, shared_libs=shared_libs + ) + execution_engine.invoke("main") + # CHECK: Unranked Memref + # CHECK: [{{.*}}] + + +run(testNanoTime) + + +# Test that nano time clock is available. +# CHECK-LABEL: TEST: testDumpToObjectFile +def testDumpToObjectFile(): + fd, object_path = tempfile.mkstemp(suffix=".o") + + try: + with Context(): + module = Module.parse( + """ + module { + func.func @main() attributes { llvm.emit_c_interface } { + return + } + }""" + ) + + execution_engine = ExecutionEngine(lowerToLLVM(module), opt_level=3) + + # CHECK: Object file exists: True + print(f"Object file exists: {os.path.exists(object_path)}") + # CHECK: Object file is empty: True + print(f"Object file is empty: {os.path.getsize(object_path) == 0}") + + execution_engine.dump_to_object_file(object_path) + + # CHECK: Object file exists: True + print(f"Object file exists: {os.path.exists(object_path)}") + # CHECK: Object file is empty: False + print(f"Object file is empty: {os.path.getsize(object_path) == 0}") + + finally: + os.close(fd) + os.remove(object_path) + + +run(testDumpToObjectFile) diff --git a/utils/build-llvm-cross-local.sh b/utils/build-llvm-cross-local.sh index 2da6e3d73c..8977b214f4 100755 --- a/utils/build-llvm-cross-local.sh +++ b/utils/build-llvm-cross-local.sh @@ -68,6 +68,10 @@ CMAKE_CONFIGS="\ -DCMAKE_BUILD_TYPE=Release \ -DLLVM_BUILD_LLVM_DYLIB=OFF \ -DLLVM_LINK_LLVM_DYLIB=OFF \ + -DCMAKE_PLATFORM_NO_VERSIONED_SONAME=ON \ + -DCMAKE_VISIBILITY_INLINES_HIDDEN=ON \ + -DCMAKE_C_VISIBILITY_PRESET=hidden \ + -DCMAKE_CXX_VISIBILITY_PRESET=hidden \ -DMLIR_BINDINGS_PYTHON_ENABLED=ON \ -DCMAKE_C_IMPLICIT_LINK_LIBRARIES=gcc_s \ -DCMAKE_CXX_IMPLICIT_LINK_LIBRARIES=gcc_s \ diff --git a/utils/build-llvm-local.sh b/utils/build-llvm-local.sh index 100aa581a1..79547719bc 100755 --- a/utils/build-llvm-local.sh +++ b/utils/build-llvm-local.sh @@ -43,6 +43,7 @@ CMAKE_CONFIGS="\ -DLLVM_ENABLE_PROJECTS=mlir \ -DLLVM_TARGETS_TO_BUILD:STRING=X86;ARM;AArch64 \ -DCMAKE_BUILD_TYPE=Release \ + -DCMAKE_PLATFORM_NO_VERSIONED_SONAME=ON \ -DCMAKE_VISIBILITY_INLINES_HIDDEN=ON \ -DCMAKE_C_VISIBILITY_PRESET=hidden \ -DCMAKE_CXX_VISIBILITY_PRESET=hidden \ diff --git a/utils/build-llvm.sh b/utils/build-llvm.sh index 99b8021c24..ff41da4b5f 100755 --- a/utils/build-llvm.sh +++ b/utils/build-llvm.sh @@ -34,6 +34,7 @@ CMAKE_CONFIGS="\ -DLLVM_INSTALL_UTILS=ON \ -DMLIR_ENABLE_BINDINGS_PYTHON=ON \ -DCMAKE_BUILD_TYPE=Release \ + -DCMAKE_PLATFORM_NO_VERSIONED_SONAME=ON \ -DCMAKE_VISIBILITY_INLINES_HIDDEN=ON \ -DCMAKE_C_VISIBILITY_PRESET=hidden \ -DCMAKE_CXX_VISIBILITY_PRESET=hidden \ diff --git a/utils/build-mlir-aie-cross.sh b/utils/build-mlir-aie-cross.sh index 5544d75daf..6f6f571742 100755 --- a/utils/build-mlir-aie-cross.sh +++ b/utils/build-mlir-aie-cross.sh @@ -55,6 +55,7 @@ CMAKE_CONFIGS="\ -DLLVM_ENABLE_RTTI=ON \ -DCMAKE_INSTALL_PREFIX=${INSTALL_DIR} \ -DCMAKE_BUILD_TYPE=Debug \ + -DCMAKE_PLATFORM_NO_VERSIONED_SONAME=ON \ -DCMAKE_VISIBILITY_INLINES_HIDDEN=ON \ -DCMAKE_C_VISIBILITY_PRESET=hidden \ -DCMAKE_CXX_VISIBILITY_PRESET=hidden \ diff --git a/utils/build-mlir-aie.sh b/utils/build-mlir-aie.sh index 4db64e135d..1d9b9edc8b 100755 --- a/utils/build-mlir-aie.sh +++ b/utils/build-mlir-aie.sh @@ -45,6 +45,7 @@ CMAKE_CONFIGS="\ -DCMAKE_MODULE_PATH=${CMAKEMODULES_DIR}/modulesXilinx \ -DCMAKE_INSTALL_PREFIX="../${INSTALL_DIR}" \ -DCMAKE_BUILD_TYPE=Release \ + -DCMAKE_PLATFORM_NO_VERSIONED_SONAME=ON \ -DCMAKE_VISIBILITY_INLINES_HIDDEN=ON \ -DCMAKE_C_VISIBILITY_PRESET=hidden \ -DCMAKE_CXX_VISIBILITY_PRESET=hidden \