[LLVMbugs] [Bug 3059] New: CBE miscompilation with global weak alias

bugzilla-daemon at cs.uiuc.edu bugzilla-daemon at cs.uiuc.edu
Wed Nov 12 13:37:57 PST 2008


           Summary: CBE miscompilation with global weak alias
           Product: libraries
           Version: trunk
          Platform: PC
        OS/Version: All
            Status: NEW
          Severity: normal
          Priority: P2
         Component: Backend: C
        AssignedTo: unassignedbugs at nondot.org
        ReportedBy: jasandov at rice.edu
                CC: llvmbugs at cs.uiuc.edu

Created an attachment (id=2199)
 --> (http://llvm.org/bugs/attachment.cgi?id=2199)
CBE weak alias test case

I'm running into an error with the CBE.  I've narrowed the problem down to a
very simple CPP program:

#include <string>

static std::string hello("Hello world!");

int main() {
  return 0;

When I generate C code (using LLVM's C backend) with the following commands, I
get a compile error on the resulting C code:

$ llvm-g++ -emit-llvm -O2 -c -o CBE_Bug.bc CBE_Bug.cpp
$ llc -f -march=c CBE_Bug.bc
$ gcc -c CBE_Bug.cbe.c
CBE_Bug.cbe.c:238: warning: conflicting types for built-in function
CBE_Bug.cbe.c: In function ‘main’:
CBE_Bug.cbe.c:269: warning: return type of ‘main’ is not ‘int’
CBE_Bug.cbe.c: In function ‘__tcf_0’:
CBE_Bug.cbe.c:307: error: ‘_ZL22__gthrw_pthread_cancelm’ undeclared (first
use in this function)
CBE_Bug.cbe.c:307: error: (Each undeclared identifier is reported only once
CBE_Bug.cbe.c:307: error: for each function it appears in.)

Here are the relevant lines from CBE_Bug.cbe.c.  Notice that "pthread_cancel"
is declared as a weak external symbol, but "_ZL22__gthrw_pthread_cancelm" is
never declared (even though it is used).

$ grep -Hn pthread_cancel CBE_Bug.cbe.c
CBE_Bug.cbe.c:228:extern unsigned int pthread_cancel(unsigned long long )
CBE_Bug.cbe.c:307:  if ((((unsigned char )(bool )(((unsigned int )(bool
)(_ZL22__gthrw_pthread_cancelm != ((unsigned int  (*) (unsigned long long
))/*NULL*/0))) != 0u)) != ((unsigned char )0))) {

Here are the relevant lines from the LLVM bitcode.  The bitcode appears
correct since "pthread_cancel" is declared as extern_weak and the alias
"_ZL22__gthrw_pthread_cancelm" is declared as a weak alias of "pthread_cancel".
 I'm just a bit confused about "pthread_cancel" -- shouldn't it be declared as
a regular external symbol?

$ llvm-dis CBE_Bug.bc
$ grep -Hn pthread_cancel CBE_Bug.ll
CBE_Bug.ll:24:@_ZL22__gthrw_pthread_cancelm = alias weak i32 (i64)*
@pthread_cancel             ; <i32 (i64)*> [#uses=1]
CBE_Bug.ll:76:  br i1 icmp ne (i8 zext (i1 icmp ne (i32 zext (i1 icmp ne (i32
(i64)* @_ZL22__gthrw_pthread_cancelm, i32 (i64)* null) to i32), i32 0) to i8),
i8 0), label %bb20, label %bb21
CBE_Bug.ll:117:declare extern_weak i32 @pthread_cancel(i64)

Finally, looking at the preprocessed CBE_Bug.cpp file (before running it
through LLVM), I can see the appropriate declarations for "pthread_cancel" and
its alias.  This (mostly) matches the LLVM representation above.

$ llvm-g++ -emit-llvm -E -o CBE_Bug.E.cpp CBE_Bug.cpp
$ grep -Hn pthread_cancel CBE_Bug.E.cpp
CBE_Bug.E.cpp:4417:extern int pthread_cancel (pthread_t __th);
CBE_Bug.E.cpp:6061:static __typeof(pthread_cancel) __gthrw_pthread_cancel
__attribute__ ((__weakref__("pthread_cancel")));
CBE_Bug.E.cpp:6078:    = __extension__ (void *) &__gthrw_pthread_cancel;

So, it looks like the CBE code generator is not producing correct output for
the alias, since the LLVM bitcode appears correct.  The CBE correctly generates
an external weak declaration for pthread_cancel, but it produces no declaration
for the associated alias.  This problem only appears when I turn on
optimization (any level above 0) -- the code compiles fine at "-O0".

I'm using LLVM release 2.4, but I also verified that the problem occurs with
the latest trunk revision from SVN.  I'm on x86_64 linux (Fedora 8) with gcc

The problem should be easy to reproduce with the input file I gave above; if
you want any other temp files, just let me know.

Configure bugmail: http://llvm.org/bugs/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
You are on the CC list for the bug.

More information about the llvm-bugs mailing list