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
|
/*
* Copyright (C) 2019-2026 Intel Corporation
*
* SPDX-License-Identifier: MIT
*
*/
#include "shared/source/compiler_interface/compiler_cache.h"
#include "shared/source/debug_settings/debug_settings_manager.h"
#include "shared/source/helpers/casts.h"
#include "shared/source/helpers/file_io.h"
#include "shared/source/helpers/hash.h"
#include "shared/source/helpers/hw_info.h"
#include "shared/source/helpers/path.h"
#include "shared/source/utilities/io_functions.h"
#include "elements_struct.h"
#include "os_inc.h"
#include <cstring>
#include <iomanip>
#include <mutex>
#include <sstream>
#include <string>
namespace NEO {
std::mutex CompilerCache::cacheAccessMtx;
const std::string CompilerCache::getCachedFileName(const HardwareInfo &hwInfo, const ArrayRef<const char> input,
const ArrayRef<const char> options, const ArrayRef<const char> internalOptions,
const ArrayRef<const char> specIds, const ArrayRef<const char> specValues,
const ArrayRef<const char> igcRevision, size_t igcLibSize, time_t igcLibMTime) {
Hash hash;
hash.update("----", 4);
hash.update(&*igcRevision.begin(), igcRevision.size());
hash.update(safePodCast<const char *>(&igcLibSize), sizeof(igcLibSize));
hash.update(safePodCast<const char *>(&igcLibMTime), sizeof(igcLibMTime));
hash.update("----", 4);
hash.update(&*input.begin(), input.size());
hash.update("----", 4);
hash.update(&*options.begin(), options.size());
hash.update("----", 4);
hash.update(&*internalOptions.begin(), internalOptions.size());
hash.update("----", 4);
hash.update(&*specIds.begin(), specIds.size());
hash.update(&*specValues.begin(), specValues.size());
hash.update("----", 4);
hash.update(safePodCast<const char *>(&hwInfo.platform), sizeof(hwInfo.platform));
hash.update("----", 4);
const auto featureTableHashStr = std::to_string(hwInfo.featureTable.asHash());
hash.update(featureTableHashStr.c_str(), featureTableHashStr.length());
hash.update("----", 4);
const auto workaroundTableHashStr = std::to_string(hwInfo.workaroundTable.asHash());
hash.update(workaroundTableHashStr.c_str(), workaroundTableHashStr.length());
hash.update("----", 4);
hash.update(reinterpret_cast<const char *>(&hwInfo.ipVersion), sizeof(uint32_t));
auto res = hash.finish();
std::stringstream stream;
stream << std::setfill('0')
<< std::setw(sizeof(res) * 2)
<< std::hex
<< res;
if (debugManager.flags.BinaryCacheTrace.get()) {
std::string traceFilePath = config.cacheDir + PATH_SEPARATOR + stream.str() + ".trace";
std::string inputFilePath = config.cacheDir + PATH_SEPARATOR + stream.str() + ".input";
std::lock_guard<std::mutex> lock(cacheAccessMtx);
auto fp = NEO::IoFunctions::fopenPtr(traceFilePath.c_str(), "w");
if (fp) {
NEO::IoFunctions::fprintf(fp, "---- igcRevision ----\n");
NEO::IoFunctions::fprintf(fp, "%s\n", &*igcRevision.begin());
NEO::IoFunctions::fprintf(fp, " libSize=%llu\n", igcLibSize);
NEO::IoFunctions::fprintf(fp, " libMTime=%llu\n", igcLibMTime);
NEO::IoFunctions::fprintf(fp, "---- input ----\n");
NEO::IoFunctions::fprintf(fp, "<%s>\n", inputFilePath.c_str());
NEO::IoFunctions::fprintf(fp, "---- options ----\n");
NEO::IoFunctions::fprintf(fp, "%s\n", &*options.begin());
NEO::IoFunctions::fprintf(fp, "---- internal options ----\n");
NEO::IoFunctions::fprintf(fp, "%s\n", &*internalOptions.begin());
NEO::IoFunctions::fprintf(fp, "---- specialization constants ----\n");
NEO::IoFunctions::fprintf(fp, "specIds=");
for (size_t idx = 0; idx < specIds.size(); idx++) {
NEO::IoFunctions::fprintf(fp, "%02x", specIds[idx]);
}
NEO::IoFunctions::fprintf(fp, "\n");
NEO::IoFunctions::fprintf(fp, "specValues=");
for (size_t idx = 0; idx < specValues.size(); idx++) {
NEO::IoFunctions::fprintf(fp, "%02x", specValues[idx]);
}
NEO::IoFunctions::fprintf(fp, "\n");
NEO::IoFunctions::fprintf(fp, "---- platform ----\n");
NEO::IoFunctions::fprintf(fp, " eProductFamily=%d\n", hwInfo.platform.eProductFamily);
NEO::IoFunctions::fprintf(fp, " ePCHProductFamily=%d\n", hwInfo.platform.ePCHProductFamily);
NEO::IoFunctions::fprintf(fp, " eDisplayCoreFamily=%d\n", hwInfo.platform.eDisplayCoreFamily);
NEO::IoFunctions::fprintf(fp, " eRenderCoreFamily=%d\n", hwInfo.platform.eRenderCoreFamily);
NEO::IoFunctions::fprintf(fp, " ePlatformType=%d\n", hwInfo.platform.ePlatformType);
NEO::IoFunctions::fprintf(fp, " usDeviceID=%d\n", hwInfo.platform.usDeviceID);
NEO::IoFunctions::fprintf(fp, " usRevId=%d\n", hwInfo.platform.usRevId);
NEO::IoFunctions::fprintf(fp, " usDeviceID_PCH=%d\n", hwInfo.platform.usDeviceID_PCH);
NEO::IoFunctions::fprintf(fp, " usRevId_PCH=%d\n", hwInfo.platform.usRevId_PCH);
NEO::IoFunctions::fprintf(fp, " eGTType=%d\n", hwInfo.platform.eGTType);
NEO::IoFunctions::fprintf(fp, "---- feature table ----\n");
auto featureTable = safePodCast<const char *>(&hwInfo.featureTable.packed);
for (size_t idx = 0; idx < sizeof(hwInfo.featureTable.packed); idx++) {
NEO::IoFunctions::fprintf(fp, "%02x.", (uint8_t)(featureTable[idx]));
}
NEO::IoFunctions::fprintf(fp, "\n");
NEO::IoFunctions::fprintf(fp, "---- workaround table ----\n");
auto workaroundTable = reinterpret_cast<const char *>(&hwInfo.workaroundTable);
for (size_t idx = 0; idx < sizeof(hwInfo.workaroundTable); idx++) {
NEO::IoFunctions::fprintf(fp, "%02x.", (uint8_t)(workaroundTable[idx]));
}
NEO::IoFunctions::fprintf(fp, "\n");
NEO::IoFunctions::fclosePtr(fp);
}
fp = NEO::IoFunctions::fopenPtr(inputFilePath.c_str(), "w");
if (fp) {
NEO::IoFunctions::fwritePtr(&*input.begin(), input.size(), 1, fp);
NEO::IoFunctions::fclosePtr(fp);
}
}
return stream.str();
}
CompilerCache::CompilerCache(const CompilerCacheConfig &cacheConfig)
: config(cacheConfig){};
std::string CompilerCache::getCachedFilePath(const std::string &cacheFile) {
std::string path = config.cacheDir;
for (int i = 0; i < maxCacheDepth; i++) {
path = joinPath(path, std::string(1, cacheFile[i]));
}
path = joinPath(path, cacheFile);
return path;
}
std::unique_ptr<char[]> CompilerCache::loadCachedBinary(const std::string &kernelFileHash, size_t &cachedBinarySize) {
auto cacheFilename = kernelFileHash + config.cacheFileExtension;
if (cacheFilename.length() < maxCacheDepth + config.cacheFileExtension.length()) {
PRINT_STRING(NEO::debugManager.flags.PrintDebugMessages.get(), stderr, "[Cache failure]: Load cached binary failed - cache file name is too short!\n");
cachedBinarySize = 0;
return nullptr;
}
std::string filePath = getCachedFilePath(cacheFilename);
return loadDataFromFile(filePath.c_str(), cachedBinarySize);
}
bool CompilerCache::getCachedFiles(std::vector<ElementsStruct> &cacheFiles) {
cacheFiles.clear();
auto filterFunction = [](const std::string_view &path) {
return path.find(".cl_cache") != path.npos || path.find(".l0_cache") != path.npos;
};
if (!getFiles(config.cacheDir, filterFunction, cacheFiles)) {
return false;
}
std::sort(cacheFiles.begin(), cacheFiles.end(), [this](const ElementsStruct &a, const ElementsStruct &b) {
return this->compareByLastAccessTime(a, b);
});
return true;
}
} // namespace NEO
|