From e129a9955a8e4918c796556cb087c09f64f9034f Mon Sep 17 00:00:00 2001 From: Robbin Ehn Date: Thu, 28 Nov 2024 14:58:50 +0100 Subject: [PATCH 1/3] gtest narrow cmpxchg --- .../gtest/riscv/test_assembler_riscv.cpp | 87 +++++++++++++++++++ 1 file changed, 87 insertions(+) diff --git a/test/hotspot/gtest/riscv/test_assembler_riscv.cpp b/test/hotspot/gtest/riscv/test_assembler_riscv.cpp index 152b997b3c8cd..670b873643423 100644 --- a/test/hotspot/gtest/riscv/test_assembler_riscv.cpp +++ b/test/hotspot/gtest/riscv/test_assembler_riscv.cpp @@ -106,4 +106,91 @@ TEST_VM(RiscV, cmov) { } } +template +class NarrowCmpxchgTester { + public: + typedef TESTSIZE (*cmpxchg_func)(intptr_t addr, TESTSIZE expected, TESTSIZE new_value, TESTSIZE result, + int64_t scratch0, int64_t scratch1, int64_t scratch2); + + static TESTSIZE narrow_cmpxchg(intptr_t addr, TESTSIZE expected, TESTSIZE new_value, TESTSIZE result, + int64_t scratch0, int64_t scratch1, int64_t scratch2, bool boolean_result = false) { + BufferBlob* bb = BufferBlob::create("riscvTest", 128); + CodeBuffer code(bb); + MacroAssembler _masm(&code); + 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(); + } + _masm.flush(); + OrderAccess::cross_modify_fence(); + TESTSIZE ret = ((cmpxchg_func)entry)(addr, expected, new_value, result, scratch0, scratch1, scratch2); + BufferBlob::free(bb); + return ret; + } +}; + +template +void run_narrow_cmpxchg_tests() { + // Assume natural aligned + TESTSIZE data[8]; + TESTSIZE ret; + for (int i = 0; i < 7; i++) { + memset(data, -1, sizeof(data)); + + data[i] = 121; + ret = NarrowCmpxchgTester::narrow_cmpxchg((intptr_t)&data[i], 121, 42, /* result */ 67, + -1, -1, -1, false); + ASSERT_EQ(ret, 121); + ASSERT_EQ(data[i], 42); + + data[i] = 121; + ret = NarrowCmpxchgTester::narrow_cmpxchg((intptr_t)&data[i], 120, 42, /* result */ 67, + -1, -1, -1, false); + ASSERT_EQ(ret, 121); + ASSERT_EQ(data[i], 121); + + data[i] = 121; + ret = NarrowCmpxchgTester::narrow_cmpxchg((intptr_t)&data[i], 121, 42, /* result */ 67, + -1, -1, -1, true); + ASSERT_EQ(ret, 1); + ASSERT_EQ(data[i], 42); + + data[i] = 121; + ret = NarrowCmpxchgTester::narrow_cmpxchg((intptr_t)&data[i], 120, 42, /* result */ 67, + -1, -1, -1, true); + ASSERT_EQ(ret, 0); + ASSERT_EQ(data[i], 121); + } +} + +TEST_VM(RiscV, cmpxchg_int16_lr_sc) { + bool zacas = UseZacas; + UseZacas = false; + run_narrow_cmpxchg_tests(); + UseZacas = zacas; +} + +TEST_VM(RiscV, cmpxchg_int8_lr_sc) { + bool zacas = UseZacas; + UseZacas = false; + run_narrow_cmpxchg_tests(); + UseZacas = zacas; +} + +TEST_VM(RiscV, cmpxchg_int16_maybe_zacas) { + if (UseZacas) { + run_narrow_cmpxchg_tests(); + } +} + +TEST_VM(RiscV, cmpxchg_int8_maybe_zacas) { + if (UseZacas) { + run_narrow_cmpxchg_tests(); + } +} + #endif // RISCV From b07b2c10bf522946f4a7ca054b79df1bf047cb73 Mon Sep 17 00:00:00 2001 From: Robbin Ehn Date: Fri, 29 Nov 2024 14:57:45 +0100 Subject: [PATCH 2/3] Remove CMF barrier --- test/hotspot/gtest/riscv/test_assembler_riscv.cpp | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/test/hotspot/gtest/riscv/test_assembler_riscv.cpp b/test/hotspot/gtest/riscv/test_assembler_riscv.cpp index 670b873643423..bebfa88815f78 100644 --- a/test/hotspot/gtest/riscv/test_assembler_riscv.cpp +++ b/test/hotspot/gtest/riscv/test_assembler_riscv.cpp @@ -47,8 +47,7 @@ class CmovTester { _masm.mv(c_rarg0, c_rarg2); _masm.ret(); } - _masm.flush(); - OrderAccess::cross_modify_fence(); + _masm.flush(); // icache invalidate int64_t ret = ((zicond_func)entry)(a0, a1, a2, a3); ASSERT_EQ(ret, result); BufferBlob::free(bb); @@ -125,8 +124,7 @@ class NarrowCmpxchgTester { _masm.mv(c_rarg0, c_rarg3); _masm.ret(); } - _masm.flush(); - OrderAccess::cross_modify_fence(); + _masm.flush(); // icache invalidate TESTSIZE ret = ((cmpxchg_func)entry)(addr, expected, new_value, result, scratch0, scratch1, scratch2); BufferBlob::free(bb); return ret; From 22e8c4e61d488d79e7c1d4289fb34508c1eacca5 Mon Sep 17 00:00:00 2001 From: Robbin Ehn Date: Mon, 2 Dec 2024 14:04:49 +0100 Subject: [PATCH 3/3] Review comment --- .../gtest/riscv/test_assembler_riscv.cpp | 17 ++++++----------- 1 file changed, 6 insertions(+), 11 deletions(-) diff --git a/test/hotspot/gtest/riscv/test_assembler_riscv.cpp b/test/hotspot/gtest/riscv/test_assembler_riscv.cpp index bebfa88815f78..ca0cbe9b46d05 100644 --- a/test/hotspot/gtest/riscv/test_assembler_riscv.cpp +++ b/test/hotspot/gtest/riscv/test_assembler_riscv.cpp @@ -111,8 +111,7 @@ class NarrowCmpxchgTester { typedef TESTSIZE (*cmpxchg_func)(intptr_t addr, TESTSIZE expected, TESTSIZE new_value, TESTSIZE result, int64_t scratch0, int64_t scratch1, int64_t scratch2); - static TESTSIZE narrow_cmpxchg(intptr_t addr, TESTSIZE expected, TESTSIZE new_value, TESTSIZE result, - int64_t scratch0, int64_t scratch1, int64_t scratch2, bool boolean_result = false) { + static TESTSIZE narrow_cmpxchg(intptr_t addr, TESTSIZE expected, TESTSIZE new_value, TESTSIZE result, bool boolean_result = false) { BufferBlob* bb = BufferBlob::create("riscvTest", 128); CodeBuffer code(bb); MacroAssembler _masm(&code); @@ -125,7 +124,7 @@ class NarrowCmpxchgTester { _masm.ret(); } _masm.flush(); // icache invalidate - TESTSIZE ret = ((cmpxchg_func)entry)(addr, expected, new_value, result, scratch0, scratch1, scratch2); + TESTSIZE ret = ((cmpxchg_func)entry)(addr, expected, new_value, result, -1, -1, -1); BufferBlob::free(bb); return ret; } @@ -140,26 +139,22 @@ void run_narrow_cmpxchg_tests() { memset(data, -1, sizeof(data)); data[i] = 121; - ret = NarrowCmpxchgTester::narrow_cmpxchg((intptr_t)&data[i], 121, 42, /* result */ 67, - -1, -1, -1, false); + ret = NarrowCmpxchgTester::narrow_cmpxchg((intptr_t)&data[i], 121, 42, /* result */ 67, false); ASSERT_EQ(ret, 121); ASSERT_EQ(data[i], 42); data[i] = 121; - ret = NarrowCmpxchgTester::narrow_cmpxchg((intptr_t)&data[i], 120, 42, /* result */ 67, - -1, -1, -1, false); + ret = NarrowCmpxchgTester::narrow_cmpxchg((intptr_t)&data[i], 120, 42, /* result */ 67, false); ASSERT_EQ(ret, 121); ASSERT_EQ(data[i], 121); data[i] = 121; - ret = NarrowCmpxchgTester::narrow_cmpxchg((intptr_t)&data[i], 121, 42, /* result */ 67, - -1, -1, -1, true); + ret = NarrowCmpxchgTester::narrow_cmpxchg((intptr_t)&data[i], 121, 42, /* result */ 67, true); ASSERT_EQ(ret, 1); ASSERT_EQ(data[i], 42); data[i] = 121; - ret = NarrowCmpxchgTester::narrow_cmpxchg((intptr_t)&data[i], 120, 42, /* result */ 67, - -1, -1, -1, true); + ret = NarrowCmpxchgTester::narrow_cmpxchg((intptr_t)&data[i], 120, 42, /* result */ 67, true); ASSERT_EQ(ret, 0); ASSERT_EQ(data[i], 121); }