如果我使用启用了内存清理程序的 clang 编译和运行它,这是一段代码,它会生成内部编译器错误。
它主要只是将一些数据放入 SSE 寄存器并调用一个函数将半浮点数转换为浮点数:
int main(int argc, char** argv) {
// Just some memory to load from.
alignas(64) std::array<uint16_t, 16> array;
array.fill(0);
__m128i ints = _mm_set1_epi64x(*reinterpret_cast<const uint64_t*>(array.data()));
// msan is happy with this version, and both versions work if we compile with anything other than -Og or -O1
// __m128i ints = _mm_set_epi64x(*reinterpret_cast<const uint64_t*>(array.data()), 0);
__m128 floats = _mm_cvtph_ps(ints);
std::array<float, 4> p;
_mm_storeu_ps(p.data(), floats);
std::cout<< p[0] << p[3] << std::endl;
return 0;
}
另请参阅https://godbolt.org/z/5xva3q -
运行由 clang10.0.1 生成的二进制文件-Og -g -std=c++17 -march=haswell -fsanitize=memory -fsanitize-memory-track-origins
会产生以下输出:
Program returned: 77
==1==WARNING: MemorySanitizer: use-of-uninitialized-value
#0 0x4a5f00 (/app/output.s+0x4a5f00)
#1 0x7f1f2b3a0b96 (/lib/x86_64-linux-gnu/libc.so.6+0x21b96)
#2 0x41f369 (/app/output.s+0x41f369)
SUMMARY: MemorySanitizer: use-of-uninitialized-value (/app/output.s+0x4a5f00)
ORIGIN: invalid (0). Might be a bug in MemorySanitizer origin tracking.
This could still be a bug in your code, too!
Exiting
我不知道为什么这会导致错误,而且它只发生在特定的优化设置中。对我来说,这似乎是一个潜在的错误,但我想向更多的观众证实这一点。
编辑:这似乎在 clang 的主干版本中得到修复。