15

The Online LLVM demo page had an option to generate LLVM C++ API code as backend from a source code. However, that demo page is now disabled. I was wondering how we can do it ourselves using the available LLVM tools.

I tried the following

clang++ -c -emit-llvm input.cpp -o input.ll
llc -march=cpp -o input.ll.cpp input.ll

which gives the following error

llc: error: invalid target 'cpp'.

I am using LLVM/Clang version 3.2.

MetallicPriest
  • 29,191
  • 52
  • 200
  • 356
  • 5
    does `llc -version` list `cpp` as a valid backend target? – Necrolis Feb 07 '13 at 13:33
  • llc -version does not show cpp on the list either. Does it require any registration or something to include cpp, or what? – MetallicPriest Feb 07 '13 at 13:54
  • @MetallicPriest I guess they just don't enable it for the online demo (it's not really what people are interested in, I guess) Why don't you install LLVM on your system locally? – us2012 Feb 07 '13 at 14:06
  • If its not in the list, then its been removed as a backend, IMO you'd get a lot better help from the LLVM/CFE mailing lists. – Necrolis Feb 07 '13 at 15:08
  • How have you compiled LLVM? If I remember correctly, cpp backend is disabled by default in cmake-based systems. So, make sure it's enabled while configuring. – Anton Korobeynikov Feb 08 '13 at 15:36
  • My llc here (llvm-3.1-12.fc18.x86_64, Fedora 18) does show cpp as a backend. – vonbrand Feb 08 '13 at 17:59
  • 1
    If you're still interested, my demo page at http://ellcc.org/demo/ can generate the C++ API code. – Richard Pennington Feb 10 '13 at 18:54

2 Answers2

14

The LLVM C++ backend has to be enabled during configuration when building LLVM. It's enabled by default in the configure (autotools) build, but not in the CMake build when you build on Windows. You can enable it by setting the appropriate flags while configuring with CMake. See this page for more information.

Quote:

LLVM_TARGETS_TO_BUILD:STRING Semicolon-separated list of targets to build, or all for building all targets. Case-sensitive. For Visual C++ defaults to X86. On the other cases defaults to all. Example: -DLLVM_TARGETS_TO_BUILD="X86;PowerPC".

UPDATE

Since version 3.9 the CppBackend is no more a valid target. They've removed from their code as the generated code were presenting a few issues.

Check this commit

Remove bit-rotten CppBackend.

This backend was supposed to generate C++ code which will re-construct
the LLVM IR passed as input. This seems to me to have very marginal
usefulness in the first place.

However, the code has never been updated to use IRBuilder, which makes
its current value negative -- people who look at the output may be
steered to use the *wrong* C++ APIs to construct IR.

Furthermore, it's generated code that doesn't compile since at least
2013.

Differential Revision: http://reviews.llvm.org/D19942

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@268631 91177308-0d34-0410-b5e6-96231b3b80d8
thiagoh
  • 7,098
  • 8
  • 51
  • 77
Eli Bendersky
  • 263,248
  • 89
  • 350
  • 412
  • 1
    Is there an alternative for the deprecated CppBackend in recent LLVM versions? Basically, something that would provide the same functionality, probably using IRBuilder. – Farzam Feb 10 '23 at 17:44
2

Sadly, this appears to no longer be possible in more recent versions of LLVM. The associated commit message explains it pretty well.

As you can see in the following commit, Remove bit-rotten CppBackend, the generated code would show issues.

commit 257fabb18605265a79397d35dd79a3973760ffaf
Author: ---
Date:   Thu May 5 14:35:40 2016 +0000

Remove bit-rotten CppBackend.

This backend was supposed to generate C++ code which will re-construct
the LLVM IR passed as input. This seems to me to have very marginal
usefulness in the first place.

However, the code has never been updated to use IRBuilder, which makes
its current value negative -- people who look at the output may be
steered to use the *wrong* C++ APIs to construct IR.

Furthermore, it's generated code that doesn't compile since at least
2013.

Differential Revision: http://reviews.llvm.org/D19942

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@268631 91177308-0d34-0410-b5e6-96231b3b80d8
thiagoh
  • 7,098
  • 8
  • 51
  • 77
Will Bradley
  • 1,733
  • 15
  • 27