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-09-26 21:34:32 +08:00
|
|
|
#include "shared/source/helpers/compiler_options_parser.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(
|
2020-10-28 17:27:14 +08:00
|
|
|
const ClDeviceVector &deviceVector,
|
2017-12-21 07:45:38 +08:00
|
|
|
const char *buildOptions,
|
|
|
|
cl_uint numInputHeaders,
|
|
|
|
const cl_program *inputHeaders,
|
2020-10-27 19:43:48 +08:00
|
|
|
const char **headerIncludeNames) {
|
2017-12-21 07:45:38 +08:00
|
|
|
cl_int retVal = CL_SUCCESS;
|
|
|
|
|
2020-10-28 17:27:14 +08:00
|
|
|
auto defaultClDevice = deviceVector[0];
|
|
|
|
UNRECOVERABLE_IF(defaultClDevice == nullptr);
|
|
|
|
auto &defaultDevice = defaultClDevice->getDevice();
|
2020-10-30 18:10:00 +08:00
|
|
|
std::string internalOptions;
|
|
|
|
initInternalOptions(internalOptions);
|
|
|
|
std::unordered_map<uint32_t, bool> sourceLevelDebuggerNotified;
|
2017-12-21 07:45:38 +08: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-11-03 18:13:26 +08:00
|
|
|
if (std::any_of(deviceVector.begin(), deviceVector.end(), [&](auto device) { return CL_BUILD_IN_PROGRESS == deviceBuildInfos[device].buildStatus; })) {
|
2017-12-21 07:45:38 +08:00
|
|
|
retVal = CL_INVALID_OPERATION;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2020-11-04 18:08:45 +08:00
|
|
|
if ((createdFrom == CreatedFrom::IL) || std::all_of(deviceVector.begin(), deviceVector.end(), [&](auto device) { return CL_PROGRAM_BINARY_TYPE_INTERMEDIATE == deviceBuildInfos[device].programBinaryType; })) {
|
2019-10-20 20:22:34 +08:00
|
|
|
retVal = CL_SUCCESS;
|
|
|
|
break;
|
|
|
|
}
|
2020-10-28 17:27:14 +08:00
|
|
|
for (const auto &device : deviceVector) {
|
2020-10-30 18:10:00 +08:00
|
|
|
sourceLevelDebuggerNotified[device->getRootDeviceIndex()] = false;
|
2020-11-03 18:13:26 +08:00
|
|
|
deviceBuildInfos[device].buildStatus = CL_BUILD_IN_PROGRESS;
|
2020-10-28 17:27:14 +08:00
|
|
|
}
|
2017-12-21 07:45:38 +08:00
|
|
|
|
|
|
|
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-10-28 17:27:14 +08:00
|
|
|
CompilerInterface *pCompilerInterface = defaultDevice.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-09-26 21:34:32 +08:00
|
|
|
if (requiresOpenClCFeatures(options)) {
|
2020-10-28 17:27:14 +08:00
|
|
|
CompilerOptions::concatenateAppend(internalOptions, defaultClDevice->peekCompilerExtensionsWithFeatures());
|
2020-09-26 21:34:32 +08:00
|
|
|
} else {
|
2020-10-28 17:27:14 +08:00
|
|
|
CompilerOptions::concatenateAppend(internalOptions, defaultClDevice->peekCompilerExtensions());
|
2020-09-26 21:34:32 +08:00
|
|
|
}
|
2018-03-09 21:40:31 +08:00
|
|
|
|
|
|
|
if (isKernelDebugEnabled()) {
|
2020-10-30 18:10:00 +08: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 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-10-28 17:27:14 +08: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 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) {
|
2020-10-28 17:27:14 +08:00
|
|
|
for (const auto &device : deviceVector) {
|
2020-11-03 18:13:26 +08:00
|
|
|
deviceBuildInfos[device].buildStatus = CL_BUILD_ERROR;
|
2020-11-04 18:08:45 +08:00
|
|
|
deviceBuildInfos[device].programBinaryType = CL_PROGRAM_BINARY_TYPE_NONE;
|
2020-10-28 17:27:14 +08:00
|
|
|
}
|
2017-12-21 07:45:38 +08:00
|
|
|
} else {
|
2020-11-04 18:08:45 +08:00
|
|
|
setBuildStatusSuccess(deviceVector, CL_PROGRAM_BINARY_TYPE_COMPILED_OBJECT);
|
2017-12-21 07:45:38 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return retVal;
|
|
|
|
}
|
2019-03-26 18:59:46 +08:00
|
|
|
} // namespace NEO
|