7

I am a newbie to LLVM and try to generate a human readable .ll file on Linux. I installed llvm-gcc but as I see it can generate only assembly code (-S option). Is there any way to get something like what is generated by llvm online compiler?

That's what I get with -S -emit-llvm on Linux:

    .file   "hello.c"

    .ident  "GCC: (Ubuntu/Linaro 4.5.1-7ubuntu2) 4.5.1 LLVM: "

    .text
    .globl  main
    .align  16, 0x90
    .type   main,@function
main:
    pushl   %ebp
    movl    %esp, %ebp
    subl    $8, %esp
    movl    $.L.str, 4(%esp)
    movl    $1, (%esp)
    call    __printf_chk
    xorl    %eax, %eax
    addl    $8, %esp
    popl    %ebp
    ret
.Ltmp0:
    .size   main, .Ltmp0-main

    .type   .L.str,@object
    .section    .rodata.str1.1,"aMS",@progbits,1
.L.str:
    .asciz   "hello world\n"
    .size   .L.str, 13

    .section    .note.GNU-stack,"",@progbits

That's what I am trying to get:

; ModuleID = '/tmp/webcompile/_7829_0.bc'
target datalayout = "e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f32:32:32-f64:64:64-v64:64:64-v128:128:128-a0:0:64-s0:64:64-f80:128:128-n8:16:32:64"
target triple = "x86_64-linux-gnu"

@.str = private constant [12 x i8] c"hello world\00", align 1 ; <[12 x i8]*> [#uses=1]

define i32 @main() nounwind {
entry:
  %0 = tail call i32 @puts(i8* getelementptr inbounds ([12 x i8]* @.str, i64 0, i64 0)) nounwind ; <i32> [#uses=0]
  ret i32 0
}

declare i32 @puts(i8* nocapture) nounwind

On windows I successfully get this file with the same command: llvm-gcc -S -emit-llvm hello.c.

Nutel
  • 2,244
  • 2
  • 27
  • 50

5 Answers5

7

Something is horrible broken in ubuntu packaging of llvm-gcc. llvm-gcc's version is 4.2.1, but here we're seeing 4.5. Please report Ubuntu bug.

Anton Korobeynikov
  • 9,074
  • 25
  • 28
4

Try:

llvm-gcc -c -emit-llvm source.cpp

use -c instead of -S.

if it does not work, use:

clang -c -emit-llvm source.cpp

You can build llvm either from Ubuntu software center or download llvm-3.0-src and clang-src to compile them yourself.

Edgar Rokjān
  • 17,245
  • 4
  • 40
  • 67
bluesea
  • 429
  • 4
  • 9
2

From the LLVM tutorial:

$ llvm-gcc -O3 -emit-llvm hello.c -c -o hello.bc

Will compile the source hello.c into bytecode file hello.bc.

Then use the llvm-dis utility to take a look at the LLVM assembly code:

$ llvm-dis < hello.bc | less
amphetamachine
  • 27,620
  • 12
  • 60
  • 72
  • 1
    I tried but get "llvm-dis: Invalid bitcode signature". I took hello.c from tutorial, llvm-gcc compiles the code without any issues – Nutel Mar 22 '11 at 00:27
  • 1
    Seems to be a bug https://bugs.launchpad.net/ubuntu/+source/dragonegg/+bug/712199 – Nutel Mar 22 '11 at 01:26
0

Do the steps on http://clang.llvm.org/get_started.html this will install llvm and clang from svn. So you'll build from source

but with minor difference: ../llvm/configure --prefix=/usr/local --enable-optimized make -j4 make install (to install tools under /usr/local)

(install release+asserts instead of debug+asserts or go with ../llvm/configure for debug+asserts I have an i5 so I used make -j4 change as you wish)

use clang not llvm-gcc. You can then directly generate an .ll by: clang -S -emit-llvm file.cpp -o file.ll

and clang -c -emit-llvm file.cpp -o file.bc

if you like to get and .s do the following: llc file.bc

this is the cleanest way for the latest version of llvm on ubuntu


Same thing happens with llvm-gcc-4.6 which is also dragonEgg version I suppose.

I'm getting llvm-gcc potential incompatible plug-in version

After this point I get exactly the same error with llvm-dis:

"llvm-dis: Invalid bitcode signature".

chrki
  • 6,143
  • 6
  • 35
  • 55
drunk teapot
  • 303
  • 2
  • 13
  • You know that the `configure` (autoconf) script has been deleted from LLVM months ago? We rely on CMake now. – Joky Oct 15 '16 at 23:29
  • Yes, my reply was 3 years ago though. It was like that at that time. – drunk teapot Oct 16 '16 at 17:28
  • Missed this, sorry. for some reason I ended up here while looking at the most recent stuff (or that's what I thought at least...) – Joky Oct 17 '16 at 03:54
0

If you want to play with LLVM, build it from source: http://clang.llvm.org/get_started.html

Otherwise, to quick start, try to install clang on Ubuntu. This is the C/C++ compiler built on top of LLVM. You will be able to generate LLVM IR from clang directly.

llvm-gcc seems to be based on dragon-egg, which is no longer supported in LLVM AFAIK.

Joky
  • 1,608
  • 11
  • 15