Skip to content

Commit 095fc23

Browse files
MarijnS95claude
andcommitted
Add CommandBuffer abstraction with backend-specific downcasting
Command buffer creation and management was previously spread across each backend's executeProgram() with no shared interface, making it impossible to manage command buffers from backend-agnostic code. This introduces a CommandBuffer base class on Device so that higher-level code can create and pass around command buffers without knowing the backend. Per-object allocator/pool ownership also prepares for future async execution where multiple command buffers may be in-flight with independent lifetimes. - DX: DXCommandBuffer owns Allocator, CmdList, Fence, Event - VK: VKCommandBuffer owns CmdPool, CmdBuffer; each submission creates a new CommandBuffer for independent lifetime management - MTL: MTLCommandBuffer wraps MTL::CommandBuffer Device::createCommandBuffer() returns Expected<unique_ptr<CommandBuffer>> with a default "not supported" implementation. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 77bccdd commit 095fc23

File tree

5 files changed

+304
-172
lines changed

5 files changed

+304
-172
lines changed

include/API/CommandBuffer.h

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
//===- CommandBuffer.h - Offload Command Buffer API -----------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
//
9+
//
10+
//===----------------------------------------------------------------------===//
11+
12+
#ifndef OFFLOADTEST_API_COMMANDBUFFER_H
13+
#define OFFLOADTEST_API_COMMANDBUFFER_H
14+
15+
#include "API/API.h"
16+
17+
#include <cassert>
18+
19+
namespace offloadtest {
20+
21+
class CommandBuffer {
22+
GPUAPI API;
23+
24+
public:
25+
explicit CommandBuffer(GPUAPI API) : API(API) {}
26+
virtual ~CommandBuffer() = default;
27+
CommandBuffer(const CommandBuffer &) = delete;
28+
CommandBuffer &operator=(const CommandBuffer &) = delete;
29+
30+
GPUAPI getAPI() const { return API; }
31+
32+
template <typename T> T &as() {
33+
assert(API == T::BackendAPI && "CommandBuffer backend mismatch");
34+
return static_cast<T &>(*this);
35+
}
36+
template <typename T> const T &as() const {
37+
assert(API == T::BackendAPI && "CommandBuffer backend mismatch");
38+
return static_cast<const T &>(*this);
39+
}
40+
};
41+
42+
} // namespace offloadtest
43+
44+
#endif // OFFLOADTEST_API_COMMANDBUFFER_H

include/API/Device.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,10 @@
1616

1717
#include "API/API.h"
1818
#include "API/Capabilities.h"
19+
#include "API/CommandBuffer.h"
1920
#include "llvm/ADT/StringRef.h"
2021
#include "llvm/ADT/iterator_range.h"
22+
#include "llvm/Support/Error.h"
2123

2224
#include <memory>
2325
#include <string>
@@ -82,6 +84,12 @@ class Device {
8284
size_t SizeInBytes) = 0;
8385
virtual void printExtra(llvm::raw_ostream &OS) {}
8486

87+
virtual llvm::Expected<std::unique_ptr<CommandBuffer>> createCommandBuffer() {
88+
return llvm::createStringError(
89+
std::errc::not_supported,
90+
"createCommandBuffer not implemented for this backend");
91+
}
92+
8593
virtual ~Device() = 0;
8694

8795
llvm::StringRef getDescription() const { return Description; }

0 commit comments

Comments
 (0)