如何预取不常用的代码? [英] How can I prefetch infrequently used code?

查看:75
本文介绍了如何预取不常用的代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将一些代码预取到指令缓存中.代码路径很少使用,但对于极少数情况,我需要将其放在指令高速缓存中或至少在L2中.对于这些罕见情况,我有一些提前通知._mm_prefetch是否适用于代码?有没有办法在缓存中获取这种不常用的代码?对于这个问题,我不在乎可移植性,所以即使asm也可以.

I want to prefetch some code into the instruction cache. The code path is used infrequently but I need it to be in the instruction cache or at least in L2 for the rare cases that it is used. I have some advance notice of these rare cases. Does _mm_prefetch work for code? Is there a way to get this infrequently used code in cache? For this problem I don't care about portability so even asm would do.

推荐答案

答案取决于您的CPU体系结构.

The answer depends on your CPU architecture.

也就是说,如果您使用的是gcc或clang,则可以使用 __ builtin_prefetch 指令尝试生成预取指令.在Pentium 3和更高版本的x86类型的体系结构上,这将生成 PREFETCHh 指令,该指令请求将数据加载到数据高速缓存层次结构中.由于这些架构具有统一的L2和更高的缓存,因此可能会有所帮助.

That said, if you are using gcc or clang, you can use the __builtin_prefetch instruction to try to generate a prefetch instruction. On Pentium 3 and later x86-type architectures, this will generate a PREFETCHh instruction, which requests a load into the data cache hierarchy. Since these architectures have unified L2 and higher caches, it may help.

函数如下:

__builtin_prefetch(const void *address, int locality);

locality 参数应在0 ... 3范围内.假设 locality 直接映射到 PREFETCHh 指令的 h 部分,您要传递1或2,这要求加载数据进入L2和更高级别的缓存.请参阅 英特尔®64和IA-32架构软件开发人员手册第2B卷:指令集参考,M-Z (PDF)第4-277页.(在此处查找其他卷.)

The locality argument should be in the range 0...3. Assuming locality maps directly to the h part of the PREFETCHh instruction, you want to pass 1 or 2, which ask for the data to be loaded into the L2 and higher caches. See Intel® 64 and IA-32 Architectures Software Developer's Manual Volume 2B: Instruction Set Reference, M-Z (PDF) page 4-277. (Find other volumes here.)

如果您使用的其他编译器没有 __ builtin_prefetch ,请查看其是否具有 _mm_prefetch 函数.您可能需要包含头文件才能获得该功能.例如,在OS X上,该函数和 locality 参数的常量在 xmmintrin.h 中声明.

If you're using another compiler that doesn't have __builtin_prefetch, see whether it has the _mm_prefetch function. You may need to include a header file to get that function. For example, on OS X, that function, and constants for the locality argument, are declared in xmmintrin.h.

这篇关于如何预取不常用的代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆