LLVM Basic Program Compilation

I am just getting started with llvm.

Here’s code I am trying to compile:


#include <stdio.h>
#include "llvm/IR/LLVMContext.h"
#include "llvm/IR/Module.h"
#include "llvm/IR/IRBuilder.h"

int main()
{
  llvm::LLVMContext& context = llvm::getGlobalContext();
  llvm::Module* module = new llvm::Module("top", context);
  llvm::IRBuilder<> builder(context); 

  module->dump( );
}

when i compile with :

What version of LLVM are you using? llvm-gcc was deprecated a long time ago, and while it doesn’t directly address your question, I suggest trying with clang(++).

  • Chris

Thank you Chris.

I am using llvm-3.4.2. I am facing the same issue when using clang++ as well.

$ clang++ try.cpp -D__STDC_CONSTANT_MACROS -D__STDC_LIMIT_MACROS llvm-config --cxxflags --ldflags --libs -S -emit-llvm

gives me errors

$clang++ try.cpp -D__STDC_CONSTANT_MACROS -D__STDC_LIMIT_MACROS -S -emit-llvm

$ lli try.s

gives me Program used external function ‘_ZN4llvm16getGlobalContextEv’ which could not be resolved!

Thank you

Hi Prakash,

The way you're trying to use LLVM seems like a very odd way to start.

The natural beginning move would be to write a C++ program that
produced a simple module and then try to execute that simple module
(maybe with lli).

But what you're doing is creating an (extremely complex!) module
(try.s) that, when executed will use LLVM again to create a much
simpler module. Rather more self-referential than I'd expect (and much
more difficult to get right, as you're discovering).

The way I'd expect you to be testing that program is:
$ clang++ `llvm-config --cxx-flags --ldflags --libs` try.cpp -otry
$ ./try 2> mymodule.ll
$ lli mymodule.ll

(You'll still get an error, because you need to define a @main
function to be able to execute a module, but it's a much simpler
error).

gives me Program used external function '_ZN4llvm16getGlobalContextEv' which
could not be resolved!

I'll say a bit about this error anyway. The "try.s" module produced by
your command has many dangling references to all the functions it
refers to from the LLVM libraries. It's the equivalent of a plain
object file ("clang -c xyz.c") and hasn't been linked at all, so you
shouldn't expect to be able to execute it directly (without more
work).

Cheers.

Tim.

Thanks a lot Tim.

I modified my code add the main function :

#include <stdio.h>
#include “llvm/IR/LLVMContext.h”
#include “llvm/IR/Module.h”
#include “llvm/IR/IRBuilder.h”
#include “llvm/IR/Function.h”

int main()
{
llvm::LLVMContext& context = llvm::getGlobalContext();
llvm::Module* module = new llvm::Module(“top”, context);
llvm::IRBuilder<> builder(context);
llvm::FunctionType *funcType =
llvm::FunctionType::get(builder.getInt32Ty(), false);
llvm::Function *mainFunc =llvm::Function::Create(funcType, llvm::Function::ExternalLinkage, “main”, module);
module->dump( );
}

and when i compile with the commands you specified i get the following error:

Undefined symbols for architecture x86_64:
“llvm::FunctionType::get(llvm::Type*, bool)”, referenced from:
_main in try-nnbqRa.o
“llvm::getGlobalContext()”, referenced from:
_main in try-nnbqRa.o
“llvm::llvm_unreachable_internal(char const*, char const*, unsigned int)”, referenced from:
llvm::User::operator delete(void*, unsigned int) in try-nnbqRa.o
“llvm::Type::getInt32Ty(llvm::LLVMContext&)”, referenced from:
llvm::IRBuilderBase::getInt32Ty() in try-nnbqRa.o
“llvm::User::operator new(unsigned long, unsigned int)”, referenced from:
llvm::Function::Create(llvm::FunctionType*, llvm::GlobalValue::LinkageTypes, llvm::Twine const&, llvm::Module*) in try-nnbqRa.o
“llvm::Module::Module(llvm::StringRef, llvm::LLVMContext&)”, referenced from:
_main in try-nnbqRa.o
“llvm::Function::Function(llvm::FunctionType*, llvm::GlobalValue::LinkageTypes, llvm::Twine const&, llvm::Module*)”, referenced from:
llvm::Function::Create(llvm::FunctionType*, llvm::GlobalValue::LinkageTypes, llvm::Twine const&, llvm::Module*) in try-nnbqRa.o
“llvm::Module::dump() const”, referenced from:
_main in try-nnbqRa.o
ld: symbol(s) not found for architecture x86_64

As you mentioned try.s is unlinked, Can you please tell me how i will link it.

Basically I am trying to write a simple interpreter for a language which has a while loop and translates the code to llvm IR. I am writing the interpreter in C.

Thanks a lot

I modified my code add the main function :

Well, that will only declare @main, I believe. You need to add an
actual function body to be able to execute it, but that problem can
wait until later.

and when i compile with the commands you specified i get the following
error:

I'm afraid I can't explain that. I can only reproduce the same results
if I omit the "--libs" flag from llvm-config, so it looks like your
LLVM installation might be broken in some way.

As you mentioned try.s is unlinked, Can you please tell me how i will link
it.

You don't need to for what you're doing (not yet a while anyway). That
would only be relevant if you actually meant to do that complicated
self-referential compilation you started with, but it sounds like that
was a mistake.

Cheers.

Tim.

Have you seen the Kaleidoscope tutorial on llvm? It is a parser to llvm IR of a toy language that is then JIT’d. Sounds like it might be similar to your goals.

http://llvm.org/docs/tutorial/LangImpl1.html

Also, Tim is right about your undefined symbols. Something might be wrong with your build or you omitted the libs from your compile.

  • Chris