upleb.uk

Public git repos — served from a NIP-34 GRASP relay at git.upleb.uk

summaryrefslogtreecommitdiff
path: root/runtime
diff options
context:
space:
mode:
authorDadaCap <167002618+DadaCap@users.noreply.github.com>2026-11-29 04:35:16 +0100
committerGitHub <noreply@github.com>2026-11-29 04:35:16 +0100
commit3ecd31707fdfab5e398fc2ad0ff86d992aefdb8a (patch)
tree839cc2caa09496f1b2f40f0db15b6bcf66e57584 /runtime
parentc8bf0e7b6568f471b42540010e335ca6413d4c58 (diff)
parent0267290f9c7ec35bece4e0b81a132f7d8310e4e6 (diff)
Merge pull request #1788 from IlyassRS3/fix/rt-gpu-label-memory-expand
runtime: preserve in-flight GPU label targets when expanding a non-heap region (RT-GPU-LABEL-MEMORY-EXPAND)
Diffstat (limited to 'runtime')
-rw-r--r--runtime/CMakeLists.txt5
-rw-r--r--runtime/src/gpu_label_memory.h10
-rw-r--r--runtime/test/gpu_label_memory_test.cpp194
3 files changed, 207 insertions, 2 deletions
diff --git a/runtime/CMakeLists.txt b/runtime/CMakeLists.txt
index d0eb56f28..20a620201 100644
--- a/runtime/CMakeLists.txt
+++ b/runtime/CMakeLists.txt
@@ -1613,6 +1613,11 @@ if(BUILD_TESTING)
1613 endif() 1613 endif()
1614 add_executable(command_list_test test/command_list_test.cpp) 1614 add_executable(command_list_test test/command_list_test.cpp)
1615 add_test(NAME command_list COMMAND command_list_test) 1615 add_test(NAME command_list COMMAND command_list_test)
1616 add_executable(gpu_label_memory_test test/gpu_label_memory_test.cpp)
1617 if(WIN32 AND MSVC)
1618 target_compile_options(gpu_label_memory_test PRIVATE /W4 /permissive- /utf-8)
1619 endif()
1620 add_test(NAME gpu_label_memory COMMAND gpu_label_memory_test)
1616 # Which SPIR-V modules declare a storage image or buffer they may write (src/spirv_stores.h), on hand-built words. 1621 # Which SPIR-V modules declare a storage image or buffer they may write (src/spirv_stores.h), on hand-built words.
1617 add_executable(spirv_stores_test test/spirv_stores_test.cpp) 1622 add_executable(spirv_stores_test test/spirv_stores_test.cpp)
1618 add_test(NAME spirv_stores COMMAND spirv_stores_test) 1623 add_test(NAME spirv_stores COMMAND spirv_stores_test)
diff --git a/runtime/src/gpu_label_memory.h b/runtime/src/gpu_label_memory.h
index ac0c53591..9ad7c1d75 100644
--- a/runtime/src/gpu_label_memory.h
+++ b/runtime/src/gpu_label_memory.h
@@ -35,8 +35,14 @@ class Memory {
35 auto previous = regions_.upper_bound(base); 35 auto previous = regions_.upper_bound(base);
36 if (!heap && previous != regions_.begin()) { 36 if (!heap && previous != regions_.begin()) {
37 --previous; 37 --previous;
38 const auto &r = *previous->second; 38 auto &r = *previous->second;
39 if (!r.heap && r.allocation_base == allocation_base && base >= r.begin && end <= r.end) return; 39 if (!r.heap && r.allocation_base == allocation_base && base >= r.begin) {
40 if (end <= r.end) return;
41 if (base == r.begin) {
42 r.end = end;
43 return;
44 }
45 }
40 } 46 }
41 regions_[base] = std::make_shared<Region>(Region{base, end, allocation_base, heap}); 47 regions_[base] = std::make_shared<Region>(Region{base, end, allocation_base, heap});
42 } 48 }
diff --git a/runtime/test/gpu_label_memory_test.cpp b/runtime/test/gpu_label_memory_test.cpp
new file mode 100644
index 000000000..db485ba3d
--- /dev/null
+++ b/runtime/test/gpu_label_memory_test.cpp
@@ -0,0 +1,194 @@
1// Portable unit and concurrency tests for leonida::label::Memory (src/gpu_label_memory.h).
2// Covers in-place non-heap region expansion (preserving in-flight GPU completion value writes),
3// partial vs entire page release generation invalidation, heap vs non-heap isolation, base-0 GPU arena
4// tracking, alignment/overflow bounds, and concurrent track/capture/publish. No Windows/Vulkan/game data.
5#include "../src/gpu_label_memory.h"
6
7#include <atomic>
8#include <cstdint>
9#include <cstdio>
10#include <limits>
11#include <thread>
12#include <vector>
13
14#define EXPECT(condition) do { \
15 if (!(condition)) { std::fprintf(stderr, "line %d: %s\n", __LINE__, #condition); return 1; } \
16} while (0)
17
18namespace {
19
20using leonida::label::Memory;
21using leonida::label::Target;
22
23bool publish_ok(Memory& mem, const Target& target, std::uintptr_t expected_addr, unsigned expected_bytes,
24 std::uintptr_t expected_alloc) {
25 bool called = false;
26 const bool ok = mem.publish(target, [&](std::uintptr_t addr, unsigned bytes, std::uintptr_t alloc) {
27 called = true;
28 return addr == expected_addr && bytes == expected_bytes && alloc == expected_alloc;
29 });
30 return ok && called;
31}
32
33bool publish_rejected(Memory& mem, const Target& target) {
34 bool called = false;
35 const bool ok = mem.publish(target, [&](std::uintptr_t, unsigned, std::uintptr_t) {
36 called = true;
37 return true;
38 });
39 return !ok && !called;
40}
41
42int test_expand_preserves_inflight_targets() {
43 Memory mem;
44 constexpr std::uintptr_t kBase = 0x100000;
45 constexpr std::uintptr_t kAlloc = 0x100000;
46 mem.track(kBase, 0x1000, kAlloc, false);
47
48 const Target t4 = mem.capture(kBase + 0x10, 4);
49 const Target t8 = mem.capture(kBase + 0x20, 8);
50 EXPECT(t4.owner.lock() != nullptr && t8.owner.lock() != nullptr);
51 EXPECT(mem.capture(kBase + 0x1000, 8).owner.lock() == nullptr);
52
53 // Growing the same non-heap region at the same base and allocation_base must update Region::end in place
54 // so in-flight Targets remain valid instead of being silently dropped.
55 mem.track(kBase, 0x4000, kAlloc, false);
56 EXPECT(publish_ok(mem, t4, kBase + 0x10, 4, kAlloc));
57 EXPECT(publish_ok(mem, t8, kBase + 0x20, 8, kAlloc));
58
59 // The newly covered tail is immediately capturable and publishable too.
60 const Target t_tail = mem.capture(kBase + 0x3ff8, 8);
61 EXPECT(publish_ok(mem, t_tail, kBase + 0x3ff8, 8, kAlloc));
62
63 // Partial release bumps generation: pre-release targets become stale, post-release targets survive further
64 // in-place expansion while the pre-release stale target remains rejected.
65 EXPECT(mem.release_pages(kBase, 0x1000, false, [] { return true; }));
66 EXPECT(publish_rejected(mem, t4));
67 EXPECT(publish_rejected(mem, t_tail));
68
69 const Target t_gen2 = mem.capture(kBase + 0x10, 8);
70 EXPECT(t_gen2.generation == t4.generation + 1);
71 mem.track(kBase, 0x8000, kAlloc, false);
72 EXPECT(publish_rejected(mem, t4));
73 EXPECT(publish_ok(mem, t_gen2, kBase + 0x10, 8, kAlloc));
74 return 0;
75}
76
77int test_subrange_and_adjacent_page_isolation() {
78 Memory mem;
79 constexpr std::uintptr_t kBase = 0x200000;
80 constexpr std::uintptr_t kAlloc = 0x200000;
81 mem.track(kBase, 0x2000, kAlloc, false);
82 const Target t0 = mem.capture(kBase + 0x100, 8);
83
84 // Re-tracking a covered subrange does not disturb existing targets.
85 mem.track(kBase + 0x1000, 0x1000, kAlloc, false);
86 EXPECT(publish_ok(mem, t0, kBase + 0x100, 8, kAlloc));
87
88 // An adjacent region at kBase + 0x2000 stays separate so partially releasing page 2 does not falsely
89 // invalidate live targets in [kBase, kBase + 0x2000).
90 mem.track(kBase + 0x2000, 0x1000, kAlloc, false);
91 const Target t_adj = mem.capture(kBase + 0x2008, 8);
92 EXPECT(publish_ok(mem, t_adj, kBase + 0x2008, 8, kAlloc));
93
94 EXPECT(mem.release_pages(kBase + 0x2000, 0x1000, false, [] { return true; }));
95 EXPECT(publish_rejected(mem, t_adj));
96 EXPECT(publish_ok(mem, t0, kBase + 0x100, 8, kAlloc));
97 return 0;
98}
99
100int test_allocation_base_and_heap_invalidation() {
101 Memory mem;
102 constexpr std::uintptr_t kBase = 0x300000;
103 mem.track(kBase, 0x1000, kBase, false);
104 const Target t_old = mem.capture(kBase + 8, 8);
105
106 // Different allocation_base at the same base represents a new reservation and invalidates prior targets.
107 mem.track(kBase, 0x2000, kBase + 0x80000, false);
108 EXPECT(publish_rejected(mem, t_old));
109 const Target t_new = mem.capture(kBase + 8, 8);
110 EXPECT(publish_ok(mem, t_new, kBase + 8, 8, kBase + 0x80000));
111
112 // Failed release callback must leave regions and generations untouched.
113 EXPECT(!mem.release_pages(kBase + 0x80000, 0, true, [] { return false; }));
114 EXPECT(publish_ok(mem, t_new, kBase + 8, 8, kBase + 0x80000));
115
116 // Heap regions are isolated from release_pages and invalidated by release_heap or re-track.
117 constexpr std::uintptr_t kHeap = 0x400000;
118 mem.track(kHeap, 0x1000, kBase + 0x80000, true);
119 const Target t_heap = mem.capture(kHeap + 16, 4);
120 EXPECT(mem.release_pages(kBase + 0x80000, 0, true, [] { return true; }));
121 EXPECT(publish_rejected(mem, t_new));
122 EXPECT(publish_ok(mem, t_heap, kHeap + 16, 4, kBase + 0x80000));
123
124 mem.release_heap(kHeap);
125 EXPECT(publish_rejected(mem, t_heap));
126 return 0;
127}
128
129int test_arena_base_zero_and_bounds() {
130 Memory mem;
131 constexpr std::uintptr_t kArenaHost = 0x500000;
132 mem.track(0, 0x10000, kArenaHost, false);
133 const Target t0 = mem.capture(0x100, 8);
134 EXPECT(publish_ok(mem, t0, 0x100, 8, kArenaHost));
135
136 // Expanding base-0 GPU arena preserves in-flight targets.
137 mem.track(0, 0x20000, kArenaHost, false);
138 EXPECT(publish_ok(mem, t0, 0x100, 8, kArenaHost));
139 const Target t_high = mem.capture(0x1fff8, 8);
140 EXPECT(publish_ok(mem, t_high, 0x1fff8, 8, kArenaHost));
141
142 // Invalid track and capture calls are rejected cleanly.
143 mem.track(0x900000, 0, 0x900000, false);
144 mem.track(0x900000, 0x1000, 0, false);
145 mem.track(std::numeric_limits<std::uintptr_t>::max() - 0x10, 0x100, 0x900000, false);
146 EXPECT(mem.capture(0x900000, 4).owner.lock() == nullptr);
147 EXPECT(mem.capture(0x100, 2).owner.lock() == nullptr);
148 EXPECT(mem.capture(0x100, 16).owner.lock() == nullptr);
149 EXPECT(mem.capture(0x102, 4).owner.lock() == nullptr);
150 EXPECT(mem.capture(0x104, 8).owner.lock() == nullptr);
151 EXPECT(mem.capture(0x1fffc, 8).owner.lock() == nullptr); // straddles end (0x20000)
152 return 0;
153}
154
155int test_concurrent_expand_and_publish() {
156 Memory mem;
157 constexpr std::uintptr_t kBase = 0x600000;
158 constexpr std::uintptr_t kAlloc = 0x600000;
159 mem.track(kBase, 0x1000, kAlloc, false);
160
161 std::atomic<bool> failed{false};
162 std::thread expander([&] {
163 for (std::uintptr_t pages = 1; pages <= 256; ++pages) mem.track(kBase, pages * 0x1000, kAlloc, false);
164 });
165 std::vector<std::thread> workers;
166 for (int w = 0; w < 3; ++w) {
167 workers.emplace_back([&, w] {
168 const std::uintptr_t addr = kBase + static_cast<std::uintptr_t>(w) * 16;
169 for (int i = 0; i < 2000; ++i) {
170 const Target t = mem.capture(addr, 8);
171 if (!publish_ok(mem, t, addr, 8, kAlloc)) {
172 failed.store(true, std::memory_order_relaxed);
173 return;
174 }
175 }
176 });
177 }
178 expander.join();
179 for (auto& worker : workers) worker.join();
180 EXPECT(!failed.load());
181 return 0;
182}
183
184} // namespace
185
186int main() {
187 if (int r = test_expand_preserves_inflight_targets()) return r;
188 if (int r = test_subrange_and_adjacent_page_isolation()) return r;
189 if (int r = test_allocation_base_and_heap_invalidation()) return r;
190 if (int r = test_arena_base_zero_and_bounds()) return r;
191 if (int r = test_concurrent_expand_and_publish()) return r;
192 std::printf("gpu_label_memory_test: ok\n");
193 return 0;
194}