1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840
|
/*
* Copyright (c) 2024, 2025, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2024, Rivos Inc. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
#if (defined(RISCV) || defined(RISCV64)) && !defined(ZERO)
#include "asm/assembler.inline.hpp"
#include "asm/macroAssembler.hpp"
#include "memory/resourceArea.hpp"
#include "metaprogramming/enableIf.hpp"
#include "runtime/orderAccess.hpp"
#include "threadHelper.inline.hpp"
#include "unittest.hpp"
#include <limits>
typedef int64_t (*zicond_func)(int64_t cmp1, int64_t cmp2, int64_t dst, int64_t src);
typedef void (MacroAssembler::*cmov_func)(Register cmp1, Register cmp2, Register dst, Register src);
class CmovTester {
public:
static void test(cmov_func func, int64_t a0, int64_t a1, int64_t a2, int64_t a3, int64_t result) {
BufferBlob* bb = BufferBlob::create("riscvTest", 128);
CodeBuffer code(bb);
MacroAssembler _masm(&code);
address entry = _masm.pc();
{
((&_masm)->*func)(c_rarg0, c_rarg1, c_rarg2, c_rarg3);
_masm.mv(c_rarg0, c_rarg2);
_masm.ret();
}
_masm.flush(); // icache invalidate
int64_t ret = ((zicond_func)entry)(a0, a1, a2, a3);
ASSERT_EQ(ret, result);
BufferBlob::free(bb);
}
};
static void run_cmov_tests() {
// If 42(a0) eq 42(a1): assign dest(a2/66) the src(a3/77), expect result: 77
CmovTester::test(&MacroAssembler::cmov_eq, 42, 42, 66, 77, 77);
// If 41(a0) eq 42(a1): assign dest(a2/66) the src(a3/77), expect result: 66
CmovTester::test(&MacroAssembler::cmov_eq, 41, 42, 66, 77, 66);
CmovTester::test(&MacroAssembler::cmov_ne, 41, 42, 66, 77, 77);
CmovTester::test(&MacroAssembler::cmov_ne, 42, 42, 66, 77, 66);
CmovTester::test(&MacroAssembler::cmov_le, 41, 42, 66, 77, 77);
CmovTester::test(&MacroAssembler::cmov_le, 42, 42, 66, 77, 77);
CmovTester::test(&MacroAssembler::cmov_le, 42, -1, 66, 77, 66);
CmovTester::test(&MacroAssembler::cmov_leu, 41, 42, 66, 77, 77);
CmovTester::test(&MacroAssembler::cmov_leu, 42, 42, 66, 77, 77);
CmovTester::test(&MacroAssembler::cmov_leu, -1, 42, 66, 77, 66);
CmovTester::test(&MacroAssembler::cmov_ge, 43, 42, 66, 77, 77);
CmovTester::test(&MacroAssembler::cmov_ge, 42, 42, 66, 77, 77);
CmovTester::test(&MacroAssembler::cmov_ge, -1, 42, 66, 77, 66);
CmovTester::test(&MacroAssembler::cmov_geu, 43, 42, 66, 77, 77);
CmovTester::test(&MacroAssembler::cmov_geu, 42, 42, 66, 77, 77);
CmovTester::test(&MacroAssembler::cmov_geu, 42, -1, 66, 77, 66);
CmovTester::test(&MacroAssembler::cmov_lt, 41, 42, 66, 77, 77);
CmovTester::test(&MacroAssembler::cmov_lt, 42, 42, 66, 77, 66);
CmovTester::test(&MacroAssembler::cmov_lt, 42, -1, 66, 77, 66);
CmovTester::test(&MacroAssembler::cmov_ltu, 41, 42, 66, 77, 77);
CmovTester::test(&MacroAssembler::cmov_ltu, 42, 42, 66, 77, 66);
CmovTester::test(&MacroAssembler::cmov_ltu, -1, 42, 66, 77, 66);
CmovTester::test(&MacroAssembler::cmov_gt, 43, 42, 66, 77, 77);
CmovTester::test(&MacroAssembler::cmov_gt, 42, 42, 66, 77, 66);
CmovTester::test(&MacroAssembler::cmov_gt, -1, 42, 66, 77, 66);
CmovTester::test(&MacroAssembler::cmov_gtu, 43, 42, 66, 77, 77);
CmovTester::test(&MacroAssembler::cmov_gtu, 42, 42, 66, 77, 66);
CmovTester::test(&MacroAssembler::cmov_gtu, 42, -1, 66, 77, 66);
}
TEST_VM(RiscV, cmov) {
run_cmov_tests();
if (UseZicond) {
UseZicond = false;
run_cmov_tests();
UseZicond = true;
}
}
template <Assembler::operand_size ASMSIZE>
bool using_narrow() {
if (ASMSIZE == Assembler::int8 || ASMSIZE == Assembler::int16) {
return !(UseZacas && UseZabha);
}
return false;
}
template <typename TESTSIZE, Assembler::operand_size ASMSIZE>
class CmpxchgTester {
// The functions expect arguments to be type represented, not C-ABI argument representation.
// Hence an unsigned should be zero-extended, and the same goes for the return value.
typedef int64_t (*cmpxchg_func)(intptr_t addr, int64_t expected, int64_t new_value, int64_t result);
typedef int64_t (*cmpxchg_narrow_func)(intptr_t addr, int64_t expected, int64_t new_value, int64_t result,
int64_t scratch0, int64_t scratch1, int64_t scratch2);
BufferBlob* _bb;
cmpxchg_func _func;
cmpxchg_narrow_func _narrow;
public:
CmpxchgTester(int variant, bool boolean_result) {
_bb = BufferBlob::create("riscvTest", 128);
CodeBuffer code(_bb);
MacroAssembler _masm(&code);
address entry = _masm.pc();
if (using_narrow<ASMSIZE>()) {
address entry = _masm.pc();
_masm.cmpxchg_narrow_value(/*addr*/ c_rarg0, /*expected*/ c_rarg1, /*new_value*/c_rarg2,
ASMSIZE, Assembler::relaxed, Assembler::relaxed,
/*result*/ c_rarg3, boolean_result, c_rarg4, c_rarg5, c_rarg6); /* Uses also t0-t1, caller saved */
_masm.mv(c_rarg0, c_rarg3);
_masm.ret();
_narrow = ((cmpxchg_narrow_func)entry);
} else {
switch(variant) {
default:
_masm.cmpxchg(/*addr*/ c_rarg0, /*expected*/ c_rarg1, /*new_value*/c_rarg2,
ASMSIZE, Assembler::aq, Assembler::rl,
/*result*/ c_rarg3, boolean_result);
_masm.mv(c_rarg0, c_rarg3);
break;
case 1:
// expected == result
_masm.cmpxchg(/*addr*/ c_rarg0, /*expected*/ c_rarg1, /*new_value*/c_rarg2,
ASMSIZE, Assembler::aq, Assembler::rl,
/*result*/ c_rarg1, boolean_result);
_masm.mv(c_rarg0, c_rarg1);
break;
case 2:
// new_value == result
_masm.cmpxchg(/*addr*/ c_rarg0, /*expected*/ c_rarg1, /*new_value*/c_rarg2,
ASMSIZE, Assembler::aq, Assembler::rl,
/*result*/ c_rarg2, boolean_result);
_masm.mv(c_rarg0, c_rarg2);
break;
case 3:
// expected == new_value
_masm.cmpxchg(/*addr*/ c_rarg0, /*expected*/ c_rarg1, /*new_value*/ c_rarg1,
ASMSIZE, Assembler::aq, Assembler::rl,
/*result*/ c_rarg2, boolean_result);
_masm.mv(c_rarg0, c_rarg2);
break;
}
_masm.ret();
_func = ((cmpxchg_func)entry);
}
_masm.flush(); // icache invalidate
}
~CmpxchgTester() {
BufferBlob::free(_bb);
}
TESTSIZE cmpxchg(intptr_t addr, TESTSIZE expected, TESTSIZE new_value) {
if (using_narrow<ASMSIZE>()) {
return _narrow(addr, expected, new_value, /* dummy result */ 67, -1, -1, -1);
} else {
return _func(addr, expected, new_value, /* dummy result */ 67);
}
}
};
template <typename TESTSIZE, Assembler::operand_size ASMSIZE>
static void plain_cmpxchg_test(int variant, TESTSIZE dv, TESTSIZE ex, TESTSIZE nv, TESTSIZE eret, TESTSIZE edata, bool bv) {
CmpxchgTester<TESTSIZE, ASMSIZE> cmpxchg(variant, bv);
TESTSIZE data = dv;
TESTSIZE ret = cmpxchg.cmpxchg((intptr_t)&data, ex, nv);
ASSERT_EQ(ret, eret);
ASSERT_EQ(data, edata);
}
template <typename TESTSIZE, Assembler::operand_size ASMSIZE>
static void run_plain_cmpxchg_tests() {
TESTSIZE max = std::numeric_limits<TESTSIZE>::max();
TESTSIZE min = std::numeric_limits<TESTSIZE>::min();
TESTSIZE val[] = {37, min, max};
for (int i = 0; i < 3; i++) {
// Normal
plain_cmpxchg_test<TESTSIZE, ASMSIZE>( 0 /* variant */ , val[i] /* start value */,
val[i] /* expected */, 42 /* new value */,
val[i] /* return */ , 42 /* end value*/, false /* boolean ret*/);
plain_cmpxchg_test<TESTSIZE, ASMSIZE>( 0 /* variant */ , val[i] /* start value */,
36 /* expected */, 42 /* new value */,
val[i] /* return */ , val[i] /* end value */, false /* boolean ret*/);
plain_cmpxchg_test<TESTSIZE, ASMSIZE>( 0 /* variant */ , val[i] /* start value */,
val[i] /* expected */, 42 /* new value */,
1 /* return */ , 42 /* end value*/, true /* boolean ret*/);
plain_cmpxchg_test<TESTSIZE, ASMSIZE>( 0 /* variant */ , val[i] /* start value */,
36 /* expected */, 42 /* new value */,
0 /* return */ , val[i] /* end value */, true /* boolean ret*/);
// result == expected register
plain_cmpxchg_test<TESTSIZE, ASMSIZE>( 1 /* variant */ , val[i] /* start value */,
val[i] /* expected */, 42 /* new value */,
val[i] /* return */ , 42 /* end value*/, false /* boolean ret*/);
plain_cmpxchg_test<TESTSIZE, ASMSIZE>( 1 /* variant */ , val[i] /* start value */,
36 /* expected */, 42 /* new value */,
val[i] /* return */ , val[i] /* end value */, false /* boolean ret*/);
plain_cmpxchg_test<TESTSIZE, ASMSIZE>( 1 /* variant */ , val[i] /* start value */,
val[i] /* expected */, 42 /* new value */,
1 /* return */ , 42 /* end value*/, true /* boolean ret*/);
plain_cmpxchg_test<TESTSIZE, ASMSIZE>( 1 /* variant */ , val[i] /* start value */,
36 /* expected */, 42 /* new value */,
0 /* return */ , val[i] /* end value */, true /* boolean ret*/);
// new_value == result register
plain_cmpxchg_test<TESTSIZE, ASMSIZE>( 2 /* variant */ , val[i] /* start value */,
val[i] /* expected */, 42 /* new value */,
val[i] /* return */ , 42 /* end value*/, false /* boolean ret*/);
plain_cmpxchg_test<TESTSIZE, ASMSIZE>( 2 /* variant */ , val[i] /* start value */,
36 /* expected */, 42 /* new value */,
val[i] /* return */ , val[i] /* end value */, false /* boolean ret*/);
plain_cmpxchg_test<TESTSIZE, ASMSIZE>( 2 /* variant */ , val[i] /* start value */,
val[i] /* expected */, 42 /* new value */,
1 /* return */ , 42 /* end value*/, true /* boolean ret*/);
plain_cmpxchg_test<TESTSIZE, ASMSIZE>( 2 /* variant */ , val[i] /* start value */,
36 /* expected */, 42 /* new value */,
0 /* return */ , val[i] /* end value */, true /* boolean ret*/);
// expected == new_value register
plain_cmpxchg_test<TESTSIZE, ASMSIZE>( 3 /* variant */ , val[i] /* start value */,
val[i] /* expected */, 42 /* new value */,
val[i] /* return */ , val[i] /* end value */, false /* boolean ret*/);
plain_cmpxchg_test<TESTSIZE, ASMSIZE>( 3 /* variant */ , val[i] /* start value */,
36 /* expected */, 42 /* new value */,
val[i] /* return */ , val[i] /* end value */, false /* boolean ret*/);
plain_cmpxchg_test<TESTSIZE, ASMSIZE>( 3 /* variant */ , val[i] /* start value */,
val[i] /* expected */, 42 /* new value */,
1 /* return */ , val[i] /* end value */, true /* boolean ret*/);
plain_cmpxchg_test<TESTSIZE, ASMSIZE>( 3 /* variant */ , val[i] /* start value */,
36 /* expected */, 42 /* new value */,
0 /* return */ , val[i] /* end value */, true /* boolean ret*/);
}
}
TEST_VM(RiscV, cmpxchg_int64_lr_sc) {
bool zacas = UseZacas;
UseZacas = false;
run_plain_cmpxchg_tests<int64_t, Assembler::int64>();
UseZacas = zacas;
}
TEST_VM(RiscV, cmpxchg_int64_maybe_zacas) {
if (UseZacas) {
run_plain_cmpxchg_tests<int64_t, Assembler::int64>();
}
}
TEST_VM(RiscV, cmpxchg_int32_lr_sc) {
bool zacas = UseZacas;
UseZacas = false;
run_plain_cmpxchg_tests<int32_t, Assembler::int32>();
UseZacas = zacas;
}
TEST_VM(RiscV, cmpxchg_int32_maybe_zacas) {
if (UseZacas) {
run_plain_cmpxchg_tests<int32_t, Assembler::int32>();
}
}
TEST_VM(RiscV, cmpxchg_uint32_lr_sc) {
bool zacas = UseZacas;
UseZacas = false;
run_plain_cmpxchg_tests<uint32_t, Assembler::uint32>();
UseZacas = zacas;
}
TEST_VM(RiscV, cmpxchg_uint32_maybe_zacas) {
if (UseZacas) {
run_plain_cmpxchg_tests<uint32_t, Assembler::uint32>();
}
}
TEST_VM(RiscV, cmpxchg_int16_maybe_zacas_zabha) {
if (UseZacas && UseZabha) {
run_plain_cmpxchg_tests<int16_t, Assembler::int16>();
}
}
TEST_VM(RiscV, cmpxchg_int8_maybe_zacas_zabha) {
if (UseZacas && UseZabha) {
run_plain_cmpxchg_tests<int8_t, Assembler::int8>();
}
}
template <typename TESTSIZE, Assembler::operand_size ASMSIZE>
static void run_narrow_cmpxchg_tests() {
CmpxchgTester<TESTSIZE, ASMSIZE> cmpxchg(0, false);
CmpxchgTester<TESTSIZE, ASMSIZE> cmpxchg_bool(0, true);
// Assume natural aligned
TESTSIZE data[8];
TESTSIZE ret;
TESTSIZE max = std::numeric_limits<TESTSIZE>::max();
TESTSIZE min = std::numeric_limits<TESTSIZE>::min();
TESTSIZE val[] = {121, min, max};
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 7; j++) {
memset(data, -1, sizeof(data));
data[i] = val[i];
ret = cmpxchg.cmpxchg((intptr_t)&data[i], val[i], 42);
ASSERT_EQ(ret, val[i]);
ASSERT_EQ(data[i], 42);
data[i] = val[i];
ret = cmpxchg.cmpxchg((intptr_t)&data[i], 120, 42);
ASSERT_EQ(ret, val[i]);
ASSERT_EQ(data[i], val[i]);
data[i] = val[i];
ret = cmpxchg_bool.cmpxchg((intptr_t)&data[i], val[i], 42);
ASSERT_EQ(ret, 1);
ASSERT_EQ(data[i], 42);
data[i] = val[i];
ret = cmpxchg_bool.cmpxchg((intptr_t)&data[i], 120, 42);
ASSERT_EQ(ret, 0);
ASSERT_EQ(data[i], val[i]);
}
}
}
TEST_VM(RiscV, cmpxchg_narrow_int16_lr_sc) {
bool zacas = UseZacas;
UseZacas = false;
run_narrow_cmpxchg_tests<int16_t, Assembler::int16>();
UseZacas = zacas;
}
TEST_VM(RiscV, cmpxchg_narrow_int16_maybe_zacas) {
if (UseZacas) {
bool zabha = UseZabha;
UseZabha = false;
run_narrow_cmpxchg_tests<int16_t, Assembler::int16>();
UseZabha = zabha;
}
}
TEST_VM(RiscV, cmpxchg_narrow_int8_lr_sc) {
bool zacas = UseZacas;
UseZacas = false;
run_narrow_cmpxchg_tests<int8_t, Assembler::int8>();
UseZacas = zacas;
}
TEST_VM(RiscV, cmpxchg_narrow_int8_maybe_zacas) {
if (UseZacas) {
bool zabha = UseZabha;
UseZabha = false;
run_narrow_cmpxchg_tests<int8_t, Assembler::int8>();
UseZabha = zabha;
}
}
template <typename TESTSIZE>
TESTSIZE next_count(TESTSIZE now, TESTSIZE add) {
if ((std::numeric_limits<TESTSIZE>::max() - add) >= now) {
return now + add;
}
TESTSIZE diff = std::numeric_limits<TESTSIZE>::max() - now;
add -= diff + 1; // add one to the diff for the wrap around.
return std::numeric_limits<TESTSIZE>::min() + add;
}
constexpr int64_t PAR_IT_END = 10000;
constexpr int64_t NUMBER_THREADS = 4;
constexpr int64_t TOTAL_ITERATIONS = NUMBER_THREADS * PAR_IT_END;
template <typename TESTSIZE, ENABLE_IF(std::numeric_limits<TESTSIZE>::max() <= (std::numeric_limits<TESTSIZE>::min() + TOTAL_ITERATIONS))>
constexpr TESTSIZE result_count() {
int64_t range = std::numeric_limits<TESTSIZE>::max() - std::numeric_limits<TESTSIZE>::min() + 1;
int64_t rest = TOTAL_ITERATIONS % range;
return std::numeric_limits<TESTSIZE>::min() + rest;
}
template <typename TESTSIZE, ENABLE_IF(std::numeric_limits<TESTSIZE>::max() > (std::numeric_limits<TESTSIZE>::min() + TOTAL_ITERATIONS))>
constexpr TESTSIZE result_count() {
return std::numeric_limits<TESTSIZE>::min() + TOTAL_ITERATIONS;
}
template <typename TESTSIZE, Assembler::operand_size ASMSIZE>
static void run_concurrent_cmpxchg_tests() {
volatile TESTSIZE data = std::numeric_limits<TESTSIZE>::min();
int num_threads = NUMBER_THREADS;
CmpxchgTester<TESTSIZE, ASMSIZE> cmpxchg(0, false); // variant 0, not bool ret
auto incThread = [&](Thread* _current, int _id) { // _id starts from 0..(CTHREAD-1)
TESTSIZE my_oldvalue = std::numeric_limits<TESTSIZE>::min() + _id;
for (int64_t i = 0; i < PAR_IT_END ; i++) {
TESTSIZE newvalue = next_count<TESTSIZE>(my_oldvalue, 1);
TESTSIZE ret;
do {
ret = cmpxchg.cmpxchg((intptr_t)&data, my_oldvalue, newvalue);
} while (ret != my_oldvalue);
my_oldvalue = next_count<TESTSIZE>(my_oldvalue, num_threads);
}
};
TestThreadGroup<decltype(incThread)> ttg(incThread, num_threads);
ttg.doit();
ttg.join();
ASSERT_EQ(data, result_count<TESTSIZE>());
}
template <typename TESTSIZE, Assembler::operand_size ASMSIZE>
static void run_concurrent_alt_cmpxchg_tests() {
volatile TESTSIZE data = std::numeric_limits<TESTSIZE>::min();
int num_threads = NUMBER_THREADS;
CmpxchgTester<TESTSIZE, ASMSIZE> cmpxchg(0, false); // variant 0, not bool ret
auto incThread = [&](Thread* _current, int _id) { // _id starts from 0..(CTHREAD-1)
for (int i = 0; i < PAR_IT_END; i++) {
TESTSIZE oldvalue;
TESTSIZE ret = 0;
do {
oldvalue = ret;
TESTSIZE newvalue = next_count<TESTSIZE>(oldvalue, 1);
ret = cmpxchg.cmpxchg((intptr_t)&data, oldvalue, newvalue);
} while (ret != oldvalue);
}
};
TestThreadGroup<decltype(incThread)> ttg(incThread, num_threads);
ttg.doit();
ttg.join();
ASSERT_EQ(data, result_count<TESTSIZE>());
}
TEST_VM(RiscV, cmpxchg_int64_concurrent_lr_sc) {
bool zacas = UseZacas;
UseZacas = false;
run_concurrent_cmpxchg_tests<int64_t, Assembler::int64>();
run_concurrent_alt_cmpxchg_tests<int64_t, Assembler::int64>();
UseZacas = zacas;
}
TEST_VM(RiscV, cmpxchg_int64_concurrent_maybe_zacas) {
if (UseZacas) {
run_concurrent_cmpxchg_tests<int64_t, Assembler::int64>();
run_concurrent_alt_cmpxchg_tests<int64_t, Assembler::int64>();
}
}
TEST_VM(RiscV, cmpxchg_int32_concurrent_lr_sc) {
bool zacas = UseZacas;
UseZacas = false;
run_concurrent_cmpxchg_tests<int32_t, Assembler::int32>();
run_concurrent_alt_cmpxchg_tests<int32_t, Assembler::int32>();
UseZacas = zacas;
}
TEST_VM(RiscV, cmpxchg_int32_concurrent_maybe_zacas) {
if (UseZacas) {
run_concurrent_cmpxchg_tests<int32_t, Assembler::int32>();
run_concurrent_alt_cmpxchg_tests<int32_t, Assembler::int32>();
}
}
TEST_VM(RiscV, cmpxchg_uint32_concurrent_lr_sc) {
bool zacas = UseZacas;
UseZacas = false;
run_concurrent_cmpxchg_tests<uint32_t, Assembler::uint32>();
run_concurrent_alt_cmpxchg_tests<uint32_t, Assembler::uint32>();
UseZacas = zacas;
}
TEST_VM(RiscV, cmpxchg_uint32_concurrent_maybe_zacas) {
if (UseZacas) {
run_concurrent_cmpxchg_tests<uint32_t, Assembler::uint32>();
run_concurrent_alt_cmpxchg_tests<uint32_t, Assembler::uint32>();
}
}
TEST_VM(RiscV, cmpxchg_narrow_int16_concurrent_lr_sc) {
bool zacas = UseZacas;
UseZacas = false;
run_concurrent_cmpxchg_tests<int16_t, Assembler::int16>();
run_concurrent_alt_cmpxchg_tests<int16_t, Assembler::int16>();
UseZacas = zacas;
}
TEST_VM(RiscV, cmpxchg_narrow_int16_concurrent_maybe_zacas) {
if (UseZacas) {
bool zabha = UseZabha;
UseZabha = false;
run_concurrent_cmpxchg_tests<int16_t, Assembler::int16>();
run_concurrent_alt_cmpxchg_tests<int16_t, Assembler::int16>();
UseZabha = zabha;
}
}
TEST_VM(RiscV, cmpxchg_narrow_int8_concurrent_lr_sc) {
bool zacas = UseZacas;
UseZacas = false;
run_concurrent_cmpxchg_tests<int8_t, Assembler::int8>();
run_concurrent_alt_cmpxchg_tests<int8_t, Assembler::int8>();
UseZacas = zacas;
}
TEST_VM(RiscV, cmpxchg_narrow_int8_concurrent_maybe_zacas) {
if (UseZacas) {
bool zabha = UseZabha;
UseZabha = false;
run_concurrent_cmpxchg_tests<int8_t, Assembler::int8>();
run_concurrent_alt_cmpxchg_tests<int8_t, Assembler::int8>();
UseZabha = zabha;
}
}
TEST_VM(RiscV, cmpxchg_int16_concurrent_maybe_zacas_zabha) {
if (UseZacas && UseZabha) {
run_concurrent_cmpxchg_tests<int16_t, Assembler::int16>();
run_concurrent_alt_cmpxchg_tests<int16_t, Assembler::int16>();
}
}
TEST_VM(RiscV, cmpxchg_int8_concurrent_maybe_zacas_zabha) {
if (UseZacas && UseZabha) {
run_concurrent_cmpxchg_tests<int8_t, Assembler::int8>();
run_concurrent_alt_cmpxchg_tests<int8_t, Assembler::int8>();
}
}
template <typename TESTSIZE, Assembler::operand_size ASMSIZE>
class WeakCmpxchgTester {
// The functions expect arguments to be type represented, not C-ABI argument representation.
// Hence an unsigned should be zero-extended, and the same goes for the return value.
typedef int64_t (*weak_cmpxchg_narrow_func)(intptr_t addr, int64_t expected, int64_t new_value, int64_t result,
int64_t scratch0, int64_t scratch1, int64_t scratch2);
typedef int64_t (*weak_cmpxchg_func)(intptr_t addr, int64_t expected, int64_t new_value, int64_t result);
BufferBlob* _bb;
weak_cmpxchg_narrow_func _narrow_weak;
weak_cmpxchg_func _weak;
public:
WeakCmpxchgTester() : _bb(nullptr), _narrow_weak(nullptr), _weak(nullptr) {
_bb = BufferBlob::create("riscvTest", 128);
CodeBuffer code(_bb);
MacroAssembler _masm(&code);
if (using_narrow<ASMSIZE>()) {
address entry = _masm.pc();
_masm.weak_cmpxchg_narrow_value(/*addr*/ c_rarg0, /*expected*/ c_rarg1, /*new_value*/ c_rarg2,
ASMSIZE, Assembler::relaxed, Assembler::relaxed,
/*result*/ c_rarg3, c_rarg4, c_rarg5, c_rarg6); /* Uses also t0-t1, caller saved */
_masm.mv(c_rarg0, c_rarg3);
_masm.ret();
_narrow_weak = ((weak_cmpxchg_narrow_func)entry);
} else {
address entry = _masm.pc();
_masm.weak_cmpxchg(/*addr*/ c_rarg0, /*expected*/ c_rarg1, /*new_value*/ c_rarg2,
ASMSIZE, Assembler::relaxed, Assembler::relaxed, /*result*/ c_rarg3);
_masm.mv(c_rarg0, c_rarg3);
_masm.ret();
_weak = ((weak_cmpxchg_func)entry);
}
_masm.flush(); // icache invalidate
}
TESTSIZE weak_cmpxchg(intptr_t addr, TESTSIZE expected, TESTSIZE new_value) {
if (using_narrow<ASMSIZE>()) {
return _narrow_weak(addr, expected, new_value, /* dummy result */ 67, -1, -1, -1);
} else {
return _weak(addr, expected, new_value, /* dummy result */ 67);
}
}
~WeakCmpxchgTester() {
BufferBlob::free(_bb);
}
};
template <typename TESTSIZE, Assembler::operand_size ASMSIZE>
void run_weak_cmpxchg_tests() {
TESTSIZE max = std::numeric_limits<TESTSIZE>::max();
TESTSIZE min = std::numeric_limits<TESTSIZE>::min();
TESTSIZE val[] = {121, min, max};
for (int i = 0; i < 3; i++) {
WeakCmpxchgTester<TESTSIZE, ASMSIZE> cmpxchg;
TESTSIZE data = val[i];
TESTSIZE ret = cmpxchg.weak_cmpxchg((intptr_t)&data, val[i], 42);
ASSERT_EQ(ret, (TESTSIZE)1);
ASSERT_EQ(data, (TESTSIZE)42);
data = val[i];
ret = cmpxchg.weak_cmpxchg((intptr_t)&data, 120, 42);
ASSERT_EQ(ret, (TESTSIZE)0);
ASSERT_EQ(data, (TESTSIZE)val[i]);
}
}
TEST_VM(RiscV, weak_cmpxchg_int64_lr_sc) {
bool zacas = UseZacas;
UseZacas = false;
run_weak_cmpxchg_tests<int64_t, Assembler::int64>();
UseZacas = zacas;
}
TEST_VM(RiscV, weak_cmpxchg_int64_maybe_zacas) {
if (UseZacas) {
run_weak_cmpxchg_tests<int64_t, Assembler::int64>();
}
}
TEST_VM(RiscV, weak_cmpxchg_int32_lr_sc) {
bool zacas = UseZacas;
UseZacas = false;
run_weak_cmpxchg_tests<int32_t, Assembler::int32>();
UseZacas = zacas;
}
TEST_VM(RiscV, weak_cmpxchg_int32_maybe_zacas) {
if (UseZacas) {
run_weak_cmpxchg_tests<int32_t, Assembler::int32>();
}
}
TEST_VM(RiscV, weak_cmpxchg_uint32_lr_sc) {
bool zacas = UseZacas;
UseZacas = false;
run_weak_cmpxchg_tests<uint32_t, Assembler::uint32>();
UseZacas = zacas;
}
TEST_VM(RiscV, weak_cmpxchg_uint32_maybe_zacas) {
if (UseZacas) {
run_weak_cmpxchg_tests<uint32_t, Assembler::uint32>();
}
}
TEST_VM(RiscV, weak_cmpxchg_narrow_int16_lr_sc) {
bool zacas = UseZacas;
UseZacas = false;
run_weak_cmpxchg_tests<int16_t, Assembler::int16>();
UseZacas = zacas;
}
TEST_VM(RiscV, weak_cmpxchg_narrow_int8_lr_sc) {
bool zacas = UseZacas;
UseZacas = false;
run_weak_cmpxchg_tests<int8_t, Assembler::int8>();
UseZacas = zacas;
}
TEST_VM(RiscV, weak_cmpxchg_narrow_int16_maybe_zacas) {
if (UseZacas) {
bool zabha = UseZabha;
UseZabha = false;
run_weak_cmpxchg_tests<int16_t, Assembler::int16>();
UseZabha = zabha;
}
}
TEST_VM(RiscV, weak_cmpxchg_narrow_int8_maybe_zacas) {
if (UseZacas) {
bool zabha = UseZabha;
UseZabha = false;
run_weak_cmpxchg_tests<int8_t, Assembler::int8>();
UseZabha = zabha;
}
}
TEST_VM(RiscV, weak_cmpxchg_int16_maybe_zacas_zabha) {
if (UseZacas && UseZabha) {
run_weak_cmpxchg_tests<int16_t, Assembler::int16>();
}
}
TEST_VM(RiscV, weak_cmpxchg_int8_maybe_zacas_zabha) {
if (UseZacas && UseZabha) {
run_weak_cmpxchg_tests<int8_t, Assembler::int8>();
}
}
template <typename TESTSIZE, Assembler::operand_size ASMSIZE>
static void run_concurrent_weak_cmpxchg_tests() {
volatile TESTSIZE data = std::numeric_limits<TESTSIZE>::min();
int num_threads = NUMBER_THREADS;
WeakCmpxchgTester<TESTSIZE, ASMSIZE> cmpxchg; // not bool ret
auto incThread = [&](Thread* _current, int _id) { // _id starts from 0..(CTHREAD-1)
TESTSIZE my_oldvalue = std::numeric_limits<TESTSIZE>::min() + _id;
for (int64_t i = 0; i < PAR_IT_END; i++) {
TESTSIZE newvalue = next_count<TESTSIZE>(my_oldvalue, 1);
TESTSIZE ret;
do {
ret = cmpxchg.weak_cmpxchg((intptr_t)&data, my_oldvalue, newvalue);
} while (ret != 1);
my_oldvalue = next_count<TESTSIZE>(my_oldvalue, num_threads);
}
};
TestThreadGroup<decltype(incThread)> ttg(incThread, num_threads);
ttg.doit();
ttg.join();
ASSERT_EQ(data, result_count<TESTSIZE>());
}
template <typename TESTSIZE, Assembler::operand_size ASMSIZE>
static void run_concurrent_alt_weak_cmpxchg_tests() {
volatile TESTSIZE data = std::numeric_limits<TESTSIZE>::min();
int num_threads = NUMBER_THREADS;
WeakCmpxchgTester<TESTSIZE, ASMSIZE> cmpxchg; // not bool ret
auto incThread = [&](Thread* _current, int _id) { // _id starts from 0..(CTHREAD-1)
for (int i = 0; i < PAR_IT_END; i++) {
TESTSIZE oldvalue;
TESTSIZE ret = 0;
do {
oldvalue = data;
TESTSIZE newvalue = next_count<TESTSIZE>(oldvalue, 1);
ret = cmpxchg.weak_cmpxchg((intptr_t)&data, oldvalue, newvalue);
} while (ret != 1);
}
};
TestThreadGroup<decltype(incThread)> ttg(incThread, num_threads);
ttg.doit();
ttg.join();
ASSERT_EQ(data, result_count<TESTSIZE>());
}
TEST_VM(RiscV, weak_cmpxchg_int64_concurrent_lr_sc) {
bool zacas = UseZacas;
UseZacas = false;
run_concurrent_weak_cmpxchg_tests<int64_t, Assembler::int64>();
run_concurrent_alt_weak_cmpxchg_tests<int64_t, Assembler::int64>();
UseZacas = zacas;
}
TEST_VM(RiscV, weak_cmpxchg_int64_concurrent_maybe_zacas) {
if (UseZacas) {
run_concurrent_weak_cmpxchg_tests<int64_t, Assembler::int64>();
run_concurrent_alt_weak_cmpxchg_tests<int64_t, Assembler::int64>();
}
}
TEST_VM(RiscV, weak_cmpxchg_int32_concurrent_lr_sc) {
bool zacas = UseZacas;
UseZacas = false;
run_concurrent_weak_cmpxchg_tests<int32_t, Assembler::int32>();
run_concurrent_alt_weak_cmpxchg_tests<int32_t, Assembler::int32>();
UseZacas = zacas;
}
TEST_VM(RiscV, weak_cmpxchg_int32_concurrent_maybe_zacas) {
if (UseZacas) {
run_concurrent_weak_cmpxchg_tests<int32_t, Assembler::int32>();
run_concurrent_alt_weak_cmpxchg_tests<int32_t, Assembler::int32>();
}
}
TEST_VM(RiscV, weak_cmpxchg_narrow_int16_concurrent_lr_sc) {
bool zacas = UseZacas;
UseZacas = false;
run_concurrent_weak_cmpxchg_tests<int16_t, Assembler::int16>();
run_concurrent_alt_weak_cmpxchg_tests<int16_t, Assembler::int16>();
UseZacas = zacas;
}
TEST_VM(RiscV, weak_cmpxchg_narrow_int16_concurrent_maybe_zacas) {
if (UseZacas) {
bool zabha = UseZabha;
UseZabha = false;
run_concurrent_weak_cmpxchg_tests<int16_t, Assembler::int16>();
run_concurrent_alt_weak_cmpxchg_tests<int16_t, Assembler::int16>();
UseZabha = zabha;
}
}
TEST_VM(RiscV, weak_cmpxchg_narrow_int8_concurrent_lr_sc) {
bool zacas = UseZacas;
UseZacas = false;
run_concurrent_weak_cmpxchg_tests<int8_t, Assembler::int8>();
run_concurrent_alt_weak_cmpxchg_tests<int8_t, Assembler::int8>();
UseZacas = zacas;
}
TEST_VM(RiscV, weak_cmpxchg_narrow_int8_concurrent_maybe_zacas) {
if (UseZacas) {
bool zabha = UseZabha;
UseZabha = false;
run_concurrent_weak_cmpxchg_tests<int8_t, Assembler::int8>();
run_concurrent_alt_weak_cmpxchg_tests<int8_t, Assembler::int8>();
UseZabha = zabha;
}
}
TEST_VM(RiscV, weak_cmpxchg_int16_concurrent_maybe_zacas_zabha) {
if (UseZacas && UseZabha) {
run_concurrent_weak_cmpxchg_tests<int16_t, Assembler::int16>();
run_concurrent_alt_weak_cmpxchg_tests<int16_t, Assembler::int16>();
}
}
TEST_VM(RiscV, weak_cmpxchg_int8_concurrent_maybe_zacas_zabha) {
if (UseZacas && UseZabha) {
run_concurrent_weak_cmpxchg_tests<int8_t, Assembler::int8>();
run_concurrent_alt_weak_cmpxchg_tests<int8_t, Assembler::int8>();
}
}
#endif // RISCV
|