Convert C variable to LLVM IR Variable

Hi everyone,

  I am doing some work with LLVM IR, I need to use LLVM IR to do operation
on C variables.

Code emission is done by LLVM JIT.

That variable is C thread local , for example

__thread int* gvar;

I think some methods to convert that variable to LLVM IR,

(1) use external function

I know LLVM IR is able to call an external function, so I can write codes
that look like:

int* load()
{
  return gvar;
}

%x = call load(); // LLVM IR

so gvar is converted to %x

This method takes a lot of work and slow down the performance.

Method 2

use LLVM InlineAsm to load that gvar
so It may look like
%x = call InlineAsm(... );

but I am not really sure how to write LLVM InlineAsm.

The reason why I take such indirect method is that as I know, JIT is not
able to

do with LLVM GlobalVariable with ThreadLocal type.

I would be grateful to you for any idea on handling this problem

Have A Nice Day

Chia Lun Liu

Method 2

use LLVM InlineAsm to load that gvar
so It may look like
%x = call InlineAsm(... );

but I am not really sure how to write LLVM InlineAsm.

  Are you looking for this?
  http://llvm.org/docs/LangRef.html#inline-assembler-expressions

HTH,
chenwj

Hi Chia Lun Liu,

> The reason why I take such indirect method is that as I know, JIT is not

able to

do with LLVM GlobalVariable with ThreadLocal type.

did you try mcjit? Run lli with -use-mcjit

Ciao, Duncan.

Hi Wei-Ren Chen,
      Thanks for your information, I also find a method to write LLVM
InlineAsm

First, write a c program which contain gcc asm ( I know how to write gcc
asm )

then use clang -S -emit-llvm $prog

next do llc -march=cpp $prog.s

You will see how to write LLVM InlineAsm in the output file by llc

Chia Lun Liu

Hi Ciao, Duncan

   I had tested your method before but fail. I did it today but fail again.

I write a simple c program

#include<stdio.h>

__thread int g[128];

int main()
{
        int i;
        for(i = 0 ; i< 128 ; i++)
        { g[i] = 2*i;
          printf("%d\n",g[i]);
        }
        return 0;
}