8

The clang 3.0 online demo page http://llvm.org/demo/index.cgi provides an option to output LLVM C++ API code" representing the LLVM-IR for the input program.

Is "produce LLVM C++ API code" output a clang option (and if so, what is it)?

Or is it an llvm tool option (which one)?

Is it possible to do the same thing but from LLVM-IR input? Basically I'd like to see the proper llvm c++ api calls needed to produce a particular given llvm-ir sequence. I'd like to learn backwards by example rather than forwards from the documentation.

Manual pages and --help and --help-hidden for clang, llvm-as and llvm-dis don't show anything obvious.

edit: OK now I see in the output on that web page, "generated by llvm2cpp". But I can't find that tool in recent llvm releases, only old releases, has a new tool in 2.9 and 3.0 taken over for llvm2cpp?

Bogatyr
  • 19,255
  • 7
  • 59
  • 72

2 Answers2

9

Yes. C++ backend is the tool which does this. Try "llc -march=cpp foo.bc"

Anton Korobeynikov
  • 9,074
  • 25
  • 28
  • thanks. I just found in the 2.3 release notes "The llvm2cpp tool has been folded into llc, use llc -march=cpp instead of llvm2cpp". BTW -march=c++ doesn't work in 3.0, but -march=cpp does – Bogatyr Mar 23 '12 at 09:20
  • Unfortunately lcc -march=cpp fails on files with debug metadata. Any known workaround to that? My whole purpose was to see the c++ apis for adding debug metadata, so this is a showstopper. – Bogatyr Mar 23 '12 at 09:21
  • @Bogatyr: this should be considered a bug. Feel free to fill a PR in LLVM bugzilla with small reproducer. – Anton Korobeynikov Mar 23 '12 at 09:54
  • [This article](http://fdiv.net/2012/11/16/llvm-generates-code-that-generates-code) has a few details to help you use `llc -march=cpp`. – jlstrecker Nov 28 '12 at 18:14
  • This doesn't work anymore. I get this error: `invalid target cpp` – NEO Mar 11 '14 at 10:33
  • 1
    @NEO, you need to make sure you compile cpp backend – Anton Korobeynikov Mar 11 '14 at 17:08
2

I ran into exactly the same problem and saw the CPPBuilder mentioned a couple of times. This approach unfortunately no longer works on recent LLVM versions as the CPPBackend was removed between 3.8 and 3.9.

If you want the CPP backend you (i) have to configure llvm and add cppbackend to -DLLVM_TARGETS_TO_BUILD during the initial configure and (ii) run an llvm <= 3.8.

The feature was removed because it did not use IRBuilder and almost nobody used it. My solution was to rely on the old version to get inspired, then implement it myself.