2017-12-21 07:45:38 +08:00
/*
2022-08-29 07:20:29 +08:00
* Copyright (C) 2018-2022 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
*
*/
#pragma once
2020-07-30 19:18:54 +08:00
#include "shared/source/debug_settings/debug_settings_manager.h"
2022-08-29 07:20:29 +08:00
#include "shared/source/helpers/debug_helpers.h"
2021-10-13 02:58:51 +08:00
#include "shared/source/helpers/validators.h"
2020-02-24 05:44:01 +08:00
#include "shared/source/os_interface/os_library.h"
2020-02-24 17:22:30 +08:00
2017-12-21 07:45:38 +08:00
#include "cif/builtins/memory/buffer/buffer.h"
#include "cif/common/cif.h"
#include "cif/import/library_api.h"
#include "ocl_igc_interface/ocl_translation_output.h"
2019-03-26 18:59:46 +08:00
namespace NEO {
2017-12-21 07:45:38 +08:00
using CIFBuffer = CIF::Builtins::BufferSimple;
template <typename TranslationCtx>
inline CIF::RAII::UPtr_t<IGC::OclTranslationOutputTagOCL> translate(TranslationCtx *tCtx, CIFBuffer *src, CIFBuffer *options,
CIFBuffer *internalOptions) {
2019-03-26 18:59:46 +08:00
if (false == NEO::areNotNullptr(tCtx, src, options, internalOptions)) {
2017-12-21 07:45:38 +08:00
return nullptr;
}
auto ret = tCtx->Translate(src, options, internalOptions, nullptr, 0);
if (ret == nullptr) {
return nullptr; // assume OOM or internal error
}
if ((ret->GetOutput() == nullptr) || (ret->GetBuildLog() == nullptr) || (ret->GetDebugData() == nullptr)) {
return nullptr; // assume OOM or internal error
}
return ret;
}
2018-08-06 19:19:32 +08:00
template <typename TranslationCtx>
inline CIF::RAII::UPtr_t<IGC::OclTranslationOutputTagOCL> translate(TranslationCtx *tCtx, CIFBuffer *src, CIFBuffer *options,
CIFBuffer *internalOptions, void *gtpinInit) {
2019-03-26 18:59:46 +08:00
if (false == NEO::areNotNullptr(tCtx, src, options, internalOptions)) {
2018-08-06 19:19:32 +08:00
return nullptr;
}
auto ret = tCtx->Translate(src, options, internalOptions, nullptr, 0, gtpinInit);
if (ret == nullptr) {
return nullptr; // assume OOM or internal error
}
if ((ret->GetOutput() == nullptr) || (ret->GetBuildLog() == nullptr) || (ret->GetDebugData() == nullptr)) {
return nullptr; // assume OOM or internal error
}
return ret;
}
2017-12-21 07:45:38 +08:00
2019-04-25 18:46:43 +08:00
template <typename TranslationCtx>
inline bool getSpecConstantsInfoImpl(TranslationCtx *tCtx,
CIFBuffer *src,
CIFBuffer *outSpecConstantsIds,
2020-03-19 15:58:12 +08:00
CIFBuffer *outSpecConstantsSizes) {
if (!NEO::areNotNullptr(tCtx, src, outSpecConstantsIds, outSpecConstantsSizes)) {
2019-04-25 18:46:43 +08:00
return false;
}
return tCtx->GetSpecConstantsInfoImpl(src, outSpecConstantsIds, outSpecConstantsSizes);
}
template <typename TranslationCtx>
inline CIF::RAII::UPtr_t<IGC::OclTranslationOutputTagOCL> translate(TranslationCtx *tCtx, CIFBuffer *src, CIFBuffer *specConstantsIds, CIFBuffer *specConstantsValues, CIFBuffer *options,
CIFBuffer *internalOptions, void *gtpinInit) {
if (false == NEO::areNotNullptr(tCtx, src, options, internalOptions)) {
return nullptr;
}
auto ret = tCtx->Translate(src, specConstantsIds, specConstantsValues, options, internalOptions, nullptr, 0, gtpinInit);
if (ret == nullptr) {
return nullptr; // assume OOM or internal error
}
if (!NEO::areNotNullptr(ret->GetOutput(), ret->GetBuildLog(), ret->GetDebugData())) {
return nullptr; // assume OOM or internal error
}
return ret;
}
2017-12-21 07:45:38 +08:00
CIF::CIFMain *createMainNoSanitize(CIF::CreateCIFMainFunc_t createFunc);
template <template <CIF::Version_t> class EntryPointT>
inline bool loadCompiler(const char *libName, std::unique_ptr<OsLibrary> &outLib,
2018-06-13 03:54:39 +08:00
CIF::RAII::UPtr_t<CIF::CIFMain> &outLibMain) {
2021-03-12 09:01:51 +08:00
std::string loadLibraryError;
auto lib = std::unique_ptr<OsLibrary>(OsLibrary::load(libName, &loadLibraryError));
2017-12-21 07:45:38 +08:00
if (lib == nullptr) {
2021-03-12 09:01:51 +08:00
NEO::printDebugString(NEO::DebugManager.flags.PrintDebugMessages.get(), stderr, "Compiler Library %s could not be loaded with error: %s\n", libName, loadLibraryError.c_str());
2017-12-21 07:45:38 +08:00
DEBUG_BREAK_IF(true); // could not load library
return false;
}
auto createMain = reinterpret_cast<CIF::CreateCIFMainFunc_t>(lib->getProcAddress(CIF::CreateCIFMainFuncName));
UNRECOVERABLE_IF(createMain == nullptr); // invalid compiler library
auto main = CIF::RAII::UPtr(createMainNoSanitize(createMain));
if (main == nullptr) {
DEBUG_BREAK_IF(true); // could not create main entry point
return false;
}
2020-07-30 19:18:54 +08:00
std::vector<CIF::InterfaceId_t> interfacesToIgnore;
if (DebugManager.flags.ZebinIgnoreIcbeVersion.get()) {
interfacesToIgnore.push_back(IGC::OclGenBinaryBase::GetInterfaceId());
}
if (false == main->IsCompatible<EntryPointT>(&interfacesToIgnore)) {
2021-07-20 03:39:56 +08:00
NEO::printDebugString(NEO::DebugManager.flags.PrintDebugMessages.get(), stderr, "Installed Compiler Library %s is incompatible\n", libName);
2017-12-21 07:45:38 +08:00
DEBUG_BREAK_IF(true); // given compiler library is not compatible
return false;
}
outLib = std::move(lib);
outLibMain = std::move(main);
return true;
}
2019-03-26 18:59:46 +08:00
} // namespace NEO