1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468
|
/*
* Copyright (C) 2017-2020 Intel Corporation
*
* SPDX-License-Identifier: MIT
*
*/
#include "shared/source/compiler_interface/compiler_interface.h"
#include "shared/source/compiler_interface/compiler_cache.h"
#include "shared/source/compiler_interface/compiler_interface.inl"
#include "shared/source/debug_settings/debug_settings_manager.h"
#include "shared/source/device/device.h"
#include "shared/source/helpers/hw_info.h"
#include "opencl/source/os_interface/os_inc_base.h"
#include "cif/common/cif_main.h"
#include "cif/helpers/error.h"
#include "cif/import/library_api.h"
#include "ocl_igc_interface/code_type.h"
#include "ocl_igc_interface/fcl_ocl_device_ctx.h"
#include "ocl_igc_interface/igc_ocl_device_ctx.h"
#undef IGC_CLEANUP
#include "ocl_igc_interface/platform_helper.h"
#include <fstream>
namespace NEO {
SpinLock CompilerInterface::spinlock;
enum CachingMode {
None,
Direct,
PreProcess
};
CompilerInterface::CompilerInterface()
: cache() {
}
CompilerInterface::~CompilerInterface() = default;
TranslationOutput::ErrorCode CompilerInterface::build(
const NEO::Device &device,
const TranslationInput &input,
TranslationOutput &output) {
if (false == isCompilerAvailable(input.srcType, input.outType)) {
return TranslationOutput::ErrorCode::CompilerNotAvailable;
}
IGC::CodeType::CodeType_t srcCodeType = input.srcType;
IGC::CodeType::CodeType_t intermediateCodeType = IGC::CodeType::undefined;
if (input.preferredIntermediateType != IGC::CodeType::undefined) {
intermediateCodeType = input.preferredIntermediateType;
}
CachingMode cachingMode = None;
if (input.allowCaching) {
if ((srcCodeType == IGC::CodeType::oclC) && (std::strstr(input.src.begin(), "#include") == nullptr)) {
cachingMode = CachingMode::Direct;
} else {
cachingMode = CachingMode::PreProcess;
}
}
std::string kernelFileHash;
if (cachingMode == CachingMode::Direct) {
kernelFileHash = CompilerCache::getCachedFileName(device.getHardwareInfo(),
input.src,
input.apiOptions,
input.internalOptions);
output.deviceBinary.mem = cache->loadCachedBinary(kernelFileHash, output.deviceBinary.size);
if (output.deviceBinary.mem) {
return TranslationOutput::ErrorCode::Success;
}
}
auto inSrc = CIF::Builtins::CreateConstBuffer(igcMain.get(), input.src.begin(), input.src.size());
auto fclOptions = CIF::Builtins::CreateConstBuffer(igcMain.get(), input.apiOptions.begin(), input.apiOptions.size());
auto fclInternalOptions = CIF::Builtins::CreateConstBuffer(igcMain.get(), input.internalOptions.begin(), input.internalOptions.size());
auto idsBuffer = CIF::Builtins::CreateConstBuffer(igcMain.get(), nullptr, 0);
auto valuesBuffer = CIF::Builtins::CreateConstBuffer(igcMain.get(), nullptr, 0);
for (const auto &specConst : input.specializedValues) {
idsBuffer->PushBackRawCopy(specConst.first);
valuesBuffer->PushBackRawCopy(specConst.second);
}
CIF::RAII::UPtr_t<CIF::Builtins::BufferSimple> intermediateRepresentation;
if (srcCodeType == IGC::CodeType::oclC) {
if (intermediateCodeType == IGC::CodeType::undefined) {
intermediateCodeType = getPreferredIntermediateRepresentation(device);
}
auto fclTranslationCtx = createFclTranslationCtx(device, srcCodeType, intermediateCodeType);
auto fclOutput = translate(fclTranslationCtx.get(), inSrc.get(),
fclOptions.get(), fclInternalOptions.get());
if (fclOutput == nullptr) {
return TranslationOutput::ErrorCode::UnknownError;
}
TranslationOutput::makeCopy(output.frontendCompilerLog, fclOutput->GetBuildLog());
if (fclOutput->Successful() == false) {
return TranslationOutput::ErrorCode::BuildFailure;
}
output.intermediateCodeType = intermediateCodeType;
TranslationOutput::makeCopy(output.intermediateRepresentation, fclOutput->GetOutput());
fclOutput->GetOutput()->Retain(); // will be used as input to compiler
intermediateRepresentation.reset(fclOutput->GetOutput());
} else {
inSrc->Retain(); // will be used as input to compiler directly
intermediateRepresentation.reset(inSrc.get());
intermediateCodeType = srcCodeType;
}
if (cachingMode == CachingMode::PreProcess) {
kernelFileHash = CompilerCache::getCachedFileName(device.getHardwareInfo(), ArrayRef<const char>(intermediateRepresentation->GetMemory<char>(), intermediateRepresentation->GetSize<char>()),
input.apiOptions,
input.internalOptions);
output.deviceBinary.mem = cache->loadCachedBinary(kernelFileHash, output.deviceBinary.size);
if (output.deviceBinary.mem) {
return TranslationOutput::ErrorCode::Success;
}
}
auto igcTranslationCtx = createIgcTranslationCtx(device, intermediateCodeType, IGC::CodeType::oclGenBin);
auto igcOutput = translate(igcTranslationCtx.get(), intermediateRepresentation.get(), idsBuffer.get(), valuesBuffer.get(),
fclOptions.get(), fclInternalOptions.get(), input.GTPinInput);
if (igcOutput == nullptr) {
return TranslationOutput::ErrorCode::UnknownError;
}
TranslationOutput::makeCopy(output.backendCompilerLog, igcOutput->GetBuildLog());
if (igcOutput->Successful() == false) {
return TranslationOutput::ErrorCode::BuildFailure;
}
if (input.allowCaching) {
cache->cacheBinary(kernelFileHash, igcOutput->GetOutput()->GetMemory<char>(), static_cast<uint32_t>(igcOutput->GetOutput()->GetSize<char>()));
}
TranslationOutput::makeCopy(output.deviceBinary, igcOutput->GetOutput());
TranslationOutput::makeCopy(output.debugData, igcOutput->GetDebugData());
return TranslationOutput::ErrorCode::Success;
}
TranslationOutput::ErrorCode CompilerInterface::compile(
const NEO::Device &device,
const TranslationInput &input,
TranslationOutput &output) {
if ((IGC::CodeType::oclC != input.srcType) && (IGC::CodeType::elf != input.srcType)) {
return TranslationOutput::ErrorCode::AlreadyCompiled;
}
if (false == isCompilerAvailable(input.srcType, input.outType)) {
return TranslationOutput::ErrorCode::CompilerNotAvailable;
}
auto outType = input.outType;
if (outType == IGC::CodeType::undefined) {
outType = getPreferredIntermediateRepresentation(device);
}
auto fclSrc = CIF::Builtins::CreateConstBuffer(fclMain.get(), input.src.begin(), input.src.size());
auto fclOptions = CIF::Builtins::CreateConstBuffer(fclMain.get(), input.apiOptions.begin(), input.apiOptions.size());
auto fclInternalOptions = CIF::Builtins::CreateConstBuffer(fclMain.get(), input.internalOptions.begin(), input.internalOptions.size());
auto fclTranslationCtx = createFclTranslationCtx(device, input.srcType, outType);
auto fclOutput = translate(fclTranslationCtx.get(), fclSrc.get(),
fclOptions.get(), fclInternalOptions.get());
if (fclOutput == nullptr) {
return TranslationOutput::ErrorCode::UnknownError;
}
TranslationOutput::makeCopy(output.frontendCompilerLog, fclOutput->GetBuildLog());
if (fclOutput->Successful() == false) {
return TranslationOutput::ErrorCode::CompilationFailure;
}
output.intermediateCodeType = outType;
TranslationOutput::makeCopy(output.intermediateRepresentation, fclOutput->GetOutput());
return TranslationOutput::ErrorCode::Success;
}
TranslationOutput::ErrorCode CompilerInterface::link(
const NEO::Device &device,
const TranslationInput &input,
TranslationOutput &output) {
if (false == isCompilerAvailable(input.srcType, input.outType)) {
return TranslationOutput::ErrorCode::CompilerNotAvailable;
}
auto inSrc = CIF::Builtins::CreateConstBuffer(igcMain.get(), input.src.begin(), input.src.size());
auto igcOptions = CIF::Builtins::CreateConstBuffer(igcMain.get(), input.apiOptions.begin(), input.apiOptions.size());
auto igcInternalOptions = CIF::Builtins::CreateConstBuffer(igcMain.get(), input.internalOptions.begin(), input.internalOptions.size());
if (inSrc == nullptr) {
return TranslationOutput::ErrorCode::UnknownError;
}
CIF::RAII::UPtr_t<IGC::OclTranslationOutputTagOCL> currOut;
inSrc->Retain(); // shared with currSrc
CIF::RAII::UPtr_t<CIF::Builtins::BufferSimple> currSrc(inSrc.get());
IGC::CodeType::CodeType_t translationChain[] = {IGC::CodeType::elf, IGC::CodeType::llvmBc, IGC::CodeType::oclGenBin};
constexpr size_t numTranslations = sizeof(translationChain) / sizeof(translationChain[0]);
for (size_t ti = 1; ti < numTranslations; ti++) {
IGC::CodeType::CodeType_t inType = translationChain[ti - 1];
IGC::CodeType::CodeType_t outType = translationChain[ti];
auto igcTranslationCtx = createIgcTranslationCtx(device, inType, outType);
currOut = translate(igcTranslationCtx.get(), currSrc.get(),
igcOptions.get(), igcInternalOptions.get(), input.GTPinInput);
if (currOut == nullptr) {
return TranslationOutput::ErrorCode::UnknownError;
}
if (currOut->Successful() == false) {
TranslationOutput::makeCopy(output.backendCompilerLog, currOut->GetBuildLog());
return TranslationOutput::ErrorCode::LinkFailure;
}
currOut->GetOutput()->Retain(); // shared with currSrc
currSrc.reset(currOut->GetOutput());
}
TranslationOutput::makeCopy(output.backendCompilerLog, currOut->GetBuildLog());
TranslationOutput::makeCopy(output.deviceBinary, currOut->GetOutput());
TranslationOutput::makeCopy(output.debugData, currOut->GetDebugData());
return TranslationOutput::ErrorCode::Success;
}
TranslationOutput::ErrorCode CompilerInterface::getSpecConstantsInfo(const NEO::Device &device, ArrayRef<const char> srcSpirV, SpecConstantInfo &output) {
if (false == isIgcAvailable()) {
return TranslationOutput::ErrorCode::CompilerNotAvailable;
}
auto igcTranslationCtx = createIgcTranslationCtx(device, IGC::CodeType::spirV, IGC::CodeType::oclGenBin);
auto inSrc = CIF::Builtins::CreateConstBuffer(igcMain.get(), srcSpirV.begin(), srcSpirV.size());
output.idsBuffer = CIF::Builtins::CreateConstBuffer(igcMain.get(), nullptr, 0);
output.sizesBuffer = CIF::Builtins::CreateConstBuffer(igcMain.get(), nullptr, 0);
auto retVal = getSpecConstantsInfoImpl(igcTranslationCtx.get(), inSrc.get(), output.idsBuffer.get(), output.sizesBuffer.get());
if (!retVal) {
return TranslationOutput::ErrorCode::UnknownError;
}
return TranslationOutput::ErrorCode::Success;
}
TranslationOutput::ErrorCode CompilerInterface::createLibrary(
NEO::Device &device,
const TranslationInput &input,
TranslationOutput &output) {
if (false == isIgcAvailable()) {
return TranslationOutput::ErrorCode::CompilerNotAvailable;
}
auto igcSrc = CIF::Builtins::CreateConstBuffer(igcMain.get(), input.src.begin(), input.src.size());
auto igcOptions = CIF::Builtins::CreateConstBuffer(igcMain.get(), input.apiOptions.begin(), input.apiOptions.size());
auto igcInternalOptions = CIF::Builtins::CreateConstBuffer(igcMain.get(), input.internalOptions.begin(), input.internalOptions.size());
auto intermediateRepresentation = IGC::CodeType::llvmBc;
auto igcTranslationCtx = createIgcTranslationCtx(device, IGC::CodeType::elf, intermediateRepresentation);
auto igcOutput = translate(igcTranslationCtx.get(), igcSrc.get(),
igcOptions.get(), igcInternalOptions.get());
if (igcOutput == nullptr) {
return TranslationOutput::ErrorCode::UnknownError;
}
TranslationOutput::makeCopy(output.backendCompilerLog, igcOutput->GetBuildLog());
if (igcOutput->Successful() == false) {
return TranslationOutput::ErrorCode::LinkFailure;
}
output.intermediateCodeType = intermediateRepresentation;
TranslationOutput::makeCopy(output.intermediateRepresentation, igcOutput->GetOutput());
return TranslationOutput::ErrorCode::Success;
}
TranslationOutput::ErrorCode CompilerInterface::getSipKernelBinary(NEO::Device &device, SipKernelType type, std::vector<char> &retBinary) {
if (false == isIgcAvailable()) {
return TranslationOutput::ErrorCode::CompilerNotAvailable;
}
const char *sipSrc = getSipLlSrc(device);
std::string sipInternalOptions = getSipKernelCompilerInternalOptions(type);
auto igcSrc = CIF::Builtins::CreateConstBuffer(igcMain.get(), sipSrc, strlen(sipSrc) + 1);
auto igcOptions = CIF::Builtins::CreateConstBuffer(igcMain.get(), nullptr, 0);
auto igcInternalOptions = CIF::Builtins::CreateConstBuffer(igcMain.get(), sipInternalOptions.c_str(), sipInternalOptions.size() + 1);
auto igcTranslationCtx = createIgcTranslationCtx(device, IGC::CodeType::llvmLl, IGC::CodeType::oclGenBin);
auto igcOutput = translate(igcTranslationCtx.get(), igcSrc.get(),
igcOptions.get(), igcInternalOptions.get());
if ((igcOutput == nullptr) || (igcOutput->Successful() == false)) {
return TranslationOutput::ErrorCode::UnknownError;
}
retBinary.assign(igcOutput->GetOutput()->GetMemory<char>(), igcOutput->GetOutput()->GetMemory<char>() + igcOutput->GetOutput()->GetSizeRaw());
return TranslationOutput::ErrorCode::Success;
}
bool CompilerInterface::loadFcl() {
return NEO::loadCompiler<IGC::FclOclDeviceCtx>(Os::frontEndDllName, fclLib, fclMain);
}
bool CompilerInterface::loadIgc() {
return NEO::loadCompiler<IGC::IgcOclDeviceCtx>(Os::igcDllName, igcLib, igcMain);
}
bool CompilerInterface::initialize(std::unique_ptr<CompilerCache> cache, bool requireFcl) {
bool fclAvailable = requireFcl ? this->loadFcl() : false;
bool igcAvailable = this->loadIgc();
this->cache.swap(cache);
return this->cache && igcAvailable && (fclAvailable || (false == requireFcl));
}
IGC::FclOclDeviceCtxTagOCL *CompilerInterface::getFclDeviceCtx(const Device &device) {
auto ulock = this->lock();
auto it = fclDeviceContexts.find(&device);
if (it != fclDeviceContexts.end()) {
return it->second.get();
}
if (fclMain == nullptr) {
DEBUG_BREAK_IF(true); // compiler not available
return nullptr;
}
auto newDeviceCtx = fclMain->CreateInterface<IGC::FclOclDeviceCtxTagOCL>();
if (newDeviceCtx == nullptr) {
DEBUG_BREAK_IF(true); // could not create device context
return nullptr;
}
newDeviceCtx->SetOclApiVersion(device.getHardwareInfo().capabilityTable.clVersionSupport * 10);
fclDeviceContexts[&device] = std::move(newDeviceCtx);
return fclDeviceContexts[&device].get();
}
IGC::IgcOclDeviceCtxTagOCL *CompilerInterface::getIgcDeviceCtx(const Device &device) {
auto ulock = this->lock();
auto it = igcDeviceContexts.find(&device);
if (it != igcDeviceContexts.end()) {
return it->second.get();
}
if (igcMain == nullptr) {
DEBUG_BREAK_IF(true); // compiler not available
return nullptr;
}
auto newDeviceCtx = igcMain->CreateInterface<IGC::IgcOclDeviceCtxTagOCL>();
if (newDeviceCtx == nullptr) {
DEBUG_BREAK_IF(true); // could not create device context
return nullptr;
}
newDeviceCtx->SetProfilingTimerResolution(static_cast<float>(device.getDeviceInfo().outProfilingTimerResolution));
auto igcPlatform = newDeviceCtx->GetPlatformHandle();
auto igcGtSystemInfo = newDeviceCtx->GetGTSystemInfoHandle();
auto igcFeWa = newDeviceCtx->GetIgcFeaturesAndWorkaroundsHandle();
if (false == NEO::areNotNullptr(igcPlatform.get(), igcGtSystemInfo.get(), igcFeWa.get())) {
DEBUG_BREAK_IF(true); // could not acquire handles to device descriptors
return nullptr;
}
const HardwareInfo *hwInfo = &device.getHardwareInfo();
auto productFamily = DebugManager.flags.ForceCompilerUsePlatform.get();
if (productFamily != "unk") {
getHwInfoForPlatformString(productFamily, hwInfo);
}
IGC::PlatformHelper::PopulateInterfaceWith(*igcPlatform, hwInfo->platform);
IGC::GtSysInfoHelper::PopulateInterfaceWith(*igcGtSystemInfo, hwInfo->gtSystemInfo);
igcFeWa.get()->SetFtrDesktop(device.getHardwareInfo().featureTable.ftrDesktop);
igcFeWa.get()->SetFtrChannelSwizzlingXOREnabled(device.getHardwareInfo().featureTable.ftrChannelSwizzlingXOREnabled);
igcFeWa.get()->SetFtrGtBigDie(device.getHardwareInfo().featureTable.ftrGtBigDie);
igcFeWa.get()->SetFtrGtMediumDie(device.getHardwareInfo().featureTable.ftrGtMediumDie);
igcFeWa.get()->SetFtrGtSmallDie(device.getHardwareInfo().featureTable.ftrGtSmallDie);
igcFeWa.get()->SetFtrGT1(device.getHardwareInfo().featureTable.ftrGT1);
igcFeWa.get()->SetFtrGT1_5(device.getHardwareInfo().featureTable.ftrGT1_5);
igcFeWa.get()->SetFtrGT2(device.getHardwareInfo().featureTable.ftrGT2);
igcFeWa.get()->SetFtrGT3(device.getHardwareInfo().featureTable.ftrGT3);
igcFeWa.get()->SetFtrGT4(device.getHardwareInfo().featureTable.ftrGT4);
igcFeWa.get()->SetFtrIVBM0M1Platform(device.getHardwareInfo().featureTable.ftrIVBM0M1Platform);
igcFeWa.get()->SetFtrGTL(device.getHardwareInfo().featureTable.ftrGT1);
igcFeWa.get()->SetFtrGTM(device.getHardwareInfo().featureTable.ftrGT2);
igcFeWa.get()->SetFtrGTH(device.getHardwareInfo().featureTable.ftrGT3);
igcFeWa.get()->SetFtrSGTPVSKUStrapPresent(device.getHardwareInfo().featureTable.ftrSGTPVSKUStrapPresent);
igcFeWa.get()->SetFtrGTA(device.getHardwareInfo().featureTable.ftrGTA);
igcFeWa.get()->SetFtrGTC(device.getHardwareInfo().featureTable.ftrGTC);
igcFeWa.get()->SetFtrGTX(device.getHardwareInfo().featureTable.ftrGTX);
igcFeWa.get()->SetFtr5Slice(device.getHardwareInfo().featureTable.ftr5Slice);
igcFeWa.get()->SetFtrGpGpuMidThreadLevelPreempt(device.getHardwareInfo().featureTable.ftrGpGpuMidThreadLevelPreempt);
igcFeWa.get()->SetFtrIoMmuPageFaulting(device.getHardwareInfo().featureTable.ftrIoMmuPageFaulting);
igcFeWa.get()->SetFtrWddm2Svm(device.getHardwareInfo().featureTable.ftrWddm2Svm);
igcFeWa.get()->SetFtrPooledEuEnabled(device.getHardwareInfo().featureTable.ftrPooledEuEnabled);
igcFeWa.get()->SetFtrResourceStreamer(device.getHardwareInfo().featureTable.ftrResourceStreamer);
igcDeviceContexts[&device] = std::move(newDeviceCtx);
return igcDeviceContexts[&device].get();
}
IGC::CodeType::CodeType_t CompilerInterface::getPreferredIntermediateRepresentation(const Device &device) {
return getFclDeviceCtx(device)->GetPreferredIntermediateRepresentation();
}
CIF::RAII::UPtr_t<IGC::FclOclTranslationCtxTagOCL> CompilerInterface::createFclTranslationCtx(const Device &device, IGC::CodeType::CodeType_t inType, IGC::CodeType::CodeType_t outType) {
auto deviceCtx = getFclDeviceCtx(device);
if (deviceCtx == nullptr) {
DEBUG_BREAK_IF(true); // could not create device context
return nullptr;
}
if (fclBaseTranslationCtx == nullptr) {
fclBaseTranslationCtx = deviceCtx->CreateTranslationCtx(inType, outType);
}
return deviceCtx->CreateTranslationCtx(inType, outType);
}
CIF::RAII::UPtr_t<IGC::IgcOclTranslationCtxTagOCL> CompilerInterface::createIgcTranslationCtx(const Device &device, IGC::CodeType::CodeType_t inType, IGC::CodeType::CodeType_t outType) {
auto deviceCtx = getIgcDeviceCtx(device);
if (deviceCtx == nullptr) {
DEBUG_BREAK_IF(true); // could not create device context
return nullptr;
}
return deviceCtx->CreateTranslationCtx(inType, outType);
}
} // namespace NEO
|