2017-12-21 00:45:38 +01:00
|
|
|
/*
|
2023-01-02 11:14:39 +00:00
|
|
|
* Copyright (C) 2018-2023 Intel Corporation
|
2017-12-21 00:45:38 +01:00
|
|
|
*
|
2018-09-18 09:11:08 +02:00
|
|
|
* SPDX-License-Identifier: MIT
|
2017-12-21 00:45:38 +01:00
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
|
2020-02-23 22:44:01 +01:00
|
|
|
#include "shared/source/compiler_interface/compiler_interface.h"
|
2022-09-06 12:12:37 +00:00
|
|
|
#include "shared/source/compiler_interface/compiler_options.h"
|
2021-12-22 15:06:58 +00:00
|
|
|
#include "shared/source/compiler_interface/compiler_warnings/compiler_warnings.h"
|
2023-01-02 16:19:30 +00:00
|
|
|
#include "shared/source/compiler_interface/external_functions.h"
|
2020-02-23 22:44:01 +01:00
|
|
|
#include "shared/source/device/device.h"
|
|
|
|
|
#include "shared/source/device_binary_format/device_binary_formats.h"
|
|
|
|
|
#include "shared/source/execution_environment/execution_environment.h"
|
2022-05-13 11:49:25 +00:00
|
|
|
#include "shared/source/helpers/addressing_mode_helper.h"
|
2020-09-26 15:34:32 +02:00
|
|
|
#include "shared/source/helpers/compiler_options_parser.h"
|
2021-09-29 19:10:53 +00:00
|
|
|
#include "shared/source/program/kernel_info.h"
|
2020-03-06 10:11:44 +01:00
|
|
|
#include "shared/source/source_level_debugger/source_level_debugger.h"
|
2021-09-24 16:40:29 +00:00
|
|
|
#include "shared/source/utilities/logger.h"
|
2020-02-24 10:22:30 +01:00
|
|
|
|
2020-03-20 11:15:25 +01:00
|
|
|
#include "opencl/source/cl_device/cl_device.h"
|
2023-03-03 13:23:24 +00:00
|
|
|
#include "opencl/source/context/context.h"
|
2020-02-22 22:50:57 +01:00
|
|
|
#include "opencl/source/gtpin/gtpin_notify.h"
|
2021-10-12 18:58:51 +00:00
|
|
|
#include "opencl/source/helpers/cl_validators.h"
|
2020-02-22 22:50:57 +01:00
|
|
|
#include "opencl/source/platform/platform.h"
|
|
|
|
|
#include "opencl/source/program/program.h"
|
2019-02-27 11:39:32 +01:00
|
|
|
|
2017-12-21 00:45:38 +01:00
|
|
|
#include <cstring>
|
2019-11-27 14:56:48 +01:00
|
|
|
#include <iterator>
|
|
|
|
|
#include <sstream>
|
2017-12-21 00:45:38 +01:00
|
|
|
|
2019-03-26 11:59:46 +01:00
|
|
|
namespace NEO {
|
2017-12-21 00:45:38 +01:00
|
|
|
|
|
|
|
|
cl_int Program::build(
|
2020-10-30 11:10:00 +01:00
|
|
|
const ClDeviceVector &deviceVector,
|
2017-12-21 00:45:38 +01:00
|
|
|
const char *buildOptions,
|
|
|
|
|
bool enableCaching) {
|
|
|
|
|
cl_int retVal = CL_SUCCESS;
|
2022-01-13 10:14:33 +00:00
|
|
|
auto internalOptions = getInternalOptions();
|
2020-10-30 11:10:00 +01:00
|
|
|
auto defaultClDevice = deviceVector[0];
|
|
|
|
|
UNRECOVERABLE_IF(defaultClDevice == nullptr);
|
|
|
|
|
auto &defaultDevice = defaultClDevice->getDevice();
|
|
|
|
|
|
|
|
|
|
std::unordered_map<uint32_t, BuildPhase> phaseReached;
|
|
|
|
|
for (const auto &clDevice : deviceVector) {
|
|
|
|
|
phaseReached[clDevice->getRootDeviceIndex()] = BuildPhase::Init;
|
|
|
|
|
}
|
2017-12-21 00:45:38 +01:00
|
|
|
do {
|
|
|
|
|
// check to see if a previous build request is in progress
|
2020-11-03 11:13:26 +01:00
|
|
|
if (std::any_of(deviceVector.begin(), deviceVector.end(), [&](auto device) { return CL_BUILD_IN_PROGRESS == deviceBuildInfos[device].buildStatus; })) {
|
2017-12-21 00:45:38 +01:00
|
|
|
retVal = CL_INVALID_OPERATION;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (isCreatedFromBinary == false) {
|
2020-10-30 11:10:00 +01:00
|
|
|
for (const auto &device : deviceVector) {
|
2020-11-03 11:13:26 +01:00
|
|
|
deviceBuildInfos[device].buildStatus = CL_BUILD_IN_PROGRESS;
|
2020-10-30 11:10:00 +01:00
|
|
|
}
|
2017-12-21 00:45:38 +01:00
|
|
|
|
2022-04-07 12:29:43 +00:00
|
|
|
if (false == requiresRebuild) {
|
|
|
|
|
if (nullptr != buildOptions) {
|
|
|
|
|
options = buildOptions;
|
|
|
|
|
} else if (this->createdFrom != CreatedFrom::BINARY) {
|
|
|
|
|
options = "";
|
|
|
|
|
}
|
2020-10-04 11:43:24 +02:00
|
|
|
}
|
2021-12-22 15:06:58 +00:00
|
|
|
|
|
|
|
|
const bool shouldSuppressRebuildWarning{CompilerOptions::extract(CompilerOptions::noRecompiledFromIr, options)};
|
2020-10-30 11:10:00 +01:00
|
|
|
extractInternalOptions(options, internalOptions);
|
2022-11-10 13:30:18 +00:00
|
|
|
CompilerOptions::applyAdditionalApiOptions(options);
|
|
|
|
|
CompilerOptions::applyAdditionalInternalOptions(internalOptions);
|
2017-12-21 00:45:38 +01:00
|
|
|
|
2020-10-30 11:10:00 +01:00
|
|
|
CompilerInterface *pCompilerInterface = defaultDevice.getCompilerInterface();
|
2017-12-21 00:45:38 +01:00
|
|
|
if (!pCompilerInterface) {
|
|
|
|
|
retVal = CL_OUT_OF_HOST_MEMORY;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-27 05:13:35 +01:00
|
|
|
disableZebinIfVmeEnabled(options, internalOptions, sourceCode);
|
2022-12-22 16:09:00 +01:00
|
|
|
|
2019-10-20 14:22:34 +02:00
|
|
|
TranslationInput inputArgs = {IGC::CodeType::oclC, IGC::CodeType::oclGenBin};
|
2020-02-16 22:49:47 +01:00
|
|
|
if (createdFrom != CreatedFrom::SOURCE) {
|
2019-10-20 14:22:34 +02:00
|
|
|
inputArgs.srcType = isSpirV ? IGC::CodeType::spirV : IGC::CodeType::llvmBc;
|
|
|
|
|
inputArgs.src = ArrayRef<const char>(irBinary.get(), irBinarySize);
|
|
|
|
|
} else {
|
|
|
|
|
inputArgs.src = ArrayRef<const char>(sourceCode.c_str(), sourceCode.size());
|
2019-08-29 15:10:51 +02:00
|
|
|
}
|
2018-12-05 10:58:08 +01:00
|
|
|
|
2019-10-20 14:22:34 +02:00
|
|
|
if (inputArgs.src.size() == 0) {
|
2017-12-21 00:45:38 +01:00
|
|
|
retVal = CL_INVALID_PROGRAM;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
2018-03-15 18:13:52 +01:00
|
|
|
if (isKernelDebugEnabled()) {
|
2018-12-05 10:58:08 +01:00
|
|
|
std::string filename;
|
2020-10-30 11:10:00 +01:00
|
|
|
for (const auto &clDevice : deviceVector) {
|
|
|
|
|
if (BuildPhase::SourceCodeNotification == phaseReached[clDevice->getRootDeviceIndex()]) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
appendKernelDebugOptions(*clDevice, internalOptions);
|
|
|
|
|
notifyDebuggerWithSourceCode(*clDevice, filename);
|
2021-03-01 16:09:19 +01:00
|
|
|
prependFilePathToOptions(filename);
|
2020-10-30 11:10:00 +01:00
|
|
|
|
|
|
|
|
phaseReached[clDevice->getRootDeviceIndex()] = BuildPhase::SourceCodeNotification;
|
2018-05-02 14:27:55 +02:00
|
|
|
}
|
2018-03-15 18:13:52 +01:00
|
|
|
}
|
|
|
|
|
|
2020-12-03 18:00:10 +00:00
|
|
|
std::string extensions = requiresOpenClCFeatures(options) ? defaultClDevice->peekCompilerExtensionsWithFeatures()
|
|
|
|
|
: defaultClDevice->peekCompilerExtensions();
|
|
|
|
|
if (requiresAdditionalExtensions(options)) {
|
|
|
|
|
extensions.erase(extensions.length() - 1);
|
|
|
|
|
extensions += ",+cl_khr_3d_image_writes ";
|
2020-06-16 20:54:15 +02:00
|
|
|
}
|
2020-12-03 18:00:10 +00:00
|
|
|
CompilerOptions::concatenateAppend(internalOptions, extensions);
|
2018-05-02 14:27:55 +02:00
|
|
|
|
2021-11-22 13:05:54 +00:00
|
|
|
if (!this->getIsBuiltIn() && DebugManager.flags.InjectInternalBuildOptions.get() != "unk") {
|
|
|
|
|
NEO::CompilerOptions::concatenateAppend(internalOptions, NEO::DebugManager.flags.InjectInternalBuildOptions.get());
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-03 13:23:24 +00:00
|
|
|
if (nullptr != this->getContextPtr()) {
|
|
|
|
|
if (this->getContext().checkIfContextIsNonZebin()) {
|
2023-04-25 15:12:25 +00:00
|
|
|
const auto &rootDevice = defaultDevice.getRootDevice();
|
|
|
|
|
rootDevice->getCompilerInterface()->addOptionDisableZebin(options, internalOptions);
|
2023-03-03 13:23:24 +00:00
|
|
|
}
|
2023-01-11 16:46:43 +00:00
|
|
|
}
|
|
|
|
|
|
2019-08-29 15:10:51 +02:00
|
|
|
inputArgs.apiOptions = ArrayRef<const char>(options.c_str(), options.length());
|
|
|
|
|
inputArgs.internalOptions = ArrayRef<const char>(internalOptions.c_str(), internalOptions.length());
|
2018-08-06 04:19:32 -07:00
|
|
|
inputArgs.GTPinInput = gtpinGetIgcInit();
|
2020-03-20 12:04:47 +01:00
|
|
|
inputArgs.specializedValues = this->specConstantsValues;
|
2017-12-21 00:45:38 +01:00
|
|
|
DBG_LOG(LogApiCalls,
|
2019-08-29 15:10:51 +02:00
|
|
|
"Build Options", inputArgs.apiOptions.begin(),
|
|
|
|
|
"\nBuild Internal Options", inputArgs.internalOptions.begin());
|
|
|
|
|
inputArgs.allowCaching = enableCaching;
|
|
|
|
|
NEO::TranslationOutput compilerOuput = {};
|
2020-10-30 11:10:00 +01:00
|
|
|
|
|
|
|
|
for (const auto &clDevice : deviceVector) {
|
2022-04-07 12:29:43 +00:00
|
|
|
if (requiresRebuild && !shouldSuppressRebuildWarning) {
|
2021-12-22 15:06:58 +00:00
|
|
|
this->updateBuildLog(clDevice->getRootDeviceIndex(), CompilerWarnings::recompiledFromIr.data(), CompilerWarnings::recompiledFromIr.length());
|
|
|
|
|
}
|
2020-10-30 11:10:00 +01:00
|
|
|
auto compilerErr = pCompilerInterface->build(clDevice->getDevice(), inputArgs, compilerOuput);
|
|
|
|
|
this->updateBuildLog(clDevice->getRootDeviceIndex(), compilerOuput.frontendCompilerLog.c_str(), compilerOuput.frontendCompilerLog.size());
|
|
|
|
|
this->updateBuildLog(clDevice->getRootDeviceIndex(), compilerOuput.backendCompilerLog.c_str(), compilerOuput.backendCompilerLog.size());
|
|
|
|
|
retVal = asClError(compilerErr);
|
|
|
|
|
if (retVal != CL_SUCCESS) {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
if (inputArgs.srcType == IGC::CodeType::oclC) {
|
|
|
|
|
this->irBinary = std::move(compilerOuput.intermediateRepresentation.mem);
|
|
|
|
|
this->irBinarySize = compilerOuput.intermediateRepresentation.size;
|
|
|
|
|
this->isSpirV = compilerOuput.intermediateCodeType == IGC::CodeType::spirV;
|
|
|
|
|
}
|
2022-01-18 17:07:29 +00:00
|
|
|
this->buildInfos[clDevice->getRootDeviceIndex()].debugData = std::move(compilerOuput.debugData.mem);
|
|
|
|
|
this->buildInfos[clDevice->getRootDeviceIndex()].debugDataSize = compilerOuput.debugData.size;
|
2020-10-30 11:10:00 +01:00
|
|
|
if (BuildPhase::BinaryCreation == phaseReached[clDevice->getRootDeviceIndex()]) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
this->replaceDeviceBinary(std::move(compilerOuput.deviceBinary.mem), compilerOuput.deviceBinary.size, clDevice->getRootDeviceIndex());
|
|
|
|
|
phaseReached[clDevice->getRootDeviceIndex()] = BuildPhase::BinaryCreation;
|
|
|
|
|
}
|
2017-12-21 00:45:38 +01:00
|
|
|
if (retVal != CL_SUCCESS) {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
updateNonUniformFlag();
|
|
|
|
|
|
2022-09-16 15:01:18 +00:00
|
|
|
retVal = processGenBinaries(deviceVector, phaseReached);
|
2019-10-07 10:58:23 +02:00
|
|
|
|
2023-03-09 16:27:52 +00:00
|
|
|
auto skipLastExplicitArg = isGTPinInitialized;
|
|
|
|
|
auto containsStatefulAccess = AddressingModeHelper::containsStatefulAccess(buildInfos[clDevices[0]->getRootDeviceIndex()].kernelInfoArray, skipLastExplicitArg);
|
2022-05-13 11:49:25 +00:00
|
|
|
auto isUserKernel = !isBuiltIn;
|
|
|
|
|
|
|
|
|
|
auto failBuildProgram = (containsStatefulAccess &&
|
|
|
|
|
isUserKernel &&
|
2023-01-23 16:24:42 +00:00
|
|
|
AddressingModeHelper::failBuildProgramWithStatefulAccess(clDevices[0]->getRootDeviceEnvironment()));
|
2022-05-13 11:49:25 +00:00
|
|
|
|
|
|
|
|
if (failBuildProgram) {
|
|
|
|
|
retVal = CL_BUILD_PROGRAM_FAILURE;
|
|
|
|
|
}
|
|
|
|
|
|
2017-12-21 00:45:38 +01:00
|
|
|
if (retVal != CL_SUCCESS) {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-16 09:02:07 +00:00
|
|
|
if (isKernelDebugEnabled() || gtpinIsGTPinInitialized()) {
|
2022-05-20 10:21:08 +00:00
|
|
|
debugNotify(deviceVector, phaseReached);
|
2018-05-02 14:27:55 +02:00
|
|
|
}
|
2023-03-01 15:02:45 +00:00
|
|
|
notifyModuleCreate();
|
2017-12-21 00:45:38 +01:00
|
|
|
} while (false);
|
|
|
|
|
|
|
|
|
|
if (retVal != CL_SUCCESS) {
|
2020-10-30 11:10:00 +01:00
|
|
|
for (const auto &device : deviceVector) {
|
2020-11-03 11:13:26 +01:00
|
|
|
deviceBuildInfos[device].buildStatus = CL_BUILD_ERROR;
|
2020-11-04 11:08:45 +01:00
|
|
|
deviceBuildInfos[device].programBinaryType = CL_PROGRAM_BINARY_TYPE_NONE;
|
2020-10-30 11:10:00 +01:00
|
|
|
}
|
2017-12-21 00:45:38 +01:00
|
|
|
} else {
|
2020-11-04 11:08:45 +01:00
|
|
|
setBuildStatusSuccess(deviceVector, CL_PROGRAM_BINARY_TYPE_EXECUTABLE);
|
2017-12-21 00:45:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return retVal;
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-30 11:10:00 +01:00
|
|
|
bool Program::appendKernelDebugOptions(ClDevice &clDevice, std::string &internalOptions) {
|
2019-12-01 16:13:21 +01:00
|
|
|
CompilerOptions::concatenateAppend(internalOptions, CompilerOptions::debugKernelEnable);
|
|
|
|
|
CompilerOptions::concatenateAppend(options, CompilerOptions::generateDebugInfo);
|
2020-02-20 08:12:44 +01:00
|
|
|
|
2020-10-30 11:10:00 +01:00
|
|
|
auto debugger = clDevice.getSourceLevelDebugger();
|
2021-04-19 11:50:53 +00:00
|
|
|
if (debugger && (NEO::SourceLevelDebugger::shouldAppendOptDisable(*debugger))) {
|
2019-12-01 16:13:21 +01:00
|
|
|
CompilerOptions::concatenateAppend(options, CompilerOptions::optDisable);
|
2018-12-05 10:58:08 +01:00
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-30 11:10:00 +01:00
|
|
|
void Program::notifyDebuggerWithSourceCode(ClDevice &clDevice, std::string &filename) {
|
|
|
|
|
if (clDevice.getSourceLevelDebugger()) {
|
|
|
|
|
clDevice.getSourceLevelDebugger()->notifySourceCode(sourceCode.c_str(), sourceCode.size(), filename);
|
2018-12-05 10:58:08 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-30 11:10:00 +01:00
|
|
|
cl_int Program::build(const ClDeviceVector &deviceVector, const char *buildOptions, bool enableCaching,
|
2017-12-21 00:45:38 +01:00
|
|
|
std::unordered_map<std::string, BuiltinDispatchInfoBuilder *> &builtinsMap) {
|
2020-10-30 11:10:00 +01:00
|
|
|
auto ret = this->build(deviceVector, buildOptions, enableCaching);
|
2017-12-21 00:45:38 +01:00
|
|
|
if (ret != CL_SUCCESS) {
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-30 17:38:28 +00:00
|
|
|
for (auto &ki : buildInfos[deviceVector[0]->getRootDeviceIndex()].kernelInfoArray) {
|
2020-10-19 20:36:05 +02:00
|
|
|
auto fit = builtinsMap.find(ki->kernelDescriptor.kernelMetadata.kernelName);
|
2017-12-21 00:45:38 +01:00
|
|
|
if (fit == builtinsMap.end()) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
ki->builtinDispatchBuilder = fit->second;
|
|
|
|
|
}
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-30 11:10:00 +01:00
|
|
|
void Program::extractInternalOptions(const std::string &options, std::string &internalOptions) {
|
2019-12-01 16:13:21 +01:00
|
|
|
auto tokenized = CompilerOptions::tokenize(options);
|
2018-10-02 15:08:23 +02:00
|
|
|
for (auto &optionString : internalOptionsToExtract) {
|
2019-12-01 16:13:21 +01:00
|
|
|
auto element = std::find(tokenized.begin(), tokenized.end(), optionString);
|
|
|
|
|
if (element == tokenized.end()) {
|
2019-11-27 14:56:48 +01:00
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (isFlagOption(optionString)) {
|
2019-12-01 16:13:21 +01:00
|
|
|
CompilerOptions::concatenateAppend(internalOptions, optionString);
|
|
|
|
|
} else if ((element + 1 != tokenized.end()) &&
|
2019-11-27 14:56:48 +01:00
|
|
|
isOptionValueValid(optionString, *(element + 1))) {
|
2019-12-01 16:13:21 +01:00
|
|
|
CompilerOptions::concatenateAppend(internalOptions, optionString);
|
|
|
|
|
CompilerOptions::concatenateAppend(internalOptions, *(element + 1));
|
2018-10-02 15:08:23 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-11-27 14:56:48 +01:00
|
|
|
|
2022-05-20 10:21:08 +00:00
|
|
|
void Program::debugNotify(const ClDeviceVector &deviceVector, std::unordered_map<uint32_t, BuildPhase> &phasesReached) {
|
|
|
|
|
for (auto &clDevice : deviceVector) {
|
|
|
|
|
auto rootDeviceIndex = clDevice->getRootDeviceIndex();
|
|
|
|
|
if (BuildPhase::DebugDataNotification == phasesReached[rootDeviceIndex]) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
notifyDebuggerWithDebugData(clDevice);
|
|
|
|
|
phasesReached[rootDeviceIndex] = BuildPhase::DebugDataNotification;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-03-26 11:59:46 +01:00
|
|
|
} // namespace NEO
|