Generate LLVM C++ API code as backend - c++

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.

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

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

Related

How to see code coverage of tests in Xcode 10 for C++ project

I have a C++ Xcode project on Mac, and I'd like to see how much code coverage my tests have. If I were building on linux, I would pass a few extra flags to GCC and then use gcov.
What I'm looking for is a way to do something similar on Mac, either through Xcode or through the command line.
This link describes how to get the code coverage tools that Xcode has built in to work, but unfortunately those seem to only work for ObjC or Swift. All the other articles I've found have been similarly unhelpful (either very out of date or only for Swift/ObjC).
I'm not sure how to get this running through the command line, since I'm not even sure what command(s) Xcode uses to build my project (I assume xcodebuild, but I'm not sure what arguments).
I'm fine with doing this either through the command line or through Xcode, whichever is easier.
Any help would be appreciated, please let me know if you need any more details.
Details:
I am using Xcode Version 10.1 (10B61).

llc has no debug or -view-dag-combine1-dags option

I have llc program installed on my computer via package manager (of course I have LLVM installed, 6.0.0 version). Also, I have it built from sources. What I want is to view DAGs, generated by llvm. But, unfortunately, I don't have any of options like -debug, -view-dag-combine1-dags and etc on both versions of llc. This drives me crazy since it is written everywhere, that this flags should help me, but it is not mentioned what to do if I don't have them. In help and man, there are no such options. I have graphviz, dot and gv installed, it should not be the problem.
Version of compiled llc.
./llc -version
LLVM (http://llvm.org/):
LLVM version 7.0.0svn
Optimized build.
Default target: x86_64-unknown-linux-gnu
Host CPU: broadwell
Registered Targets:
and targets...
I've built it with RelWithDebugInfo flag, maybe I should've built it with Debug flag? Or is there any flag in cmake, that I should enable? I believe that I've googled enough and I couldn't find any information.
Executing llc --help-hidden | grep view-dag using my debug build results in:
-filter-view-dags= -
Only display the basic block whose name matches this for all
view-*-dags options
-view-dag-combine-lt-dags - Pop up a window to show dags before the post legalize types dag combine
pass
-view-dag-combine1-dags - Pop up a window to show dags before the first dag combine pass
-view-dag-combine2-dags - Pop up a window to show dags before the second dag combine pass
If you look into llvm/lib/CodeGen/SelectionDAG/SelectionDAGISel.cpp, that command-line option is between #ifndef NDEBUG preprocessor statements, so those options are only exposed when you have a debug build.
Changing the CMAKE_BUILD_TYPE to Debug and recompiling should be enough.
I did it and it works.
$ cmake -DCMAKE_BUILD_TYPE:STRING=Debug
You can check: https://github.com/llvm/llvm-project/blob/master/llvm/docs/CMake.rst#id5
and if you want to check DAGs then maybe it helps you.
here: https://stackoverflow.com/a/52095047/12365658

Boost testrunner in Eclipse fails to generate valid XML

I am attempting to run unit tests from Eclipse on C++ code. My Eclipse is Mars.2 Release (4.5.2). I originally downloaded the Java Developer's version, but I've installed C/C++ plugins. I wrote a simple class and then a boost unit test for that class. I can compile the code both in Eclipse and on the command line to get an executable. If I run the executable on the command line, I get this output
Running 1 test case...
*** No errors detected
So far so good.
Now if I try to run inside Eclipse, I get an error. At first it appeared that my test was not running at all. In the "C/C++ Unit" tab, I got a red bar on the right side of the screen, no information where I should be seeing individual test results, and a message "XML parse error: Premature end of file" in small text right below the row of tabs. By stopping in the debugger, I was able to see that I'm victim of a known bug. Basically there's some incompatibility between Eclipse and boost testrunner, where one is specifying XML and the other xml (different case) for the format. I tried this work-around mentioned in the discussion about the bug at that link:
As a workaround you can add following arguments in the unittests' run configuration:
--output_format=XML --log_level=all --report_level=no --
Eclipse's test runner appends it's arguments to these. Arguments after -- are passed on to your test script and ignored by Boost.Test.
With that in place, I still have problems, but the problem is different depending on whether I run with debugging or not. Without debugging, the result is the same as noted above. With debugging, I get information about my test - noting that it ran and passed - on the left side of the "C/C++ Unit" tab but I still get the red bar indicating a failure. The small text in this case says: "XML parse error: Content is not allowed in tailing section."
The bug noted above appears to be about 2 yrs old, although the discussion has activity as recently as last month. I guess there's some other work around? It seems likely that the community has been running tests in the intervening two years!
I tried updating Eclipse via Help -> Check for Updates. That did not improve my situation. I suspect that other information may be valuable to help, but I'm not sure what to provide. I'll watch comments and update if / where I can.
Reluctantly, I upgraded to the Neon.2 release of Eclipse. The underlying bug is still present, as described in my question, but the work-around noted works in that release. Copying that down from the question for completeness:
As a workaround you can add following arguments in the unittests' run configuration:
--output_format=XML --log_level=all --report_level=no --
Eclipse's test runner appends it's arguments to these. Arguments after -- are passed on to your test script and ignored by Boost.Test.
Would have been nice if this worked in Mars as well since upgrading is a bit of a hassle - especially with support for multiple development languages - but at least I can move on. Probably would have had to upgrade at some point in any case.

LLVM 3.8: implementing a pass with ad IDE (Xcode)

I'm a newbie in both LLVM developing and cpp.
I wanted to ask if there was a way to develop a pass for LLVM in Xcode having all the typical features available in an IDE like autocomplete and syntax code highlighting.
Right now I am just writing c++ code, checking everything I need on the documentation, but, as you can imagine, this is really slowing me down and it's really error prone...
Don't know if this can help, but my LLVM folder is structured this way:
"llvm_3.8_source/" root folder of llvm3.8 source files
"llvm_3.8_source/build" root folder of my llvm3.8 build
Thanks
Yes, it is possible.
LLVM uses CMake as a build-system generator. CMake supports such things as old good makefiles, ninja, xcode, and visual studio.
You can simply create Xcode-project using the following commands from terminal:
cd llvm_build
cmake -G Xcode path/to/llvm/sources
open LLVM.xcodeproj
First you will see lots of errors and 'red' marks. It's because some parts of LLVM sources are generated during compilation.
All files will be generated as soon as you attempt to build project first time and all the 'red' marks will gone.
You can read this article to get a bit more info on the topic:
Getting started with LLVM/Clang on OS X

How to do automatic g++ compilation after each save?

I'm writing simple C++ code (one "cpp" file). Now I compile my program by manually running g++ from command line. But I was wondering if there is any way to do automatic g++ compilation after each save of file.
I'm using Ubuntu and Sublime Text 3.
There are also much fancier solutions available:
YouCompleteMe compiles and analyses the code in background while you type, for Vim
Emacs-ycmd is one of several ports to Emacs
RStudio IDE does the for C++ code (when though it is an R environment)
You can 'cook' similar things much simpler by using Linux kernel hooks in userspace to react whenever a file you watch is saved. Here you'd simply call g++, or as others already said, you probably want make.
One of the hooks is inotify; there are several Ubuntu packages accessing this (which I have yet to use). Here is one:
edd#max:~$ apt-cache show entr
Package: entr
Priority: optional
Section: universe/misc
Installed-Size: 61
Maintainer: Ubuntu Developers <ubuntu-devel-discuss#lists.ubuntu.com>
Original-Maintainer: Yuri D'Elia <wavexx#thregr.org>
Architecture: amd64
Version: 2.6-1
Depends: libbsd0 (>= 0.6.0), libc6 (>= 2.4)
Filename: pool/universe/e/entr/entr_2.6-1_amd64.deb
Size: 11310
MD5sum: 818b54114577b8d15d619577acf52c97
SHA1: b8422ee12546843f3357c1114bf1f5eeea8c863e
SHA256: ada9aa2ea218fc9f7a255b576d5069dda06da369ecc4000bb596e398b04eeafd
Description-en: Run arbitrary commands when files change
The Event Notify Test Runner (entr) runs arbitrary commands when
files change. Changes are detected through the kqueue/inotify
kernel interface.
Description-md5: 52fe22e37b3719b7c736bf46a6d8c9b7
Homepage: http://entrproject.org/
Bugs: https://bugs.launchpad.net/ubuntu/+filebug
Origin: Ubuntu
edd#max:~$
Create a makefile. Most editors have a shortcut keypress that executes make.
You can use make command.
Example: Imagine the file name is test.cpp then you can execute the following command:
make test
It will automatically compile using g++ as the extension is .cpp
If you are using VS Code, you can install the extension C/C++ Compile Run for the VS Code.
Once you change the code, just click F6 for executing it without passing any additional arguments, or F7 to type the passing arguments in the Command Palette, which will be passed to the code at the beginning of the execution.
C/C++ Compile Run
Use a proper build system that integrates with your IDE/editor.
There are many to choose from. My personal preference is SCons (http://scons.org/) but there are also options like CMake (https://cmake.org/) or autotools (https://en.m.wikipedia.org/wiki/GNU_Build_System) available or simply a plain Makefile.
And there are many more less known ones as well.
Check them out and pick what suits your needs best.