Repository navigation
V3EmitC: Use a reference instead of a pointer for VlSelf - #5254
Conversation
1797ae6 to
c0c52ad
Compare
wsnyder
left a comment
There was a problem hiding this comment.
Great idea, thanks for doing this! Using pointers dates back to when there was only C ;)
Please cleanup the tests etc and I'll review again when you mark non-draft.
|
I think I may need some help to create a dummy function that still preserves the original function using |
|
I'm not sure your exact question, but picking one of the failures: This is trying to reference a AstNodeClass, so it still needs |
Yes. For this, I have an idea to check if the string will be |
5a478ec to
775d168
Compare
|
Now, I found a much easier way to do so. Use |
|
Can you provide a web link to that ref concept? |
If you want the Using |
b1dda25 to
92db227
Compare
Currently, we use pointers for VlSelf and dereference it every time we process the RTL. However, when the C++ compiler sees a pointer instead of a reference, it will lose a dereferenceable attribute, so the compiler can not speculatively load the value of the pointer and must generate more branches to prevent the unnecessary deference of the pointer. This will cause more branches to be generated in the final binary, which increases the burden on CPU branch prediction. Even worse, the branch footprint in Verilator generated code on large RTL designs such as XiangShan already suppresses the size of the branch target buffer of CPUs we use now, which will cause more branch misprediction and performance degradation. Thus, we should use a reference instead of a pointer for VlSelf to tell the C++ compiler that the value of VlSelf is always dereferenceable, and the compiler can speculatively load the value of VlSelf without generating more branches. A simple evaluation performed on XiangShan commit 136f64975e with LLVM16 packaged by Debian (version 16.0.6 (27+b1)) based on Verilator commit 6882f8c with 1 thread EMU running 2 iter coremark workload on OpenXiangShan/ready-to-run commit 24ab7d76e5 on Intel 13900K shows sampled branch instructions reduced from 132746731584 to 121130263138 (-8.75%), the running time reduced from 838.95s to 753.43s (-10.19%). This increases l1-dcache-loads from 414383597179 to 426881071898 (+3.02%) and slightly increases l1-dcache-load-misses from 33706712758 to 33967571525 (+0.77%), but the LLC load-misses are reduced from 203290290 to 139019005 (-31.62%), maybe because the branch misprediction of wrong path is reduced. I also tested this on 8 threads EMU with Clang PGO as our routine to use Verilated XiangShan on 7950X3D + LLVM 14 and 13900K + LLVM 16. All show a slight improvement in performance even though we reduced the branches with PGO on and slightly increased the load instructions. And I haven't found any performance degradation on Verilated XiangShan RTL so far. This change also allows more compiler optimization to be performed in verilator-generated code. I am currently working on it.
92db227 to
35d7d24
Compare
|
Now, I think this PR is ready to merge. I also want to share some raw benchmark results: https://gist.github.com/cyyself/eb7b0fbba87c76eb80a897f8ccfa19bc |
Since verilator#5254 generated functions read model state through vlSelfRef, obtained with `auto& vlSelfRef = std::ref(*vlSelf).get();`. That only works because reference_wrapper::get() returns T&, so its call result carries LLVM's `dereferenceable` attribute, and only the SimplifyCFG run before the inliner can use it. LLVM 23.1 (llvm/llvm-project#204795) trusts `dereferenceable` only at the point where it is stated unless the object provably cannot be freed before the use; for a call result that proof needs an uninterrupted walk back to the call, which the long generated functions cannot provide, so with LLVM 23.1 and later the branches removed by verilator#5254 come back. When the model object is the function argument instead, LLVM only needs the function to be `nofree`, which it infers for the generated functions, and the fact then holds for the whole function. Loose functions therefore now take `Model& vlSelfRef` instead of `Model* vlSelf`, with a `vlSelf` pointer derived from it for code that needs one. Parts of this change were prepared with the help of a generative AI tool (Claude Fable 5.1). Signed-off-by: Yangyu Chen <cyy@cyyself.name>
Currently, we use pointers for VlSelf and dereference it every time we process the RTL. However, when the C++ compiler sees a pointer instead of a reference, it will lose a dereferenceable attribute, so the compiler can not speculatively load the value of the pointer and must generate more branches to prevent the unnecessary deference of the pointer. This will cause more branches to be generated in the final binary, which increases the burden on CPU branch prediction. Even worse, the branch footprint in Verilator generated code on large RTL designs such as XiangShan already suppresses the size of the branch target buffer of CPUs we use now, which will cause more branch misprediction and performance degradation.
Thus, we should use a reference instead of a pointer for VlSelf to tell the C++ compiler that the value of VlSelf is always dereferenceable, and the compiler can speculatively load the value of VlSelf without generating more branches.
A simple evaluation performed on XiangShan commit 136f64975e with LLVM16 packaged by Debian (version 16.0.6 (27+b1)) based on Verilator commit 6882f8c with 1 thread EMU running 2 iter coremark workload on OpenXiangShan/ready-to-run commit 24ab7d76e5 on Intel 13900K shows sampled branch instructions reduced from 132746731584 to 121130263138 (-8.75%), the running time reduced from 838.95s to 753.43s (-10.19%).
This increases l1-dcache-loads from 414383597179 to 426881071898 (+3.02%) and slightly increases l1-dcache-load-misses from 33706712758 to 33967571525 (+0.77%), but the LLC load-misses are reduced from 203290290 to 139019005 (-31.62%), maybe because the branch misprediction of wrong path is reduced. I also tested this on 8 threads EMU with Clang PGO as our routine to use Verilated XiangShan on 7950X3D + LLVM 14 and 13900K + LLVM 16. All show a slight improvement in performance even though we reduced the branches with PGO on and slightly increased the load instructions. And I haven't found any performance degradation on Verilated XiangShan RTL so far.
This change also allows more compiler optimization to be performed in verilator-generated code. I am currently working on it.