File: ocloc_igc_facade.cpp

package info (click to toggle)
intel-compute-runtime 25.44.36015.8-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 79,632 kB
  • sloc: cpp: 931,547; lisp: 2,074; sh: 719; makefile: 162; python: 21
file content (237 lines) | stat: -rw-r--r-- 9,450 bytes parent folder | download
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
/*
 * Copyright (C) 2022-2025 Intel Corporation
 *
 * SPDX-License-Identifier: MIT
 *
 */

#include "shared/offline_compiler/source/ocloc_igc_facade.h"

#include "shared/offline_compiler/source/ocloc_api.h"
#include "shared/offline_compiler/source/ocloc_arg_helper.h"
#include "shared/source/compiler_interface/igc_platform_helper.h"
#include "shared/source/compiler_interface/os_compiler_cache_helper.h"
#include "shared/source/helpers/compiler_product_helper.h"
#include "shared/source/helpers/constants.h"
#include "shared/source/helpers/debug_helpers.h"
#include "shared/source/helpers/hw_info.h"
#include "shared/source/helpers/string.h"
#include "shared/source/os_interface/os_inc_base.h"
#include "shared/source/os_interface/os_library.h"

#include "ocl_igc_interface/platform_helper.h"

#include <vector>

namespace NEO {

CIF::CIFMain *createMainNoSanitize(CIF::CreateCIFMainFunc_t createFunc);

OclocIgcFacade::OclocIgcFacade(OclocArgHelper *argHelper)
    : argHelper{argHelper} {
}

OclocIgcFacade::~OclocIgcFacade() = default;

int OclocIgcFacade::initialize(const HardwareInfo &hwInfo) {
    if (initialized) {
        return OCLOC_SUCCESS;
    }

    auto compilerProductHelper = NEO::CompilerProductHelper::create(hwInfo.platform.eProductFamily);
    igcLib = loadIgcLibrary(compilerProductHelper ? compilerProductHelper->getCustomIgcLibraryName() : nullptr);
    if (!igcLib) {
        argHelper->printf("Error! Loading of IGC library has failed! Filename: %s\n", (compilerProductHelper && compilerProductHelper->getCustomIgcLibraryName()) ? compilerProductHelper->getCustomIgcLibraryName() : Os::igcDllName);
        return OCLOC_OUT_OF_HOST_MEMORY;
    }

    std::string igcPath = igcLib->getFullPath();
    igcLibSize = NEO::getFileSize(igcPath);
    igcLibMTime = NEO::getFileModificationTime(igcPath);

    const auto igcCreateMainFunction = loadCreateIgcMainFunction();
    if (!igcCreateMainFunction) {
        argHelper->printf("Error! Cannot load required functions from IGC library.\n");
        return OCLOC_OUT_OF_HOST_MEMORY;
    }

    igcMain = createIgcMain(igcCreateMainFunction);
    if (!igcMain) {
        argHelper->printf("Error! Cannot create IGC main component!\n");
        return OCLOC_OUT_OF_HOST_MEMORY;
    }

    const std::vector<CIF::InterfaceId_t> interfacesToIgnore = {IGC::OclGenBinaryBase::GetInterfaceId()};
    if (!isIgcInterfaceCompatible(interfacesToIgnore)) {
        const auto incompatibleInterface{getIncompatibleInterface(interfacesToIgnore)};
        argHelper->printf("Error! Incompatible interface in IGC: %s\n", incompatibleInterface.c_str());

        DEBUG_BREAK_IF(true);
        return OCLOC_OUT_OF_HOST_MEMORY;
    }

    if (!isPatchtokenInterfaceSupported()) {
        argHelper->printf("Error! Patchtoken interface is missing.\n");
        return OCLOC_OUT_OF_HOST_MEMORY;
    }

    {
        // revision is sha-1 hash
        igcRevision.resize(41);
        igcRevision[0] = '\0';
        auto igcDeviceCtx3 = createIgcDeviceContext3();
        if (igcDeviceCtx3) {
            const char *revision = igcDeviceCtx3->GetIGCRevision();
            strncpy_s(igcRevision.data(), 41, revision, 40);
        }
    }

    igcDeviceCtx = createIgcDeviceContext();
    if (!igcDeviceCtx) {
        argHelper->printf("Error! Cannot create IGC device context!\n");
        return OCLOC_OUT_OF_HOST_MEMORY;
    }

    igcDeviceCtx->SetProfilingTimerResolution(static_cast<float>(CommonConstants::defaultProfilingTimerResolution));

    const auto igcPlatform = getIgcPlatformHandle();
    const auto igcGtSystemInfo = getGTSystemInfoHandle();
    const auto igcFtrWa = getIgcFeaturesAndWorkaroundsHandle();

    if (!igcPlatform || !igcGtSystemInfo || !igcFtrWa) {
        argHelper->printf("Error! IGC device context has not been properly created!\n");
        return OCLOC_OUT_OF_HOST_MEMORY;
    }

    populateIgcPlatform(*igcPlatform, hwInfo);
    IGC::GtSysInfoHelper::PopulateInterfaceWith(*igcGtSystemInfo.get(), hwInfo.gtSystemInfo);

    populateWithFeatures(igcFtrWa.get(), hwInfo, compilerProductHelper.get());

    initialized = true;
    return OCLOC_SUCCESS;
}

std::unique_ptr<OsLibrary> OclocIgcFacade::loadIgcLibrary(const char *libName) const {
    auto effectiveLibName = libName ? libName : Os::igcDllName;
    return std::unique_ptr<OsLibrary>{OsLibrary::loadFunc({effectiveLibName})};
}

CIF::CreateCIFMainFunc_t OclocIgcFacade::loadCreateIgcMainFunction() const {
    return reinterpret_cast<CIF::CreateCIFMainFunc_t>(igcLib->getProcAddress(CIF::CreateCIFMainFuncName));
}

CIF::RAII::UPtr_t<CIF::CIFMain> OclocIgcFacade::createIgcMain(CIF::CreateCIFMainFunc_t createMainFunction) const {
    return CIF::RAII::UPtr(createMainNoSanitize(createMainFunction));
}

bool OclocIgcFacade::isIgcInterfaceCompatible(const std::vector<CIF::InterfaceId_t> &interfacesToIgnore) const {
    return igcMain->IsCompatible<IGC::IgcOclDeviceCtx>(&interfacesToIgnore);
}

std::string OclocIgcFacade::getIncompatibleInterface(const std::vector<CIF::InterfaceId_t> &interfacesToIgnore) const {
    return CIF::InterfaceIdCoder::Dec(igcMain->FindIncompatible<IGC::IgcOclDeviceCtx>(&interfacesToIgnore));
}

bool OclocIgcFacade::isPatchtokenInterfaceSupported() const {
    CIF::Version_t verMin = 0, verMax = 0;
    return igcMain->FindSupportedVersions<IGC::IgcOclDeviceCtx>(IGC::OclGenBinaryBase::GetInterfaceId(), verMin, verMax);
}

CIF::RAII::UPtr_t<IGC::IgcOclDeviceCtxTagOCL> OclocIgcFacade::createIgcDeviceContext() const {
    return igcMain->CreateInterface<IGC::IgcOclDeviceCtxTagOCL>();
}

CIF::RAII::UPtr_t<IGC::IgcOclDeviceCtx<3>> OclocIgcFacade::createIgcDeviceContext3() const {
    return igcMain->CreateInterface<IGC::IgcOclDeviceCtx<3>>();
}

CIF::RAII::UPtr_t<IGC::PlatformTagOCL> OclocIgcFacade::getIgcPlatformHandle() const {
    return igcDeviceCtx->GetPlatformHandle();
}

CIF::RAII::UPtr_t<IGC::GTSystemInfoTagOCL> OclocIgcFacade::getGTSystemInfoHandle() const {
    return igcDeviceCtx->GetGTSystemInfoHandle();
}

CIF::RAII::UPtr_t<IGC::IgcFeaturesAndWorkaroundsTagOCL> OclocIgcFacade::getIgcFeaturesAndWorkaroundsHandle() const {
    return igcDeviceCtx->GetIgcFeaturesAndWorkaroundsHandle();
}

void OclocIgcFacade::populateWithFeatures(IGC::IgcFeaturesAndWorkaroundsTagOCL *handle, const HardwareInfo &hwInfo, const CompilerProductHelper *compilerProductHelper) const {
    if (compilerProductHelper) {
        handle->SetFtrGpGpuMidThreadLevelPreempt(compilerProductHelper->isMidThreadPreemptionSupported(hwInfo));
    }

    handle->SetFtrWddm2Svm(hwInfo.featureTable.flags.ftrWddm2Svm);
    handle->SetFtrPooledEuEnabled(hwInfo.featureTable.flags.ftrPooledEuEnabled);
}

const char *OclocIgcFacade::getIgcRevision() {
    return igcRevision.data();
}

size_t OclocIgcFacade::getIgcLibSize() {
    return igcLibSize;
}

time_t OclocIgcFacade::getIgcLibMTime() {
    return igcLibMTime;
}

CIF::RAII::UPtr_t<CIF::Builtins::BufferLatest> OclocIgcFacade::createConstBuffer(const void *data, size_t size) {
    return CIF::Builtins::CreateConstBuffer(igcMain.get(), data, size);
}

CIF::RAII::UPtr_t<IGC::IgcOclTranslationCtxTagOCL> OclocIgcFacade::createTranslationContext(IGC::CodeType::CodeType_t inType, IGC::CodeType::CodeType_t outType) {
    return igcDeviceCtx->CreateTranslationCtx(inType, outType);
}

bool OclocIgcFacade::isInitialized() const {
    return initialized;
}

OclocIgcAsFcl::OclocIgcAsFcl(OclocArgHelper *argHelper) : igc(std::make_unique<OclocIgcFacade>(argHelper)) {}
OclocIgcAsFcl::~OclocIgcAsFcl() = default;

int OclocIgcAsFcl::initialize(const HardwareInfo &hwInfo) {
    auto ret = igc->initialize(hwInfo);
    if (OCLOC_SUCCESS != ret) {
        return ret;
    }
    auto compilerProductHelper = NEO::CompilerProductHelper::create(hwInfo.platform.eProductFamily);
    if (!compilerProductHelper) {
        return OCLOC_INVALID_DEVICE;
    }
    this->preferredIntermediateRepresentation = compilerProductHelper->getPreferredIntermediateRepresentation();
    return OCLOC_SUCCESS;
}

bool OclocIgcAsFcl::isInitialized() const {
    return igc->isInitialized();
}
IGC::CodeType::CodeType_t OclocIgcAsFcl::getPreferredIntermediateRepresentation() const {
    return this->preferredIntermediateRepresentation;
}

CIF::RAII::UPtr_t<CIF::Builtins::BufferLatest> OclocIgcAsFcl::createConstBuffer(const void *data, size_t size) {
    return igc->createConstBuffer(data, size);
}

CIF::RAII::UPtr_t<IGC::OclTranslationOutputTagOCL> OclocIgcAsFcl::translate(IGC::CodeType::CodeType_t inType, IGC::CodeType::CodeType_t outType, CIF::Builtins::BufferLatest *error,
                                                                            CIF::Builtins::BufferSimple *src,
                                                                            CIF::Builtins::BufferSimple *options,
                                                                            CIF::Builtins::BufferSimple *internalOptions,
                                                                            CIF::Builtins::BufferSimple *tracingOptions,
                                                                            uint32_t tracingOptionsCount) {

    auto translationCtx = igc->createTranslationContext(inType, outType);

    if ((nullptr != error->GetMemory<char>()) || (nullptr == translationCtx)) {
        return nullptr;
    }

    return translationCtx->Translate(src, options, internalOptions, nullptr, 0);
}

} // namespace NEO