2017-12-21 07:45:38 +08:00
|
|
|
/*
|
2020-01-14 21:32:11 +08:00
|
|
|
* Copyright (C) 2017-2020 Intel Corporation
|
2017-12-21 07:45:38 +08:00
|
|
|
*
|
2018-09-18 15:11:08 +08:00
|
|
|
* SPDX-License-Identifier: MIT
|
2017-12-21 07:45:38 +08:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2019-10-22 18:09:08 +08:00
|
|
|
#include "core/compiler_interface/compiler_interface.h"
|
2020-02-07 01:57:00 +08:00
|
|
|
#include "core/device/device.h"
|
2020-02-05 15:30:17 +08:00
|
|
|
#include "core/execution_environment/execution_environment.h"
|
2019-10-07 16:58:23 +08:00
|
|
|
#include "core/utilities/time_measure_wrapper.h"
|
2020-01-27 20:59:19 +08:00
|
|
|
#include "runtime/device/cl_device.h"
|
2019-02-27 18:39:32 +08:00
|
|
|
#include "runtime/gtpin/gtpin_notify.h"
|
|
|
|
#include "runtime/helpers/validators.h"
|
2017-12-21 07:45:38 +08:00
|
|
|
#include "runtime/platform/platform.h"
|
2019-10-28 02:48:26 +08:00
|
|
|
#include "runtime/program/kernel_info.h"
|
2019-10-22 18:09:08 +08:00
|
|
|
#include "runtime/program/program.h"
|
2018-05-02 20:27:55 +08:00
|
|
|
#include "runtime/source_level_debugger/source_level_debugger.h"
|
2019-12-10 23:26:35 +08:00
|
|
|
#include "runtime/utilities/logger.h"
|
2019-02-27 18:39:32 +08:00
|
|
|
|
2019-12-01 23:13:21 +08:00
|
|
|
#include "compiler_options.h"
|
|
|
|
|
2017-12-21 07:45:38 +08:00
|
|
|
#include <cstring>
|
2019-11-27 21:56:48 +08:00
|
|
|
#include <iterator>
|
|
|
|
#include <sstream>
|
2017-12-21 07:45:38 +08:00
|
|
|
|
2019-03-26 18:59:46 +08:00
|
|
|
namespace NEO {
|
2017-12-21 07:45:38 +08:00
|
|
|
|
|
|
|
cl_int Program::build(
|
|
|
|
cl_uint numDevices,
|
|
|
|
const cl_device_id *deviceList,
|
|
|
|
const char *buildOptions,
|
|
|
|
void(CL_CALLBACK *funcNotify)(cl_program program, void *userData),
|
|
|
|
void *userData,
|
|
|
|
bool enableCaching) {
|
|
|
|
cl_int retVal = CL_SUCCESS;
|
|
|
|
|
|
|
|
do {
|
|
|
|
if (((deviceList == nullptr) && (numDevices != 0)) ||
|
|
|
|
((deviceList != nullptr) && (numDevices == 0))) {
|
|
|
|
retVal = CL_INVALID_VALUE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((funcNotify == nullptr) &&
|
|
|
|
(userData != nullptr)) {
|
|
|
|
retVal = CL_INVALID_VALUE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// if a device_list is specified, make sure it points to our device
|
|
|
|
// NOTE: a null device_list is ok - it means "all devices"
|
|
|
|
if (deviceList && validateObject(*deviceList) != CL_SUCCESS) {
|
|
|
|
retVal = CL_INVALID_DEVICE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// check to see if a previous build request is in progress
|
|
|
|
if (buildStatus == CL_BUILD_IN_PROGRESS) {
|
|
|
|
retVal = CL_INVALID_OPERATION;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isCreatedFromBinary == false) {
|
|
|
|
buildStatus = CL_BUILD_IN_PROGRESS;
|
|
|
|
|
|
|
|
options = (buildOptions) ? buildOptions : "";
|
2018-10-02 21:08:23 +08:00
|
|
|
extractInternalOptions(options);
|
2019-01-24 17:00:27 +08:00
|
|
|
applyAdditionalOptions();
|
2017-12-21 07:45:38 +08:00
|
|
|
|
2018-08-01 15:06:46 +08:00
|
|
|
CompilerInterface *pCompilerInterface = this->executionEnvironment.getCompilerInterface();
|
2017-12-21 07:45:38 +08:00
|
|
|
if (!pCompilerInterface) {
|
|
|
|
retVal = CL_OUT_OF_HOST_MEMORY;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2019-10-20 20:22:34 +08:00
|
|
|
TranslationInput inputArgs = {IGC::CodeType::oclC, IGC::CodeType::oclGenBin};
|
2019-08-29 21:10:51 +08:00
|
|
|
if ((createdFrom == CreatedFrom::IL) || (this->programBinaryType == CL_PROGRAM_BINARY_TYPE_INTERMEDIATE)) {
|
2019-10-20 20:22:34 +08: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 21:10:51 +08:00
|
|
|
}
|
2018-12-05 17:58:08 +08:00
|
|
|
|
2019-10-20 20:22:34 +08:00
|
|
|
if (inputArgs.src.size() == 0) {
|
2017-12-21 07:45:38 +08:00
|
|
|
retVal = CL_INVALID_PROGRAM;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2018-03-16 01:13:52 +08:00
|
|
|
if (isKernelDebugEnabled()) {
|
2018-12-05 17:58:08 +08:00
|
|
|
std::string filename;
|
|
|
|
appendKernelDebugOptions();
|
|
|
|
notifyDebuggerWithSourceCode(filename);
|
|
|
|
if (!filename.empty()) {
|
|
|
|
// Add "-s" flag first so it will be ignored by clang in case the options already have this flag set.
|
|
|
|
options = std::string("-s ") + filename + " " + options;
|
2018-05-02 20:27:55 +08:00
|
|
|
}
|
2018-03-16 01:13:52 +08:00
|
|
|
}
|
|
|
|
|
2020-01-30 17:58:03 +08:00
|
|
|
auto compilerExtensionsOptions = this->pDevice->peekCompilerExtensions();
|
2018-07-31 17:01:13 +08:00
|
|
|
if (internalOptions.find(compilerExtensionsOptions) == std::string::npos) {
|
2019-12-01 23:13:21 +08:00
|
|
|
CompilerOptions::concatenateAppend(internalOptions, compilerExtensionsOptions);
|
2018-07-31 17:01:13 +08:00
|
|
|
}
|
2018-05-02 20:27:55 +08:00
|
|
|
|
2019-08-29 21:10:51 +08:00
|
|
|
inputArgs.apiOptions = ArrayRef<const char>(options.c_str(), options.length());
|
|
|
|
inputArgs.internalOptions = ArrayRef<const char>(internalOptions.c_str(), internalOptions.length());
|
2018-08-06 19:19:32 +08:00
|
|
|
inputArgs.GTPinInput = gtpinGetIgcInit();
|
2019-08-29 21:10:51 +08:00
|
|
|
inputArgs.specConstants.idsBuffer = this->specConstantsIds.get();
|
|
|
|
inputArgs.specConstants.sizesBuffer = this->specConstantsSizes.get();
|
|
|
|
inputArgs.specConstants.valuesBuffer = this->specConstantsValues.get();
|
2017-12-21 07:45:38 +08:00
|
|
|
DBG_LOG(LogApiCalls,
|
2019-08-29 21:10:51 +08:00
|
|
|
"Build Options", inputArgs.apiOptions.begin(),
|
|
|
|
"\nBuild Internal Options", inputArgs.internalOptions.begin());
|
|
|
|
inputArgs.allowCaching = enableCaching;
|
|
|
|
NEO::TranslationOutput compilerOuput = {};
|
2020-01-14 21:32:11 +08:00
|
|
|
auto compilerErr = pCompilerInterface->build(this->pDevice->getDevice(), inputArgs, compilerOuput);
|
2019-08-29 21:10:51 +08:00
|
|
|
this->updateBuildLog(this->pDevice, compilerOuput.frontendCompilerLog.c_str(), compilerOuput.frontendCompilerLog.size());
|
|
|
|
this->updateBuildLog(this->pDevice, compilerOuput.backendCompilerLog.c_str(), compilerOuput.backendCompilerLog.size());
|
|
|
|
retVal = asClError(compilerErr);
|
2017-12-21 07:45:38 +08:00
|
|
|
if (retVal != CL_SUCCESS) {
|
|
|
|
break;
|
|
|
|
}
|
2019-10-20 20:22:34 +08:00
|
|
|
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;
|
|
|
|
}
|
2019-08-29 21:10:51 +08:00
|
|
|
this->genBinary = std::move(compilerOuput.deviceBinary.mem);
|
|
|
|
this->genBinarySize = compilerOuput.deviceBinary.size;
|
|
|
|
this->debugData = std::move(compilerOuput.debugData.mem);
|
|
|
|
this->debugDataSize = compilerOuput.debugData.size;
|
2017-12-21 07:45:38 +08:00
|
|
|
}
|
|
|
|
updateNonUniformFlag();
|
|
|
|
|
2019-10-07 16:58:23 +08:00
|
|
|
if (DebugManager.flags.PrintProgramBinaryProcessingTime.get()) {
|
|
|
|
retVal = TimeMeasureWrapper::functionExecution(*this, &Program::processGenBinary);
|
|
|
|
} else {
|
|
|
|
retVal = processGenBinary();
|
|
|
|
}
|
|
|
|
|
2017-12-21 07:45:38 +08:00
|
|
|
if (retVal != CL_SUCCESS) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2018-05-02 20:27:55 +08:00
|
|
|
if (isKernelDebugEnabled()) {
|
|
|
|
processDebugData();
|
|
|
|
if (pDevice->getSourceLevelDebugger()) {
|
2019-07-29 19:35:55 +08:00
|
|
|
for (auto kernelInfo : kernelInfoArray) {
|
|
|
|
pDevice->getSourceLevelDebugger()->notifyKernelDebugData(kernelInfo);
|
2018-05-02 20:27:55 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-21 07:45:38 +08:00
|
|
|
separateBlockKernels();
|
|
|
|
} while (false);
|
|
|
|
|
|
|
|
if (retVal != CL_SUCCESS) {
|
|
|
|
buildStatus = CL_BUILD_ERROR;
|
|
|
|
programBinaryType = CL_PROGRAM_BINARY_TYPE_NONE;
|
|
|
|
} else {
|
|
|
|
buildStatus = CL_BUILD_SUCCESS;
|
|
|
|
programBinaryType = CL_PROGRAM_BINARY_TYPE_EXECUTABLE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (funcNotify != nullptr) {
|
|
|
|
(*funcNotify)(this, userData);
|
|
|
|
}
|
|
|
|
|
|
|
|
return retVal;
|
|
|
|
}
|
|
|
|
|
2018-12-05 17:58:08 +08:00
|
|
|
bool Program::appendKernelDebugOptions() {
|
2019-12-01 23:13:21 +08:00
|
|
|
CompilerOptions::concatenateAppend(internalOptions, CompilerOptions::debugKernelEnable);
|
|
|
|
CompilerOptions::concatenateAppend(options, CompilerOptions::generateDebugInfo);
|
|
|
|
auto sourceLevelDebugger = pDevice->getSourceLevelDebugger();
|
|
|
|
if (sourceLevelDebugger && sourceLevelDebugger->isOptimizationDisabled()) {
|
|
|
|
CompilerOptions::concatenateAppend(options, CompilerOptions::optDisable);
|
2018-12-05 17:58:08 +08:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Program::notifyDebuggerWithSourceCode(std::string &filename) {
|
|
|
|
if (pDevice->getSourceLevelDebugger()) {
|
|
|
|
pDevice->getSourceLevelDebugger()->notifySourceCode(sourceCode.c_str(), sourceCode.size(), filename);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-14 21:32:11 +08:00
|
|
|
cl_int Program::build(const Device *pDevice, const char *buildOptions, bool enableCaching,
|
2017-12-21 07:45:38 +08:00
|
|
|
std::unordered_map<std::string, BuiltinDispatchInfoBuilder *> &builtinsMap) {
|
2020-01-31 22:00:25 +08:00
|
|
|
cl_device_id deviceId = pDevice->getSpecializedDevice<ClDevice>();
|
2020-01-14 21:32:11 +08:00
|
|
|
auto ret = this->build(1, &deviceId, buildOptions, nullptr, nullptr, enableCaching);
|
2017-12-21 07:45:38 +08:00
|
|
|
if (ret != CL_SUCCESS) {
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (auto &ki : this->kernelInfoArray) {
|
|
|
|
auto fit = builtinsMap.find(ki->name);
|
|
|
|
if (fit == builtinsMap.end()) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
ki->builtinDispatchBuilder = fit->second;
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2019-11-27 21:56:48 +08:00
|
|
|
void Program::extractInternalOptions(const std::string &options) {
|
2019-12-01 23:13:21 +08:00
|
|
|
auto tokenized = CompilerOptions::tokenize(options);
|
2018-10-02 21:08:23 +08:00
|
|
|
for (auto &optionString : internalOptionsToExtract) {
|
2019-12-01 23:13:21 +08:00
|
|
|
auto element = std::find(tokenized.begin(), tokenized.end(), optionString);
|
|
|
|
if (element == tokenized.end()) {
|
2019-11-27 21:56:48 +08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isFlagOption(optionString)) {
|
2019-12-01 23:13:21 +08:00
|
|
|
CompilerOptions::concatenateAppend(internalOptions, optionString);
|
|
|
|
} else if ((element + 1 != tokenized.end()) &&
|
2019-11-27 21:56:48 +08:00
|
|
|
isOptionValueValid(optionString, *(element + 1))) {
|
2019-12-01 23:13:21 +08:00
|
|
|
CompilerOptions::concatenateAppend(internalOptions, optionString);
|
|
|
|
CompilerOptions::concatenateAppend(internalOptions, *(element + 1));
|
2018-10-02 21:08:23 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-11-27 21:56:48 +08:00
|
|
|
|
2019-03-26 18:59:46 +08:00
|
|
|
} // namespace NEO
|