2020-04-09 21:08:17 +08:00
|
|
|
/*
|
2025-04-08 22:43:25 +08:00
|
|
|
* Copyright (C) 2020-2025 Intel Corporation
|
2020-04-09 21:08:17 +08:00
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: MIT
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2023-01-02 17:56:45 +08:00
|
|
|
#include "shared/source/built_ins/sip.h"
|
2020-04-09 21:08:17 +08:00
|
|
|
#include "shared/source/compiler_interface/compiler_interface.h"
|
|
|
|
|
|
|
|
#include "ocl_igc_interface/fcl_ocl_device_ctx.h"
|
|
|
|
#include "ocl_igc_interface/igc_ocl_device_ctx.h"
|
|
|
|
|
|
|
|
#include <functional>
|
|
|
|
#include <map>
|
2024-10-15 02:23:13 +08:00
|
|
|
#include <optional>
|
2020-04-09 21:08:17 +08:00
|
|
|
#include <string>
|
|
|
|
|
|
|
|
namespace NEO {
|
|
|
|
|
|
|
|
class MockCompilerInterface : public CompilerInterface {
|
|
|
|
public:
|
2023-04-27 16:42:36 +08:00
|
|
|
using CompilerInterface::cache;
|
2023-03-03 19:24:48 +08:00
|
|
|
using CompilerInterface::fclBaseTranslationCtx;
|
2020-12-09 15:41:52 +08:00
|
|
|
using CompilerInterface::fclDeviceContexts;
|
2020-04-09 21:08:17 +08:00
|
|
|
using CompilerInterface::initialize;
|
|
|
|
using CompilerInterface::isCompilerAvailable;
|
|
|
|
using CompilerInterface::isFclAvailable;
|
|
|
|
using CompilerInterface::isIgcAvailable;
|
|
|
|
|
2024-10-15 02:23:13 +08:00
|
|
|
using CompilerInterface::defaultIgc;
|
|
|
|
using CompilerInterface::fcl;
|
|
|
|
|
|
|
|
using CompilerInterface::customCompilerLibraries;
|
|
|
|
using CompilerInterface::finalizerDeviceContexts;
|
2025-04-08 22:43:25 +08:00
|
|
|
using CompilerInterface::useIgcAsFcl;
|
2020-04-09 21:08:17 +08:00
|
|
|
|
|
|
|
bool loadFcl() override {
|
|
|
|
if (failLoadFcl) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return CompilerInterface::loadFcl();
|
|
|
|
}
|
|
|
|
|
2024-10-15 02:23:13 +08:00
|
|
|
bool loadIgcBasedCompiler(CompilerLibraryEntry &entryPoint, const char *libName) override {
|
2020-04-09 21:08:17 +08:00
|
|
|
if (failLoadIgc) {
|
|
|
|
return false;
|
|
|
|
}
|
2024-10-15 02:23:13 +08:00
|
|
|
|
|
|
|
if (igcLibraryNameOverride.has_value()) {
|
|
|
|
libName = *igcLibraryNameOverride;
|
|
|
|
}
|
|
|
|
|
|
|
|
return CompilerInterface::loadIgcBasedCompiler(entryPoint, libName);
|
2020-04-09 21:08:17 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void setFclDeviceCtx(const Device &d, IGC::FclOclDeviceCtxTagOCL *ctx) {
|
|
|
|
this->fclDeviceContexts[&d] = CIF::RAII::RetainAndPack<IGC::FclOclDeviceCtxTagOCL>(ctx);
|
|
|
|
}
|
|
|
|
|
2024-10-15 02:23:13 +08:00
|
|
|
std::unordered_map<const Device *, fclDevCtxUptr> &getFclDeviceContexts() {
|
2020-04-09 21:08:17 +08:00
|
|
|
return this->fclDeviceContexts;
|
|
|
|
}
|
|
|
|
|
|
|
|
void setIgcDeviceCtx(const Device &d, IGC::IgcOclDeviceCtxTagOCL *ctx) {
|
|
|
|
this->igcDeviceContexts[&d] = CIF::RAII::RetainAndPack<IGC::IgcOclDeviceCtxTagOCL>(ctx);
|
|
|
|
}
|
|
|
|
|
2024-10-15 02:23:13 +08:00
|
|
|
void setFinalizerDeviceCtx(const Device &d, IGC::IgcOclDeviceCtxTagOCL *ctx) {
|
|
|
|
this->finalizerDeviceContexts[&d] = CIF::RAII::RetainAndPack<IGC::IgcOclDeviceCtxTagOCL>(ctx);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::unordered_map<const Device *, igcDevCtxUptr> &getIgcDeviceContexts() {
|
2020-04-09 21:08:17 +08:00
|
|
|
return this->igcDeviceContexts;
|
|
|
|
}
|
|
|
|
|
|
|
|
void setDeviceCtx(const Device &d, IGC::IgcOclDeviceCtxTagOCL *ctx) {
|
|
|
|
setIgcDeviceCtx(d, ctx);
|
|
|
|
}
|
|
|
|
|
|
|
|
void setDeviceCtx(const Device &d, IGC::FclOclDeviceCtxTagOCL *ctx) {
|
|
|
|
setFclDeviceCtx(d, ctx);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename DeviceCtx>
|
2024-10-15 02:23:13 +08:00
|
|
|
std::unordered_map<const Device *, CIF::RAII::UPtr_t<DeviceCtx>> &getDeviceContexts();
|
2020-04-09 21:08:17 +08:00
|
|
|
|
|
|
|
std::unique_lock<SpinLock> lock() override {
|
|
|
|
if (lockListener != nullptr) {
|
|
|
|
lockListener(*this);
|
|
|
|
}
|
|
|
|
|
|
|
|
return std::unique_lock<SpinLock>(spinlock);
|
|
|
|
}
|
|
|
|
|
2020-04-15 23:18:27 +08:00
|
|
|
void setIgcMain(CIF::CIFMain *main) {
|
2024-10-15 02:23:13 +08:00
|
|
|
this->defaultIgc.entryPoint.release();
|
|
|
|
this->defaultIgc.entryPoint.reset(main);
|
2020-04-09 21:08:17 +08:00
|
|
|
}
|
|
|
|
|
2020-04-15 23:18:27 +08:00
|
|
|
void setFclMain(CIF::CIFMain *main) {
|
2024-10-15 02:23:13 +08:00
|
|
|
this->fcl.entryPoint.release();
|
|
|
|
this->fcl.entryPoint.reset(main);
|
2020-04-09 21:08:17 +08:00
|
|
|
}
|
|
|
|
|
2021-11-25 18:34:35 +08:00
|
|
|
IGC::IgcOclDeviceCtxTagOCL *getIgcDeviceCtx(const Device &device) override {
|
|
|
|
if (failGetIgcDeviceCtx) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
return CompilerInterface::getIgcDeviceCtx(device);
|
|
|
|
}
|
|
|
|
|
2020-04-09 21:08:17 +08:00
|
|
|
CIF::RAII::UPtr_t<IGC::FclOclTranslationCtxTagOCL> createFclTranslationCtx(const Device &device,
|
|
|
|
IGC::CodeType::CodeType_t inType,
|
|
|
|
IGC::CodeType::CodeType_t outType) override {
|
|
|
|
requestedTranslationCtxs.emplace_back(inType, outType);
|
|
|
|
if (failCreateFclTranslationCtx) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
return CompilerInterface::createFclTranslationCtx(device, inType, outType);
|
|
|
|
}
|
|
|
|
|
|
|
|
CIF::RAII::UPtr_t<IGC::IgcOclTranslationCtxTagOCL> createIgcTranslationCtx(const Device &device,
|
|
|
|
IGC::CodeType::CodeType_t inType,
|
|
|
|
IGC::CodeType::CodeType_t outType) override {
|
|
|
|
requestedTranslationCtxs.emplace_back(inType, outType);
|
|
|
|
if (failCreateIgcTranslationCtx) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
return CompilerInterface::createIgcTranslationCtx(device, inType, outType);
|
|
|
|
}
|
|
|
|
|
2024-10-15 02:23:13 +08:00
|
|
|
CIF::RAII::UPtr_t<IGC::IgcOclTranslationCtxTagOCL> createFinalizerTranslationCtx(const Device &device,
|
|
|
|
IGC::CodeType::CodeType_t inType,
|
|
|
|
IGC::CodeType::CodeType_t outType) override {
|
|
|
|
requestedTranslationCtxs.emplace_back(inType, outType);
|
|
|
|
if (failCreateFinalizerTranslationCtx) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
return CompilerInterface::createFinalizerTranslationCtx(device, inType, outType);
|
|
|
|
}
|
|
|
|
|
2020-04-09 21:08:17 +08:00
|
|
|
IGC::FclOclTranslationCtxTagOCL *getFclBaseTranslationCtx() {
|
|
|
|
return this->fclBaseTranslationCtx.get();
|
|
|
|
}
|
|
|
|
|
2021-10-18 23:59:47 +08:00
|
|
|
TranslationOutput::ErrorCode getSipKernelBinary(NEO::Device &device, SipKernelType type, std::vector<char> &retBinary,
|
2021-03-31 10:31:58 +08:00
|
|
|
std::vector<char> &stateAreaHeader) override {
|
2020-04-09 21:08:17 +08:00
|
|
|
if (this->sipKernelBinaryOverride.size() > 0) {
|
|
|
|
retBinary = this->sipKernelBinaryOverride;
|
|
|
|
this->requestedSipKernel = type;
|
2023-12-19 15:40:17 +08:00
|
|
|
return TranslationOutput::ErrorCode::success;
|
2020-04-09 21:08:17 +08:00
|
|
|
} else {
|
2021-10-18 23:59:47 +08:00
|
|
|
return CompilerInterface::getSipKernelBinary(device, type, retBinary, stateAreaHeader);
|
2020-04-09 21:08:17 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-29 00:44:23 +08:00
|
|
|
CIF::RAII::UPtr_t<IGC::IgcFeaturesAndWorkaroundsTagOCL> getIgcFeaturesAndWorkarounds(const NEO::Device &device) override {
|
|
|
|
if (nullptr != igcFeaturesAndWorkaroundsTagOCL) {
|
|
|
|
return CIF::RAII::RetainAndPack<IGC::IgcFeaturesAndWorkaroundsTagOCL>(igcFeaturesAndWorkaroundsTagOCL);
|
|
|
|
} else {
|
|
|
|
return CompilerInterface::getIgcFeaturesAndWorkarounds(device);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-04-09 21:08:17 +08:00
|
|
|
static std::vector<char> getDummyGenBinary();
|
2020-12-02 18:22:27 +08:00
|
|
|
static void releaseDummyGenBinary();
|
2020-04-09 21:08:17 +08:00
|
|
|
|
|
|
|
void (*lockListener)(MockCompilerInterface &compInt) = nullptr;
|
|
|
|
void *lockListenerData = nullptr;
|
2021-12-29 00:44:23 +08:00
|
|
|
IGC::IgcFeaturesAndWorkaroundsTagOCL *igcFeaturesAndWorkaroundsTagOCL = nullptr;
|
2020-04-09 21:08:17 +08:00
|
|
|
bool failCreateFclTranslationCtx = false;
|
|
|
|
bool failCreateIgcTranslationCtx = false;
|
2024-10-15 02:23:13 +08:00
|
|
|
bool failCreateFinalizerTranslationCtx = false;
|
2020-04-09 21:08:17 +08:00
|
|
|
bool failLoadFcl = false;
|
|
|
|
bool failLoadIgc = false;
|
2021-11-25 18:34:35 +08:00
|
|
|
bool failGetIgcDeviceCtx = false;
|
2024-10-15 02:23:13 +08:00
|
|
|
std::optional<const char *> igcLibraryNameOverride;
|
2020-04-09 21:08:17 +08:00
|
|
|
|
|
|
|
using TranslationOpT = std::pair<IGC::CodeType::CodeType_t, IGC::CodeType::CodeType_t>;
|
|
|
|
std::vector<TranslationOpT> requestedTranslationCtxs;
|
|
|
|
|
|
|
|
std::vector<char> sipKernelBinaryOverride;
|
2023-12-11 19:02:15 +08:00
|
|
|
SipKernelType requestedSipKernel = SipKernelType::count;
|
2020-04-09 21:08:17 +08:00
|
|
|
|
|
|
|
IGC::IgcOclDeviceCtxTagOCL *peekIgcDeviceCtx(Device *device) { return igcDeviceContexts[device].get(); }
|
|
|
|
};
|
|
|
|
|
|
|
|
template <>
|
2024-10-15 02:23:13 +08:00
|
|
|
inline std::unordered_map<const Device *, MockCompilerInterface::igcDevCtxUptr> &MockCompilerInterface::getDeviceContexts<IGC::IgcOclDeviceCtxTagOCL>() {
|
2020-04-09 21:08:17 +08:00
|
|
|
return getIgcDeviceContexts();
|
|
|
|
}
|
|
|
|
|
|
|
|
template <>
|
2024-10-15 02:23:13 +08:00
|
|
|
inline std::unordered_map<const Device *, MockCompilerInterface::fclDevCtxUptr> &MockCompilerInterface::getDeviceContexts<IGC::FclOclDeviceCtxTagOCL>() {
|
2020-04-09 21:08:17 +08:00
|
|
|
return getFclDeviceContexts();
|
|
|
|
}
|
|
|
|
|
|
|
|
struct MockCompilerInterfaceCaptureBuildOptions : CompilerInterface {
|
2022-11-02 21:36:46 +08:00
|
|
|
TranslationOutput::ErrorCode compile(const NEO::Device &device, const TranslationInput &input, TranslationOutput &out) override {
|
2020-04-09 21:08:17 +08:00
|
|
|
buildOptions.clear();
|
|
|
|
if ((input.apiOptions.size() > 0) && (input.apiOptions.begin() != nullptr)) {
|
|
|
|
buildOptions.assign(input.apiOptions.begin(), input.apiOptions.end());
|
|
|
|
}
|
|
|
|
buildInternalOptions.clear();
|
|
|
|
if ((input.internalOptions.size() > 0) && (input.internalOptions.begin() != nullptr)) {
|
|
|
|
buildInternalOptions.assign(input.internalOptions.begin(), input.internalOptions.end());
|
|
|
|
}
|
2022-11-02 21:36:46 +08:00
|
|
|
|
|
|
|
auto copy = [](TranslationOutput::MemAndSize &dst, TranslationOutput::MemAndSize &src) {
|
|
|
|
if (src.size > 0) {
|
|
|
|
dst.size = src.size;
|
|
|
|
dst.mem.reset(new char[src.size]);
|
|
|
|
std::memcpy(dst.mem.get(), src.mem.get(), src.size);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
copy(out.debugData, output.debugData);
|
|
|
|
copy(out.intermediateRepresentation, output.intermediateRepresentation);
|
|
|
|
copy(out.deviceBinary, output.intermediateRepresentation);
|
|
|
|
out.intermediateCodeType = output.intermediateCodeType;
|
|
|
|
|
2023-12-19 15:40:17 +08:00
|
|
|
return TranslationOutput::ErrorCode::success;
|
2020-04-09 21:08:17 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
TranslationOutput::ErrorCode build(const NEO::Device &device, const TranslationInput &input, TranslationOutput &out) override {
|
|
|
|
return this->MockCompilerInterfaceCaptureBuildOptions::compile(device, input, out);
|
|
|
|
}
|
|
|
|
|
2020-06-10 16:38:40 +08:00
|
|
|
TranslationOutput::ErrorCode link(const NEO::Device &device,
|
|
|
|
const TranslationInput &input,
|
|
|
|
TranslationOutput &output) override {
|
|
|
|
return this->MockCompilerInterfaceCaptureBuildOptions::compile(device, input, output);
|
|
|
|
}
|
|
|
|
|
2022-11-02 21:36:46 +08:00
|
|
|
TranslationOutput output;
|
2020-04-09 21:08:17 +08:00
|
|
|
std::string buildOptions;
|
|
|
|
std::string buildInternalOptions;
|
|
|
|
};
|
|
|
|
} // namespace NEO
|