2017-12-21 07:45:38 +08:00
|
|
|
/*
|
2018-04-18 21:48:03 +08:00
|
|
|
* Copyright (c) 2017 - 2018, Intel Corporation
|
2017-12-21 07:45:38 +08:00
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a
|
|
|
|
* copy of this software and associated documentation files (the "Software"),
|
|
|
|
* to deal in the Software without restriction, including without limitation
|
|
|
|
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
|
|
|
* and/or sell copies of the Software, and to permit persons to whom the
|
|
|
|
* Software is furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included
|
|
|
|
* in all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
|
|
|
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
|
|
|
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
|
|
|
|
* OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
|
|
|
|
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
|
|
|
* OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "elf/writer.h"
|
|
|
|
#include "runtime/compiler_interface/compiler_interface.h"
|
2018-03-09 21:40:31 +08:00
|
|
|
#include "runtime/compiler_interface/compiler_options.h"
|
2017-12-21 07:45:38 +08:00
|
|
|
#include "runtime/platform/platform.h"
|
2018-05-02 20:27:55 +08:00
|
|
|
#include "runtime/source_level_debugger/source_level_debugger.h"
|
2017-12-21 07:45:38 +08:00
|
|
|
#include "runtime/helpers/validators.h"
|
|
|
|
#include "program.h"
|
|
|
|
#include <cstring>
|
|
|
|
|
|
|
|
namespace OCLRT {
|
|
|
|
|
|
|
|
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;
|
|
|
|
cl_program program;
|
|
|
|
Program *pHeaderProgObj;
|
|
|
|
size_t compileDataSize;
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
buildStatus = CL_BUILD_IN_PROGRESS;
|
|
|
|
|
|
|
|
options = (buildOptions != nullptr) ? buildOptions : "";
|
|
|
|
std::string reraStr = "-cl-intel-gtpin-rera";
|
|
|
|
size_t pos = options.find(reraStr);
|
|
|
|
if (pos != std::string::npos) {
|
|
|
|
// compile option "-cl-intel-gtpin-rera" is present, move it to internalOptions
|
|
|
|
size_t reraLen = reraStr.length();
|
|
|
|
options.erase(pos, reraLen);
|
|
|
|
internalOptions.append(reraStr);
|
|
|
|
internalOptions.append(" ");
|
|
|
|
}
|
|
|
|
|
|
|
|
// create ELF writer to process all sources to be compiled
|
2018-07-28 04:59:40 +08:00
|
|
|
CLElfLib::CElfWriter elfWriter(CLElfLib::E_EH_TYPE::EH_TYPE_OPENCL_SOURCE, CLElfLib::E_EH_MACHINE::EH_MACHINE_NONE, 0);
|
2017-12-21 07:45:38 +08:00
|
|
|
|
2018-07-28 04:59:40 +08:00
|
|
|
CLElfLib::SSectionNode sectionNode(CLElfLib::E_SH_TYPE::SH_TYPE_OPENCL_SOURCE, CLElfLib::E_SH_FLAG::SH_FLAG_NONE, "CLMain", sourceCode, static_cast<uint32_t>(sourceCode.size() + 1u));
|
2017-12-21 07:45:38 +08:00
|
|
|
|
|
|
|
// add main program's source
|
2018-07-28 04:59:40 +08:00
|
|
|
elfWriter.addSection(sectionNode);
|
2017-12-21 07:45:38 +08:00
|
|
|
|
|
|
|
for (cl_uint i = 0; i < numInputHeaders; i++) {
|
|
|
|
program = inputHeaders[i];
|
|
|
|
if (program == nullptr) {
|
|
|
|
retVal = CL_INVALID_PROGRAM;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
pHeaderProgObj = castToObject<Program>(program);
|
|
|
|
if (pHeaderProgObj == nullptr) {
|
|
|
|
retVal = CL_INVALID_PROGRAM;
|
|
|
|
break;
|
|
|
|
}
|
2018-07-28 04:59:40 +08:00
|
|
|
sectionNode.name = headerIncludeNames[i];
|
|
|
|
sectionNode.type = CLElfLib::E_SH_TYPE::SH_TYPE_OPENCL_HEADER;
|
|
|
|
sectionNode.flag = CLElfLib::E_SH_FLAG::SH_FLAG_NONE;
|
2017-12-21 07:45:38 +08:00
|
|
|
// collect required data from the header
|
2018-07-28 04:59:40 +08:00
|
|
|
retVal = pHeaderProgObj->getSource(sectionNode.data);
|
2017-12-21 07:45:38 +08:00
|
|
|
if (retVal != CL_SUCCESS) {
|
|
|
|
break;
|
|
|
|
}
|
2018-07-28 04:59:40 +08:00
|
|
|
|
|
|
|
sectionNode.dataSize = static_cast<uint32_t>(sectionNode.data.size());
|
|
|
|
elfWriter.addSection(sectionNode);
|
2017-12-21 07:45:38 +08:00
|
|
|
}
|
|
|
|
if (retVal != CL_SUCCESS) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2018-07-28 04:59:40 +08:00
|
|
|
compileDataSize = elfWriter.getTotalBinarySize();
|
|
|
|
std::vector<char> compileData(compileDataSize);
|
|
|
|
elfWriter.resolveBinary(compileData);
|
2017-12-21 07:45:38 +08:00
|
|
|
|
|
|
|
CompilerInterface *pCompilerInterface = getCompilerInterface();
|
|
|
|
if (!pCompilerInterface) {
|
|
|
|
retVal = CL_OUT_OF_HOST_MEMORY;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
TranslationArgs inputArgs = {};
|
|
|
|
|
|
|
|
// set parameters for compilation
|
2018-01-16 01:16:50 +08:00
|
|
|
internalOptions.append(platform()->peekCompilerExtensions());
|
2018-03-09 21:40:31 +08:00
|
|
|
|
|
|
|
if (isKernelDebugEnabled()) {
|
|
|
|
internalOptions.append(CompilerOptions::debugKernelEnable);
|
2018-04-18 21:48:03 +08:00
|
|
|
options.append(" -g ");
|
2018-05-02 20:27:55 +08:00
|
|
|
if (pDevice->getSourceLevelDebugger()) {
|
|
|
|
if (pDevice->getSourceLevelDebugger()->isOptimizationDisabled()) {
|
|
|
|
options.append("-cl-opt-disable ");
|
|
|
|
}
|
|
|
|
std::string filename;
|
|
|
|
pDevice->getSourceLevelDebugger()->notifySourceCode(sourceCode.c_str(), sourceCode.size(), filename);
|
|
|
|
if (!filename.empty()) {
|
|
|
|
options = std::string("-s ") + filename + " " + options;
|
|
|
|
}
|
|
|
|
}
|
2018-03-09 21:40:31 +08:00
|
|
|
}
|
|
|
|
|
2018-07-28 04:59:40 +08:00
|
|
|
inputArgs.pInput = compileData.data();
|
|
|
|
inputArgs.InputSize = static_cast<uint32_t>(compileDataSize);
|
2017-12-21 07:45:38 +08:00
|
|
|
inputArgs.pOptions = options.c_str();
|
2018-07-28 04:59:40 +08:00
|
|
|
inputArgs.OptionsSize = static_cast<uint32_t>(options.length());
|
2017-12-21 07:45:38 +08:00
|
|
|
inputArgs.pInternalOptions = internalOptions.c_str();
|
2018-07-28 04:59:40 +08:00
|
|
|
inputArgs.InternalOptionsSize = static_cast<uint32_t>(internalOptions.length());
|
2017-12-21 07:45:38 +08:00
|
|
|
inputArgs.pTracingOptions = nullptr;
|
|
|
|
inputArgs.TracingOptionsCount = 0;
|
|
|
|
|
|
|
|
retVal = pCompilerInterface->compile(*this, inputArgs);
|
|
|
|
if (retVal != CL_SUCCESS) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
} // namespace OCLRT
|