Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions profile/device/aie_trace/client/aie_trace_offload_client.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@


extern "C" {
#include <xaiengine.h>
#include <xaiengine/xaiegbl_params.h>
#include <aie_codegen.h>
#include <aie_codegen_inc/xaiegbl_params.h>
}

namespace xdp {
Expand Down
6 changes: 6 additions & 0 deletions profile/device/aie_trace/ve2/aie_trace_offload_ve2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@

#define XDP_PLUGIN_SOURCE

#ifdef XDP_USE_AIE_CODEGEN
extern "C" {
#include <aie_codegen.h>
}
#endif

#include <iostream>

#include "core/include/xrt.h"
Expand Down
5 changes: 5 additions & 0 deletions profile/device/aie_trace/ve2/aie_trace_offload_ve2.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,13 @@

extern "C"
{
#ifdef XDP_USE_AIE_CODEGEN
#include <aie_codegen.h>
#include <aie_codegen_inc/xaiegbl.h>
#else
#include "xaiengine/xaiegbl.h"
#include <xaiengine.h>
#endif
}

namespace xdp {
Expand Down
5 changes: 5 additions & 0 deletions profile/device/common/aie_driver_common_util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,13 @@
#include <set>

extern "C" {
#ifdef XDP_USE_AIE_CODEGEN
#include <aie_codegen.h>
#include <aie_codegen_inc/xaiegbl_params.h>
#else
#include <xaiengine.h>
#include <xaiengine/xaiegbl_params.h>
#endif
}

#include "core/common/message.h"
Expand Down
5 changes: 5 additions & 0 deletions profile/device/common/client_transaction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,13 @@
#include "transactions/op_buf.hpp"

extern "C" {
#ifdef XDP_USE_AIE_CODEGEN
#include <aie_codegen.h>
#include <aie_codegen_inc/xaiegbl_params.h>
#else
#include <xaiengine.h>
#include <xaiengine/xaiegbl_params.h>
#endif
}

// ***************************************************************
Expand Down
7 changes: 6 additions & 1 deletion profile/device/common/transactions/op_init.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,13 @@
#ifndef __OPINIT_HPP__
#define __OPINIT_HPP__

extern "C" {
#ifdef XDP_USE_AIE_CODEGEN
#include <aie_codegen.h>
#else
#include <xaiengine.h>

#endif
}
#include "op_types.h"
#include <cstring>

Expand Down
196 changes: 196 additions & 0 deletions profile/device/common/ve2/ve2_transaction.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,196 @@
// SPDX-License-Identifier: Apache-2.0
// Copyright (C) 2026 Advanced Micro Devices, Inc. All rights reserved

#include <sstream>

#include "ve2_transaction.h"
#include "core/common/message.h"
#include "xrt/experimental/xrt_elf.h"
#include "xrt/experimental/xrt_ext.h"
#include "xrt/experimental/xrt_module.h"
#include "xrt/xrt_hw_context.h"
#include "xrt/xrt_kernel.h"

#include "core/common/aiebu/src/cpp/include/aiebu/aiebu_assembler.h"
#include "core/common/aiebu/src/cpp/include/aiebu/aiebu_error.h"

#include <cstdlib>
#include <iostream>
#include <fstream>
#include <iomanip>

extern "C" {
#include <aie_codegen.h>
#include <aie_codegen_inc/xaiegbl_params.h>
}

namespace xdp::aie {
using severity_level = xrt_core::message::severity_level;

bool VE2Transaction::initializeTransaction(XAie_DevInst* aieDevInst, std::string tName)
{
setTransactionName(tName);
xrt_core::message::send(xrt_core::message::severity_level::debug, "XRT",
"Writing to New Control Code ASM file: " + getAsmFileName());

try {
// On VE2 Linux, the default IO backend is Linux IO which tries to open
// the AIE character device — this fails on XDNA (PCIe NPU) since the
// AIE is managed by the XDNA driver, not the Linux AIE driver.
// Explicitly switch to control-code backend before opening the ASM file.
XAie_SetIOBackend(aieDevInst, XAIE_IO_BACKEND_CONTROLCODE);
XAie_OpenControlCodeFile(aieDevInst, getAsmFileName().c_str(), 8192);
XAie_StartNewJob(aieDevInst, XAIE_START_JOB);
return true;
}
catch(const std::exception& e) {
xrt_core::message::send(xrt_core::message::severity_level::error, "XRT",
"Error in generating asm File: " + getAsmFileName() + "\n" + e.what());
}
xrt_core::message::send(severity_level::warning, "XRT", "AIE Transaction Initialization Failed.");
return false;
}

bool VE2Transaction::completeASM(XAie_DevInst* aieDevInst)
{
//
// 1. End generation of ASM file
//
try {
XAie_EndJob(aieDevInst);
XAie_EndPage(aieDevInst);
XAie_CloseControlCodeFile(aieDevInst);
}
catch(const std::exception& e) {
xrt_core::message::send(xrt_core::message::severity_level::error, "XRT",
"Error in generating ASM file: " + getAsmFileName() + "\n" + e.what());
return false;
}
return true;
}

bool VE2Transaction::generateELF()
{
//
// 2. Convert ASM to ELF
//
// Fill this vector with ASM content
std::vector<char> control_code_buf;
std::vector<std::string> libpaths;
libpaths.push_back("./");

try {
#if 1
//Read ASM file
std::string asmFileName = getAsmFileName();
if (!std::filesystem::exists(asmFileName))
throw std::runtime_error("file:" + asmFileName + " not found\n");

std::ifstream inAsm(asmFileName, std::ios::in | std::ios::binary);
std::cout << "Open file " << asmFileName << std::endl;

auto file_size = std::filesystem::file_size(asmFileName);
control_code_buf.resize(file_size);

inAsm.read(control_code_buf.data(), file_size);
std::streamsize bytesRead = inAsm.gcount();
if (static_cast<std::size_t>(bytesRead) != static_cast<std::size_t>(file_size)) {
std::cerr << "Read " << bytesRead << " bytes but expected " << file_size
<< " for file " << asmFileName << '\n';
control_code_buf.resize(static_cast<std::size_t>(bytesRead)); // keep only read bytes
} else {
std::cout << "ASM file read (" << file_size << " bytes): " << asmFileName << '\n';
}

//Convert ASM to ELF data.
auto as = aiebu::aiebu_assembler(aiebu::aiebu_assembler::buffer_type::asm_aie2ps,
control_code_buf, std::vector<std::string>{}, libpaths);

//Write elf data to a file
auto e = as.get_elf();
std::cout << "Elf size:" << e.size() << std::endl;
std::ofstream outElf(getElfFileName(), std::ios_base::binary);
outElf.write(e.data(), e.size());
#else
auto check1 = std::getenv("AIEBU_REPO");
auto check2 = std::getenv("PYTHONPATH");
if ((check1 == nullptr) || (check2 == nullptr)) {
xrt_core::message::send(xrt_core::message::severity_level::warning, "XRT",
"Please define AIEBU_REPO and PYTHONPATH so elf generation can work.");
return false;
}

std::stringstream command;
command << "${AIEBU_REPO}/src/python/aiebu/control_asm_disasm.py -t aie4 "
<< getAsmFileName() << " -o " << getElfFileName();
xrt_core::message::send(xrt_core::message::severity_level::debug, "XRT",
"Generating ELF using: " + command.str());
if (system(command.str().c_str())) {
xrt_core::message::send(xrt_core::message::severity_level::debug, "XRT",
"Elf generation failed");
return false;
}
#endif
}
catch(const std::exception& e) {
xrt_core::message::send(xrt_core::message::severity_level::error, "XRT",
"Error in generating Elf file: " + getElfFileName() + "\n" + e.what());
return false;
}
return true;
}

bool VE2Transaction::submitELF(xrt::hw_context hwContext)
{
//
// 3. Submit ELF to microcontroller
//
xrt_core::message::send(xrt_core::message::severity_level::debug, "XRT",
"Start New Control Code Elf");
xrt::elf profileElf;
try {
profileElf = xrt::elf(getElfFileName());
}
catch (...) {
xrt_core::message::send(xrt_core::message::severity_level::warning, "XRT",
"Failed to load " + getElfFileName() + ". Cannot configure AIE to profile.");
return false;
}

xrt_core::message::send(xrt_core::message::severity_level::debug, "XRT", "Elf Object Created");
xrt::module mod{profileElf};

xrt_core::message::send(xrt_core::message::severity_level::debug, "XRT", "Module Created");
xrt::kernel kernel;
try {
kernel = xrt::ext::kernel{hwContext, mod, "XDP_KERNEL:{IPUV1CNN}"};
} catch (...) {
xrt_core::message::send(xrt_core::message::severity_level::warning, "XRT",
"XDP_KERNEL not found in HW Context. Unable to run " + getElfFileName());
return false;
}

xrt_core::message::send(xrt_core::message::severity_level::debug, "XRT", "XDP_KERNEL created");
xrt::run run{kernel};

xrt_core::message::send(xrt_core::message::severity_level::debug, "XRT", "Kernel run created");
run.start();

xrt_core::message::send(xrt_core::message::severity_level::debug, "XRT", "Run started");
run.wait2();

xrt_core::message::send(xrt_core::message::severity_level::debug, "XRT", "Wait done!");
return true;
}

bool VE2Transaction::submitTransaction(XAie_DevInst* aieDevInst, xrt::hw_context hwContext)
{
if (!completeASM(aieDevInst))
return false;
if (!generateELF())
return false;
if (!submitELF(hwContext))
return false;
return true;
}
}
54 changes: 54 additions & 0 deletions profile/device/common/ve2/ve2_transaction.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// SPDX-License-Identifier: Apache-2.0
// Copyright (C) 2026 Advanced Micro Devices, Inc. All rights reserved

#ifndef VE2_TRANSACTION_DOT_H
#define VE2_TRANSACTION_DOT_H

#include <cstdint>
#include <string>
#include <vector>

#include "xrt/xrt_hw_context.h"
#include "xrt/xrt_kernel.h"

extern "C" {
#include <aie_codegen.h>
#include <aie_codegen_inc/xaiegbl_params.h>
}

/**
* @brief VE2Transaction class for generating and submitting VE2 XDNA transactions
*
* This class is used to generate and submit VE2 transactions. It is used to generate the ASM file, the ELF file, and submit the transaction.
*
*/

namespace xdp::aie {
class VE2Transaction {
public:
VE2Transaction() {};
bool initializeTransaction(XAie_DevInst* aieDevInst, std::string tName);
bool submitTransaction(XAie_DevInst* aieDevInst, xrt::hw_context hwContext);
bool completeASM(XAie_DevInst* aieDevInst);
bool generateELF();
bool submitELF(xrt::hw_context hwContext);

void setTransactionName(std::string newTransactionName) {m_transactionName = newTransactionName;}
std::string getAsmFileName() { return m_transactionName + ".asm"; }
std::string getElfFileName() { return m_transactionName + ".elf"; }
int getGroupID(int id, xrt::hw_context hwContext) {
xrt::kernel kernel = xrt::kernel(hwContext, "XDP_KERNEL");
return kernel.group_id(id);
}

private:
std::string m_transactionName;
std::vector<uint8_t> m_columns;
std::vector<uint8_t> m_rows;
std::vector<uint64_t> m_offsets;
std::vector<uint32_t> m_values;
};

} // namespace xdp::aie

#endif
5 changes: 5 additions & 0 deletions profile/plugin/aie_base/aie_base_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,13 @@
#include "xdp/profile/plugin/aie_base/generations/aie_generations.h"

extern "C" {
#ifdef XDP_USE_AIE_CODEGEN
#include <aie_codegen.h>
#include <aie_codegen_inc/xaiegbl_params.h>
#else
#include <xaiengine.h>
#include <xaiengine/xaiegbl_params.h>
#endif
}

namespace xdp::aie {
Expand Down
8 changes: 4 additions & 4 deletions profile/plugin/aie_debug/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ file(GLOB AIE_DRIVER_COMMON_UTIL_FILES
if (XDP_VE2_BUILD_CMAKE STREQUAL "yes")
add_library(xdp_aie_debug_plugin SHARED ${AIE_DEBUG_PLUGIN_FILES})
add_dependencies(xdp_aie_debug_plugin xdp_core xrt_coreutil)
target_link_libraries(xdp_aie_debug_plugin PRIVATE xdp_core xrt_coreutil xaiengine)
target_compile_definitions(xdp_aie_debug_plugin PRIVATE FAL_LINUX="on" XDP_VE2_BUILD=1)
target_link_libraries(xdp_aie_debug_plugin PRIVATE xdp_core xrt_coreutil aie_codegen)
target_compile_definitions(xdp_aie_debug_plugin PRIVATE FAL_LINUX="on" XDP_VE2_BUILD=1 XDP_USE_AIE_CODEGEN=1)
target_include_directories(xdp_aie_debug_plugin PRIVATE ${CMAKE_SOURCE_DIR}/src)

install (TARGETS xdp_aie_debug_plugin
Expand All @@ -46,8 +46,8 @@ if (XDP_VE2_BUILD_CMAKE STREQUAL "yes")
elseif (XDP_CLIENT_BUILD_CMAKE STREQUAL "yes")
add_library(xdp_aie_debug_plugin SHARED ${AIE_DEBUG_PLUGIN_FILES} ${AIE_DRIVER_COMMON_UTIL_FILES})
add_dependencies(xdp_aie_debug_plugin xdp_core xrt_coreutil)
target_link_libraries(xdp_aie_debug_plugin PRIVATE xdp_core xrt_coreutil xaiengine)
target_compile_definitions(xdp_aie_debug_plugin PRIVATE XDP_CLIENT_BUILD=1 -DXAIE_FEATURE_MSVC)
target_link_libraries(xdp_aie_debug_plugin PRIVATE xdp_core xrt_coreutil aie_codegen)
target_compile_definitions(xdp_aie_debug_plugin PRIVATE XDP_CLIENT_BUILD=1 XDP_USE_AIE_CODEGEN=1 -DXAIE_FEATURE_MSVC)
target_include_directories(xdp_aie_debug_plugin PRIVATE ${AIERT_DIR}/include)
set_target_properties(xdp_aie_debug_plugin PROPERTIES VERSION ${XRT_VERSION_STRING} SOVERSION ${XRT_SOVERSION})

Expand Down
5 changes: 5 additions & 0 deletions profile/plugin/aie_debug/aie_debug_metadata.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,13 @@
#include "xdp/profile/plugin/vp_base/vp_base_plugin.h"

extern "C" {
#ifdef XDP_USE_AIE_CODEGEN
#include <aie_codegen.h>
#include <aie_codegen_inc/xaiegbl_params.h>
#else
#include <xaiengine.h>
#include <xaiengine/xaiegbl_params.h>
#endif
}

namespace xdp {
Expand Down
4 changes: 2 additions & 2 deletions profile/plugin/aie_debug/client/aie_debug.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
#include "core/include/xrt/xrt_hw_context.h"

extern "C" {
#include <xaiengine.h>
#include <xaiengine/xaiegbl_params.h>
#include <aie_codegen.h>
#include <aie_codegen_inc/xaiegbl_params.h>
}

namespace xdp {
Expand Down
Loading
Loading