如何不优化掉 - 一个愚蠢的功能机制 [英] How not to optimize away - mechanics of a folly function

查看:216
本文介绍了如何不优化掉 - 一个愚蠢的功能机制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种编程技术,以确保用于基准变量(没有观察到副作用)将不会被编译器优化掉

I was searching for a programming technique that would ensure variables used for benchmarking (without observable side effects) won't be optimized away by the compiler

给出了一些信息,但我最终使用的愚蠢和下面的函数

This gives some info, but I ended up using folly and the following function

/**
 * Call doNotOptimizeAway(var) against variables that you use for
 * benchmarking but otherwise are useless. The compiler tends to do a
 * good job at eliminating unused variables, and this function fools
 * it into thinking var is in fact needed.
 */
#ifdef _MSC_VER

#pragma optimize("", off)

template <class T>
void doNotOptimizeAway(T&& datum) {
  datum = datum;
}

#pragma optimize("", on)

#else
template <class T>
void doNotOptimizeAway(T&& datum) {
  asm volatile("" : "+r" (datum));
}
#endif

我想用上面的,但我有它的运作的知之甚少。我在非VC ++部分最感兴趣和为什么/怎么行

asm volatile("" : "+r" (datum));

创建了一个非优化的环境或为什么这个东西人会选择实现这样的事情。另外,2种方法之间的比较会很有趣(我不知道如何编译优化的作品,但它看起来像一个清洁的解决方案 - 非便携式虽然)

creates a non optimizable context or why is this something one would choose to implement such a thing. Also a comparison between the 2 methods would be interesting (I don't know how pragma optimize works but it looks like a cleaner solution - non portable though)

推荐答案

有是禁用的优化,所以如果你需要禁用的优化,你的局限性无论你实现恰好提供的标准方法。它没有意义的两种做法,除非你找到支持他们两个编译器进行比较。

There is no standard way to disable optimisations, so if you need to disable optimisations, you're limited to whatever your implementation happens to provide. It doesn't make sense to compare the two approaches unless you find a compiler that supports them both.

总之,在海湾合作委员会,

Anyway, in GCC,

asm volatile("" : "+r" (datum));

意味着由用户提供的未经证实的组装code嵌入到由GCC生成的程序集。第一个字符串()包含装配code注入。它是空的,所以实际上并没有那么被排放在所有的code。

means that unverified assembly code supplied by the user is embedded into the assembly generated by GCC. The first string literal ("") contains the assembly code to inject. It's empty, so there isn't actually any code that gets emitted at all.

后的部分通知海合会关于大会code的作用。 + R(数据)意味着,海合会应承担大会code读取和修改基准。即使它没有。这其中的原因是,该结束了存储基准值的任何先前计算不能丢弃不必要的。与此同时,大会code本身不能被丢弃不必要的,因为潜在的修改基准的。 挥发性也标志着大会code作为code,它不能被优化掉,的as这里记录的:

The part after the : informs GCC about the effect of the assembly code. "+r" (datum) means that GCC should assume that the assembly code reads and modifies datum. Even though it doesn't. The reason for that is that any earlier calculations that end up storing a value in datum cannot be discarded as unnecessary. At the same time, the assembly code itself cannot be discarded as unnecessary, because of the potential modification to datum. volatile also marks the assembly code as code that must not be optimised away, as documented here:

GCC的优化有时丢弃 ASM 语句,如果他们决定有没有必要为输出变量。此外,优化可能会移动code移出循环,如果他们认为,code总是会返回相同的结果(即没有其输入值的通话之间切换)。使用挥发性预选赛禁用这些优化。 [...]

GCC's optimizers sometimes discard asm statements if they determine there is no need for the output variables. Also, the optimizers may move code out of loops if they believe that the code will always return the same result (i.e. none of its input values change between calls). Using the volatile qualifier disables these optimizations. [...]

这似乎有点太多使用prevent两种不同的方法组装code被删除,真的,但我想这是最好的肯定。

It seems a bit much to use two different approaches to prevent the assembly code from being removed, really, but I guess it's best to be sure.

研究约束意味着code并不关心所有的东西哪一个寄存器GCC提供的用于组装code使用,<一个HREF =htt​​ps://gcc.gnu.org/onlinedocs/gcc/Simple-Constraints.html#Simple-Constraints>记录在这里的:

The r constraint means that the code does not care all that much which register GCC makes available for the assembly code to use, and is documented here:

R结果
  寄存器操作数,允许只要它是通用寄存器。

‘r’
    A register operand is allowed provided that it is in a general register.

+ 修改意味着code可读取和写入基准和<一个HREF =htt​​ps://gcc.gnu.org/onlinedocs/gcc/Modifiers.html#Modifiers>记录在这里的:

The + modifier means that the code may read from and write to datum, and is documented here:

+结果
  表明这个操作数是读取和由指令写入。 [...]

‘+’
    Means that this operand is both read and written by the instruction. [...]

这篇关于如何不优化掉 - 一个愚蠢的功能机制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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