Files
blazej-smorawski 80989c822c [SPIRV][Offload] Implement DynSharedMemory for L0 (#231160)
This PR implements SLM for Level Zero backend. Every kernel in a module
lowered by the new SPIR-V pass receives a trailing `__local` argument,
including kernels that do not use SLM.

In order to support existing code that uses offload, the PR adds an
additional pass to SPIRV backend. The idea is that the pass is searching
for all extern unsized workgroup arrays and then it converts them to a
global variable that is initialized by an additional kernel argument to
every kernel in the module, including kernels that do not reference the
array.

The pass closes the gap between cuda/hsa and level_zero handling of the
dynamically sized workgroup memory, because level_zero (and openCL) only
support the local memory sized through kernel arguments compared to cuda
which configures it during kernel launch. It was inspired by existing
code in intel's fork of llvm.

Adds two new tests that exercise the local memory in liboffload and
openMP lit. They both follow a similar pattern where the local memory is
used to share data between threads, so we can be certain that no thread
overwrites it. It was necessary, because Intel Graphics Compiler would
sometimes be okay with such uninitialized array and the threads would
use memory that was not allocated for them. Tests such as
`dyn_groupprivate.cpp` are not enabled yet for intel GPUs due to
problems with generic mode.

Assisted by claude.
2026-11-25 14:35:51 +01:00

122 lines
4.3 KiB
C++

//===--- Level Zero Target RTL Implementation -----------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
// GenericKernel implementation for SPIR-V/Xe machine.
//
//===----------------------------------------------------------------------===//
#ifndef OPENMP_LIBOMPTARGET_PLUGINS_NEXTGEN_LEVEL_ZERO_L0KERNEL_H
#define OPENMP_LIBOMPTARGET_PLUGINS_NEXTGEN_LEVEL_ZERO_L0KERNEL_H
#include "L0Defs.h"
#include "L0Trace.h"
#include "PluginInterface.h"
namespace llvm::offload::plugin {
// Forward declarations.
class L0DeviceTy;
class L0ProgramTy;
class L0QueueTy;
class LevelZeroPluginContextTy;
struct L0LaunchEnvTy;
/// Kernel properties.
struct KernelPropertiesTy {
uint32_t Width = 0;
uint32_t SIMDWidth = 0;
uint32_t MaxThreadGroupSize = 0;
uint32_t NumKernelArgs = 0;
uint32_t LocalMemSize = 0;
bool UsesDynamicSharedMem = false;
ze_kernel_indirect_access_flags_t IndirectAccessFlags =
std::numeric_limits<decltype(IndirectAccessFlags)>::max();
std::mutex Mtx;
};
struct L0LaunchEnvTy {
ze_group_count_t GroupCounts = {0, 0, 0};
ze_group_size_t GroupSizes = {0, 0, 0};
KernelPropertiesTy &KernelPR;
bool IsCooperative = false;
void **ArgPtrs = nullptr;
int64_t *ArgSizes = nullptr;
uint32_t NumArgs = 0;
/// Size of the dynamic shared memory, passed as a trailing `__local`
/// argument if the kernel takes one.
size_t DynSharedMemorySize = 0;
bool HasDynSharedMemoryArg = false;
std::unique_lock<std::mutex> Lock;
L0LaunchEnvTy(KernelPropertiesTy &KernelPR,
const KernelLaunchArgsTy &LaunchArgs)
: KernelPR(KernelPR), IsCooperative(LaunchArgs.Flags.Cooperative),
ArgPtrs(LaunchArgs.Args), ArgSizes(LaunchArgs.ArgSizes),
NumArgs(LaunchArgs.NumArgs),
DynSharedMemorySize(LaunchArgs.DynCGroupMem),
Lock(KernelPR.Mtx, std::defer_lock) {}
};
class L0KernelTy : public GenericKernelTy {
// L0 Kernel Handle.
ze_kernel_handle_t zeKernel;
// Kernel Properties.
mutable KernelPropertiesTy Properties;
Error buildKernel(L0ProgramTy &Program);
Error readKernelProperties(L0ProgramTy &Program);
ze_group_size_t createKernelGroups(L0DeviceTy &L0Device, L0LaunchEnvTy &KEnv,
uint32_t NumThreads[3],
uint32_t NumBlocks[3]) const;
Error setIndirectFlags(L0DeviceTy &L0Device,
LevelZeroPluginContextTy &UserCtx,
L0LaunchEnvTy &KEnv) const;
public:
/// Create a L0 kernel with a name and an execution mode.
L0KernelTy(StringRef Name) : GenericKernelTy(Name), zeKernel(nullptr) {}
~L0KernelTy() = default;
L0KernelTy(const L0KernelTy &) = delete;
L0KernelTy(L0KernelTy &&) = delete;
L0KernelTy &operator=(const L0KernelTy &) = delete;
L0KernelTy &operator=(const L0KernelTy &&) = delete;
KernelPropertiesTy &getProperties() const { return Properties; }
/// Initialize the L0 kernel.
Error initImpl(GenericDeviceTy &GenericDevice, DeviceImageTy &Image) override;
/// Launch the L0 kernel function.
Error launchImpl(GenericDeviceTy &GenericDevice, uint32_t NumThreads[3],
uint32_t NumBlocks[3], uint32_t DynBlockMemSize,
KernelLaunchArgsTy &LaunchArgs,
AsyncInfoWrapperTy &AsyncInfoWrapper,
GenericProfilerTy *ProfilerPtr) const override;
Error deinit() {
CALL_ZE_RET_ERROR(zeKernelDestroy, zeKernel);
return Plugin::success();
}
Expected<uint64_t> maxGroupSize(GenericDeviceTy &GenericDevice,
uint64_t DynamicMemSize) const override {
return Plugin::error(ErrorCode::UNIMPLEMENTED,
"maxGroupSize not implemented yet");
}
Expected<uint32_t>
getMaxCooperativeGroupCount(GenericDeviceTy &GenericDevice,
const uint32_t NumThreads[3],
uint32_t DynBlockMemSize) const override;
ze_kernel_handle_t getZeKernel() const { return zeKernel; }
};
} // namespace llvm::offload::plugin
#endif // OPENMP_LIBOMPTARGET_PLUGINS_NEXTGEN_LEVEL_ZERO_L0KERNEL_H