<html>
<head>
<meta content="text/html; charset=ISO-8859-1"
http-equiv="Content-Type">
</head>
<body text="#000000" vlink="#551A8B" alink="#EE0000" link="#0B6CDA"
bgcolor="#ffffff">
<div class="moz-cite-prefix">I tried using 'getPointerToFunction()'
and the correct function I generated was called from C code, but
when it runs inside the JIT engine it always returns the value of
the first function. I even tried to 'deleteBody()' and
'removeFromParent()' from the first function generated, and still
returns the first value.<br>
<br>
I also tried using 'dump()' to see if the values where being
updated. This is the C code I run with the JIT engine:<br>
<font face="Courier New, Courier, monospace"><br>
</font>
<blockquote><font face="Courier New, Courier, monospace">#include
<stdio.h></font><br>
<br>
<font face="Courier New, Courier, monospace">int sum(int a, int
b); // declaration</font><br>
<br>
<font face="Courier New, Courier, monospace">int get_int(int a,
int b)</font><br>
<font face="Courier New, Courier, monospace">{</font><br>
<font face="Courier New, Courier, monospace"> int x =
sum(a,b);</font><br>
<font face="Courier New, Courier, monospace"> printf("The
result was: %d\n",x);</font><br>
<font face="Courier New, Courier, monospace"> return x;</font><br>
<font face="Courier New, Courier, monospace">}</font><br>
<br>
</blockquote>
As you can see I print the return value of the function. The
functions I generate and that I called 'get_int_*()' in the
previous email are now called 'sum_mockup_N()'. This is the code
LLVM IR code:<br>
<br>
<blockquote><small><font face="Courier New, Courier, monospace">;
Function Attrs: nounwind uwtable</font></small><br>
<small><font face="Courier New, Courier, monospace">define i32
@get_int(i32 %a, i32 %b) #0 {</font></small><br>
<small><font face="Courier New, Courier, monospace">entry:</font></small><br>
<small><font face="Courier New, Courier, monospace"> %a.addr =
alloca i32, align 4</font></small><br>
<small><font face="Courier New, Courier, monospace"> %b.addr =
alloca i32, align 4</font></small><br>
<small><font face="Courier New, Courier, monospace"> %x =
alloca i32, align 4</font></small><br>
<small><font face="Courier New, Courier, monospace"> store i32
%a, i32* %a.addr, align 4</font></small><br>
<small><font face="Courier New, Courier, monospace"> store i32
%b, i32* %b.addr, align 4</font></small><br>
<small><font face="Courier New, Courier, monospace"> %0 = load
i32* %a.addr, align 4</font></small><br>
<small><font face="Courier New, Courier, monospace"> %1 = load
i32* %b.addr, align 4</font></small><br>
<small><font face="Courier New, Courier, monospace"> <b><big><small>%call
= call i32 @sum_mockup(i32 %0, i32 %1)</small></big></b></font></small>
<small><font face="Courier New, Courier, monospace"><b>; Use the
first function generated</b></font></small><br>
<small><font face="Courier New, Courier, monospace"> store i32
%call, i32* %x, align 4</font></small><br>
<small><font face="Courier New, Courier, monospace"> %2 = load
i32* %x, align 4</font></small><br>
<small><font face="Courier New, Courier, monospace"> %call1 =
call i32 (i8*, ...)* @printf(i8* getelementptr inbounds ([20
x i8]* @.str, i32 0, i32 0), i32 %2)</font></small><br>
<small><font face="Courier New, Courier, monospace"> %3 = load
i32* %x, align 4</font></small><br>
<small><font face="Courier New, Courier, monospace"> ret i32 %3</font></small><br>
<small><font face="Courier New, Courier, monospace">}</font></small><br>
<br>
<small><font face="Courier New, Courier, monospace">The result
was: 10</font></small><br>
<br>
<small><font face="Courier New, Courier, monospace">; sum always
remains a declaration</font></small><br>
<small><font face="Courier New, Courier, monospace">declare i32
@sum(i32, i32) #1</font></small><br>
<br>
<small><font face="Courier New, Courier, monospace"><b>define
i32 @sum_mockup(i32, i32)</b> <b>#1 {</b></font></small><b><br>
</b><b><small><font face="Courier New, Courier, monospace">mockup_block:</font></small></b><b><br>
</b><b><small><font face="Courier New, Courier, monospace"> ret
i32 10</font></small></b><b><br>
</b><b><small><font face="Courier New, Courier, monospace">}</font></small></b><br>
<br>
<small><font face="Courier New, Courier, monospace">****************************</font></small><br>
<font face="Courier New, Courier, monospace"><b><small>[C Code]
getPointerToFunction()= 10</small></b><b> </b><b><small>
// Pointer to</small></b> </font><small><font
face="Courier New, Courier, monospace"><b>@sum_mockup(i32,
i32)</b></font></small><br>
<small><font face="Courier New, Courier, monospace">****************************</font></small><br>
<br>
<small><font face="Courier New, Courier, monospace">; Function
Attrs: nounwind uwtable</font></small><br>
<small><font face="Courier New, Courier, monospace">define i32
@get_int(i32 %a, i32 %b) #0 {</font></small><br>
<small><font face="Courier New, Courier, monospace">entry:</font></small><br>
<small><font face="Courier New, Courier, monospace"> %a.addr =
alloca i32, align 4</font></small><br>
<small><font face="Courier New, Courier, monospace"> %b.addr =
alloca i32, align 4</font></small><br>
<small><font face="Courier New, Courier, monospace"> %x =
alloca i32, align 4</font></small><br>
<small><font face="Courier New, Courier, monospace"> store i32
%a, i32* %a.addr, align 4</font></small><br>
<small><font face="Courier New, Courier, monospace"> store i32
%b, i32* %b.addr, align 4</font></small><br>
<small><font face="Courier New, Courier, monospace"> %0 = load
i32* %a.addr, align 4</font></small><br>
<small><font face="Courier New, Courier, monospace"> %1 = load
i32* %b.addr, align 4</font></small><br>
<small><font face="Courier New, Courier, monospace"> <b>%call
= call i32 @sum(i32 %0, i32 %1)</b></font></small> <b><small><font
face="Courier New, Courier, monospace">// Change it back
to the declaration</font></small></b><br>
<small><font face="Courier New, Courier, monospace"> store i32
%call, i32* %x, align 4</font></small><br>
<small><font face="Courier New, Courier, monospace"> %2 = load
i32* %x, align 4</font></small><br>
<small><font face="Courier New, Courier, monospace"> %call1 =
call i32 (i8*, ...)* @printf(i8* getelementptr inbounds ([20
x i8]* @.str, i32 0, i32 0), i32 %2)</font></small><br>
<small><font face="Courier New, Courier, monospace"> %3 = load
i32* %x, align 4</font></small><br>
<small><font face="Courier New, Courier, monospace"> ret i32 %3</font></small><br>
<small><font face="Courier New, Courier, monospace">}</font></small><br>
<br>
<br>
<small><font face="Courier New, Courier, monospace">; Function
Attrs: nounwind uwtable</font></small><br>
<small><font face="Courier New, Courier, monospace">define i32
@get_int(i32 %a, i32 %b) #0 {</font></small><br>
<small><font face="Courier New, Courier, monospace">entry:</font></small><br>
<small><font face="Courier New, Courier, monospace"> %a.addr =
alloca i32, align 4</font></small><br>
<small><font face="Courier New, Courier, monospace"> %b.addr =
alloca i32, align 4</font></small><br>
<small><font face="Courier New, Courier, monospace"> %x =
alloca i32, align 4</font></small><br>
<small><font face="Courier New, Courier, monospace"> store i32
%a, i32* %a.addr, align 4</font></small><br>
<small><font face="Courier New, Courier, monospace"> store i32
%b, i32* %b.addr, align 4</font></small><br>
<small><font face="Courier New, Courier, monospace"> %0 = load
i32* %a.addr, align 4</font></small><br>
<small><font face="Courier New, Courier, monospace"> %1 = load
i32* %b.addr, align 4</font></small><br>
<small><font face="Courier New, Courier, monospace"> <b>%call
= call i32 @sum_mockup_1(i32 %0, i32 %1) ; Use the other
function generated</b></font></small><br>
<small><font face="Courier New, Courier, monospace"> store i32
%call, i32* %x, align 4</font></small><br>
<small><font face="Courier New, Courier, monospace"> %2 = load
i32* %x, align 4</font></small><br>
<small><font face="Courier New, Courier, monospace"> %call1 =
call i32 (i8*, ...)* @printf(i8* getelementptr inbounds ([20
x i8]* @.str, i32 0, i32 0), i32 %2)</font></small><br>
<small><font face="Courier New, Courier, monospace"> %3 = load
i32* %x, align 4</font></small><br>
<small><font face="Courier New, Courier, monospace"> ret i32 %3</font></small><br>
<small><font face="Courier New, Courier, monospace">}</font></small><br>
<br>
<small><font face="Courier New, Courier, monospace">The result
was: 10</font></small><br>
<br>
<small><font face="Courier New, Courier, monospace">declare i32
@sum(i32, i32) #1</font></small><br>
<br>
<br>
<b><small><font face="Courier New, Courier, monospace">define
i32 @sum_mockup_1(i32, i32) #1 {</font></small></b><b><br>
</b><b><small><font face="Courier New, Courier, monospace">mockup_block:</font></small></b><b><br>
</b><b><small><font face="Courier New, Courier, monospace"> ret
i32 5</font></small></b><b><br>
</b><b><small><font face="Courier New, Courier, monospace">}</font></small></b><br>
<br>
<small><font face="Courier New, Courier, monospace">****************************</font></small><br>
<b><small><font face="Courier New, Courier, monospace">[C Code]
getPointerToFunction()= 5</font></small></b> <font
face="Courier New, Courier, monospace"><small><b>// Pointer to</b></small></font>
<small><font face="Courier New, Courier, monospace"><b>@sum_mockup_1(i32,
i32)</b></font></small><br>
<small><font face="Courier New, Courier, monospace">****************************</font></small><br>
<br>
<small><font face="Courier New, Courier, monospace">; Function
Attrs: nounwind uwtable</font></small><br>
<small><font face="Courier New, Courier, monospace">define i32
@get_int(i32 %a, i32 %b) #0 {</font></small><br>
<small><font face="Courier New, Courier, monospace">entry:</font></small><br>
<small><font face="Courier New, Courier, monospace"> %a.addr =
alloca i32, align 4</font></small><br>
<small><font face="Courier New, Courier, monospace"> %b.addr =
alloca i32, align 4</font></small><br>
<small><font face="Courier New, Courier, monospace"> %x =
alloca i32, align 4</font></small><br>
<small><font face="Courier New, Courier, monospace"> store i32
%a, i32* %a.addr, align 4</font></small><br>
<small><font face="Courier New, Courier, monospace"> store i32
%b, i32* %b.addr, align 4</font></small><br>
<small><font face="Courier New, Courier, monospace"> %0 = load
i32* %a.addr, align 4</font></small><br>
<small><font face="Courier New, Courier, monospace"> %1 = load
i32* %b.addr, align 4</font></small><br>
<small><font face="Courier New, Courier, monospace"> <b>%call
= call i32 @sum(i32 %0, i32 %1)</b></font></small> <b><small><font
face="Courier New, Courier, monospace">// Change it back
to the declaration</font></small></b><br>
<small><font face="Courier New, Courier, monospace"> store i32
%call, i32* %x, align 4</font></small><br>
<small><font face="Courier New, Courier, monospace"> %2 = load
i32* %x, align 4</font></small><br>
<small><font face="Courier New, Courier, monospace"> %call1 =
call i32 (i8*, ...)* @printf(i8* getelementptr inbounds ([20
x i8]* @.str, i32 0, i32 0), i32 %2)</font></small><br>
<small><font face="Courier New, Courier, monospace"> %3 = load
i32* %x, align 4</font></small><br>
<small><font face="Courier New, Courier, monospace"> ret i32 %3</font></small><br>
<small><font face="Courier New, Courier, monospace">}</font></small><br>
</blockquote>
<br>
If I get a pointer to the function 'get_int()' I still have the
same problem of getting the same return value of the first
generated function. But when I get a pointer to the generated
functions themselves 'sum_mockup_*()' I get the actual value I'm
expecting.<br>
<br>
<br>
<br>
On 28/06/14 22:48, Yaron Keren wrote:<br>
</div>
<blockquote
cite="mid:CANa4zJp4N_jdA5EtaVdsJyLEGXoH2o2--89739ejjnWP6FUvVQ@mail.gmail.com"
type="cite">
<div dir="rtl">
<div dir="ltr">getFunction() -> getPointerToFunction()</div>
<div dir="ltr"><br>
</div>
<div dir="ltr"><br>
</div>
</div>
<div class="gmail_extra">
<div dir="ltr"><br>
<br>
<div class="gmail_quote">2014-06-29 6:40 GMT+03:00 Yaron Keren
<span dir="ltr"><<a moz-do-not-send="true"
href="mailto:yaron.keren@gmail.com" target="_blank">yaron.keren@gmail.com</a>></span>:<br>
<blockquote class="gmail_quote" style="margin:0
.8ex;border-left:1px #ccc solid;border-right:1px #ccc
solid;padding-left:1ex;padding-right:1ex">
<div dir="rtl">
<div dir="ltr">Hi Adrian,</div>
<div dir="ltr"><br>
</div>
<div dir="ltr">
<span
style="font-family:arial,sans-serif;font-size:12.727272033691406px">freeMachineCodeForFunction
is required but </span><span
style="font-family:arial,sans-serif;font-size:12.727272033691406px">recompileAndLinkFunction
is not,</span></div>
<div dir="ltr"><span
style="font-family:arial,sans-serif;font-size:12.727272033691406px">you
can use </span>getFunction() always.<br>
</div>
<div dir="ltr"><br>
</div>
<div dir="ltr">Try to M->dump()
calling M->getFunction() where M is the Module *.</div>
<div dir="ltr">See if how the changes appear in the
module dump as expected.</div>
<div dir="ltr"><br>
</div>
<div dir="ltr">Yaron</div>
<div dir="ltr"><br>
</div>
<div dir="ltr"><br>
</div>
<div dir="ltr"><br>
</div>
</div>
<div class="gmail_extra">
<br>
<br>
<div class="gmail_quote">
<div dir="ltr">2014-06-29 5:56 GMT+03:00 Adrian Ortega
<span dir="ltr"><<a moz-do-not-send="true"
href="mailto:elfus0.1@gmail.com" target="_blank">elfus0.1@gmail.com</a>></span>:</div>
<blockquote class="gmail_quote" style="margin:0 0 0
.8ex;border-left:1px #ccc solid;padding-left:1ex">
<div>
<div class="h5">
<div text="#000000" bgcolor="#FFFFFF"> Hello,<br>
<br>
The problem I'm having is that I modify a
function body by using
'Value::replaceAllUsesWith' and then execute
it with the JIT engine several times but I
always get the output from the first iteration
for all the iterations.<br>
<br>
This is what I do:<br>
<br>
<br>
I generate the following 2 functions on the
fly based on the FunctionType of the
declaration below in C code.<br>
<br>
<blockquote><font face="Courier New, Courier,
monospace">define i32 @get_int_5(i32, i32)
#1 {</font><br>
<font face="Courier New, Courier, monospace">
ret i32 5</font><br>
<font face="Courier New, Courier, monospace">}</font><br>
<br>
<font face="Courier New, Courier, monospace">define
i32 @get_int_10(i32, i32) #1 {</font><br>
<font face="Courier New, Courier, monospace">
ret i32 10</font><br>
<font face="Courier New, Courier, monospace">}</font></blockquote>
I have the following C code:
<blockquote><br>
<font face="Courier New, Courier, monospace"><font
face="Courier New, Courier, monospace">//
This is only a declaration<br>
// I use this FunctionType to generate
the functions above<br>
</font>int sum(int a, int b); <br>
<br>
int get_int()<br>
{</font><font face="Courier New, Courier,
monospace"><font face="Courier New,
Courier, monospace"><br>
</font> return sum(a,b); <br>
}<br>
<br>
</font></blockquote>
I replace the call to 'sum()' by calling
Value::replaceAllUsesWith for one of the
functions generated above, run with JIT. I
checked the code generated and it actually
changes the call from 'sum()' to 'get_int_5()'
and I get a 5 as return value when I call the
function with the JIT execution engine.<br>
<br>
Then I repeat the previous step using
'Value::replaceAllUsesWith' for the next
function I generated and run it with JIT. Then
again the code generated is what I expected,
this time<br>
function call changes from 'sum()' to
'get_int_10()', however the problem is I get a
5 instead of a 10.<br>
<br>
I tried 'recompileAndLinkFunction' as well as
'freeMachineCodeForFunction' and I always get
the return value from the first function, and
not the second as I should even though the
generated code that I dump() says that it has
the correct function call.<br>
<br>
I am using version 3.4 for both clang and
llvm. And also I'm using the JIT Engine and
not the MCJIT.<br>
<br>
Do you have have any idea why the references
or 'uses' changes are not reflected in the
code JIT'ed ?<br>
<br>
Regards.<br>
<blockquote><br>
</blockquote>
<br>
<br>
<br>
<br>
<br>
</div>
<br>
</div>
</div>
_______________________________________________<br>
LLVM Developers mailing list<br>
<a moz-do-not-send="true"
href="mailto:LLVMdev@cs.uiuc.edu" target="_blank">LLVMdev@cs.uiuc.edu</a>
<a moz-do-not-send="true"
href="http://llvm.cs.uiuc.edu" target="_blank">http://llvm.cs.uiuc.edu</a><br>
<a moz-do-not-send="true"
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>
</div>
</div>
</blockquote>
<br>
</body>
</html>