<html>
<head>
<base href="https://bugs.llvm.org/">
</head>
<body><table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Bug ID</th>
<td><a class="bz_bug_link
bz_status_NEW "
title="NEW - Invalid optimization with math library `fmax` and signed zeros"
href="https://bugs.llvm.org/show_bug.cgi?id=46627">46627</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>Invalid optimization with math library `fmax` and signed zeros
</td>
</tr>
<tr>
<th>Product</th>
<td>clang
</td>
</tr>
<tr>
<th>Version</th>
<td>10.0
</td>
</tr>
<tr>
<th>Hardware</th>
<td>PC
</td>
</tr>
<tr>
<th>OS</th>
<td>Linux
</td>
</tr>
<tr>
<th>Status</th>
<td>NEW
</td>
</tr>
<tr>
<th>Severity</th>
<td>enhancement
</td>
</tr>
<tr>
<th>Priority</th>
<td>P
</td>
</tr>
<tr>
<th>Component</th>
<td>-New Bugs
</td>
</tr>
<tr>
<th>Assignee</th>
<td>unassignedclangbugs@nondot.org
</td>
</tr>
<tr>
<th>Reporter</th>
<td>mpeddie@gmail.com
</td>
</tr>
<tr>
<th>CC</th>
<td>htmldeveloper@gmail.com, llvm-bugs@lists.llvm.org, neeilans@live.com, richard-llvm@metafoo.co.uk
</td>
</tr></table>
<p>
<div>
<pre>Created <span class=""><a href="attachment.cgi?id=23697" name="attach_23697" title="Preprocessed source for the example program">attachment 23697</a> <a href="attachment.cgi?id=23697&action=edit" title="Preprocessed source for the example program">[details]</a></span>
Preprocessed source for the example program
My simple test program gives different results depending on whether I enable
optimizations with -O, in the presence of -fno-builtin-fmax. The program is as
follows (preprocessed source attached):
#include <math.h>
#include <stdio.h>
int main(int argc __attribute__((unused)), char **argv
__attribute__((unused))) {
const double a = fmax(0.0, -0.0);
const double b = 1.0 / a;
const int c = isinf(b);
printf("Is b (%a) infinite? isinf() says: %d\n", b, c);
return 0;
}
No matter whether fmax returns 0.0 or -0.0, I expect b to be infinite and
isinf(b) to return a nonzero value. If I compile this program without
optimizations, running it displays
Is b (-inf) infinite? isinf() says: -1
as expected. If I enable optimizations with -O and disable compile-time
evaluation with -fno-builtin-fmax, running it displays
Is b (-inf) infinite? isinf() says: 0
If I pass the arguments to fmax in the opposite order
const double a = fmax(-0.0, 0.0);
then isinf() returns 1 as expected. If I assign 0.0 or -0.0 to a some other
way than by calling fmax(), then isinf() returns nonzero as expected. The
example program in the similarly-titled
<a class="bz_bug_link
bz_status_RESOLVED bz_closed"
title="RESOLVED FIXED - Invalid optimization with `fmax` and `-inf`"
href="show_bug.cgi?id=37776">https://bugs.llvm.org/show_bug.cgi?id=37776</a> works correctly independent of
optimization settings.
The attached file test.i is the preprocessed source file. The test program
compiles without errors or warning and never triggers the undefined-behavior
sanitizer. The compiler invocation and complete output follows:
clang-10 -v -save-temps -O -fno-builtin-isinf -fno-builtin-fmax -std=c11 -o
test -fstack-protector-all -fstrict-overflow -D_FORTIFY_SOURCE=2 -fPIC
-fsanitize=undefined -fsanitize=address -Wall -Wextra -Werror -Wimplicit
-Wshadow -Wswitch-default test.c -lm
clang version 10.0.0-4
Target: x86_64-pc-linux-gnu
Thread model: posix
InstalledDir: /usr/bin
Found candidate GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/10
Found candidate GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/5
Found candidate GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/5.5.0
Found candidate GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/6
Found candidate GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/6.5.0
Found candidate GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/7
Found candidate GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/7.5.0
Found candidate GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/8
Found candidate GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/9
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/10
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/5
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/5.5.0
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/6
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/6.5.0
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/7
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/7.5.0
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/8
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/9
Selected GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/10
Candidate multilib: .;@m64
Candidate multilib: 32;@m32
Candidate multilib: x32;@mx32
Selected multilib: .;@m64
"/usr/lib/llvm-10/bin/clang" -cc1 -triple x86_64-pc-linux-gnu -E
-save-temps=cwd -disable-free -disable-llvm-verifier -discard-value-names
-main-file-name test.c -mrelocation-model pic -pic-level 2 -mthread-model posix
-mframe-pointer=none -fmath-errno -fno-rounding-math -masm-verbose
-mconstructor-aliases -munwind-tables -target-cpu x86-64 -dwarf-column-info
-fno-split-dwarf-inlining -debugger-tuning=gdb -v -resource-dir
/usr/lib/llvm-10/lib/clang/10.0.0 -D _FORTIFY_SOURCE=2 -internal-isystem
/usr/local/include -internal-isystem /usr/lib/llvm-10/lib/clang/10.0.0/include
-internal-externc-isystem /usr/include/x86_64-linux-gnu
-internal-externc-isystem /include -internal-externc-isystem /usr/include -O2
-Wall -Wextra -Werror -Wimplicit -Wshadow -Wswitch-default -std=c11
-fdebug-compilation-dir /home/peddie -ferror-limit 19 -fmessage-length 0
-fsanitize=address,alignment,array-bounds,bool,builtin,enum,float-cast-overflow,function,integer-divide-by-zero,nonnull-attribute,null,object-size,pointer-overflow,return,returns-nonnull-attribute,shift-base,shift-exponent,signed-integer-overflow,unreachable,vla-bound,vptr
-fsanitize-recover=alignment,array-bounds,bool,builtin,enum,float-cast-overflow,function,integer-divide-by-zero,nonnull-attribute,null,object-size,pointer-overflow,returns-nonnull-attribute,shift-base,shift-exponent,signed-integer-overflow,vla-bound,vptr
-fsanitize-system-blacklist=/usr/lib/llvm-10/lib/clang/10.0.0/share/asan_blacklist.txt
-fsanitize-address-use-after-scope -fno-assume-sane-operator-new
-stack-protector 3 -fno-builtin-isinf -fno-builtin-fmax -fgnuc-version=4.2.1
-fobjc-runtime=gcc -fdiagnostics-show-option -fcolor-diagnostics
-vectorize-loops -vectorize-slp -faddrsig -o test.i -x c test.c
clang -cc1 version 10.0.0 based upon LLVM 10.0.0 default target
x86_64-pc-linux-gnu
ignoring nonexistent directory "/include"
#include "..." search starts here:
#include <...> search starts here:
/usr/local/include
/usr/lib/llvm-10/lib/clang/10.0.0/include
/usr/include/x86_64-linux-gnu
/usr/include
End of search list.
"/usr/lib/llvm-10/bin/clang" -cc1 -triple x86_64-pc-linux-gnu -emit-llvm-bc
-emit-llvm-uselists -save-temps=cwd -disable-free -disable-llvm-verifier
-discard-value-names -main-file-name test.c -mrelocation-model pic -pic-level 2
-mthread-model posix -mframe-pointer=none -fmath-errno -fno-rounding-math
-masm-verbose -mconstructor-aliases -munwind-tables -target-cpu x86-64
-dwarf-column-info -fno-split-dwarf-inlining -debugger-tuning=gdb -v
-resource-dir /usr/lib/llvm-10/lib/clang/10.0.0 -O2 -Wall -Wextra -Werror
-Wimplicit -Wshadow -Wswitch-default -std=c11 -fdebug-compilation-dir
/home/peddie -ferror-limit 19 -fmessage-length 0
-fsanitize=address,alignment,array-bounds,bool,builtin,enum,float-cast-overflow,function,integer-divide-by-zero,nonnull-attribute,null,object-size,pointer-overflow,return,returns-nonnull-attribute,shift-base,shift-exponent,signed-integer-overflow,unreachable,vla-bound,vptr
-fsanitize-recover=alignment,array-bounds,bool,builtin,enum,float-cast-overflow,function,integer-divide-by-zero,nonnull-attribute,null,object-size,pointer-overflow,returns-nonnull-attribute,shift-base,shift-exponent,signed-integer-overflow,vla-bound,vptr
-fsanitize-system-blacklist=/usr/lib/llvm-10/lib/clang/10.0.0/share/asan_blacklist.txt
-fsanitize-address-use-after-scope -fno-assume-sane-operator-new
-stack-protector 3 -fno-builtin-isinf -fno-builtin-fmax -fgnuc-version=4.2.1
-fobjc-runtime=gcc -fdiagnostics-show-option -fcolor-diagnostics
-vectorize-loops -vectorize-slp -disable-llvm-passes -faddrsig -o test.bc -x
cpp-output test.i
clang -cc1 version 10.0.0 based upon LLVM 10.0.0 default target
x86_64-pc-linux-gnu
#include "..." search starts here:
End of search list.
"/usr/lib/llvm-10/bin/clang" -cc1 -triple x86_64-pc-linux-gnu -S
-save-temps=cwd -disable-free -disable-llvm-verifier -discard-value-names
-main-file-name test.c -mrelocation-model pic -pic-level 2 -mthread-model posix
-mframe-pointer=none -fmath-errno -fno-rounding-math -masm-verbose
-mconstructor-aliases -munwind-tables -target-cpu x86-64 -dwarf-column-info
-fno-split-dwarf-inlining -debugger-tuning=gdb -v -resource-dir
/usr/lib/llvm-10/lib/clang/10.0.0 -O2 -Wall -Wextra -Werror -Wimplicit -Wshadow
-Wswitch-default -std=c11 -fdebug-compilation-dir /home/peddie -ferror-limit 19
-fmessage-length 0
-fsanitize=address,alignment,array-bounds,bool,builtin,enum,float-cast-overflow,function,integer-divide-by-zero,nonnull-attribute,null,object-size,pointer-overflow,return,returns-nonnull-attribute,shift-base,shift-exponent,signed-integer-overflow,unreachable,vla-bound,vptr
-fsanitize-recover=alignment,array-bounds,bool,builtin,enum,float-cast-overflow,function,integer-divide-by-zero,nonnull-attribute,null,object-size,pointer-overflow,returns-nonnull-attribute,shift-base,shift-exponent,signed-integer-overflow,vla-bound,vptr
-fsanitize-system-blacklist=/usr/lib/llvm-10/lib/clang/10.0.0/share/asan_blacklist.txt
-fsanitize-address-use-after-scope -fno-assume-sane-operator-new
-stack-protector 3 -fno-builtin-isinf -fno-builtin-fmax -fgnuc-version=4.2.1
-fobjc-runtime=gcc -fdiagnostics-show-option -fcolor-diagnostics
-vectorize-loops -vectorize-slp -faddrsig -o test.s -x ir test.bc
clang -cc1 version 10.0.0 based upon LLVM 10.0.0 default target
x86_64-pc-linux-gnu
"/usr/lib/llvm-10/bin/clang" -cc1as -triple x86_64-pc-linux-gnu -filetype obj
-main-file-name test.c -target-cpu x86-64 -dwarf-version=4 -mrelocation-model
pic -o test.o test.s
"/usr/bin/ld" --hash-style=both --build-id --eh-frame-hdr -m elf_x86_64
-dynamic-linker /lib64/ld-linux-x86-64.so.2 -o test
/usr/bin/../lib/gcc/x86_64-linux-gnu/10/../../../x86_64-linux-gnu/crt1.o
/usr/bin/../lib/gcc/x86_64-linux-gnu/10/../../../x86_64-linux-gnu/crti.o
/usr/bin/../lib/gcc/x86_64-linux-gnu/10/crtbegin.o
-L/usr/bin/../lib/gcc/x86_64-linux-gnu/10
-L/usr/bin/../lib/gcc/x86_64-linux-gnu/10/../../../x86_64-linux-gnu
-L/lib/x86_64-linux-gnu -L/lib/../lib64 -L/usr/lib/x86_64-linux-gnu
-L/usr/bin/../lib/gcc/x86_64-linux-gnu/10/../../..
-L/usr/lib/llvm-10/bin/../lib -L/lib -L/usr/lib --whole-archive
/usr/lib/llvm-10/lib/clang/10.0.0/lib/linux/libclang_rt.asan-x86_64.a
--no-whole-archive
--dynamic-list=/usr/lib/llvm-10/lib/clang/10.0.0/lib/linux/libclang_rt.asan-x86_64.a.syms
test.o -lm --no-as-needed -lpthread -lrt -lm -ldl -lgcc --as-needed -lgcc_s
--no-as-needed -lc -lgcc --as-needed -lgcc_s --no-as-needed
/usr/bin/../lib/gcc/x86_64-linux-gnu/10/crtend.o
/usr/bin/../lib/gcc/x86_64-linux-gnu/10/../../../x86_64-linux-gnu/crtn.o</pre>
</div>
</p>
<hr>
<span>You are receiving this mail because:</span>
<ul>
<li>You are on the CC list for the bug.</li>
</ul>
</body>
</html>