Bug 52592

Summary: [4.7/4.8 Regression] compilation failure with undefined reference to `__builtin_iround'
Product: gcc Reporter: ojab <ojab>
Component: middle-endAssignee: Jakub Jelinek <jakub>
Status: RESOLVED FIXED    
Severity: critical Keywords: link-failure, wrong-code
Priority: P3    
Version: 4.7.0   
Target Milestone: 4.7.0   
Host: Target: x86_64-*-linux-gnu
Build: Known to work:
Known to fail: Last reconfirmed: 2012-03-15 00:00:00
Attachments: Preprocessed source
Patch which fixes the problem
gcc47-pr52592.patch

Description ojab 2012-03-15 05:36:11 UTC
MPlayer build fails with gcc-4.7.0rc2 (see http://bugzilla.mplayerhq.hu/show_bug.cgi?id=2047 ):

gcc -o mencoder mencoder.o parser-mecmd.o … -lfribidi -lass -lz -lbz2 -lmad -lvpx -lpthread -ldl -rdynamic   -lmp3lame
ffmpeg/libavcodec/libavcodec.a(ffv1.o): In function `encode_init':
ffv1.c:(.text.unlikely+0x16a1): undefined reference to `__builtin_iround'
collect2: error: ld returned 1 exit status
make: *** [mencoder] Error 1
make: *** Waiting for unfinished jobs....
ffmpeg/libavcodec/libavcodec.a(ffv1.o): In function `encode_init':
ffv1.c:(.text.unlikely+0x16a1): undefined reference to `__builtin_iround'
collect2: error: ld returned 1 exit status
make: *** [mplayer] Error 1


Looks like related to http://gcc.gnu.org/ml/gcc-patches/2011-08/msg01001.html and 
>Please document those in doc/extend.texi and make sure they do not leak into the global namespace as ifloor, etc., but are only available as __builtin_ifloor, etc..
from http://gcc.gnu.org/ml/gcc-patches/2011-08/msg01003.html

mplayer builds fine with CFLAGS=-ffast-math, I think that it can be gcc bug.

Please tell me what additional info is needed.
Comment 1 Andrew Pinski 2012-03-15 06:00:37 UTC
What target is this on?
Comment 2 Andrew Pinski 2012-03-15 06:01:53 UTC
Can you attach the preprocessed source for ffv1.c ?
Also can you give the exact command is used to compile ffv1.c ?
Comment 3 ojab 2012-03-15 06:04:02 UTC
$ gcc -v
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-unknown-linux-gnu/4.7.0/lto-wrapper
Target: x86_64-unknown-linux-gnu
Configured with: ../gcc-4.7.0-RC-20120314/configure --prefix=/usr --libexecdir=/usr/lib --enable-shared --enable-threads=posix --enable-__cxa_atexit --enable-clocale=gnu --enable-languages=c,c++ --disable-multilib --disable-bootstrap --with-system-zlib
Thread model: posix
gcc version 4.7.0 20120314 (prerelease) (GCC)

Compilation string:
gcc -I. -I./  -DHAVE_AV_CONFIG_H -Wundef -Wall -Wno-switch -Wno-parentheses -Wpointer-arith -Wredundant-decls -Wstrict-prototypes -Wmissing-prototypes -Wdisabled-optimization -Wno-pointer-sign -Wdeclaration-after-statement -std=gnu99 -Werror-implicit-function-declaration -O4 -march=native -mtune=native -pipe -ffast-math -fomit-frame-pointer -fno-tree-vectorize -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64 -D_LARGEFILE64_SOURCE -Ilibdvdread4 -I. -Iffmpeg  -D_REENTRANT   -I/usr/include/freetype2  -MD -MP -c -o libavcodec/ffv1.o libavcodec/ffv1.c
Comment 4 ojab 2012-03-15 06:04:38 UTC
Created attachment 26896 [details]
Preprocessed source
Comment 5 Andrew Pinski 2012-03-15 06:13:11 UTC
Reducing a testcase.
Comment 6 Andrew Pinski 2012-03-15 06:13:42 UTC
Note a simple testcase works correctly.
Comment 7 Andrew Pinski 2012-03-15 06:43:55 UTC
Reduced testcase:
  int best_state[256][256];
 __attribute__((cold)) 
int encode_init(double p) {
  return best_state[(int)round(p)][0];
}

--- CUT ---
(define_expand "lround<X87MODEF:mode><SWI248x:mode>2"
  [(match_operand:SWI248x 0 "nonimmediate_operand" "")
   (match_operand:X87MODEF 1 "register_operand" "")]
  "(TARGET_USE_FANCY_MATH_387
    && (!(SSE_FLOAT_MODE_P (<X87MODEF:MODE>mode) && TARGET_SSE_MATH)
        || TARGET_MIX_SSE_I387)
    && flag_unsafe_math_optimizations)
   || (SSE_FLOAT_MODE_P (<X87MODEF:MODE>mode) && TARGET_SSE_MATH
       && <SWI248x:MODE>mode != HImode
       && ((<SWI248x:MODE>mode != DImode) || TARGET_64BIT)
       && !flag_trapping_math && !flag_rounding_math)"
{
  if (optimize_insn_for_size_p ())
    FAIL;

--- CUT ---
Looks like there is a disconnect about when choosing iround over lround.

I bet the code that handles the expanding of __builtin_iround (expand_builtin_int_roundingfn_2) should do exactly what the expanding of iceil does (expand_builtin_int_roundingfn).
Comment 8 Andrew Pinski 2012-03-15 07:10:46 UTC
Here is the smallest testcase:
 __attribute__((cold))
int encode_init(double p) {
 return (int)__builtin_round(p);
}
---- CUT ----
I think I have a patch, just testing it a little bit.
Comment 9 Andrew Pinski 2012-03-15 07:25:21 UTC
Created attachment 26897 [details]
Patch which fixes the problem

I don't have enough time to regress test this patch though.
But here is the ChangeLog for it:
* builtins.c (expand_builtin_int_roundingfn_2): If the expanding of iround optab fails, then expand using lround call.
Comment 10 Jakub Jelinek 2012-03-15 09:36:02 UTC
Created attachment 26898 [details]
gcc47-pr52592.patch

Fixed up and simplified patch, untested so far.

I guess expand_builtin_int_roundingfn (for 4.8 only) could be simplified too, by using mathfn_built_in_1 (, , 0); and dropping the whole if (fallback_fndecl == NULL_TREE) { ... }.
Comment 11 Jakub Jelinek 2012-03-15 13:30:10 UTC
Author: jakub
Date: Thu Mar 15 13:30:04 2012
New Revision: 185431

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=185431
Log:
	PR middle-end/52592
	* builtins.c (expand_builtin_int_roundingfn_2): If expanding
	BUILT_IN_IR{INT,OUND}* using optab fails, emit lr{int,ound}*
	calls instead of __builtin_ir{int,ound}*.

	* gcc.dg/pr52592.c: New test.

Added:
    trunk/gcc/testsuite/gcc.dg/pr52592.c
Modified:
    trunk/gcc/ChangeLog
    trunk/gcc/builtins.c
    trunk/gcc/testsuite/ChangeLog
Comment 12 Jakub Jelinek 2012-03-15 13:40:19 UTC
Author: jakub
Date: Thu Mar 15 13:40:13 2012
New Revision: 185432

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=185432
Log:
	PR middle-end/52592
	* builtins.c (expand_builtin_int_roundingfn_2): If expanding
	BUILT_IN_IR{INT,OUND}* using optab fails, emit lr{int,ound}*
	calls instead of __builtin_ir{int,ound}*.

	* gcc.dg/pr52592.c: New test.

Added:
    branches/gcc-4_7-branch/gcc/testsuite/gcc.dg/pr52592.c
Modified:
    branches/gcc-4_7-branch/gcc/ChangeLog
    branches/gcc-4_7-branch/gcc/builtins.c
    branches/gcc-4_7-branch/gcc/testsuite/ChangeLog
Comment 13 Jakub Jelinek 2012-03-15 13:42:41 UTC
Fixed.