llvm / llvm-project

The LLVM Project is a collection of modular and reusable compiler and toolchain technologies.
http://llvm.org
Other
29.39k stars 12.15k forks source link

llvm-g++ produces non-working output #1061

Closed llvmbot closed 14 years ago

llvmbot commented 18 years ago
Bugzilla Link 689
Resolution FIXED
Resolved on Mar 06, 2010 13:59
Version 1.6
OS MacOS X
Reporter LLVM Bugzilla Contributor

Extended Description

It looks like there's a bug in llvm-g++: a very simple c++ source code compiled by llvm-gcc produces a working bytecode, but the same file compiled by llvm-g++ generates a smaller and non working output:

PowerBookG4:/llvm/test mycroft$ cat hello.cpp

include

int main() { std::cout << "hello world\n"; return 0; }

PowerBookG4:/llvm/test mycroft$ llvm-gcc hello.cpp PowerBookG4:/llvm/test mycroft$ ls -l total 440 -rwxr-xr-x 1 mycroft admin 64 Jan 12 00:03 a.out -rwxr-xr-x 1 mycroft admin 216923 Jan 12 00:03 a.out.bc -rw-r--r-- 1 mycroft admin 95 Jan 12 00:00 hello.cpp PowerBookG4:/llvm/test mycroft$ ./a.out hello world

PowerBookG4:/llvm/test mycroft$ rm a.*

PowerBookG4:/llvm/test mycroft$ llvm-g++ hello.cpp PowerBookG4:/llvm/test mycroft$ ls -l total 112 -rwxr-xr-x 1 mycroft admin 64 Jan 12 00:04 a.out -rwxr-xr-x 1 mycroft admin 47320 Jan 12 00:04 a.out.bc -rw-r--r-- 1 mycroft admin 95 Jan 12 00:00 hello.cpp PowerBookG4:/llvm/test mycroft$ ./a.out Segmentation fault

lattner commented 18 years ago

This is fixed in recent drops of llvm-gcc4.

-Chris

lattner commented 18 years ago

From your transcript, it looks like everything is working perfectly. To build .bc files with llvm-gcc4 use either:

llvm-gcc file.c -c -emit-llvm -o file.bc -O3

or: llvm-gcc file.c -S -emit-llvm -o file.ll -O3 llvm-as file.ll -o file.bc

-Chris

llvmbot commented 18 years ago

still the same: no bytecode. here's the gcc4 directory (it's in the path)

powerbookg4:/llvm/llvm-gcc/bin mycroft$ ls -l total 97864 -rwxr-xr-x 4 mycroft admin 5586284 Jun 22 07:25 c++ -rwxr-xr-x 1 mycroft admin 5585780 Jun 22 07:25 cpp -rwxr-xr-x 4 mycroft admin 5586284 Jun 22 07:25 g++ -rwxr-xr-x 3 mycroft admin 5584860 Jun 22 07:25 gcc -rwxr-xr-x 1 mycroft admin 15728 Jun 22 07:25 gccbug -rwxr-xr-x 1 mycroft admin 5367872 Jun 22 07:25 gcov lrwxr-xr-x 1 mycroft admin 3 Jun 27 09:58 llvm-c++ -> c++ lrwxr-xr-x 1 mycroft admin 3 Jun 27 09:58 llvm-cpp -> cpp lrwxr-xr-x 1 mycroft admin 3 Jun 27 09:58 llvm-g++ -> g++ lrwxr-xr-x 1 mycroft admin 3 Jun 27 09:58 llvm-gcc -> gcc lrwxr-xr-x 1 mycroft admin 6 Jun 27 09:58 llvm-gccbug -> gccbug lrwxr-xr-x 1 mycroft admin 4 Jun 27 09:58 llvm-gcov -> gcov -rwxr-xr-x 4 mycroft admin 5586284 Jun 22 07:25 powerpc-apple-darwin8.6.0-c++ -rwxr-xr-x 4 mycroft admin 5586284 Jun 22 07:25 powerpc-apple-darwin8.6.0-g++ -rwxr-xr-x 3 mycroft admin 5584860 Jun 22 07:25 powerpc-apple-darwin8.6.0-gcc -rwxr-xr-x 3 mycroft admin 5584860 Jun 22 07:25 powerpc-apple-darwin8.6.0-gcc-4.0.1

powerbookg4:/llvm/test mycroft$ ls hello.cpp test.c

powerbookg4:/llvm/test mycroft$ llvm-gcc hello.cpp /usr/bin/ld: Undefined symbols: std::ios_base::Init::Init() std::ios_base::Init::~Init() std::cout std::basic_ostream<char, std::char_traits >& std::operator<< <std::char_traits > (std::basic_ostream<char, std::char_traits >&, char const*) collect2: ld returned 1 exit status

powerbookg4:/llvm/test mycroft$ llvm-g++ hello.cpp powerbookg4:/llvm/test mycroft$ ls a.out hello.cpp test.c powerbookg4:/llvm/test mycroft$ ./a.out hello world

powerbookg4:/llvm/test mycroft$ llvm-gcc test.c -S -o - -emit-llvm -v Using built-in specs. Target: powerpc-apple-darwin8.6.0 Configured with: ../gccsrc/configure --prefix=/llvm/llvm-gcc -enable-llvm=/llvm/llvmobj --enable- languages=c,c++,objc,obj-c++ Thread model: posix gcc version 4.0.1 LLVM (Apple Computer, Inc. build 5400) /llvm/llvm-gcc/libexec/gcc/powerpc-apple-darwin8.6.0/4.0.1/cc1 -quiet -v -DDYNAMIC test.c - emit-llvm -fPIC -quiet -dumpbase test.c -auxbase-strip - -version -o - ignoring nonexistent directory "/llvm/llvm-gcc/lib/gcc/powerpc-apple-darwin8.6.0/4.0.1/../../../../ powerpc-apple-darwin8.6.0/include"

include "..." search starts here:

include <...> search starts here:

/usr/local/include /llvm/llvm-gcc/include /llvm/llvm-gcc/lib/gcc/powerpc-apple-darwin8.6.0/4.0.1/include /usr/include /System/Library/Frameworks /Library/Frameworks End of search list. GNU C version 4.0.1 LLVM (Apple Computer, Inc. build 5400) (powerpc-apple-darwin8.6.0) compiled by GNU C version 4.0.1 (Apple Computer, Inc. build 5341). GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072 Compiler executable checksum: 552bb8881767f70d3bc8f89513fbe650 ; ModuleID = 'test.c' target endian = big target pointersize = 32 target triple = "powerpc-apple-darwin8.6.0"

implementation ; Functions:

int %foo() { entry: %retval = alloca int, align 4 ; <int*> [#uses=1] "alloca point" = cast int 0 to int ; [#uses=0] br label %return

return: ; preds = %entry %retval = load int* %retval ; [#uses=1] ret int %retval }

if you are interested:

powerbookg4:/llvm/test mycroft$ llvm-gcc hello.cpp -S -o - -emit-llvm -v Using built-in specs. Target: powerpc-apple-darwin8.6.0 Configured with: ../gccsrc/configure --prefix=/llvm/llvm-gcc -enable-llvm=/llvm/llvmobj --enable- languages=c,c++,objc,obj-c++ Thread model: posix gcc version 4.0.1 LLVM (Apple Computer, Inc. build 5400) /llvm/llvm-gcc/libexec/gcc/powerpc-apple-darwin8.6.0/4.0.1/cc1plus -quiet -v -DDYNAMIC hello.cpp -emit-llvm -fPIC -quiet -dumpbase hello.cpp -auxbase-strip - -version -o - - D__private_extern__=extern ignoring nonexistent directory "/llvm/llvm-gcc/lib/gcc/powerpc-apple-darwin8.6.0/4.0.1/../../../../ powerpc-apple-darwin8.6.0/include"

include "..." search starts here:

include <...> search starts here:

/llvm/llvm-gcc/lib/gcc/powerpc-apple-darwin8.6.0/4.0.1/../../../../include/c++/4.0.1 /llvm/llvm-gcc/lib/gcc/powerpc-apple-darwin8.6.0/4.0.1/../../../../include/c++/4.0.1/powerpc- apple-darwin8.6.0 /llvm/llvm-gcc/lib/gcc/powerpc-apple-darwin8.6.0/4.0.1/../../../../include/c++/4.0.1/backward /usr/local/include /llvm/llvm-gcc/include /llvm/llvm-gcc/lib/gcc/powerpc-apple-darwin8.6.0/4.0.1/include /usr/include /System/Library/Frameworks /Library/Frameworks End of search list. GNU C++ version 4.0.1 LLVM (Apple Computer, Inc. build 5400) (powerpc-apple-darwin8.6.0) compiled by GNU C version 4.0.1 (Apple Computer, Inc. build 5341). GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072 Compiler executable checksum: 099f891e5e9b8ccebf1bf8512c58370d ; ModuleID = 'hello.cpp' target endian = big target pointersize = 32 target triple = "powerpc-apple-darwin8.6.0" "struct.std::basic_ios<char,std::char_traits >" = type { "struct.std::ios_base", "struct.std::basic_ostream<char,std::char_traits >", sbyte, uint, "struct.std::basic_streambuf<char,std::char_traits >", "struct.std::ctype", "struct.std::num_get<char,std::istreambuf_iterator<char, std::char_traits > >", "struct.std::num_get<char,std::istreambuf_iterator<char, std::char_traits > >" } "struct.std::basic_ostream<char,std::char_traits >" = type { int (...), "struct.std::basic_ios<char,std::char_traits >" } "struct.std::basic_streambuf<char,std::char_traits >" = type { int (...), sbyte, sbyte, sbyte, sbyte, sbyte, sbyte, "struct.std::locale" } "struct.std::ctype" = type { "struct.std::locale::facet", int, uint, int, int, uint*, sbyte, [256 x sbyte], [256 x sbyte], sbyte } "struct.std::ctype_base" = type { ubyte } "struct.std::ios_base" = type { int (...)*, int, int, uint, uint, uint, "struct.std::ios_base::_Callback_list", "struct.std::ios_base::_Words", [8 x "struct.std::ios_base::_Words"], int, "struct.std::ios_base::_Words", "struct.std::locale" } "struct.std::ios_base::Init" = type { ubyte } "struct.std::ios_base::_Callback_list" = type { "struct.std::ios_base::_Callback_list", void (uint, "struct.std::ios_base", int), int, int } "struct.std::ios_base::_Words" = type { sbyte, int } "struct.std::locale" = type { "struct.std::locale::_Impl" } "struct.std::locale::_Impl" = type { int, "struct.std::locale::facet", uint, "struct.std::locale::facet", sbyte } "struct.std::locale::facet" = type { int (...), int } "struct.std::num_get<char,std::istreambuf_iterator<char, std::char_traits > >" = type { "struct.std::locale::facet" } "struct.std::num_put<char,std::ostreambuf_iterator<char, std::char_traits > >" = type { "struct.std::locale::facet" } %_ZSt8ioinit = internal global "struct.std::ctype_base" zeroinitializer ; <"struct.std::ctype_base"> [#uses=2] %_ZSt4cout = external global "struct.std::basic_ostream<char,std::char_traits >" ; <"struct.std::basic_ostream<char,std::char_traits >"> [#uses=1] %str = internal constant [13 x sbyte] c"hello world\0A\00" ; <[13 x sbyte]> [#uses=1] %llvm.global_ctors = appending global [1 x { int, void () }] [ { int, void () } { int 65535, void () % _GLOBAL__I_main } ] ; <[1 x { int, void () }]> [#uses=0] %llvm.global_dtors = appending global [1 x { int, void () }] [ { int, void () } { int 65535, void ()* % _GLOBALD_main } ] ; <[1 x { int, void () }]> [#uses=0]

implementation ; Functions:

int %main() { entry: %retval = alloca int, align 4 ; <int> [#uses=3] %tmp = alloca int, align 4 ; <int> [#uses=4] "alloca point" = cast int 0 to int ; [#uses=0] %tmp = getelementptr [13 x sbyte] %str, int 0, uint 0 ; <sbyte> [#uses=1] %tmp = call "struct.std::basic_ostream<char,std::char_traits >" % _ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc ( "struct.std::basic_ostream<char,std::char_traits >" %_ZSt4cout, sbyte %tmp ) ; <"struct.std::basic_ostream<char,std::char_traits >"> [#uses=0] store int 0, int %tmp %tmp = load int %tmp ; [#uses=1] store int %tmp, int %retval br label %return ; No predecessors! store int 0, int %tmp %tmp1 = load int %tmp ; [#uses=1] store int %tmp1, int %retval br label %return

return: ; preds = %0, %entry %retval = load int* %retval ; [#uses=1] ret int %retval }

internal void %_GLOBAL__I_main() section "TEXT,StaticInit,regular,pure_instructions" { entry: call void %_Z41__static_initialization_and_destruction_0ii( int 1, int 65535 ) br label %return

return: ; preds = %entry ret void }

internal void %_GLOBAL__D_main() section "TEXT,StaticInit,regular,pure_instructions" { entry: call void %_Z41__static_initialization_and_destruction_0ii( int 0, int 65535 ) br label %return

return: ; preds = %entry ret void }

declare "struct.std::basic_ostream<char,std::char_traits >" % _ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc ("struct.std::basic_ostream<char,std::char_traits >", sbyte*)

internal void %_Z41static_initialization_and_destruction_0ii(int %initialize_p, int %priority) section "TEXT,StaticInit,regular,pure_instructions" { entry: %initialize_p_addr = alloca int ; <int*> [#uses=3] %priority_addr = alloca int ; <int*> [#uses=3] "alloca point" = cast int 0 to int ; [#uses=0] store int %initialize_p, int %initialize_p_addr store int %priority, int %__priority_addr %tmp = load int* %__priority_addr ; [#uses=1] %tmp = seteq int %tmp, 65535 ; [#uses=2] %tmp = cast bool %tmp to uint ; [#uses=0] br bool %tmp, label %cond_true, label %cond_false4

cond_true: ; preds = %entry %tmp1 = load int* %__initialize_p_addr ; [#uses=1] %tmp2 = seteq int %tmp1, 1 ; [#uses=2] %tmp2 = cast bool %tmp2 to uint ; [#uses=0] br bool %tmp2, label %cond_true3, label %cond_false

cond_true3: ; preds = %cond_true call void %_ZNSt8ios_base4InitC1Ev( "struct.std::ctype_base"* %_ZSt8__ioinit ) br label %cond_next

cond_false: ; preds = %cond_true br label %cond_next

cond_next: ; preds = %cond_false, %cond_true3 br label %cond_next5

cond_false4: ; preds = %entry br label %cond_next5

cond_next5: ; preds = %cond_false4, %cond_next %tmp6 = load int* %__priority_addr ; [#uses=1] %tmp7 = seteq int %tmp6, 65535 ; [#uses=2] %tmp7 = cast bool %tmp7 to uint ; [#uses=0] br bool %tmp7, label %cond_true8, label %cond_false14

cond_true8: ; preds = %cond_next5 %tmp9 = load int* %__initialize_p_addr ; [#uses=1] %tmp10 = seteq int %tmp9, 0 ; [#uses=2] %tmp10 = cast bool %tmp10 to uint ; [#uses=0] br bool %tmp10, label %cond_true11, label %cond_false12

cond_true11: ; preds = %cond_true8 call void %_ZNSt8ios_base4InitD1Ev( "struct.std::ctype_base"* %_ZSt8__ioinit ) br label %cond_next13

cond_false12: ; preds = %cond_true8 br label %cond_next13

cond_next13: ; preds = %cond_false12, %cond_true11 br label %cond_next15

cond_false14: ; preds = %cond_next5 br label %cond_next15

cond_next15: ; preds = %cond_false14, %cond_next13 br label %return

return: ; preds = %cond_next15 ret void }

declare void %_ZNSt8ios_base4InitC1Ev("struct.std::ctype_base"*)

declare void %_ZNSt8ios_base4InitD1Ev("struct.std::ctype_base"*)

lattner commented 18 years ago

I'm not that stupid... ;) It's the first thing I checked.

No insult intended. Often the most obvious cause is the right one :)

If I had to guess, I would guess it's because you renamed the executables. Please try naming them "gcc" and using a symlink from llvm-gcc to gcc. GCC is funny about how it finds parts like "cc1".

If that doesn't help, please try a simple example, e.g.:

echo "int foo(){}" > test.c llvm-gcc test.c -S -o - -emit-llvm -v

and include the output.

Thanks,

-Chris

llvmbot commented 18 years ago

I forgot to mention: this is the version I recompiled, however the binaries you distribute have a similar version dump. (the filename is originally gcc and not llvm-gcc, but I renamed it)

llvmbot commented 18 years ago

I'm not that stupid... ;) It's the first thing I checked.

powerbookg4:/llvm/llvm-gcc4/bin mycroft$ ./llvm-gcc -v Using built-in specs. Target: powerpc-apple-darwin8.6.0 Configured with: ../gccsrc/configure --prefix=/llvm/llvm-gcc -enable-llvm=/llvm/llvmobj --enable- languages=c,c++,objc,obj-c++ Thread model: posix gcc version 4.0.1 LLVM (Apple Computer, Inc. build 5400)

lattner commented 18 years ago

run llvm-gcc4 -v. Does it print the string "LLVM"? If not, you have a native GCC, not an LLVM GCC.

llvmbot commented 18 years ago

recompiling the gcc4 frontend partially fixes: the new gcc4 does compile the "hello world" c++ sample, it will NOT produce bytecode, but at least the binary file works.

llvmbot commented 18 years ago

Hi,

the bug reappeared with llvm 1.7, and it's much worse:

I'm working on a powerbook g4, with 10.4.6 I use the binary version of llvm-gcc v3.4 from the download section on the website. I configure llvm with "--enable-optimized --enable-jit"

llvm compiles fine, but now BOTH llvm-gcc and llvm-g++ produce an identical bytecode (and segmentation fault...)

if I use llvm-gcc4 (powerpc) binaries I get a bizarre result: first, in the archive, the executables don't have the llvm- prefix. If I rename them and compile llvm as above, when I compile something I don't get bytecode, but a native executable, which however does not run (it says: invalid CPU...)

please, tell me what's happening (hint: please, provide a BINARY setup...)

llvmbot commented 18 years ago

I'm observing the same behaviour on i386 with LLVM 1.7. Whenever I compile a C++ code that tries to write to cout I get a segmentation fault as soon as I run it. I tried to compile the frontend from SVN sources but failed so far.

Anyway, since it appears to work fine for most other users, I expect that I must have made a simple mistake during my LLVM setup. Any idea what this could be?

lattner commented 18 years ago

ok, sounds fair.

-Chris

llvmbot commented 18 years ago

thanks, I've only a 56k dial-up connection here, so I won't try to checkout llvm-gcc... If you prefer, close this bug. I'll open a new one if necessary (I hope not to! :)

lattner commented 18 years ago

Were you able to reproduce this problem with a CVS llvm-gcc? If you can't check, I'm going to have to close this as "works for me".

Thanks,

-Chris

lattner commented 18 years ago

when you link with 'gcc', it uses the system libstdc++ library. When you link with g++, it uses the LLVM version.

BTW, note that compiling a program like that invokes the JIT when you run a.out.

-Chris

llvmbot commented 18 years ago

yes, it's the released 1.6 binary package downloaded from website. I run fixheader with MacOS 10.4.3 but now it's running in 10.4.4

I'll have some more check and let you know, but why gcc works when g++ does not?

lattner commented 18 years ago

This implies that llvm-g++'s libstdc++ is corrupt. Where did you get your llvmgcc? Is it llvm-gcc 1.6 or something earlier? If it is 1.6, please try updating it to the latest CVS version: it works fine for me on OS X 10.4.4.

-Chris