防止GCC在基准代码中移动代码 [英] Prevent code being moved by GCC in benchmark code
问题描述
- Pre
- 开始计时器
- 正文
- 停止计时器
- 发布
我想告诉GCC,每个块必须按照上面的顺序保存,而不会有任何指令泄漏到另一个块中。理想情况下,计时器只能测量第3步,但出于实际原因,至少要测量步骤3和至多<步骤2-4>就足够了。我只是想确保我记录了测量步骤1或5的任何部分。
目前我使用 __ sync_synchronize $ c $定时器中的c>功能可以发出完整的内存围栏。我的希望是,除了作为围栏之外,这个函数被标记以防止重新排序。
这个调用是否为 __ sync_synchronize
足够吗?同样在逻辑上,C ++ 11的fence命令也可以根据标准的文本来满足吗?
c $ c> Start-Timer 是一个函数调用, Stop-Timer
是另一个函数调用,优化器几乎没有机会将<从 Pre
或发布
到< code> Body
。
所有 Start-Timer
函数被调用之前完成(这里有一个序列点)。在执行 Post
Stop-Timer
的所有副作用必须完成(还有一个序列点)。因此,编译器必须使用 code> Start-Timer
和 Stop-Timer
可见的代码来生成代码,周围溢出材料,我不相信它即使这样做也可以。
I'm trying to fine tune some benchmark code we are using and am wondering if there is a way to communicate to GCC explicitly how to order certain bits of code. For example, given these blocks of code:
- Pre
- Start-Timer
- Body
- Stop-Timer
- Post
I wish to tell GCC that each block must be kept in the above order without any instruction leakage into the other block. Ideally the timer would measure only Step 3, however, for practical reasons measuring at least Step 3 and at most Steps 2-4 will suffice. I just want to make sure I'm note measuring any part of Step 1 or 5.
Currently I use a __sync_synchronize
in the Timer functions to issue a full memory fence. My hope is that, in addition to being a fence, that this function is marked to prevent reordering.
Is this call to __sync_synchronize
sufficient? Also logically, would the C++11 fence commands also suffice according to the text of the standard?
If the Start-Timer
is a function call and the Stop-Timer
is another function call, the optimizer has little opportunity to move the Body
around, or spill material from Pre
or Post
into Body
.
All the side-effects from Pre
must be complete before the Start-Timer
function is called (there's a sequence point there). All the side effects of Stop-Timer
must be complete before executing Post
(there's a sequence point there, too). So the compiler would have to have the code for Start-Timer
and Stop-Timer
visible to monkey with the generated code, spilling material around, and I'm not convinced it could do so even then.
So, in summary, I don't think you have to worry about it if you use function calls to start and stop the timer.
这篇关于防止GCC在基准代码中移动代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!