Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Locality for prefetch built-in proposal #46

Merged
merged 4 commits into from
Dec 5, 2023

Conversation

BeMg
Copy link
Contributor

@BeMg BeMg commented Jul 13, 2023

ARM[1] provide the its own semantic on prefetch built-in locality.

It seem we could do the same thing in RISC-V.

[1] https://developer.arm.com/documentation/101458/2010/Coding-best-practice/Prefetching-with---builtin-prefetch

@kito-cheng
Copy link
Collaborator

@asb
Copy link

asb commented Jul 13, 2023

Thanks for documenting this. I think it would be good to explicitly mention the zicbop extension here.

This also exposes an issue with our treatment of the 'zihintntl' extension on the LLVM side at least (I'm not sure if the same issue is there in GCC). The proposed prefetch locality patch for LLVM only emits the ntl hints if the zihintntl extension is known to be present. We should probably emit the add x0, x0, x? based mnemonics directly even if zihintntl isn't known to be present.

@kito-cheng
Copy link
Collaborator

kito-cheng commented Jul 14, 2023

This also exposes an issue with our treatment of the 'zihintntl' extension on the LLVM side at least (I'm not sure if the same issue is there in GCC). The proposed prefetch locality patch for LLVM only emits the ntl hints if the zihintntl extension is known to be present. We should probably emit the add x0, x0, x? based mnemonics directly even if zihintntl isn't known to be present.

GCC implementation also require zihintntl too, that also remind another issue I discussed with @topperc here is: should we allow those hint mnemonics still available even corresponding is not enabled? And actually prefetch is also a hint instruction too, so we might did the same treatment for Zicbop too.

I guess this worth to put to the next meeting agenda?

riscv-c-api.md Outdated
| `__builtin_prefetch(ptr, 0, 2 /* locality */);` | `ntl.p1 + prefetch.r (ptr)` |
| `__builtin_prefetch(ptr, 0, 3 /* locality */);` | `prefetch.r (ptr)` |

Compiler only emits the ntlh hints if the Zihintntl extension is enabled.
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this might be over-specifying what compilers should do (plus it doesn't mention Zicbop).

How about "The following table presents the mapping from the __builtin_prefetch function to the corresponding assembly instructions assuming the presence of the Zihintntl and Zicbop extensions."?

Based on the last discussion we're probably erring in favour of being conservative and not emitting those hints eagerly (due to concerns about the opcodes being used for something else), but I think such a description wouldn't restrict compilers from emitting those hints even if the support for zicbop/zihintntl is unknown.

riscv-c-api.md Outdated
@@ -235,6 +235,25 @@ long __riscv_clmul (long a, long b); // clmul rd, rs1, rs2
vint8m1_t __riscv_vadd_vv_i8m1(vint8m1_t vs2, vint8m1_t vs1, size_t vl); // vadd.vv vd, vs2, vs1
```

### Prefetch Intrinsics

The Zicbop extension provide the prefetch instruction to allow users to optimize data access patterns by providing hints to the hardware regarding future data accesses. It is supported through a compiler-defined built-in function with three arguments that specify its behavior.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit s/provide/provides

riscv-c-api.md Outdated
void __builtin_prefetch(const void *addr, int rw, int locality)
```

The locality for the built-in `__builtin_prefetch` function in RISC-V can be achieved using the Non-Temporal Locality Hints (Zihintntl) extension. According to Non-Temporal Locality Hints extension, it indicates that a cache line should be prefetched into a cache that is outer from the level specified by the NTL when a NTL instruction is applied to prefetch instruction.
Copy link

@hiraditya hiraditya Aug 1, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit:

According to Non-Temporal Locality Hints extension, it indicates that a cache line should be prefetched into a cache that is outer from the level specified by the NTL when a NTL instruction is applied to prefetch instruction.

When a Non-Temporal Locality (NTL) Hints instruction is applied to prefetch instruction, a cache line should be prefetched into a cache level that is higher than the level specified by the NTL.

@hiraditya
Copy link

Thanks for addressing the changes i requested.

@BeMg BeMg requested review from hiraditya and asb November 7, 2023 02:00
Copy link
Collaborator

@kito-cheng kito-cheng left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, @asb do you mind take a look again? :)

@BeMg
Copy link
Contributor Author

BeMg commented Dec 5, 2023

Resolve merge conflict.

@kito-cheng
Copy link
Collaborator

Although @asb is not response yet, but he has aware this PR and patch for LLVM[1], also it merged into LLVM trunk, so I treat it as no objection from LLVM community and moving this PR forward.

[1] https://reviews.llvm.org/D154691

@kito-cheng kito-cheng merged commit 0b9f9b8 into riscv-non-isa:master Dec 5, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants