[cfe-dev] Optimization creates infinite loop

Alex Rønne Petersen via cfe-dev cfe-dev at lists.llvm.org
Fri Nov 1 03:10:53 PDT 2019


Hello,

This isn't really surprising. Since RT_Time is not marked volatile,
nor is it accessed through std::atomic functions (or the equivalent
builtins), the compiler has no reason to assume that any external
actor can change the value of it. So the load is moved out of the loop
since doing it repeatedly would, from the compiler's point of view, be
wasteful.

If RT_Time can be changed by hardware or something along those lines,
then volatile is appropriate. If another thread can change it, you
should be using atomic APIs to access it.


On Fri, Nov 1, 2019 at 10:59 AM Gaier, Bjoern via cfe-dev
<cfe-dev at lists.llvm.org> wrote:
>
> Hello Clang-Developer,
>
>
>
> We ran into a – for us big problem – with the Clang compiler. We had the following code:
>
> extern double RT_Time;
>
> void Sample1sec()
>
> {
>
>        const double startTime = RT_Time;
>
>        while(RT_Time < (startTime + 5.0005));
>
> }
>
>
>
> The value of RT_Time will change after a while and the loop is supposed to wait for it. However the assembly code will lead into an endless loop if the condition is not meet the first time:
>
> "?Sample1sec@@YAXXZ":                   # @"?Sample1sec@@YAXXZ"
>
> .Lfunc_begin0:
>
>                 .cv_func_id 0
>
> # %bb.0:
>
>                 .cv_file 1 "C:\\Users\\h16020\\Desktop\\Projects\\Squirrel\\Project\\Leviathan\\CM_Driver1\\CM_Driver1.cpp" "4E92768969662F6E513C11595FEA9D63" 1
>
>                 .cv_loc  0 1 5 0                 # CM_Driver1.cpp:5:0
>
>                 movsd  xmm0, qword ptr [rip + "?RT_Time@@3NA"] # xmm0 = mem[0],zero
>
> .Ltmp0:
>
>                 #DEBUG_VALUE: Sample1sec:startTime <- $xmm0
>
>                 movsd  xmm1, qword ptr [rip + __real at 40140083126e978d] # xmm1 = mem[0],zero
>
>                 .cv_loc  0 1 0 0                 # CM_Driver1.cpp:0:0
>
>                 addsd   xmm1, xmm0
>
>                 ucomisd               xmm1, xmm0
>
>                 .cv_loc  0 1 6 0                 # CM_Driver1.cpp:6:0
>
>                 jbe         .LBB0_2
>
> .Ltmp1:
>
>                 .p2align                4, 0x90
>
> .LBB0_1:                                # =>This Inner Loop Header: Depth=1
>
>                 #DEBUG_VALUE: Sample1sec:startTime <- $xmm0
>
>                 jmp        .LBB0_1
>
> .Ltmp2:
>
> .LBB0_2:
>
>                 #DEBUG_VALUE: Sample1sec:startTime <- $xmm0
>
>                 .cv_loc  0 1 7 0                 # CM_Driver1.cpp:7:0
>
>                 ret
>
> .Ltmp3:
>
> .Lfunc_end0:
>
>
>
> Marking RT_Time with volatile fixes the problem. But I wonder, why is doing Clang that optimization? RT_Time was not declared ‘const’ so didn’t Clang had to know the value can change?
>
> Is this a bug or do I understand something wrong?
>
>
>
> Kind greetings and thank you for the help in advance,
>
> Björn
>
>
>
> P.S.
>
> My compile flags (when using -###) are:
>
> clang-cl.exe "-cc1" "-triple" "x86_64-pc-windows-msvc19.16.27034" "-S" "-disable-free" "-disable-llvm-verifier" "-discard-value-names" "-main-file-name" "CM_Driver1.cpp" "-mrelocation-model" "pic" "-pic-level" "2" "-mthread-model" "posix" "-relaxed-aliasing" "-fmath-errno" "-masm-verbose" "-mconstructor-aliases" "-munwind-tables" "-target-cpu" "x86-64" "-mllvm" "-x86-asm-syntax=intel" "-D_MT" "-flto-visibility-public-std" "--dependent-lib=libcmt" "--dependent-lib=oldnames" "-fcxx-exceptions" "-fexceptions" "-fexternc-nounwind" "-fms-volatile" "-fdefault-calling-conv=cdecl" "-fdiagnostics-format" "msvc" "-gcodeview" "-debug-info-kind=limited" "-momit-leaf-frame-pointer" "-ffunction-sections" "-coverage-notes-file" "C:\\Users\\h16020\\Desktop\\Projects\\Squirrel\\Project\\Leviathan\\CM_Driver1\\CM_Driver1.gcno" "-resource-dir" "S:\\STSHARE\\EXTERNAL\\LLVM\\lib\\clang\\9.0.0" "-D" "WIN32" "-D" "NTSYSAD_CM" "-D" "_CONSOLE" "-D" "CODEMODULE" "-D" "__asm=RemoveAssembly" "-D" "_UNICODE" "-D" "UNICODE" "-internal-isystem" "S:\\STSHARE\\EXTERNAL\\LLVM\\lib\\clang\\9.0.0\\include" "-internal-isystem" "S:\\STSHARE\\EXTERNAL\\LLVM\\Include" "-internal-isystem" "S:\\STEXEC\\ExecutionEnvironment\\INCLUDE" "-internal-isystem" "C:\\Program Files (x86)\\Microsoft Visual Studio\\2017\\Professional\\VC\\Tools\\MSVC\\14.16.27023\\include" "-internal-isystem" "C:\\Program Files (x86)\\Microsoft Visual Studio\\2017\\Professional\\VC\\Tools\\MSVC\\14.16.27023\\atlmfc\\include" "-internal-isystem" "C:\\Program Files (x86)\\Microsoft Visual Studio\\2017\\Professional\\VC\\Auxiliary\\VS\\include" "-internal-isystem" "C:\\Program Files (x86)\\Windows Kits\\10\\Include\\10.0.10240.0\\ucrt" "-internal-isystem" "C:\\Program Files (x86)\\Windows Kits\\8.1\\Include\\um" "-internal-isystem" "C:\\Program Files (x86)\\Windows Kits\\8.1\\Include\\shared" "-internal-isystem" "C:\\Program Files (x86)\\Windows Kits\\8.1\\Include\\winrt" "-internal-isystem" "C:\\Program Files (x86)\\Windows Kits\\NETFXSDK\\4.6.1\\Include\\um" "-O2" "-WCL4" "-Wno-error" "-Wno-unused-command-line-argument" "-Wno-microsoft-cast" "-Wno-writable-strings" "-Wno-microsoft-enum-forward-reference" "-Wno-invalid-token-paste" "-fdeprecated-macro" "-fdebug-compilation-dir" "C:\\Users\\h16020\\Desktop\\Projects\\Squirrel\\Project\\Leviathan\\CM_Driver1" "-ferror-limit" "3000" "-fmessage-length" "0" "-fno-use-cxa-atexit" "-fms-extensions" "-fms-compatibility" "-fms-compatibility-version=19.16.27034" "-std=c++14" "-fno-threadsafe-statics" "-fdelayed-template-parsing" "-finline-functions" "-fobjc-runtime=gcc" "-fno-caret-diagnostics" "-fdiagnostics-show-option" "-fno-show-column" "-vectorize-loops" "-vectorize-slp" "-faddrsig" "-o" "x64\\Release\\CM_Driver1.asm" "-x" "c++" "CM_Driver1.cpp"
>
>
>
> clang-cl.exe "-cc1as" "-triple" "x86_64-pc-windows-msvc19.16.27034" "-filetype" "obj" "-main-file-name" "CM_Driver1.cpp" "-target-cpu" "x86-64" "-dwarf-version=4" "-mrelocation-model" "pic" "-mincremental-linker-compatible" "-o" "x64\\Release\\CM_Driver1.obj" "x64\\Release\\CM_Driver1.asm"
>
> Als GmbH eingetragen im Handelsregister Bad Homburg v.d.H. HRB 9816, USt.ID-Nr. DE 114 165 789 Geschäftsführer: Dr. Hiroshi Nakamura, Dr. Robert Plank, Markus Bode, Heiko Lampert, Takashi Nagano, Takeshi Fukushima. Junichi Tajika
> _______________________________________________
> cfe-dev mailing list
> cfe-dev at lists.llvm.org
> https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev



More information about the cfe-dev mailing list