From e118a0020937a02d4a94999ba981753a7b302dbc Mon Sep 17 00:00:00 2001 From: Maksim Levental Date: Tue, 14 May 2024 19:50:20 -0700 Subject: [PATCH] Xrt loading (#8) --- .github/workflows/base/action.yml | 47 +- .github/workflows/tests.yml | 65 +- .github/workflows/wheels.yml | 57 +- .gitignore | 17 +- .gitmodules | 3 + CMakeLists.txt | 221 ++++-- README.md | 38 +- examples/{e2e.py => cdo.py} | 82 ++- examples/load_xrt.py | 240 +++++++ examples/pi.xclbin | Bin 7411 -> 0 bytes examples/xrt.py | 13 +- include/elf.h | 265 ------- include/elftypes.h | 47 -- patches/aie-rt.diff | 23 + patches/apply_patches.sh | 29 + patches/bootgen.diff | 66 ++ patches/pyxrt.diff | 32 + pyproject.toml | 14 +- requirements-dev.txt | 8 +- requirements.txt | 1 - setup.py | 35 +- tests/test_basic.py | 1 + tests/{test_e2e.py => test_cdo.py} | 120 ++-- third_party/XRT | 2 +- third_party/bootgen | 1 + util/FindOpenSSL.cmake | 804 ++++++++++++++++++++++ {scripts => util}/gen_cdo.py | 0 {scripts => util}/gen_xaie_ctypes.py | 12 +- scripts/stuff.cmake => util/helpers.cmake | 38 +- util/remove_path_limit.ps1 | 2 + xaiepy/.gitignore | 5 +- xaiepy/bootgen.cpp | 23 + xaiepy/bootgen.py | 22 + xaiepy/cdo.py | 281 +++----- xaiepy/xclbinutil.cpp | 25 + xaiepy/xclbinutil.py | 116 ++++ 36 files changed, 1984 insertions(+), 771 deletions(-) rename examples/{e2e.py => cdo.py} (60%) create mode 100644 examples/load_xrt.py delete mode 100644 examples/pi.xclbin delete mode 100644 include/elf.h delete mode 100644 include/elftypes.h create mode 100644 patches/aie-rt.diff create mode 100755 patches/apply_patches.sh create mode 100644 patches/bootgen.diff create mode 100644 patches/pyxrt.diff rename tests/{test_e2e.py => test_cdo.py} (51%) create mode 160000 third_party/bootgen create mode 100644 util/FindOpenSSL.cmake rename {scripts => util}/gen_cdo.py (100%) rename {scripts => util}/gen_xaie_ctypes.py (97%) rename scripts/stuff.cmake => util/helpers.cmake (100%) create mode 100644 util/remove_path_limit.ps1 create mode 100644 xaiepy/bootgen.cpp create mode 100644 xaiepy/bootgen.py create mode 100644 xaiepy/xclbinutil.cpp create mode 100644 xaiepy/xclbinutil.py diff --git a/.github/workflows/base/action.yml b/.github/workflows/base/action.yml index 82e76ef..58a7cca 100644 --- a/.github/workflows/base/action.yml +++ b/.github/workflows/base/action.yml @@ -7,6 +7,9 @@ inputs: ARCH: description: 'matrix.arch' required: true + PY_VERSION: + description: 'matrix.py_version' + required: true description: '' @@ -14,6 +17,9 @@ outputs: BOOST_ROOT: description: '' value: ${{ steps.install_boost.outputs.BOOST_ROOT }} + BOOST_VERSION: + description: '' + value: 1.74.0 runs: using: "composite" @@ -22,12 +28,16 @@ runs: uses: aminya/setup-cpp@v1 with: compiler: gcc - vcvarsall: ${{ contains(inputs.OS, 'windows') }} + vcvarsall: ${{ contains(inputs.OS, 'windows') }} # evaluate to the string 'true' msvc: ${{ contains(inputs.OS, 'windows') }} choco: ${{ contains(inputs.OS, 'windows') }} cmake: true ninja: true + - uses: actions/setup-python@v4 + with: + python-version: ${{ inputs.PY_VERSION }} + - uses: makslevental/mlir-wheels/actions/setup_ccache@d77bf5dc69c46a8c2738b44528749768888eb361 id: setup_ccache with: @@ -36,8 +46,39 @@ runs: WORKSPACE_ROOT: ${{ github.workspace }} - name: Install boost for XRT - if: contains(inputs.OS, 'windows') || contains(inputs.OS, 'ubuntu') uses: MarkusJx/install-boost@v2.4.5 id: install_boost with: - boost_version: 1.73.0 \ No newline at end of file + boost_version: 1.74.0 + + - name: Build and install system deps + shell: bash + run: | + + if [ ${{ inputs.OS }} == 'windows-2019' ]; then + choco install -y pkgconfiglite protoc + else + sudo apt install -y libdrm-dev ocl-icd-opencl-dev rapidjson-dev libprotobuf-dev systemtap-sdt-dev uuid-dev python3-dev + fi + + - name: Remove path limit and export VCTOOLSVERSION + if: contains(inputs.OS, 'windows') + shell: pwsh + run: | + + pwsh.exe .\util\remove_path_limit.ps1 + + - name: export VCTOOLSVERSION + if: contains(inputs.OS, 'windows') + shell: bash + run: | + + VCTOOLSVERSION=${VCTOOLSVERSION:0:4} + echo "VCTOOLSVERSION=$VCTOOLSVERSION" >> $GITHUB_ENV + + - name: Install python deps + shell: bash + run: | + + ls -lah + pip install -r requirements-dev.txt diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 3e2869c..cb85b59 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -20,7 +20,7 @@ jobs: strategy: fail-fast: false matrix: - OS: [ ubuntu-20.04, windows-2019, macos-12 ] + OS: [ ubuntu-20.04, windows-2019 ] PY_VERSION: [ "3.8", "3.9", "3.10", "3.11", "3.12" ] defaults: @@ -28,12 +28,13 @@ jobs: shell: bash steps: + - name: Checkout actions uses: actions/checkout@v3 with: submodules: true - - name: Get ELFIO + - name: Get Submodules run: | git submodule update --init --recursive @@ -43,29 +44,18 @@ jobs: with: OS: ${{ matrix.OS }} ARCH: ${{ matrix.ARCH }} - - - uses: actions/setup-python@v4 - with: - python-version: ${{ matrix.PY_VERSION }} - - - name: Build and install protoc - if: contains(matrix.OS, 'windows') || contains(matrix.OS, 'ubuntu') - run: | - - if [ ${{ matrix.OS }} == 'windows-2019' ]; then - choco install -y pkgconfiglite protoc - else - sudo apt install libdrm-dev ocl-icd-opencl-dev rapidjson-dev libprotobuf-dev systemtap-sdt-dev - fi + PY_VERSION: ${{ matrix.PY_VERSION }} - name: build wheel env: BOOST_ROOT: ${{ steps.base.outputs.BOOST_ROOT }} - BOOST_VERSION: 1.73.0 + BOOST_VERSION: ${{ steps.base.outputs.BOOST_VERSION }} run: | - pip install -r requirements-dev.txt - pip wheel . -v -w wheelhouse --no-build-isolation + REPO_SRC_DIR=$PWD bash patches/apply_patches.sh + CIBW_BUILD=${{ matrix.PY_VERSION }} + export CIBW_BUILD="cp${CIBW_BUILD//./}-*" + cibuildwheel --output-dir wheelhouse - name: test run: | @@ -82,9 +72,9 @@ jobs: - name: Test mwe shell: bash run: | - + pushd examples - python e2e.py + python cdo.py popd test-linux-aarch64: @@ -97,51 +87,39 @@ jobs: PY_VERSION: [ "3.8", "3.9", "3.10", "3.11", "3.12" ] steps: + - name: Checkout actions uses: actions/checkout@v3 with: submodules: true - - name: Get ELFIO + - name: Get Submodules run: | git submodule update --init --recursive - - name: Setup Cpp - uses: aminya/setup-cpp@v1 - with: - compiler: gcc - cmake: true - ninja: true - - name: Set up QEMU uses: docker/setup-qemu-action@v2 with: platforms: aarch64 - - uses: actions/setup-python@v4 - with: - python-version: ${{ matrix.PY_VERSION }} - - uses: ./.github/workflows/base id: base with: OS: ${{ matrix.OS }} ARCH: ${{ matrix.ARCH }} - - - name: Install XRT deps - run: | - - sudo apt install libdrm-dev ocl-icd-opencl-dev rapidjson-dev libprotobuf-dev systemtap-sdt-dev + PY_VERSION: ${{ matrix.PY_VERSION }} - name: build wheel env: BOOST_ROOT: ${{ steps.base.outputs.BOOST_ROOT }} - BOOST_VERSION: 1.73.0 + BOOST_VERSION: ${{ steps.base.outputs.BOOST_VERSION }} run: | - pip install -r requirements-dev.txt - pip wheel . -v -w wheelhouse --no-build-isolation + REPO_SRC_DIR=$PWD bash patches/apply_patches.sh + CIBW_BUILD=${{ matrix.PY_VERSION }} + export CIBW_BUILD="cp${CIBW_BUILD//./}-*" + cibuildwheel --output-dir wheelhouse - name: test run: | @@ -154,7 +132,8 @@ jobs: - name: Test mwe shell: bash run: | - + + export LD_LIBRARY_PATH=/opt/xilinx/xrt/lib pushd examples - python e2e.py + python cdo.py popd diff --git a/.github/workflows/wheels.yml b/.github/workflows/wheels.yml index 0b1bcef..da6215c 100644 --- a/.github/workflows/wheels.yml +++ b/.github/workflows/wheels.yml @@ -5,6 +5,12 @@ on: push: branches: - main + pull_request: + types: [assigned, opened, synchronize, reopened] + +concurrency: + group: test-${{ github.event.number || github.sha }} + cancel-in-progress: true jobs: @@ -23,12 +29,6 @@ jobs: - OS: windows-2019 ARCH: AMD64 - - OS: macos-12 - ARCH: x86_64 - - - OS: macos-12 - ARCH: arm64 - defaults: run: shell: bash @@ -40,34 +40,29 @@ jobs: with: submodules: true - - name: Get ELFIO + - name: Get Submodules run: | git submodule update --init --recursive - uses: ./.github/workflows/base - if: contains(matrix.OS, 'windows') id: base with: OS: ${{ matrix.OS }} ARCH: ${{ matrix.ARCH }} - - - name: Build and install protoc - if: contains(matrix.OS, 'windows') - run: | - - choco install -y pkgconfiglite protoc + PY_VERSION: '3.10' # build - name: cibuildwheel python bindings env: BOOST_ROOT: ${{ steps.base.outputs.BOOST_ROOT }} - BOOST_VERSION: 1.73.0 + BOOST_VERSION: ${{ steps.base.outputs.BOOST_VERSION }} run: | - pip install cibuildwheel - cibuildwheel --output-dir wheelhouse + REPO_SRC_DIR=$PWD bash patches/apply_patches.sh + CIBW_BUILD="cp38-* cp39-* cp310-* cp311-* cp312-*" \ + cibuildwheel --output-dir wheelhouse # done @@ -85,31 +80,39 @@ jobs: steps: + - name: Set up QEMU + uses: docker/setup-qemu-action@v2 + with: + platforms: aarch64 + - name: Checkout actions uses: actions/checkout@v3 with: submodules: true - - name: Get ELFIO + - name: Get Submodules run: | git submodule update --init --recursive - - name: Set up QEMU - uses: docker/setup-qemu-action@v2 + - uses: ./.github/workflows/base + id: base with: - platforms: aarch64 + OS: ubuntu-20.04 + ARCH: aarch64 + PY_VERSION: '3.10' # build - name: cibuildwheel python bindings aarch64 env: BOOST_ROOT: ${{ steps.base.outputs.BOOST_ROOT }} - BOOST_VERSION: 1.73.0 + BOOST_VERSION: ${{ steps.base.outputs.BOOST_VERSION }} run: | - pip install cibuildwheel - cibuildwheel --output-dir wheelhouse + REPO_SRC_DIR=$PWD bash patches/apply_patches.sh + CIBW_BUILD="cp38-* cp39-* cp310-* cp311-* cp312-*" \ + cibuildwheel --output-dir wheelhouse # done @@ -121,7 +124,7 @@ jobs: upload_bindings_wheels: - if: github.event_name != 'pull_request' && github.ref_name == 'main' + if: github.event_name != 'pull_request' needs: [build, build-linux-aarch64] @@ -144,8 +147,8 @@ jobs: with: artifacts: "dist/*.whl" token: "${{ secrets.GITHUB_TOKEN }}" - tag: "latest" - name: "latest" + tag: ${{ github.ref_name == 'main' && 'release' || 'dev' }} + name: ${{ github.ref_name == 'main' && 'release' || 'dev' }} removeArtifacts: false allowUpdates: true replacesArtifacts: true diff --git a/.gitignore b/.gitignore index df58d7d..02ff0d1 100644 --- a/.gitignore +++ b/.gitignore @@ -159,5 +159,18 @@ cython_debug/ # and can be added to the global gitignore or merged into this file. For a more nuclear # option (not recommended) you can uncomment the following to ignore the entire idea folder. .idea/ -cmake-build-debug -cmake-build-release +cmake-build-* +.envrc +*.whl +*.zip +*.ilk +*.pyd +*.lib +*.exp +*.pdb +*.dll +*.json +*.pdi +*.bif +*.bin +*.xclbin diff --git a/.gitmodules b/.gitmodules index 162ae50..42ab2e0 100644 --- a/.gitmodules +++ b/.gitmodules @@ -4,3 +4,6 @@ [submodule "third_party/XRT"] path = third_party/XRT url = git@github.com:Xilinx/XRT.git +[submodule "third_party/bootgen"] + path = third_party/bootgen + url = git@github.com:Xilinx/bootgen.git diff --git a/CMakeLists.txt b/CMakeLists.txt index 5b69e1f..7575e73 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -2,8 +2,6 @@ cmake_minimum_required(VERSION 3.15) project(xaiepy) set(CMAKE_CXX_STANDARD 17) -include(collect) - if(POLICY CMP0144) # respect all caps _ROOT environment variable to search for # package stuff @@ -14,9 +12,83 @@ if(POLICY CMP0148) cmake_policy(SET CMP0148 NEW) endif() -include(scripts/stuff.cmake) +if(WIN32) + # see util/remove_path_limit.ps1 + set(CMAKE_OBJECT_PATH_MAX 4096) +endif() + +set(OUTPUT_DIR + ${CMAKE_CURRENT_BINARY_DIR} + CACHE STRING "Output directory") + +# ############################################################################## +# requirements +# ############################################################################## + +if(NOT ${CMAKE_SIZEOF_VOID_P} EQUAL 8) + # if you open up the wrong developer shell in windows... + message( + FATAL_ERROR + "Something has gone terribly wrong: CMAKE_SIZEOF_VOID_P=${CMAKE_SIZEOF_VOID_P}" + ) +endif() +# can delete after this is all worked out +# https://gitlab.kitware.com/cmake/cmake/-/issues/25702 +find_package(OpenSSL) +if(NOT DEFINED OPENSSL_FOUND OR NOT ${OPENSSL_FOUND}) + list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}/util") + find_package(OpenSSL) + if(NOT DEFINED USE_OUR_FIND_OPENSSL OR NOT ${USE_OUR_FIND_OPENSSL}) + message(FATAL_ERROR "Didn't pickup our FindOpenSSL.cmake") + endif() + if(NOT DEFINED OPENSSL_FOUND OR NOT ${OPENSSL_FOUND}) + message(FATAL_ERROR "OpenSSL not found") + endif() +endif() +message(STATUS "OpenSSL include directories:" ${OPENSSL_INCLUDE_DIR}) + +include(util/helpers.cmake) configure_python_dev_packages() +find_package(Threads REQUIRED) +find_package(Boost REQUIRED COMPONENTS system program_options filesystem) +message(STATUS "Boost include directories:" ${Boost_INCLUDE_DIRS}) + +if(NOT WIN32) + find_package(RapidJSON REQUIRED) +endif() + +# ############################################################################## +# bootgen +# ############################################################################## + +set(BOOTGEN_SOURCE_DIR ${PROJECT_SOURCE_DIR}/third_party/bootgen) +file(GLOB BOOTGEN_SOURCES "${BOOTGEN_SOURCE_DIR}/*.c" + "${BOOTGEN_SOURCE_DIR}/*.cpp") +list(REMOVE_ITEM BOOTGEN_SOURCES "${BOOTGEN_SOURCE_DIR}/main.cpp") + +add_library(bootgen-lib STATIC ${BOOTGEN_SOURCES}) +if(CMAKE_CXX_COMPILER_ID MATCHES "MSVC") + target_compile_definitions(bootgen-lib PUBLIC YY_NO_UNISTD_H) +endif() +target_include_directories(bootgen-lib PUBLIC ${BOOTGEN_SOURCE_DIR} + ${OPENSSL_INCLUDE_DIR}) +target_compile_definitions(bootgen-lib PUBLIC OPENSSL_USE_APPLINK) +target_link_libraries(bootgen-lib PUBLIC OpenSSL::SSL OpenSSL::applink) + +add_library(cdo_driver STATIC ${BOOTGEN_SOURCE_DIR}/cdo-driver/cdo_driver.c) +target_include_directories(cdo_driver PUBLIC ${BOOTGEN_SOURCE_DIR}/cdo-driver) +set_target_properties(cdo_driver PROPERTIES LINKER_LANGUAGE C) + +pybind11_add_module(_bootgen xaiepy/bootgen.cpp) +target_include_directories(_bootgen PUBLIC ${BOOTGEN_SOURCE_DIR}) +target_link_libraries(_bootgen PUBLIC bootgen-lib) + +# ############################################################################## +# aie-rt +# ############################################################################## + +include(third_party/aie-rt/fal/cmake/collect.cmake) set(AIERT_SRC_DIR ${PROJECT_SOURCE_DIR}/third_party/aie-rt/driver/src) # gotta add the subdirectory so the copies to build/include/xaiengine occur... add_subdirectory(${AIERT_SRC_DIR}) @@ -26,70 +98,105 @@ if(uppercase_CMAKE_BUILD_TYPE STREQUAL "DEBUG") set(XAIE_DEBUG "XAIE_DEBUG") endif() -target_compile_options(aienginev2 PRIVATE -Wall -Wextra -D__AIECDO__) target_compile_definitions(aienginev2 PUBLIC ${XAIE_DEBUG}) get_target_property(AIERT_SRCS aienginev2 SOURCES) list(TRANSFORM AIERT_SRCS PREPEND ${AIERT_SRC_DIR}/) +# elf.h +target_include_directories(aienginev2 PUBLIC SYSTEM ${BOOTGEN_SOURCE_DIR}) get_target_property(AIERT_INCLUDE_DIRECTORIES aienginev2 INCLUDE_DIRECTORIES) add_library(xaie SHARED ${AIERT_SRCS}) -set_target_properties(xaie PROPERTIES LINKER_LANGUAGE C) target_compile_definitions(xaie PUBLIC ${XAIE_DEBUG} __AIECDO__) target_include_directories(xaie PUBLIC ${AIERT_INCLUDE_DIRECTORIES}) -target_include_directories(xaie PUBLIC SYSTEM ${PROJECT_SOURCE_DIR}/include) -if(${CMAKE_SYSTEM_NAME} MATCHES "Darwin") - target_link_options( - xaie - PUBLIC - -Wl,-U,_cdo_BlockSet32 - -Wl,-U,_cdo_BlockWrite32 - -Wl,-U,_cdo_MaskPoll - -Wl,-U,_cdo_MaskWrite32 - -Wl,-U,_cdo_Write32) +set_target_properties(xaie PROPERTIES LINKER_LANGUAGE C) +target_link_libraries(xaie PUBLIC cdo_driver) + +add_dependencies(xaie _bootgen) + +# ############################################################################## +# xclbinutil +# ############################################################################## + +set(XRT_SOURCE_DIR ${PROJECT_SOURCE_DIR}/third_party/XRT) +add_subdirectory(${XRT_SOURCE_DIR}/src) +include_directories(${XRT_SOURCE_DIR}/src/runtime_src/core/include) + +set(XCLBINUTIL_SOURCE_DIR + ${PROJECT_SOURCE_DIR}/third_party/XRT/src/runtime_src/tools/xclbinutil) + +configure_file(${XRT_SOURCE_DIR}/src/CMake/config/version.h.in + ${XRT_BINARY_DIR}/src/gen/version.h) + +file( + GLOB + XCLBINUTIL_SRCS + "${XCLBINUTIL_SOURCE_DIR}/DTC*.cxx" + "${XCLBINUTIL_SOURCE_DIR}/FDT*.cxx" + "${XCLBINUTIL_SOURCE_DIR}/CBOR.cxx" + "${XCLBINUTIL_SOURCE_DIR}/RapidJsonUtilities.cxx" + "${XCLBINUTIL_SOURCE_DIR}/KernelUtilities.cxx" + "${XCLBINUTIL_SOURCE_DIR}/ElfUtilities.cxx" + "${XCLBINUTIL_SOURCE_DIR}/FormattedOutput.cxx" + "${XCLBINUTIL_SOURCE_DIR}/ParameterSectionData.cxx" + "${XCLBINUTIL_SOURCE_DIR}/Section.cxx" # Note: Due to linking dependency + # issue, this entry needs to be before the other sections + "${XCLBINUTIL_SOURCE_DIR}/Section*.cxx" + "${XCLBINUTIL_SOURCE_DIR}/Resources*.cxx" + "${XCLBINUTIL_SOURCE_DIR}/XclBinClass.cxx" + "${XCLBINUTIL_SOURCE_DIR}/XclBinSignature.cxx" + "${XCLBINUTIL_SOURCE_DIR}/XclBinUtilities.cxx") + +# shared not static because static will let linker trim SectionMemTopology +# initializers... +add_library(xclbinutil-lib SHARED ${XCLBINUTIL_SRCS}) + +if(NOT WIN32) + target_link_libraries(xclbinutil-lib PUBLIC crypto) + target_compile_definitions(xclbinutil-lib + PUBLIC ENABLE_JSON_SCHEMA_VALIDATION) + target_link_libraries(xclbinutil-lib PUBLIC transformcdo) +else() + target_compile_options(xclbinutil-lib PUBLIC "/EHsc") + # if you get + # LINK : fatal error LNK1104: cannot open file + # 'libboost_filesystem-vc142-mt-gd-x64-1_74.lib' +# target_compile_definitions(xclbinutil-lib PUBLIC BOOST_ALL_DYN_LINK) endif() -option(OUTPUT_DIR "Output directory" ${CMAKE_CURRENT_BINARY_DIR}) +set(THREADS_PREFER_PTHREAD_FLAG ON) +target_link_libraries(xclbinutil-lib PUBLIC ${Boost_LIBRARIES} Threads::Threads) +target_include_directories(xclbinutil-lib PUBLIC ${XRT_BINARY_DIR}/gen + ${Boost_INCLUDE_DIRS}) + +pybind11_add_module(_xclbinutil xaiepy/xclbinutil.cpp) +target_include_directories(_xclbinutil PUBLIC ${XCLBINUTIL_SOURCE_DIR}) +target_link_libraries(_xclbinutil PUBLIC xclbinutil-lib) + +add_dependencies(xaie _xclbinutil) + +# ############################################################################## +# pyxrt +# ############################################################################## set_target_properties( - xaie + pyxrt PROPERTIES - LIBRARY_OUTPUT_DIRECTORY - ${OUTPUT_DIR} - ARCHIVE_OUTPUT_DIRECTORY - ${OUTPUT_DIR} - RUNTIME_OUTPUT_DIRECTORY - ${OUTPUT_DIR}) - -option(BUILD_PYXRT "Build XRT" ON) -if(BUILD_PYXRT) - find_package(Boost REQUIRED) - include_directories(${Boost_INCLUDE_DIRS}) - - if(WIN32) - file( - READ - ${PROJECT_SOURCE_DIR}/third_party/XRT/src/runtime_src/core/common/memalign.h - FILE_CONTENTS) - string( - REPLACE - "defined(_WINDOWS)" - "defined(_WINDOWS) || defined(_WIN32) || defined(WIN32) || defined(__CYGWIN__) || defined(__MINGW32__)" - FILE_CONTENTS - "${FILE_CONTENTS}") - file( - WRITE - ${PROJECT_SOURCE_DIR}/third_party/XRT/src/runtime_src/core/common/memalign.h - "${FILE_CONTENTS}") - endif() - add_subdirectory(${PROJECT_SOURCE_DIR}/third_party/XRT/src) - set_target_properties( - pyxrt - PROPERTIES - LIBRARY_OUTPUT_DIRECTORY - ${OUTPUT_DIR} - ARCHIVE_OUTPUT_DIRECTORY - ${OUTPUT_DIR} - RUNTIME_OUTPUT_DIRECTORY - ${OUTPUT_DIR}) - add_dependencies(xaie pyxrt) -endif() + # pyxrt and xrt in general do a ridiculous dance with drivers + # https://github.com/Xilinx/XRT/blob/edcae12640ce96ec597c4c0cc1b2a850cfcc5c8b/src/runtime_src/core/common/module_loader.cpp#L201-L205 + SKIP_BUILD_RPATH ON + BUILD_WITH_INSTALL_RPATH ON) + +add_dependencies(xaie pyxrt) + +# ############################################################################## +# finish +# ############################################################################## + +set_target_properties(_bootgen _xclbinutil bootgen-lib cdo_driver xaie + xclbinutil-lib PROPERTIES POSITION_INDEPENDENT_CODE ON) + +set_target_properties( + _bootgen _xclbinutil pyxrt xaie xclbinutil-lib + PROPERTIES LIBRARY_OUTPUT_DIRECTORY ${OUTPUT_DIR} + ARCHIVE_OUTPUT_DIRECTORY ${OUTPUT_DIR} + RUNTIME_OUTPUT_DIRECTORY ${OUTPUT_DIR}) diff --git a/README.md b/README.md index df041f5..827d22c 100644 --- a/README.md +++ b/README.md @@ -1,9 +1,24 @@ # xaiepy AIE-RT + Python +# Downloading/installing + +## Release + +```shell +$ pip install xaiepy -f https://github.com/nod-ai/prototype-aie-toolchain/releases/expanded_assets/release +``` + +## Dev + +```shell +$ pip install xaiepy -f https://github.com/nod-ai/prototype-aie-toolchain/releases/expanded_assets/dev +``` + +# Demo + ```shell -$ pip install xaiepy -f https://github.com/makslevental/xaiepy/releases/expanded_assets/latest -$ python tests/test_basic.py +$ python examples/basic.py opcode='XAIE_IO_WRITE' reg_off=000000000021d000 @@ -19,4 +34,21 @@ opcode='XAIE_IO_WRITE' reg_off=000000000021d040 val=0 mask=000000000fffc000 -``` \ No newline at end of file +``` + +# Development + +## Gotchas + +If **ON WINDOWS** you're having trouble with + +```shell +CMake Error at C:/Users/maksim/miniconda3/envs/xaiepy/Lib/site-packages/cmake/data/share/cmake-3.29/Modules/FindPackageHandleStandardArgs.cmake:230 (message): + Could NOT find OpenSSL, try to set the path to OpenSSL root folder in the + system variable OPENSSL_ROOT_DIR (missing: OPENSSL_CRYPTO_LIBRARY) (found + version "3.3.0") +``` + +you probably have the win32 developer shell open; open the `x64 Native Tools Command Prompt for VS` instead ([or copy the settings from the win32 powershell to a new link](https://developercommunity.visualstudio.com/t/the-developer-powershell-for-vs-2022-should-use-x6/1568773#T-N10609425)) 🤦. + +**Note**: `setup.py` expects OpenSSL to be installed @ `C:\Program Files\OpenSSL`. \ No newline at end of file diff --git a/examples/e2e.py b/examples/cdo.py similarity index 60% rename from examples/e2e.py rename to examples/cdo.py index 22a5239..d377545 100644 --- a/examples/e2e.py +++ b/examples/cdo.py @@ -1,6 +1,25 @@ +import json +import logging import platform from pathlib import Path +from xaiepy import bootgen, xclbinutil +from xaiepy.cdo import ( + startCDOFileStream, + FileHeader, + configureHeader, + endCurrentCDOFileStream, + EnAXIdebug, + setEndianness, + Little_Endian, +) + +logging.basicConfig( + level=logging.DEBUG, + format="%(message)s", + datefmt="%H:%M:%S", +) + from xaiepy import ( XAie_Config, XAie_BackendType, @@ -26,14 +45,11 @@ StrmSwPortType, XAie_EnableAieToShimDmaStrmPort, XAie_DmaDesc, - XAie_StartTransaction, - XAie_ExportTransactionInstance, - _XAie_Txn_Submit, - get_written_addresses, - get_addr_tile, - reset_written_addresses, ) +if platform.system() != "Windows": + from xaiepy import XAie_ErrorHandlingInit + XAIE_DEV_GEN_AIEML = 2 XAIE_BASE_ADDR = 0x40000000 XAIE_COL_SHIFT = 25 @@ -44,7 +60,10 @@ XAIE_TRANSACTION_DISABLE_AUTO_FLUSH = 0b0 DDR_AIE_ADDR_OFFSET = 0x80000000 -reset_written_addresses() +col = 0 +tile_0_0 = XAie_LocType(0, col) +tile_0_1 = XAie_LocType(1, col) +tile_0_2 = XAie_LocType(2, col) configPtr = XAie_Config( XAIE_DEV_GEN_AIEML, @@ -66,21 +85,18 @@ XAie_SetupPartitionConfig(devInst, 0, 1, 1) XAie_CfgInitialize(devInst, configPtr) -# XAie_ErrorHandlingInit(devInst) - -XAie_StartTransaction(devInst, XAIE_TRANSACTION_DISABLE_AUTO_FLUSH) - XAie_UpdateNpiAddr(devInst, 0) -col = 0 -tile_0_0 = XAie_LocType(0, col) -tile_0_1 = XAie_LocType(1, col) -tile_0_2 = XAie_LocType(2, col) +EnAXIdebug() +setEndianness(Little_Endian) +cdo_fp = Path(__file__).parent.absolute() / "pi_cdo.bin" +startCDOFileStream(str(cdo_fp)) +FileHeader() +if platform.system() != "Windows": + XAie_ErrorHandlingInit(devInst) -XAie_LoadElf( - devInst, tile_0_2, str(Path(__file__).parent.absolute() / "pi.elf"), False -) +XAie_LoadElf(devInst, tile_0_2, str(Path(__file__).parent.absolute() / "pi.elf"), False) XAie_CoreReset(devInst, tile_0_2) XAie_CoreUnreset(devInst, tile_0_2) @@ -111,10 +127,26 @@ XAie_EnableAieToShimDmaStrmPort(devInst, tile_0_0, 2) XAie_CoreEnable(devInst, tile_0_2) -txn_inst = XAie_ExportTransactionInstance(devInst) - -if platform.system() != "Windows": - _XAie_Txn_Submit(devInst, txn_inst) - for addr, data in get_written_addresses().items(): - c, r, offset = get_addr_tile(addr) - print(c, r, f"{offset=:08x}", f"{data=:08x}") +configureHeader() +endCurrentCDOFileStream() + +bif_fp = Path(__file__).parent.absolute() / "pi.bif" +with open(bif_fp, "w") as f: + f.write(bootgen.emit_design_bif([cdo_fp])) + +pdi_fp = Path(__file__).parent.absolute() / "pi.pdi" +bootgen.make_design_pdi(str(bif_fp), str(pdi_fp)) +mem_top_json_fp = Path(__file__).parent.absolute() / "mem_topology.json" +with open(mem_top_json_fp, "w") as f: + json.dump(xclbinutil.mem_topology, f) +aie_part_json_fp = Path(__file__).parent.absolute() / "aie_partition.json" +with open(aie_part_json_fp, "w") as f: + json.dump(xclbinutil.emit_partition(pdi_fp, num_cols=1), f) +kernels_json_fp = Path(__file__).parent.absolute() / "kernels.json" +with open(kernels_json_fp, "w") as f: + json.dump(xclbinutil.emit_design_kernel_json(), f) + +pi_xclbin_fp = Path(__file__).parent.absolute() / "pi.xclbin" +xclbinutil.make_xclbin( + str(mem_top_json_fp), str(aie_part_json_fp), str(kernels_json_fp), str(pi_xclbin_fp) +) diff --git a/examples/load_xrt.py b/examples/load_xrt.py new file mode 100644 index 0000000..768204b --- /dev/null +++ b/examples/load_xrt.py @@ -0,0 +1,240 @@ +import json +import logging +import platform +from pathlib import Path + +import numpy as np +from xaiepy import bootgen, xclbinutil +from xaiepy.cdo import ( + startCDOFileStream, + FileHeader, + configureHeader, + endCurrentCDOFileStream, + EnAXIdebug, + setEndianness, + Little_Endian, +) +from xaiepy.pyxrt import ert_cmd_state + +logging.basicConfig( + level=logging.DEBUG, + format="%(message)s", + datefmt="%H:%M:%S", +) + +from xaiepy import ( + XAie_Config, + XAie_BackendType, + XAie_PartitionProp, + XAie_DevInst, + XAie_CfgInitialize, + XAie_LocType, + XAie_LoadElf, + XAie_SetupPartitionConfig, + XAie_UpdateNpiAddr, + XAie_CoreReset, + XAie_CoreUnreset, + XAie_LockSetValue, + XAie_Lock, + XAie_DmaDescInit, + XAie_DmaSetAddrLen, + XAie_DmaEnableBd, + XAie_DmaWriteBd, + XAie_DmaChannelSetStartQueue, + XAie_DmaChannelEnable, + XAie_StrmConnCctEnable, + XAie_CoreEnable, + StrmSwPortType, + XAie_EnableAieToShimDmaStrmPort, + XAie_DmaDesc, +) + +if platform.system() != "Windows": + from xaiepy import XAie_ErrorHandlingInit + +XAIE_DEV_GEN_AIEML = 2 +XAIE_BASE_ADDR = 0x40000000 +XAIE_COL_SHIFT = 25 +XAIE_ROW_SHIFT = 20 +XAIE_SHIM_ROW = 0 +XAIE_MEM_TILE_ROW_START = 1 +XAIE_PARTITION_BASE_ADDR = 0x0 +XAIE_TRANSACTION_DISABLE_AUTO_FLUSH = 0b0 +DDR_AIE_ADDR_OFFSET = 0x80000000 + +col = 0 +tile_0_0 = XAie_LocType(0, col) +tile_0_1 = XAie_LocType(1, col) +tile_0_2 = XAie_LocType(2, col) + +configPtr = XAie_Config( + XAIE_DEV_GEN_AIEML, + XAIE_BASE_ADDR, + XAIE_COL_SHIFT, + XAIE_ROW_SHIFT, + 6, + 5, + XAIE_SHIM_ROW, + XAIE_MEM_TILE_ROW_START, + 1, + (XAIE_MEM_TILE_ROW_START + 1), + (6 - 1 - 1), + XAie_PartitionProp(), + XAie_BackendType.XAIE_IO_BACKEND_CDO, +) + +devInst = XAie_DevInst() + +XAie_SetupPartitionConfig(devInst, 0, 1, 1) +XAie_CfgInitialize(devInst, configPtr) +XAie_UpdateNpiAddr(devInst, 0) + +EnAXIdebug() +setEndianness(Little_Endian) +cdo_fp = Path(__file__).parent.absolute() / "pi_cdo.bin" +startCDOFileStream(str(cdo_fp)) +FileHeader() + +if platform.system() != "Windows": + XAie_ErrorHandlingInit(devInst) + +XAie_LoadElf(devInst, tile_0_2, str(Path(__file__).parent.absolute() / "pi.elf"), False) + +XAie_CoreReset(devInst, tile_0_2) +XAie_CoreUnreset(devInst, tile_0_2) +XAie_LockSetValue(devInst, tile_0_2, XAie_Lock(0, 1)) +XAie_LockSetValue(devInst, tile_0_2, XAie_Lock(1, 0)) + +dmaTileBd = XAie_DmaDesc() +XAie_DmaDescInit(devInst, dmaTileBd, tile_0_2) +dmaTileBd.DmaMod.contents.SetLock(dmaTileBd, XAie_Lock(1, -1), XAie_Lock(0, 1), 1, 0) +XAie_DmaSetAddrLen(dmaTileBd, 1024, 4) +XAie_DmaEnableBd(dmaTileBd) +XAie_DmaWriteBd(devInst, dmaTileBd, tile_0_2, 0) +XAie_DmaChannelSetStartQueue(devInst, tile_0_2, 0, 1, 0, 1, 0) +XAie_DmaChannelEnable(devInst, tile_0_2, 0, 1) + +XAie_StrmConnCctEnable( + devInst, tile_0_0, StrmSwPortType.CTRL, 0, StrmSwPortType.SOUTH, 0 +) +XAie_StrmConnCctEnable( + devInst, tile_0_0, StrmSwPortType.NORTH, 0, StrmSwPortType.SOUTH, 2 +) +XAie_StrmConnCctEnable( + devInst, tile_0_1, StrmSwPortType.NORTH, 0, StrmSwPortType.SOUTH, 0 +) +XAie_StrmConnCctEnable( + devInst, tile_0_2, StrmSwPortType.DMA, 0, StrmSwPortType.SOUTH, 0 +) +XAie_EnableAieToShimDmaStrmPort(devInst, tile_0_0, 2) +XAie_CoreEnable(devInst, tile_0_2) + +configureHeader() +endCurrentCDOFileStream() + +bif_fp = Path(__file__).parent.absolute() / "pi.bif" +with open(bif_fp, "w") as f: + f.write(bootgen.emit_design_bif([cdo_fp])) + +pdi_fp = Path(__file__).parent.absolute() / "pi.pdi" +bootgen.make_design_pdi(str(bif_fp), str(pdi_fp)) +mem_top_json_fp = Path(__file__).parent.absolute() / "mem_topology.json" +with open(mem_top_json_fp, "w") as f: + json.dump(xclbinutil.mem_topology, f) +aie_part_json_fp = Path(__file__).parent.absolute() / "aie_partition.json" +with open(aie_part_json_fp, "w") as f: + json.dump(xclbinutil.emit_partition(pdi_fp, num_cols=1), f) +kernels_json_fp = Path(__file__).parent.absolute() / "kernels.json" +with open(kernels_json_fp, "w") as f: + json.dump(xclbinutil.emit_design_kernel_json(), f) + +pi_xclbin_fp = Path(__file__).parent.absolute() / "pi.xclbin" +xclbinutil.make_xclbin( + str(mem_top_json_fp), str(aie_part_json_fp), str(kernels_json_fp), str(pi_xclbin_fp) +) + +try: + from xaiepy import pyxrt +except ImportError as e: + assert ( + e.args[0] + == "libxrt_coreutil.so: cannot open shared object file: No such file or directory" + ) + raise ImportError( + "Can't find libxrt_coreutil.so; you probably need to set LD_LIBRARY_PATH=/opt/xilinx/xrt/lib" + ) + +_PROLOG = [ + 0x00000011, + 0x01000405, + 0x01000100, + 0x0B590100, + 0x000055FF, + 0x00000001, + 0x00000010, + 0x314E5A5F, + 0x635F5F31, + 0x676E696C, + 0x39354E5F, + 0x6E693131, + 0x5F727473, + 0x64726F77, + 0x00004573, + 0x07BD9630, + 0x000055FF, +] + +shim_instr_v = [ + 0x06000100, + 0x00000000, + 0x00000001, + 0x00000000, + 0x00000000, + 0x00000000, + 0x80000000, + 0x00000000, + 0x00000000, + 0x02000000, + 0x02000000, + 0x0001D204, + 0x80000000, + 0x03000000, + 0x00010100, +] + +instr_v = _PROLOG + shim_instr_v + + +def init_xrt_load_kernel(xclbin: Path, kernel_name): + device = pyxrt.device(0) + xclbin = pyxrt.xclbin(str(xclbin)) + xkernel = next(k for k in xclbin.get_kernels() if k.get_name() == kernel_name) + device.register_xclbin(xclbin) + context = pyxrt.hw_context(device, xclbin.get_uuid()) + kernel = pyxrt.kernel(context, xkernel.get_name()) + return device, kernel + + +device, kernel = init_xrt_load_kernel(pi_xclbin_fp, "MLIR_AIE") + +instr_v = np.array(instr_v, dtype=np.uint32) + +bo_instr = pyxrt.bo(device, len(instr_v) * 4, pyxrt.bo.cacheable, kernel.group_id(0)) +bo_inout0 = pyxrt.bo(device, 1 * 4, pyxrt.bo.host_only, kernel.group_id(2)) + +inout0 = np.ones((1,), dtype=np.float32) + +bo_instr.write(instr_v, 0) +bo_inout0.write(inout0, 0) + +bo_instr.sync(pyxrt.xclBOSyncDirection.XCL_BO_SYNC_BO_TO_DEVICE) +bo_inout0.sync(pyxrt.xclBOSyncDirection.XCL_BO_SYNC_BO_TO_DEVICE) + +h = kernel(bo_instr, len(instr_v), bo_inout0) +assert h.wait() == ert_cmd_state.ERT_CMD_STATE_COMPLETED +bo_inout0.sync(pyxrt.xclBOSyncDirection.XCL_BO_SYNC_BO_FROM_DEVICE) +entire_buffer = bo_inout0.read(4, 0).view(np.float32) +print(entire_buffer[0]) +v = entire_buffer[0].item() +assert isinstance(v, float) +assert np.isclose(v, 3.14) diff --git a/examples/pi.xclbin b/examples/pi.xclbin deleted file mode 100644 index 8956e468a368c83915c0f05398b4c061ae7406c8..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 7411 zcmeHMO>7&-6&{MTrCMo48Hy`84zgJXs1i8QQj{smm1IhmQVX(VMn8sywAiecLuwOp zmm!xbRYSE7g%O~JVW3D5H9+7?fgtF?$srBk3!U~L1U>jvSU{V)m-Z4AG72OC?3#UV zW|kaErYto&wO)fe-@JK0Gw)|+v@V+IdCTr5<;Tyq9(evogGmCLY2fI*ASlop7S*#x z&b3_2u?0Qq=ipisZ-tl`tv0utIsRPm3-&65Kp(Vl;Mw)&xrH&vDLrD~sVxkg5EXn0 zF@CrX-!;MK+4a`Z;PV73Ab^z9XQyYp5o{}br3#htO-~I?R44*`imh{E^U0Cpu2I40H@)wX939GGd{WLlI#~UB65}=`t=irB}7nfhHeBK7# z$}=_+k1Iiih~-x32)wn|5O>fizHsx`Xit{MT(vSridN-C1y%_jdatwZ=`;v?KaY;B zSKJtEsFg~XWu9+~=kJyOjqyG;auzHQl7hLs^6B-m-`7Ce<9@a-#HS@3kZ_NLk&d1~ z{vgAI;h4m8HRdNLfeU%AN%*>iOA-#meHH#V(9zE*`#bTu7 zgE{hlv)^32UoOurYR_~P3HdlMFEmUnUjFmOUp+2tJK9LF+Fxz@s10-}vaY$s!FIng zjww!_^pPz3+4~25kY3%dwR1|zPu>z+9kb~1VcXFpTY^4tyiLf0tgl1yM^`^9w}9Sv zAv4y>KWKvV<64k^{j+i-$b7(QvzMChpN{=5NDh~TPr?40U+LUWuA~3%mpcv~40g0v z?QT22gTWeJv^V!ob36AkdSRBVKp0PTHot%Jau)$VeM3q(TgdTjKi}&{Gf5}pYZ##+_?Vpvd|^3 z2(#=>WW9s-ZUhgaFHRHRjbOttptU&p;q&i3_u=;kV{tMU*?(m2t6v``XJM{Zp4asq3EC`Yl}`A_PjMyxNg@hnr$YFWaHgf$7rBpjFUYZA^% zctyfjC45c7*Ckw%@GS}7k?@9u31Cb|dH$_Fo}l}D;xM{}JTbOT`SrJF0u3nF_67F= z`YR}~r?Y8;d2@yN>TVSWf&M*07(FV{kC3OxcLlx2$8HRar)>k{!}$YukK!~|&|YA} z$IdCd8hG(S+xQ!aLp31&4r|Cw-jFrOJ&%Wt!|Rry-G+8=pR4By=v)J(X!Fh<=_5TJ~TF}Drv(`72voYP)${Fm$Cp7*@Eh6 zVTx(drFMcQUQ8ISq2vqmoG+v*$%1X-rPN5nlvM>Jb#6zu5(BDM?9)POSB)+smlAWZ z?7W*(6(fF zH=pW--SY&W(trE++xb&xhQ}uLiLt4vvs3!W(9DoNJu@^lb3x6U3p8WsV(U~-Tu?K{ z>rPHR0rh)K%~&?W8sMzqnhQYJ)a7I90yPp8xGty@M#?gQT;!itfN|PN*@j!l(KFOe zxhRqp4-AgerQx(=z9CV)(Z}uomj@slW^L=u0v#KHZ0PRm=?&9JL=z8;&R7{b?HaJn zLN@k>j)#x;_Czr)U#2X-rksq-f=POgUjabi#|2Q-{Sr1Iybl~xC!7R@0fb|=OLIxX zq_eZkxM#Fp3nFoGT{t&9c@DGy<-D05wrrsxtR;Js^N9$J^d`bl!%Wcmh!)mDVa+%m zG5aX+kI?x-3M5b(N+=H(5jgzyYAKCdb^-`YA5l-d3d}m5_%JpxI-!rirys^r$WxpR z+Xi$z$;XG8sSrAeakxebD7YuF?!1wwy10-q&K~YgSnO_Xn6)PGbJQbD{A77~JD-Me z*}|zc9|VCcc{LnHKUTaNHP~iXH&T+)T0QfL8a-jt*f!$GEGAd0Cu!8^*&JmntEY`6 zya-^(DA!npLI;6p_EbRfC bH$|R;-v12M?gIbAhu`vnhg|YUCrADR@Srv| diff --git a/examples/xrt.py b/examples/xrt.py index d7c62c0..9b35d28 100644 --- a/examples/xrt.py +++ b/examples/xrt.py @@ -4,9 +4,7 @@ import numpy as np from xaiepy import pyxrt - -os.environ["XILINX_XRT"] = "/opt/xilinx/xrt" -os.environ["LD_LIBRARY_PATH"] = "/opt/xilinx/xrt/lib" +from xaiepy.pyxrt import ert_cmd_state def init_xrt_load_kernel(xclbin: Path, kernel_name): @@ -24,7 +22,8 @@ def init_xrt_load_kernel(xclbin: Path, kernel_name): "MLIR_AIE", ) -instr_v = [ + +_PROLOG = [ 0x00000011, 0x01000405, 0x01000100, @@ -42,6 +41,9 @@ def init_xrt_load_kernel(xclbin: Path, kernel_name): 0x00004573, 0x07BD9630, 0x000055FF, +] + +shim_instr_v = [ 0x06000100, 0x00000000, 0x00000001, @@ -60,6 +62,7 @@ def init_xrt_load_kernel(xclbin: Path, kernel_name): ] +instr_v = _PROLOG + shim_instr_v instr_v = np.array(instr_v, dtype=np.uint32) bo_instr = pyxrt.bo(device, len(instr_v) * 4, pyxrt.bo.cacheable, kernel.group_id(0)) @@ -74,7 +77,7 @@ def init_xrt_load_kernel(xclbin: Path, kernel_name): bo_inout0.sync(pyxrt.xclBOSyncDirection.XCL_BO_SYNC_BO_TO_DEVICE) h = kernel(bo_instr, len(instr_v), bo_inout0) -h.wait() +assert h.wait() == ert_cmd_state.ERT_CMD_STATE_COMPLETED bo_inout0.sync(pyxrt.xclBOSyncDirection.XCL_BO_SYNC_BO_FROM_DEVICE) entire_buffer = bo_inout0.read(4, 0).view(np.float32) print(entire_buffer[0]) diff --git a/include/elf.h b/include/elf.h deleted file mode 100644 index 474cac9..0000000 --- a/include/elf.h +++ /dev/null @@ -1,265 +0,0 @@ -/****************************************************************************** -* Copyright 2015-2022 Xilinx, Inc. -* Copyright 2022-2023 Advanced Micro Devices, Inc. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -******************************************************************************/ - -#ifndef _SYS_ELF_H -#define _SYS_ELF_H - -/* -------------------------------------------------------------------------------- -*********************************************** H E A D E R F I L E S *** -------------------------------------------------------------------------------- -*/ -#include "elftypes.h" - -#ifdef __cplusplus -extern "C" { -#endif - - -/* -------------------------------------------------------------------------------- -********************************************* P R E P R O C E S S O R S *** -------------------------------------------------------------------------------- -*/ -#define EI_NIDENT 16 -#define ELF32_FSZ_ADDR 4 -#define ELF32_FSZ_HALF 2 -#define ELF32_FSZ_OFF 4 -#define ELF32_FSZ_SWORD 4 -#define ELF32_FSZ_WORD 4 - -/* e_ident[] indexes */ -#define EI_MAG0 0 -#define EI_MAG1 1 -#define EI_MAG2 2 -#define EI_MAG3 3 -#define EI_CLASS 4 -#define EI_DATA 5 -#define EI_VERSION 6 -#define EI_PAD 7 - -/* EI_MAG */ -#define ELFMAG0 0x7f -#define ELFMAG1 'E' -#define ELFMAG2 'L' -#define ELFMAG3 'F' -#define ELFMAG "\177ELF" -#define SELFMAG 4 - -/* EI_DATA */ -#define ELFDATANONE 0 -#define ELFDATA2LSB 1 -#define ELFDATA2MSB 2 -#define ELFDATANUM 3 - -/* e_type */ -#define ET_NONE 0 -#define ET_REL 1 -#define ET_EXEC 2 -#define ET_DYN 3 -#define ET_CORE 4 -#define ET_NUM 5 - -/* processor specific range */ -#define ET_LOPROC 0xff00 -#define ET_HIPROC 0xffff - -/* e_machine */ -#define EM_NONE 0 -#define EM_M32 1 /* AT&T WE 32100 */ -#define EM_SPARC 2 /* Sun SPARC */ -#define EM_386 3 /* Intel 80386 */ -#define EM_68K 4 /* Motorola 68000 */ -#define EM_88K 5 /* Motorola 88000 */ -#define EM_486 6 /* Intel 80486 */ -#define EM_860 7 /* Intel i860 */ -#define EM_MIPS 8 /* MIPS RS3000 Big-Endian */ -#define EM_UNKNOWN9 9 -#define EM_MIPS_RS3_LE 10 /* MIPS RS3000 Little-Endian */ -#define EM_RS6000 11 /* RS6000 */ -#define EM_UNKNOWN12 12 -#define EM_UNKNOWN13 13 -#define EM_UNKNOWN14 14 -#define EM_PA_RISC 15 /* PA-RISC */ -#define EM_nCUBE 16 /* nCUBE */ -#define EM_VPP500 17 /* Fujitsu VPP500 */ -#define EM_SPARC32PLUS 18 /* Sun SPARC 32+ */ -#define EM_UNKNOWN19 19 -#define EM_PPC 20 /* PowerPC */ -#define EM_NUM 21 - -/* e_version, EI_VERSION */ -#define EV_NONE 0 -#define EV_CURRENT 1 -#define EV_NUM 2 - -/* p_type */ -#define PT_NULL 0 -#define PT_LOAD 1 -#define PT_DYNAMIC 2 -#define PT_INTERP 3 -#define PT_NOTE 4 -#define PT_SHLIB 5 -#define PT_PHDR 6 -#define PT_NUM 7 - -/* processor specific range */ -#define PT_LOPROC 0x70000000 -#define PT_HIPROC 0x7fffffff - -/* p_flags */ -#define PF_R 0x4 -#define PF_W 0x2 -#define PF_X 0x1 - -/* processor specific values */ -#define PF_MASKPROC 0xf0000000 - -/* sh_type */ -#define SHT_NULL 0 -#define SHT_PROGBITS 1 -#define SHT_SYMTAB 2 -#define SHT_STRTAB 3 -#define SHT_RELA 4 -#define SHT_HASH 5 -#define SHT_DYNAMIC 6 -#define SHT_NOTE 7 -#define SHT_NOBITS 8 -#define SHT_REL 9 -#define SHT_SHLIB 10 -#define SHT_DYNSYM 11 -#define SHT_NUM 12 - -#define SHT_LOSUNW 0x6ffffffd -#define SHT_SUNW_verdef 0x6ffffffd -#define SHT_SUNW_verneed 0x6ffffffe -#define SHT_SUNW_versym 0x6fffffff -#define SHT_HISUNW 0x6fffffff - -/* processor specific range */ -#define SHT_LOPROC 0x70000000 -#define SHT_HIPROC 0x7fffffff -#define SHT_LOUSER 0x80000000 -#define SHT_HIUSER 0xffffffff - -/* sh_flags */ -#define SHF_WRITE 0x1 -#define SHF_ALLOC 0x2 -#define SHF_EXECINSTR 0x4 - -/* processor specific values */ -#define SHF_MASKPROC 0xf0000000 - -/* special section numbers */ -#define SHN_UNDEF 0 -#define SHN_LORESERVE 0xff00 -#define SHN_ABS 0xfff1 -#define SHN_COMMON 0xfff2 -#define SHN_HIRESERVE 0xffff - -/* processor specific range */ -#define SHN_LOPROC 0xff00 -#define SHN_HIPROC 0xff1f - -#define STN_UNDEF 0 - - -/* -------------------------------------------------------------------------------- -*************************************************** S T R U C T U R E S *** -------------------------------------------------------------------------------- -*/ -/******************************************************************************/ -typedef struct { - unsigned char e_ident[EI_NIDENT]; /* ident bytes */ - Elf32_Half e_type; /* file type */ - Elf32_Half e_machine; /* target machine */ - Elf32_Word e_version; /* file version */ - Elf32_Addr e_entry; /* start address */ - Elf32_Off e_phoff; /* phdr file offset */ - Elf32_Off e_shoff; /* shdr file offset */ - Elf32_Word e_flags; /* file flags */ - Elf32_Half e_ehsize; /* sizeof ehdr */ - Elf32_Half e_phentsize; /* sizeof phdr */ - Elf32_Half e_phnum; /* number phdrs */ - Elf32_Half e_shentsize; /* sizeof shdr */ - Elf32_Half e_shnum; /* number shdrs */ - Elf32_Half e_shstrndx; /* shdr string index */ -} Elf32_Ehdr; - -/******************************************************************************/ -typedef struct { - unsigned char e_ident[EI_NIDENT]; /* ident bytes */ - Elf64_Half e_type; /* file type */ - Elf64_Half e_machine; /* target machine */ - Elf64_Word e_version; /* file version */ - Elf64_Addr e_entry; /* start address */ - Elf64_Off e_phoff; /* phdr file offset */ - Elf64_Off e_shoff; /* shdr file offset */ - Elf64_Word e_flags; /* file flags */ - Elf64_Half e_ehsize; /* sizeof ehdr */ - Elf64_Half e_phentsize; /* sizeof phdr */ - Elf64_Half e_phnum; /* number phdrs */ - Elf64_Half e_shentsize; /* sizeof shdr */ - Elf64_Half e_shnum; /* number shdrs */ - Elf64_Half e_shstrndx; /* shdr string index */ -} Elf64_Ehdr; - -/******************************************************************************/ -typedef struct -{ - Elf32_Word p_type; /* entry type */ - Elf32_Off p_offset; /* file offset */ - Elf32_Addr p_vaddr; /* virtual address */ - Elf32_Addr p_paddr; /* physical address */ - Elf32_Word p_filesz; /* file size */ - Elf32_Word p_memsz; /* memory size */ - Elf32_Word p_flags; /* entry flags */ - Elf32_Word p_align; /* memory/file alignment */ -} Elf32_Phdr; - -/******************************************************************************/ -typedef struct -{ - Elf32_Word sh_name; /* section name */ - Elf32_Word sh_type; /* SHT_... */ - Elf32_Word sh_flags; /* SHF_... */ - Elf32_Addr sh_addr; /* virtual address */ - Elf32_Off sh_offset; /* file offset */ - Elf32_Word sh_size; /* section size */ - Elf32_Word sh_link; /* misc info */ - Elf32_Word sh_info; /* misc info */ - Elf32_Word sh_addralign; /* memory alignment */ - Elf32_Word sh_entsize; /* entry size if table */ -} Elf32_Shdr; - -/******************************************************************************/ -typedef struct -{ - Elf32_Word st_name; - Elf32_Addr st_value; - Elf32_Word st_size; - unsigned char st_info; /* bind, type: ELF_32_ST_... */ - unsigned char st_other; - Elf32_Half st_shndx; /* SHN_... */ -} Elf32_Sym; - -#ifdef __cplusplus -} -#endif - -#endif diff --git a/include/elftypes.h b/include/elftypes.h deleted file mode 100644 index 7dfa6f6..0000000 --- a/include/elftypes.h +++ /dev/null @@ -1,47 +0,0 @@ -/****************************************************************************** -* Copyright 2015-2022 Xilinx, Inc. -* Copyright 2022-2023 Advanced Micro Devices, Inc. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -******************************************************************************/ - -#pragma once - -#ifndef _ELFTYPES_H_ -#define _ELFTYPES_H_ - -/* -------------------------------------------------------------------------------- -*********************************************** H E A D E R F I L E S *** -------------------------------------------------------------------------------- -*/ -#include - - -/* ELF 32-bit */ -typedef uint32_t Elf32_Addr; -typedef uint16_t Elf32_Half; -typedef uint32_t Elf32_Off; -typedef int32_t Elf32_Sword; -typedef uint32_t Elf32_Xword; -typedef uint32_t Elf32_Word; - -/* ELF 64-bit */ -typedef uint64_t Elf64_Addr; -typedef uint64_t Elf64_Off; -typedef int64_t Elf64_Sxword; -typedef uint64_t Elf64_Xword; -typedef int32_t Elf32_Sword; -typedef uint32_t Elf64_Word; -typedef uint16_t Elf64_Half; -#endif diff --git a/patches/aie-rt.diff b/patches/aie-rt.diff new file mode 100644 index 0000000..c05bbb5 --- /dev/null +++ b/patches/aie-rt.diff @@ -0,0 +1,23 @@ +Submodule third_party/aie-rt contains modified content +diff --git a/third_party/aie-rt/driver/src/CMakeLists.txt b/third_party/aie-rt/driver/src/CMakeLists.txt +index beacb4a..d279531 100644 +--- a/third_party/aie-rt/driver/src/CMakeLists.txt ++++ b/third_party/aie-rt/driver/src/CMakeLists.txt +@@ -3,7 +3,7 @@ + cmake_minimum_required(VERSION 3.15) + project(aienginev2) + +-find_package(common) ++#find_package(common) + collector_create (PROJECT_LIB_HEADERS "${CMAKE_CURRENT_SOURCE_DIR}") + collector_create (PROJECT_LIB_SOURCES "${CMAKE_CURRENT_SOURCE_DIR}") + include_directories(${CMAKE_BINARY_DIR}/include/xaiengine) +@@ -170,4 +170,7 @@ file(COPY ${_headers} DESTINATION ${CMAKE_BINARY_DIR}/include/xaiengine) + add_library(aienginev2 STATIC ${_sources}) + + set_target_properties(aienginev2 PROPERTIES LINKER_LANGUAGE C) +-target_compile_options(aienginev2 PRIVATE -Wall -Wextra -D__AIEBAREMETAL__) ++target_compile_options(aienginev2 PRIVATE -D__AIECDO__) ++if(CMAKE_COMPILER_IS_GNUCXX) ++ target_compile_options(aienginev2 PRIVATE -Wall -Wextra) ++endif(CMAKE_COMPILER_IS_GNUCXX) diff --git a/patches/apply_patches.sh b/patches/apply_patches.sh new file mode 100755 index 0000000..a02cebd --- /dev/null +++ b/patches/apply_patches.sh @@ -0,0 +1,29 @@ +#!/usr/bin/env bash +set -uxo pipefail + +HERE=$(dirname "$(readlink -f "${BASH_SOURCE[0]}")") +REPO_SRC_DIR=${REPO_SRC_DIR:=$HERE/..} +pushd $REPO_SRC_DIR + +# note that space before slash is important +PATCHES="\ +bootgen \ +pyxrt \ +aie-rt" + +if [[ x"${APPLY_PATCHES:-true}" == x"true" ]]; then + for PATCH in $PATCHES; do + echo "applying $PATCH" + git apply --ignore-space-change --ignore-whitespace patches/$PATCH.diff + ERROR=$? + if [ $ERROR != 0 ]; then + git apply --ignore-space-change --ignore-whitespace --verbose patches/$PATCH.diff -R --check + ERROR=$? + if [ $ERROR != 0 ]; then + exit $ERROR + fi + fi + done +fi + +pushd $REPO_SRC_DIR diff --git a/patches/bootgen.diff b/patches/bootgen.diff new file mode 100644 index 0000000..6f153fc --- /dev/null +++ b/patches/bootgen.diff @@ -0,0 +1,66 @@ +Submodule third_party/bootgen contains modified content +diff --git a/third_party/bootgen/cdo-alloc.c b/third_party/bootgen/cdo-alloc.c +index e3a8cb3..d5e6efa 100755 +--- a/third_party/bootgen/cdo-alloc.c ++++ b/third_party/bootgen/cdo-alloc.c +@@ -18,7 +18,7 @@ + #include + #include + #include +-#include ++//#include + #include + #include "cdo-alloc.h" + +diff --git a/third_party/bootgen/cdo-driver/cdo_driver.h b/third_party/bootgen/cdo-driver/cdo_driver.h +index be282e4..2bef792 100755 +--- a/third_party/bootgen/cdo-driver/cdo_driver.h ++++ b/third_party/bootgen/cdo-driver/cdo_driver.h +@@ -49,17 +49,23 @@ typedef struct cdoHeader { + + } cdoHeader; + +-void startCDOFileStream(const char* cdoFileName); +-void endCurrentCDOFileStream(); +-void FileHeader(); +-void EnAXIdebug(); +-void setEndianness(bool endianness); +-void SectionHeader(); +-void configureHeader(); +-unsigned int getPadBytesForDmaWrCmdAlignment(uint32_t DmaCmdLength); +-void insertNoOpCommand(unsigned int numPadBytes); +-void insertDmaWriteCmdHdr(uint32_t DmaCmdLength); +-void disableDmaCmdAlignment(); ++#if defined(__GNUC__) ++#define DLL_PUBLIC __attribute__ ((used)) ++#elif defined(_MSC_VER) ++#define DLL_PUBLIC __declspec(dllexport) ++#endif ++ ++DLL_PUBLIC void startCDOFileStream(const char* cdoFileName); ++DLL_PUBLIC void endCurrentCDOFileStream(); ++DLL_PUBLIC void FileHeader(); ++DLL_PUBLIC void EnAXIdebug(); ++DLL_PUBLIC void setEndianness(bool endianness); ++//void SectionHeader(); ++DLL_PUBLIC void configureHeader(); ++DLL_PUBLIC unsigned int getPadBytesForDmaWrCmdAlignment(uint32_t DmaCmdLength); ++DLL_PUBLIC void insertNoOpCommand(unsigned int numPadBytes); ++DLL_PUBLIC void insertDmaWriteCmdHdr(uint32_t DmaCmdLength); ++DLL_PUBLIC void disableDmaCmdAlignment(); + + + #endif /* _CDO_DRIVER_H_ */ +diff --git a/third_party/bootgen/cdo-npi.c b/third_party/bootgen/cdo-npi.c +index d357724..c8d0643 100755 +--- a/third_party/bootgen/cdo-npi.c ++++ b/third_party/bootgen/cdo-npi.c +@@ -18,7 +18,7 @@ + #include + #include + #include +-#include ++//#include + #include + #include + #include diff --git a/patches/pyxrt.diff b/patches/pyxrt.diff new file mode 100644 index 0000000..d02dcb4 --- /dev/null +++ b/patches/pyxrt.diff @@ -0,0 +1,32 @@ +Submodule third_party/XRT contains modified content +diff --git a/third_party/XRT/src/runtime_src/core/common/memalign.h b/third_party/XRT/src/runtime_src/core/common/memalign.h +index 98caadc4a..3661cd3a3 100644 +--- a/third_party/XRT/src/runtime_src/core/common/memalign.h ++++ b/third_party/XRT/src/runtime_src/core/common/memalign.h +@@ -28,7 +28,7 @@ posix_memalign(void **memptr, size_t alignment, size_t size) + { + #if defined(__linux__) + return ::posix_memalign(memptr,alignment,size); +-#elif defined(_WINDOWS) ++#elif defined(_WINDOWS) || defined(_WIN32) || defined(WIN32) || defined(__CYGWIN__) || defined(__MINGW32__) || defined(_WIN32) || defined(WIN32) || defined(__CYGWIN__) || defined(__MINGW32__) + // this is not good, _aligned_malloc requires _aligned_free + // power of 2 + if (!alignment || (alignment & (alignment - 1))) +@@ -51,7 +51,7 @@ namespace detail { + template + struct aligned_ptr_deleter + { +-#if defined(_WINDOWS) ++#if defined(_WINDOWS) || defined(_WIN32) || defined(WIN32) || defined(__CYGWIN__) || defined(__MINGW32__) || defined(_WIN32) || defined(WIN32) || defined(__CYGWIN__) || defined(__MINGW32__) + void operator() (MyType* ptr) { _aligned_free(ptr); } + #else + void operator() (MyType* ptr) { free(ptr); } +@@ -74,7 +74,7 @@ aligned_alloc(size_t align, size_t size) + if (!align || (align & (align - 1))) + throw std::runtime_error("xrt_core::aligned_alloc requires power of 2 for alignment"); + +-#if defined(_WINDOWS) ++#if defined(_WINDOWS) || defined(_WIN32) || defined(WIN32) || defined(__CYGWIN__) || defined(__MINGW32__) || defined(_WIN32) || defined(WIN32) || defined(__CYGWIN__) || defined(__MINGW32__) + return aligned_ptr_t(reinterpret_cast(_aligned_malloc(size, align))); + #else + return aligned_ptr_t(reinterpret_cast(::aligned_alloc(align, size))); diff --git a/pyproject.toml b/pyproject.toml index 99a2728..0b2ae33 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,7 +1,6 @@ [tool.cibuildwheel] environment = { PIP_NO_BUILD_ISOLATION = "false" } build-verbosity = 3 -build = "cp38-* cp39-* cp310-* cp311-* cp312-*" manylinux-aarch64-image = "manylinux_2_28" manylinux-x86_64-image = "manylinux_2_28" @@ -10,19 +9,17 @@ skip = ["*-manylinux_i686", "*-musllinux*"] environment-pass = [ "PIP_NO_BUILD_ISOLATION", ] -before-build = [ - "rm -rf {project}/build", - "rm -rf *egg*", - "pip install -r requirements-dev.txt", +before-all = [ "yum install -y epel-release && yum install -y ninja-build cmake openssl openssl-devel boost-devel protobuf-devel libdrm-devel ocl-icd-devel systemtap-sdt-devel rapidjson ncurses-devel zlib-static libuuid-devel" ] - -[tool.cibuildwheel.macos] before-build = [ "rm -rf {project}/build", "rm -rf *egg*", "pip install -r requirements-dev.txt", ] +repair-wheel-command = [ + "auditwheel repair -w {dest_dir} {wheel} --exclude libxrt_coreutil.so" +] [tool.cibuildwheel.windows] skip = ["*-win32"] @@ -31,3 +28,6 @@ before-build = [ "rm -rf *egg*", "pip install -r requirements-dev.txt", ] +repair-wheel-command = [ + "delvewheel repair -v -w {dest_dir} {wheel} --ignore-in-wheel --no-dll xrt_coreutil.dll;msvcp140.dll" +] diff --git a/requirements-dev.txt b/requirements-dev.txt index 9761adb..be5e64c 100644 --- a/requirements-dev.txt +++ b/requirements-dev.txt @@ -3,9 +3,13 @@ setuptools>=64 wheel ninja -cmake>=3.28 +# https://gitlab.kitware.com/cmake/cmake/-/issues/25702#note_1485760 +cmake>=3.29 ctypesgen cmake-format black pytest -pybind11[global] \ No newline at end of file +pybind11[global] +delvewheel; sys_platform == 'win32' +auditwheel; sys_platform == 'linux' +cibuildwheel \ No newline at end of file diff --git a/requirements.txt b/requirements.txt index 3f8e471..296d654 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,2 +1 @@ -cffi numpy \ No newline at end of file diff --git a/setup.py b/setup.py index 0ca8428..e1d4331 100644 --- a/setup.py +++ b/setup.py @@ -6,6 +6,7 @@ from pathlib import Path from pprint import pprint +from cmake import cmake_executable_path from pip._internal.network.session import PipSession from pip._internal.req import parse_requirements from setuptools import Extension, setup @@ -69,13 +70,14 @@ def build_extension(self, ext: CMakeExtension) -> None: cmake_args += [ "-DCMAKE_C_COMPILER=cl", "-DCMAKE_CXX_COMPILER=cl", - "-DCMAKE_MSVC_RUNTIME_LIBRARY=MultiThreaded", - "-DCMAKE_C_FLAGS=/MT", - "-DCMAKE_SHARED_LINKER_FLAGS=/FORCE:UNRESOLVED", - "-DCMAKE_CXX_FLAGS=/MT /EHsc", "-DCMAKE_WINDOWS_EXPORT_ALL_SYMBOLS=ON", "-DCMAKE_SUPPORT_WINDOWS_EXPORT_ALL_SYMBOLS=ON", ] + if check_env("OPENSSL_ROOT_DIR"): + OPENSSL_ROOT_DIR = os.getenv( + "OPENSSL_ROOT_DIR", "C:\\Program Files\\OpenSSL" + ) + cmake_args += [f"-DOPENSSL_ROOT_DIR={OPENSSL_ROOT_DIR}"] if "CMAKE_ARGS" in os.environ: cmake_args += [item for item in os.environ["CMAKE_ARGS"].split(" ") if item] @@ -112,36 +114,29 @@ def build_extension(self, ext: CMakeExtension) -> None: env = os.environ.copy() print("ENV", pprint(os.environ), file=sys.stderr) print("CMAKE_ARGS", cmake_args, file=sys.stderr) + print(f"{cmake_executable_path=}", file=sys.stderr) + CMAKE_EXE_PATH = cmake_executable_path / "bin" / "cmake" subprocess.run( - ["cmake", ext.sourcedir, *cmake_args], cwd=build_temp, check=True, env=env + [str(CMAKE_EXE_PATH), ext.sourcedir, *cmake_args], + cwd=build_temp, + check=True, + env=env, ) subprocess.run( - ["cmake", "--build", ".", "--target", "xaie", *build_args], + [CMAKE_EXE_PATH, "--build", ".", "--target", "xaie", *build_args], cwd=build_temp, check=True, env=env, ) sys.path.append(str(HERE)) - from scripts import gen_xaie_ctypes - from scripts import gen_cdo + from util import gen_xaie_ctypes gen_xaie_ctypes.generate( build_temp / "include", extdir / PACKAGE_NAME / "__init__.py", - HERE / "include", - ) - - if platform.system() in {"Darwin", "Linux"}: - shlib_ext = "so" - elif platform.system() == "Windows": - shlib_ext = "pyd" - else: - raise NotImplementedError(f"unknown platform {platform.system()}") - - gen_cdo.build_ffi( - str(build_temp), str(extdir / PACKAGE_NAME / f"_cdo.{shlib_ext}") + elf_include_dir=HERE / "third_party" / "bootgen", ) diff --git a/tests/test_basic.py b/tests/test_basic.py index 785592e..8dae336 100644 --- a/tests/test_basic.py +++ b/tests/test_basic.py @@ -44,6 +44,7 @@ dev_inst = XAie_DevInst() XAie_CfgInitialize(dev_inst, config) + def test_transaction(): XAie_StartTransaction(dev_inst, XAIE_TRANSACTION_DISABLE_AUTO_FLUSH) diff --git a/tests/test_e2e.py b/tests/test_cdo.py similarity index 51% rename from tests/test_e2e.py rename to tests/test_cdo.py index c6ec865..16ed728 100644 --- a/tests/test_e2e.py +++ b/tests/test_cdo.py @@ -1,6 +1,24 @@ +import json +import logging import platform from pathlib import Path +from xaiepy.cdo import ( + startCDOFileStream, + FileHeader, + configureHeader, + endCurrentCDOFileStream, + EnAXIdebug, + setEndianness, + Little_Endian, +) + +logging.basicConfig( + level=logging.DEBUG, + format="%(message)s", + datefmt="%H:%M:%S", +) + from xaiepy import ( XAie_Config, XAie_BackendType, @@ -26,14 +44,13 @@ StrmSwPortType, XAie_EnableAieToShimDmaStrmPort, XAie_DmaDesc, - XAie_StartTransaction, - XAie_ExportTransactionInstance, - XAie_TxnOpcode, - _XAie_Txn_Submit, - get_written_addresses, - get_addr_tile, reset_written_addresses, + bootgen, + xclbinutil, ) +if platform.system() != "Windows": + from xaiepy import XAie_ErrorHandlingInit + XAIE_DEV_GEN_AIEML = 2 XAIE_BASE_ADDR = 0x40000000 XAIE_COL_SHIFT = 25 @@ -44,40 +61,44 @@ XAIE_TRANSACTION_DISABLE_AUTO_FLUSH = 0b0 DDR_AIE_ADDR_OFFSET = 0x80000000 -configPtr = XAie_Config( - XAIE_DEV_GEN_AIEML, - XAIE_BASE_ADDR, - XAIE_COL_SHIFT, - XAIE_ROW_SHIFT, - 6, - 5, - XAIE_SHIM_ROW, - XAIE_MEM_TILE_ROW_START, - 1, - (XAIE_MEM_TILE_ROW_START + 1), - (6 - 1 - 1), - XAie_PartitionProp(), - XAie_BackendType.XAIE_IO_BACKEND_CDO, -) - -devInst = XAie_DevInst() +col = 0 -XAie_SetupPartitionConfig(devInst, 0, 1, 1) -XAie_CfgInitialize(devInst, configPtr) -# XAie_ErrorHandlingInit(devInst) +def test_cdo_gen(): + tile_0_0 = XAie_LocType(0, col) + tile_0_1 = XAie_LocType(1, col) + tile_0_2 = XAie_LocType(2, col) -def test_transaction(): - reset_written_addresses() + configPtr = XAie_Config( + XAIE_DEV_GEN_AIEML, + XAIE_BASE_ADDR, + XAIE_COL_SHIFT, + XAIE_ROW_SHIFT, + 6, + 5, + XAIE_SHIM_ROW, + XAIE_MEM_TILE_ROW_START, + 1, + (XAIE_MEM_TILE_ROW_START + 1), + (6 - 1 - 1), + XAie_PartitionProp(), + XAie_BackendType.XAIE_IO_BACKEND_CDO, + ) - XAie_StartTransaction(devInst, XAIE_TRANSACTION_DISABLE_AUTO_FLUSH) + devInst = XAie_DevInst() + XAie_SetupPartitionConfig(devInst, 0, 1, 1) + XAie_CfgInitialize(devInst, configPtr) XAie_UpdateNpiAddr(devInst, 0) - col = 0 - tile_0_0 = XAie_LocType(0, col) - tile_0_1 = XAie_LocType(1, col) - tile_0_2 = XAie_LocType(2, col) + EnAXIdebug() + setEndianness(Little_Endian) + cdo_fp = Path(__file__).parent.absolute() / "pi_cdo.bin" + startCDOFileStream(str(cdo_fp)) + FileHeader() + + if platform.system() != "Windows": + XAie_ErrorHandlingInit(devInst) XAie_LoadElf( devInst, tile_0_2, str(Path(__file__).parent.absolute() / "pi.elf"), False @@ -114,10 +135,29 @@ def test_transaction(): XAie_EnableAieToShimDmaStrmPort(devInst, tile_0_0, 2) XAie_CoreEnable(devInst, tile_0_2) - txn_inst = XAie_ExportTransactionInstance(devInst) - if platform.system() != "Windows": - _XAie_Txn_Submit(devInst, txn_inst) - - for addr, data in get_written_addresses().items(): - c, r, offset = get_addr_tile(addr) - print(c, r, f"{offset=:08x}", f"{data=:08x}") + configureHeader() + endCurrentCDOFileStream() + + bif_fp = Path(__file__).parent.absolute() / "pi.bif" + with open(bif_fp, "w") as f: + f.write(bootgen.emit_design_bif([cdo_fp])) + + pdi_fp = Path(__file__).parent.absolute() / "pi.pdi" + bootgen.make_design_pdi(str(bif_fp), str(pdi_fp)) + mem_top_json_fp = Path(__file__).parent.absolute() / "mem_topology.json" + with open(mem_top_json_fp, "w") as f: + json.dump(xclbinutil.mem_topology, f) + aie_part_json_fp = Path(__file__).parent.absolute() / "aie_partition.json" + with open(aie_part_json_fp, "w") as f: + json.dump(xclbinutil.emit_partition(pdi_fp, num_cols=1), f) + kernels_json_fp = Path(__file__).parent.absolute() / "kernels.json" + with open(kernels_json_fp, "w") as f: + json.dump(xclbinutil.emit_design_kernel_json(), f) + + pi_xclbin_fp = Path(__file__).parent.absolute() / "pi.xclbin" + xclbinutil.make_xclbin( + str(mem_top_json_fp), + str(aie_part_json_fp), + str(kernels_json_fp), + str(pi_xclbin_fp), + ) diff --git a/third_party/XRT b/third_party/XRT index 7ae1da5..57b0979 160000 --- a/third_party/XRT +++ b/third_party/XRT @@ -1 +1 @@ -Subproject commit 7ae1da5dc208edd0f1a34f85ca6460916033ad1b +Subproject commit 57b097954501efeaa3c0518dcdbc3bf3d0985992 diff --git a/third_party/bootgen b/third_party/bootgen new file mode 160000 index 0000000..92e09bf --- /dev/null +++ b/third_party/bootgen @@ -0,0 +1 @@ +Subproject commit 92e09bf37ea17d7b1f0e102a2548f27fb768651c diff --git a/util/FindOpenSSL.cmake b/util/FindOpenSSL.cmake new file mode 100644 index 0000000..df078eb --- /dev/null +++ b/util/FindOpenSSL.cmake @@ -0,0 +1,804 @@ +################################################################################################### +# THIS IS A PATCHED VERSION FROM https://gitlab.kitware.com/cmake/cmake/-/issues/25702#note_1485779 +# SEE https://gitlab.kitware.com/cmake/cmake/-/issues/25702#note_1485760 +################################################################################################### + +# Distributed under the OSI-approved BSD 3-Clause License. See accompanying +# file Copyright.txt or https://cmake.org/licensing for details. + +#[=======================================================================[.rst: +FindOpenSSL +----------- + +Find the OpenSSL encryption library. + +This module finds an installed OpenSSL library and determines its version. + +.. versionadded:: 3.19 + When a version is requested, it can be specified as a simple value or as a + range. For a detailed description of version range usage and capabilities, + refer to the :command:`find_package` command. + +.. versionadded:: 3.18 + Support for OpenSSL 3.0. + +Optional COMPONENTS +^^^^^^^^^^^^^^^^^^^ + +.. versionadded:: 3.12 + +This module supports two optional COMPONENTS: ``Crypto`` and ``SSL``. Both +components have associated imported targets, as described below. + +Imported Targets +^^^^^^^^^^^^^^^^ + +.. versionadded:: 3.4 + +This module defines the following :prop_tgt:`IMPORTED` targets: + +``OpenSSL::SSL`` + The OpenSSL ``ssl`` library, if found. +``OpenSSL::Crypto`` + The OpenSSL ``crypto`` library, if found. +``OpenSSL::applink`` + .. versionadded:: 3.18 + + The OpenSSL ``applink`` components that might be need to be compiled into + projects under MSVC. This target is available only if found OpenSSL version + is not less than 0.9.8. By linking this target the above OpenSSL targets can + be linked even if the project has different MSVC runtime configurations with + the above OpenSSL targets. This target has no effect on platforms other than + MSVC. + +NOTE: Due to how ``INTERFACE_SOURCES`` are consumed by the consuming target, +unless you certainly know what you are doing, it is always preferred to link +``OpenSSL::applink`` target as ``PRIVATE`` and to make sure that this target is +linked at most once for the whole dependency graph of any library or +executable: + +.. code-block:: cmake + + target_link_libraries(myTarget PRIVATE OpenSSL::applink) + +Otherwise you would probably encounter unexpected random problems when building +and linking, as both the ISO C and the ISO C++ standard claims almost nothing +about what a link process should be. + +Result Variables +^^^^^^^^^^^^^^^^ + +This module will set the following variables in your project: + +``OPENSSL_FOUND`` + System has the OpenSSL library. If no components are requested it only + requires the crypto library. +``OPENSSL_INCLUDE_DIR`` + The OpenSSL include directory. +``OPENSSL_CRYPTO_LIBRARY`` + The OpenSSL crypto library. +``OPENSSL_CRYPTO_LIBRARIES`` + The OpenSSL crypto library and its dependencies. +``OPENSSL_SSL_LIBRARY`` + The OpenSSL SSL library. +``OPENSSL_SSL_LIBRARIES`` + The OpenSSL SSL library and its dependencies. +``OPENSSL_LIBRARIES`` + All OpenSSL libraries and their dependencies. +``OPENSSL_VERSION`` + This is set to ``$major.$minor.$revision$patch`` (e.g. ``0.9.8s``). +``OPENSSL_APPLINK_SOURCE`` + The sources in the target ``OpenSSL::applink`` that is mentioned above. This + variable shall always be undefined if found openssl version is less than + 0.9.8 or if platform is not MSVC. + +Hints +^^^^^ + +The following variables may be set to control search behavior: + +``OPENSSL_ROOT_DIR`` + Set to the root directory of an OpenSSL installation. + +``OPENSSL_USE_STATIC_LIBS`` + .. versionadded:: 3.4 + + Set to ``TRUE`` to look for static libraries. + +``OPENSSL_MSVC_STATIC_RT`` + .. versionadded:: 3.5 + + Set to ``TRUE`` to choose the MT version of the lib. + +``ENV{PKG_CONFIG_PATH}`` + On UNIX-like systems, ``pkg-config`` is used to locate the system OpenSSL. + Set the ``PKG_CONFIG_PATH`` environment variable to look in alternate + locations. Useful on multi-lib systems. +#]=======================================================================] + +set(USE_OUR_FIND_OPENSSL TRUE) + +macro(_OpenSSL_test_and_find_dependencies ssl_library crypto_library) + unset(_OpenSSL_extra_static_deps) + if(UNIX AND + (("${ssl_library}" MATCHES "\\${CMAKE_STATIC_LIBRARY_SUFFIX}$") OR + ("${crypto_library}" MATCHES "\\${CMAKE_STATIC_LIBRARY_SUFFIX}$"))) + set(_OpenSSL_has_dependencies TRUE) + unset(_OpenSSL_has_dependency_zlib) + if(OPENSSL_USE_STATIC_LIBS) + set(_OpenSSL_libs "${_OPENSSL_STATIC_LIBRARIES}") + set(_OpenSSL_ldflags_other "${_OPENSSL_STATIC_LDFLAGS_OTHER}") + else() + set(_OpenSSL_libs "${_OPENSSL_LIBRARIES}") + set(_OpenSSL_ldflags_other "${_OPENSSL_LDFLAGS_OTHER}") + endif() + if(_OpenSSL_libs) + unset(_OpenSSL_has_dependency_dl) + foreach(_OPENSSL_DEP_LIB IN LISTS _OpenSSL_libs) + if (_OPENSSL_DEP_LIB STREQUAL "ssl" OR _OPENSSL_DEP_LIB STREQUAL "crypto") + # ignoring: these are the targets + elseif(_OPENSSL_DEP_LIB STREQUAL CMAKE_DL_LIBS) + set(_OpenSSL_has_dependency_dl TRUE) + elseif(_OPENSSL_DEP_LIB STREQUAL "z") + find_package(ZLIB) + set(_OpenSSL_has_dependency_zlib TRUE) + else() + list(APPEND _OpenSSL_extra_static_deps "${_OPENSSL_DEP_LIB}") + endif() + endforeach() + unset(_OPENSSL_DEP_LIB) + elseif(CMAKE_SYSTEM_NAME STREQUAL "Linux") + set(_OpenSSL_has_dependency_dl TRUE) + endif() + if(_OpenSSL_ldflags_other) + unset(_OpenSSL_has_dependency_threads) + foreach(_OPENSSL_DEP_LDFLAG IN LISTS _OpenSSL_ldflags_other) + if (_OPENSSL_DEP_LDFLAG STREQUAL "-pthread") + set(_OpenSSL_has_dependency_threads TRUE) + find_package(Threads) + endif() + endforeach() + unset(_OPENSSL_DEP_LDFLAG) + elseif(CMAKE_SYSTEM_NAME STREQUAL "Linux") + set(_OpenSSL_has_dependency_threads TRUE) + find_package(Threads) + endif() + unset(_OpenSSL_libs) + unset(_OpenSSL_ldflags_other) + else() + set(_OpenSSL_has_dependencies FALSE) + endif() +endmacro() + +function(_OpenSSL_add_dependencies libraries_var) + if(_OpenSSL_has_dependency_zlib) + list(APPEND ${libraries_var} ${ZLIB_LIBRARY}) + endif() + if(_OpenSSL_has_dependency_threads) + list(APPEND ${libraries_var} ${CMAKE_THREAD_LIBS_INIT}) + endif() + if(_OpenSSL_has_dependency_dl) + list(APPEND ${libraries_var} ${CMAKE_DL_LIBS}) + endif() + list(APPEND ${libraries_var} ${_OpenSSL_extra_static_deps}) + set(${libraries_var} ${${libraries_var}} PARENT_SCOPE) +endfunction() + +function(_OpenSSL_target_add_dependencies target) + if(_OpenSSL_has_dependencies) + if(_OpenSSL_has_dependency_zlib) + set_property( TARGET ${target} APPEND PROPERTY INTERFACE_LINK_LIBRARIES ZLIB::ZLIB ) + endif() + if(_OpenSSL_has_dependency_threads) + set_property( TARGET ${target} APPEND PROPERTY INTERFACE_LINK_LIBRARIES Threads::Threads) + endif() + if(_OpenSSL_has_dependency_dl) + set_property( TARGET ${target} APPEND PROPERTY INTERFACE_LINK_LIBRARIES ${CMAKE_DL_LIBS} ) + endif() + if(_OpenSSL_extra_static_deps) + set_property( TARGET ${target} APPEND PROPERTY INTERFACE_LINK_LIBRARIES ${_OpenSSL_extra_static_deps}) + endif() + endif() + if(WIN32 AND OPENSSL_USE_STATIC_LIBS) + if(WINCE) + set_property( TARGET ${target} APPEND PROPERTY INTERFACE_LINK_LIBRARIES ws2 ) + else() + set_property( TARGET ${target} APPEND PROPERTY INTERFACE_LINK_LIBRARIES ws2_32 ) + endif() + set_property( TARGET ${target} APPEND PROPERTY INTERFACE_LINK_LIBRARIES crypt32 ) + endif() +endfunction() + +if (UNIX) + find_package(PkgConfig QUIET) + pkg_check_modules(_OPENSSL QUIET openssl) +endif () + +# Support preference of static libs by adjusting CMAKE_FIND_LIBRARY_SUFFIXES +if(OPENSSL_USE_STATIC_LIBS) + set(_openssl_ORIG_CMAKE_FIND_LIBRARY_SUFFIXES ${CMAKE_FIND_LIBRARY_SUFFIXES}) + if(MSVC) + set(CMAKE_FIND_LIBRARY_SUFFIXES .lib .a ${CMAKE_FIND_LIBRARY_SUFFIXES}) + else() + set(CMAKE_FIND_LIBRARY_SUFFIXES .a ) + endif() +endif() + +if(CMAKE_SYSTEM_NAME STREQUAL "QNX" AND + CMAKE_SYSTEM_VERSION VERSION_GREATER_EQUAL "7.0" AND CMAKE_SYSTEM_VERSION VERSION_LESS "7.1" AND + OpenSSL_FIND_VERSION VERSION_GREATER_EQUAL "1.1" AND OpenSSL_FIND_VERSION VERSION_LESS "1.2") + # QNX 7.0.x provides openssl 1.0.2 and 1.1.1 in parallel: + # * openssl 1.0.2: libcrypto.so.2 and libssl.so.2, headers under usr/include/openssl + # * openssl 1.1.1: libcrypto1_1.so.2.1 and libssl1_1.so.2.1, header under usr/include/openssl1_1 + # See http://www.qnx.com/developers/articles/rel_6726_0.html + set(_OPENSSL_FIND_PATH_SUFFIX "openssl1_1") + set(_OPENSSL_NAME_POSTFIX "1_1") +else() + set(_OPENSSL_FIND_PATH_SUFFIX "include") +endif() + +if (OPENSSL_ROOT_DIR OR NOT "$ENV{OPENSSL_ROOT_DIR}" STREQUAL "") + set(_OPENSSL_ROOT_HINTS HINTS ${OPENSSL_ROOT_DIR} ENV OPENSSL_ROOT_DIR) + set(_OPENSSL_ROOT_PATHS NO_DEFAULT_PATH) +elseif (MSVC) + # http://www.slproweb.com/products/Win32OpenSSL.html + set(_OPENSSL_ROOT_HINTS + HINTS + "[HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\OpenSSL (32-bit)_is1;Inno Setup: App Path]" + "[HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\OpenSSL (64-bit)_is1;Inno Setup: App Path]" + ) + + if("${CMAKE_SIZEOF_VOID_P}" STREQUAL "8") + set(_arch "Win64") + file(TO_CMAKE_PATH "$ENV{PROGRAMFILES}" _programfiles) + else() + set(_arch "Win32") + set(_progfiles_x86 "ProgramFiles(x86)") + if(NOT "$ENV{${_progfiles_x86}}" STREQUAL "") + # under windows 64 bit machine + file(TO_CMAKE_PATH "$ENV{${_progfiles_x86}}" _programfiles) + else() + # under windows 32 bit machine + file(TO_CMAKE_PATH "$ENV{ProgramFiles}" _programfiles) + endif() + endif() + + set(_OPENSSL_ROOT_PATHS + PATHS + "${_programfiles}/OpenSSL" + "${_programfiles}/OpenSSL-${_arch}" + "C:/OpenSSL/" + "C:/OpenSSL-${_arch}/" + ) + unset(_programfiles) + unset(_arch) +endif () + +set(_OPENSSL_ROOT_HINTS_AND_PATHS + ${_OPENSSL_ROOT_HINTS} + ${_OPENSSL_ROOT_PATHS} + ) + +find_path(OPENSSL_INCLUDE_DIR + NAMES + openssl/ssl.h + ${_OPENSSL_ROOT_HINTS_AND_PATHS} + HINTS + ${_OPENSSL_INCLUDEDIR} + ${_OPENSSL_INCLUDE_DIRS} + PATH_SUFFIXES + ${_OPENSSL_FIND_PATH_SUFFIX} +) + +if(WIN32 AND NOT CYGWIN) + if(MSVC) + # /MD and /MDd are the standard values - if someone wants to use + # others, the libnames have to change here too + # use also ssl and ssleay32 in debug as fallback for openssl < 0.9.8b + # enable OPENSSL_MSVC_STATIC_RT to get the libs build /MT (Multithreaded no-DLL) + # In Visual C++ naming convention each of these four kinds of Windows libraries has it's standard suffix: + # * MD for dynamic-release + # * MDd for dynamic-debug + # * MT for static-release + # * MTd for static-debug + + # Implementation details: + # We are using the libraries located in the VC subdir instead of the parent directory even though : + # libeay32MD.lib is identical to ../libeay32.lib, and + # ssleay32MD.lib is identical to ../ssleay32.lib + # enable OPENSSL_USE_STATIC_LIBS to use the static libs located in lib/VC/static + + if (OPENSSL_MSVC_STATIC_RT) + set(_OPENSSL_MSVC_RT_MODE "MT") + else () + set(_OPENSSL_MSVC_RT_MODE "MD") + endif () + + # Since OpenSSL 1.1, lib names are like libcrypto32MTd.lib and libssl32MTd.lib + if( "${CMAKE_SIZEOF_VOID_P}" STREQUAL "8" ) + set(_OPENSSL_MSVC_ARCH_SUFFIX "64") + else() + set(_OPENSSL_MSVC_ARCH_SUFFIX "32") + endif() + + if(OPENSSL_USE_STATIC_LIBS) + set(_OPENSSL_STATIC_SUFFIX + "_static" + ) + set(_OPENSSL_PATH_SUFFIXES_DEBUG + "lib/VC/x${_OPENSSL_MSVC_ARCH_SUFFIX}/${_OPENSSL_MSVC_RT_MODE}d" + "lib/VC/static" + "VC/static" + "lib" + ) + set(_OPENSSL_PATH_SUFFIXES_RELEASE + "lib/VC/x${_OPENSSL_MSVC_ARCH_SUFFIX}/${_OPENSSL_MSVC_RT_MODE}" + "lib/VC/static" + "VC/static" + "lib" + ) + else() + set(_OPENSSL_STATIC_SUFFIX + "" + ) + set(_OPENSSL_PATH_SUFFIXES_DEBUG + "lib/VC/x${_OPENSSL_MSVC_ARCH_SUFFIX}/${_OPENSSL_MSVC_RT_MODE}d" + "lib/VC" + "VC" + "lib" + ) + set(_OPENSSL_PATH_SUFFIXES_RELEASE + "lib/VC/x${_OPENSSL_MSVC_ARCH_SUFFIX}/${_OPENSSL_MSVC_RT_MODE}" + "lib/VC" + "VC" + "lib" + ) + endif () + message(STATUS "searching for MSVC libs in ${_OPENSSL_PATH_SUFFIXES_DEBUG}") + message(STATUS "searching for MSVC libs in ${_OPENSSL_PATH_SUFFIXES_RELEASE}") + message(FATAL_ERROR) + + find_library(LIB_EAY_DEBUG + NAMES + # When OpenSSL is built with default options, the static library name is suffixed with "_static". + # Looking the "libcrypto_static.lib" with a higher priority than "libcrypto.lib" which is the + # import library of "libcrypto.dll". + libcrypto${_OPENSSL_STATIC_SUFFIX}${_OPENSSL_MSVC_ARCH_SUFFIX}${_OPENSSL_MSVC_RT_MODE}d + libcrypto${_OPENSSL_STATIC_SUFFIX}${_OPENSSL_MSVC_RT_MODE}d + libcrypto${_OPENSSL_STATIC_SUFFIX}d + libeay32${_OPENSSL_STATIC_SUFFIX}${_OPENSSL_MSVC_RT_MODE}d + libeay32${_OPENSSL_STATIC_SUFFIX}d + crypto${_OPENSSL_STATIC_SUFFIX}d + # When OpenSSL is built with the "-static" option, only the static build is produced, + # and it is not suffixed with "_static". + libcrypto${_OPENSSL_MSVC_ARCH_SUFFIX}${_OPENSSL_MSVC_RT_MODE}d + libcrypto${_OPENSSL_MSVC_RT_MODE}d + libcryptod + libeay32${_OPENSSL_MSVC_RT_MODE}d + libeay32d + cryptod + NAMES_PER_DIR + ${_OPENSSL_ROOT_HINTS_AND_PATHS} + PATH_SUFFIXES + ${_OPENSSL_PATH_SUFFIXES_DEBUG} + ) + + find_library(LIB_EAY_RELEASE + NAMES + # When OpenSSL is built with default options, the static library name is suffixed with "_static". + # Looking the "libcrypto_static.lib" with a higher priority than "libcrypto.lib" which is the + # import library of "libcrypto.dll". + libcrypto${_OPENSSL_STATIC_SUFFIX}${_OPENSSL_MSVC_ARCH_SUFFIX}${_OPENSSL_MSVC_RT_MODE} + libcrypto${_OPENSSL_STATIC_SUFFIX}${_OPENSSL_MSVC_RT_MODE} + libcrypto${_OPENSSL_STATIC_SUFFIX} + libeay32${_OPENSSL_STATIC_SUFFIX}${_OPENSSL_MSVC_RT_MODE} + libeay32${_OPENSSL_STATIC_SUFFIX} + crypto${_OPENSSL_STATIC_SUFFIX} + # When OpenSSL is built with the "-static" option, only the static build is produced, + # and it is not suffixed with "_static". + libcrypto${_OPENSSL_MSVC_ARCH_SUFFIX}${_OPENSSL_MSVC_RT_MODE} + libcrypto${_OPENSSL_MSVC_RT_MODE} + libcrypto + libeay32${_OPENSSL_MSVC_RT_MODE} + libeay32 + crypto + NAMES_PER_DIR + ${_OPENSSL_ROOT_HINTS_AND_PATHS} + PATH_SUFFIXES + ${_OPENSSL_PATH_SUFFIXES_RELEASE} + ) + + find_library(SSL_EAY_DEBUG + NAMES + # When OpenSSL is built with default options, the static library name is suffixed with "_static". + # Looking the "libssl_static.lib" with a higher priority than "libssl.lib" which is the + # import library of "libssl.dll". + libssl${_OPENSSL_STATIC_SUFFIX}${_OPENSSL_MSVC_ARCH_SUFFIX}${_OPENSSL_MSVC_RT_MODE}d + libssl${_OPENSSL_STATIC_SUFFIX}${_OPENSSL_MSVC_RT_MODE}d + libssl${_OPENSSL_STATIC_SUFFIX}d + ssleay32${_OPENSSL_STATIC_SUFFIX}${_OPENSSL_MSVC_RT_MODE}d + ssleay32${_OPENSSL_STATIC_SUFFIX}d + ssl${_OPENSSL_STATIC_SUFFIX}d + # When OpenSSL is built with the "-static" option, only the static build is produced, + # and it is not suffixed with "_static". + libssl${_OPENSSL_MSVC_ARCH_SUFFIX}${_OPENSSL_MSVC_RT_MODE}d + libssl${_OPENSSL_MSVC_RT_MODE}d + libssld + ssleay32${_OPENSSL_MSVC_RT_MODE}d + ssleay32d + ssld + NAMES_PER_DIR + ${_OPENSSL_ROOT_HINTS_AND_PATHS} + PATH_SUFFIXES + ${_OPENSSL_PATH_SUFFIXES_DEBUG} + ) + + find_library(SSL_EAY_RELEASE + NAMES + # When OpenSSL is built with default options, the static library name is suffixed with "_static". + # Looking the "libssl_static.lib" with a higher priority than "libssl.lib" which is the + # import library of "libssl.dll". + libssl${_OPENSSL_STATIC_SUFFIX}${_OPENSSL_MSVC_ARCH_SUFFIX}${_OPENSSL_MSVC_RT_MODE} + libssl${_OPENSSL_STATIC_SUFFIX}${_OPENSSL_MSVC_RT_MODE} + libssl${_OPENSSL_STATIC_SUFFIX} + ssleay32${_OPENSSL_STATIC_SUFFIX}${_OPENSSL_MSVC_RT_MODE} + ssleay32${_OPENSSL_STATIC_SUFFIX} + ssl${_OPENSSL_STATIC_SUFFIX} + # When OpenSSL is built with the "-static" option, only the static build is produced, + # and it is not suffixed with "_static". + libssl${_OPENSSL_MSVC_ARCH_SUFFIX}${_OPENSSL_MSVC_RT_MODE} + libssl${_OPENSSL_MSVC_RT_MODE} + libssl + ssleay32${_OPENSSL_MSVC_RT_MODE} + ssleay32 + ssl + NAMES_PER_DIR + ${_OPENSSL_ROOT_HINTS_AND_PATHS} + PATH_SUFFIXES + ${_OPENSSL_PATH_SUFFIXES_RELEASE} + ) + + set(LIB_EAY_LIBRARY_DEBUG "${LIB_EAY_DEBUG}") + set(LIB_EAY_LIBRARY_RELEASE "${LIB_EAY_RELEASE}") + set(SSL_EAY_LIBRARY_DEBUG "${SSL_EAY_DEBUG}") + set(SSL_EAY_LIBRARY_RELEASE "${SSL_EAY_RELEASE}") + + include(SelectLibraryConfigurations) + select_library_configurations(LIB_EAY) + select_library_configurations(SSL_EAY) + + mark_as_advanced(LIB_EAY_LIBRARY_DEBUG LIB_EAY_LIBRARY_RELEASE + SSL_EAY_LIBRARY_DEBUG SSL_EAY_LIBRARY_RELEASE) + set(OPENSSL_SSL_LIBRARY ${SSL_EAY_LIBRARY} ) + set(OPENSSL_CRYPTO_LIBRARY ${LIB_EAY_LIBRARY} ) + elseif(MINGW) + # same player, for MinGW + set(LIB_EAY_NAMES crypto libeay32) + set(SSL_EAY_NAMES ssl ssleay32) + find_library(LIB_EAY + NAMES + ${LIB_EAY_NAMES} + NAMES_PER_DIR + ${_OPENSSL_ROOT_HINTS_AND_PATHS} + PATH_SUFFIXES + "lib/MinGW" + "lib" + "lib64" + ) + + find_library(SSL_EAY + NAMES + ${SSL_EAY_NAMES} + NAMES_PER_DIR + ${_OPENSSL_ROOT_HINTS_AND_PATHS} + PATH_SUFFIXES + "lib/MinGW" + "lib" + "lib64" + ) + + mark_as_advanced(SSL_EAY LIB_EAY) + set(OPENSSL_SSL_LIBRARY ${SSL_EAY} ) + set(OPENSSL_CRYPTO_LIBRARY ${LIB_EAY} ) + unset(LIB_EAY_NAMES) + unset(SSL_EAY_NAMES) + else() + # Not sure what to pick for -say- intel, let's use the toplevel ones and hope someone report issues: + find_library(LIB_EAY + NAMES + libcrypto + libeay32 + NAMES_PER_DIR + ${_OPENSSL_ROOT_HINTS_AND_PATHS} + HINTS + ${_OPENSSL_LIBDIR} + PATH_SUFFIXES + lib + ) + + find_library(SSL_EAY + NAMES + libssl + ssleay32 + NAMES_PER_DIR + ${_OPENSSL_ROOT_HINTS_AND_PATHS} + HINTS + ${_OPENSSL_LIBDIR} + PATH_SUFFIXES + lib + ) + + mark_as_advanced(SSL_EAY LIB_EAY) + set(OPENSSL_SSL_LIBRARY ${SSL_EAY} ) + set(OPENSSL_CRYPTO_LIBRARY ${LIB_EAY} ) + endif() +else() + + find_library(OPENSSL_SSL_LIBRARY + NAMES + ssl${_OPENSSL_NAME_POSTFIX} + ssleay32 + ssleay32MD + NAMES_PER_DIR + ${_OPENSSL_ROOT_HINTS_AND_PATHS} + HINTS + ${_OPENSSL_LIBDIR} + ${_OPENSSL_LIBRARY_DIRS} + PATH_SUFFIXES + lib lib64 + ) + + find_library(OPENSSL_CRYPTO_LIBRARY + NAMES + crypto${_OPENSSL_NAME_POSTFIX} + NAMES_PER_DIR + ${_OPENSSL_ROOT_HINTS_AND_PATHS} + HINTS + ${_OPENSSL_LIBDIR} + ${_OPENSSL_LIBRARY_DIRS} + PATH_SUFFIXES + lib lib64 + ) + + mark_as_advanced(OPENSSL_CRYPTO_LIBRARY OPENSSL_SSL_LIBRARY) + +endif() + +set(OPENSSL_SSL_LIBRARIES ${OPENSSL_SSL_LIBRARY}) +set(OPENSSL_CRYPTO_LIBRARIES ${OPENSSL_CRYPTO_LIBRARY}) +set(OPENSSL_LIBRARIES ${OPENSSL_SSL_LIBRARIES} ${OPENSSL_CRYPTO_LIBRARIES} ) +_OpenSSL_test_and_find_dependencies("${OPENSSL_SSL_LIBRARY}" "${OPENSSL_CRYPTO_LIBRARY}") +if(_OpenSSL_has_dependencies) + _OpenSSL_add_dependencies( OPENSSL_SSL_LIBRARIES ) + _OpenSSL_add_dependencies( OPENSSL_CRYPTO_LIBRARIES ) + _OpenSSL_add_dependencies( OPENSSL_LIBRARIES ) +endif() + +function(from_hex HEX DEC) + string(TOUPPER "${HEX}" HEX) + set(_res 0) + string(LENGTH "${HEX}" _strlen) + + while (_strlen GREATER 0) + math(EXPR _res "${_res} * 16") + string(SUBSTRING "${HEX}" 0 1 NIBBLE) + string(SUBSTRING "${HEX}" 1 -1 HEX) + if (NIBBLE STREQUAL "A") + math(EXPR _res "${_res} + 10") + elseif (NIBBLE STREQUAL "B") + math(EXPR _res "${_res} + 11") + elseif (NIBBLE STREQUAL "C") + math(EXPR _res "${_res} + 12") + elseif (NIBBLE STREQUAL "D") + math(EXPR _res "${_res} + 13") + elseif (NIBBLE STREQUAL "E") + math(EXPR _res "${_res} + 14") + elseif (NIBBLE STREQUAL "F") + math(EXPR _res "${_res} + 15") + else() + math(EXPR _res "${_res} + ${NIBBLE}") + endif() + + string(LENGTH "${HEX}" _strlen) + endwhile() + + set(${DEC} ${_res} PARENT_SCOPE) +endfunction() + +if(OPENSSL_INCLUDE_DIR AND EXISTS "${OPENSSL_INCLUDE_DIR}/openssl/opensslv.h") + file(STRINGS "${OPENSSL_INCLUDE_DIR}/openssl/opensslv.h" openssl_version_str + REGEX "^#[\t ]*define[\t ]+OPENSSL_VERSION_NUMBER[\t ]+0x([0-9a-fA-F])+.*") + + if(openssl_version_str) + # The version number is encoded as 0xMNNFFPPS: major minor fix patch status + # The status gives if this is a developer or prerelease and is ignored here. + # Major, minor, and fix directly translate into the version numbers shown in + # the string. The patch field translates to the single character suffix that + # indicates the bug fix state, which 00 -> nothing, 01 -> a, 02 -> b and so + # on. + + string(REGEX REPLACE "^.*OPENSSL_VERSION_NUMBER[\t ]+0x([0-9a-fA-F])([0-9a-fA-F][0-9a-fA-F])([0-9a-fA-F][0-9a-fA-F])([0-9a-fA-F][0-9a-fA-F])([0-9a-fA-F]).*$" + "\\1;\\2;\\3;\\4;\\5" OPENSSL_VERSION_LIST "${openssl_version_str}") + list(GET OPENSSL_VERSION_LIST 0 OPENSSL_VERSION_MAJOR) + list(GET OPENSSL_VERSION_LIST 1 OPENSSL_VERSION_MINOR) + from_hex("${OPENSSL_VERSION_MINOR}" OPENSSL_VERSION_MINOR) + list(GET OPENSSL_VERSION_LIST 2 OPENSSL_VERSION_FIX) + from_hex("${OPENSSL_VERSION_FIX}" OPENSSL_VERSION_FIX) + list(GET OPENSSL_VERSION_LIST 3 OPENSSL_VERSION_PATCH) + + if (NOT OPENSSL_VERSION_PATCH STREQUAL "00") + from_hex("${OPENSSL_VERSION_PATCH}" _tmp) + # 96 is the ASCII code of 'a' minus 1 + math(EXPR OPENSSL_VERSION_PATCH_ASCII "${_tmp} + 96") + unset(_tmp) + # Once anyone knows how OpenSSL would call the patch versions beyond 'z' + # this should be updated to handle that, too. This has not happened yet + # so it is simply ignored here for now. + string(ASCII "${OPENSSL_VERSION_PATCH_ASCII}" OPENSSL_VERSION_PATCH_STRING) + endif () + + set(OPENSSL_VERSION "${OPENSSL_VERSION_MAJOR}.${OPENSSL_VERSION_MINOR}.${OPENSSL_VERSION_FIX}${OPENSSL_VERSION_PATCH_STRING}") + else () + # Since OpenSSL 3.0.0, the new version format is MAJOR.MINOR.PATCH and + # a new OPENSSL_VERSION_STR macro contains exactly that + file(STRINGS "${OPENSSL_INCLUDE_DIR}/openssl/opensslv.h" OPENSSL_VERSION_STR + REGEX "^#[\t ]*define[\t ]+OPENSSL_VERSION_STR[\t ]+\"([0-9])+\\.([0-9])+\\.([0-9])+\".*") + string(REGEX REPLACE "^.*OPENSSL_VERSION_STR[\t ]+\"([0-9]+\\.[0-9]+\\.[0-9]+)\".*$" + "\\1" OPENSSL_VERSION_STR "${OPENSSL_VERSION_STR}") + + set(OPENSSL_VERSION "${OPENSSL_VERSION_STR}") + + # Setting OPENSSL_VERSION_MAJOR OPENSSL_VERSION_MINOR and OPENSSL_VERSION_FIX + string(REGEX MATCHALL "([0-9])+" OPENSSL_VERSION_NUMBER "${OPENSSL_VERSION}") + list(POP_FRONT OPENSSL_VERSION_NUMBER + OPENSSL_VERSION_MAJOR + OPENSSL_VERSION_MINOR + OPENSSL_VERSION_FIX) + + unset(OPENSSL_VERSION_NUMBER) + unset(OPENSSL_VERSION_STR) + endif () +endif () + +foreach(_comp IN LISTS OpenSSL_FIND_COMPONENTS) + if(_comp STREQUAL "Crypto") + if(EXISTS "${OPENSSL_INCLUDE_DIR}" AND + (EXISTS "${OPENSSL_CRYPTO_LIBRARY}" OR + EXISTS "${LIB_EAY_LIBRARY_DEBUG}" OR + EXISTS "${LIB_EAY_LIBRARY_RELEASE}") + ) + set(OpenSSL_${_comp}_FOUND TRUE) + else() + set(OpenSSL_${_comp}_FOUND FALSE) + endif() + elseif(_comp STREQUAL "SSL") + if(EXISTS "${OPENSSL_INCLUDE_DIR}" AND + (EXISTS "${OPENSSL_SSL_LIBRARY}" OR + EXISTS "${SSL_EAY_LIBRARY_DEBUG}" OR + EXISTS "${SSL_EAY_LIBRARY_RELEASE}") + ) + set(OpenSSL_${_comp}_FOUND TRUE) + else() + set(OpenSSL_${_comp}_FOUND FALSE) + endif() + else() + message(WARNING "${_comp} is not a valid OpenSSL component") + set(OpenSSL_${_comp}_FOUND FALSE) + endif() +endforeach() +unset(_comp) + +include(FindPackageHandleStandardArgs) +find_package_handle_standard_args(OpenSSL + REQUIRED_VARS + OPENSSL_CRYPTO_LIBRARY + OPENSSL_INCLUDE_DIR + VERSION_VAR + OPENSSL_VERSION + HANDLE_VERSION_RANGE + HANDLE_COMPONENTS + FAIL_MESSAGE + "Could NOT find OpenSSL, try to set the path to OpenSSL root folder in the system variable OPENSSL_ROOT_DIR" +) + +mark_as_advanced(OPENSSL_INCLUDE_DIR) + +if(OPENSSL_FOUND) + if(NOT TARGET OpenSSL::Crypto AND + (EXISTS "${OPENSSL_CRYPTO_LIBRARY}" OR + EXISTS "${LIB_EAY_LIBRARY_DEBUG}" OR + EXISTS "${LIB_EAY_LIBRARY_RELEASE}") + ) + add_library(OpenSSL::Crypto UNKNOWN IMPORTED) + set_target_properties(OpenSSL::Crypto PROPERTIES + INTERFACE_INCLUDE_DIRECTORIES "${OPENSSL_INCLUDE_DIR}") + if(EXISTS "${OPENSSL_CRYPTO_LIBRARY}") + set_target_properties(OpenSSL::Crypto PROPERTIES + IMPORTED_LINK_INTERFACE_LANGUAGES "C" + IMPORTED_LOCATION "${OPENSSL_CRYPTO_LIBRARY}") + endif() + if(EXISTS "${LIB_EAY_LIBRARY_RELEASE}") + set_property(TARGET OpenSSL::Crypto APPEND PROPERTY + IMPORTED_CONFIGURATIONS RELEASE) + set_target_properties(OpenSSL::Crypto PROPERTIES + IMPORTED_LINK_INTERFACE_LANGUAGES_RELEASE "C" + IMPORTED_LOCATION_RELEASE "${LIB_EAY_LIBRARY_RELEASE}") + endif() + if(EXISTS "${LIB_EAY_LIBRARY_DEBUG}") + set_property(TARGET OpenSSL::Crypto APPEND PROPERTY + IMPORTED_CONFIGURATIONS DEBUG) + set_target_properties(OpenSSL::Crypto PROPERTIES + IMPORTED_LINK_INTERFACE_LANGUAGES_DEBUG "C" + IMPORTED_LOCATION_DEBUG "${LIB_EAY_LIBRARY_DEBUG}") + endif() + _OpenSSL_target_add_dependencies(OpenSSL::Crypto) + endif() + + if(NOT TARGET OpenSSL::SSL AND + (EXISTS "${OPENSSL_SSL_LIBRARY}" OR + EXISTS "${SSL_EAY_LIBRARY_DEBUG}" OR + EXISTS "${SSL_EAY_LIBRARY_RELEASE}") + ) + add_library(OpenSSL::SSL UNKNOWN IMPORTED) + set_target_properties(OpenSSL::SSL PROPERTIES + INTERFACE_INCLUDE_DIRECTORIES "${OPENSSL_INCLUDE_DIR}") + if(EXISTS "${OPENSSL_SSL_LIBRARY}") + set_target_properties(OpenSSL::SSL PROPERTIES + IMPORTED_LINK_INTERFACE_LANGUAGES "C" + IMPORTED_LOCATION "${OPENSSL_SSL_LIBRARY}") + endif() + if(EXISTS "${SSL_EAY_LIBRARY_RELEASE}") + set_property(TARGET OpenSSL::SSL APPEND PROPERTY + IMPORTED_CONFIGURATIONS RELEASE) + set_target_properties(OpenSSL::SSL PROPERTIES + IMPORTED_LINK_INTERFACE_LANGUAGES_RELEASE "C" + IMPORTED_LOCATION_RELEASE "${SSL_EAY_LIBRARY_RELEASE}") + endif() + if(EXISTS "${SSL_EAY_LIBRARY_DEBUG}") + set_property(TARGET OpenSSL::SSL APPEND PROPERTY + IMPORTED_CONFIGURATIONS DEBUG) + set_target_properties(OpenSSL::SSL PROPERTIES + IMPORTED_LINK_INTERFACE_LANGUAGES_DEBUG "C" + IMPORTED_LOCATION_DEBUG "${SSL_EAY_LIBRARY_DEBUG}") + endif() + if(TARGET OpenSSL::Crypto) + set_target_properties(OpenSSL::SSL PROPERTIES + INTERFACE_LINK_LIBRARIES OpenSSL::Crypto) + endif() + _OpenSSL_target_add_dependencies(OpenSSL::SSL) + endif() + + if("${OPENSSL_VERSION_MAJOR}.${OPENSSL_VERSION_MINOR}.${OPENSSL_VERSION_FIX}" VERSION_GREATER_EQUAL "0.9.8") + if(MSVC) + if(EXISTS "${OPENSSL_INCLUDE_DIR}") + set(_OPENSSL_applink_paths PATHS ${OPENSSL_INCLUDE_DIR}) + endif() + find_file(OPENSSL_APPLINK_SOURCE + NAMES + openssl/applink.c + ${_OPENSSL_applink_paths} + NO_DEFAULT_PATH) + if(OPENSSL_APPLINK_SOURCE) + set(_OPENSSL_applink_interface_srcs ${OPENSSL_APPLINK_SOURCE}) + endif() + endif() + if(NOT TARGET OpenSSL::applink) + add_library(OpenSSL::applink INTERFACE IMPORTED) + set_property(TARGET OpenSSL::applink APPEND + PROPERTY INTERFACE_SOURCES + ${_OPENSSL_applink_interface_srcs}) + endif() + endif() +endif() + +# Restore the original find library ordering +if(OPENSSL_USE_STATIC_LIBS) + set(CMAKE_FIND_LIBRARY_SUFFIXES ${_openssl_ORIG_CMAKE_FIND_LIBRARY_SUFFIXES}) +endif() + +unset(_OPENSSL_FIND_PATH_SUFFIX) +unset(_OPENSSL_NAME_POSTFIX) +unset(_OpenSSL_extra_static_deps) +unset(_OpenSSL_has_dependency_dl) +unset(_OpenSSL_has_dependency_threads) +unset(_OpenSSL_has_dependency_zlib) diff --git a/scripts/gen_cdo.py b/util/gen_cdo.py similarity index 100% rename from scripts/gen_cdo.py rename to util/gen_cdo.py diff --git a/scripts/gen_xaie_ctypes.py b/util/gen_xaie_ctypes.py similarity index 97% rename from scripts/gen_xaie_ctypes.py rename to util/gen_xaie_ctypes.py index 603c06c..44567ca 100644 --- a/scripts/gen_xaie_ctypes.py +++ b/util/gen_xaie_ctypes.py @@ -44,7 +44,6 @@ def __init__(self, outpath, options, data): # before everything else self.file.write("from .typed_ctypes_enum import *\n") - self.file.write("from .cdo import *\n") self.print_preamble() self.file.write("\n") @@ -247,7 +246,10 @@ def print_loader(self): self.file.write("\n") -EXCLUDED_HEADERS = {"xaie_interrupt.h"} +EXCLUDED_HEADERS = set() + +if platform.system() == "Windows": + EXCLUDED_HEADERS.add("xaie_interrupt.h") def generate(xaie_build_include_dir: Path, output: Path, elf_include_dir: Path): @@ -281,7 +283,11 @@ def generate(xaie_build_include_dir: Path, output: Path, elf_include_dir: Path): cpp_undefines=[], debug_level=0, embed_preamble=True, - exclude_symbols=["XAie_MapIrqIdToCols"], + exclude_symbols=[ + "XAie_MapIrqIdToCols", + "XAie_DisableErrorInterrupts", + "XAie_BacktrackErrorInterrupts", + ], header_template=None, # blows up file include_macros=False, diff --git a/scripts/stuff.cmake b/util/helpers.cmake similarity index 100% rename from scripts/stuff.cmake rename to util/helpers.cmake index 3fb25be..b0aa773 100644 --- a/scripts/stuff.cmake +++ b/util/helpers.cmake @@ -43,25 +43,6 @@ function(print_target_properties target) endforeach() endfunction() -macro(configure_python_dev_packages) - # apparently alma 8 doesn't have the full development lib??? - set(_python_development_component Development.Module) - - find_package( - Python3 ${PY_VERSION} - COMPONENTS Interpreter ${_python_development_component} - REQUIRED) - unset(_python_development_component) - message(STATUS "Found python include dirs: ${Python3_INCLUDE_DIRS}") - message(STATUS "Found python libraries: ${Python3_LIBRARIES}") - detect_pybind11_install() - find_package(pybind11 CONFIG REQUIRED) - message(STATUS "Found pybind11 v${pybind11_VERSION}: ${pybind11_INCLUDE_DIR}") - message(STATUS "Python prefix = '${PYTHON_MODULE_PREFIX}', " - "suffix = '${PYTHON_MODULE_SUFFIX}', " - "extension = '${PYTHON_MODULE_EXTENSION}") -endmacro() - # Detects a pybind11 package installed in the current python environment and # sets variables to allow it to be found. This allows pybind11 to be installed # via pip, which typically yields a much more recent version than the OS @@ -93,3 +74,22 @@ function(detect_pybind11_install) PARENT_SCOPE) endif() endfunction() + +macro(configure_python_dev_packages) + # apparently alma 8 doesn't have the full development lib??? + set(_python_development_component Development.Module) + + find_package( + Python3 ${PY_VERSION} + COMPONENTS Interpreter ${_python_development_component} + REQUIRED) + unset(_python_development_component) + message(STATUS "Found python include dirs: ${Python3_INCLUDE_DIRS}") + message(STATUS "Found python libraries: ${Python3_LIBRARIES}") + detect_pybind11_install() + find_package(pybind11 CONFIG REQUIRED) + message(STATUS "Found pybind11 v${pybind11_VERSION}: ${pybind11_INCLUDE_DIR}") + message(STATUS "Python prefix = '${PYTHON_MODULE_PREFIX}', " + "suffix = '${PYTHON_MODULE_SUFFIX}', " + "extension = '${PYTHON_MODULE_EXTENSION}") +endmacro() diff --git a/util/remove_path_limit.ps1 b/util/remove_path_limit.ps1 new file mode 100644 index 0000000..e1eff97 --- /dev/null +++ b/util/remove_path_limit.ps1 @@ -0,0 +1,2 @@ +# https://stackoverflow.com/a/68353105 +New-ItemProperty -Path "HKLM:\SYSTEM\CurrentControlSet\Control\FileSystem" -Name "LongPathsEnabled" -Value 1 -PropertyType DWORD -Force \ No newline at end of file diff --git a/xaiepy/.gitignore b/xaiepy/.gitignore index b1fbdb9..93f5256 100644 --- a/xaiepy/.gitignore +++ b/xaiepy/.gitignore @@ -1,4 +1 @@ -libxaie.so -_cdo.so -_cdo.dll -__init__.py +__init__.py \ No newline at end of file diff --git a/xaiepy/bootgen.cpp b/xaiepy/bootgen.cpp new file mode 100644 index 0000000..3d506fd --- /dev/null +++ b/xaiepy/bootgen.cpp @@ -0,0 +1,23 @@ +// +// Created by mlevental on 5/12/24. +// + +#include + +#include "bootimage.h" + +using namespace pybind11::literals; + +PYBIND11_MODULE(_bootgen, m) { + m.def("make_design_pdi", [](std::string bifFilePath, std::string outputFilePath, bool debug) { + Options options; + if (debug) + options.SetLogLevel(LogLevel::TRACE); + options.SetArchType(Arch::VERSAL); + options.SetBifFilename(bifFilePath); + options.SetOverwrite(true); + options.InsertOutputFileNames(outputFilePath); + BIF_File bif(bifFilePath); + bif.Process(options); + }, "bif_fp"_a, "output_fp"_a, "debug"_a = false); +} \ No newline at end of file diff --git a/xaiepy/bootgen.py b/xaiepy/bootgen.py new file mode 100644 index 0000000..4ea7c1a --- /dev/null +++ b/xaiepy/bootgen.py @@ -0,0 +1,22 @@ +from ._bootgen import * +from textwrap import dedent + + +def emit_design_bif(cdo_files): + cdo_files = "\n".join(map(lambda c: f"file={c}", cdo_files)) + return dedent( + f"""\ + all: + {{ + id_code = 0x14ca8093 + extended_id_code = 0x01 + image + {{ + name=aie_image, id=0x1c000000 + {{ type=cdo + {cdo_files} + }} + }} + }} + """ + ) diff --git a/xaiepy/cdo.py b/xaiepy/cdo.py index 52e4c6a..abde922 100644 --- a/xaiepy/cdo.py +++ b/xaiepy/cdo.py @@ -1,209 +1,96 @@ -import logging -import platform -import sys -from collections import OrderedDict - -logger = logging.getLogger(__name__) - -if platform.system() != "Windows": - from os import RTLD_GLOBAL, RTLD_NOW - - old = sys.getdlopenflags() - try: - sys.setdlopenflags(RTLD_GLOBAL | RTLD_NOW) - from ._cdo import ffi - finally: - sys.setdlopenflags(old) -else: - from ._cdo import ffi - -# produced from third_party/aie-rt/driver/src/global/xaiemlgbl_params.h -_CORE_TILE_MAP = { - 0x00031150: 0x000FFFFF, # XAIEMLGBL_CORE_MODULE_CORE_LE - 0x00031170: 0x00001800, # XAIEMLGBL_CORE_MODULE_CORE_CR - 0x00032000: 0x00000002, # XAIEMLGBL_CORE_MODULE_CORE_CONTROL - 0x00032004: 0x00000002, # XAIEMLGBL_CORE_MODULE_CORE_STATUS - 0x000340F0: 0xFFFFFFFF, # XAIEMLGBL_CORE_MODULE_TIMER_TRIG_EVENT_LOW_VALUE - 0x000340F4: 0xFFFFFFFF, # XAIEMLGBL_CORE_MODULE_TIMER_TRIG_EVENT_HIGH_VALUE - 0x00034200: 0x00000002, # XAIEMLGBL_CORE_MODULE_EVENT_STATUS0 - 0x00034500: 0x00000FFF, # XAIEMLGBL_CORE_MODULE_EVENT_GROUP_0_ENABLE - 0x00034504: 0x0000003F, # XAIEMLGBL_CORE_MODULE_EVENT_GROUP_PC_ENABLE - 0x00034508: 0x000001FF, # XAIEMLGBL_CORE_MODULE_EVENT_GROUP_CORE_STALL_ENABLE - 0x0003450C: 0x00001FFF, # XAIEMLGBL_CORE_MODULE_EVENT_GROUP_CORE_PROGRAM_FLOW_ENABLE - 0x00034510: 0x01FFFFFF, # XAIEMLGBL_CORE_MODULE_EVENT_GROUP_ERRORS0_ENABLE - 0x00034514: 0x01FFFFFF, # XAIEMLGBL_CORE_MODULE_EVENT_GROUP_ERRORS1_ENABLE - 0x00034518: 0xFFFFFFFF, # XAIEMLGBL_CORE_MODULE_EVENT_GROUP_STREAM_SWITCH_ENABLE - 0x0003451C: 0x0000FFFF, # XAIEMLGBL_CORE_MODULE_EVENT_GROUP_BROADCAST_ENABLE - 0x00034520: 0x0000000F, # XAIEMLGBL_CORE_MODULE_EVENT_GROUP_USER_EVENT_ENABLE - 0x00036030: 0x0000000F, # XAIEMLGBL_CORE_MODULE_TILE_CONTROL - 0x0003FF38: 0x00000007, # XAIEMLGBL_CORE_MODULE_STREAM_SWITCH_ADAPTIVE_CLOCK_GATE_ABORT_PERIOD - 0x00060000: 0x00000037, # XAIEMLGBL_CORE_MODULE_MODULE_CLOCK_CONTROL - 0x000140F0: 0xFFFFFFFF, # XAIEMLGBL_CORE_MODULE_TIMER_TRIG_EVENT_LOW_VALUE - 0x000140F4: 0xFFFFFFFF, # XAIEMLGBL_CORE_MODULE_TIMER_TRIG_EVENT_HIGH_VALUE - 0x00014200: 0x00000002, # XAIEMLGBL_CORE_MODULE_EVENT_STATUS0 - 0x00014500: 0x000003FF, # XAIEMLGBL_CORE_MODULE_EVENT_GROUP_0_ENABLE - 0x00014504: 0x00000003, # XAIEMLGBL_CORE_MODULE_EVENT_GROUP_WATCHPOINT_ENABLE - 0x00014508: 0x00FFFFFF, # XAIEMLGBL_CORE_MODULE_EVENT_GROUP_DMA_ENABLE - 0x0001450C: 0xFFFFFFFF, # XAIEMLGBL_CORE_MODULE_EVENT_GROUP_LOCK_ENABLE - 0x00014510: 0x000000FF, # XAIEMLGBL_CORE_MODULE_EVENT_GROUP_MEMORY_CONFLICT_ENABLE - 0x00014514: 0x0000FFFF, # XAIEMLGBL_CORE_MODULE_EVENT_GROUP_ERROR_ENABLE - 0x00014518: 0x0000FFFF, # XAIEMLGBL_CORE_MODULE_EVENT_GROUP_BROADCAST_ENABLE - 0x0001451C: 0x0000000F, # XAIEMLGBL_CORE_MODULE_EVENT_GROUP_USER_EVENT_ENABLE - 0x0001F100: 0x0000003F, # XAIEMLGBL_CORE_MODULE_LOCKS_EVENT_SELECTION_0 - 0x0001F104: 0x0000003F, # XAIEMLGBL_CORE_MODULE_LOCKS_EVENT_SELECTION_1 - 0x0001F108: 0x0000003F, # XAIEMLGBL_CORE_MODULE_LOCKS_EVENT_SELECTION_2 - 0x0001F10C: 0x0000003F, # XAIEMLGBL_CORE_MODULE_LOCKS_EVENT_SELECTION_3 - 0x0001F110: 0x0000003F, # XAIEMLGBL_CORE_MODULE_LOCKS_EVENT_SELECTION_4 - 0x0001F114: 0x0000003F, # XAIEMLGBL_CORE_MODULE_LOCKS_EVENT_SELECTION_5 - 0x0001F118: 0x0000003F, # XAIEMLGBL_CORE_MODULE_LOCKS_EVENT_SELECTION_6 - 0x0001F11C: 0x0000003F, # XAIEMLGBL_CORE_MODULE_LOCKS_EVENT_SELECTION_7 -} - -_MEM_TILE_MAP = { - 0x000940F0: 0xFFFFFFFF, # XAIEMLGBL_CORE_MODULE_TIMER_TRIG_EVENT_LOW_VALUE - 0x000940F4: 0xFFFFFFFF, # XAIEMLGBL_CORE_MODULE_TIMER_TRIG_EVENT_HIGH_VALUE - 0x00094200: 0x00000002, # XAIEMLGBL_CORE_MODULE_EVENT_STATUS0 - 0x00094500: 0x00000FFF, # XAIEMLGBL_CORE_MODULE_EVENT_GROUP_0_ENABLE - 0x00094504: 0x0000000F, # XAIEMLGBL_CORE_MODULE_EVENT_GROUP_WATCHPOINT_ENABLE - 0x00094508: 0x00FFFFFF, # XAIEMLGBL_CORE_MODULE_EVENT_GROUP_DMA_ENABLE - 0x0009450C: 0xFFFFFFFF, # XAIEMLGBL_CORE_MODULE_EVENT_GROUP_LOCK_ENABLE - 0x00094510: 0xFFFFFFFF, # XAIEMLGBL_CORE_MODULE_EVENT_GROUP_STREAM_SWITCH_ENABLE - 0x00094514: 0x0000FFFF, # XAIEMLGBL_CORE_MODULE_EVENT_GROUP_MEMORY_CONFLICT_ENABLE - 0x00094518: 0x00000FFF, # XAIEMLGBL_CORE_MODULE_EVENT_GROUP_ERROR_ENABLE - 0x0009451C: 0x0000FFFF, # XAIEMLGBL_CORE_MODULE_EVENT_GROUP_BROADCAST_ENABLE - 0x00094520: 0x00000003, # XAIEMLGBL_CORE_MODULE_EVENT_GROUP_USER_EVENT_ENABLE - 0x00096030: 0x0000000F, # XAIEMLGBL_CORE_MODULE_TILE_CONTROL - 0x00096048: 0x00000002, # XAIEMLGBL_CORE_MODULE_MEMORY_CONTROL - 0x000B0F38: 0x00000007, # XAIEMLGBL_CORE_MODULE_STREAM_SWITCH_ADAPTIVE_CLOCK_GATE_ABORT_PERIOD - 0x000C0400: 0x0000003F, # XAIEMLGBL_CORE_MODULE_LOCKS_EVENT_SELECTION_0 - 0x000C0404: 0x0000003F, # XAIEMLGBL_CORE_MODULE_LOCKS_EVENT_SELECTION_1 - 0x000C0408: 0x0000003F, # XAIEMLGBL_CORE_MODULE_LOCKS_EVENT_SELECTION_2 - 0x000C040C: 0x0000003F, # XAIEMLGBL_CORE_MODULE_LOCKS_EVENT_SELECTION_3 - 0x000C0410: 0x0000003F, # XAIEMLGBL_CORE_MODULE_LOCKS_EVENT_SELECTION_4 - 0x000C0414: 0x0000003F, # XAIEMLGBL_CORE_MODULE_LOCKS_EVENT_SELECTION_5 - 0x000C0418: 0x0000003F, # XAIEMLGBL_CORE_MODULE_LOCKS_EVENT_SELECTION_6 - 0x000C041C: 0x0000003F, # XAIEMLGBL_CORE_MODULE_LOCKS_EVENT_SELECTION_7 - 0x000FFF00: 0x00000033, # XAIEMLGBL_CORE_MODULE_MODULE_CLOCK_CONTROL -} - -_NOC_TILE_MAP = { - 0x00014100: 0x0000003F, # XAIEMLGBL_NOC_MODULE_LOCKS_EVENT_SELECTION_0 - 0x00014104: 0x0000003F, # XAIEMLGBL_NOC_MODULE_LOCKS_EVENT_SELECTION_1 - 0x00014108: 0x0000003F, # XAIEMLGBL_NOC_MODULE_LOCKS_EVENT_SELECTION_2 - 0x0001410C: 0x0000003F, # XAIEMLGBL_NOC_MODULE_LOCKS_EVENT_SELECTION_3 - 0x00014110: 0x0000003F, # XAIEMLGBL_NOC_MODULE_LOCKS_EVENT_SELECTION_4 - 0x00014114: 0x0000003F, # XAIEMLGBL_NOC_MODULE_LOCKS_EVENT_SELECTION_5 - 0x00033000: 0x000000DB, # XAIEMLGBL_NOC_MODULE_PL_INTERFACE_UPSIZER_CONFIG - 0x00033004: 0x000006DB, # XAIEMLGBL_NOC_MODULE_PL_INTERFACE_DOWNSIZER_CONFIG - 0x000340F0: 0xFFFFFFFF, # XAIEMLGBL_NOC_MODULE_TIMER_TRIG_EVENT_LOW_VALUE - 0x000340F4: 0xFFFFFFFF, # XAIEMLGBL_NOC_MODULE_TIMER_TRIG_EVENT_HIGH_VALUE - 0x00034200: 0x00000002, # XAIEMLGBL_NOC_MODULE_EVENT_STATUS0 - 0x00034500: 0x000003FF, # XAIEMLGBL_NOC_MODULE_EVENT_GROUP_0_ENABLE - 0x00034504: 0x00FFFFFF, # XAIEMLGBL_NOC_MODULE_EVENT_GROUP_DMA_ACTIVITY_ENABLE - 0x00034508: 0x00FFFFFF, # XAIEMLGBL_NOC_MODULE_EVENT_GROUP_LOCK_ENABLE - 0x0003450C: 0x00000FFF, # XAIEMLGBL_NOC_MODULE_EVENT_GROUP_ERRORS_ENABLE - 0x00034510: 0xFFFFFFFF, # XAIEMLGBL_NOC_MODULE_EVENT_GROUP_STREAM_SWITCH_ENABLE - 0x00034514: 0x0000FFFF, # XAIEMLGBL_NOC_MODULE_EVENT_GROUP_BROADCAST_A_ENABLE - 0x00036030: 0x0000000F, # XAIEMLGBL_NOC_MODULE_TILE_CONTROL - 0x0003FF38: 0x00000007, # XAIEMLGBL_NOC_MODULE_STREAM_SWITCH_ADAPTIVE_CLOCK_GATE_ABORT_PERIOD - 0x000FFF00: 0x0000003B, # XAIEMLGBL_NOC_MODULE_MODULE_CLOCK_CONTROL_0 - 0x000FFF04: 0x00000001, # XAIEMLGBL_NOC_MODULE_MODULE_CLOCK_CONTROL_1 -} - -_ADDRESS_ALREADY_WRITTEN_TO = None - -XAIE_COL_SHIFT = 25 -XAIE_ROW_SHIFT = 20 - -assert XAIE_COL_SHIFT > XAIE_ROW_SHIFT - - -def is_core_tile(col, row): - return row > 1 - - -def is_mem_tile(col, row): - return row == 1 - - -noc_columns = {0, 1, 2, 3} - - -def is_shim_noc_tile(col, row): - return row == 0 and col in noc_columns - - -def get_tile_addr(c, r): - return (c & 0xFF) << XAIE_COL_SHIFT | (r & 0xFF) << XAIE_ROW_SHIFT - - -def get_addr_tile(addr): - offset = addr & 0xFFFFF - addr >>= XAIE_ROW_SHIFT - r = addr & 0xF - addr >>= XAIE_COL_SHIFT - XAIE_ROW_SHIFT - c = addr & 0xF - return c, r, offset - - -def init_value(Addr): - c, r, offset = get_addr_tile(Addr) - if is_mem_tile(c, r): - return _MEM_TILE_MAP.get(offset, 0) - elif is_core_tile(c, r): - return _CORE_TILE_MAP.get(offset, 0) - elif is_shim_noc_tile(c, r): - return _NOC_TILE_MAP.get(offset, 0) - else: - raise RuntimeError("unknown tile") - - -@ffi.def_extern() -def cdo_Write32(Addr, Data): - _ADDRESS_ALREADY_WRITTEN_TO[Addr] = Data - logger.debug(f"Write32 {Addr=:08x} {Data=:08x}") - - -def Read32(Addr): - if Addr not in _ADDRESS_ALREADY_WRITTEN_TO: - _ADDRESS_ALREADY_WRITTEN_TO[Addr] = init_value(Addr) - Data = _ADDRESS_ALREADY_WRITTEN_TO[Addr] - logger.debug(f"Read32 {Addr=:08x} {Data=:08x}") - return Data +from _ctypes import Structure +from ctypes import c_int, c_bool, c_uint +from . import uint32_t, _lib, String -@ffi.def_extern() -def cdo_BlockWrite32(Addr, pData, size): - logger.debug(f"BlockWrite32 {Addr=:08x} {pData=} {size}") - data = ffi.unpack(pData, size) - for i, v in enumerate(data): - cdo_Write32(Addr + i * 4, v) +enum_byte_ordering = c_int +Little_Endian = 0 -@ffi.def_extern() -def cdo_MaskWrite32(Addr, Mask, Data): - RegVal = Read32(Addr) - logger.debug(f"MaskWrite32 {Addr=:08x} {Mask=:08x} {RegVal=:08x} {Data=:08x}") - RegVal &= ~Mask - cdo_Write32(Addr, RegVal | Data) +Big_Endian = Little_Endian + 1 +enum_CDO_COMMAND = c_int -@ffi.def_extern() -def cdo_MaskPoll(Addr, Mask, Expected_Value, TimeoutInMS): - logger.debug(f"MaskPoll {Addr=:08x} {Mask=:08x} {Expected_Value=} {TimeoutInMS}") - raise NotImplementedError +CDO_CMD_DMA_WRITE = 0x105 +CDO_CMD_MASK_POLL64 = 0x106 -@ffi.def_extern() -def cdo_BlockSet32(Addr, Data, size): - logger.debug(f"BlockSet32 {Addr=:08x} {Data=} {size=}") - raise NotImplementedError +CDO_CMD_MASK_WRITE64 = 0x107 +CDO_CMD_WRITE64 = 0x108 -def get_written_addresses(): - return dict(_ADDRESS_ALREADY_WRITTEN_TO) +CDO_CMD_NO_OPERATION = 0x111 -def reset_written_addresses(): - global _ADDRESS_ALREADY_WRITTEN_TO - _ADDRESS_ALREADY_WRITTEN_TO = OrderedDict() +class struct_cdoHeader(Structure): + pass + + +struct_cdoHeader.__slots__ = [ + "NumWords", + "IdentWord", + "Version", + "CDOLength", + "CheckSum", +] +struct_cdoHeader._fields_ = [ + ("NumWords", uint32_t), + ("IdentWord", uint32_t), + ("Version", uint32_t), + ("CDOLength", uint32_t), + ("CheckSum", uint32_t), +] + +cdoHeader = struct_cdoHeader + + +startCDOFileStream = _lib.get("startCDOFileStream", "cdecl") +startCDOFileStream.argtypes = [String] +startCDOFileStream.restype = None + + +endCurrentCDOFileStream = _lib.get("endCurrentCDOFileStream", "cdecl") +endCurrentCDOFileStream.argtypes = [] +endCurrentCDOFileStream.restype = None + + +FileHeader = _lib.get("FileHeader", "cdecl") +FileHeader.argtypes = [] +FileHeader.restype = None + + +EnAXIdebug = _lib.get("EnAXIdebug", "cdecl") +EnAXIdebug.argtypes = [] +EnAXIdebug.restype = None + + +setEndianness = _lib.get("setEndianness", "cdecl") +setEndianness.argtypes = [c_bool] +setEndianness.restype = None + + +configureHeader = _lib.get("configureHeader", "cdecl") +configureHeader.argtypes = [] +configureHeader.restype = None + + +getPadBytesForDmaWrCmdAlignment = _lib.get("getPadBytesForDmaWrCmdAlignment", "cdecl") +getPadBytesForDmaWrCmdAlignment.argtypes = [uint32_t] +getPadBytesForDmaWrCmdAlignment.restype = c_uint + + +insertNoOpCommand = _lib.get("insertNoOpCommand", "cdecl") +insertNoOpCommand.argtypes = [c_uint] +insertNoOpCommand.restype = None + + +insertDmaWriteCmdHdr = _lib.get("insertDmaWriteCmdHdr", "cdecl") +insertDmaWriteCmdHdr.argtypes = [uint32_t] +insertDmaWriteCmdHdr.restype = None + + +disableDmaCmdAlignment = _lib.get("disableDmaCmdAlignment", "cdecl") +disableDmaCmdAlignment.argtypes = [] +disableDmaCmdAlignment.restype = None + +cdoHeader = struct_cdoHeader diff --git a/xaiepy/xclbinutil.cpp b/xaiepy/xclbinutil.cpp new file mode 100644 index 0000000..142f2f8 --- /dev/null +++ b/xaiepy/xclbinutil.cpp @@ -0,0 +1,25 @@ +// +// Created by mlevental on 5/12/24. +// + +#include + +#include "XclBinUtilities.h" +#include "XclBinClass.h" + +using namespace pybind11::literals; + +PYBIND11_MODULE(_xclbinutil, m) { + m.def("make_xclbin", + [](const std::string &memTopologyJsonFilePath, const std::string &aiePartitionFilePath, + const std::string &kernelJsonFilePath, + const std::string &outputFilePath) { + XclBin xclBin; + ParameterSectionData memTopSec("MEM_TOPOLOGY:JSON:" + memTopologyJsonFilePath); + xclBin.addReplaceSection(memTopSec); + ParameterSectionData aiePartSec("AIE_PARTITION:JSON:" + aiePartitionFilePath); + xclBin.addReplaceSection(aiePartSec); + xclBin.addKernels(kernelJsonFilePath); + xclBin.writeXclBinBinary(outputFilePath, false); + }, "mem_topology_json_fp"_a, "aie_partition_json_fp"_a, "kernel_json_fp"_a, "output_fp"_a); +} \ No newline at end of file diff --git a/xaiepy/xclbinutil.py b/xaiepy/xclbinutil.py new file mode 100644 index 0000000..ea8cdfc --- /dev/null +++ b/xaiepy/xclbinutil.py @@ -0,0 +1,116 @@ +import random + +from ._xclbinutil import * + +mem_topology = { + "mem_topology": { + "m_count": "2", + "m_mem_data": [ + { + "m_type": "MEM_DRAM", + "m_used": "1", + "m_sizeKB": "0x10000", + "m_tag": "HOST", + "m_base_address": "0x4000000", + }, + { + "m_type": "MEM_DRAM", + "m_used": "1", + "m_sizeKB": "0xc000", + "m_tag": "SRAM", + "m_base_address": "0x4000000", + }, + ], + } +} + + +def emit_partition(pdi_fp, kernel_id="0x901", start_columns=None, num_cols=None): + assert num_cols is not None or start_columns is not None + if start_columns is None: + start_columns = list(range(1, 6 - num_cols)) + + uuid = random.randint(2222, 9999) + return { + "aie_partition": { + "name": "QoS", + "operations_per_cycle": "2048", + "inference_fingerprint": "23423", + "pre_post_fingerprint": "12345", + "partition": { + "column_width": num_cols, + "start_columns": start_columns, + }, + "PDIs": [ + { + "uuid": "00000000-0000-0000-0000-00000000" + str(uuid), + "file_name": str(pdi_fp), + "cdo_groups": [ + { + "name": "DPU", + "type": "PRIMARY", + "pdi_id": "0x01", + "dpu_kernel_ids": [kernel_id], + "pre_cdo_groups": ["0xC1"], + } + ], + } + ], + } + } + + +def emit_design_kernel_json( + kernel_name="MLIR_AIE", + kernel_id="0x901", + instance_name="MLIRAIE", + buffer_args=None, +): + if buffer_args is None: + buffer_args = ["in", "tmp", "out"] + + arguments = [ + { + "name": "instr", + "memory-connection": "SRAM", + "address-qualifier": "GLOBAL", + "type": "char *", + "offset": "0x00", + }, + { + "name": "ninstr", + "address-qualifier": "SCALAR", + "type": "uint64_t", + "offset": "0x08", + }, + ] + + offset = 0x10 + for buf in buffer_args: + arg = { + "name": buf, + "memory-connection": "HOST", + "address-qualifier": "GLOBAL", + "type": "char *", + "offset": str(hex(offset)), + } + arguments.append(arg) + offset += 0x8 + + return { + "ps-kernels": { + "kernels": [ + { + "name": kernel_name, + "type": "dpu", + "extended-data": { + "subtype": "DPU", + "functional": "1", + "dpu_kernel_id": kernel_id, + }, + "arguments": arguments, + "instances": [{"name": instance_name}], + } + ] + } + }