mirror of
https://github.com/intel/compute-runtime.git
synced 2025-12-21 09:14:47 +08:00
Initial commit
Change-Id: I4bf1707bd3dfeadf2c17b0a7daff372b1925ebbd
This commit is contained in:
139
offline_compiler/CMakeLists.txt
Normal file
139
offline_compiler/CMakeLists.txt
Normal file
@@ -0,0 +1,139 @@
|
||||
# Copyright (c) 2017, Intel Corporation
|
||||
#
|
||||
# Permission is hereby granted, free of charge, to any person obtaining a
|
||||
# copy of this software and associated documentation files (the "Software"),
|
||||
# to deal in the Software without restriction, including without limitation
|
||||
# the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
||||
# and/or sell copies of the Software, and to permit persons to whom the
|
||||
# Software is furnished to do so, subject to the following conditions:
|
||||
#
|
||||
# The above copyright notice and this permission notice shall be included
|
||||
# in all copies or substantial portions of the Software.
|
||||
#
|
||||
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
||||
# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
||||
# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
|
||||
# OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
|
||||
# ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
||||
# OTHER DEALINGS IN THE SOFTWARE.
|
||||
|
||||
cmake_minimum_required(VERSION 3.2.0 FATAL_ERROR)
|
||||
|
||||
project(cloc)
|
||||
|
||||
set(CLOC_SRCS_LIB
|
||||
${IGDRCL_SOURCE_DIR}/offline_compiler/offline_compiler.cpp
|
||||
${IGDRCL_SOURCE_DIR}/offline_compiler/offline_compiler.h
|
||||
${IGDRCL_SOURCE_DIR}/offline_compiler/options.cpp
|
||||
${IGDRCL_SOURCE_DIR}/offline_compiler/helper.cpp
|
||||
${IGDRCL_SOURCE_DIR}/runtime/compiler_interface/create_main.cpp
|
||||
${IGDRCL_SOURCE_DIR}/runtime/helpers/hw_info.cpp
|
||||
${IGDRCL_SOURCE_DIR}/runtime/helpers/file_io.cpp
|
||||
${IGDRCL_SOURCE_DIR}/runtime/helpers/abort.cpp
|
||||
${IGDRCL_SOURCE_DIR}/runtime/helpers/debug_helpers.cpp
|
||||
)
|
||||
|
||||
if (WIN32)
|
||||
list (APPEND CLOC_SRCS_LIB
|
||||
${IGDRCL_SOURCE_DIR}/runtime/os_interface/windows/os_library.cpp
|
||||
${IGDRCL_SOURCE_DIR}/runtime/os_interface/windows/options.cpp
|
||||
)
|
||||
endif (WIN32)
|
||||
|
||||
if (UNIX)
|
||||
list (APPEND CLOC_SRCS_LIB
|
||||
${IGDRCL_SOURCE_DIR}/runtime/os_interface/linux/os_library.cpp
|
||||
${IGDRCL_SOURCE_DIR}/runtime/os_interface/linux/options.cpp
|
||||
)
|
||||
endif (UNIX)
|
||||
|
||||
list (APPEND HW_SRC_INCLUDES ${IGDRCL_SOURCE_DIR}/runtime/gen_common)
|
||||
|
||||
set(CLOC_LIB_FLAGS_DEFINITIONS
|
||||
-DCIF_HEADERS_ONLY_BUILD
|
||||
)
|
||||
|
||||
set(OPTIONAL_RUNTIME_GENX_FILES
|
||||
hw_info.cpp
|
||||
)
|
||||
|
||||
foreach(GEN_NUM RANGE 0 ${MAX_GEN} 1)
|
||||
GEN_CONTAINS_PLATFORMS("SUPPORTED" ${GEN_NUM} GENX_HAS_PLATFORMS)
|
||||
if(${GENX_HAS_PLATFORMS})
|
||||
foreach(SRC_IT ${OPTIONAL_RUNTIME_GENX_FILES})
|
||||
set(SRC_FILE ${IGDRCL_SOURCE_DIR}/runtime/gen${GEN_NUM}/${SRC_IT})
|
||||
if(EXISTS ${SRC_FILE})
|
||||
list(APPEND CLOC_SRCS_LIB ${SRC_FILE})
|
||||
endif()
|
||||
endforeach()
|
||||
|
||||
GET_PLATFORMS_FOR_GEN("SUPPORTED" ${GEN_NUM} SUPPORTED_GENX_PLATFORMS)
|
||||
foreach(PLATFORM_IT ${SUPPORTED_GENX_PLATFORMS})
|
||||
string(TOLOWER ${PLATFORM_IT} PLATFORM_IT_LOWER)
|
||||
list (APPEND CLOC_SRCS_LIB
|
||||
${IGDRCL_SOURCE_DIR}/runtime/gen${GEN_NUM}/hw_info_${PLATFORM_IT_LOWER}.cpp
|
||||
${IGDRCL_SOURCE_DIR}/runtime/gen${GEN_NUM}/enable_${PLATFORM_IT_LOWER}.cpp
|
||||
)
|
||||
endforeach(PLATFORM_IT)
|
||||
endif(${GENX_HAS_PLATFORMS})
|
||||
endforeach(GEN_NUM)
|
||||
|
||||
set(CLOC_SRCS
|
||||
${CLOC_SRCS_LIB}
|
||||
main.cpp
|
||||
${IGDRCL_SOURCE_DIR}/offline_compiler/CMakeLists.txt
|
||||
)
|
||||
|
||||
add_executable(cloc ${CLOC_SRCS})
|
||||
|
||||
if(IGC_OCL_ADAPTOR_DIR) # IGC/AdaptorOCL
|
||||
target_include_directories(cloc PUBLIC "${IGC_OCL_ADAPTOR_DIR}")
|
||||
endif(IGC_OCL_ADAPTOR_DIR)
|
||||
|
||||
if(CIF_BASE_DIR)
|
||||
target_include_directories(cloc PUBLIC "${CIF_BASE_DIR}")
|
||||
endif(CIF_BASE_DIR)
|
||||
|
||||
SET(CLOC_INCLUDES
|
||||
"${HW_SRC_INCLUDES}"
|
||||
"${UMKM_SHAREDDATA_INCLUDE_PATHS}"
|
||||
"${KHRONOS_HEADERS_DIR}"
|
||||
"${IGDRCL__IGC_INCLUDE_DIR}"
|
||||
"${THIRD_PARTY_DIR}"
|
||||
PARENT_SCOPE
|
||||
)
|
||||
|
||||
target_include_directories(cloc BEFORE PRIVATE
|
||||
"${HW_SRC_INCLUDES}"
|
||||
"${UMKM_SHAREDDATA_INCLUDE_PATHS}"
|
||||
"${KHRONOS_HEADERS_DIR}"
|
||||
"${IGDRCL__IGC_INCLUDE_DIR}"
|
||||
"${THIRD_PARTY_DIR}"
|
||||
)
|
||||
target_compile_definitions(cloc PUBLIC ${CLOC_LIB_FLAGS_DEFINITIONS} ${SUPPORTED_GEN_FLAGS_DEFINITONS} DEFAULT_PLATFORM=${DEFAULT_SUPPORTED_PLATFORM})
|
||||
|
||||
if(UNIX)
|
||||
target_link_libraries(cloc dl pthread)
|
||||
endif(UNIX)
|
||||
|
||||
target_link_libraries(cloc elflib)
|
||||
|
||||
source_group("source files" FILES ${CLOC_SRCS})
|
||||
set_target_properties(cloc PROPERTIES FOLDER "offline_compiler")
|
||||
set_property(TARGET cloc APPEND_STRING PROPERTY COMPILE_FLAGS ${ASAN_FLAGS} ${TSAN_FLAGS})
|
||||
|
||||
add_custom_target(copy_compiler_files DEPENDS ${IGDRCL__IGC_TARGETS})
|
||||
set_target_properties(copy_compiler_files PROPERTIES FOLDER "opencl runtime")
|
||||
|
||||
foreach(TARGET_tmp ${IGDRCL__IGC_TARGETS})
|
||||
add_custom_command(
|
||||
TARGET copy_compiler_files
|
||||
PRE_BUILD
|
||||
COMMAND echo copying $<TARGET_FILE:${TARGET_tmp}> to "$<TARGET_FILE_DIR:cloc>"
|
||||
COMMAND ${CMAKE_COMMAND} -E copy_if_different $<TARGET_FILE:${TARGET_tmp}> $<TARGET_FILE_DIR:cloc>
|
||||
)
|
||||
endforeach(TARGET_tmp)
|
||||
|
||||
SET(CLOC_SRCS_LIB ${CLOC_SRCS_LIB} PARENT_SCOPE)
|
||||
SET(CLOC_LIB_FLAGS_DEFINITIONS ${CLOC_LIB_FLAGS_DEFINITIONS} PARENT_SCOPE)
|
||||
42
offline_compiler/create_command_stream.cpp
Normal file
42
offline_compiler/create_command_stream.cpp
Normal file
@@ -0,0 +1,42 @@
|
||||
/*
|
||||
* Copyright (c) 2017, Intel Corporation
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a
|
||||
* copy of this software and associated documentation files (the "Software"),
|
||||
* to deal in the Software without restriction, including without limitation
|
||||
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
||||
* and/or sell copies of the Software, and to permit persons to whom the
|
||||
* Software is furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included
|
||||
* in all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
||||
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
||||
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
|
||||
* OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
|
||||
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
||||
* OTHER DEALINGS IN THE SOFTWARE.
|
||||
*/
|
||||
|
||||
#include "runtime/command_stream/command_stream_receiver.h"
|
||||
#include "hw_info.h"
|
||||
#include "runtime/helpers/debug_helpers.h"
|
||||
|
||||
namespace OCLRT {
|
||||
extern CommandStreamReceiverCreateFunc commandStreamReceiverFactory[2 * IGFX_MAX_CORE];
|
||||
|
||||
CommandStreamReceiver *createCommandStream(const HardwareInfo *pHwInfo) {
|
||||
DEBUG_BREAK_IF(nullptr == pHwInfo->pPlatform);
|
||||
auto funcCreate = commandStreamReceiverFactory[IGFX_MAX_CORE + pHwInfo->pPlatform->eRenderCoreFamily];
|
||||
|
||||
return funcCreate ? funcCreate(*pHwInfo) : nullptr;
|
||||
}
|
||||
|
||||
bool getDevices(HardwareInfo **hwInfo, size_t &numDevicesReturned) {
|
||||
*hwInfo = nullptr;
|
||||
numDevicesReturned = 0;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
63
offline_compiler/helper.cpp
Normal file
63
offline_compiler/helper.cpp
Normal file
@@ -0,0 +1,63 @@
|
||||
/*
|
||||
* Copyright (c) 2017, Intel Corporation
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a
|
||||
* copy of this software and associated documentation files (the "Software"),
|
||||
* to deal in the Software without restriction, including without limitation
|
||||
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
||||
* and/or sell copies of the Software, and to permit persons to whom the
|
||||
* Software is furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included
|
||||
* in all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
||||
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
||||
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
|
||||
* OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
|
||||
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
||||
* OTHER DEALINGS IN THE SOFTWARE.
|
||||
*/
|
||||
|
||||
#include "runtime/helpers/hw_info.h"
|
||||
#include "runtime/os_interface/debug_settings_manager.h"
|
||||
|
||||
namespace OCLRT {
|
||||
|
||||
template <DebugFunctionalityLevel DebugLevel>
|
||||
DebugSettingsManager<DebugLevel>::DebugSettingsManager() {
|
||||
}
|
||||
|
||||
template <DebugFunctionalityLevel DebugLevel>
|
||||
DebugSettingsManager<DebugLevel>::~DebugSettingsManager() {
|
||||
}
|
||||
|
||||
template <DebugFunctionalityLevel DebugLevel>
|
||||
void DebugSettingsManager<DebugLevel>::writeToFile(std::string filename, const char *str, size_t length, std::ios_base::openmode mode) {
|
||||
std::ofstream outFile(filename, mode);
|
||||
if (outFile.is_open()) {
|
||||
outFile.write(str, length);
|
||||
outFile.close();
|
||||
}
|
||||
}
|
||||
|
||||
// Global Debug Settings Manager
|
||||
DebugSettingsManager<globalDebugFunctionalityLevel> DebugManager;
|
||||
|
||||
// Global table of hardware prefixes
|
||||
const char *hardwarePrefix[IGFX_MAX_PRODUCT] = {
|
||||
nullptr,
|
||||
};
|
||||
|
||||
// Global table of family names
|
||||
const char *familyName[IGFX_MAX_CORE] = {
|
||||
nullptr,
|
||||
};
|
||||
|
||||
// Global table of family names
|
||||
bool familyEnabled[IGFX_MAX_CORE] = {
|
||||
false,
|
||||
};
|
||||
|
||||
} // namespace OCLRT
|
||||
54
offline_compiler/main.cpp
Normal file
54
offline_compiler/main.cpp
Normal file
@@ -0,0 +1,54 @@
|
||||
/*
|
||||
* Copyright (c) 2017, Intel Corporation
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a
|
||||
* copy of this software and associated documentation files (the "Software"),
|
||||
* to deal in the Software without restriction, including without limitation
|
||||
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
||||
* and/or sell copies of the Software, and to permit persons to whom the
|
||||
* Software is furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included
|
||||
* in all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
||||
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
||||
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
|
||||
* OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
|
||||
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
||||
* OTHER DEALINGS IN THE SOFTWARE.
|
||||
*/
|
||||
|
||||
#include "config.h"
|
||||
|
||||
#include "offline_compiler/offline_compiler.h"
|
||||
#include "runtime/os_interface/os_library.h"
|
||||
|
||||
#include <CL/cl.h>
|
||||
|
||||
using namespace OCLRT;
|
||||
|
||||
int main(int numArgs, const char *argv[]) {
|
||||
int retVal = CL_SUCCESS;
|
||||
OfflineCompiler *pCompiler = OfflineCompiler::create(numArgs, argv, retVal);
|
||||
|
||||
if (retVal == CL_SUCCESS) {
|
||||
retVal = pCompiler->build();
|
||||
|
||||
std::string buildLog = pCompiler->getBuildLog();
|
||||
if (buildLog.empty() == false) {
|
||||
printf("%s\n", buildLog.c_str());
|
||||
}
|
||||
|
||||
if (retVal == CL_SUCCESS) {
|
||||
if (!pCompiler->isQuiet())
|
||||
printf("Build succeeded.\n");
|
||||
} else {
|
||||
printf("Build failed with error code: %d\n", retVal);
|
||||
}
|
||||
}
|
||||
|
||||
delete pCompiler;
|
||||
return retVal;
|
||||
}
|
||||
781
offline_compiler/offline_compiler.cpp
Normal file
781
offline_compiler/offline_compiler.cpp
Normal file
@@ -0,0 +1,781 @@
|
||||
/*
|
||||
* Copyright (c) 2017, Intel Corporation
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a
|
||||
* copy of this software and associated documentation files (the "Software"),
|
||||
* to deal in the Software without restriction, including without limitation
|
||||
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
||||
* and/or sell copies of the Software, and to permit persons to whom the
|
||||
* Software is furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included
|
||||
* in all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
||||
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
||||
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
|
||||
* OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
|
||||
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
||||
* OTHER DEALINGS IN THE SOFTWARE.
|
||||
*/
|
||||
|
||||
#include "cif/common/cif_main.h"
|
||||
#include "cif/helpers/error.h"
|
||||
#include "cif/import/library_api.h"
|
||||
#include "ocl_igc_interface/code_type.h"
|
||||
#include "ocl_igc_interface/fcl_ocl_device_ctx.h"
|
||||
#include "ocl_igc_interface/igc_ocl_device_ctx.h"
|
||||
#include "ocl_igc_interface/platform_helper.h"
|
||||
#include "offline_compiler.h"
|
||||
#include "igfxfmid.h"
|
||||
#include "runtime/helpers/file_io.h"
|
||||
#include "runtime/os_interface/debug_settings_manager.h"
|
||||
#include "runtime/os_interface/os_inc.h"
|
||||
#include "runtime/os_interface/os_library.h"
|
||||
#include "runtime/helpers/string.h"
|
||||
#include "runtime/helpers/debug_helpers.h"
|
||||
#include "runtime/helpers/hw_info.h"
|
||||
#include "runtime/helpers/validators.h"
|
||||
#include "elf/writer.h"
|
||||
#include <iomanip>
|
||||
#include <list>
|
||||
#include <algorithm>
|
||||
#include <iostream>
|
||||
|
||||
#ifdef _WIN32
|
||||
#include <direct.h>
|
||||
#define MakeDirectory _mkdir
|
||||
#define GetCurrentWorkingDirectory _getcwd
|
||||
#else
|
||||
#include <sys/stat.h>
|
||||
#define MakeDirectory(dir) mkdir(dir, 0777)
|
||||
#define GetCurrentWorkingDirectory getcwd
|
||||
#endif
|
||||
|
||||
namespace OCLRT {
|
||||
|
||||
CIF::CIFMain *createMainNoSanitize(CIF::CreateCIFMainFunc_t createFunc);
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
// StringsAreEqual
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
bool stringsAreEqual(const char *string1, const char *string2) {
|
||||
if (string2 == nullptr)
|
||||
return false;
|
||||
return (strcmp(string1, string2) == 0);
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
// convertToPascalCase
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
std::string convertToPascalCase(const std::string &inString) {
|
||||
std::string outString;
|
||||
bool capitalize = true;
|
||||
|
||||
for (unsigned int i = 0; i < inString.length(); i++) {
|
||||
if (isalpha(inString[i]) && capitalize == true) {
|
||||
outString += toupper(inString[i]);
|
||||
capitalize = false;
|
||||
} else if (inString[i] == '_') {
|
||||
capitalize = true;
|
||||
} else {
|
||||
outString += inString[i];
|
||||
}
|
||||
}
|
||||
return outString;
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
// ctor
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
OfflineCompiler::OfflineCompiler() = default;
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
// dtor
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
OfflineCompiler::~OfflineCompiler() {
|
||||
delete[] llvmBinary;
|
||||
delete[] genBinary;
|
||||
delete[] elfBinary;
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
// Create
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
OfflineCompiler *OfflineCompiler::create(uint32_t numArgs, const char **argv, int &retVal) {
|
||||
retVal = CL_SUCCESS;
|
||||
auto pOffCompiler = new OfflineCompiler();
|
||||
|
||||
if (pOffCompiler) {
|
||||
retVal = pOffCompiler->initialize(numArgs, argv);
|
||||
}
|
||||
|
||||
if (retVal != CL_SUCCESS) {
|
||||
delete pOffCompiler;
|
||||
pOffCompiler = nullptr;
|
||||
}
|
||||
|
||||
return pOffCompiler;
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
// buildSourceCode
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
int OfflineCompiler::buildSourceCode() {
|
||||
int retVal = CL_SUCCESS;
|
||||
|
||||
do {
|
||||
if (strcmp(sourceCode.c_str(), "") == 0) {
|
||||
retVal = CL_INVALID_PROGRAM;
|
||||
break;
|
||||
}
|
||||
UNRECOVERABLE_IF(fclDeviceCtx == nullptr);
|
||||
UNRECOVERABLE_IF(igcDeviceCtx == nullptr);
|
||||
|
||||
IGC::CodeType::CodeType_t intermediateRepresentation = useLlvmText ? IGC::CodeType::llvmLl : IGC::CodeType::llvmBc;
|
||||
auto fclSrc = CIF::Builtins::CreateConstBuffer(fclMain.get(), sourceCode.c_str(), sourceCode.size());
|
||||
auto fclOptions = CIF::Builtins::CreateConstBuffer(fclMain.get(), options.c_str(), options.size());
|
||||
auto fclInternalOptions = CIF::Builtins::CreateConstBuffer(fclMain.get(), internalOptions.c_str(), internalOptions.size());
|
||||
|
||||
auto fclTranslationCtx = fclDeviceCtx->CreateTranslationCtx(IGC::CodeType::oclC, intermediateRepresentation);
|
||||
auto igcTranslationCtx = igcDeviceCtx->CreateTranslationCtx(intermediateRepresentation, IGC::CodeType::oclGenBin);
|
||||
|
||||
if (false == OCLRT::areNotNullptr(fclSrc.get(), fclOptions.get(), fclInternalOptions.get(),
|
||||
fclTranslationCtx.get(), igcTranslationCtx.get())) {
|
||||
retVal = CL_OUT_OF_HOST_MEMORY;
|
||||
break;
|
||||
}
|
||||
|
||||
auto fclOutput = fclTranslationCtx->Translate(fclSrc.get(), fclOptions.get(),
|
||||
fclInternalOptions.get(), nullptr, 0);
|
||||
|
||||
if (fclOutput == nullptr) {
|
||||
retVal = CL_OUT_OF_HOST_MEMORY;
|
||||
break;
|
||||
}
|
||||
|
||||
UNRECOVERABLE_IF(fclOutput->GetBuildLog() == nullptr);
|
||||
UNRECOVERABLE_IF(fclOutput->GetOutput() == nullptr);
|
||||
|
||||
if (fclOutput->Successful() == false) {
|
||||
updateBuildLog(fclOutput->GetBuildLog()->GetMemory<char>(), fclOutput->GetBuildLog()->GetSizeRaw());
|
||||
retVal = CL_BUILD_PROGRAM_FAILURE;
|
||||
break;
|
||||
}
|
||||
|
||||
storeBinary(llvmBinary, llvmBinarySize, fclOutput->GetOutput()->GetMemory<char>(), fclOutput->GetOutput()->GetSizeRaw());
|
||||
updateBuildLog(fclOutput->GetBuildLog()->GetMemory<char>(), fclOutput->GetBuildLog()->GetSizeRaw());
|
||||
|
||||
auto igcOutput = igcTranslationCtx->Translate(fclOutput->GetOutput(), fclOptions.get(),
|
||||
fclInternalOptions.get(),
|
||||
nullptr, 0);
|
||||
if (igcOutput == nullptr) {
|
||||
retVal = CL_OUT_OF_HOST_MEMORY;
|
||||
break;
|
||||
}
|
||||
|
||||
UNRECOVERABLE_IF(igcOutput->GetBuildLog() == nullptr);
|
||||
UNRECOVERABLE_IF(igcOutput->GetOutput() == nullptr);
|
||||
storeBinary(genBinary, genBinarySize, igcOutput->GetOutput()->GetMemory<char>(), igcOutput->GetOutput()->GetSizeRaw());
|
||||
updateBuildLog(igcOutput->GetBuildLog()->GetMemory<char>(), igcOutput->GetBuildLog()->GetSizeRaw());
|
||||
retVal = igcOutput->Successful() ? CL_SUCCESS : CL_BUILD_PROGRAM_FAILURE;
|
||||
} while (0);
|
||||
|
||||
return retVal;
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
// build
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
int OfflineCompiler::build() {
|
||||
int retVal = CL_SUCCESS;
|
||||
|
||||
retVal = buildSourceCode();
|
||||
|
||||
if (retVal == CL_SUCCESS) {
|
||||
generateElfBinary();
|
||||
writeOutAllFiles();
|
||||
}
|
||||
|
||||
return retVal;
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
// updateBuildLog
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
void OfflineCompiler::updateBuildLog(const char *pErrorString, const size_t errorStringSize) {
|
||||
std::string errorString = (errorStringSize && pErrorString) ? std::string(pErrorString, pErrorString + errorStringSize) : "";
|
||||
if (errorString[0] != '\0') {
|
||||
if (buildLog.empty()) {
|
||||
buildLog.assign(errorString);
|
||||
} else {
|
||||
buildLog.append("\n" + errorString);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
// getBuildLog
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
std::string &OfflineCompiler::getBuildLog() {
|
||||
return buildLog;
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
// getHardwareInfo
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
int OfflineCompiler::getHardwareInfo(const char *pDeviceName) {
|
||||
int retVal = CL_INVALID_DEVICE;
|
||||
|
||||
for (unsigned int productId = 0; productId < IGFX_MAX_PRODUCT; ++productId) {
|
||||
if (stringsAreEqual(pDeviceName, hardwarePrefix[productId])) {
|
||||
if (hardwareInfoTable[productId]) {
|
||||
hwInfo = hardwareInfoTable[productId];
|
||||
retVal = CL_SUCCESS;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return retVal;
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
// getStringWithinDelimiters
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
std::string OfflineCompiler::getStringWithinDelimiters(const std::string &src) {
|
||||
size_t start = src.find("R\"===(");
|
||||
size_t stop = src.find(")===\"");
|
||||
|
||||
DEBUG_BREAK_IF(std::string::npos == start);
|
||||
DEBUG_BREAK_IF(std::string::npos == stop);
|
||||
|
||||
start += strlen("R\"===(");
|
||||
size_t size = stop - start;
|
||||
|
||||
std::string dst(src, start, size);
|
||||
return dst;
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
// Initialize
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
int OfflineCompiler::initialize(uint32_t numArgs, const char **argv) {
|
||||
int retVal = CL_SUCCESS;
|
||||
const char *pSource = nullptr;
|
||||
void *pSourceFromFile = nullptr;
|
||||
size_t sourceFromFileSize = 0;
|
||||
|
||||
retVal = parseCommandLine(numArgs, argv);
|
||||
if (retVal != CL_SUCCESS) {
|
||||
return retVal;
|
||||
}
|
||||
|
||||
parseDebugSettings();
|
||||
|
||||
if (options.empty()) {
|
||||
// try to read options from file if not provided by commandline
|
||||
std::string optionsFileName = inputFile;
|
||||
size_t ext_start = optionsFileName.find(".cl");
|
||||
if (ext_start != std::string::npos) {
|
||||
optionsFileName.replace(ext_start, strlen(".cl"), "_options.txt");
|
||||
void *pOptions = nullptr;
|
||||
size_t optionsSize = loadDataFromFile(optionsFileName.c_str(), pOptions);
|
||||
if (optionsSize > 0) {
|
||||
options = (char *)pOptions;
|
||||
// Remove comment containing copyright header
|
||||
size_t commentBegin = options.find_first_of("/*");
|
||||
size_t commentEnd = options.find_last_of("*/");
|
||||
if (commentBegin != std::string::npos && commentEnd != std::string::npos) {
|
||||
options = options.replace(commentBegin, commentEnd - commentBegin + 1, "");
|
||||
size_t optionsBegin = options.find_first_not_of(" \t\n\r");
|
||||
if (optionsBegin != std::string::npos) {
|
||||
options = options.substr(optionsBegin, options.length());
|
||||
}
|
||||
}
|
||||
auto trimPos = options.find_last_not_of(" \n\r");
|
||||
options = options.substr(0, trimPos + 1);
|
||||
if (!isQuiet())
|
||||
printf("Building with options:\n%s\n", options.c_str());
|
||||
}
|
||||
deleteDataReadFromFile(pOptions);
|
||||
}
|
||||
}
|
||||
|
||||
// set up the device inside the program
|
||||
sourceFromFileSize = loadDataFromFile(inputFile.c_str(), pSourceFromFile);
|
||||
struct Helper {
|
||||
static void deleter(void *ptr) { deleteDataReadFromFile(ptr); }
|
||||
};
|
||||
auto sourceRaii = std::unique_ptr<void, decltype(&Helper::deleter)>{pSourceFromFile, Helper::deleter};
|
||||
if (sourceFromFileSize == 0) {
|
||||
retVal = INVALID_FILE;
|
||||
return retVal;
|
||||
}
|
||||
|
||||
// we also accept files used as runtime builtins
|
||||
pSource = strstr((const char *)pSourceFromFile, "R\"===(");
|
||||
sourceCode = (pSource != nullptr) ? getStringWithinDelimiters((char *)pSourceFromFile) : (char *)pSourceFromFile;
|
||||
|
||||
this->fclLib.reset(OsLibrary::load(Os::frontEndDllName));
|
||||
if (this->fclLib == nullptr) {
|
||||
return CL_OUT_OF_HOST_MEMORY;
|
||||
}
|
||||
|
||||
auto fclCreateMain = reinterpret_cast<CIF::CreateCIFMainFunc_t>(this->fclLib->getProcAddress(CIF::CreateCIFMainFuncName));
|
||||
if (fclCreateMain == nullptr) {
|
||||
return CL_OUT_OF_HOST_MEMORY;
|
||||
}
|
||||
|
||||
this->fclMain = CIF::RAII::UPtr(createMainNoSanitize(fclCreateMain));
|
||||
if (this->fclMain == nullptr) {
|
||||
return CL_OUT_OF_HOST_MEMORY;
|
||||
}
|
||||
|
||||
if (false == this->fclMain->IsCompatible<IGC::FclOclDeviceCtx>()) {
|
||||
// given FCL is not compatible
|
||||
DEBUG_BREAK_IF(true);
|
||||
return CL_OUT_OF_HOST_MEMORY;
|
||||
}
|
||||
|
||||
this->fclDeviceCtx = this->fclMain->CreateInterface<IGC::FclOclDeviceCtxTagOCL>();
|
||||
if (this->fclDeviceCtx == nullptr) {
|
||||
return CL_OUT_OF_HOST_MEMORY;
|
||||
}
|
||||
|
||||
fclDeviceCtx->SetOclApiVersion(hwInfo->capabilityTable.clVersionSupport * 10);
|
||||
|
||||
this->igcLib.reset(OsLibrary::load(Os::igcDllName));
|
||||
if (this->igcLib == nullptr) {
|
||||
return CL_OUT_OF_HOST_MEMORY;
|
||||
}
|
||||
|
||||
auto igcCreateMain = reinterpret_cast<CIF::CreateCIFMainFunc_t>(this->igcLib->getProcAddress(CIF::CreateCIFMainFuncName));
|
||||
if (igcCreateMain == nullptr) {
|
||||
return CL_OUT_OF_HOST_MEMORY;
|
||||
}
|
||||
|
||||
this->igcMain = CIF::RAII::UPtr(createMainNoSanitize(igcCreateMain));
|
||||
if (this->igcMain == nullptr) {
|
||||
return CL_OUT_OF_HOST_MEMORY;
|
||||
}
|
||||
|
||||
if (false == this->igcMain->IsCompatible<IGC::IgcOclDeviceCtx>()) {
|
||||
// given IGC is not compatible
|
||||
DEBUG_BREAK_IF(true);
|
||||
return CL_OUT_OF_HOST_MEMORY;
|
||||
}
|
||||
|
||||
this->igcDeviceCtx = this->igcMain->CreateInterface<IGC::IgcOclDeviceCtxTagOCL>();
|
||||
if (this->igcDeviceCtx == nullptr) {
|
||||
return CL_OUT_OF_HOST_MEMORY;
|
||||
}
|
||||
this->igcDeviceCtx->SetProfilingTimerResolution(static_cast<float>(hwInfo->capabilityTable.defaultProfilingTimerResolution));
|
||||
auto igcPlatform = this->igcDeviceCtx->GetPlatformHandle();
|
||||
auto igcGtSystemInfo = this->igcDeviceCtx->GetGTSystemInfoHandle();
|
||||
auto igcFeWa = this->igcDeviceCtx->GetIgcFeaturesAndWorkaroundsHandle();
|
||||
if ((igcPlatform == nullptr) || (igcGtSystemInfo == nullptr) || (igcFeWa == nullptr)) {
|
||||
return CL_OUT_OF_HOST_MEMORY;
|
||||
}
|
||||
IGC::PlatformHelper::PopulateInterfaceWith(*igcPlatform.get(), *hwInfo->pPlatform);
|
||||
IGC::GtSysInfoHelper::PopulateInterfaceWith(*igcGtSystemInfo.get(), *hwInfo->pSysInfo);
|
||||
// populate with features
|
||||
igcFeWa.get()->SetFtrDesktop(hwInfo->pSkuTable->ftrDesktop);
|
||||
igcFeWa.get()->SetFtrChannelSwizzlingXOREnabled(hwInfo->pSkuTable->ftrChannelSwizzlingXOREnabled);
|
||||
|
||||
igcFeWa.get()->SetFtrGtBigDie(hwInfo->pSkuTable->ftrGtBigDie);
|
||||
igcFeWa.get()->SetFtrGtMediumDie(hwInfo->pSkuTable->ftrGtMediumDie);
|
||||
igcFeWa.get()->SetFtrGtSmallDie(hwInfo->pSkuTable->ftrGtSmallDie);
|
||||
|
||||
igcFeWa.get()->SetFtrGT1(hwInfo->pSkuTable->ftrGT1);
|
||||
igcFeWa.get()->SetFtrGT1_5(hwInfo->pSkuTable->ftrGT1_5);
|
||||
igcFeWa.get()->SetFtrGT2(hwInfo->pSkuTable->ftrGT2);
|
||||
igcFeWa.get()->SetFtrGT3(hwInfo->pSkuTable->ftrGT3);
|
||||
igcFeWa.get()->SetFtrGT4(hwInfo->pSkuTable->ftrGT4);
|
||||
|
||||
igcFeWa.get()->SetFtrIVBM0M1Platform(hwInfo->pSkuTable->ftrIVBM0M1Platform);
|
||||
igcFeWa.get()->SetFtrGTL(hwInfo->pSkuTable->ftrGT1);
|
||||
igcFeWa.get()->SetFtrGTM(hwInfo->pSkuTable->ftrGT2);
|
||||
igcFeWa.get()->SetFtrGTH(hwInfo->pSkuTable->ftrGT3);
|
||||
|
||||
igcFeWa.get()->SetFtrSGTPVSKUStrapPresent(hwInfo->pSkuTable->ftrSGTPVSKUStrapPresent);
|
||||
igcFeWa.get()->SetFtrGTA(hwInfo->pSkuTable->ftrGTA);
|
||||
igcFeWa.get()->SetFtrGTC(hwInfo->pSkuTable->ftrGTC);
|
||||
igcFeWa.get()->SetFtrGTX(hwInfo->pSkuTable->ftrGTX);
|
||||
igcFeWa.get()->SetFtr5Slice(hwInfo->pSkuTable->ftr5Slice);
|
||||
|
||||
igcFeWa.get()->SetFtrGpGpuMidThreadLevelPreempt(hwInfo->pSkuTable->ftrGpGpuMidThreadLevelPreempt);
|
||||
igcFeWa.get()->SetFtrIoMmuPageFaulting(hwInfo->pSkuTable->ftrIoMmuPageFaulting);
|
||||
igcFeWa.get()->SetFtrWddm2Svm(hwInfo->pSkuTable->ftrWddm2Svm);
|
||||
igcFeWa.get()->SetFtrPooledEuEnabled(hwInfo->pSkuTable->ftrPooledEuEnabled);
|
||||
|
||||
igcFeWa.get()->SetFtrResourceStreamer(hwInfo->pSkuTable->ftrResourceStreamer);
|
||||
|
||||
return retVal;
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
// ParseCommandLine
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
int OfflineCompiler::parseCommandLine(uint32_t numArgs, const char **argv) {
|
||||
int retVal = CL_SUCCESS;
|
||||
bool compile32 = false;
|
||||
bool compile64 = false;
|
||||
|
||||
if (numArgs < 2) {
|
||||
printUsage();
|
||||
retVal = PRINT_USAGE;
|
||||
}
|
||||
|
||||
for (uint32_t argIndex = 1; argIndex < numArgs; argIndex++) {
|
||||
if ((stringsAreEqual(argv[argIndex], "-file")) &&
|
||||
(argIndex + 1 < numArgs)) {
|
||||
inputFile = argv[argIndex + 1];
|
||||
argIndex++;
|
||||
} else if (stringsAreEqual(argv[argIndex], "-32")) {
|
||||
compile32 = true;
|
||||
internalOptions.append(" -m32 ");
|
||||
} else if (stringsAreEqual(argv[argIndex], "-64")) {
|
||||
compile64 = true;
|
||||
internalOptions.append(" -m64 ");
|
||||
} else if (stringsAreEqual(argv[argIndex], "-cl-intel-greater-than-4GB-buffer-required")) {
|
||||
internalOptions.append(" -cl-intel-greater-than-4GB-buffer-required ");
|
||||
} else if ((stringsAreEqual(argv[argIndex], "-device")) &&
|
||||
(argIndex + 1 < numArgs)) {
|
||||
deviceName = argv[argIndex + 1];
|
||||
argIndex++;
|
||||
} else if (stringsAreEqual(argv[argIndex], "-llvm_text")) {
|
||||
useLlvmText = true;
|
||||
} else if (stringsAreEqual(argv[argIndex], "-cpp_file")) {
|
||||
useCppFile = true;
|
||||
} else if ((stringsAreEqual(argv[argIndex], "-options")) &&
|
||||
(argIndex + 1 < numArgs)) {
|
||||
options = argv[argIndex + 1];
|
||||
argIndex++;
|
||||
} else if (stringsAreEqual(argv[argIndex], "-options_name")) {
|
||||
useOptionsSuffix = true;
|
||||
} else if ((stringsAreEqual(argv[argIndex], "-out_dir")) &&
|
||||
(argIndex + 1 < numArgs)) {
|
||||
outputDirectory = argv[argIndex + 1];
|
||||
argIndex++;
|
||||
} else if (stringsAreEqual(argv[argIndex], "-q")) {
|
||||
quiet = true;
|
||||
} else if (stringsAreEqual(argv[argIndex], "-?")) {
|
||||
printUsage();
|
||||
retVal = PRINT_USAGE;
|
||||
} else {
|
||||
printf("Invalid option (arg %d): %s\n", argIndex, argv[argIndex]);
|
||||
retVal = INVALID_COMMAND_LINE;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (retVal == CL_SUCCESS) {
|
||||
if (compile32 && compile64) {
|
||||
printf("Error: Cannot compile for 32-bit and 64-bit, please choose one.\n");
|
||||
retVal = INVALID_COMMAND_LINE;
|
||||
} else if (inputFile.empty()) {
|
||||
printf("Error: Input file name missing.\n");
|
||||
retVal = INVALID_COMMAND_LINE;
|
||||
} else if (deviceName.empty()) {
|
||||
printf("Error: Device name missing.\n");
|
||||
retVal = INVALID_COMMAND_LINE;
|
||||
} else if (!fileExists(inputFile)) {
|
||||
printf("Error: Input file %s missing.\n", inputFile.c_str());
|
||||
retVal = INVALID_FILE;
|
||||
} else {
|
||||
retVal = getHardwareInfo(deviceName.c_str());
|
||||
if (retVal != CL_SUCCESS) {
|
||||
printf("Error: Cannot get HW Info for device %s.\n", deviceName.c_str());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return retVal;
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
// ParseCommandLine
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
void OfflineCompiler::parseDebugSettings() {
|
||||
if (DebugManager.flags.EnableStatelessToStatefulBufferOffsetOpt.get()) {
|
||||
internalOptions += "-cl-intel-has-buffer-offset-arg ";
|
||||
}
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
// ParseBinAsCharArray
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
std::string OfflineCompiler::parseBinAsCharArray(uint8_t *binary, size_t size, std::string &deviceName, std::string &fileName) {
|
||||
std::string builtinName = convertToPascalCase(fileName);
|
||||
std::ostringstream out;
|
||||
|
||||
// Convert binary to cpp
|
||||
out << "#include <cstddef>\n";
|
||||
out << "#include <cstdint>\n\n";
|
||||
out << "size_t " << builtinName << "BinarySize_" << deviceName << " = " << size << ";\n";
|
||||
out << "uint32_t " << builtinName << "Binary_" << deviceName << "[" << (size + 3) / 4 << "] = {"
|
||||
<< std::endl
|
||||
<< " ";
|
||||
|
||||
uint32_t *binaryUint = (uint32_t *)binary;
|
||||
for (size_t i = 0; i < (size + 3) / 4; i++) {
|
||||
if (i != 0) {
|
||||
out << ", ";
|
||||
if (i % 8 == 0) {
|
||||
out << std::endl
|
||||
<< " ";
|
||||
}
|
||||
}
|
||||
if (i < size / 4) {
|
||||
out << "0x" << std::hex << std::setw(8) << std::setfill('0') << binaryUint[i];
|
||||
} else {
|
||||
uint32_t lastBytes = size & 0x3;
|
||||
uint32_t lastUint = 0;
|
||||
uint8_t *pLastUint = (uint8_t *)&lastUint;
|
||||
for (uint32_t j = 0; j < lastBytes; j++) {
|
||||
pLastUint[sizeof(uint32_t) - 1 - j] = binary[i * 4 + j];
|
||||
}
|
||||
out << "0x" << std::hex << std::setw(8) << std::setfill('0') << lastUint;
|
||||
}
|
||||
}
|
||||
out << "};" << std::endl;
|
||||
|
||||
out << std::endl
|
||||
<< "#include \"runtime/built_ins/registry/built_ins_registry.h\"\n"
|
||||
<< std::endl;
|
||||
out << "namespace OCLRT {" << std::endl;
|
||||
out << "static RegisterEmbeddedResource register" << builtinName << "Bin(" << std::endl;
|
||||
out << " createBuiltinResourceName(" << std::endl;
|
||||
out << " EBuiltInOps::" << builtinName << "," << std::endl;
|
||||
out << " BuiltinCode::getExtension(BuiltinCode::ECodeType::Binary), \"" << deviceName << "\", 0)" << std::endl;
|
||||
out << " .c_str()," << std::endl;
|
||||
out << " (const char *)" << builtinName << "Binary"
|
||||
<< "_" << deviceName << "," << std::endl;
|
||||
out << " " << builtinName << "BinarySize_" << deviceName << ");" << std::endl;
|
||||
out << "}" << std::endl;
|
||||
|
||||
return out.str();
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
// GetFileNameTrunk
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
std::string OfflineCompiler::getFileNameTrunk(std::string &filePath) {
|
||||
size_t slashPos = filePath.find_last_of("\\/", filePath.size()) + 1;
|
||||
size_t extPos = filePath.find_last_of(".", filePath.size());
|
||||
if (extPos == std::string::npos) {
|
||||
extPos = filePath.size();
|
||||
}
|
||||
|
||||
std::string fileName;
|
||||
std::string fileTrunk = filePath.substr(slashPos, (extPos - slashPos));
|
||||
|
||||
return fileTrunk;
|
||||
}
|
||||
//
|
||||
std::string getDevicesTypes() {
|
||||
std::list<std::string> prefixes;
|
||||
for (int j = 0; j < IGFX_MAX_PRODUCT; j++) {
|
||||
if (hardwarePrefix[j] == nullptr)
|
||||
continue;
|
||||
prefixes.push_back(hardwarePrefix[j]);
|
||||
}
|
||||
|
||||
ostringstream os;
|
||||
for (auto it = prefixes.begin(); it != prefixes.end(); it++) {
|
||||
if (it != prefixes.begin())
|
||||
os << ",";
|
||||
os << *it;
|
||||
}
|
||||
|
||||
return os.str();
|
||||
}
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
// PrintUsage
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
void OfflineCompiler::printUsage() {
|
||||
|
||||
printf("Compiles CL files into llvm (.bc or .ll), gen isa (.gen), and binary files (.bin)\n\n");
|
||||
printf("cloc -file <filename> -device <device_type> [-outdir <output_dir>]\n\n");
|
||||
printf(" -file <filename> Indicates the CL kernel file to be compiled.\n");
|
||||
printf(" -device <device_type> Indicates which device for which we will compile.\n");
|
||||
printf(" <device_type> can be: %s\n", getDevicesTypes().c_str());
|
||||
printf(" -out_dir <output_dir> Indicates the directory into which the compiled files\n");
|
||||
printf(" will be placed.\n");
|
||||
printf(" -llvm_text Readable LLVM text will be output in a .ll file instead of\n");
|
||||
printf(" through the default lllvm binary (.bc) file.\n");
|
||||
printf(" -cpp_file Cpp file with scheduler program binary will be generated.");
|
||||
printf(" -options <options> Compiler options.\n");
|
||||
printf(" -options_name Add suffix with compile options to filename\n");
|
||||
printf(" -32 Force compile to 32-bit binary.\n");
|
||||
printf(" -64 Force compile to 64-bit binary.\n");
|
||||
printf(" -q Be more quiet. print only warnings and errors.\n");
|
||||
printf(" -? Print this usage message.\n");
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
// StoreBinary
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
void OfflineCompiler::storeBinary(
|
||||
char *&pDst,
|
||||
size_t &dstSize,
|
||||
const void *pSrc,
|
||||
const size_t srcSize) {
|
||||
dstSize = 0;
|
||||
|
||||
DEBUG_BREAK_IF(!(pSrc && srcSize > 0));
|
||||
|
||||
delete[] pDst;
|
||||
pDst = new char[srcSize];
|
||||
|
||||
dstSize = (cl_uint)srcSize;
|
||||
memcpy_s(pDst, dstSize, pSrc, srcSize);
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
// GenerateElfBinary
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
bool OfflineCompiler::generateElfBinary() {
|
||||
bool retVal = true;
|
||||
CLElfLib::CElfWriter *pElfWriter = nullptr;
|
||||
|
||||
if (!genBinary || !genBinarySize) {
|
||||
retVal = false;
|
||||
}
|
||||
|
||||
if (retVal) {
|
||||
pElfWriter = CLElfLib::CElfWriter::create(CLElfLib::EH_TYPE_OPENCL_EXECUTABLE, CLElfLib::EH_MACHINE_NONE, 0);
|
||||
|
||||
if (pElfWriter) {
|
||||
CLElfLib::SSectionNode sectionNode;
|
||||
|
||||
// Always add the options string
|
||||
sectionNode.Name = "BuildOptions";
|
||||
sectionNode.Type = CLElfLib::SH_TYPE_OPENCL_OPTIONS;
|
||||
sectionNode.pData = (char *)options.c_str();
|
||||
sectionNode.DataSize = (uint32_t)(strlen(options.c_str()) + 1);
|
||||
|
||||
retVal = pElfWriter->addSection(§ionNode);
|
||||
|
||||
if (retVal) {
|
||||
sectionNode.Name = "Intel(R) OpenCL LLVM Object";
|
||||
sectionNode.Type = CLElfLib::SH_TYPE_OPENCL_LLVM_BINARY;
|
||||
sectionNode.pData = llvmBinary;
|
||||
sectionNode.DataSize = (uint32_t)llvmBinarySize;
|
||||
retVal = pElfWriter->addSection(§ionNode);
|
||||
}
|
||||
|
||||
// Add the device binary if it exists
|
||||
if (retVal && genBinary) {
|
||||
sectionNode.Name = "Intel(R) OpenCL Device Binary";
|
||||
sectionNode.Type = CLElfLib::SH_TYPE_OPENCL_DEV_BINARY;
|
||||
sectionNode.pData = genBinary;
|
||||
sectionNode.DataSize = (uint32_t)genBinarySize;
|
||||
|
||||
retVal = pElfWriter->addSection(§ionNode);
|
||||
}
|
||||
|
||||
if (retVal) {
|
||||
// get the size
|
||||
retVal = pElfWriter->resolveBinary(elfBinary, elfBinarySize);
|
||||
}
|
||||
|
||||
if (retVal) {
|
||||
// allocate the binary
|
||||
elfBinary = new char[elfBinarySize];
|
||||
|
||||
retVal = pElfWriter->resolveBinary(elfBinary, elfBinarySize);
|
||||
}
|
||||
} else {
|
||||
retVal = false;
|
||||
}
|
||||
|
||||
CLElfLib::CElfWriter::destroy(pElfWriter);
|
||||
}
|
||||
|
||||
return retVal;
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
// WriteOutAllFiles
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
void OfflineCompiler::writeOutAllFiles() {
|
||||
std::string fileTrunk = getFileNameTrunk(inputFile);
|
||||
std::string fileBase = fileTrunk + "_" + deviceName;
|
||||
|
||||
if (outputDirectory != "") {
|
||||
std::list<std::string> dirList;
|
||||
std::string tmp = outputDirectory;
|
||||
size_t pos = outputDirectory.size() + 1;
|
||||
|
||||
do {
|
||||
dirList.push_back(tmp);
|
||||
pos = tmp.find_last_of("/\\", pos);
|
||||
tmp = tmp.substr(0, pos);
|
||||
} while (pos != std::string::npos);
|
||||
|
||||
while (!dirList.empty()) {
|
||||
MakeDirectory(dirList.back().c_str());
|
||||
dirList.pop_back();
|
||||
}
|
||||
}
|
||||
|
||||
if (llvmBinary) {
|
||||
std::string llvmOutputFile = (outputDirectory == "") ? "" : outputDirectory + "/";
|
||||
(useLlvmText == true) ? llvmOutputFile.append(fileBase + ".ll") : llvmOutputFile.append(fileBase + ".bc");
|
||||
|
||||
if (useOptionsSuffix) {
|
||||
std::string opts(options.c_str());
|
||||
std::replace(opts.begin(), opts.end(), ' ', '_');
|
||||
llvmOutputFile.append(opts);
|
||||
}
|
||||
|
||||
writeDataToFile(
|
||||
llvmOutputFile.c_str(),
|
||||
llvmBinary,
|
||||
llvmBinarySize);
|
||||
}
|
||||
|
||||
if (genBinary) {
|
||||
std::string genOutputFile = (outputDirectory == "") ? "" : outputDirectory + "/";
|
||||
genOutputFile.append(fileBase + ".gen");
|
||||
|
||||
if (useOptionsSuffix) {
|
||||
std::string opts(options.c_str());
|
||||
std::replace(opts.begin(), opts.end(), ' ', '_');
|
||||
genOutputFile.append(opts);
|
||||
}
|
||||
|
||||
writeDataToFile(
|
||||
genOutputFile.c_str(),
|
||||
genBinary,
|
||||
genBinarySize);
|
||||
|
||||
if (useCppFile) {
|
||||
std::string cppOutputFile = (outputDirectory == "") ? "" : outputDirectory + "/";
|
||||
cppOutputFile.append(fileBase + ".cpp");
|
||||
std::string cpp = parseBinAsCharArray((uint8_t *)genBinary, genBinarySize, deviceName, fileTrunk);
|
||||
writeDataToFile(cppOutputFile.c_str(), cpp.c_str(), cpp.size());
|
||||
}
|
||||
}
|
||||
|
||||
if (elfBinary) {
|
||||
std::string elfOutputFile = (outputDirectory == "") ? "" : outputDirectory + "/";
|
||||
|
||||
elfOutputFile.append(fileBase + ".bin");
|
||||
|
||||
if (useOptionsSuffix) {
|
||||
std::string opts(options.c_str());
|
||||
std::replace(opts.begin(), opts.end(), ' ', '_');
|
||||
elfOutputFile.append(opts);
|
||||
}
|
||||
|
||||
writeDataToFile(
|
||||
elfOutputFile.c_str(),
|
||||
elfBinary,
|
||||
elfBinarySize);
|
||||
}
|
||||
}
|
||||
} // namespace OCLRT
|
||||
106
offline_compiler/offline_compiler.h
Normal file
106
offline_compiler/offline_compiler.h
Normal file
@@ -0,0 +1,106 @@
|
||||
/*
|
||||
* Copyright (c) 2017, Intel Corporation
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a
|
||||
* copy of this software and associated documentation files (the "Software"),
|
||||
* to deal in the Software without restriction, including without limitation
|
||||
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
||||
* and/or sell copies of the Software, and to permit persons to whom the
|
||||
* Software is furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included
|
||||
* in all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
||||
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
||||
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
|
||||
* OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
|
||||
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
||||
* OTHER DEALINGS IN THE SOFTWARE.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
#include "cif/common/cif_main.h"
|
||||
#include "ocl_igc_interface/igc_ocl_device_ctx.h"
|
||||
#include "ocl_igc_interface/fcl_ocl_device_ctx.h"
|
||||
#include <cstdint>
|
||||
#include <string>
|
||||
#include <memory>
|
||||
|
||||
namespace OCLRT {
|
||||
|
||||
struct HardwareInfo;
|
||||
class OsLibrary;
|
||||
|
||||
std::string convertToPascalCase(const std::string &inString);
|
||||
|
||||
enum ErrorCode {
|
||||
INVALID_COMMAND_LINE = -5150,
|
||||
INVALID_FILE = -5151,
|
||||
PRINT_USAGE = -5152,
|
||||
};
|
||||
|
||||
class OfflineCompiler {
|
||||
public:
|
||||
static OfflineCompiler *create(uint32_t numArgs, const char **argv, int &retVal);
|
||||
int build();
|
||||
std::string &getBuildLog();
|
||||
void printUsage();
|
||||
|
||||
OfflineCompiler &operator=(const OfflineCompiler &) = delete;
|
||||
OfflineCompiler(const OfflineCompiler &) = delete;
|
||||
~OfflineCompiler();
|
||||
|
||||
bool isQuiet() const {
|
||||
return quiet;
|
||||
}
|
||||
|
||||
std::string parseBinAsCharArray(uint8_t *binary, size_t size, std::string &deviceName, std::string &fileName);
|
||||
|
||||
protected:
|
||||
OfflineCompiler();
|
||||
|
||||
int getHardwareInfo(const char *pDeviceName);
|
||||
std::string getFileNameTrunk(std::string &filePath);
|
||||
std::string getStringWithinDelimiters(const std::string &src);
|
||||
int initialize(uint32_t numArgs, const char **argv);
|
||||
int parseCommandLine(uint32_t numArgs, const char **argv);
|
||||
void parseDebugSettings();
|
||||
void storeBinary(char *&pDst, size_t &dstSize, const void *pSrc, const size_t srcSize);
|
||||
int buildSourceCode();
|
||||
void updateBuildLog(const char *pErrorString, const size_t errorStringSize);
|
||||
bool generateElfBinary();
|
||||
void writeOutAllFiles();
|
||||
const HardwareInfo *hwInfo = nullptr;
|
||||
|
||||
std::string deviceName;
|
||||
std::string inputFile;
|
||||
std::string outputFile;
|
||||
std::string outputDirectory;
|
||||
std::string options;
|
||||
std::string internalOptions;
|
||||
std::string sourceCode;
|
||||
std::string buildLog;
|
||||
|
||||
bool useLlvmText = false;
|
||||
bool useCppFile = false;
|
||||
bool useOptionsSuffix = false;
|
||||
bool quiet = false;
|
||||
|
||||
char *elfBinary = nullptr;
|
||||
size_t elfBinarySize = 0;
|
||||
char *genBinary = nullptr;
|
||||
size_t genBinarySize = 0;
|
||||
char *llvmBinary = nullptr;
|
||||
size_t llvmBinarySize = 0;
|
||||
|
||||
std::unique_ptr<OsLibrary> igcLib = nullptr;
|
||||
CIF::RAII::UPtr_t<CIF::CIFMain> igcMain = nullptr;
|
||||
CIF::RAII::UPtr_t<IGC::IgcOclDeviceCtxTagOCL> igcDeviceCtx = nullptr;
|
||||
|
||||
std::unique_ptr<OsLibrary> fclLib = nullptr;
|
||||
CIF::RAII::UPtr_t<CIF::CIFMain> fclMain = nullptr;
|
||||
CIF::RAII::UPtr_t<IGC::FclOclDeviceCtxTagOCL> fclDeviceCtx = nullptr;
|
||||
};
|
||||
} // namespace OCLRT
|
||||
43
offline_compiler/options.cpp
Normal file
43
offline_compiler/options.cpp
Normal file
@@ -0,0 +1,43 @@
|
||||
/*
|
||||
* Copyright (c) 2017, Intel Corporation
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a
|
||||
* copy of this software and associated documentation files (the "Software"),
|
||||
* to deal in the Software without restriction, including without limitation
|
||||
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
||||
* and/or sell copies of the Software, and to permit persons to whom the
|
||||
* Software is furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included
|
||||
* in all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
||||
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
||||
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
|
||||
* OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
|
||||
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
||||
* OTHER DEALINGS IN THE SOFTWARE.
|
||||
*/
|
||||
|
||||
#include "hw_cmds.h"
|
||||
#include "runtime/helpers/options.h"
|
||||
#include "runtime/helpers/array_count.h"
|
||||
#include <cstddef>
|
||||
|
||||
namespace OCLRT {
|
||||
// AUB file folder location
|
||||
const char *folderAUB = "aub_out";
|
||||
|
||||
// Initial value for HW tag
|
||||
uint32_t initialHardwareTag = (uint32_t)-1;
|
||||
|
||||
// Number of devices in the platform
|
||||
static const HardwareInfo *DefaultPlatformDevices[] =
|
||||
{
|
||||
&DEFAULT_PLATFORM::hwInfo,
|
||||
};
|
||||
|
||||
size_t numPlatformDevices = ARRAY_COUNT(DefaultPlatformDevices);
|
||||
const HardwareInfo **platformDevices = DefaultPlatformDevices;
|
||||
} // namespace OCLRT
|
||||
Reference in New Issue
Block a user