<div dir="ltr">There isn't any magic command for this - you'd have to write some C++ code/a custom LLVM optimization pass.<br><br>Though, that said - perhaps it should just be a runtime parameter where you rely on LLVM to inline/optimize things away? You could do some relatively smaller code generation - generate a function that calls into the generic function (that takes the functior by pointer) and have the generic function be always_inline, so it gets inlined into the outer, type-specific function, without you having to write code to create all that IR on every use (instead relying on the inliner to create it for you, essentially)</div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Mon, Jul 8, 2019 at 4:33 PM Rajesh S R <<a href="mailto:srrajesh1989@gmail.com">srrajesh1989@gmail.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr">Thanks David! <div><br></div><div>I am not clear on how to achieve this though. Could you give more info on this?<div><br></div><div>I expect something like this:</div><div><br></div><div>sort.cc file:</div><div><br></div><div>bool Compare(void* a, void* b) {</div><div> return false;</div><div>}</div><div><br></div><div>void SortFunc(void* arr, int len) {</div><div> std::sort(arr, len, &Compare)</div><div>}</div><div><br></div><div>$MAGIC_COMMAND sort.cc -o a.llvm</div><div><br></div><div>a.llvm is a bytecode which can be loaded at runtime in my JIT module and write some code to replace "Compare" function with our own "Compare" function.</div><div><div><br></div><div><br></div></div></div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Mon, Jul 8, 2019 at 1:17 PM David Blaikie <<a href="mailto:dblaikie@gmail.com" target="_blank">dblaikie@gmail.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr">That's an option, for sure - having llvm::Functions you could clone into your module, replace the call to the Compare function to a call to the specific comparison you want & the let the optimizers do their work.</div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Wed, Jul 3, 2019 at 4:23 PM Rajesh S R <<a href="mailto:srrajesh1989@gmail.com" target="_blank">srrajesh1989@gmail.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr">Thanks David! I understand that std::sort doesn't exist without types especially at bytecode layer. <div><br></div><div>What I was thinking was something like the following:<div><br></div><div>Compile std::sort with a thunk function Compare(void*, void*) {rerturn false} into bytecode with an option say noinline and always make the function call or even a simple unoptimized bytecode which guarantees that Compare exists as a function without any inlining.</div><div><br></div><div>At run time implement a new Compare function for your type and replace the Compare function with the new Compare implemented with JIT. Now the JIT runtime has whole program in bytecode which it can aggressively optimize. </div></div><div>A good way to realize this "thunking" into LLVM JIT can enable lots of optimized algorithms to be ported into JIT without having to re-invent them for each JIT runtime.</div><div><br></div><div>Thoughts?</div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Wed, Jul 3, 2019 at 3:56 PM David Blaikie <<a href="mailto:dblaikie@gmail.com" target="_blank">dblaikie@gmail.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr">If you consider how std::sort works - it doesn't exist in the machine (or even LLVM IR) level without a specific type - so if you were to support something like std::sort in your language (JITed or not), it means some form of specialization of your types/functions based on other types.<br><br>So, yes, something like "own Sort function inside JIT" - if your language doesn't support a way to write this in the language itself (if it doesn't have anything like C++ templates or an equivalent generic thing).<br><br>Otherwise you can do something like qsort (which uses a function pointer to parameterize the comparison) & perhaps force or encourage the optimizer to inline and collapse away this indirection.</div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Wed, Jul 3, 2019 at 3:43 PM Rajesh S R via llvm-dev <<a href="mailto:llvm-dev@lists.llvm.org" target="_blank">llvm-dev@lists.llvm.org</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr">Hi LLVM devs,<div>The performance of C++ std::sort comes from being able to inline the comparator. For a JIT generated data type, using the comparator as a function call from std::sort may not be ideal. So, i was wondering how can we make a JIT-sort which is as good as statically compiled std::sort with comparator inlined.</div><div><br></div><div>What is the recommended way to pass a an existing function like std::sort into JIT so that it can optimize the whole program? For example, how do we generate the bytecode for existing function (say some external tools) and give to JIT runtime. Is there some code sample?</div><div><br></div><div>The alternative is to rollout my own Sort function inside JIT, but i don't want to do that and want to take this as an opportunity to learn the general approach of passing existing function definitions into JIT to do whole program optimization like inlining.</div><div><br></div><div>I found this <a href="https://stackoverflow.com/questions/10587250/fast-to-compile-efficient-sort-algorithm-for-jit-compilation" target="_blank">stackoverflow question </a>which is related to what I am asking for, but I don't see any final conclusion on this beyond incurring a function call for each comparison.</div><div><br></div><div>Thanks!</div><div><br></div><div>Rajesh S R</div></div>
_______________________________________________<br>
LLVM Developers mailing list<br>
<a href="mailto:llvm-dev@lists.llvm.org" target="_blank">llvm-dev@lists.llvm.org</a><br>
<a href="https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev" rel="noreferrer" target="_blank">https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev</a><br>
</blockquote></div>
</blockquote></div>
</blockquote></div>
</blockquote></div>
</blockquote></div>