0

I want to generate LLVM-IR that contains a call to a C++ STL function. e.g. std::sort(). I think I need to go through following steps to achieve the task.

  1. Link std::sort() into the IR
  2. Get a reference to function std::sort()
  3. Create a call to std::sort() using IRBuilder.CreateCall(~).

I'm not sure how to achieve the first step above.

lonelyjoe
  • 115
  • 2
  • 7
  • 3
    Do you need `sort` for a specific T? Or you want to sort on a type that you only know at runtime? – sbabbi Jul 29 '21 at 20:40
  • `std::sort` is a template, not a function. – Paul Sanders Jul 29 '21 at 20:48
  • @sbabbi Yes, I only need `sort` for a specific `T`. I know which `T` to use at compile-time . – lonelyjoe Jul 29 '21 at 21:40
  • I can't really write a full answer right now. You are on the right track. For step 1, i would simply compile a C++ source file with an instantiation of sort. Then you get a pointer to such function and you cast it to intptr_t. For step 2, you create a constant value within LLVM using the intptr_t from the previous step, then you use a Bitcast to convert it to the correct function pointer type. Now you can use your function anywhere you want within LLVM. – sbabbi Aug 02 '21 at 11:22

0 Answers0