防止GCC在基准代码中移动代码 [英] Prevent code being moved by GCC in benchmark code

查看:106
本文介绍了防止GCC在基准代码中移动代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图微调我们正在使用的一些基准测试代码,并且想知道是否有办法与GCC明确沟通如何订购某些代码。例如,给定这些代码块:


  1. Pre

  2. 开始计时器

  3. 正文

  4. 停止计时器

  5. 发布

我想告诉GCC,每个块必须按照上面的顺序保存,而不会有任何指令泄漏到另一个块中。理想情况下,计时器只能测量第3步,但出于实际原因,至少要测量步骤3和至多<步骤2-4>就足够了。我只是想确保我记录了测量步骤1或5的任何部分。



目前我使用 __ sync_synchronize 功能可以发出完整的内存围栏。我的希望是,除了作为围栏之外,这个函数被标记以防止重新排序。



这个调用是否为 __ sync_synchronize 足够吗?同样在逻辑上,C ++ 11的fence命令也可以根据标准的文本来满足吗?

解决方案

c $ c> Start-Timer 是一个函数调用, Stop-Timer 是另一个函数调用,优化器几乎没有机会将<从 Pre 发布到< code> Body



所有 Pre 的副作用在 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:

  1. Pre
  2. Start-Timer
  3. Body
  4. Stop-Timer
  5. 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屋!

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