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
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2020-02-24 05:44:01 +08:00
|
|
|
#include "shared/source/compiler_interface/compiler_interface.h"
|
|
|
|
#include "shared/source/device/device.h"
|
|
|
|
#include "shared/source/device_binary_format/elf/elf.h"
|
|
|
|
#include "shared/source/device_binary_format/elf/elf_encoder.h"
|
|
|
|
#include "shared/source/device_binary_format/elf/ocl_elf.h"
|
|
|
|
#include "shared/source/execution_environment/execution_environment.h"
|
2020-03-06 17:11:44 +08:00
|
|
|
#include "shared/source/source_level_debugger/source_level_debugger.h"
|
2020-02-24 17:22:30 +08:00
|
|
|
|
2020-03-20 18:15:25 +08:00
|
|
|
#include "opencl/source/cl_device/cl_device.h"
|
2020-02-23 05:50:57 +08:00
|
|
|
#include "opencl/source/helpers/validators.h"
|
|
|
|
#include "opencl/source/platform/platform.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 "program.h"
|
2019-02-27 18:39:32 +08:00
|
|
|
|
2017-12-21 07:45:38 +08:00
|
|
|
#include <cstring>
|
|
|
|
|
2019-03-26 18:59:46 +08:00
|
|
|
namespace NEO {
|
2017-12-21 07:45:38 +08:00
|
|
|
|
|
|
|
cl_int Program::compile(
|
|
|
|
cl_uint numDevices,
|
|
|
|
const cl_device_id *deviceList,
|
|
|
|
const char *buildOptions,
|
|
|
|
cl_uint numInputHeaders,
|
|
|
|
const cl_program *inputHeaders,
|
|
|
|
const char **headerIncludeNames,
|
|
|
|
void(CL_CALLBACK *funcNotify)(cl_program program, void *userData),
|
|
|
|
void *userData) {
|
|
|
|
cl_int retVal = CL_SUCCESS;
|
|
|
|
|
|
|
|
do {
|
|
|
|
if (((deviceList == nullptr) && (numDevices != 0)) ||
|
|
|
|
((deviceList != nullptr) && (numDevices == 0))) {
|
|
|
|
retVal = CL_INVALID_VALUE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (numInputHeaders == 0) {
|
|
|
|
if ((headerIncludeNames != nullptr) || (inputHeaders != nullptr)) {
|
|
|
|
retVal = CL_INVALID_VALUE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if ((headerIncludeNames == nullptr) || (inputHeaders == nullptr)) {
|
|
|
|
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 != nullptr) && validateObject(*deviceList) != CL_SUCCESS) {
|
|
|
|
retVal = CL_INVALID_DEVICE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (buildStatus == CL_BUILD_IN_PROGRESS) {
|
|
|
|
retVal = CL_INVALID_OPERATION;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2019-10-20 20:22:34 +08:00
|
|
|
if ((createdFrom == CreatedFrom::IL) || (this->programBinaryType == CL_PROGRAM_BINARY_TYPE_INTERMEDIATE)) {
|
|
|
|
retVal = CL_SUCCESS;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2017-12-21 07:45:38 +08:00
|
|
|
buildStatus = CL_BUILD_IN_PROGRESS;
|
|
|
|
|
|
|
|
options = (buildOptions != nullptr) ? buildOptions : "";
|
2019-07-19 17:51:00 +08:00
|
|
|
|
2020-04-01 18:21:48 +08:00
|
|
|
for (const auto &optionString : {CompilerOptions::gtpinRera, CompilerOptions::greaterThan4gbBuffersRequired}) {
|
2020-05-25 22:39:16 +08:00
|
|
|
size_t pos = options.find(optionString.data());
|
2019-07-19 17:51:00 +08:00
|
|
|
if (pos != std::string::npos) {
|
|
|
|
options.erase(pos, optionString.length());
|
2019-12-01 23:13:21 +08:00
|
|
|
CompilerOptions::concatenateAppend(internalOptions, optionString);
|
2019-07-19 17:51:00 +08:00
|
|
|
}
|
2017-12-21 07:45:38 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// create ELF writer to process all sources to be compiled
|
2020-01-26 02:18:48 +08:00
|
|
|
NEO::Elf::ElfEncoder<> elfEncoder(true, true, 1U);
|
|
|
|
elfEncoder.getElfFileHeader().type = NEO::Elf::ET_OPENCL_SOURCE;
|
|
|
|
elfEncoder.appendSection(NEO::Elf::SHT_OPENCL_SOURCE, "CLMain", sourceCode);
|
2017-12-21 07:45:38 +08:00
|
|
|
|
|
|
|
for (cl_uint i = 0; i < numInputHeaders; i++) {
|
2020-01-26 02:18:48 +08:00
|
|
|
auto program = inputHeaders[i];
|
2017-12-21 07:45:38 +08:00
|
|
|
if (program == nullptr) {
|
|
|
|
retVal = CL_INVALID_PROGRAM;
|
|
|
|
break;
|
|
|
|
}
|
2020-01-26 02:18:48 +08:00
|
|
|
auto pHeaderProgObj = castToObject<Program>(program);
|
2017-12-21 07:45:38 +08:00
|
|
|
if (pHeaderProgObj == nullptr) {
|
|
|
|
retVal = CL_INVALID_PROGRAM;
|
|
|
|
break;
|
|
|
|
}
|
2020-01-26 02:18:48 +08:00
|
|
|
|
|
|
|
std::string includeHeaderSource;
|
|
|
|
retVal = pHeaderProgObj->getSource(includeHeaderSource);
|
2017-12-21 07:45:38 +08:00
|
|
|
if (retVal != CL_SUCCESS) {
|
|
|
|
break;
|
|
|
|
}
|
2018-07-28 04:59:40 +08:00
|
|
|
|
2020-01-26 02:18:48 +08:00
|
|
|
elfEncoder.appendSection(NEO::Elf::SHT_OPENCL_HEADER, ConstStringRef(headerIncludeNames[i], strlen(headerIncludeNames[i])), includeHeaderSource);
|
2017-12-21 07:45:38 +08:00
|
|
|
}
|
|
|
|
if (retVal != CL_SUCCESS) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2020-01-26 02:18:48 +08:00
|
|
|
std::vector<uint8_t> compileData = elfEncoder.encode();
|
2017-12-21 07:45:38 +08:00
|
|
|
|
2020-02-27 22:32:57 +08:00
|
|
|
CompilerInterface *pCompilerInterface = pDevice->getCompilerInterface();
|
2017-12-21 07:45:38 +08:00
|
|
|
if (!pCompilerInterface) {
|
|
|
|
retVal = CL_OUT_OF_HOST_MEMORY;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2019-08-29 21:10:51 +08:00
|
|
|
TranslationInput inputArgs = {IGC::CodeType::elf, IGC::CodeType::undefined};
|
2017-12-21 07:45:38 +08:00
|
|
|
|
|
|
|
// set parameters for compilation
|
2020-02-20 15:12:44 +08:00
|
|
|
auto clDevice = this->pDevice->getSpecializedDevice<ClDevice>();
|
|
|
|
UNRECOVERABLE_IF(clDevice == nullptr);
|
|
|
|
auto compilerExtensionsOptions = clDevice->peekCompilerExtensions();
|
2020-01-30 17:58:03 +08:00
|
|
|
CompilerOptions::concatenateAppend(internalOptions, compilerExtensionsOptions);
|
2020-06-17 02:54:15 +08:00
|
|
|
auto compilerFeaturesOptions = clDevice->peekCompilerFeatures();
|
|
|
|
CompilerOptions::concatenateAppend(internalOptions, compilerFeaturesOptions);
|
2018-03-09 21:40:31 +08:00
|
|
|
|
|
|
|
if (isKernelDebugEnabled()) {
|
2018-12-05 17:58:08 +08:00
|
|
|
std::string filename;
|
|
|
|
appendKernelDebugOptions();
|
|
|
|
notifyDebuggerWithSourceCode(filename);
|
|
|
|
if (!filename.empty()) {
|
|
|
|
options = std::string("-s ") + filename + " " + options;
|
2018-05-02 20:27:55 +08:00
|
|
|
}
|
2018-03-09 21:40:31 +08:00
|
|
|
}
|
|
|
|
|
2020-01-26 02:18:48 +08:00
|
|
|
inputArgs.src = ArrayRef<const char>(reinterpret_cast<const char *>(compileData.data()), compileData.size());
|
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());
|
2017-12-21 07:45:38 +08:00
|
|
|
|
2019-08-29 21:10:51 +08:00
|
|
|
TranslationOutput compilerOuput;
|
2020-02-20 15:12:44 +08:00
|
|
|
auto compilerErr = pCompilerInterface->compile(*this->pDevice, inputArgs, compilerOuput);
|
2020-08-24 19:07:06 +08:00
|
|
|
this->updateBuildLog(this->pDevice->getRootDeviceIndex(), compilerOuput.frontendCompilerLog.c_str(), compilerOuput.frontendCompilerLog.size());
|
|
|
|
this->updateBuildLog(this->pDevice->getRootDeviceIndex(), compilerOuput.backendCompilerLog.c_str(), compilerOuput.backendCompilerLog.size());
|
2019-08-29 21:10:51 +08:00
|
|
|
retVal = asClError(compilerErr);
|
2017-12-21 07:45:38 +08:00
|
|
|
if (retVal != CL_SUCCESS) {
|
|
|
|
break;
|
|
|
|
}
|
2019-08-29 21:10:51 +08:00
|
|
|
|
|
|
|
this->irBinary = std::move(compilerOuput.intermediateRepresentation.mem);
|
|
|
|
this->irBinarySize = compilerOuput.intermediateRepresentation.size;
|
|
|
|
this->isSpirV = compilerOuput.intermediateCodeType == IGC::CodeType::spirV;
|
|
|
|
this->debugData = std::move(compilerOuput.debugData.mem);
|
|
|
|
this->debugDataSize = compilerOuput.debugData.size;
|
|
|
|
|
2017-12-21 07:45:38 +08:00
|
|
|
updateNonUniformFlag();
|
|
|
|
} 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_COMPILED_OBJECT;
|
|
|
|
}
|
|
|
|
|
|
|
|
internalOptions.clear();
|
|
|
|
|
|
|
|
if (funcNotify != nullptr) {
|
|
|
|
(*funcNotify)(this, userData);
|
|
|
|
}
|
|
|
|
|
|
|
|
return retVal;
|
|
|
|
}
|
2019-03-26 18:59:46 +08:00
|
|
|
} // namespace NEO
|