[llvm] [Offload][L0][NFC] Remove staging buffers dead-code (PR #227979)

via llvm-commits llvm-commits at lists.llvm.org
Thu Oct 1 00:04:36 PDT 2026


llvmorg-github-actions[bot] wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-offload

Author: Alex Duran (adurang)

<details>
<summary>Changes</summary>

Async queues were the only user of Staging Buffers and as they were removed this is now dead code we can remove.

---
Full diff: https://github.com/llvm/llvm-project/pull/227979.diff


10 Files Affected:

- (modified) offload/plugins-nextgen/level_zero/include/L0Context.h (-22) 
- (modified) offload/plugins-nextgen/level_zero/include/L0Defs.h (-4) 
- (modified) offload/plugins-nextgen/level_zero/include/L0Device.h (-2) 
- (modified) offload/plugins-nextgen/level_zero/include/L0Memory.h (-86) 
- (modified) offload/plugins-nextgen/level_zero/include/L0Options.h (-6) 
- (modified) offload/plugins-nextgen/level_zero/include/L0Plugin.h (-7) 
- (modified) offload/plugins-nextgen/level_zero/src/L0Context.cpp (-10) 
- (modified) offload/plugins-nextgen/level_zero/src/L0Device.cpp (-2) 
- (modified) offload/plugins-nextgen/level_zero/src/L0Options.cpp (-13) 
- (modified) offload/plugins-nextgen/level_zero/src/L0Plugin.cpp (-2) 


``````````diff
diff --git a/offload/plugins-nextgen/level_zero/include/L0Context.h b/offload/plugins-nextgen/level_zero/include/L0Context.h
index 6f243cbdb4d13..274885cdd589d 100644
--- a/offload/plugins-nextgen/level_zero/include/L0Context.h
+++ b/offload/plugins-nextgen/level_zero/include/L0Context.h
@@ -17,7 +17,6 @@
 #include "L0Compat.h"
 #include "L0Event.h"
 #include "L0Memory.h"
-#include "PerThreadTable.h"
 #include "level_zero/ze_api.h"
 
 namespace llvm::omp::target::plugin {
@@ -25,16 +24,6 @@ namespace llvm::omp::target::plugin {
 class LevelZeroPluginTy;
 class LevelZeroPluginContextTy;
 
-class L0ContextTLSTy {
-  StagingBufferTy StagingBuffer;
-
-public:
-  StagingBufferTy &getStagingBuffer() { return StagingBuffer; }
-  const StagingBufferTy &getStagingBuffer() const { return StagingBuffer; }
-
-  Error deinit() { return StagingBuffer.clear(); }
-};
-
 // Helper for managing Level Zero APIs.
 // It provides two interfaces - by default it tries to call the function
 // directly - either through dlopen or directly linked (see L0DynWrapper.cpp).
@@ -90,15 +79,6 @@ class ZeDispatcher {
   decltype(Fn) FuncPtr = nullptr;
 };
 
-struct L0ContextTLSTableTy
-    : public PerThreadContainer<
-          std::unordered_map<ze_context_handle_t, L0ContextTLSTy>> {
-  Error deinit() {
-    return PerThreadTable::deinit(
-        [](L0ContextTLSTy &Entry) -> auto { return Entry.deinit(); });
-  }
-};
-
 /// Driver and context-specific resources. We assume a single context per
 /// driver.
 class L0ContextTy {
@@ -157,8 +137,6 @@ class L0ContextTy {
 
   LevelZeroPluginTy &getPlugin() const { return Plugin; }
 
-  StagingBufferTy &getStagingBuffer();
-
   /// Add imported external pointer region.
   void addImported(void *Ptr, size_t Size) {
     (void)ImportedPtrs.try_emplace(reinterpret_cast<uintptr_t>(Ptr), Size);
diff --git a/offload/plugins-nextgen/level_zero/include/L0Defs.h b/offload/plugins-nextgen/level_zero/include/L0Defs.h
index 1c616b93b779f..a79e3c7ea311f 100644
--- a/offload/plugins-nextgen/level_zero/include/L0Defs.h
+++ b/offload/plugins-nextgen/level_zero/include/L0Defs.h
@@ -27,10 +27,6 @@ namespace llvm::omp::target::plugin {
 
 /// Default alignmnet for allocation.
 constexpr size_t L0DefaultAlignment = 0;
-/// Default staging buffer size for host to device copy (16KB).
-constexpr size_t L0StagingBufferSize = (1 << 14);
-/// Default staging buffer count.
-constexpr size_t L0StagingBufferCount = 64;
 /// USM allocation threshold where preallocation does not pay off (128MB).
 constexpr size_t L0UsmPreAllocThreshold = (128 << 20);
 /// Host USM allocation threshold where preallocation does not pay off (8MB).
diff --git a/offload/plugins-nextgen/level_zero/include/L0Device.h b/offload/plugins-nextgen/level_zero/include/L0Device.h
index 3341f261deaf9..5df5422d4c7d0 100644
--- a/offload/plugins-nextgen/level_zero/include/L0Device.h
+++ b/offload/plugins-nextgen/level_zero/include/L0Device.h
@@ -467,8 +467,6 @@ class L0DeviceTy final : public GenericDeviceTy {
     return L0Context.getEventPool().releaseEventObject(EventObj);
   }
 
-  StagingBufferTy &getStagingBuffer() { return L0Context.getStagingBuffer(); }
-
   bool supportsLargeMem() const { return L0Context.supportsLargeMem(); }
 
   /// Returns the Queue from an async info object, or creates a new one if
diff --git a/offload/plugins-nextgen/level_zero/include/L0Memory.h b/offload/plugins-nextgen/level_zero/include/L0Memory.h
index db7623afbb63e..32bb0a5dc288c 100644
--- a/offload/plugins-nextgen/level_zero/include/L0Memory.h
+++ b/offload/plugins-nextgen/level_zero/include/L0Memory.h
@@ -437,92 +437,6 @@ class MemAllocatorTy {
   }
 }; /// MemAllocatorTy
 
-/// Staging buffer.
-/// A single staging buffer is not enough when batching is enabled since there
-/// can be multiple pending copy operations.
-class StagingBufferTy {
-  /// Context for L0 calls.
-  ze_context_handle_t Context = nullptr;
-  /// Max allowed size for staging buffer.
-  size_t Size = L0StagingBufferSize;
-  /// Number of buffers allocated together.
-  size_t Count = L0StagingBufferCount;
-  /// Buffers increasing by Count if a new buffer is required.
-  llvm::SmallVector<void *> Buffers;
-  /// Next buffer location in the buffers.
-  size_t Offset = 0;
-
-  Expected<void *> addBuffers() {
-    ze_host_mem_alloc_desc_t AllocDesc{ZE_STRUCTURE_TYPE_HOST_MEM_ALLOC_DESC,
-                                       nullptr, 0};
-    void *Ret = nullptr;
-    size_t AllocSize = Size * Count;
-    CALL_ZE_RET_ERROR(zeMemAllocHost, Context, &AllocDesc, AllocSize,
-                      L0DefaultAlignment, &Ret);
-    Buffers.push_back(Ret);
-    return Ret;
-  }
-
-public:
-  StagingBufferTy() = default;
-  StagingBufferTy(const StagingBufferTy &) = delete;
-  StagingBufferTy(StagingBufferTy &&) = delete;
-  StagingBufferTy &operator=(const StagingBufferTy &) = delete;
-  StagingBufferTy &operator=(const StagingBufferTy &&) = delete;
-  ~StagingBufferTy() = default;
-
-  Error clear() {
-    for (auto *Ptr : Buffers)
-      CALL_ZE_RET_ERROR(zeMemFree, Context, Ptr);
-    Context = nullptr;
-    return Plugin::success();
-  }
-
-  bool initialized() const { return Context != nullptr; }
-
-  void init(ze_context_handle_t ContextIn, size_t SizeIn, size_t CountIn) {
-    Context = ContextIn;
-    Size = SizeIn;
-    Count = CountIn;
-  }
-
-  void reset() { Offset = 0; }
-
-  /// Always return the first buffer.
-  Expected<void *> get() {
-    if (Size == 0 || Count == 0)
-      return nullptr;
-    return Buffers.empty() ? addBuffers() : Buffers.front();
-  }
-
-  /// Return the next available buffer.
-  Expected<void *> getNext() {
-    void *Ret = nullptr;
-    if (Size == 0 || Count == 0)
-      return Ret;
-
-    size_t AllocSize = Size * Count;
-    bool NeedToGrow = Buffers.empty() || Offset >= Buffers.size() * AllocSize;
-    if (NeedToGrow) {
-      auto PtrOrErr = addBuffers();
-      if (!PtrOrErr)
-        return PtrOrErr.takeError();
-      Ret = *PtrOrErr;
-    } else
-      Ret = reinterpret_cast<void *>(
-          reinterpret_cast<uintptr_t>(Buffers.back()) + (Offset % AllocSize));
-
-    if (!Ret)
-      return nullptr;
-
-    Offset += Size;
-    return Ret;
-  }
-
-  /// Return either a fixed buffer or next buffer.
-  Expected<void *> get(bool Next) { return Next ? getNext() : get(); }
-};
-
 } // namespace llvm::omp::target::plugin
 
 #endif // OPENMP_LIBOMPTARGET_PLUGINS_NEXTGEN_LEVEL_ZERO_L0MEMORY_H
diff --git a/offload/plugins-nextgen/level_zero/include/L0Options.h b/offload/plugins-nextgen/level_zero/include/L0Options.h
index 2ac5b9399d18d..0febd9acc1960 100644
--- a/offload/plugins-nextgen/level_zero/include/L0Options.h
+++ b/offload/plugins-nextgen/level_zero/include/L0Options.h
@@ -73,12 +73,6 @@ struct L0OptionsTy {
   /// Binary flags.
   L0OptionFlagsTy Flags;
 
-  /// Staging buffer size.
-  size_t StagingBufferSize = L0StagingBufferSize;
-
-  /// Staging buffer count.
-  size_t StagingBufferCount = L0StagingBufferCount;
-
   struct MemPoolConfigTy {
     bool Use;
     int32_t AllocMax;
diff --git a/offload/plugins-nextgen/level_zero/include/L0Plugin.h b/offload/plugins-nextgen/level_zero/include/L0Plugin.h
index d6656cb4cf792..03d187b7cd91e 100644
--- a/offload/plugins-nextgen/level_zero/include/L0Plugin.h
+++ b/offload/plugins-nextgen/level_zero/include/L0Plugin.h
@@ -87,9 +87,6 @@ class LevelZeroPluginTy final : public GenericPluginTy {
   /// Context (and Driver) specific data.
   std::list<L0ContextTy> ContextList;
 
-  // Table containing per-thread information for each Context using TLS.
-  L0ContextTLSTableTy ContextTLSTable;
-
   /// L0 plugin options.
   L0OptionsTy Options;
 
@@ -101,10 +98,6 @@ class LevelZeroPluginTy final : public GenericPluginTy {
   LevelZeroPluginTy() : GenericPluginTy(getTripleArch()) {}
   virtual ~LevelZeroPluginTy() = default;
 
-  L0ContextTLSTy &getContextTLS(ze_context_handle_t Context) {
-    return ContextTLSTable.get(Context);
-  }
-
   const L0OptionsTy &getOptions() { return Options; }
 
   const L0DeviceTy &getDeviceFromId(int32_t DeviceId) const {
diff --git a/offload/plugins-nextgen/level_zero/src/L0Context.cpp b/offload/plugins-nextgen/level_zero/src/L0Context.cpp
index 5899611d6c7d5..526a25c970c61 100644
--- a/offload/plugins-nextgen/level_zero/src/L0Context.cpp
+++ b/offload/plugins-nextgen/level_zero/src/L0Context.cpp
@@ -159,14 +159,4 @@ Error L0ContextTy::deinit() {
   return Plugin::success();
 }
 
-StagingBufferTy &L0ContextTy::getStagingBuffer() {
-  auto &TLS = Plugin.getContextTLS(getZeContext());
-  auto &Buffer = TLS.getStagingBuffer();
-  const auto &Options = Plugin.getOptions();
-  if (!Buffer.initialized())
-    Buffer.init(getZeContext(), Options.StagingBufferSize,
-                Options.StagingBufferCount);
-  return Buffer;
-}
-
 } // namespace llvm::omp::target::plugin
diff --git a/offload/plugins-nextgen/level_zero/src/L0Device.cpp b/offload/plugins-nextgen/level_zero/src/L0Device.cpp
index e41268d7eb843..95d87cca55657 100644
--- a/offload/plugins-nextgen/level_zero/src/L0Device.cpp
+++ b/offload/plugins-nextgen/level_zero/src/L0Device.cpp
@@ -292,7 +292,6 @@ Error L0DeviceTy::synchronizeImpl(__tgt_async_info &AsyncInfo,
 
   if (ReleaseQueue) {
     releaseQueue(Queue);
-    getStagingBuffer().reset();
     AsyncInfo.Queue = nullptr;
   }
 
@@ -327,7 +326,6 @@ Error L0DeviceTy::queryAsyncImpl(__tgt_async_info &AsyncInfo, bool ReleaseQueue,
 
   if (ReleaseQueue) {
     releaseQueue(Queue);
-    getStagingBuffer().reset();
     AsyncInfo.Queue = nullptr;
   }
 
diff --git a/offload/plugins-nextgen/level_zero/src/L0Options.cpp b/offload/plugins-nextgen/level_zero/src/L0Options.cpp
index 01c62173ee719..21a666a1141fe 100644
--- a/offload/plugins-nextgen/level_zero/src/L0Options.cpp
+++ b/offload/plugins-nextgen/level_zero/src/L0Options.cpp
@@ -146,19 +146,6 @@ void L0OptionsTy::processEnvironmentVars() {
     CommonSpecConstants.addConstant<char>(0xFF747469, 1);
   }
 
-  // LIBOMPTARGET_LEVEL_ZERO_STAGING_BUFFER_SIZE=<SizeInKB>.
-  const Envar<size_t> StagingBufferSizeVar(
-      "LIBOMPTARGET_LEVEL_ZERO_STAGING_BUFFER_SIZE");
-  if (StagingBufferSizeVar.isPresent()) {
-    size_t SizeInKB = StagingBufferSizeVar;
-    if (SizeInKB > (16 << 10)) {
-      SizeInKB = (16 << 10);
-      ODBG(OLDT_Init) << "Staging buffer size is capped at " << SizeInKB
-                      << " KB";
-    }
-    StagingBufferSize = SizeInKB << 10;
-  }
-
   // LIBOMPTARGET_LEVEL_ZERO_COMMAND_MODE=<Fmt>.
   // <Fmt> := inorder | sync
   // inorder: use level zero command queue in-order mode. This is the default.
diff --git a/offload/plugins-nextgen/level_zero/src/L0Plugin.cpp b/offload/plugins-nextgen/level_zero/src/L0Plugin.cpp
index a09ca28ad6df8..6c0042d6f370f 100644
--- a/offload/plugins-nextgen/level_zero/src/L0Plugin.cpp
+++ b/offload/plugins-nextgen/level_zero/src/L0Plugin.cpp
@@ -134,8 +134,6 @@ Expected<int32_t> LevelZeroPluginTy::initImpl() {
 
 Error LevelZeroPluginTy::deinitImpl() {
   ODBG(OLDT_Deinit) << "Deinit Level0 plugin!";
-  if (auto Err = ContextTLSTable.deinit())
-    return Err;
   for (auto &Context : ContextList)
     if (auto Err = Context.deinit())
       return Err;

``````````

</details>


https://github.com/llvm/llvm-project/pull/227979


More information about the llvm-commits mailing list