Skip to content
Merged
Show file tree
Hide file tree
Changes from 27 commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
5c5cf8f
initial commit, retrieval not working yet. several todos pending
cperkinsintel Sep 18, 2024
0d3ac02
s.b. working, but seeing an error ... investigating
cperkinsintel Sep 19, 2024
e4da64d
guards
cperkinsintel Sep 24, 2024
30a11bb
zomg working
cperkinsintel Sep 25, 2024
12e12a7
code reorg, step 1
cperkinsintel Sep 26, 2024
194b518
more reorg, testing
cperkinsintel Sep 27, 2024
8844e20
last minute clean up
cperkinsintel Sep 27, 2024
f054c26
resolve merge conflicts
cperkinsintel Sep 27, 2024
4e4b93e
fix for spirv
cperkinsintel Sep 28, 2024
ae2e98a
forgot to qualify leak check for windows
cperkinsintel Sep 28, 2024
126e39a
all OCL tests should declare that ocloc dependency
cperkinsintel Sep 28, 2024
01fe513
GCC<8 support
cperkinsintel Oct 1, 2024
19c1f08
reviewer feedback
cperkinsintel Oct 1, 2024
7177dca
Merge branch 'sycl' into cperkins-kernel_compiler-sycl-cache
cperkinsintel Oct 10, 2024
e63561d
resolve merge conflict
cperkinsintel Oct 23, 2024
a571185
multiple devices supported for kernel_compiler caching
cperkinsintel Oct 23, 2024
49a3828
multiple devices
cperkinsintel Oct 25, 2024
9841c43
clang-format fighting with itself?
cperkinsintel Oct 25, 2024
31d790d
Merge branch 'sycl' into cperkins-kernel_compiler-sycl-cache
cperkinsintel Oct 25, 2024
9d646a7
resolve merge conflicts
cperkinsintel Oct 28, 2024
3340a23
finish multi-device support
cperkinsintel Oct 29, 2024
b4e8069
Merge branch 'sycl' into cperkins-kernel_compiler-sycl-cache
cperkinsintel Oct 29, 2024
e5af452
updated for new cache_trace env, added cache testing to sycl_jit
cperkinsintel Oct 29, 2024
a57c665
comments and cleanup
cperkinsintel Oct 29, 2024
5bbceb7
clang-format can go f..ree itself
cperkinsintel Oct 29, 2024
10d918b
update unsupported test
cperkinsintel Oct 30, 2024
dc7d2a7
unsupported-INTENDED
cperkinsintel Oct 30, 2024
a3d9f96
committed-tracker and other sins
cperkinsintel Nov 1, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
170 changes: 120 additions & 50 deletions sycl/source/detail/kernel_bundle_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include <detail/kernel_compiler/kernel_compiler_opencl.hpp>
#include <detail/kernel_compiler/kernel_compiler_sycl.hpp>
#include <detail/kernel_impl.hpp>
#include <detail/persistent_device_code_cache.hpp>
#include <detail/program_manager/program_manager.hpp>
#include <sycl/backend_types.hpp>
#include <sycl/context.hpp>
Expand Down Expand Up @@ -396,6 +397,53 @@ class kernel_bundle_impl {
return SS.str();
}

bool
extKernelCompilerFetchFromCache(const std::vector<device> Devices,
const std::vector<std::string> &BuildOptions,
const std::string &SourceStr,
ur_program_handle_t &UrProgram) {
using ContextImplPtr = std::shared_ptr<sycl::detail::context_impl>;
ContextImplPtr ContextImpl = getSyclObjImpl(MContext);
const AdapterPtr &Adapter = ContextImpl->getAdapter();

std::string UserArgs = syclex::detail::userArgsAsString(BuildOptions);

std::vector<ur_device_handle_t> DeviceHandles;
std::transform(
Devices.begin(), Devices.end(), std::back_inserter(DeviceHandles),
[](const device &Dev) { return getSyclObjImpl(Dev)->getHandleRef(); });

std::vector<const uint8_t *> Binaries;
std::vector<size_t> Lengths;
std::vector<std::vector<std::vector<char>>> PersistentBinaries;
for (size_t i = 0; i < Devices.size(); i++) {
std::vector<std::vector<char>> BinProg =
PersistentDeviceCodeCache::getCompiledKernelFromDisc(
Devices[i], UserArgs, SourceStr);

// exit if any device binary is missing
if (BinProg.empty()) {
return false;
}
PersistentBinaries.push_back(BinProg);

Binaries.push_back((uint8_t *)(PersistentBinaries[i][0].data()));
Lengths.push_back(PersistentBinaries[i][0].size());
}

ur_program_properties_t Properties = {};
Properties.stype = UR_STRUCTURE_TYPE_PROGRAM_PROPERTIES;
Properties.pNext = nullptr;
Properties.count = 0;
Properties.pMetadatas = nullptr;

Adapter->call<UrApiKind::urProgramCreateWithBinary>(
ContextImpl->getHandleRef(), DeviceHandles.size(), DeviceHandles.data(),
Lengths.data(), Binaries.data(), &Properties, &UrProgram);

return true;
}

std::shared_ptr<kernel_bundle_impl>
build_from_source(const std::vector<device> Devices,
const std::vector<std::string> &BuildOptions,
Expand All @@ -415,57 +463,68 @@ class kernel_bundle_impl {
DeviceVec.push_back(Dev);
}

const auto spirv = [&]() -> std::vector<uint8_t> {
if (Language == syclex::source_language::opencl) {
// if successful, the log is empty. if failed, throws an error with the
// compilation log.
const auto &SourceStr = std::get<std::string>(this->Source);
std::vector<uint32_t> IPVersionVec(Devices.size());
std::transform(DeviceVec.begin(), DeviceVec.end(), IPVersionVec.begin(),
[&](ur_device_handle_t d) {
uint32_t ipVersion = 0;
Adapter->call<UrApiKind::urDeviceGetInfo>(
d, UR_DEVICE_INFO_IP_VERSION, sizeof(uint32_t),
&ipVersion, nullptr);
return ipVersion;
});
return syclex::detail::OpenCLC_to_SPIRV(SourceStr, IPVersionVec,
BuildOptions, LogPtr);
}
if (Language == syclex::source_language::spirv) {
const auto &SourceBytes =
std::get<std::vector<std::byte>>(this->Source);
std::vector<uint8_t> Result(SourceBytes.size());
std::transform(SourceBytes.cbegin(), SourceBytes.cend(), Result.begin(),
[](std::byte B) { return static_cast<uint8_t>(B); });
return Result;
}
if (Language == syclex::source_language::sycl) {
const auto &SourceStr = std::get<std::string>(this->Source);
return syclex::detail::SYCL_to_SPIRV(SourceStr, IncludePairs,
BuildOptions, LogPtr,
RegisteredKernelNames);
}
if (Language == syclex::source_language::sycl_jit) {
const auto &SourceStr = std::get<std::string>(this->Source);
return syclex::detail::SYCL_JIT_to_SPIRV(SourceStr, IncludePairs,
BuildOptions, LogPtr,
RegisteredKernelNames);
}
throw sycl::exception(
make_error_code(errc::invalid),
"OpenCL C and SPIR-V are the only supported languages at this time");
}();

ur_program_handle_t UrProgram = nullptr;
Adapter->call<UrApiKind::urProgramCreateWithIL>(ContextImpl->getHandleRef(),
spirv.data(), spirv.size(),
nullptr, &UrProgram);
// program created by urProgramCreateWithIL is implicitly retained.
if (UrProgram == nullptr)
throw sycl::exception(
sycl::make_error_code(errc::invalid),
"urProgramCreateWithIL resulted in a null program handle.");
// SourceStrPtr will be null when source is Spir-V bytes.
const std::string *SourceStrPtr = std::get_if<std::string>(&this->Source);
bool FetchedFromCache = false;
if (PersistentDeviceCodeCache::isEnabled() && SourceStrPtr) {
FetchedFromCache = extKernelCompilerFetchFromCache(
Devices, BuildOptions, *SourceStrPtr, UrProgram);
}

if (!FetchedFromCache) {
const auto spirv = [&]() -> std::vector<uint8_t> {
if (Language == syclex::source_language::opencl) {
// if successful, the log is empty. if failed, throws an error with
// the compilation log.
std::vector<uint32_t> IPVersionVec(Devices.size());
std::transform(DeviceVec.begin(), DeviceVec.end(),
IPVersionVec.begin(), [&](ur_device_handle_t d) {
uint32_t ipVersion = 0;
Adapter->call<UrApiKind::urDeviceGetInfo>(
d, UR_DEVICE_INFO_IP_VERSION, sizeof(uint32_t),
&ipVersion, nullptr);
return ipVersion;
});
return syclex::detail::OpenCLC_to_SPIRV(*SourceStrPtr, IPVersionVec,
BuildOptions, LogPtr);
}
if (Language == syclex::source_language::spirv) {
const auto &SourceBytes =
std::get<std::vector<std::byte>>(this->Source);
std::vector<uint8_t> Result(SourceBytes.size());
std::transform(SourceBytes.cbegin(), SourceBytes.cend(),
Result.begin(),
[](std::byte B) { return static_cast<uint8_t>(B); });
return Result;
}
if (Language == syclex::source_language::sycl) {
return syclex::detail::SYCL_to_SPIRV(*SourceStrPtr, IncludePairs,
BuildOptions, LogPtr,
RegisteredKernelNames);
}
if (Language == syclex::source_language::sycl_jit) {
const auto &SourceStr = std::get<std::string>(this->Source);
return syclex::detail::SYCL_JIT_to_SPIRV(SourceStr, IncludePairs,
BuildOptions, LogPtr,
RegisteredKernelNames);
}
throw sycl::exception(
make_error_code(errc::invalid),
"SYCL C++, OpenCL C and SPIR-V are the only supported "
"languages at this time");
}();

Adapter->call<UrApiKind::urProgramCreateWithIL>(
ContextImpl->getHandleRef(), spirv.data(), spirv.size(), nullptr,
&UrProgram);
// program created by urProgramCreateWithIL is implicitly retained.
if (UrProgram == nullptr)
throw sycl::exception(
sycl::make_error_code(errc::invalid),
"urProgramCreateWithIL resulted in a null program handle.");

} // if(!FetchedFromCache)

std::string XsFlags = extractXsFlags(BuildOptions);
auto Res = Adapter->call_nocheck<UrApiKind::urProgramBuildExp>(
Expand Down Expand Up @@ -501,6 +560,17 @@ class kernel_bundle_impl {
nullptr, MContext, MDevices, bundle_state::executable, KernelIDs,
UrProgram);
device_image_plain DevImg{DevImgImpl};

// If caching enabled and kernel not fetched from cache, cache.
if (PersistentDeviceCodeCache::isEnabled() && !FetchedFromCache &&
SourceStrPtr) {
for (const auto &Device : Devices) {
PersistentDeviceCodeCache::putCompiledKernelToDisc(
Device, syclex::detail::userArgsAsString(BuildOptions),
*SourceStrPtr, UrProgram);
}
}

return std::make_shared<kernel_bundle_impl>(MContext, MDevices, DevImg,
KernelNames, Language);
}
Expand Down
9 changes: 9 additions & 0 deletions sycl/source/detail/kernel_compiler/kernel_compiler_sycl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,15 @@ SYCL_to_SPIRV(const std::string &SYCLSource, include_pairs_t IncludePairs,
throw sycl::exception(sycl::errc::build,
"kernel_compiler does not support GCC<8");
}

std::string userArgsAsString(const std::vector<std::string> &UserArguments) {
return std::accumulate(UserArguments.begin(), UserArguments.end(),
std::string(""),
[](const std::string &A, const std::string &B) {
return A.empty() ? B : A + " " + B;
});
}

} // namespace detail
} // namespace ext::oneapi::experimental
} // namespace _V1
Expand Down
2 changes: 2 additions & 0 deletions sycl/source/detail/kernel_compiler/kernel_compiler_sycl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ SYCL_to_SPIRV(const std::string &Source, include_pairs_t IncludePairs,

bool SYCL_Compilation_Available();

std::string userArgsAsString(const std::vector<std::string> &UserArguments);

spirv_vec_t
SYCL_JIT_to_SPIRV(const std::string &Source, include_pairs_t IncludePairs,
const std::vector<std::string> &UserArgs, std::string *LogPtr,
Expand Down
Loading
Loading