Search code examples
iosclangllvmbitcode

Rebuild opt modified bitcode files using Apple clang


I modified a bitcode enabled IPA using the open source LLVM toolchain's opt and ran my own LLVM pass on the bitcode files.

After modifications, I tried to use Apple's LLVM toolchain's clang to rebuild the bitcode files to get the binary but I got the following error

ld: Invalid record (Producer: 'LLVM13.0.0' Reader: 'LLVM APPLE_1_1300.0.29.30_0') for architecture arm64

So it seems I cannot mix and match the tools from the two toolchains? I read online that it was possible to use clang to run an LLVM Pass (compiled as dylib) directly so I tried to do that with Apple's clang but the LLVM Pass did not seem to run and the output from Apple's clang did not mention if the LLVM Pass was successful.

What is the proper command line to use for clang if I want it to run my LLVM Pass for optimization? I am using the new LLVM Pass Manager when writing the code for the LLVM Pass (dylib).


Solution

  • You can mix and match as far as LLVM is converned; the bitcode format is kept compatible for a long time.

    But: First, any compiler may add add additional requirements, such as "all functions that meet requirement x must call runtime function y at point z". Second, your passes might create an invalid LLVM file. I've found it particularly tricky to keep the DWARF debug information valid and coherent as I change code Try calling verifyModule() after each of your passes.