JIT and array pointers

I'm trying to pass an array pointer to my run-time generated module, and
after a long time of searching for the answer, the only thing I got was
a headache. Basically I have a few arrays (few megabytes which will
sometimes be accessed as 8 / 16 / 32 / 64 bit binary / fp values), that
will be modified by both the generated module, and my main c program, so
I would like to put those into global variables. I also tried passing it
as a function argument (with argument type PointerType::get
(Type::UIntTy)), and failed miserably. I feel lost. Right now I got only
a small module with some simple operations (big thanks to the examples,
they really help a lot). Maybe its because I'm not a c++ guy, but I
really want to use llvm, especially because of it's nice job at
optimizing code. And I only need a few basic building blocks to get
things started.


There are many possible ways to do this, can you be a bit more specific about what you're trying to do?


Here is a basic example:

There are many possible ways to do this, can you be a bit more specific
about what you're trying to do?

Here is a basic example:

Ah, ok, I see what you're trying to do. Below is some *pseudo* code for the basic idea:

unsigned int buff[4096];

int main (void)
buff[0] = 1;

// compile and execute code that will change buff[0] to 2
Value *c0 = ConstantUInt::get (Type::UIntTy, 0);
Value *c2 = ConstantUInt::get (Type::UIntTy, 2);

Module *m = new Module ("test_module");

Here, create an LLVM GlobalVariable 'BuffGV' of the appropriate type, and insert it into m:

GlobalVariable *BuffGV = new GlobalVariable([4096 x uint], false, GlobalVariable::ExternalLinkage, 0, "Buff", m);

Function *f = m->getOrInsertFunction ("test_function", Type::VoidTy,
BasicBlock *b = new BasicBlock ("entry_block", f);

// Here is the problem.
// I need to get the buff pointer into variable, which I can
// pass to GetElementPtrInst
// Value *pbuff = ??
// -> %pbuff = external global [4096 x uint]

    pbuff = BuffGV;

Value *ptr = new GetElementPtrInst (pbuff, c0, c0, "ptr", b);
// -> %ptr = getelementptr [4096 x uint]* %buff, int 0, int 0
new StoreInst (c2, ptr, b);
// -> store uint 2, uint* %ptr
new ReturnInst (b);

ExistingModuleProvider *mp = new ExistingModuleProvider (m);
ExecutionEngine *ee = ExecutionEngine::create (mp, false);

Tell the EE that buffGV -> buff:

ee->addGlobalMapping(BuffGV, buff);

Then you should be good to go!


cout << "constructed module:\n\n" << *m << "\n\n";

vector <GenericValue> args;
GenericValue ret = ee->runFunction (f, args);

printf ("buff[0] is now: %d\n", buff[0]);
return 0;

I've been trying to do it with GlobalVariable. It seems like a
completely basic thing, but I just can't get it to work.

LLVM Developers mailing list
LLVMdev@cs.uiuc.edu http://llvm.cs.uiuc.edu


Thanks for taking the time to answer. It's working now.