2017-12-21 00:45:38 +01:00
|
|
|
/*
|
2020-01-14 14:32:11 +01:00
|
|
|
* Copyright (C) 2017-2020 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"
|
|
|
|
#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-09-26 15:34:32 +02:00
|
|
|
#include "shared/source/helpers/compiler_options_parser.h"
|
2020-03-06 10:11:44 +01:00
|
|
|
#include "shared/source/source_level_debugger/source_level_debugger.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"
|
2020-02-22 22:50:57 +01:00
|
|
|
#include "opencl/source/helpers/validators.h"
|
|
|
|
#include "opencl/source/platform/platform.h"
|
2019-02-27 11:39:32 +01:00
|
|
|
|
2019-12-01 16:13:21 +01:00
|
|
|
#include "compiler_options.h"
|
2017-12-21 00:45:38 +01:00
|
|
|
#include "program.h"
|
2019-02-27 11:39:32 +01:00
|
|
|
|
2017-12-21 00:45:38 +01:00
|
|
|
#include <cstring>
|
|
|
|
|
2019-03-26 11:59:46 +01:00
|
|
|
namespace NEO {
|
2017-12-21 00:45:38 +01:00
|
|
|
|
|
|
|
cl_int Program::compile(
|
2020-10-28 10:27:14 +01:00
|
|
|
const ClDeviceVector &deviceVector,
|
2017-12-21 00:45:38 +01:00
|
|
|
const char *buildOptions,
|
|
|
|
cl_uint numInputHeaders,
|
|
|
|
const cl_program *inputHeaders,
|
2020-10-27 12:43:48 +01:00
|
|
|
const char **headerIncludeNames) {
|
2017-12-21 00:45:38 +01:00
|
|
|
cl_int retVal = CL_SUCCESS;
|
|
|
|
|
2020-10-28 10:27:14 +01:00
|
|
|
auto defaultClDevice = deviceVector[0];
|
|
|
|
UNRECOVERABLE_IF(defaultClDevice == nullptr);
|
|
|
|
auto &defaultDevice = defaultClDevice->getDevice();
|
2020-10-30 11:10:00 +01:00
|
|
|
std::string internalOptions;
|
|
|
|
initInternalOptions(internalOptions);
|
|
|
|
std::unordered_map<uint32_t, bool> sourceLevelDebuggerNotified;
|
2017-12-21 00:45:38 +01:00
|
|
|
do {
|
|
|
|
if (numInputHeaders == 0) {
|
|
|
|
if ((headerIncludeNames != nullptr) || (inputHeaders != nullptr)) {
|
|
|
|
retVal = CL_INVALID_VALUE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if ((headerIncludeNames == nullptr) || (inputHeaders == nullptr)) {
|
|
|
|
retVal = CL_INVALID_VALUE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-28 10:27:14 +01:00
|
|
|
if (std::any_of(deviceVector.begin(), deviceVector.end(), [&](auto device) { return CL_BUILD_IN_PROGRESS == buildStatuses[device]; })) {
|
2017-12-21 00:45:38 +01:00
|
|
|
retVal = CL_INVALID_OPERATION;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2019-10-20 14:22:34 +02:00
|
|
|
if ((createdFrom == CreatedFrom::IL) || (this->programBinaryType == CL_PROGRAM_BINARY_TYPE_INTERMEDIATE)) {
|
|
|
|
retVal = CL_SUCCESS;
|
|
|
|
break;
|
|
|
|
}
|
2020-10-28 10:27:14 +01:00
|
|
|
for (const auto &device : deviceVector) {
|
2020-10-30 11:10:00 +01:00
|
|
|
sourceLevelDebuggerNotified[device->getRootDeviceIndex()] = false;
|
2020-10-28 10:27:14 +01:00
|
|
|
buildStatuses[device] = CL_BUILD_IN_PROGRESS;
|
|
|
|
}
|
2017-12-21 00:45:38 +01:00
|
|
|
|
|
|
|
options = (buildOptions != nullptr) ? buildOptions : "";
|
2019-07-19 11:51:00 +02:00
|
|
|
|
2020-04-01 12:21:48 +02:00
|
|
|
for (const auto &optionString : {CompilerOptions::gtpinRera, CompilerOptions::greaterThan4gbBuffersRequired}) {
|
2020-05-25 16:39:16 +02:00
|
|
|
size_t pos = options.find(optionString.data());
|
2019-07-19 11:51:00 +02:00
|
|
|
if (pos != std::string::npos) {
|
|
|
|
options.erase(pos, optionString.length());
|
2019-12-01 16:13:21 +01:00
|
|
|
CompilerOptions::concatenateAppend(internalOptions, optionString);
|
2019-07-19 11:51:00 +02:00
|
|
|
}
|
2017-12-21 00:45:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// create ELF writer to process all sources to be compiled
|
2020-01-25 19:18:48 +01: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 00:45:38 +01:00
|
|
|
|
|
|
|
for (cl_uint i = 0; i < numInputHeaders; i++) {
|
2020-01-25 19:18:48 +01:00
|
|
|
auto program = inputHeaders[i];
|
2017-12-21 00:45:38 +01:00
|
|
|
if (program == nullptr) {
|
|
|
|
retVal = CL_INVALID_PROGRAM;
|
|
|
|
break;
|
|
|
|
}
|
2020-01-25 19:18:48 +01:00
|
|
|
auto pHeaderProgObj = castToObject<Program>(program);
|
2017-12-21 00:45:38 +01:00
|
|
|
if (pHeaderProgObj == nullptr) {
|
|
|
|
retVal = CL_INVALID_PROGRAM;
|
|
|
|
break;
|
|
|
|
}
|
2020-01-25 19:18:48 +01:00
|
|
|
|
|
|
|
std::string includeHeaderSource;
|
|
|
|
retVal = pHeaderProgObj->getSource(includeHeaderSource);
|
2017-12-21 00:45:38 +01:00
|
|
|
if (retVal != CL_SUCCESS) {
|
|
|
|
break;
|
|
|
|
}
|
2018-07-27 22:59:40 +02:00
|
|
|
|
2020-01-25 19:18:48 +01:00
|
|
|
elfEncoder.appendSection(NEO::Elf::SHT_OPENCL_HEADER, ConstStringRef(headerIncludeNames[i], strlen(headerIncludeNames[i])), includeHeaderSource);
|
2017-12-21 00:45:38 +01:00
|
|
|
}
|
|
|
|
if (retVal != CL_SUCCESS) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2020-01-25 19:18:48 +01:00
|
|
|
std::vector<uint8_t> compileData = elfEncoder.encode();
|
2017-12-21 00:45:38 +01:00
|
|
|
|
2020-10-28 10:27:14 +01:00
|
|
|
CompilerInterface *pCompilerInterface = defaultDevice.getCompilerInterface();
|
2017-12-21 00:45:38 +01:00
|
|
|
if (!pCompilerInterface) {
|
|
|
|
retVal = CL_OUT_OF_HOST_MEMORY;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2019-08-29 15:10:51 +02:00
|
|
|
TranslationInput inputArgs = {IGC::CodeType::elf, IGC::CodeType::undefined};
|
2017-12-21 00:45:38 +01:00
|
|
|
|
|
|
|
// set parameters for compilation
|
2020-09-26 15:34:32 +02:00
|
|
|
if (requiresOpenClCFeatures(options)) {
|
2020-10-28 10:27:14 +01:00
|
|
|
CompilerOptions::concatenateAppend(internalOptions, defaultClDevice->peekCompilerExtensionsWithFeatures());
|
|
|
|
CompilerOptions::concatenateAppend(internalOptions, defaultClDevice->peekCompilerFeatures());
|
2020-09-26 15:34:32 +02:00
|
|
|
} else {
|
2020-10-28 10:27:14 +01:00
|
|
|
CompilerOptions::concatenateAppend(internalOptions, defaultClDevice->peekCompilerExtensions());
|
2020-09-26 15:34:32 +02:00
|
|
|
}
|
2018-03-09 14:40:31 +01:00
|
|
|
|
|
|
|
if (isKernelDebugEnabled()) {
|
2020-10-30 11:10:00 +01:00
|
|
|
for (const auto &device : deviceVector) {
|
|
|
|
if (sourceLevelDebuggerNotified[device->getRootDeviceIndex()]) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
appendKernelDebugOptions(*device, internalOptions);
|
|
|
|
std::string filename;
|
|
|
|
notifyDebuggerWithSourceCode(*device, filename);
|
|
|
|
if (!filename.empty()) {
|
|
|
|
options = std::string("-s ") + filename + " " + options;
|
|
|
|
}
|
|
|
|
|
|
|
|
sourceLevelDebuggerNotified[device->getRootDeviceIndex()] = true;
|
2018-05-02 14:27:55 +02:00
|
|
|
}
|
2018-03-09 14:40:31 +01:00
|
|
|
}
|
|
|
|
|
2020-01-25 19:18:48 +01:00
|
|
|
inputArgs.src = ArrayRef<const char>(reinterpret_cast<const char *>(compileData.data()), compileData.size());
|
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());
|
2017-12-21 00:45:38 +01:00
|
|
|
|
2019-08-29 15:10:51 +02:00
|
|
|
TranslationOutput compilerOuput;
|
2020-10-28 10:27:14 +01:00
|
|
|
auto compilerErr = pCompilerInterface->compile(defaultDevice, inputArgs, compilerOuput);
|
|
|
|
for (const auto &device : deviceVector) {
|
|
|
|
this->updateBuildLog(device->getRootDeviceIndex(), compilerOuput.frontendCompilerLog.c_str(), compilerOuput.frontendCompilerLog.size());
|
|
|
|
this->updateBuildLog(device->getRootDeviceIndex(), compilerOuput.backendCompilerLog.c_str(), compilerOuput.backendCompilerLog.size());
|
|
|
|
}
|
2019-08-29 15:10:51 +02:00
|
|
|
retVal = asClError(compilerErr);
|
2017-12-21 00:45:38 +01:00
|
|
|
if (retVal != CL_SUCCESS) {
|
|
|
|
break;
|
|
|
|
}
|
2019-08-29 15:10:51 +02: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 00:45:38 +01:00
|
|
|
updateNonUniformFlag();
|
|
|
|
} while (false);
|
|
|
|
|
|
|
|
if (retVal != CL_SUCCESS) {
|
2020-10-28 10:27:14 +01:00
|
|
|
for (const auto &device : deviceVector) {
|
|
|
|
buildStatuses[device] = CL_BUILD_ERROR;
|
|
|
|
}
|
2017-12-21 00:45:38 +01:00
|
|
|
programBinaryType = CL_PROGRAM_BINARY_TYPE_NONE;
|
|
|
|
} else {
|
2020-10-28 10:27:14 +01:00
|
|
|
for (const auto &device : deviceVector) {
|
|
|
|
buildStatuses[device] = CL_BUILD_SUCCESS;
|
|
|
|
}
|
2017-12-21 00:45:38 +01:00
|
|
|
programBinaryType = CL_PROGRAM_BINARY_TYPE_COMPILED_OBJECT;
|
|
|
|
}
|
|
|
|
|
|
|
|
return retVal;
|
|
|
|
}
|
2019-03-26 11:59:46 +01:00
|
|
|
} // namespace NEO
|