简体   繁体   English

如何不优化远离 - 愚蠢功能的机制

[英]How not to optimize away - mechanics of a folly function

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

I want to use the above, but I have little understanding of its workings . 我想使用上面的内容,但我对它的工作原理知之甚少 I'm mostly interested in the non VC++ portion and why/how the line 我最感兴趣的是非VC ++部分以及为什么/如何使用该行

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

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) 另外两种方法之间的比较会很有趣(我不知道pragma optimize是如何工作的,但它看起来像一个更干净的解决方案 - 虽然不便携)

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));

means that unverified assembly code supplied by the user is embedded into the assembly generated by GCC. 表示用户提供的未验证汇编代码嵌入到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. 它是空的,所以根本没有任何代码可以发出。

The part after the : informs GCC about the effect of the assembly code. 以下部分:通知GCC汇编代码的效果。 "+r" (datum) means that GCC should assume that the assembly code reads and modifies datum . "+r" (datum)表示GCC应假定汇编代码读取并修改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. 这样做的原因是任何最终在datum存储值的早期计算都不能作为不必要的丢弃。 At the same time, the assembly code itself cannot be discarded as unnecessary, because of the potential modification to datum . 同时,由于对datum的潜在修改,汇编代码本身不能作为不必要的丢弃。 volatile also marks the assembly code as code that must not be optimised away, as documented here : volatile也将汇编代码标记为不能优化的代码, 如下所述

GCC's optimizers sometimes discard asm statements if they determine there is no need for the output variables. 如果GCC的优化器确定不需要输出变量,它们有时会丢弃asm语句。 Also, the optimizers may move code out of loops if they believe that the code will always return the same result (ie none of its input values change between calls). 此外,如果优化器认为代码将始终返回相同的结果(即调用之间其输入值都不变),则优化器可能会将代码移出循环。 Using the volatile qualifier disables these optimizations. 使用volatile限定符会禁用这些优化。 [...] [...]

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. 使用两种不同的方法防止汇编代码被删除似乎有点太多,但我想最好确定一下。

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约束意味着代码并不关心GCC使汇编代码可以使用哪个寄存器,并在此处记录

'r' 'R'
A register operand is allowed provided that it is in a general register. 只要它在通用寄存器中,就允许使用寄存器操作数。

The + modifier means that the code may read from and write to datum , and is documented here : +修饰符意味着代码可以读取和写入datum ,并在此处记录

'+' '+'
Means that this operand is both read and written by the instruction. 表示该操作数由指令读取和写入。 [...] [...]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM