<html><head><meta http-equiv="Content-Type" content="text/html charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class="">Hi,<div class=""><br class=""></div><div class="">I’m fairly new to the world of C compiler edge cases and their undefined behaviour or what the C standards say about this particular one, but as it’s behaviour changed ‘recently’ I’d like to ask for some clarification nonetheless.</div><div class=""><br class=""></div><div class="">Since moving the build process of our iOS app to Xcode 7, an animation that was expected to run for a long time suddenly ran for a very short period. The issue turned out to be a change in how `HUGE_VAL` as an argument was converted to a `long`, which is the parameter type the function expected. The full case in question can be found here <a href="https://github.com/artsy/eigen/issues/838#issuecomment-145600551" class="">https://github.com/artsy/eigen/issues/838#issuecomment-145600551</a>, but I have reduced it to the following example for ease of understanding.</div><div class=""><br class=""></div><div class="">——</div><div class=""><br class=""></div><div class="">~/tmp » cat huge-vail.c<br class="">// This could use the HUGE_VAL macro instead of __builtin_huge_val, but I’m<br class="">// using the built-in one here, because that’s what the macro would use under<br class="">// the hood.<br class=""><br class="">#include <math.h><br class="">#include <assert.h><br class=""><br class="">void succeed(double value) { assert(value > 43); }<br class="">void fail(long value)      { assert(value > 43); }<br class=""><br class="">int main(void)<br class="">{<br class="">  asm volatile ("movq $42, %%rdi" :);<br class=""><br class="">  // When passing to a function with the correct parameter type, this works as expected.<br class="">  succeed(__builtin_huge_val());<br class="">  // When passing to a function that changes the type, the value in the $rdi register is passed as-is.<br class="">  fail(__builtin_huge_val());<br class=""><br class="">  return 0;<br class="">}</div><div class=""><br class=""></div><div class="">——</div><div class=""><br class=""></div><div class=""><div class="">~/tmp » clang -v</div><div class="">Apple LLVM version 7.0.0 (clang-700.0.72)</div><div class="">Target: x86_64-apple-darwin14.5.0</div><div class="">Thread model: posix</div><div class="">~/tmp » clang huge-vail.c -o huge-vail</div><div class="">~/tmp » ./huge-vail </div><div class="">Assertion failed: (value > 43), function fail, file huge-vail.c, line 9.</div><div class="">fish: Job 1, './huge-vail ' terminated by signal SIGABRT (Abort)</div></div><div class=""><br class=""></div><div class="">When looking at the LLVM IR generated for this file, there’s a clear difference in what gets passed to the function. Specifically the undef part, which is considered a ‘undefined variable’:</div><div class=""><br class=""></div><div class=""><div class="">define i32 @main() #0 {</div><div class="">  %1 = alloca i32, align 4</div><div class="">  store i32 0, i32* %1</div><div class="">  call void asm sideeffect "movq $$42, %rdi", "~{dirflag},~{fpsr},~{flags}"() #3, !srcloc !1</div><div class="">  call void @succeed(double 0x7FF0000000000000)</div><div class="">  call void @fail(i64 undef)</div><div class="">  ret i32 0</div><div class="">}</div></div><div class=""><br class=""></div><div class="">——</div><div class=""><br class=""></div><div class="">With an older clang this works as I would (naively) expect and how it has been working for the past 3 years:<br class=""><br class=""><div class="">~/tmp » /Applications/Xcode-6.4.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang -v</div><div class="">Apple LLVM version 6.1.0 (clang-602.0.53) (based on LLVM 3.6.0svn)</div><div class="">Target: x86_64-apple-darwin14.5.0</div><div class="">Thread model: posix</div><div class="">~/tmp » /Applications/Xcode-6.4.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang huge-vail.c -o huge-vail</div><div class="">~/tmp » ./huge-vail </div><div class="">~/tmp » echo $status</div><div class="">0</div><br class="Apple-interchange-newline">The LLVM IR for that is:<br class=""><br class=""><div class="">define i32 @main() #0 {</div><div class="">  %1 = alloca i32, align 4</div><div class="">  store i32 0, i32* %1</div><div class="">  call void asm sideeffect "movq $$42, %rdi", "~{dirflag},~{fpsr},~{flags}"() #4, !srcloc !1</div><div class="">  call void @succeed(double 0x7FF0000000000000)</div><div class="">  call void @fail(i64 9223372036854775807)</div><div class="">  ret i32 0</div><div class="">}</div></div><div class=""><br class=""></div><div class="">——</div><div class=""><br class=""></div><div class="">I understand that `HUGE_VAL` is actually a `double` and so the `double` to `long` conversion is probably where this behaviour comes from, but I wonder:</div><div class="">* What the reason is to mark it as undefined now vs the large value used previously?</div><div class="">* If there is a compiler flag that would have turned on checks for this undefined behaviour? I’ve tried the flags listed here in the manual, but none of those triggered a warning for me: <a href="http://clang.llvm.org/docs/UsersManual.html#controlling-code-generation" class="">http://clang.llvm.org/docs/UsersManual.html#controlling-code-generation</a></div><div class=""><br class=""></div><div class="">Simply pointing me to a doc that explains this case is perfectly fine too!</div><div class=""><br class=""></div><div class="">Kind regards,</div><div class="">Eloy Durán</div><div class=""><br class=""></div></body></html>