From 2e664d2492f492eeed5618e8c4e7308b81dfdffa Mon Sep 17 00:00:00 2001 From: Jacob Lambert Date: Thu, 13 Apr 2023 16:25:13 -0700 Subject: [PATCH] SWDEV-371628 - Shift device lib linking into clang driver Previously, we used the following approach and Comgr actions for device lib linking: AMD_COMGR_COMPILE_SOURCE_TO_BC (compile with clang driver) AMD_COMGR_ADD_DEVICE_LIBRARIES (link in device libs with llvm-link API) However, the clang driver can link in device libraries as part of compilation, assuming a --rocm-path is set. In this context, this is accomplished by using the following Comgr action instead: AMD_COMGR_COMPILE_SOURCE_WITH_DEVICE_LIBS_TO_BC (compile and link in device libs with clang driver) Change-Id: I661465865365afecc44aa15d4df91bfab361af8d [ROCm/clr commit: a4c5c440085a911d5802222578d85feeed8bef90] --- projects/clr/rocclr/device/devprogram.cpp | 46 +++++++++-------------- projects/clr/rocclr/device/devprogram.hpp | 6 +-- 2 files changed, 21 insertions(+), 31 deletions(-) diff --git a/projects/clr/rocclr/device/devprogram.cpp b/projects/clr/rocclr/device/devprogram.cpp index 493f20b48d..f989f13ef4 100644 --- a/projects/clr/rocclr/device/devprogram.cpp +++ b/projects/clr/rocclr/device/devprogram.cpp @@ -348,7 +348,7 @@ amd_comgr_status_t Program::createAction(const amd_comgr_language_t oclver, bool Program::linkLLVMBitcode(const amd_comgr_data_set_t inputs, const std::vector& options, amd::option::Options* amdOptions, amd_comgr_data_set_t* output, - char* binaryData[], size_t* binarySize, const bool link_dev_libs) { + char* binaryData[], size_t* binarySize) { amd_comgr_language_t langver = getCOMGRLanguage(isHIP(), *amdOptions); if (langver == AMD_COMGR_LANGUAGE_NONE) { @@ -357,28 +357,13 @@ bool Program::linkLLVMBitcode(const amd_comgr_data_set_t inputs, // Create the action for linking amd_comgr_action_info_t action; - amd_comgr_data_set_t dataSetDevLibs; bool hasAction = false; - bool hasDataSetDevLibs = false; amd_comgr_status_t status = createAction(langver, options, &action, &hasAction); - if (link_dev_libs) { - if (status == AMD_COMGR_STATUS_SUCCESS) { - status = amd::Comgr::create_data_set(&dataSetDevLibs); - } - - if (status == AMD_COMGR_STATUS_SUCCESS) { - hasDataSetDevLibs = true; - status = amd::Comgr::do_action(AMD_COMGR_ACTION_ADD_DEVICE_LIBRARIES, action, inputs, - dataSetDevLibs); - extractBuildLog(dataSetDevLibs); - } - } - if (status == AMD_COMGR_STATUS_SUCCESS) { status = amd::Comgr::do_action(AMD_COMGR_ACTION_LINK_BC_TO_BC, action, - (link_dev_libs) ? dataSetDevLibs : inputs, *output); + inputs, *output); extractBuildLog(*output); } @@ -395,17 +380,14 @@ bool Program::linkLLVMBitcode(const amd_comgr_data_set_t inputs, amd::Comgr::destroy_action_info(action); } - if (hasDataSetDevLibs) { - amd::Comgr::destroy_data_set(dataSetDevLibs); - } - return (status == AMD_COMGR_STATUS_SUCCESS); } bool Program::compileToLLVMBitcode(const amd_comgr_data_set_t compileInputs, const std::vector& options, amd::option::Options* amdOptions, - char* binaryData[], size_t* binarySize) { + char* binaryData[], size_t* binarySize, + const bool link_dev_libs) { amd_comgr_language_t langver = getCOMGRLanguage(isHIP(), *amdOptions); if (langver == AMD_COMGR_LANGUAGE_NONE) { @@ -479,8 +461,14 @@ bool Program::compileToLLVMBitcode(const amd_comgr_data_set_t compileInputs, // Compiling the source codes with precompiled headers or directly compileInputs if (status == AMD_COMGR_STATUS_SUCCESS) { - status = amd::Comgr::do_action(AMD_COMGR_ACTION_COMPILE_SOURCE_TO_BC, - action, input, output); + if (link_dev_libs) { + status = amd::Comgr::do_action(AMD_COMGR_ACTION_COMPILE_SOURCE_WITH_DEVICE_LIBS_TO_BC, + action, input, output); + } + else { + status = amd::Comgr::do_action(AMD_COMGR_ACTION_COMPILE_SOURCE_TO_BC, + action, input, output); + } extractBuildLog(output); } @@ -677,8 +665,11 @@ bool Program::compileImplLC(const std::string& sourceCode, driverOptions.push_back("-mllvm"); driverOptions.push_back("-amdgpu-early-inline-all"); #endif - driverOptions.push_back("-mllvm"); - driverOptions.push_back("-amdgpu-prelink"); + + // TODO: Re-enable prelink once folding optimizations no longer + // introduce undefined symbol errors + //driverOptions.push_back("-mllvm"); + //driverOptions.push_back("-amdgpu-prelink"); if (!device().settings().enableWgpMode_) { driverOptions.push_back("-mcumode"); @@ -968,9 +959,8 @@ bool Program::linkImplLC(const std::vector& inputPrograms, char* binaryData = nullptr; size_t binarySize = 0; std::vector linkOptions; - constexpr bool kLinkDevLibs = false; bool ret = linkLLVMBitcode(inputs, linkOptions, options, &output, &binaryData, - &binarySize, kLinkDevLibs); + &binarySize); amd::Comgr::destroy_data_set(output); amd::Comgr::destroy_data_set(inputs); diff --git a/projects/clr/rocclr/device/devprogram.hpp b/projects/clr/rocclr/device/devprogram.hpp index 55eac9984e..08a8d0dec2 100644 --- a/projects/clr/rocclr/device/devprogram.hpp +++ b/projects/clr/rocclr/device/devprogram.hpp @@ -457,13 +457,13 @@ class Program : public amd::HeapObject { bool linkLLVMBitcode(const amd_comgr_data_set_t inputs, const std::vector& options, amd::option::Options* amdOptions, amd_comgr_data_set_t* output, - char* binaryData[] = nullptr, size_t* binarySize = nullptr, - const bool link_dev_libs = true); + char* binaryData[] = nullptr, size_t* binarySize = nullptr); //! Create the bitcode of the compiled input dataset bool compileToLLVMBitcode(const amd_comgr_data_set_t compileInputs, const std::vector& options, amd::option::Options* amdOptions, - char* binaryData[], size_t* binarySize); + char* binaryData[], size_t* binarySize, + const bool link_dev_libs = true); //! Compile and create the excutable of the input dataset bool compileAndLinkExecutable(const amd_comgr_data_set_t inputs,