Skip to content

Commit c7f8c89

Browse files
Remove the old padding of alignment while tracing.
Add new partially AI generated test for trace to replay alignment correspondence.
1 parent 2cdd695 commit c7f8c89

4 files changed

Lines changed: 176 additions & 3 deletions

File tree

CMakeLists.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,12 @@ set_tests_properties(trace_test_buffers_replay PROPERTIES FIXTURES_REQUIRED trac
342342
add_test(NAME trace_test_buffers_validate COMMAND ${CMAKE_CURRENT_BINARY_DIR}/lava-tool -V tracing_buffers.vk)
343343
set_tests_properties(trace_test_buffers_validate PROPERTIES FIXTURES_REQUIRED tracing_buffers)
344344

345+
internal_test(tracing_alignment)
346+
add_test(NAME trace_test_alignment_replay COMMAND ${CMAKE_CURRENT_BINARY_DIR}/lava-replay -V tracing_alignment.vk)
347+
set_tests_properties(trace_test_alignment_replay PROPERTIES FIXTURES_REQUIRED tracing_alignment)
348+
add_test(NAME trace_test_alignment_validate COMMAND ${CMAKE_CURRENT_BINARY_DIR}/lava-tool -V tracing_alignment.vk)
349+
set_tests_properties(trace_test_alignment_validate PROPERTIES FIXTURES_REQUIRED tracing_alignment)
350+
345351
add_executable(write1 tests/write1.cpp)
346352
target_include_directories(write1 ${COMMON_INCLUDE})
347353
target_link_libraries(write1 ${COMMON_LIBRARIES} lavatube)

scripts/util.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1752,7 +1752,7 @@ def loadfunc(name, node, target, header):
17521752
if name in replay_post_calls: # hard-coded post handling
17531753
z.do('if (reader.run) replay_post_%s(reader, %s%s);' % (name, 'retval, ' if retval != 'void' else '', ', '.join(call_list)))
17541754
# Flexible post-handling
1755-
if not name in spec.special_count_funcs and not name in ignore_on_read:
1755+
if not name in spec.special_count_funcs and not name in skip_post_calls and name != 'vkGetPhysicalDeviceWaylandPresentationSupportKHR':
17561756
z.do('for (auto* c : %s_callbacks) c(%s);' % (name, ', '.join(call_list)))
17571757
if name in replay_postprocess_calls:
17581758
z.do('if (!reader.run) replay_postprocess_%s(reader, %s%s);' % (name, 'retval, ' if retval != 'void' else '', ', '.join(call_list)))

src/hardcode_write.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -259,8 +259,6 @@ static void extend_bits(VkMemoryRequirements* pMemoryRequirements)
259259
pMemoryRequirements->memoryTypeBits &= ~(1 << i); // no, clear
260260
}
261261
}
262-
// extend alignment
263-
pMemoryRequirements->alignment = std::max<VkDeviceSize>(pMemoryRequirements->alignment, 256);
264262
frame_mutex.unlock();
265263
}
266264

tests/tracing_alignment.cpp

Lines changed: 169 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,169 @@
1+
// Verify buffer memory requirements alignments match between trace and replay paths
2+
3+
#include "tests/common.h"
4+
#include "memory.h"
5+
#include "packfile.h"
6+
#include "vk_wrapper_auto.h"
7+
#include <stdio.h>
8+
#include <assert.h>
9+
#include <unordered_map>
10+
#include <vector>
11+
12+
#define TEST_NAME "tracing_alignment"
13+
14+
static std::unordered_map<uint32_t, VkDeviceSize> trace_alignments;
15+
static std::unordered_map<uint32_t, VkDeviceSize> replay_alignments;
16+
static std::vector<uint32_t> buffer_indices;
17+
static size_t replay_call_index = 0;
18+
19+
static trackedbuffer make_trackedbuffer(const VkBufferCreateInfo& info, uint32_t idx)
20+
{
21+
trackedbuffer t;
22+
t.index = idx;
23+
t.size = info.size;
24+
t.flags = info.flags;
25+
t.sharingMode = info.sharingMode;
26+
t.usage = info.usage;
27+
t.object_type = VK_OBJECT_TYPE_BUFFER;
28+
t.enter_initialized();
29+
return t;
30+
}
31+
32+
static void trace()
33+
{
34+
vulkan_req_t reqs;
35+
reqs.apiVersion = VK_API_VERSION_1_3;
36+
vulkan_setup_t vulkan = test_init(TEST_NAME, reqs);
37+
VkResult result;
38+
39+
trace_alignments.clear();
40+
replay_alignments.clear();
41+
buffer_indices.clear();
42+
43+
const VkDeviceSize sizes[] = { 64, 4096 + 13, 1024 * 1024 + 7 };
44+
VkBufferCreateInfo info = { VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO, nullptr };
45+
info.usage = VK_BUFFER_USAGE_TRANSFER_SRC_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT | VK_BUFFER_USAGE_STORAGE_BUFFER_BIT;
46+
info.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
47+
48+
for (unsigned i = 0; i < sizeof(sizes) / sizeof(sizes[0]); i++)
49+
{
50+
info.size = sizes[i];
51+
VkBuffer buffer = VK_NULL_HANDLE;
52+
result = trace_vkCreateBuffer(vulkan.device, &info, nullptr, &buffer);
53+
check(result);
54+
char name[32];
55+
snprintf(name, sizeof(name), "alignment-buffer-%u", i);
56+
test_set_name(vulkan.device, VK_OBJECT_TYPE_BUFFER, (uint64_t)buffer, name);
57+
58+
VkMemoryRequirements raw_req = {};
59+
wrap_vkGetBufferMemoryRequirements(vulkan.device, buffer, &raw_req);
60+
assert(raw_req.alignment != 0);
61+
62+
const uint32_t index = trace_vkGetDeviceTracingObjectPropertyTRACETOOLTEST(vulkan.device, VK_OBJECT_TYPE_BUFFER, (uint64_t)buffer, VK_TRACING_OBJECT_PROPERTY_INDEX_TRACETOOLTEST);
63+
64+
VkMemoryRequirements traced_req = {};
65+
trace_vkGetBufferMemoryRequirements(vulkan.device, buffer, &traced_req);
66+
assert(traced_req.alignment == raw_req.alignment);
67+
68+
trackedbuffer tracked = make_trackedbuffer(info, index);
69+
memory_requirements replay_req = get_trackedbuffer_memory_requirements(vulkan.device, tracked);
70+
assert(replay_req.requirements.alignment == raw_req.alignment);
71+
72+
trace_alignments[index] = traced_req.alignment;
73+
buffer_indices.push_back(index);
74+
trace_vkDestroyBuffer(vulkan.device, buffer, nullptr);
75+
ILOG("Trace %d align=%d buffer=%lu", (int)index, (int)raw_req.alignment, (unsigned long)buffer);
76+
}
77+
test_done(vulkan);
78+
}
79+
80+
static bool getnext(lava_file_reader& t)
81+
{
82+
bool done = false;
83+
const uint8_t instrtype = t.read_uint8_t();
84+
if (instrtype == PACKET_VULKAN_API_CALL)
85+
{
86+
const uint16_t apicall = t.read_apicall();
87+
if (apicall == 1) done = true; // vkDestroyInstance
88+
}
89+
else if (instrtype == PACKET_THREAD_BARRIER)
90+
{
91+
t.read_barrier();
92+
}
93+
else if (instrtype == PACKET_IMAGE_UPDATE || instrtype == PACKET_IMAGE_UPDATE2)
94+
{
95+
update_image_packet(instrtype, t);
96+
}
97+
else if (instrtype == PACKET_BUFFER_UPDATE || instrtype == PACKET_BUFFER_UPDATE2)
98+
{
99+
update_buffer_packet(instrtype, t);
100+
}
101+
else if (instrtype == PACKET_TENSOR_UPDATE)
102+
{
103+
update_tensor_packet(instrtype, t);
104+
}
105+
else assert(false);
106+
t.parent->allocator.self_test();
107+
return !done;
108+
}
109+
110+
static void record_vkGetBufferMemoryRequirements(VkDevice device, VkBuffer buffer, VkMemoryRequirements* pMemoryRequirements)
111+
{
112+
(void)device;
113+
(void)buffer;
114+
assert(pMemoryRequirements != nullptr);
115+
assert(replay_call_index < buffer_indices.size());
116+
const uint32_t idx = buffer_indices.at(replay_call_index++);
117+
replay_alignments[idx] = pMemoryRequirements->alignment;
118+
ILOG("Found replay alignment idx=%d align=%d", (int)idx, (int)pMemoryRequirements->alignment);
119+
}
120+
121+
static void retrace()
122+
{
123+
// Verify trace file itself
124+
Json::Value tracking = packed_json("tracking.json", TEST_NAME ".vk");
125+
unsigned matched = 0;
126+
for (const auto& entry : tracking["VkBuffer"])
127+
{
128+
const uint32_t index = entry["index"].asUInt();
129+
assert(entry.isMember("req_alignment"));
130+
const VkDeviceSize recorded = entry["req_alignment"].asUInt();
131+
assert(recorded == trace_alignments.at(index));
132+
matched++;
133+
}
134+
assert(matched == trace_alignments.size());
135+
136+
// Replay it
137+
lava_reader r(TEST_NAME ".vk");
138+
replay_call_index = 0;
139+
replay_alignments.clear();
140+
vkGetBufferMemoryRequirements_callbacks.push_back(record_vkGetBufferMemoryRequirements);
141+
lava_file_reader& t = r.file_reader(0);
142+
int remaining = r.allocator.self_test();
143+
assert(remaining == 0);
144+
while (getnext(t)) {}
145+
remaining = r.allocator.self_test();
146+
assert(remaining == 0);
147+
assert(replay_call_index == buffer_indices.size());
148+
assert(replay_alignments.size() == trace_alignments.size());
149+
150+
// Check data we collected during run
151+
for (const auto& it : trace_alignments)
152+
{
153+
assert(replay_alignments.count(it.first) > 0);
154+
assert(replay_alignments.at(it.first) == it.second);
155+
}
156+
for (const auto& entry : tracking["VkBuffer"])
157+
{
158+
const uint32_t idx = entry["index"].asUInt();
159+
assert(replay_alignments.count(idx) > 0);
160+
assert(replay_alignments.at(idx) == trace_alignments.at(idx));
161+
}
162+
}
163+
164+
int main()
165+
{
166+
trace();
167+
retrace();
168+
return 0;
169+
}

0 commit comments

Comments
 (0)