Skip to content

Commit 4465361

Browse files
luhenryRealFYang
authored andcommitted
8295948: Support for Zicbop/prefetch instructions on RISC-V
Reviewed-by: fyang, yadongwang
1 parent f2acdfd commit 4465361

File tree

3 files changed

+83
-3
lines changed

3 files changed

+83
-3
lines changed

src/hotspot/cpu/riscv/riscv.ad

+39
Original file line numberDiff line numberDiff line change
@@ -4289,6 +4289,16 @@ pipe_class ldiv_reg_reg(iRegI dst, iRegI src1, iRegI src2)
42894289

42904290
//------- Load pipeline operations ------------------------
42914291

4292+
// Load - prefetch
4293+
// Eg. PREFETCH_W mem
4294+
pipe_class iload_prefetch(memory mem)
4295+
%{
4296+
single_instruction;
4297+
mem : ID(read);
4298+
DECODE : ID;
4299+
LDST : MEM;
4300+
%}
4301+
42924302
// Load - reg, mem
42934303
// E.g. LA Rd, mem
42944304
pipe_class iload_reg_mem(iRegI dst, memory mem)
@@ -5190,6 +5200,35 @@ instruct storeNKlass(iRegN src, memory mem)
51905200
ins_pipe(istore_reg_mem);
51915201
%}
51925202

5203+
// ============================================================================
5204+
// Prefetch instructions
5205+
// Must be safe to execute with invalid address (cannot fault).
5206+
5207+
instruct prefetchalloc( memory mem ) %{
5208+
predicate(UseZicbop);
5209+
match(PrefetchAllocation mem);
5210+
5211+
ins_cost(ALU_COST * 1);
5212+
format %{ "prefetch_w $mem\t# Prefetch for write" %}
5213+
5214+
ins_encode %{
5215+
if (is_imm_in_range($mem$$disp, 12, 0)) {
5216+
if (($mem$$disp & 0x1f) == 0) {
5217+
__ prefetch_w(as_Register($mem$$base), $mem$$disp);
5218+
} else {
5219+
__ addi(t0, as_Register($mem$$base), $mem$$disp);
5220+
__ prefetch_w(t0, 0);
5221+
}
5222+
} else {
5223+
__ mv(t0, $mem$$disp);
5224+
__ add(t0, as_Register($mem$$base), t0);
5225+
__ prefetch_w(t0, 0);
5226+
}
5227+
%}
5228+
5229+
ins_pipe(iload_prefetch);
5230+
%}
5231+
51935232
// ============================================================================
51945233
// Atomic operation instructions
51955234
//

src/hotspot/cpu/riscv/vm_version_riscv.cpp

+36-2
Original file line numberDiff line numberDiff line change
@@ -237,9 +237,43 @@ void VM_Version::c2_initialize() {
237237
}
238238
}
239239

240-
// disable prefetch
241-
if (FLAG_IS_DEFAULT(AllocatePrefetchStyle)) {
240+
if (!UseZicbop) {
241+
if (!FLAG_IS_DEFAULT(AllocatePrefetchStyle)) {
242+
warning("Zicbop is not available on this CPU");
243+
}
242244
FLAG_SET_DEFAULT(AllocatePrefetchStyle, 0);
245+
} else {
246+
// Limit AllocatePrefetchDistance so that it does not exceed the
247+
// constraint in AllocatePrefetchDistanceConstraintFunc.
248+
if (FLAG_IS_DEFAULT(AllocatePrefetchDistance)) {
249+
FLAG_SET_DEFAULT(AllocatePrefetchDistance, MIN2(512, 3 * (int)CacheLineSize));
250+
}
251+
if (FLAG_IS_DEFAULT(AllocatePrefetchStepSize)) {
252+
FLAG_SET_DEFAULT(AllocatePrefetchStepSize, (int)CacheLineSize);
253+
}
254+
if (FLAG_IS_DEFAULT(PrefetchScanIntervalInBytes)) {
255+
FLAG_SET_DEFAULT(PrefetchScanIntervalInBytes, 3 * (int)CacheLineSize);
256+
}
257+
if (FLAG_IS_DEFAULT(PrefetchCopyIntervalInBytes)) {
258+
FLAG_SET_DEFAULT(PrefetchCopyIntervalInBytes, 3 * (int)CacheLineSize);
259+
}
260+
261+
if (PrefetchCopyIntervalInBytes != -1 &&
262+
((PrefetchCopyIntervalInBytes & 7) || (PrefetchCopyIntervalInBytes >= 32768))) {
263+
warning("PrefetchCopyIntervalInBytes must be -1, or a multiple of 8 and < 32768");
264+
PrefetchCopyIntervalInBytes &= ~7;
265+
if (PrefetchCopyIntervalInBytes >= 32768) {
266+
PrefetchCopyIntervalInBytes = 32760;
267+
}
268+
}
269+
if (AllocatePrefetchDistance !=-1 && (AllocatePrefetchDistance & 7)) {
270+
warning("AllocatePrefetchDistance must be multiple of 8");
271+
AllocatePrefetchDistance &= ~7;
272+
}
273+
if (AllocatePrefetchStepSize & 7) {
274+
warning("AllocatePrefetchStepSize must be multiple of 8");
275+
AllocatePrefetchStepSize &= ~7;
276+
}
243277
}
244278

245279
if (FLAG_IS_DEFAULT(UseMulAddIntrinsic)) {

src/hotspot/os_cpu/linux_riscv/prefetch_linux_riscv.inline.hpp

+8-1
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,18 @@
2828

2929
#include "runtime/prefetch.hpp"
3030

31-
3231
inline void Prefetch::read (const void *loc, intx interval) {
32+
if (interval >= 0 && UseZicbop) {
33+
// encoding for prefetch.r
34+
asm("ori zero, %0, 1" : : "r"(intptr_t(loc)+interval));
35+
}
3336
}
3437

3538
inline void Prefetch::write(void *loc, intx interval) {
39+
if (interval >= 0 && UseZicbop) {
40+
// encoding for prefetch.w
41+
asm("ori zero, %0, 3" : : "r"(intptr_t(loc)+interval));
42+
}
3643
}
3744

3845
#endif // OS_CPU_LINUX_RISCV_VM_PREFETCH_LINUX_RISCV_INLINE_HPP

0 commit comments

Comments
 (0)