[英]How to stop VC++ compiler from reordering code?
I have a code like that:我有这样的代码:
const uint64_t tsc = __rdtsc();
const __m128 res = computeSomethingExpensive();
const uint64_t elapsed = __rdtsc() - tsc;
printf( "%" PRIu64 " cycles", elapsed );
In release builds, this prints garbage like “38 cycles” because VC++ compiler reordered my code:在发布版本中,这会打印“38 个周期”之类的垃圾,因为 VC++ 编译器重新排序了我的代码:
const uint64_t tsc = __rdtsc();
00007FFF3D398D00 rdtsc
00007FFF3D398D02 shl rdx,20h
00007FFF3D398D06 or rax,rdx
00007FFF3D398D09 mov r9,rax
const uint64_t elapsed = __rdtsc() - tsc;
00007FFF3D398D0C rdtsc
00007FFF3D398D0E shl rdx,20h
00007FFF3D398D12 or rax,rdx
00007FFF3D398D15 mov rbx,rax
00007FFF3D398D18 sub rbx,r9
const __m128 res = …
00007FFF3D398D1B lea rdx,[rcx+98h]
00007FFF3D398D22 mov rcx,r10
00007FFF3D398D25 call computeSomethingExpensive (07FFF3D393E50h)
What's the best way to fix?修复的最佳方法是什么?
PS I'm aware rdtsc
doesn't count cycles, it measures time based on CPU's base frequency. PS 我知道
rdtsc
不计算周期,它根据 CPU 的基本频率测量时间。 I'm OK with that, I still want to measure that number.我同意,我仍然想测量这个数字。
Update: godbolt link更新:神螺栓链接
Adding a fake store添加假商店
static bool save = false;
if (save)
{
static float res1[4];
_mm_store_ps(res1, res);
}
before the second __rdtsc
seem to be enough to fool the compiler.在第二个
__rdtsc
之前似乎足以愚弄编译器。
(Not adding a real store to avoid contention if this function is called in multiple threads, though could use TLS to avoid that) (如果在多个线程中调用此 function ,则不添加真实存储以避免争用,但可以使用 TLS 来避免这种情况)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.