| 1 |
/* |
| 2 |
* Copyright 2025 Collabora Ltd |
| 3 |
* Copyright (C) 2025 Arm Ltd. |
| 4 |
* SPDX-License-Identifier: MIT |
| 5 |
*/ |
| 6 |
|
| 7 |
#include "cs_builder.h" |
| 8 |
|
| 9 |
#include <gtest/gtest.h> |
| 10 |
#include "mesa-gtest-extras.h" |
| 11 |
|
| 12 |
#define MAX_OUTPUT_SIZE 512 |
| 13 |
|
| 14 |
class CsBuilderTest : public ::testing::Test { |
| 15 |
public: |
| 16 |
CsBuilderTest(); |
| 17 |
~CsBuilderTest(); |
| 18 |
|
| 19 |
struct cs_builder b; |
| 20 |
uint64_t *output; |
| 21 |
}; |
| 22 |
|
| 23 |
CsBuilderTest::CsBuilderTest() |
| 24 |
{ |
| 25 |
output = new uint64_t[MAX_OUTPUT_SIZE]; |
| 26 |
struct cs_builder_conf conf = { |
| 27 |
.nr_registers = 96, |
| 28 |
.nr_kernel_registers = 4, |
| 29 |
}; |
| 30 |
struct cs_buffer buffer = { |
| 31 |
.cpu = output, |
| 32 |
.gpu = 0x0, |
| 33 |
.capacity = MAX_OUTPUT_SIZE, |
| 34 |
}; |
| 35 |
cs_builder_init(&b, &conf, buffer); |
| 36 |
} |
| 37 |
|
| 38 |
CsBuilderTest::~CsBuilderTest() |
| 39 |
{ |
| 40 |
cs_builder_fini(&b); |
| 41 |
delete output; |
| 42 |
} |
| 43 |
|
| 44 |
TEST_F(CsBuilderTest, basic) |
| 45 |
{ |
| 46 |
cs_move32_to(&b, cs_reg32(&b, 42), 0xdeadbeef); |
| 47 |
cs_end(&b); |
| 48 |
|
| 49 |
uint64_t expected[] = { |
| 50 |
0x022a0000deadbeef, /* MOVE32 r42, #0xdeadbeef */ |
| 51 |
}; |
| 52 |
|
| 53 |
EXPECT_EQ(b.root_chunk.size, ARRAY_SIZE(expected)); |
| 54 |
EXPECT_U64_ARRAY_EQUAL(output, expected, ARRAY_SIZE(expected)); |
| 55 |
} |
| 56 |
|
| 57 |
TEST_F(CsBuilderTest, maybe_no_patch) |
| 58 |
{ |
| 59 |
struct cs_maybe *maybe; |
| 60 |
cs_move32_to(&b, cs_reg32(&b, 42), 0xabad1dea); |
| 61 |
cs_maybe(&b, &maybe) { |
| 62 |
cs_move32_to(&b, cs_reg32(&b, 42), 0xdeadbeef); |
| 63 |
} |
| 64 |
cs_end(&b); |
| 65 |
|
| 66 |
uint64_t expected[] = { |
| 67 |
0x022a0000abad1dea, /* MOVE32 r42, #0xabad1dea */ |
| 68 |
0x0000000000000000, /* NOP */ |
| 69 |
}; |
| 70 |
EXPECT_EQ(b.root_chunk.size, ARRAY_SIZE(expected)); |
| 71 |
EXPECT_U64_ARRAY_EQUAL(output, expected, ARRAY_SIZE(expected)); |
| 72 |
} |
| 73 |
|
| 74 |
TEST_F(CsBuilderTest, maybe_patch) |
| 75 |
{ |
| 76 |
struct cs_maybe *maybe; |
| 77 |
cs_move32_to(&b, cs_reg32(&b, 42), 0xabad1dea); |
| 78 |
cs_maybe(&b, &maybe) { |
| 79 |
cs_move32_to(&b, cs_reg32(&b, 42), 0xdeadbeef); |
| 80 |
} |
| 81 |
cs_patch_maybe(&b, maybe); |
| 82 |
cs_end(&b); |
| 83 |
|
| 84 |
uint64_t expected_patched[] = { |
| 85 |
0x022a0000abad1dea, /* MOVE32 r42, #0xabad1dea */ |
| 86 |
0x022a0000deadbeef, /* MOVE32 r42, #0xdeadbeef */ |
| 87 |
}; |
| 88 |
EXPECT_EQ(b.root_chunk.size, ARRAY_SIZE(expected_patched)); |
| 89 |
EXPECT_U64_ARRAY_EQUAL(output, expected_patched, ARRAY_SIZE(expected_patched)); |
| 90 |
} |
| 91 |
|
| 92 |
/* If cs_maybe is called inside a block, we defer calculating the patch |
| 93 |
* address until the outer blocks are closed. */ |
| 94 |
TEST_F(CsBuilderTest, maybe_inner_block) |
| 95 |
{ |
| 96 |
struct cs_maybe *maybe; |
| 97 |
cs_move32_to(&b, cs_reg32(&b, 42), 0xabad1dea); |
| 98 |
cs_if(&b, MALI_CS_CONDITION_GREATER, cs_reg32(&b, 42)) { |
| 99 |
cs_maybe(&b, &maybe) { |
| 100 |
cs_move32_to(&b, cs_reg32(&b, 42), 0xdeadbeef); |
| 101 |
} |
| 102 |
cs_move32_to(&b, cs_reg32(&b, 42), 0xabcdef01); |
| 103 |
} |
| 104 |
cs_patch_maybe(&b, maybe); |
| 105 |
cs_end(&b); |
| 106 |
|
| 107 |
uint64_t expected_patched[] = { |
| 108 |
0x022a0000abad1dea, /* MOVE32 r42, #0xabad1dea */ |
| 109 |
0x16002a0000000002, /* BRANCH le, r42, #0x2 */ |
| 110 |
0x022a0000deadbeef, /* MOVE32 r42, #0xdeadbeef */ |
| 111 |
0x022a0000abcdef01, /* MOVE32 r42, #0xabcdef01 */ |
| 112 |
}; |
| 113 |
EXPECT_EQ(b.root_chunk.size, ARRAY_SIZE(expected_patched)); |
| 114 |
EXPECT_U64_ARRAY_EQUAL(output, expected_patched, ARRAY_SIZE(expected_patched)); |
| 115 |
} |
| 116 |
|
| 117 |
/* If cs_patch_maybe is patched before the outer block that cs_maybe was |
| 118 |
* called in is closed, the instructions need to be patched in a different |
| 119 |
* location. */ |
| 120 |
TEST_F(CsBuilderTest, maybe_early_patch) |
| 121 |
{ |
| 122 |
struct cs_maybe *maybe; |
| 123 |
cs_move32_to(&b, cs_reg32(&b, 42), 0xabad1dea); |
| 124 |
cs_if(&b, MALI_CS_CONDITION_GREATER, cs_reg32(&b, 42)) { |
| 125 |
cs_maybe(&b, &maybe) { |
| 126 |
cs_move32_to(&b, cs_reg32(&b, 42), 0xdeadbeef); |
| 127 |
} |
| 128 |
cs_move32_to(&b, cs_reg32(&b, 42), 0xabcdef01); |
| 129 |
|
| 130 |
cs_patch_maybe(&b, maybe); |
| 131 |
} |
| 132 |
cs_end(&b); |
| 133 |
|
| 134 |
uint64_t expected_patched[] = { |
| 135 |
0x022a0000abad1dea, /* MOVE32 r42, #0xabad1dea */ |
| 136 |
0x16002a0000000002, /* BRANCH le, r42, #0x2 */ |
| 137 |
0x022a0000deadbeef, /* MOVE32 r42, #0xdeadbeef */ |
| 138 |
0x022a0000abcdef01, /* MOVE32 r42, #0xabcdef01 */ |
| 139 |
}; |
| 140 |
EXPECT_EQ(b.root_chunk.size, ARRAY_SIZE(expected_patched)); |
| 141 |
EXPECT_U64_ARRAY_EQUAL(output, expected_patched, ARRAY_SIZE(expected_patched)); |
| 142 |
} |
| 143 |
|
| 144 |
/* If inside the loop no register is used that is getting loaded at the moment, |
| 145 |
* do not emit a WAIT on continue / going back to start. */ |
| 146 |
TEST_F(CsBuilderTest, loop_ls_tracker_unrelated_inside) |
| 147 |
{ |
| 148 |
struct cs_index r0 = cs_reg32(&b, 0); |
| 149 |
struct cs_index r1 = cs_reg32(&b, 1); |
| 150 |
struct cs_index addr = cs_reg64(&b, 10); |
| 151 |
|
| 152 |
cs_load32_to(&b, r0, addr, 0x0); |
| 153 |
cs_while(&b, MALI_CS_CONDITION_ALWAYS, cs_undef()) { |
| 154 |
cs_add32(&b, r1, r1, 0x0); |
| 155 |
cs_break(&b); |
| 156 |
} |
| 157 |
cs_add32(&b, r0, r0, 0xab); |
| 158 |
cs_end(&b); |
| 159 |
|
| 160 |
uint64_t expected_patched[] = { |
| 161 |
0x14000a0000010000, /* LOAD_MULTIPLE r0, addr, #0x0 */ |
| 162 |
0x1001010000000000, /* ADD32 r1, r1, #0x0 */ |
| 163 |
0x1600000060000001, /* BRANCH al, r0, #1 */ |
| 164 |
0x160000006000fffd, /* BRANCH al, r0, #-3 */ |
| 165 |
0x0300000000010000, /* WAIT #0x1 */ |
| 166 |
0x10000000000000ab, /* ADD32 r0, r0, #0xab */ |
| 167 |
}; |
| 168 |
EXPECT_EQ(b.root_chunk.size, ARRAY_SIZE(expected_patched)); |
| 169 |
EXPECT_U64_ARRAY_EQUAL(output, expected_patched, |
| 170 |
ARRAY_SIZE(expected_patched)); |
| 171 |
} |
| 172 |
|
| 173 |
/* If a load is started inside the loop it has to be waited for after the loop. */ |
| 174 |
TEST_F(CsBuilderTest, loop_ls_tracker_load_only_inside_if) |
| 175 |
{ |
| 176 |
struct cs_index r0 = cs_reg32(&b, 0); |
| 177 |
struct cs_index addr = cs_reg64(&b, 10); |
| 178 |
|
| 179 |
cs_while(&b, MALI_CS_CONDITION_ALWAYS, cs_undef()) { |
| 180 |
cs_if(&b, MALI_CS_CONDITION_LESS, r0) { |
| 181 |
cs_load32_to(&b, r0, addr, 0x0); |
| 182 |
} |
| 183 |
cs_break(&b); |
| 184 |
} |
| 185 |
cs_add32(&b, r0, r0, 0xab); |
| 186 |
cs_end(&b); |
| 187 |
|
| 188 |
uint64_t expected_patched[] = { |
| 189 |
0x1600000050000001, /* BRANCH ge, r0, #1 */ |
| 190 |
0x14000a0000010000, /* LOAD_MULTIPLE r0, addr, #0x0 */ |
| 191 |
0x1600000060000002, /* BRANCH al, r0, #2 */ |
| 192 |
/* This WAIT is unnecessary because the loop body doesn't use r0. */ |
| 193 |
0x0300000000010000, /* WAIT #0x1 */ |
| 194 |
0x160000006000fffb, /* BRANCH al, r0, #-5 */ |
| 195 |
0x0300000000010000, /* WAIT #0x1 */ |
| 196 |
0x10000000000000ab, /* ADD32 r0, r0, #0xab */ |
| 197 |
}; |
| 198 |
EXPECT_EQ(b.root_chunk.size, ARRAY_SIZE(expected_patched)); |
| 199 |
EXPECT_U64_ARRAY_EQUAL(output, expected_patched, |
| 200 |
ARRAY_SIZE(expected_patched)); |
| 201 |
} |
| 202 |
|
| 203 |
/* If a load is started inside the loop with a continue in the if, it has to be |
| 204 |
* waited for on continue. */ |
| 205 |
TEST_F(CsBuilderTest, loop_ls_tracker_load_only_continue_inside_if) |
| 206 |
{ |
| 207 |
struct cs_index r0 = cs_reg32(&b, 0); |
| 208 |
struct cs_index addr = cs_reg64(&b, 10); |
| 209 |
|
| 210 |
cs_add32(&b, r0, r0, 0x0); |
| 211 |
cs_while(&b, MALI_CS_CONDITION_ALWAYS, cs_undef()) { |
| 212 |
cs_if(&b, MALI_CS_CONDITION_LESS, cs_reg32(&b, 1)) { |
| 213 |
cs_load32_to(&b, r0, addr, 0x0); |
| 214 |
cs_continue(&b); |
| 215 |
} |
| 216 |
cs_break(&b); |
| 217 |
} |
| 218 |
cs_add32(&b, r0, r0, 0xab); |
| 219 |
cs_end(&b); |
| 220 |
|
| 221 |
uint64_t expected_patched[] = { |
| 222 |
0x1000000000000000, /* ADD32 r0, r0, #0x0 */ |
| 223 |
0x1600010050000003, /* BRANCH ge, r1, #3 */ |
| 224 |
0x14000a0000010000, /* LOAD_MULTIPLE r0, addr, #0x0 */ |
| 225 |
0x0300000000010000, /* WAIT #0x1 */ |
| 226 |
0x160000006000fffc, /* BRANCH al, r0, #-4 */ |
| 227 |
0x1600000060000001, /* BRANCH al, r1, #1 */ |
| 228 |
0x160000006000fffa, /* BRANCH al, r0, #-6 */ |
| 229 |
0x10000000000000ab, /* ADD32 r0, r0, #0xab */ |
| 230 |
}; |
| 231 |
EXPECT_EQ(b.root_chunk.size, ARRAY_SIZE(expected_patched)); |
| 232 |
EXPECT_U64_ARRAY_EQUAL(output, expected_patched, |
| 233 |
ARRAY_SIZE(expected_patched)); |
| 234 |
} |
| 235 |
|
| 236 |
/* If a load is started inside the loop with a break in the if, it has to be |
| 237 |
* waited for after the loop. */ |
| 238 |
TEST_F(CsBuilderTest, loop_ls_tracker_load_only_break_inside_if) |
| 239 |
{ |
| 240 |
struct cs_index r0 = cs_reg32(&b, 0); |
| 241 |
struct cs_index addr = cs_reg64(&b, 10); |
| 242 |
|
| 243 |
cs_add32(&b, r0, r0, 0x0); |
| 244 |
cs_while(&b, MALI_CS_CONDITION_ALWAYS, cs_undef()) { |
| 245 |
cs_if(&b, MALI_CS_CONDITION_LESS, cs_reg32(&b, 1)) { |
| 246 |
cs_load32_to(&b, r0, addr, 0x0); |
| 247 |
cs_break(&b); |
| 248 |
} |
| 249 |
} |
| 250 |
cs_add32(&b, r0, r0, 0xab); |
| 251 |
cs_end(&b); |
| 252 |
|
| 253 |
uint64_t expected_patched[] = { |
| 254 |
0x1000000000000000, /* ADD32 r0, r0, #0x0 */ |
| 255 |
0x1600010050000002, /* BRANCH ge, r1, #2 */ |
| 256 |
0x14000a0000010000, /* LOAD_MULTIPLE r0, addr, #0x0 */ |
| 257 |
0x1600000060000002, /* BRANCH al, r0, #2 */ |
| 258 |
0x0300000000010000, /* WAIT #0x1 */ |
| 259 |
0x160000006000fffb, /* BRANCH al, r0, #-5 */ |
| 260 |
0x0300000000010000, /* WAIT #0x1 */ |
| 261 |
0x10000000000000ab, /* ADD32 r0, r0, #0xab */ |
| 262 |
}; |
| 263 |
EXPECT_EQ(b.root_chunk.size, ARRAY_SIZE(expected_patched)); |
| 264 |
EXPECT_U64_ARRAY_EQUAL(output, expected_patched, |
| 265 |
ARRAY_SIZE(expected_patched)); |
| 266 |
} |
| 267 |
|
| 268 |
/* If a register is loaded inside the loop, that was already getting loaded |
| 269 |
* when the loop began, there is no need to add a WAIT on continue. If that |
| 270 |
* register is used again after the loop, a WAIT has to be added. */ |
| 271 |
TEST_F(CsBuilderTest, loop_ls_tracker_load_same_inside) |
| 272 |
{ |
| 273 |
struct cs_index r0 = cs_reg32(&b, 0); |
| 274 |
struct cs_index addr = cs_reg64(&b, 10); |
| 275 |
|
| 276 |
cs_load32_to(&b, r0, addr, 0x0); |
| 277 |
cs_while(&b, MALI_CS_CONDITION_ALWAYS, cs_undef()) { |
| 278 |
cs_add32(&b, r0, r0, 0x0); |
| 279 |
cs_load32_to(&b, r0, addr, 0x0); |
| 280 |
cs_if(&b, MALI_CS_CONDITION_LESS, cs_reg32(&b, 1)) { |
| 281 |
cs_break(&b); |
| 282 |
} |
| 283 |
} |
| 284 |
cs_add32(&b, r0, r0, 0xab); |
| 285 |
cs_end(&b); |
| 286 |
|
| 287 |
uint64_t expected_patched[] = { |
| 288 |
0x14000a0000010000, /* LOAD_MULTIPLE r0, addr, #0x0 */ |
| 289 |
0x0300000000010000, /* WAIT #0x1 */ |
| 290 |
0x1000000000000000, /* ADD32 r0, r0, #0x0 */ |
| 291 |
0x14000a0000010000, /* LOAD_MULTIPLE r0, addr, #0x0 */ |
| 292 |
0x1600010050000001, /* BRANCH ge, r1, #1 */ |
| 293 |
0x1600000060000001, /* BRANCH al, r0, #1 */ |
| 294 |
0x160000006000fffa, /* BRANCH al, r0, #-6 */ |
| 295 |
0x0300000000010000, /* WAIT #0x1 */ |
| 296 |
0x10000000000000ab, /* ADD32 r0, r0, #0xab */ |
| 297 |
}; |
| 298 |
EXPECT_EQ(b.root_chunk.size, ARRAY_SIZE(expected_patched)); |
| 299 |
EXPECT_U64_ARRAY_EQUAL(output, expected_patched, |
| 300 |
ARRAY_SIZE(expected_patched)); |
| 301 |
} |
| 302 |
|
| 303 |
/* If the register that is used and loaded in the loop body is also used as the |
| 304 |
* condition, we need to WAIT on continue because the WAIT for the condition is |
| 305 |
* emitted before the loop body. */ |
| 306 |
TEST_F(CsBuilderTest, loop_ls_tracker_load_same_inside_use_as_cond) |
| 307 |
{ |
| 308 |
struct cs_index r0 = cs_reg32(&b, 0); |
| 309 |
struct cs_index addr = cs_reg64(&b, 10); |
| 310 |
|
| 311 |
cs_load32_to(&b, r0, addr, 0x0); |
| 312 |
cs_while(&b, MALI_CS_CONDITION_LESS, r0) { |
| 313 |
cs_add32(&b, r0, r0, 0x0); |
| 314 |
cs_load32_to(&b, r0, addr, 0x0); |
| 315 |
cs_if(&b, MALI_CS_CONDITION_LESS, cs_reg32(&b, 1)) { |
| 316 |
cs_break(&b); |
| 317 |
} |
| 318 |
} |
| 319 |
cs_add32(&b, r0, r0, 0xab); |
| 320 |
cs_end(&b); |
| 321 |
|
| 322 |
uint64_t expected_patched[] = { |
| 323 |
0x14000a0000010000, /* LOAD_MULTIPLE r0, addr, #0x0 */ |
| 324 |
0x0300000000010000, /* WAIT #0x1 */ |
| 325 |
0x1600000050000006, /* BRANCH ge, r0, #6 */ |
| 326 |
0x1000000000000000, /* ADD32 r0, r0, #0x0 */ |
| 327 |
0x14000a0000010000, /* LOAD_MULTIPLE r0, addr, #0x0 */ |
| 328 |
0x1600010050000001, /* BRANCH ge, r1, #1 */ |
| 329 |
0x1600000060000002, /* BRANCH al, r0, #2 */ |
| 330 |
0x0300000000010000, /* WAIT #0x1 */ |
| 331 |
0x160000004000fffa, /* BRANCH lt, r0, #-6 */ |
| 332 |
0x0300000000010000, /* WAIT #0x1 */ |
| 333 |
0x10000000000000ab, /* ADD32 r0, r0, #0xab */ |
| 334 |
}; |
| 335 |
EXPECT_EQ(b.root_chunk.size, ARRAY_SIZE(expected_patched)); |
| 336 |
EXPECT_U64_ARRAY_EQUAL(output, expected_patched, |
| 337 |
ARRAY_SIZE(expected_patched)); |
| 338 |
} |
| 339 |
|
| 340 |
/* If we flush a load/store from outside the cs_maybe block, it still needs |
| 341 |
* to be flushed the next time it is accessed because the cs_maybe block may |
| 342 |
* not have been executed */ |
| 343 |
TEST_F(CsBuilderTest, maybe_flush_outer_load) |
| 344 |
{ |
| 345 |
struct cs_maybe *maybe; |
| 346 |
struct cs_index addr = cs_reg64(&b, 0); |
| 347 |
struct cs_index reg1 = cs_reg32(&b, 3); |
| 348 |
struct cs_index reg2 = cs_reg32(&b, 4); |
| 349 |
|
| 350 |
cs_load32_to(&b, reg1, addr, 0); |
| 351 |
cs_maybe(&b, &maybe) { |
| 352 |
/* This should flush the load to reg */ |
| 353 |
cs_add32(&b, reg2, reg1, 0); |
| 354 |
} |
| 355 |
/* This should also flush the load to reg */ |
| 356 |
cs_add32(&b, reg2, reg1, 0); |
| 357 |
cs_patch_maybe(&b, maybe); |
| 358 |
cs_end(&b); |
| 359 |
|
| 360 |
uint64_t expected_patched[] = { |
| 361 |
0x1403000000010000, /* LOAD_MULTIPLE r3, [d0] */ |
| 362 |
/* inside maybe block */ |
| 363 |
0x0300000000010000, /* WAIT 1 */ |
| 364 |
0x1004030000000000, /* ADD_IMM32 r4, r3, 0 */ |
| 365 |
/* outside maybe block */ |
| 366 |
0x0300000000010000, /* WAIT 1 */ |
| 367 |
0x1004030000000000, /* ADD_IMM32 r4, r3, 0 */ |
| 368 |
}; |
| 369 |
EXPECT_EQ(b.root_chunk.size, ARRAY_SIZE(expected_patched)); |
| 370 |
EXPECT_U64_ARRAY_EQUAL(output, expected_patched, ARRAY_SIZE(expected_patched)); |
| 371 |
} |
| 372 |
|
| 373 |
/* If we initiate a load/store inside the cs_maybe block, it needs to be |
| 374 |
* flushed at the end of the block */ |
| 375 |
TEST_F(CsBuilderTest, maybe_flush_inner_load) |
| 376 |
{ |
| 377 |
struct cs_maybe *maybe; |
| 378 |
struct cs_index addr = cs_reg64(&b, 0); |
| 379 |
struct cs_index reg1 = cs_reg32(&b, 3); |
| 380 |
struct cs_index reg2 = cs_reg32(&b, 4); |
| 381 |
|
| 382 |
cs_maybe(&b, &maybe) { |
| 383 |
cs_load32_to(&b, reg1, addr, 0); |
| 384 |
/* This should flush the load to reg */ |
| 385 |
cs_add32(&b, reg2, reg1, 0); |
| 386 |
} |
| 387 |
/* This should not flush the load to reg */ |
| 388 |
cs_add32(&b, reg2, reg1, 0); |
| 389 |
cs_patch_maybe(&b, maybe); |
| 390 |
cs_end(&b); |
| 391 |
|
| 392 |
uint64_t expected_patched[] = { |
| 393 |
/* inside maybe block */ |
| 394 |
0x1403000000010000, /* LOAD_MULTIPLE r3, [d0] */ |
| 395 |
0x0300000000010000, /* WAIT 1 */ |
| 396 |
0x1004030000000000, /* ADD_IMM32 r4, r3, 0 */ |
| 397 |
/* outside maybe block */ |
| 398 |
0x1004030000000000, /* ADD_IMM32 r4, r3, 0 */ |
| 399 |
}; |
| 400 |
EXPECT_EQ(b.root_chunk.size, ARRAY_SIZE(expected_patched)); |
| 401 |
EXPECT_U64_ARRAY_EQUAL(output, expected_patched, ARRAY_SIZE(expected_patched)); |
| 402 |
} |
| 403 |
|