如何让 GCC 在使用内部函数时使用两个以上的 SIMD 寄存器?
我正在编写一些代码并尝试使用 SIMD 内在函数 SSE2/3 来加速它。 我的代码性质如此,我需要将一些数据加载到 XMM 寄存器中并对其进行多次操作。 当我查看生成的汇编代码时,GCC 似乎不断将数据刷新回内存,以便重新加载 XMM0 和 XMM1 中的其他内容。 我正在针对 x86-64 进行编译,因此我有 15 个寄存器。 为什么 GCC 只使用两个,我该怎么做才能要求它使用更多? 有什么方法可以将某些值“固定”在寄存器中吗? 我将“register”关键字添加到变量定义中,但生成的汇编代码是相同的。
I am writing some code and trying to speed it up using SIMD intrinsics SSE2/3. My code is of such nature that I need to load some data into an XMM register and act on it many times. When I'm looking at the assembler code generated, it seems that GCC keeps flushing the data back to the memory, in order to reload something else in XMM0 and XMM1. I am compiling for x86-64 so I have 15 registers. Why is GCC using only two and what can I do to ask it to use more? Is there any way that I can "pin" some value in a register? I added the "register" keyword to my variable definition, but the generated assembly code is identical.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
听起来您在编译时禁用了优化,因此 C 语句之间的寄存器中没有保存任何变量,甚至
int
也不保存。使用
gcc -O3 -march=native
进行编译,让编译器生成不那么糟糕的 asm,并针对您的机器进行了优化。 默认值为-O0
,具有“通用”目标 ISA 和调整。另请参阅 clang 为什么会产生使用 -O0 的 asm 效率低下(对于这个简单的浮点和)? 了解更多关于为什么“调试”构建通常是这样的,以及
register int foo;
或register __m128 bar;
即使在调试版本中也可以保留在寄存器中。 但是,如果您希望代码整体运行得更快,那么实际上让编译器优化以及使用寄存器要好得多!It sounds like you compiled with optimization disabled, so no variables are kept in registers between C statements, not even
int
.Compile with
gcc -O3 -march=native
to let the compiler make non-terrible asm, optimized for your machine. The default is-O0
with a "generic" target ISA and tuning.See also Why does clang produce inefficient asm with -O0 (for this simple floating point sum)? for more about why "debug" builds in general are like that, and the fact that
register int foo;
orregister __m128 bar;
can stay in a register even in a debug build. But it's much better to actually have the compiler optimize, as well as using registers, if you want your code to run fast overall!如果您要为每个内在函数指定单独的寄存器,那么您不妨只编写程序集目录,特别是考虑到 gcc 在许多情况下不必要地悲观内在函数的讨厌习惯。
If you're getting to the point where you're specifying individual registers for each intrinsic, you might as well just write the assembly directory, especially given gcc's nasty habit of pessimizing intrinsics unnecessarily in many cases.
是的你可以。 Explicit Reg Vars 讨论了固定 a 所需的语法变量到特定的寄存器。
Yes, you can. Explicit Reg Vars talks about the syntax you need to pin a variable to a specific register.