<div dir="ltr">Thank you Lang. You are right. The structure ExprState is allocated in every run, I should not hard code the pointer in IR. I tried to google an example using IRBuilder to allocate structure and access its element, but couldn't find any. Could you give me a simple example on it? Once I create a new ExprState and pass it to <span style="color:rgb(80,0,80);font-family:'courier new',monospace;font-size:13px">@IrExprGetValue1</span> I think the problem should be solved. </div><div class="gmail_extra"><br><div class="gmail_quote">On Fri, Sep 12, 2014 at 10:57 AM, Lang Hames <span dir="ltr"><<a href="mailto:lhames@gmail.com" target="_blank">lhames@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr">Hi Cheng,<div><br></div><div>Thanks for attaching the object file and bit code - it's very helpful.</div><div><br></div><div>I think the problem is that there is a pointer address hard-coded into your IR. If the data that pointer references is stored at a different address on your second run (which is highly likely) you get the failure that you're seeing.</div><div><br></div><div>In your definition of JittedOpExpr, the first call instruction (to IRExprGetValue1) is:</div><span class=""><div><br></div><div><font face="courier new, monospace"><span style="font-size:13px">%lhs = call i64 @IrExprGetValue1(%struct.</span><span style="font-size:13px">ExprState* inttoptr (i64 </span><span style="font-size:13px">140715076067816</span><span style="font-size:13px"> to %struct.ExprState*), %struct.ExprContext* %econtext, i8* %isNull, i32* %isDone)</span><br></font></div><div><span style="font-family:arial,sans-serif;font-size:13px"><br></span></div></span>Note the hard coded pointer address 140715076067816 (0x7ffac81fa1e8). That should probably have been a reference to the %expr argument of JittedOpExpr. If you change this call to use %expr rather than the hard-coded address I think it will fix your issue.<div><br></div><div>I hope this helps.<br><div><br></div><div>Regards,</div><div>Lang.</div><div><br></div><div><br></div></div></div><div class="HOEnZb"><div class="h5"><div class="gmail_extra"><br><div class="gmail_quote">On Thu, Sep 11, 2014 at 2:45 PM, Cheng Zhu <span dir="ltr"><<a href="mailto:chengzhu@gmail.com" target="_blank">chengzhu@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><div>Thank you Lang. I attached the ELF object file here for your reference. Here is the IR dump of JittedOpExpr LLVM function. IrExprGetValue1 LLVM function calls to external function expr->evalfunc(expr, econtext, isNull, isDone); which should be pointed by 0x7fe4801fa1f8. However, only the first time MCJIT generated object point to expr->evalfunc but second time when program load from object cache does not. In the second time, before program load object cache, brand new llvm::Module and llvm::ExecutionEngine are created. IrExprGetValue is saved in file as IR format and will be loaded into module every run. JittedOpExpr and other LLVM function are created and associated with the new Module. </div><div> </div><div>; Function Attrs: uwtable<br>define i64 @IrExprGetValue1(%struct.ExprState* %expr, %struct.ExprContext* %econtext, i8* %isNull, i32* %isDone) #0 {<br>entry:<br> %expr.addr = alloca %struct.ExprState*, align 8<br> %econtext.addr = alloca %struct.ExprContext*, align 8<br> %isNull.addr = alloca i8*, align 8<br> %isDone.addr = alloca i32*, align 8<br> store %struct.ExprState* %expr, %struct.ExprState** %expr.addr, align 8<br> store %struct.ExprContext* %econtext, %struct.ExprContext** %econtext.addr, align 8<br> store i8* %isNull, i8** %isNull.addr, align 8<br> store i32* %isDone, i32** %isDone.addr, align 8<br> %0 = load %struct.ExprState** %expr.addr, align 8<br> %evalfunc = getelementptr inbounds %struct.ExprState* %0, i32 0, i32 2<br> %evalfunc1 = bitcast {}** %evalfunc to i64 (%struct.ExprState*, %struct.ExprContext*, i8*, i32*)**<br> %1 = load i64 (%struct.ExprState*, %struct.ExprContext*, i8*, i32*)** %evalfunc1, align 8<br> %2 = load %struct.ExprState** %expr.addr, align 8<br> %3 = load %struct.ExprContext** %econtext.addr, align 8<br> %4 = load i8** %isNull.addr, align 8<br> %5 = load i32** %isDone.addr, align 8<br> %call = call i64 %1(%struct.ExprState* %2, %struct.ExprContext* %3, i8* %4, i32* %5)<br> ret i64 %call<br>}</div><div><br>define i64 @JittedIntLit() {<br>entry:<br> ret i64 5<br>}</div><div><br>define i64 @JittedOpExpr(%struct.ExprState* %expr, %struct.ExprContext* %econtext, i8* %isNull, i32* %isDone) {<br>entry:<br> %lhs = call i64 @IrExprGetValue1(%struct.ExprState* inttoptr (i64 140715076067816 to %struct.ExprState*), %struct.ExprContext* %econtext, i8* %isNull, i32* %isDone)<br> %rhs = call i64 @JittedIntLit()<br> %tmp_add = add i64 %lhs, %rhs<br> ret i64 %tmp_add<br>}</div><div> </div><div>and /samba/data/gDB2/src/ptcompiler/compiler/llvm_ir/GaussDB.ir contains </div><div> </div><div>extern "C"<br>Datum IrExprGetValue(ExprState* expr, ExprContext *econtext, bool *isNull, ExprDoneCond *isDone) {<br> return expr->evalfunc(expr, econtext, isNull, isDone);<br>}</div><div> </div><div>Cheng</div></div><div class="gmail_extra"><div><div><br><div class="gmail_quote">On Thu, Sep 11, 2014 at 1:26 PM, Lang Hames <span dir="ltr"><<a href="mailto:lhames@gmail.com" target="_blank">lhames@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr">Hi Cheng,<div><br></div>It sounds like the either (1) 0x7fe4801fa1f8 is part of the environment, and it's not being configured the same way the 2nd time around, or (2) The JIT is handling on-disk objects differently from in-memory ones. The first option is more likely.<div><br></div><div>It would be helpful if you could attach the object that was stored in your cache.<br><div><br></div><div>Cheers,</div><div>Lang.</div></div></div><div class="gmail_extra"><br><div class="gmail_quote"><div><div>On Thu, Sep 11, 2014 at 10:58 AM, Cheng Zhu <span dir="ltr"><<a href="mailto:chengzhu@gmail.com" target="_blank">chengzhu@gmail.com</a>></span> wrote:<br></div></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;padding-left:1ex;border-left-color:rgb(204,204,204);border-left-width:1px;border-left-style:solid"><div><div><div dir="ltr"><div>Hi, All</div><div> </div><div>I have a problem to reuse mcjit jitted code loaded from ObjectCache from a file. In the first run, I use MCJIT generate function JittedOpExpr object code as following and it runs OK. 0x7fe4801fa1f8 at instruction 0x00007fe4cc6c2014 points to 0x69382E which is the beginning of ExecEvalVar function. Then I save the object code into a file after implementing notifyObjectCompiled method. </div><div> </div><div><br> IrExprGetValue:<br>0x00007fe4cc6c2000: push %rbp<br>0x00007fe4cc6c2001: mov %rsp,%rbp<br>0x00007fe4cc6c2004: mov 0x10(%rdi),%rax<br>0x00007fe4cc6c2008: pop %rbp<br>0x00007fe4cc6c2009: jmpq *%rax<br>0x00007fe4cc6c200b: nopl 0x0(%rax,%rax,1)<br> JittedOpExpr:<br>0x00007fe4cc6c2010: push %rbp<br>0x00007fe4cc6c2011: mov %rsp,%rbp<br><strong>0x00007fe4cc6c2014: movabs $0x7fe4801fa1f8,%rax</strong><br>0x00007fe4cc6c201e: movabs $0x7fe4801fa1e8,%rdi<br>0x00007fe4cc6c2028: callq *(%rax)<br>0x00007fe4cc6c202a: add $0x5,%rax<br>0x00007fe4cc6c202e: pop %rbp<br>0x00007fe4cc6c202f: retq <br>0x00007fe4cc6c2030: adc $0x0,%al<br>0x00007fe4cc6c2032: add %al,(%rax)<br>0x00007fe4cc6c2034: add %al,(%rax)</div><div> </div><div><strong>0x7fe4801fa1f8 -> 0x69382E</strong></div><div><strong> ExecEvalVar</strong>(ExprState*, ExprContext*, bool*, ExprDoneCond*):<br>0x000000000069382e: push %rbp<br>0x000000000069382f: mov %rsp,%rbp<br>0x0000000000693832: push %r12<br><br clear="all"></div><div>In the next run, I buildedunction of JittedOpExpr again and loaded compiled object from that saved binary file using getObject and went through getPointertoFunction from MCJIT execution engine, I got the following object code in memory. But this time 0x7fe4801fa1f8 points to 0x0, so when callq *(%rax) it couldn't find the ExecEvalVar anymore. I followed the blog "Object Caching with the Kaleidoscope Example Problem" written by Andy Kaylor by implementing my own MCJITObjectCache class. Did I miss anything here? Thank you very much. </div><div> </div><div> IrExprGetValue:<br>0x00007fe4cc6c2000: push %rbp<br>0x00007fe4cc6c2001: mov %rsp,%rbp<br>0x00007fe4cc6c2004: mov 0x10(%rdi),%rax<br>0x00007fe4cc6c2008: pop %rbp<br>0x00007fe4cc6c2009: jmpq *%rax<br>0x00007fe4cc6c200b: nopl 0x0(%rax,%rax,1)<br> JittedOpExpr:<br>0x00007fe4cc6c2010: push %rbp<br>0x00007fe4cc6c2011: mov %rsp,%rbp<br><strong>0x00007fe4cc6c2014: movabs $0x7fe4801fa1f8,%rax</strong><br>0x00007fe4cc6c201e: movabs $0x7fe4801fa1e8,%rdi<br>0x00007fe4cc6c2028: callq *(%rax)<br>0x00007fe4cc6c202a: add $0x5,%rax<br>0x00007fe4cc6c202e: pop %rbp<br>0x00007fe4cc6c202f: retq </div><div> </div><div><strong>but 0x7fe4801fa1f8 -> 000000000</strong><span><font color="#888888"><br><br>-- <br>Best regards<br><br>Cheng
</font></span></div></div>
<br></div></div>_______________________________________________<br>
LLVM Developers mailing list<br>
<a href="mailto:LLVMdev@cs.uiuc.edu" target="_blank">LLVMdev@cs.uiuc.edu</a> <a href="http://llvm.cs.uiuc.edu" target="_blank">http://llvm.cs.uiuc.edu</a><br>
<a href="http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev" target="_blank">http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev</a><br>
<br></blockquote></div><br></div>
</blockquote></div><br><br clear="all"><br></div></div><span><font color="#888888">-- <br>Best regards<br><br>Cheng
</font></span></div>
</blockquote></div><br></div>
</div></div></blockquote></div><br><br clear="all"><div><br></div>-- <br>Best regards<br><br>Cheng
</div>