Re: Discuss the RVC implementation
Xiaolin Zheng
yunyao.zxl at alibaba-inc.com
Tue Sep 20 10:44:21 UTC 2022
Hi Felix,
TL;DR of code size evaluations, stably reproduced:
If a piece of code is 100 bytes full of 4-byte instructions:
1. In the current master branch with RVC, it may shrink to 95 bytes. (compression rate is %5)
2. With the new implementation at [1], it may shrink to 84 bytes. (compression rate is 16%; ~11% more than master)
3. With the special patch at [2] (a special optimization of compressing two "slli"s in the movptr), it may shrink to 79 bytes. (compression rate is 21%; ~%5 addition to the previous one, because movptr() is used in a quite big quantity. But this patch might need further beautification for the hard-coded enumeration and will cause complexity for reviewing, so we'd postpone that temporarily)
These are evaluated by a hand-written toy histogram[3], excluding the scratch_emit, and tested with release build (for fastdebug build, the compression rate is far more than release mode; but we may not care about that), only for evaluation purposes.
About the performance, I need more time to make some more evaluations. Due to the patch of the new implementation should wait for the loom port merging first, we have plenty of time then. I am going to make a long run of specjbb2015 to measure it on average. Will update the result in the same thread.
---------------
Precisely, here are some detailed data about the code size.
This histogram mentioned above presents all the instructions emitted in a JVM process, shown when exiting. For example, the picture in [4].
The second row (RVC instructions) + The third row (4-byte normal instructions) = The fourth row (total instructions); sorted by the fourth row.
If RVC is not enabled, the second row is always 0 and the third row is always equal to the fourth row.
Tested with the new RVC [1] branch with springboot / springboot-petclinic / SPECjvm2008 / SPECjbb2015(when exiting), the results are all a stable ~84%. The SPECjvm2008 results are at [5]. Please search the keywords "Ideally Code Size Could Shrink to" in the files in the browser for more details.
P.S.: the result with the special patch [2] is about ~79% at [6] for future references, but might be reserved for now.
Best,
Xiaolin
[1] https://github.com/zhengxiaolinX/jdk/commits/REBASE-rvc-beautify
[2] https://github.com/zhengxiaolinX/jdk/commit/3a4d80197da0c497c844016b9a9fbae541eca9c8
[3] https://github.com/zhengxiaolinX/jdk/commit/5312cbd8ac860f47b109ab2a99750041865c018d
[4] https://github.com/openjdk/riscv-port/pull/34
[5] http://cr.openjdk.java.net/~xlinzheng/rvc-size/size/
[6] http://cr.openjdk.java.net/~xlinzheng/rvc-size/size-full/
------------------------------------------------------------------
From:yangfei <yangfei at iscas.ac.cn>
Send Time:2022年9月17日(星期六) 21:12
To:郑孝林(云矅) <yunyao.zxl at alibaba-inc.com>
Cc:riscv-port-dev <riscv-port-dev at openjdk.org>
Subject:Re: Discuss the RVC implementation
Hi Xiaolin,
Your new proposal for supporting the RVC extension looks interesting.
May I ask if you have any performance data including code size measured?
Also it's appreciated if you have more details about the issue with MachBranch nodes.
Thanks,
Fei
-----Original Messages-----
From:"Xiaolin Zheng" <yunyao.zxl at alibaba-inc.com>
Sent Time:2022-09-15 10:52:59 (Thursday)
To: riscv-port-dev <riscv-port-dev at openjdk.org>
Cc:
Subject: Discuss the RVC implementation
Hi team,
I am going to describe a different implementation of RVC for our backend.
## Background
The RISC-V C extension, also known as RVC, could transform 4-byte instructions to 2-byte counterparts when eligible (for example, as the manual, Rd/Rs of instruction ranges from [x8,x15] might be one common requirement, etc.).
## The current implementation in the Hotspot
The current implementation[0] is a transient one, introducing a "CompressibleRegion" by using RTTI[1] to indicate that instructions inside these regions can be safely substituted by the RVC counterparts, if convertible; and the implementation also uses a, say, "whitelist mode" by using the "CompressibleRegion" mentioned above to "manually mark out safe regions", then batch emit them if could. However, after a deeper look, we might discover the current "whitelist mode" has several shortages:
## Shortages of the current implementation
1. Coverages:
The current implementation only covers some of C2 match rules, and only some small part of stub code, so there is obviously far more space to reduce the total code size. In my observations, some RISC-V instruction sequences generally occupy a bit more space than AArch64 ones[2]. With the new implementations, we could achieve a code size level alike AArch64's generated code. Some better, some still worse than AArch64 one in my simple observation.
2. Though safe, I'd say it's very much not easy to maintain. The background is, most of the patchable instructions cannot be easily transformed into their shorter counterparts[3], and they need to be prevented from being compressed. So comes the question: we must make sure no patchable relocation is inside the range of a "CompressibleRegion". For example, the string comparison intrinsic function[4] looks very delicious: transforming it and its siblings may result in a yummy compression rate. But programmers might have to check lots of its callees to find if there is just one patchable relocation hidden inside that causes the whole intrinsic incompressible. This could cause extra burden for programmers, so I bet no one would like to add "CompressibleRegion" for his/her code :-)
3. Performance:
Better performance of generated code is a little side effect this extension gives us, the smaller the I$ size, the better performance though - please see Andrew Waterman's paper[5] for more reference there. Anyway, it looks like a higher general compression rate is better for performance.
The main issue here is the granularity of "CompressibleRegion" is a bit coarse. "Why not exclude the incompressible parts" may come up to us naturally. And after some diggings, we may find: we just need to exclude countable places that would be patched back (mostly relocations), and several code slices with a fixed length, which will be calculated, such as "emit_static_call_stub". All remaining instructions could be safely transformed into RVC counterparts if eligible. So maybe, say, the "blacklist mode"?
## The new implementation
To implement the "blacklist mode" in the backend, we need two things:
1. an "IncompressibleRegion", indicating instructions inside it should remain in their normal 4-byte form no matter what happens.
2. a simple strategy to exclude patchable instructions, mainly for relocations. So we can see the new strategy is highly bounded to relocations' positions:
We all know the "relocate()" in Hotspot VM is a mark that only has an explicit "start point" without an end point, and some of them could be patched back. Therefore, we can use a simple strategy: introduce a lambda as another argument to assign "end point" semantics to the relocations, for completing our requirements without extra costs. For example:
Originally:
```
__ relocate(safepoint_pc.rspec());
__ la(t0, safepoint_pc.target());
__ sd(t0, Address(xthread, JavaThread::saved_exception_pc_offset()));
```
After introducing a simple lambda as an extra argument:
```
__ relocate(safepoint_pc.rspec(), [&] { // The relocate() hides an "IncompressibleRegion" in it
__ la(t0, safepoint_pc.target()); // This patchable instruction sequence is incompressible
});
_ sd(t0, Address(xthread, JavaThread::saved_exception_pc_offset()));
```
Well, simple but effective. Excluding such countable dynamically patchable places and unifying all relocations, all other instructions can be safely transformed, without messing up the current code style. Programmers could just keep aligning the same style; most of the time they have no need to care about whether the RVC exists or not and things get converted automatically. The proposed new sample code is again, here[6].
## Other things worth being noticed
1. Instruction patching issues
With the C extension, the backend mixes with both 2-byte and 4-byte instructions. It gets a little CISC alike. We know the Hotspot would patch instructions when code is running at full speed, such as call instructions, nops used for deoptimizations (the nops at the entry points, and post-call nops after loom). Instruction patching is delicate so we must carefully handle such places, to keep these 4-byte instructions from spanning cachelines. Though remaining a 4-byte normal form even with RVC, they might sit at a 2-byte aligned boundary. Such cases should definitely not happen, for patching such places spanning cachelines would lose the atomicity. So shortly, we must properly align them, such as [7][8]. Such a problem could exist with RVC, no matter "whitelist mode" or "blacklist mode". It is a general problem for instruction patching. I will add more strong assertions to the potential places (trampoline_call might be a very good spot, for patchable "static_call", "opt_virtual" and "virtual" relocations) to check alignment in the future patches.
2. MachBranch Nodes
And MachBranch nodes: they are not easy to be tamed because the "fake label"[9] in PhaseOutput::scratch_emit_size() cannot tell us the real distance of the label. But we can leave them alone in this discussion, for there will be patches to handle those afterward.
That's nearly all. Thanks for reaching here despite the verbosity. It would be very nice to receive any suggestions.
Best,
Xiaolin
[0] Original patch: https://github.com/openjdk/riscv-port/pull/34
[1] Of course, the "CompressibleRegion" is good, I like it; and this idea is not from myself.
[2] For a simple example, a much commonly used fixed-length movptr() uses up six 4-byte instructions (lui+addi+slli+addi+slli+addi, MIPS alike instructions using arithmetical calculations with signed extensions, but not anyone's fault :-) ), while the AArch64 counterpart only takes three 4-byte instructions (movz+movk+movk). They are both going to mov a 48-bit immediate. After accumulation, the size differs quite a lot.
[3] 2-byte instructions have fewer bits, so comes shorter immediate encoding etc. compared to the 4-byte counterparts. After we transform patchable instructions (ones at marks of patchable relocations, etc.) to 2-byte ones, when they are patched to a larger value or farther distances afterward, it is possible that they sadly find themselves, the shorter instructions, cannot cover the newly patched value. So we need to exclude patchable instructions (at the relocation marks etc.) from being compressed.
[4] https://github.com/openjdk/jdk/blob/7f3250d71c4866a64eb73f52140c669fe90f122f/src/hotspot/cpu/riscv/riscv.ad#L10032-L10035
[5] https://digitalassets.lib.berkeley.edu/etd/ucb/text/Waterman_berkeley_0028E_15908.pdf, Page 64: "5.4 The RVC Extension, Performance Implications"
[6] https://github.com/zhengxiaolinX/jdk/tree/REBASE-rvc-beautify
[7] https://github.com/openjdk/jdk/blob/7f3250d71c4866a64eb73f52140c669fe90f122f/src/hotspot/cpu/riscv/riscv.ad#L9873
[8] https://github.com/openjdk/jdk/blob/7f3250d71c4866a64eb73f52140c669fe90f122f/src/hotspot/cpu/riscv/c1_LIRAssembler_riscv.cpp#L1348-L1353
[9] https://github.com/openjdk/jdk/blob/211fab8d361822bbd1a34a88626853bf4a029af5/src/hotspot/share/opto/output.cpp#L3331-L3340
More information about the riscv-port-dev
mailing list