Skip to content

Commit

Permalink
base
Browse files Browse the repository at this point in the history
  • Loading branch information
robehn committed Dec 4, 2024
1 parent 9e2b66f commit 919d445
Showing 1 changed file with 70 additions and 12 deletions.
82 changes: 70 additions & 12 deletions test/hotspot/gtest/riscv/test_assembler_riscv.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
#include "asm/macroAssembler.hpp"
#include "memory/resourceArea.hpp"
#include "runtime/orderAccess.hpp"
#include "threadHelper.inline.hpp"
#include "unittest.hpp"

typedef int64_t (*zicond_func)(int64_t cmp1, int64_t cmp2, int64_t dst, int64_t src);
Expand Down Expand Up @@ -107,40 +108,43 @@ TEST_VM(RiscV, cmov) {

template <typename TESTSIZE, Assembler::operand_size ASMSIZE>
class CmpxchgTester {
public:
typedef TESTSIZE (*cmpxchg_func)(intptr_t addr, TESTSIZE expected, TESTSIZE new_value, TESTSIZE result);

static TESTSIZE base_cmpxchg(int variant, intptr_t addr, TESTSIZE expected, TESTSIZE new_value, TESTSIZE result, bool boolean_result = false) {
BufferBlob* bb = BufferBlob::create("riscvTest", 128);
CodeBuffer code(bb);
BufferBlob* _bb;
cmpxchg_func _func;

public:
CmpxchgTester(int variant, bool boolean_result) {
_bb = BufferBlob::create("riscvTest", 128);
CodeBuffer code(_bb);
MacroAssembler _masm(&code);
address entry = _masm.pc();
{
switch(variant) {
default:
_masm.cmpxchg(/*addr*/ c_rarg0, /*expected*/ c_rarg1, /*new_value*/c_rarg2,
ASMSIZE, Assembler::relaxed, Assembler::relaxed,
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::relaxed, Assembler::relaxed,
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::relaxed, Assembler::relaxed,
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::relaxed, Assembler::relaxed,
ASMSIZE, Assembler::aq, Assembler::rl,
/*result*/ c_rarg2, boolean_result);
_masm.mv(c_rarg0, c_rarg2);
break;
Expand All @@ -149,16 +153,23 @@ class CmpxchgTester {
_masm.ret();
}
_masm.flush(); // icache invalidate
TESTSIZE ret = ((cmpxchg_func)entry)(addr, expected, new_value, result);
BufferBlob::free(bb);
return ret;
_func = ((cmpxchg_func)entry);
}

~CmpxchgTester() {
BufferBlob::free(_bb);
}

TESTSIZE cmpxchg(intptr_t addr, TESTSIZE expected, TESTSIZE new_value) {
return _func(addr, expected, new_value, /* dummy result */ 67);
}
};

template <typename TESTSIZE, Assembler::operand_size ASMSIZE>
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 = CmpxchgTester<TESTSIZE, ASMSIZE>::base_cmpxchg(variant, (intptr_t)&data, ex, nv, /* dummy */ 67, bv);
TESTSIZE ret = cmpxchg.cmpxchg((intptr_t)&data, ex, nv);
ASSERT_EQ(ret, eret);
ASSERT_EQ(data, edata);
}
Expand Down Expand Up @@ -260,6 +271,53 @@ TEST_VM(RiscV, cmpxchg_int32_plain_maybe_zacas) {
}
}

template <typename TESTSIZE, Assembler::operand_size ASMSIZE>
void run_concurrent_cmpxchg_tests() {
volatile TESTSIZE data = 0;
int num_threads = 4;
CmpxchgTester<TESTSIZE, ASMSIZE> cmpxchg(0, false); // variant 0, not bool ret
auto incThread = [&](Thread* _current, int _id) {
for (int i = 0; i < 1000; i++) {
TESTSIZE oldvalue = _id + num_threads * i;
TESTSIZE newvalue = oldvalue + 1;
TESTSIZE ret;
do {
ret = cmpxchg.cmpxchg((intptr_t)&data, oldvalue, newvalue);
} while (ret != oldvalue);
break;
}
};
TestThreadGroup<decltype(incThread)> ttg(incThread, num_threads);
ttg.doit();
ttg.join();
}

TEST_VM(RiscV, cmpxchg_int64_concurrent_lr_sc) {
bool zacas = UseZacas;
UseZacas = false;
run_concurrent_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>();
}
}

TEST_VM(RiscV, cmpxchg_int32_concurrent_lr_sc) {
bool zacas = UseZacas;
UseZacas = false;
run_concurrent_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>();
}
}

template <typename TESTSIZE, Assembler::operand_size ASMSIZE>
class NarrowCmpxchgTester {
public:
Expand Down

0 comments on commit 919d445

Please sign in to comment.