diff --git a/CMakeLists.txt b/CMakeLists.txt index f8fe867fb8..a34af1e3a2 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -286,6 +286,7 @@ endif() # Last because each of these has its own CMakeLists.txt which reloads/re-finds LLVM, thus resettings globals. add_subdirectory(programming_examples) +add_subdirectory(programming_guide) add_subdirectory(test) add_subdirectory(mlir_tutorials) add_subdirectory(cmake/modules) diff --git a/programming_guide/CMakeLists.txt b/programming_guide/CMakeLists.txt new file mode 100755 index 0000000000..d51f8c6f68 --- /dev/null +++ b/programming_guide/CMakeLists.txt @@ -0,0 +1,145 @@ +# +# This file is licensed under the Apache License v2.0 with LLVM Exceptions. +# See https://llvm.org/LICENSE.txt for license information. +# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +# +# Copyright (C) 2022, Advanced Micro Devices, Inc. + +cmake_minimum_required(VERSION 3.10) + +if(POLICY CMP0074) + cmake_policy(SET CMP0074 NEW) +endif() + +if(POLICY CMP0068) + cmake_policy(SET CMP0068 NEW) + set(CMAKE_BUILD_WITH_INSTALL_NAME_DIR ON) +endif() + +if(POLICY CMP0075) + cmake_policy(SET CMP0075 NEW) +endif() + +if(POLICY CMP0077) + cmake_policy(SET CMP0077 NEW) +endif() + +if(POLICY CMP0144) + cmake_policy(SET CMP0144 NEW) +endif() + +project(ref-designs LANGUAGES CXX C) + +# find package AIE if running tests from AIE installation +if(NOT AIE_BINARY_DIR) + find_package(AIE REQUIRED CONFIG) + set(LibXAIE_${AIE_RUNTIME_TEST_TARGET}_DIR CACHE STRING "") #pick up libxaiengine from installation folder if no other location specified +endif() + +# default to x86_64 system architecture for runtime target +set(AIE_RUNTIME_TARGETS "x86_64" CACHE STRING "Architectures to compile the runtime libraries for.") +list(GET AIE_RUNTIME_TARGETS 0 firstRuntimeTarget) +set(AIE_RUNTIME_TEST_TARGET ${firstRuntimeTarget} CACHE STRING "Runtime architecture to test with.") + +set(CMAKE_CXX_STANDARD 14) +set(CMAKE_CXX_STANDARD_REQUIRED YES) + +option(LLVM_INCLUDE_TOOLS "Generate build targets for the LLVM tools." ON) +option(LLVM_BUILD_TOOLS "Build the LLVM tools. If OFF, just generate build targets." ON) + +if(Vitis_FOUND) + set(DEFAULT_ENABLE_CHESS_TESTS ON) +else() + set(DEFAULT_ENABLE_CHESS_TESTS OFF) +endif() +option(ENABLE_CHESS_TESTS "Enable backend tests using xchesscc" ${DEFAULT_ENABLE_CHESS_TESTS}) + +if(${CMAKE_HOST_SYSTEM_PROCESSOR} STREQUAL aarch64) + set(DEFAULT_ENABLE_BOARD_TESTS ON) +endif() +option(ENABLE_BOARD_TESTS "Enable board tests" ${DEFAULT_ENABLE_BOARD_TESTS}) + +find_package(MLIR REQUIRED CONFIG) + +message(STATUS "Using MLIRConfig.cmake in: ${MLIR_DIR}") +message(STATUS "Using LLVMConfig.cmake in: ${LLVM_DIR}") + +set(LLVM_RUNTIME_OUTPUT_INTDIR ${CMAKE_BINARY_DIR}/bin) +set(LLVM_LIBRARY_OUTPUT_INTDIR ${CMAKE_BINARY_DIR}/lib) +set(MLIR_BINARY_DIR ${CMAKE_BINARY_DIR}) + +find_package(Vitis 2023.2 COMPONENTS AIE AIE2) +find_package(Python3 COMPONENTS Interpreter) +find_package(XRT) +find_package(OpenCV) +find_package(hsa-runtime64) + +# Look for LibXAIE +if (DEFINED LibXAIE_${AIE_RUNTIME_TEST_TARGET}_DIR) + message("Ref designs using xaiengine from LibXAIE_${AIE_RUNTIME_TEST_TARGET}_DIR=${LibXAIE_${AIE_RUNTIME_TEST_TARGET}_DIR}") + set(LibXAIE_ROOT ${LibXAIE_${target}_DIR}) + find_package(LibXAIE) +else() + if(DEFINED VITIS_ROOT) + message(STATUS "Ref designs have Vitis available, no libxaie location specified so pick up from build area") + set(LibXAIE_FOUND TRUE) + endif() +endif() + +# Define the default arguments to use with 'lit', and an option for the user to +# override. +set(LIT_ARGS_DEFAULT "-sv") +if (MSVC_IDE OR XCODE) + set(LIT_ARGS_DEFAULT "${LIT_ARGS_DEFAULT} --no-progress-bar") +endif() +set(LLVM_LIT_ARGS "${LIT_ARGS_DEFAULT}" CACHE STRING "Default options for lit") + +list(APPEND CMAKE_MODULE_PATH "${MLIR_CMAKE_DIR}") +list(APPEND CMAKE_MODULE_PATH "${LLVM_CMAKE_DIR}") + +include(TableGen) +include(AddLLVM) +include(AddMLIR) +include(HandleLLVMOptions) + +include_directories(${LLVM_INCLUDE_DIRS}) +include_directories(${MLIR_INCLUDE_DIRS}) +include_directories(${PROJECT_SOURCE_DIR}/include) +include_directories(${PROJECT_BINARY_DIR}/include) +add_definitions(${LLVM_DEFINITIONS}) + +if(ENABLE_CHESS_TESTS) +set(CONFIG_ENABLE_CHESS_TESTS 1) +else() +set(CONFIG_ENABLE_CHESS_TESTS 0) +endif() +if(ENABLE_BOARD_TESTS) +set(CONFIG_ENABLE_BOARD_TESTS 1) +else() +set(CONFIG_ENABLE_BOARD_TESTS 0) +endif() +if(LibXAIE_FOUND) +set(CONFIG_HAS_LIBXAIE 1) +else() +set(CONFIG_HAS_LIBXAIE 0) +endif() +configure_lit_site_cfg( + ${CMAKE_CURRENT_SOURCE_DIR}/lit.site.cfg.py.in + ${CMAKE_CURRENT_BINARY_DIR}/lit.site.cfg.py + MAIN_CONFIG + ${CMAKE_CURRENT_SOURCE_DIR}/lit.cfg.py +) + +set(TEST_DEPENDS + FileCheck count not + AIEPythonModules + aie-opt + aie-translate +) + +add_lit_testsuite(check-programming-guide "Running the programming guide code samples" + ${CMAKE_CURRENT_BINARY_DIR} + DEPENDS ${TEST_DEPENDS} + ARGS "-sv --timeout 600" +) +set_target_properties(check-reference-designs PROPERTIES FOLDER "Programming Guide") diff --git a/programming_guide/lit.cfg.py b/programming_guide/lit.cfg.py new file mode 100755 index 0000000000..d0e5d11ffc --- /dev/null +++ b/programming_guide/lit.cfg.py @@ -0,0 +1,303 @@ +# ./lit.cfg.py -*- Python -*- +# +# This file is licensed under the Apache License v2.0 with LLVM Exceptions. +# See https://llvm.org/LICENSE.txt for license information. +# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +# +# Copyright (C) 2022, Advanced Micro Devices, Inc. + +import os +import re +import shutil +import subprocess + +import lit.formats +import lit.util + +from lit.llvm import llvm_config + +# Configuration file for the 'lit' test runner. + +# name: The name of this test suite. +config.name = "AIE_PROGRAMMING_GUIDE" + +config.test_format = lit.formats.ShTest(not llvm_config.use_lit_shell) + +# suffixes: A list of file extensions to treat as test files. +config.suffixes = [".mlir"] + +# test_source_root: The root path where tests are located. +config.test_source_root = os.path.dirname(__file__) + +config.substitutions.append(("%PATH%", config.environment["PATH"])) +config.substitutions.append(("%shlibext", config.llvm_shlib_ext)) +config.substitutions.append(("%extraAieCcFlags%", config.extraAieCcFlags)) +config.substitutions.append( + ( + "%host_runtime_lib%", + os.path.join(config.aie_obj_root, "runtime_lib", config.aieHostTarget), + ) +) +config.substitutions.append(("%aietools", config.vitis_aietools_dir)) +# for xchesscc_wrapper +llvm_config.with_environment("AIETOOLS", config.vitis_aietools_dir) + +# for python +llvm_config.with_environment("PYTHONPATH", os.path.join(config.aie_obj_root, "python")) + +run_on_npu = "echo" +xrt_flags = "" + +# Not using run_on_board anymore, need more specific per-platform commands +config.substitutions.append(("%run_on_board", "echo")) + +if config.hsa_dir and (not ("NOTFOUND" in config.hsa_dir)): + if not "hsa" in config.aieHostTarget: + print( + "ROCm found, but disabled because host target {}".format( + config.aieHostTarget + ) + ) + config.substitutions.append(("%run_on_vck5000", "echo")) + config.substitutions.append(("%link_against_hsa%", "")) + config.substitutions.append(("%HSA_DIR%", "")) + + else: + # Getting the path to the ROCm directory. hsa-runtime64 points to the cmake + # directory so need to go up three directories + rocm_root = os.path.join(config.hsa_dir, "..", "..", "..") + print("Found ROCm:", rocm_root) + config.available_features.add("hsa") + config.substitutions.append(("%HSA_DIR%", "{}".format(rocm_root))) + config.substitutions.append(("%link_against_hsa%", "--link_against_hsa")) + found_vck5000 = False + + if config.enable_board_tests: + # If board tests are enabled, make sure there is an AIE ROCm device that we can find + try: + # Making sure that we use the experimental ROCm install that can see the AIE device + my_env = os.environ.copy() + my_env.update(LD_LIBRARY_PATH="{}/lib/".format(rocm_root)) + result = subprocess.run( + ["rocminfo"], + stdout=subprocess.PIPE, + stderr=subprocess.PIPE, + env=my_env, + ) + result = result.stdout.decode("utf-8").split("\n") + + # Go through result and look for the VCK5000 + for l in result: + if "Versal VCK5000" in l: + print("Found VCK500 in rocminfo. Enabling on board tests") + found_vck5000 = True + config.substitutions.append( + ("%run_on_vck5000", "flock /tmp/vck5000.lock") + ) + break + + if not found_vck5000: + config.substitutions.append(("%run_on_vck5000", "echo")) + print( + "Enable board set and HSA found but couldn't find device using rocminfo" + ) + + except: + print("Enable board set and HSA found but unable to run rocminfo") + pass + else: + print("Skipping execution of unit tests (ENABLE_BOARD_TESTS=OFF)") + config.substitutions.append(("%run_on_vck5000", "echo")) +else: + print("ROCm not found") + config.substitutions.append(("%run_on_vck5000", "echo")) + config.substitutions.append(("%link_against_hsa%", "")) + config.substitutions.append(("%HSA_DIR%", "")) + + +if config.xrt_lib_dir: + print("xrt found at", os.path.dirname(config.xrt_lib_dir)) + xrt_flags = "-I{} -L{} -luuid -lxrt_coreutil".format( + config.xrt_include_dir, config.xrt_lib_dir + ) + try: + xbutil = os.path.join(config.xrt_bin_dir, "xbutil") + result = subprocess.run( + [xbutil, "examine"], stdout=subprocess.PIPE, stderr=subprocess.PIPE + ) + result = result.stdout.decode("utf-8").split("\n") + # Starting with Linux 6.8 the format is like "[0000:66:00.1] : RyzenAI-npu1" + p = re.compile("\[.+:.+:.+\].+(Phoenix|RyzenAI-(npu\d))") + for l in result: + m = p.match(l) + if m: + print("Found Ryzen AI device:", m.group().split()[0]) + if len(m.groups()) == 2: + # Prepare the future + aie_model = m.group(2) + print("\tmodel:", aie_model) + config.available_features.add("ryzen_ai") + run_on_npu = ( + f"flock /tmp/npu.lock {config.aie_src_root}/utils/run_on_npu.sh" + ) + except: + print("Failed to run xbutil") + pass +else: + print("xrt not found") + +config.substitutions.append(("%run_on_npu", run_on_npu)) +config.substitutions.append(("%xrt_flags", xrt_flags)) +config.substitutions.append(("%XRT_DIR", config.xrt_dir)) +config.environment["XRT_HACK_UNSECURE_LOADING_XCLBIN"] = "1" + +opencv_flags = "" +if config.opencv_include_dir and config.opencv_libs: + print("opencv found") + config.available_features.add("opencv") + opencv_flags = opencv_flags + " -I" + config.opencv_include_dir + if config.opencv_lib_dir: + opencv_flags = opencv_flags + " -L" + config.opencv_lib_dir + libs = config.opencv_libs.split(";") + opencv_flags = opencv_flags + " " + " ".join(["-l" + l for l in libs]) +else: + print("opencv not found") + opencv_flags = "" +config.substitutions.append(("%opencv_flags", opencv_flags)) + +try: + import torch + + config.available_features.add("torch") +except ImportError: + print("torch not found", file=sys.stderr) + pass + +VitisSysrootFlag = "" +if "x86_64" in config.aieHostTarget: + config.substitutions.append(("%aieHostTargetTriplet%", "x86_64-unknown-linux-gnu")) +elif config.aieHostTarget == "aarch64": + config.substitutions.append(("%aieHostTargetTriplet%", "aarch64-linux-gnu")) + VitisSysrootFlag = "--sysroot=" + config.vitis_sysroot + +config.substitutions.append(("%VitisSysrootFlag%", VitisSysrootFlag)) +config.substitutions.append(("%aieHostTargetArch%", config.aieHostTarget)) + +llvm_config.with_system_environment(["HOME", "INCLUDE", "LIB", "TMP", "TEMP"]) + +llvm_config.use_default_substitutions() + +# excludes: A list of directories to exclude from the testsuite. The 'Inputs' +# subdirectories contain auxiliary inputs for various tests in their parent +# directories. +config.excludes = [ + "Inputs", + "Examples", + "CMakeLists.txt", + "README.txt", + "LICENSE.txt", + "aie.mlir.prj", + "lit.cfg.py", +] + +config.aie_tools_dir = os.path.join(config.aie_obj_root, "bin") + + +def prepend_path(path): + global llvm_config + paths = [path] + + current_paths = llvm_config.config.environment.get("PATH", None) + if current_paths: + paths.extend(current_paths.split(os.path.pathsep)) + paths = [os.path.normcase(os.path.normpath(p)) for p in paths] + else: + paths = [] + + llvm_config.config.environment["PATH"] = os.pathsep.join(paths) + + +# Setup the path. +prepend_path(config.llvm_tools_dir) +prepend_path(config.peano_tools_dir) +prepend_path(config.aie_tools_dir) +# llvm_config.with_environment('LM_LICENSE_FILE', os.getenv('LM_LICENSE_FILE')) +# llvm_config.with_environment('XILINXD_LICENSE_FILE', os.getenv('XILINXD_LICENSE_FILE')) +if config.vitis_root: + config.vitis_aietools_bin = os.path.join(config.vitis_aietools_dir, "bin") + prepend_path(config.vitis_aietools_bin) + llvm_config.with_environment("VITIS", config.vitis_root) + +# Test to see if we have the peano backend. +try: + result = subprocess.run( + [os.path.join(config.peano_tools_dir, "llc"), "-mtriple=aie", "--version"], + stdout=subprocess.PIPE, + stderr=subprocess.PIPE, + ) + if re.search("Xilinx AI Engine", result.stdout.decode("utf-8")) is not None: + config.available_features.add("peano") + print("Peano found: " + shutil.which("llc")) + else: + print("Peano not found, but expected at ", config.peano_tools_dir) +except Exception as e: + print("Peano not found, but expected at ", config.peano_tools_dir) + +print("Looking for Chess...") +# test if LM_LICENSE_FILE valid +if config.enable_chess_tests: + result = None + if config.vitis_root: + result = shutil.which("xchesscc") + + if result != None: + print("Chess found: " + result) + config.available_features.add("chess") + config.available_features.add("valid_xchess_license") + lm_license_file = os.getenv("LM_LICENSE_FILE") + if lm_license_file != None: + llvm_config.with_environment("LM_LICENSE_FILE", lm_license_file) + xilinxd_license_file = os.getenv("XILINXD_LICENSE_FILE") + if xilinxd_license_file != None: + llvm_config.with_environment("XILINXD_LICENSE_FILE", xilinxd_license_file) + + validate_chess = False + if validate_chess: + import subprocess + + result = subprocess.run( + ["xchesscc", "+v"], stdout=subprocess.PIPE, stderr=subprocess.PIPE + ) + validLMLicense = len(result.stderr.decode("utf-8")) == 0 + else: + validLMLicense = lm_license_file or xilinxd_license_file + + if not lm_license_file and not xilinxd_license_file: + print( + "WARNING: no valid xchess license that is required by some of the lit tests" + ) + elif os.getenv("XILINXD_LICENSE_FILE") is not None: + print("Chess license found") + llvm_config.with_environment( + "XILINXD_LICENSE_FILE", os.getenv("XILINXD_LICENSE_FILE") + ) + else: + print("Chess not found") + +tool_dirs = [config.aie_tools_dir, config.peano_tools_dir, config.llvm_tools_dir] +tools = [ + "aie-opt", + "aie-translate", + "aiecc.py", + "ld.lld", + "llc", + "llvm-objdump", + "opt", + "xchesscc_wrapper", +] + +llvm_config.add_tool_substitutions(tools, tool_dirs) + +if config.enable_board_tests: + lit_config.parallelism_groups["board"] = 1 + config.parallelism_group = "board" diff --git a/programming_guide/lit.local.cfg b/programming_guide/lit.local.cfg new file mode 100755 index 0000000000..bd10fd2b0a --- /dev/null +++ b/programming_guide/lit.local.cfg @@ -0,0 +1,13 @@ +# +# This file is licensed under the Apache License v2.0 with LLVM Exceptions. +# See https://llvm.org/LICENSE.txt for license information. +# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +# +# Copyright (C) 2022, Advanced Micro Devices, Inc. + +config.unsupported = [] + +config.suffixes = ['.lit'] + +if not config.has_libxaie: + config.unsupported = ["programming_guide"] diff --git a/programming_guide/lit.site.cfg.py.in b/programming_guide/lit.site.cfg.py.in new file mode 100755 index 0000000000..ad32695fcf --- /dev/null +++ b/programming_guide/lit.site.cfg.py.in @@ -0,0 +1,84 @@ +# ./lit.site.cfg.py.in -*- Python -*- +# +# This file is licensed under the Apache License v2.0 with LLVM Exceptions. +# See https://llvm.org/LICENSE.txt for license information. +# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +# +# Copyright (C) 2022, Advanced Micro Devices, Inc. + +@LIT_SITE_CFG_IN_HEADER@ + +import sys +import lit.util + +config.host_triple = "@LLVM_HOST_TRIPLE@" +config.target_triple = "@TARGET_TRIPLE@" +config.llvm_src_root = "@LLVM_SOURCE_DIR@" +config.llvm_obj_root = "@LLVM_BINARY_DIR@" +config.llvm_tools_dir = "@LLVM_TOOLS_DIR@" +config.llvm_lib_dir = "@LLVM_LIBRARY_DIR@" +config.llvm_shlib_dir = "@SHLIBDIR@" +config.llvm_shlib_ext = "@SHLIBEXT@" +config.llvm_exe_ext = "@EXEEXT@" +config.peano_tools_dir = "@PEANO_INSTALL_DIR@/bin" +config.lit_tools_dir = "@LLVM_LIT_TOOLS_DIR@" +config.python_executable = "@Python3_EXECUTABLE@" +config.gold_executable = "@GOLD_EXECUTABLE@" +config.ld64_executable = "@LD64_EXECUTABLE@" +config.enable_shared = @ENABLE_SHARED@ +config.enable_assertions = @ENABLE_ASSERTIONS@ +config.targets_to_build = "@TARGETS_TO_BUILD@" +config.native_target = "@LLVM_NATIVE_ARCH@" +config.llvm_bindings = "@LLVM_BINDINGS@".split(' ') +config.host_os = "@HOST_OS@" +config.host_cc = "@HOST_CC@" +config.host_cxx = "@HOST_CXX@" +# Note: ldflags can contain double-quoted paths, so must use single quotes here. +config.host_ldflags = '@HOST_LDFLAGS@' +config.llvm_use_sanitizer = "@LLVM_USE_SANITIZER@" +config.llvm_host_triple = '@LLVM_HOST_TRIPLE@' +config.host_arch = "@HOST_ARCH@" +config.xrt_dir = "@XRT_DIR@" +config.xrt_bin_dir = "@XRT_BIN_DIR@" +config.xrt_lib_dir = "@XRT_LIB_DIR@" +config.xrt_include_dir = "@XRT_INCLUDE_DIR@" +config.opencv_libs = "@OpenCV_LIBS@" +config.opencv_lib_dir = "@OpenCV_LIB_PATH@" +config.opencv_include_dir = "@OpenCV_INCLUDE_DIRS@" + +config.aie_src_root = "@AIE_SOURCE_DIR@" +config.aie_obj_root = "@AIE_BINARY_DIR@" +# test_exec_root: The root path where tests should be run. +config.test_exec_root = "@CMAKE_CURRENT_BINARY_DIR@" + +config.hsa_dir = "@hsa-runtime64_DIR@" +config.hsa_found = lit.util.pythonize_bool("@hsa-runtime64_FOUND@") + +# pass on vitis settings +config.enable_chess_tests = @CONFIG_ENABLE_CHESS_TESTS@ +config.enable_board_tests = @CONFIG_ENABLE_BOARD_TESTS@ +config.vitis_root = "@VITIS_ROOT@" +config.vitis_aietools_dir = "@VITIS_AIETOOLS_DIR@" +config.vitis_sysroot = "@Sysroot@" +config.has_libxaie = @CONFIG_HAS_LIBXAIE@ +config.extraAieCcFlags = "@extraAieCcFlags@" +config.aieHostTarget = "@AIE_RUNTIME_TEST_TARGET@" +config.aieInstallPrefix = "@CMAKE_INSTALL_PREFIX@" + +# Support substitution of the tools_dir with user parameters. This is +# used when we can't determine the tool dir at configuration time. +try: + config.llvm_tools_dir = config.llvm_tools_dir % lit_config.params + config.llvm_shlib_dir = config.llvm_shlib_dir % lit_config.params +except KeyError: + e = sys.exc_info()[1] + key, = e.args + lit_config.fatal("unable to find %r parameter, use '--param=%s=VALUE'" % (key,key)) + +config.aie_include_integration_tests = "@AIE_INCLUDE_INTEGRATION_TESTS@" + +import lit.llvm +lit.llvm.initialize(lit_config, config) + +# Let the main config do the real work. +lit_config.load_config(config, "@PROJECT_SOURCE_DIR@/lit.cfg.py") diff --git a/programming_guide/section-1/Makefile b/programming_guide/section-1/Makefile index 1a3d65de9a..e021316310 100644 --- a/programming_guide/section-1/Makefile +++ b/programming_guide/section-1/Makefile @@ -6,9 +6,11 @@ # ##===----------------------------------------------------------------------===## -include ../../programming_examples/makefile-common +srcdir := $(shell dirname $(realpath $(firstword $(MAKEFILE_LIST)))) -build/aie.mlir: aie2.py +include ${srcdir}/../../programming_examples/makefile-common + +build/aie.mlir: ${srcdir}/aie2.py mkdir -p ${@D} python3 $< > $@ diff --git a/programming_guide/section-1/run_makefile.lit b/programming_guide/section-1/run_makefile.lit new file mode 100644 index 0000000000..fd436178e2 --- /dev/null +++ b/programming_guide/section-1/run_makefile.lit @@ -0,0 +1,7 @@ +// (c) Copyright 2024 Advanced Micro Devices, Inc. + // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception + // + // REQUIRES: ryzen_ai, chess + // + // RUN: make -f %S/Makefile clean + // RUN: make -f %S/Makefile \ No newline at end of file diff --git a/programming_guide/section-3/Makefile b/programming_guide/section-3/Makefile index 77688005e3..f2536fc7c6 100644 --- a/programming_guide/section-3/Makefile +++ b/programming_guide/section-3/Makefile @@ -6,29 +6,31 @@ # ##===----------------------------------------------------------------------===## -include ../../programming_examples/makefile-common +srcdir := $(shell dirname $(realpath $(firstword $(MAKEFILE_LIST)))) + +include ${srcdir}/../../programming_examples/makefile-common all: build/final.xclbin build/insts.txt targetname = vectorScalar -build/aie.mlir: aie2.py +build/aie.mlir: ${srcdir}/aie2.py mkdir -p ${@D} python3 $< > $@ -build/scale.o: vector_scalar_mul.cc +build/scale.o: ${srcdir}/vector_scalar_mul.cc mkdir -p ${@D} - cd ${@D} && xchesscc_wrapper ${CHESSCCWRAP2_FLAGS} -c $(<:%=../%) -o ${@F} + cd ${@D} && xchesscc_wrapper ${CHESSCCWRAP2_FLAGS} -c $< -o ${@F} build/final.xclbin: build/aie.mlir build/scale.o mkdir -p ${@D} cd ${@D} && aiecc.py --aie-generate-cdo --no-compile-host --xclbin-name=${@F} \ --aie-generate-npu --npu-insts-name=insts.txt $(<:%=../%) -${targetname}.exe: test.cpp +${targetname}.exe: ${srcdir}/test.cpp rm -rf _build mkdir -p _build - cd _build && ${powershell} cmake .. -DTARGET_NAME=${targetname} + cd _build && ${powershell} cmake ${srcdir} -DTARGET_NAME=${targetname} cd _build && ${powershell} cmake --build . --config Release ifeq "${powershell}" "powershell.exe" cp _build/${targetname}.exe $@ @@ -40,7 +42,7 @@ run: ${targetname}.exe build/final.xclbin build/insts.txt ${powershell} ./$< -x build/final.xclbin -i build/insts.txt -k MLIR_AIE run_py: build/final.xclbin build/insts.txt - ${powershell} python3 test.py -x build/final.xclbin -i build/insts.txt -k MLIR_AIE + ${powershell} python3 ${srcdir}/test.py -x build/final.xclbin -i build/insts.txt -k MLIR_AIE clean: rm -rf build _build ${targetname}.exe diff --git a/programming_guide/section-3/run_makefile.lit b/programming_guide/section-3/run_makefile.lit new file mode 100644 index 0000000000..e8cc49032b --- /dev/null +++ b/programming_guide/section-3/run_makefile.lit @@ -0,0 +1,10 @@ +// (c) Copyright 2024 Advanced Micro Devices, Inc. + // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception + // + // REQUIRES: ryzen_ai, chess + // + // RUN: make -f %S/Makefile clean + // RUN: make -f %S/Makefile + // RUN: %run_on_npu make -f %S/Makefile run | FileCheck %s + // RUN: %run_on_npu make -f %S/Makefile run_py | FileCheck %s + // CHECK: PASS! \ No newline at end of file diff --git a/programming_guide/section-4/section-4a/Makefile b/programming_guide/section-4/section-4a/Makefile index 3b0140656f..370ac81cfc 100644 --- a/programming_guide/section-4/section-4a/Makefile +++ b/programming_guide/section-4/section-4a/Makefile @@ -6,30 +6,32 @@ # ##===----------------------------------------------------------------------===## -include ../../../programming_examples/makefile-common +srcdir := $(shell dirname $(realpath $(firstword $(MAKEFILE_LIST)))) + +include ${srcdir}/../../../programming_examples/makefile-common all: build/final.xclbin targetname = myFirstProgram -build/aie.mlir: aie2.py +build/aie.mlir: ${srcdir}/aie2.py mkdir -p ${@D} python3 $< > $@ -build/scale.o: vector_scalar_mul.cc +build/scale.o: ${srcdir}/vector_scalar_mul.cc mkdir -p ${@D} - cd ${@D} && xchesscc_wrapper ${CHESSCCWRAP2_FLAGS} -c $(<:%=../%) -o ${@F} + cd ${@D} && xchesscc_wrapper ${CHESSCCWRAP2_FLAGS} -c $< -o ${@F} build/final.xclbin: build/aie.mlir build/scale.o mkdir -p ${@D} cd ${@D} && aiecc.py --aie-generate-cdo --no-compile-host --xclbin-name=${@F} \ --aie-generate-npu --npu-insts-name=insts.txt $(<:%=../%) -${targetname}.exe: test.cpp +${targetname}.exe: ${srcdir}/test.cpp rm -rf _build mkdir -p _build - cd _build && ${powershell} cmake -E env CXXFLAGS="-std=c++23 -ggdb" cmake .. -D CMAKE_C_COMPILER=gcc-13 -D CMAKE_CXX_COMPILER=g++-13 -DTARGET_NAME=${targetname} - cd _build && ${powershell} cmake --build . --config Release + cd _build && ${powershell} cmake -E env CXXFLAGS="-std=c++23 -ggdb" cmake ${srcdir} -D CMAKE_C_COMPILER=gcc-13 -D CMAKE_CXX_COMPILER=g++-13 -DTARGET_NAME=${targetname} + cd _build && ${powershell} cmake --build , --config Release ifeq "${powershell}" "powershell.exe" cp _build/${targetname}.exe $@ else diff --git a/programming_guide/section-4/section-4a/run_makefile.lit b/programming_guide/section-4/section-4a/run_makefile.lit new file mode 100644 index 0000000000..e8cc49032b --- /dev/null +++ b/programming_guide/section-4/section-4a/run_makefile.lit @@ -0,0 +1,10 @@ +// (c) Copyright 2024 Advanced Micro Devices, Inc. + // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception + // + // REQUIRES: ryzen_ai, chess + // + // RUN: make -f %S/Makefile clean + // RUN: make -f %S/Makefile + // RUN: %run_on_npu make -f %S/Makefile run | FileCheck %s + // RUN: %run_on_npu make -f %S/Makefile run_py | FileCheck %s + // CHECK: PASS! \ No newline at end of file diff --git a/programming_guide/section-4/section-4b/Makefile b/programming_guide/section-4/section-4b/Makefile index 8b7b1cc434..cc9e5cf231 100644 --- a/programming_guide/section-4/section-4b/Makefile +++ b/programming_guide/section-4/section-4b/Makefile @@ -6,7 +6,9 @@ # ##===----------------------------------------------------------------------===## -include ../../../programming_examples/makefile-common +srcdir := $(shell dirname $(realpath $(firstword $(MAKEFILE_LIST)))) + +include ${srcdir}/../../../programming_examples/makefile-common all: build/final.xclbin @@ -14,23 +16,23 @@ targetname = myFirstProgram trace_size = 8192 -build/aie.mlir: aie2.py +build/aie.mlir: ${srcdir}/aie2.py mkdir -p ${@D} python3 $< > $@ -build/scale.o: vector_scalar_mul.cc +build/scale.o: ${srcdir}/vector_scalar_mul.cc mkdir -p ${@D} - cd ${@D} && xchesscc_wrapper ${CHESSCCWRAP2_FLAGS} -c $(<:%=../%) -o ${@F} + cd ${@D} && xchesscc_wrapper ${CHESSCCWRAP2_FLAGS} -c $< -o ${@F} build/final.xclbin: build/aie.mlir build/scale.o mkdir -p ${@D} cd ${@D} && aiecc.py --aie-generate-cdo --no-compile-host --xclbin-name=${@F} \ --aie-generate-npu --npu-insts-name=insts.txt $(<:%=../%) -${targetname}.exe: test.cpp +${targetname}.exe: ${srcdir}/test.cpp rm -rf _build mkdir -p _build - cd _build && ${powershell} cmake -E env CXXFLAGS="-std=c++23 -ggdb" cmake .. -D CMAKE_C_COMPILER=gcc-13 -D CMAKE_CXX_COMPILER=g++-13 -DTARGET_NAME=${targetname} + cd _build && ${powershell} cmake -E env CXXFLAGS="-std=c++23 -ggdb" cmake ${srcdir} -D CMAKE_C_COMPILER=gcc-13 -D CMAKE_CXX_COMPILER=g++-13 -DTARGET_NAME=${targetname} cd _build && ${powershell} cmake --build . --config Release ifeq "${powershell}" "powershell.exe" cp _build/${targetname}.exe $@ @@ -49,7 +51,7 @@ trace: ${targetname}.exe build/final.xclbin build/insts.txt ../../../programming_examples/utils/parse_trace.py --filename trace.txt --mlir build/aie.mlir --colshift 1 > trace_4b.json trace_py: build/final.xclbin build/insts.txt - ${powershell} python3 test.py -x build/final.xclbin -i build/insts.txt -k MLIR_AIE -t ${trace_size} + ${powershell} python3 ${srcdir}/test.py -x build/final.xclbin -i build/insts.txt -k MLIR_AIE -t ${trace_size} ../../../programming_examples/utils/parse_trace.py --filename trace.txt --mlir build/aie.mlir --colshift 1 > trace_4b.json clean_trace: diff --git a/programming_guide/section-4/section-4b/run_makefile.lit b/programming_guide/section-4/section-4b/run_makefile.lit new file mode 100644 index 0000000000..e8cc49032b --- /dev/null +++ b/programming_guide/section-4/section-4b/run_makefile.lit @@ -0,0 +1,10 @@ +// (c) Copyright 2024 Advanced Micro Devices, Inc. + // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception + // + // REQUIRES: ryzen_ai, chess + // + // RUN: make -f %S/Makefile clean + // RUN: make -f %S/Makefile + // RUN: %run_on_npu make -f %S/Makefile run | FileCheck %s + // RUN: %run_on_npu make -f %S/Makefile run_py | FileCheck %s + // CHECK: PASS! \ No newline at end of file