MinGW Notification List
mingw****@lists*****
Thu Jul 26 22:39:28 JST 2018
#38417: Memory load reordering optimization (-O2) bug Open Date: 2018-07-26 22:39 Last Update: 2018-07-26 22:39 URL for this Ticket: https://osdn.net//projects/mingw/ticket/38417 RSS feed for this Ticket: https://osdn.net/ticket/ticket_rss.php?group_id=3917&tid=38417 --------------------------------------------------------------------- Last Changes/Comment on this Ticket: 2018-07-26 22:39 Updated by: nik-ponomarev * New Ticket "Memory load reordering optimization (-O2) bug" created --------------------------------------------------------------------- Ticket Status: Reporter: nik-ponomarev Owner: (None) Type: Issues Status: Open Priority: 5 - Medium MileStone: (None) Component: (None) Severity: 5 - Medium Resolution: None --------------------------------------------------------------------- Ticket details: DescriptionThe bug is related to some internal code structure, so tried to reproduce the situation as close as possible. mingw with -O2 optimization flag loads memset-ed value on the first load of atomic (just struct name) value read and initialized value (right value) at the second load call. Fixes: * Removing volatile keyword * Changing int32_t to unsigned long * Other reduction of code complexity What is wrong with this code? Is it a bug? GCC7 does not have this issue. Reproducer #include <new> #include <iostream> #include <cstring> struct atomic { unsigned long my_value; public: atomic() = default ; atomic(unsigned long value) : my_value(value) {} unsigned long load() { int32_t* v = reinterpret_cast<int32_t*>(&my_value); return (volatile int32_t&)(*v); } }; //-------------------------------------------------------------------------------------------------------- struct TestStruct { atomic storage; TestStruct(){ memset(static_cast<void*>(&storage),-1,sizeof(storage)); } void operator()(unsigned long i){ atomic* ptr2 = new (&storage) atomic(i); unsigned long val1 = ptr2->load(); std::cout << "Expected:" << i << " Value first: " << val1 << std::endl; unsigned long val2 = ptr2->load(); std::cout << "Expected:" << i << " Value second: " << val2 << std::endl; } }; int main () { TestStruct()(0L); //just long also should fail; return 0; }Output: Expected:0 Value first: 4294967295 Expected:0 Value second: 0 EnvironmentWindows Server 2016 gcc -v Using built-in specs. COLLECT_GCC=gcc COLLECT_LTO_WRAPPER=d:/mingwcompilers/mingw_15.4_gcc_7.3.0/bin/../libexec/gcc/x86_64-w64-mingw32/7.3.0/lto-wrapper.exe Target: x86_64-w64-mingw32 Configured with: ../src/configure --enable-languages=c,c++ --build=x86_64-w64-mingw32 --host=x86_64-w64-mingw32 --target=x86_64-w64-mingw32 --disable-multilib --prefix=/c/temp/gcc/dest --with-sysroot=/c/temp/gcc/dest --disable-libstdcxx-pch --disable-libstdcxx-verbose --disable-nls --disable-shared --disable-win32-registry --with-tune=haswell --enable-threads=posix --enable-libgomp Thread model: posix gcc version 7.3.0 (GCC) ld -v GNU ld (GNU Binutils) 2.29.1 -- Ticket information of MinGW - Minimalist GNU for Windows project MinGW - Minimalist GNU for Windows Project is hosted on OSDN Project URL: https://osdn.net/projects/mingw/ OSDN: https://osdn.net URL for this Ticket: https://osdn.net//projects/mingw/ticket/38417 RSS feed for this Ticket: https://osdn.net/ticket/ticket_rss.php?group_id=3917&tid=38417