Hi Nick:<div><br></div><div>Thanks for the response.</div><div><br></div><div>You are right about sinf. </div><div>I changed the constant to sscanf(argv[1], ...), and it gave me back 0 again.</div><div>I also compared the disassembled code, and saw it was indeed the case.</div>
<div><br></div><div>The primary reason I use interpreter is because I wish to do runtime checking on memory access pattern in multithreaded C programs. for example, if thread one is in the critical section, I wish to know if there are any other threads accessing the variables in the critical section without first acquiring a lock. I can do it with native code, but I will have to insert memory check function calls before all memory access instructions (load/store...), which is painful. But if I can run the program in interpreter, I can intercept the load and store instruction in the interpreter, and do the memory check there, which is completely transparent to the user program.</div>
<div><br></div><div>Do you have any suggestions on how LLVM may help in this case?</div><div><br></div><div><br></div><div>Thanks</div><div>Xu</div><div><br><br><div class="gmail_quote">On Wed, Nov 18, 2009 at 12:40 AM, Nick Lewycky <span dir="ltr"><<a href="mailto:nicholas@mxc.ca">nicholas@mxc.ca</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;"><div class="im">Xu Yang wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
Hi Nick:<br>
<br>
The first problem have been solved by calling llvm-ld:<br>
<br>
$ llvm-ld -o hellosin.llvm hellosin.bc -lm<br>
$ lli -force-interpreter=true -load=/usr/lib/libm.so hellosin.llvm.bc<br>
hello sin:       0.50<br>
</blockquote>
<br></div>
Only because the optimizer saw sin(constant) and folded it away. The entire program became 'print constant string'. There is certainly a bug calling sinf() from the interpreter but I don't know what it is yet.<div class="im">
<br>
<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
The pthread problem remains after llvm-ld:<br>
<br>
$ lli -force-interpreter=true -load=/lib/libpthread.so.0 phello.llvm.bc<br>
0   lli 0x08796bf8<br>
Segmentation fault<br>
</blockquote>
<br></div>
I don't expect this to work at all. Unlike the JIT, I don't know of any work having been done to make the interpreter thread-safe.<br>
<br>
By the way, the interpreter can't support many more things, such as qsort() or any method which expects to call a function pointer. Is there a reason you need to use the interpreter? It's been largely undermaintained because it's pretty much useless when you've got a working JIT.<br>

<br>
Nick<br>
<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div class="im">
For those who are getting "invalid ELF header" error when calling<br>
llvm-ld with -lpthread,<br>
please change the load module from /usr/lib/pthread.a to<br>
/lib/libpthread.so.0.<br>
<br>
Thanks<br>
Xu<br>
<br>
On Wed, Nov 18, 2009 at 12:02 AM, Xu Yang <<a href="mailto:yangx2000@gmail.com" target="_blank">yangx2000@gmail.com</a><br></div><div><div></div><div class="h5">
<mailto:<a href="mailto:yangx2000@gmail.com" target="_blank">yangx2000@gmail.com</a>>> wrote:<br>
<br>
    Hi Nick:<br>
<br>
    Thanks for pointing me to libffi.<br>
    Recompile LLVM with libffi does solve the problem of printf.<br>
    But it still has other problems:<br>
    1) sinf() returns 0 in the interpreter, but returns correct value in<br>
    JIT (see hellosin.c)<br>
    2) calling pthread_create cause lli to crash in the interpreter<br>
    mode, but no problem in JIT (see phello.c).<br>
<br>
    My questions are:<br>
    i) can I call any arbitrary external function in the interpreter?<br>
    ii) how do I specify the dynamic library (where the external<br>
    function is implemented), when I call the interpreter?<br>
<br>
    Thanks<br>
    Xu<br>
<br>
    hellosin.c<br>
    =======================<br>
    #include <stdio.h><br>
    #include <math.h><br>
<br>
<br>
    int main(int argc, char** argv){<br>
       float f = sinf(0.5235988f);<br>
       printf("hello sin: %10.2f\n", f);<br>
       return 0;<br>
    }<br>
    ========================<br>
    $ llvm-gcc -o hellosin.bc -emit-llvm -c hellosin.c<br>
    $ lli -force-interpreter=true hellosin.bc<br>
    hello sin:       0.00<br>
    $ lli hellosin.bc<br>
    hello sin:       0.50<br>
<br>
    phello.c<br>
    ========================<br>
    #include <pthread.h><br>
    #include <stdio.h><br>
    #define NUM_THREADS     4<br>
<br>
    void *PrintHello(void *threadid)<br>
    {<br>
        long tid;<br>
        tid = (long)threadid;<br>
        printf("Hello from %ld.\n", tid);<br>
        pthread_exit(NULL);<br>
    }<br>
<br>
    int main (int argc, char *argv[])<br>
    {<br>
        pthread_t threads[NUM_THREADS];<br>
        int ret;<br>
        long t;<br>
        for(t=0; t<NUM_THREADS; t++){<br>
    #ifdef DEBUG<br>
           printf("creating thread %ld\n", t);<br>
    #endif<br>
           ret = pthread_create(&threads[t], NULL, PrintHello, (void *)t);<br>
           if (ret){<br>
              printf("pthread_create ERROR: %d\n", ret);<br>
              return(-1);<br>
           }<br>
        }<br>
        pthread_exit(NULL);<br>
    }<br>
    ========================<br>
    $ llvm-gcc -o phello.bc -emit-llvm -c phello.c<br>
    $ lli -force-interpreter=true phello.bc<br>
    0   lli 0x08796bf8<br>
    Segmentation fault<br>
    $ lli phello.bc<br>
    Hello from 0.<br>
    Hello from 1.<br>
    Hello from 2.<br>
    Hello from 3.<br>
<br>
<br>
    On Tue, Nov 17, 2009 at 2:52 AM, Nick Lewycky <<a href="mailto:nicholas@mxc.ca" target="_blank">nicholas@mxc.ca</a><br></div></div><div class="im">
    <mailto:<a href="mailto:nicholas@mxc.ca" target="_blank">nicholas@mxc.ca</a>>> wrote:<br>
<br>
        Timo Juhani Lindfors wrote:<br>
<br></div>
            Nick Lewycky<<a href="mailto:nicholas@mxc.ca" target="_blank">nicholas@mxc.ca</a> <mailto:<a href="mailto:nicholas@mxc.ca" target="_blank">nicholas@mxc.ca</a>>>  writes:<div class="im"><br>
<br>
                The interpreter uses libffi to make external function<br>
                calls. However, it<br>
                needs to be detected at LLVM's compile time. If you're<br>
                using the<br>
                released packages, we disabled that because we were<br>
                worried about users<br>
                who don't have libffi installed.<br>
<br>
<br>
            This seems to be quite a common problem (I too hit it once,<br>
            thought it<br>
            was a bug and reported it at<br>
            <a href="http://llvm.org/bugs/show_bug.cgi?id=5466" target="_blank">http://llvm.org/bugs/show_bug.cgi?id=5466</a>) how about making<br>
            lli inform<br>
            the user that LLVM was built without FFI support?<br>
<br>
<br>
        Thanks for the reminder. I recall looking at the patch but I<br>
        didn't apply it at the time because I couldn't figure out why<br>
        the code above it used errs() in one case and llvm_report_error<br>
        in another.<br>
<br>
        Nick<br>
<br>
<br>
<br>
</div></blockquote>
<br>
</blockquote></div><br></div>