0

I want to use LLVM to JIT compile some code in my game using the LLVM C/C++ API. The JIT compiled functions should be able to effect the physics of the world, which is handled by bullet3.
To do so I need/want to allocate some space on the stack for a btVector3. This class is not simply a four element array of floats, but fulfills many alignment and SIMD properties, so I think manually creating a struct type would be hard and difficult to maintain.

Is there a way to load the definitions from the btVector3.h file into a LLVM module?

I already tried a few things:

  • I tried to compile the btVector3.h file into a shared object file using g++. This does not work as neither nm nor readelf recognize the resulting file as a valid object.
  • I tried compiling the btVector3.h file into a .bc file using clang++ -c -emit-llvm. This also faces the same issue when using llvm-nm to check the resulting file.
  • I also tried to create a proxy file that uses all necessary functions and then compile the proxy. This works for accessing functions like btVector3::x() in the resulting shared object file, but does not allow me to access the definition of the structure itself.

I can work around the issue by simplifying the btVector3 object to a 16bit align float array, and then every time I want to pass a vector to an extern function void foo(btVector3&) I could call a function like:

void _foo(float* p)
{
    btVector3 v(p[0], p[1], p[2]);

    foo(v);

    p[0] = v.x();
    p[1] = v.y();
    p[2] = v.z();
}

which I compile into a shared object file beforehand. This feels bad, but would fix the problem and I could even imagine that the JIT optimizer would inline this function, which could fix the problem.

I would be happy about either a way to directly incorporate the definitions as I have stated in the beginning. I would also be happy about any other work around ideas.

5
  • As I understand your problem, there are two basic approaches: ① Automatically generate an array type an alloca that, and ② automatically generate a complicated struct type and alloca that. Commented Jun 16, 2024 at 20:03
  • Exactly, but how can I generate them automatically. Idealy I could call something like StructType *btVector3Ty = StructType::create(context, btVector3). But to my knowlage there is no such constructor. Commented Jun 17, 2024 at 7:14
  • I looked at some similar code and saw a three-call sequence: getIfExists() and if that's null, create(), and if that has no body fields, setBody(). Commented Jun 17, 2024 at 9:43
  • Sorry for the late answer. I do not understand how you're suggestion fixes my problem. On what should I call getIfExists()? If I try to look for "btVector3" it will always return null because I did not manually register it yet, no? Commented Jun 18, 2024 at 7:14
  • That wasn't clear in the code at which I looked. In that code, different call paths might create the StructType, including one that loaded a previously written Module from a file and implicitly created all of the types used in that file. If you know that you'll never load a Module from file, you shouldn't need getIfExists(). Commented Jun 22, 2024 at 14:37

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.