<html xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:w="urn:schemas-microsoft-com:office:word" xmlns:m="http://schemas.microsoft.com/office/2004/12/omml" xmlns="http://www.w3.org/TR/REC-html40">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta name="Generator" content="Microsoft Word 15 (filtered medium)">
<style><!--
/* Font Definitions */
@font-face
        {font-family:"Cambria Math";
        panose-1:2 4 5 3 5 4 6 3 2 4;}
@font-face
        {font-family:Calibri;
        panose-1:2 15 5 2 2 2 4 3 2 4;}
@font-face
        {font-family:Consolas;
        panose-1:2 11 6 9 2 2 4 3 2 4;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
        {margin:0cm;
        margin-bottom:.0001pt;
        font-size:11.0pt;
        font-family:"Calibri",sans-serif;
        mso-fareast-language:EN-US;}
a:link, span.MsoHyperlink
        {mso-style-priority:99;
        color:#0563C1;
        text-decoration:underline;}
a:visited, span.MsoHyperlinkFollowed
        {mso-style-priority:99;
        color:#954F72;
        text-decoration:underline;}
span.EmailStyle17
        {mso-style-type:personal-compose;
        font-family:"Calibri",sans-serif;
        color:windowtext;}
.MsoChpDefault
        {mso-style-type:export-only;
        font-family:"Calibri",sans-serif;
        mso-fareast-language:EN-US;}
@page WordSection1
        {size:612.0pt 792.0pt;
        margin:70.85pt 70.85pt 2.0cm 70.85pt;}
div.WordSection1
        {page:WordSection1;}
--></style><!--[if gte mso 9]><xml>
<o:shapedefaults v:ext="edit" spidmax="1026" />
</xml><![endif]--><!--[if gte mso 9]><xml>
<o:shapelayout v:ext="edit">
<o:idmap v:ext="edit" data="1" />
</o:shapelayout></xml><![endif]-->
</head>
<body lang="DE" link="#0563C1" vlink="#954F72">
<div class="WordSection1">
<p class="MsoNormal"><span lang="EN-GB">Hello Clang-Developer,<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-GB"><o:p> </o:p></span></p>
<p class="MsoNormal"><span lang="EN-GB">We ran into a – for us big problem – with the Clang compiler. We had the following code:<o:p></o:p></span></p>
<p class="MsoNormal" style="text-autospace:none"><span lang="EN-GB" style="font-size:9.5pt;font-family:Consolas;color:blue">extern</span><span lang="EN-GB" style="font-size:9.5pt;font-family:Consolas;color:black">
</span><span lang="EN-GB" style="font-size:9.5pt;font-family:Consolas;color:blue">double</span><span lang="EN-GB" style="font-size:9.5pt;font-family:Consolas;color:black"> RT_Time;<o:p></o:p></span></p>
<p class="MsoNormal" style="text-autospace:none"><span lang="EN-GB" style="font-size:9.5pt;font-family:Consolas;color:blue">void</span><span lang="EN-GB" style="font-size:9.5pt;font-family:Consolas;color:black"> Sample1sec()<o:p></o:p></span></p>
<p class="MsoNormal" style="text-autospace:none"><span lang="EN-GB" style="font-size:9.5pt;font-family:Consolas;color:black">{<o:p></o:p></span></p>
<p class="MsoNormal" style="text-autospace:none"><span lang="EN-GB" style="font-size:9.5pt;font-family:Consolas;color:black">      
</span><span lang="EN-GB" style="font-size:9.5pt;font-family:Consolas;color:blue">const</span><span lang="EN-GB" style="font-size:9.5pt;font-family:Consolas;color:black">
</span><span lang="EN-GB" style="font-size:9.5pt;font-family:Consolas;color:blue">double</span><span lang="EN-GB" style="font-size:9.5pt;font-family:Consolas;color:black"> startTime = RT_Time;<o:p></o:p></span></p>
<p class="MsoNormal" style="text-autospace:none"><span lang="EN-GB" style="font-size:9.5pt;font-family:Consolas;color:black">      
</span><span style="font-size:9.5pt;font-family:Consolas;color:blue">while</span><span style="font-size:9.5pt;font-family:Consolas;color:black">(RT_Time < (startTime + 5.0005));<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:9.5pt;font-family:Consolas;color:black">}<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:9.5pt;font-family:Consolas;color:black"><o:p> </o:p></span></p>
<p class="MsoNormal"><span lang="EN-GB">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:<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-GB">"?Sample1sec@@YAXXZ":                   # @"?Sample1sec@@YAXXZ"<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-GB">.Lfunc_begin0:<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-GB">                .cv_func_id 0<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-GB"># %bb.0:<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-GB">                .cv_file 1 "C:\\Users\\h16020\\Desktop\\Projects\\Squirrel\\Project\\Leviathan\\CM_Driver1\\CM_Driver1.cpp" "4E92768969662F6E513C11595FEA9D63" 1<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-GB">                .cv_loc  0 1 5 0                 # CM_Driver1.cpp:5:0<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-GB">                movsd  xmm0, qword ptr [rip + "?RT_Time@@3NA"] # xmm0 = mem[0],zero<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-GB">.Ltmp0:<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-GB">                #DEBUG_VALUE: Sample1sec:startTime <- $xmm0<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-GB">                movsd  xmm1, qword ptr [rip + __real@40140083126e978d] # xmm1 = mem[0],zero<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-GB">                .cv_loc  0 1 0 0                 # CM_Driver1.cpp:0:0<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-GB">                addsd   xmm1, xmm0<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-GB">                ucomisd               xmm1, xmm0<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-GB">                .cv_loc  0 1 6 0                 # CM_Driver1.cpp:6:0<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-GB">                jbe         .LBB0_2<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-GB">.Ltmp1:<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-GB">                .p2align                4, 0x90<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-GB">.LBB0_1:                                # =>This Inner Loop Header: Depth=1<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-GB">                #DEBUG_VALUE: Sample1sec:startTime <- $xmm0<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-GB">                jmp        .LBB0_1<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-GB">.Ltmp2:<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-GB">.LBB0_2:<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-GB">                #DEBUG_VALUE: Sample1sec:startTime <- $xmm0<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-GB">                .cv_loc  0 1 7 0                 # CM_Driver1.cpp:7:0<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-GB">                ret<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-GB">.Ltmp3:<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-GB">.Lfunc_end0:<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-GB"><o:p> </o:p></span></p>
<p class="MsoNormal"><span lang="EN-GB">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?
<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-GB">Is this a bug or do I understand something wrong?<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-GB"><o:p> </o:p></span></p>
<p class="MsoNormal"><span lang="EN-GB">Kind greetings and thank you for the help in advance,<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-GB">Björn<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-GB"><o:p> </o:p></span></p>
<p class="MsoNormal"><span lang="EN-GB">P.S.<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-GB">My compile flags (when using -###) are:<o:p></o:p></span></p>
<p class="MsoNormal" style="text-autospace:none"><span lang="EN-GB" style="font-size:9.5pt;font-family:Consolas;color:black">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"<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-GB" style="font-size:9.5pt;font-family:Consolas;color:black"><o:p> </o:p></span></p>
<p class="MsoNormal"><span lang="EN-GB" style="font-size:9.5pt;font-family:Consolas;color:black">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"</span><span lang="EN-GB"><o:p></o:p></span></p>
</div>
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
</body>
</html>