Skip to content

Commit

Permalink
restore xclbinutil as an exe because of static initializers
Browse files Browse the repository at this point in the history
  • Loading branch information
makslevental committed Aug 18, 2024
1 parent 9d42e4a commit 93bd659
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 42 deletions.
12 changes: 12 additions & 0 deletions cmake/iree_aie_utils.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception

# https://stackoverflow.com/a/49216539/9045206
# TODO(max): https://cmake.org/cmake/help/latest/command/target_compile_options.html#arguments
# these add private flags; implement both private and public/interface
function(remove_flag_from_target _target _flag)
get_target_property(_target_cxx_flags ${_target} COMPILE_OPTIONS)
if(_target_cxx_flags)
Expand All @@ -13,6 +15,16 @@ function(remove_flag_from_target _target _flag)
endif()
endfunction()

function(add_flags_to_target _target _flags)
get_target_property(_target_cxx_flags ${_target} COMPILE_OPTIONS)
if(_target_cxx_flags)
list(APPEND _target_cxx_flags ${_flags})
set_target_properties(${_target} PROPERTIES COMPILE_OPTIONS "${_target_cxx_flags}")
else()
set_target_properties(${_target} PROPERTIES COMPILE_OPTIONS "${_flags}")
endif()
endfunction()

function(replace_string_in_file _file _match_string _replace_string)
if(NOT (EXISTS ${_file}))
message(FATAL_ERROR "file ${_file} does not exist")
Expand Down
43 changes: 21 additions & 22 deletions cmake/iree_aie_xrt.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ FetchContent_Declare(
URL https://github.com/boostorg/boost/releases/download/boost-1.81.0/boost-1.81.0.7z
USES_TERMINAL_DOWNLOAD TRUE
GIT_PROGRESS TRUE
DOWNLOAD_NO_EXTRACT FALSE)
DOWNLOAD_NO_EXTRACT FALSE
# prevents configure from rerunning all the time
URL_HASH MD5=84bc7c861606dc66bcfbeb660fcddfd2)
FetchContent_MakeAvailable(Boost)
set(IREE_AIE_BOOST_LIBS
any
Expand Down Expand Up @@ -91,9 +93,6 @@ replace_string_in_file(${IREE_XRT_SOURCE_DIR}/runtime_src/core/common/query.h
# xclbinutil
# ##############################################################################

replace_string_in_file("${_xclbinutil_source_dir}/xclbinutil.cxx"
"int main" "int iree_aie_xclbinutil_main")

set(_noop_xclbin_sig_cxx "
#include \"XclBinSignature.h\"
void signXclBinImage(const std::string& _fileOnDisk,
Expand Down Expand Up @@ -121,8 +120,8 @@ file(
"${_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}/Section*.cxx"
"${_xclbinutil_source_dir}/Resources*.cxx"
"${_xclbinutil_source_dir}/XclBinClass.cxx"
Expand All @@ -132,21 +131,25 @@ file(
"${_xclbinutil_source_dir}/XclBinUtilMain.cxx"
)

add_library(iree-aie-xclbinutil STATIC ${_xclbinutil_srcs})
set(_xrt_compile_options "")
if(WIN32)
list(APPEND _xrt_compile_options /EHsc /GR)
else()
list(APPEND _xrt_compile_options -fexceptions -frtti)
endif()
target_compile_options(iree-aie-xclbinutil PRIVATE ${_xrt_compile_options})
# Unlike bootgen, xclbinutil cannot be built separately as a static archive (I wish!)
# because the linker will DCE static initializers in SectionMemTopology.cxx
# and then --add-replace-section:MEM_TOPOLOGY won't work...
# XRT/src/runtime_src/tools/xclbinutil/SectionMemTopology.cxx#L26-L41
add_executable(iree-aie-xclbinutil ${_xclbinutil_srcs})
function(add_xrt_flags_to_target _target)
set(_xrt_compile_options CACHE STRING "")
if(WIN32)
list(APPEND _xrt_compile_options /EHsc /GR)
else()
list(APPEND _xrt_compile_options -fexceptions -frtti)
endif()
add_flags_to_target(${_target} ${_xrt_compile_options})
endfunction()
add_xrt_flags_to_target(iree-aie-xclbinutil)

set(THREADS_PREFER_PTHREAD_FLAG ON)
set(_xclbin_libs $<BUILD_LOCAL_INTERFACE:${IREE_AIE_BOOST_LIBS}> Threads::Threads)
set(_xclbinutil_compile_definitions
-DBOOST_BIND_GLOBAL_PLACEHOLDERS
# prevents collision with bootgen's Section class
-DSection=XCLBinUtilSection)
set(_xclbinutil_compile_definitions -DBOOST_BIND_GLOBAL_PLACEHOLDERS)

if(NOT WIN32)
list(APPEND _xclbinutil_compile_definitions -DENABLE_JSON_SCHEMA_VALIDATION)
Expand All @@ -161,10 +164,6 @@ target_include_directories(iree-aie-xclbinutil
PRIVATE ${XRT_BINARY_DIR}/gen
${IREE_XRT_SOURCE_DIR}/runtime_src/core/include
${_xclbinutil_source_dir})
# for some reason windows doesn't respect the standard output path without this
set_target_properties(iree-aie-xclbinutil
PROPERTIES LIBRARY_OUTPUT_DIRECTORY "${PROJECT_BINARY_DIR}/lib"
RUNTIME_OUTPUT_DIRECTORY "${PROJECT_BINARY_DIR}/lib")
iree_install_targets(
TARGETS iree-aie-xclbinutil
COMPONENT IREEBundledLibraries
Expand Down Expand Up @@ -201,6 +200,6 @@ foreach(_core_lib IN LISTS _core_libs)
target_include_directories(${_core_lib} SYSTEM PUBLIC
${IREE_XRT_SOURCE_DIR}/runtime_src/core/common/elf)
target_compile_definitions(${_core_lib} PUBLIC -DBOOST_BIND_GLOBAL_PLACEHOLDERS)
target_compile_options(${_core_lib} PUBLIC ${_xrt_compile_options})
add_xrt_flags_to_target(${_core_lib})
target_link_libraries(${_core_lib} PUBLIC $<BUILD_LOCAL_INTERFACE:${IREE_AIE_BOOST_LIBS}>)
endforeach()
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@ iree_cc_library(
iree-amd-aie::aie_runtime::iree_aie_runtime_static
iree::target::amd-aie::Transforms
iree-aie-bootgen
iree-aie-xclbinutil
)

add_dependencies(iree_target_amd-aie_Target_AIETargets iree-aie-xclbinutil)

iree_cc_library(
NAME
Target
Expand Down
32 changes: 13 additions & 19 deletions compiler/plugins/target/AMD-AIE/iree-amd-aie/Target/XCLBinGen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
#define DEBUG_TYPE "amdaie-xclbingen"

extern int iree_aie_bootgen_main(int argc, const char *argv[]);
extern int iree_aie_xclbinutil_main(int argc, char **argv);

// https://stackoverflow.com/a/60198074
namespace uuid {
Expand Down Expand Up @@ -886,21 +885,20 @@ static LogicalResult generateXCLBin(
// Execute the xclbinutil command.
std::string memArg = "MEM_TOPOLOGY:JSON:" + memTopologyJsonFile.string();
std::string partArg = "AIE_PARTITION:JSON:" + aiePartitionJsonFile.string();
FailureOr<Path> xclbinutilBin =
findAMDAIETool("iree-aie-xclbinutil", amdAIEInstallDir);

{
if (inputXclbin) {
// Create aie_partition.json.
Path aieInputPartitionJsonFile = tempDir / "aie_input_partition.json";
std::string inputPartArg =
"AIE_PARTITION:JSON:" + aieInputPartitionJsonFile.string();
std::vector<std::string> inputFlags{"", "--dump-section",
inputPartArg, "--force",
"--input", *inputXclbin};
std::vector<char *> cstrings;
cstrings.reserve(inputFlags.size());
for (const auto &inputFlag : inputFlags) {
cstrings.push_back(const_cast<char *>(inputFlag.c_str()));
}
if (iree_aie_xclbinutil_main(cstrings.size(), &cstrings[0])) {
std::vector<std::string> inputFlags{"--dump-section", inputPartArg,
"--force", "--input", *inputXclbin};

if (!succeeded(xclbinutilBin) ||
!runTool(xclbinutilBin.value(), inputFlags, verbose)) {
llvm::errs() << "failed to execute xclbinutil";
return failure();
}
Expand Down Expand Up @@ -948,16 +946,12 @@ static LogicalResult generateXCLBin(
} else {
flags.insert(flags.end(), {"--add-replace-section", memArg});
}
flags.insert(flags.end(), {"--add-kernel", kernelsJsonFile.string(),
"--add-replace-section", partArg, "--force",
"--output", std::string(Output)});
flags.insert(flags.end(),
{"--add-kernel", kernelsJsonFile, "--add-replace-section",
partArg, "--force", "--output", std::string(Output)});

std::vector<char *> cstrings;
cstrings.reserve(flags.size());
for (const auto &inputFlag : flags) {
cstrings.push_back(const_cast<char *>(inputFlag.c_str()));
}
if (iree_aie_xclbinutil_main(cstrings.size(), &cstrings[0])) {
if (!succeeded(xclbinutilBin) ||
!runTool(xclbinutilBin.value(), flags, verbose)) {
llvm::errs() << "failed to execute xclbinutil";
return failure();
}
Expand Down
2 changes: 2 additions & 0 deletions runtime/src/iree-amd-aie/driver/xrt/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,5 @@ iree_cc_library(
$<BUILD_LOCAL_INTERFACE:xrt_coreutil>
PUBLIC
)

add_xrt_flags_to_target(iree-amd-aie_driver_xrt_xrt)

0 comments on commit 93bd659

Please sign in to comment.