Skip to content

Commit af564e4

Browse files
Yadong WangRealFYang
Yadong Wang
authored andcommitted
8299844: RISC-V: Implement _onSpinWait intrinsic
Reviewed-by: fjiang, fyang, luhenry
1 parent 5dfc4ec commit af564e4

File tree

8 files changed

+133
-13
lines changed

8 files changed

+133
-13
lines changed

src/hotspot/cpu/riscv/assembler_riscv.hpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
* Copyright (c) 1997, 2022, Oracle and/or its affiliates. All rights reserved.
33
* Copyright (c) 2014, 2020, Red Hat Inc. All rights reserved.
4-
* Copyright (c) 2020, 2022, Huawei Technologies Co., Ltd. All rights reserved.
4+
* Copyright (c) 2020, 2023, Huawei Technologies Co., Ltd. All rights reserved.
55
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
66
*
77
* This code is free software; you can redistribute it and/or modify it
@@ -682,13 +682,13 @@ class Assembler : public AbstractAssembler {
682682
unsigned insn = 0;
683683
guarantee(predecessor < 16, "predecessor is invalid");
684684
guarantee(successor < 16, "successor is invalid");
685-
patch((address)&insn, 6, 0, 0b001111);
686-
patch((address)&insn, 11, 7, 0b00000);
685+
patch((address)&insn, 6, 0, 0b001111); // opcode
686+
patch((address)&insn, 11, 7, 0b00000); // rd
687687
patch((address)&insn, 14, 12, 0b000);
688-
patch((address)&insn, 19, 15, 0b00000);
689-
patch((address)&insn, 23, 20, successor);
690-
patch((address)&insn, 27, 24, predecessor);
691-
patch((address)&insn, 31, 28, 0b0000);
688+
patch((address)&insn, 19, 15, 0b00000); // rs1
689+
patch((address)&insn, 23, 20, successor); // succ
690+
patch((address)&insn, 27, 24, predecessor); // pred
691+
patch((address)&insn, 31, 28, 0b0000); // fm
692692
emit(insn);
693693
}
694694

src/hotspot/cpu/riscv/c1_LIRAssembler_riscv.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
* Copyright (c) 2000, 2022, Oracle and/or its affiliates. All rights reserved.
33
* Copyright (c) 2014, 2020, Red Hat Inc. All rights reserved.
4-
* Copyright (c) 2020, 2022, Huawei Technologies Co., Ltd. All rights reserved.
4+
* Copyright (c) 2020, 2023, Huawei Technologies Co., Ltd. All rights reserved.
55
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
66
*
77
* This code is free software; you can redistribute it and/or modify it
@@ -1924,7 +1924,7 @@ void LIR_Assembler::membar_loadstore() { __ membar(MacroAssembler::LoadStore); }
19241924
void LIR_Assembler::membar_storeload() { __ membar(MacroAssembler::StoreLoad); }
19251925

19261926
void LIR_Assembler::on_spin_wait() {
1927-
Unimplemented();
1927+
__ pause();
19281928
}
19291929

19301930
void LIR_Assembler::get_thread(LIR_Opr result_reg) {

src/hotspot/cpu/riscv/globals_riscv.hpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
22
* Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.
3-
* Copyright (c) 2020, 2022, Huawei Technologies Co., Ltd. All rights reserved.
3+
* Copyright (c) 2020, 2023, Huawei Technologies Co., Ltd. All rights reserved.
44
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
55
*
66
* This code is free software; you can redistribute it and/or modify it
@@ -108,6 +108,8 @@ define_pd_global(intx, InlineSmallCode, 1000);
108108
product(bool, UseZicbom, false, EXPERIMENTAL, "Use Zicbom instructions") \
109109
product(bool, UseZicbop, false, EXPERIMENTAL, "Use Zicbop instructions") \
110110
product(bool, UseZicboz, false, EXPERIMENTAL, "Use Zicboz instructions") \
111+
product(bool, UseZihintpause, false, EXPERIMENTAL, \
112+
"Use Zihintpause instructions") \
111113
product(bool, UseRVVForBigIntegerShiftIntrinsics, true, \
112114
"Use RVV instructions for left/right shift of BigInteger")
113115

src/hotspot/cpu/riscv/macroAssembler_riscv.hpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,10 @@ class MacroAssembler: public Assembler {
365365
return ((predecessor & 0x3) << 2) | (successor & 0x3);
366366
}
367367

368+
void pause() {
369+
fence(w, 0);
370+
}
371+
368372
// prints msg, dumps registers and stops execution
369373
void stop(const char* msg);
370374

src/hotspot/cpu/riscv/riscv.ad

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//
22
// Copyright (c) 2003, 2023, Oracle and/or its affiliates. All rights reserved.
33
// Copyright (c) 2014, 2020, Red Hat Inc. All rights reserved.
4-
// Copyright (c) 2020, 2022, Huawei Technologies Co., Ltd. All rights reserved.
4+
// Copyright (c) 2020, 2023, Huawei Technologies Co., Ltd. All rights reserved.
55
// DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
66
//
77
// This code is free software; you can redistribute it and/or modify it
@@ -949,6 +949,7 @@ definitions %{
949949
int_def FDIV_COST ( 2000, 20 * DEFAULT_COST); // fdiv
950950
int_def FSQRT_COST ( 2500, 25 * DEFAULT_COST); // fsqrt
951951
int_def VOLATILE_REF_COST ( 1000, 10 * DEFAULT_COST);
952+
int_def CACHE_MISS_COST ( 2000, 20 * DEFAULT_COST); // typicall cache miss penalty
952953
%}
953954

954955

@@ -1815,6 +1816,8 @@ const bool Matcher::match_rule_supported(int opcode) {
18151816
}
18161817

18171818
switch (opcode) {
1819+
case Op_OnSpinWait:
1820+
return VM_Version::supports_on_spin_wait();
18181821
case Op_CacheWB: // fall through
18191822
case Op_CacheWBPreSync: // fall through
18201823
case Op_CacheWBPostSync:
@@ -7872,6 +7875,20 @@ instruct membar_volatile() %{
78727875
ins_pipe(pipe_serial);
78737876
%}
78747877

7878+
instruct spin_wait() %{
7879+
predicate(UseZihintpause);
7880+
match(OnSpinWait);
7881+
ins_cost(CACHE_MISS_COST);
7882+
7883+
format %{ "spin_wait" %}
7884+
7885+
ins_encode %{
7886+
__ pause();
7887+
%}
7888+
7889+
ins_pipe(pipe_serial);
7890+
%}
7891+
78757892
// ============================================================================
78767893
// Cast Instructions (Java-level type cast)
78777894

src/hotspot/cpu/riscv/vm_version_riscv.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
22
* Copyright (c) 1997, 2022, Oracle and/or its affiliates. All rights reserved.
3-
* Copyright (c) 2020, 2022, Huawei Technologies Co., Ltd. All rights reserved.
3+
* Copyright (c) 2020, 2023, Huawei Technologies Co., Ltd. All rights reserved.
44
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
55
*
66
* This code is free software; you can redistribute it and/or modify it
@@ -77,6 +77,9 @@ void VM_Version::initialize() {
7777
if (FLAG_IS_DEFAULT(UseZfhmin)) {
7878
FLAG_SET_DEFAULT(UseZfhmin, true);
7979
}
80+
if (FLAG_IS_DEFAULT(UseZihintpause)) {
81+
FLAG_SET_DEFAULT(UseZihintpause, true);
82+
}
8083
}
8184

8285
if (UseZic64b) {

src/hotspot/cpu/riscv/vm_version_riscv.hpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
* Copyright (c) 1997, 2020, Oracle and/or its affiliates. All rights reserved.
33
* Copyright (c) 2014, 2020, Red Hat Inc. All rights reserved.
4-
* Copyright (c) 2020, 2022, Huawei Technologies Co., Ltd. All rights reserved.
4+
* Copyright (c) 2020, 2023, Huawei Technologies Co., Ltd. All rights reserved.
55
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
66
*
77
* This code is free software; you can redistribute it and/or modify it
@@ -61,6 +61,8 @@ enum VM_MODE {
6161

6262
constexpr static bool supports_stack_watermark_barrier() { return true; }
6363

64+
static bool supports_on_spin_wait() { return UseZihintpause; }
65+
6466
enum Feature_Flag {
6567
#define CPU_FEATURE_FLAGS(decl) \
6668
decl(I, "i", 8) \
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
/*
2+
* Copyright Amazon.com Inc. or its affiliates. All Rights Reserved.
3+
* Copyright (c) 2023, Huawei Technologies Co., Ltd. All rights reserved.
4+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5+
*
6+
* This code is free software; you can redistribute it and/or modify it
7+
* under the terms of the GNU General Public License version 2 only, as
8+
* published by the Free Software Foundation.
9+
*
10+
* This code is distributed in the hope that it will be useful, but WITHOUT
11+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13+
* version 2 for more details (a copy is included in the LICENSE file that
14+
* accompanied this code).
15+
*
16+
* You should have received a copy of the GNU General Public License version
17+
* 2 along with this work; if not, write to the Free Software Foundation,
18+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
19+
*
20+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
21+
* or visit www.oracle.com if you need additional information or have any
22+
* questions.
23+
*/
24+
25+
/**
26+
* @test TestOnSpinWaitRISCV64
27+
* @summary Checks that java.lang.Thread.onSpinWait is intrinsified with instructions
28+
* @library /test/lib
29+
*
30+
* @requires vm.flagless
31+
* @requires os.arch=="riscv64"
32+
*
33+
* @run driver compiler.onSpinWait.TestOnSpinWaitRISCV64 c1
34+
* @run driver compiler.onSpinWait.TestOnSpinWaitRISCV64 c2
35+
*/
36+
37+
package compiler.onSpinWait;
38+
39+
import java.util.ArrayList;
40+
import java.util.Iterator;
41+
import java.util.ListIterator;
42+
import jdk.test.lib.process.OutputAnalyzer;
43+
import jdk.test.lib.process.ProcessTools;
44+
45+
public class TestOnSpinWaitRISCV64 {
46+
public static void main(String[] args) throws Exception {
47+
String compiler = args[0];
48+
ArrayList<String> command = new ArrayList<String>();
49+
command.add("-XX:+IgnoreUnrecognizedVMOptions");
50+
command.add("-showversion");
51+
command.add("-XX:+UnlockDiagnosticVMOptions");
52+
command.add("-XX:+PrintCompilation");
53+
command.add("-XX:+PrintInlining");
54+
command.add("-XX:+UnlockExperimentalVMOptions");
55+
command.add("-XX:+UseZihintpause");
56+
if (compiler.equals("c2")) {
57+
command.add("-XX:-TieredCompilation");
58+
} else if (compiler.equals("c1")) {
59+
command.add("-XX:+TieredCompilation");
60+
command.add("-XX:TieredStopAtLevel=1");
61+
} else {
62+
throw new RuntimeException("Unknown compiler: " + compiler);
63+
}
64+
command.add("-Xbatch");
65+
command.add(Launcher.class.getName());
66+
67+
ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(command);
68+
69+
OutputAnalyzer analyzer = new OutputAnalyzer(pb.start());
70+
71+
analyzer.shouldHaveExitValue(0);
72+
73+
if (compiler.equals("c2")) {
74+
analyzer.shouldContain("java.lang.Thread::onSpinWait (1 bytes) (intrinsic)");
75+
} else {
76+
analyzer.shouldContain("java.lang.Thread::onSpinWait (1 bytes) intrinsic");
77+
}
78+
}
79+
80+
static class Launcher {
81+
public static void main(final String[] args) throws Exception {
82+
int end = 20_000;
83+
84+
for (int i=0; i < end; i++) {
85+
test();
86+
}
87+
}
88+
static void test() {
89+
java.lang.Thread.onSpinWait();
90+
}
91+
}
92+
}

0 commit comments

Comments
 (0)