Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Questions for compiling to LLVM

I've been playing around with LLVM hoping to learn how to use it.

However, my mind is boggled by the level of complexity of the interface.

Take for example their Fibonacci function

int fib(int x) {
    if(x<=2) 
        return 1;
    return fib(x-1) + fib(x-2);
   }

To get this to output LLVM IR, it takes 61 lines of code!!!

They also include BrainFuck which is known for having the smallest compiler (200 bytes). Unfortunately, with LLVM, it is over 600 lines (18 kb).

Is this the norm for compiler backends? So far it seems like it would be far easier to do an assembly or C backend.

like image 766
Unknown Avatar asked Apr 09 '09 06:04

Unknown


People also ask

How long does LLVM take to compile?

The website displays data from that repository. The server this runs on only has 2 cores, so a full LLVM build can take more than two hours. For smaller changes, building LLVM from ccache and compiling the benchmarks takes about 20 minutes.

How does the LLVM compiler work?

How A LLVM Compiler Works. On the front end, the LLVM compiler infrastructure uses clang — a compiler for programming languages C, C++ and CUDA — to turn source code into an interim format. Then the LLVM clang code generator on the back end turns the interim format into final machine code.

Is LLVM better than GCC?

Advantages of GCC GCC supports more traditional languages than Clang and LLVM, such as Ada, Fortran, and Go. GCC supports more less-popular architectures, and supported RISC-V earlier than Clang and LLVM. GCC supports more language extensions and more assembly language features than Clang and LLVM.


1 Answers

The problem lies with C++ and not LLVM.

Use a language designed for metaprogramming, like OCaml, and your compiler will be vastly smaller. For example, this OCaml Journal article describes an 87-line LLVM-based Brainfuck compiler, this mailing list post describes complete programming language implementation including parser that can compile the Fibonacci function (amongst other programs) and the whole compiler is under 100 lines of OCaml code using LLVM, and HLVM is a high-level virtual machine with multicore-capable garbage collection in under 2,000 lines of OCaml code using LLVM.

like image 86
J D Avatar answered Sep 29 '22 11:09

J D