2

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).

localacct
  • 611
  • 5
  • 13
  • 2
    You can mix and match as far as LLVM is converned. But: First, any compiler may add add additional requirements, such as "all functions that meet requirement x must call runtime function 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. – arnt Jan 03 '22 at 16:25
  • 1
    Hi @arnt Thanks for the suggestion. I do make changes by adding a function and inserting call instructions as well. I suppose you are referring to the VerifierPass (https://llvm.org/doxygen/classllvm_1_1VerifierPass.html)? It says it creates a pass for the legacy pass manager but I am using the new pass manager, would that help? – localacct Jan 04 '22 at 08:03
  • Sorry @arnt, I think you meant these functions (https://llvm.org/doxygen/Verifier_8h.html) instead. – localacct Jan 04 '22 at 11:40
  • 1
    I meant the functions in verifier.h, yes, but the pass will also do. Verify any way you want, but verify early and often. It costs a little CPU time and discovers some/many bugs before the bugs strain the grey matter. – arnt Jan 04 '22 at 14:15
  • Hi @arnt I followed your suggestion about looking at the code I added and it helped me to look in the right direction instead of wondering if there are issues mixing the two toolchains. I created a function in my LLVM Pass but I inserted a call to it later on in a CallInst erroneously. The LLVM Pass compiles fine and the bitcode modification worked as well. The error came when I tried to rebuild the bitcode files and I saw the error message I posted in this question. Perhaps you can use your comment as the answer? Then I can accept it. – localacct Jan 07 '22 at 05:38

1 Answers1

1

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.

arnt
  • 8,949
  • 5
  • 24
  • 32