Adding LLVM Plugin with CMake for C++ library - c++

I am trying to use an LLVM plugin (Enzyme) for a C++ library I am writing using CMake for the build process. I am new to CMake and LLVM so not sure about the best way to approach this using Modern CMake practices.
I have indentified a few steps that I need to perform in order to get the plugin working, but am not sure how to implement this in CMake for my library.
Require LLVM-12 and LLVM-LIT to be installed.
Compile plugin against LLVM which generates ClangEnzyme-.so shared library
Now I can compile C++ using the plugin with clang test2.cpp -Xclang -load -Xclang /path/to/ClangEnzyme-11.so -O2 -fno-vectorize -fno-unroll-loops
How would I go about writing a CMake program to add this plugin for my library so it can be linked and used by the rest of the library?
Any help is greatly appreciated

Related

Custom build cmake using standard library also for project with lower gcc version

I have a custom build cmake v3.10.0 which was compiled with a gcc_4.8.3. I am using this custom build cmake to compile a cmake project that must use gcc _4.1.2 because of legacy code.
Executing cmake promted me with an error because it needs to use the libstdc++-IFX.so.6 provided by gcc_4.8.3 which I fixed by adding the path to the correct library in my LD_LIBRARY_PATH before the path to the libraries provided by gcc_4.1.2.
Compiling my project and linking an executable (which is done by c++) results in the linker taking the gcc_4.8.3 stdlibs over the gcc_4.1.2 libs. Is there any way to tell cmake to not use the libraries it needs for himself for my cmake project preferably without touching LD_LIBRARY_PATH?
Edits:
#squareskittles comment: I did read and try everything this post suggest but without any changes. The libstdc++-IFX.so.6 is still taken from gcc_4.8.3

How can I bypass dependency builds while building my clang plugin?

Is it possible to build a clang plugin using an already built version of clang?
I've done all the steps to download and build clang but I don't know how to set up a tool so that the Makefile doesn't traverse the entire llvm and clang directories each time that I compile my tool (traversing the Makefile seems to take about as long as building my single file plugin).
Thanks.
If you are using the CMake build system (which you should be with reset LLVM), then CMake creates special targets to bypass dependency checks.
If you target name is target, call:
make target/fast
And then the Makefile will not check/rebuild all the dependent libraries. There is also a make install/fast.

How to add a X86 backend pass in LLVM without having to rebuild all sorts of shared libraries, including clang?

How to add a X86 backend pass in LLVM without having to rebuild all sorts of shared libraries, including clang?
After having studied the LLVM "middle-end" for a while (IR/opt), I've decided to move into the backend.
Here, I've created a very simply MachineFunctionPass in .../lib/Target/X86/, which compiles just fine.
Now, from what I understand, I cannot just load the LLVM backend pass using llc, like it was possible for opt w.r.t. IR-passes. Instead, I've have to build the new pass into the LLVM infrastructure, so I issue the command cmake . --build from my llvm/build directory.
This has the effect that all sorts of shared libraries are build again, including all kinds of clang specific .so files.
The problem is that this take more than 30 minutes, so this cannot be the correct way of building a simple backend pass. How do I quickly configure and build the pass into LLVM, so that I can use it with llc?
I am not an expert in LLVM, but this seems to work for me:
Go the LLVM build directory: cd ~/llvm/build/.
Proceed to the llc build directory: cd tools/llc/.
Build llc: cmake --build.
Now, instead of using the topmost cmake build script, which causes the entire project to be build (changed files and dependencies), only the llc tool is build.
I haven't tested this thoroughly.
Slightly different from the above post:
cmake -G Ninja ...
to configure llvm.
Then
ninja llc
should work fine.

Is there way to package Tensorflow for c++ api?

I've been developing c++ project using a Tensorflow c++ api. it just execute created tensorflow's graph from Python. I build it using bazel with Tensorflow code now. But I think it's inefficient way.
I want just Tensorflow library and header files, and Just compile my project only using Cmake.
I know how to build shared library.
bazel build -c opt --config=cuda //tensorflow:libtensorflow.so but this command just make a libtensorflow.so file. I can't find header files for build my project.
Is there way to package tensorflow library for c++? such as mvn package command.
As far as I know, there is no official distributable C++ API package. There is, however, tensorflow_cc project that builds and installs TF C++ API for you, along with convenient CMake targets you can link against. According to your description, that may be just what you need.
If your operating system is Debian or Ubuntu, you can download unofficial prebuilt packages with the Tensorflow C/C++ libraries. This distribution can be used for C/C++ inference with CPU, GPU support is not included:
https://github.com/kecsap/tensorflow_cpp_packaging/releases
There are instructions written how to freeze a checkpoint in Tensorflow (TFLearn) and load this model for inference with the C/C++ API:
https://github.com/kecsap/tensorflow_cpp_packaging/blob/master/README.md
Beware: I am the developer of this Github project.
As Floop already mentioned, his tensorflow_cc project is also a good alternative without packaging, especially if you want GPU support for the inference.
You can build tensorflow with CMake. This also creates a TensorflowConfig.cmake, which you can integrate in your project
https://github.com/tensorflow/tensorflow/tree/master/tensorflow/contrib/cmake
Little hint: You have to build the shared lib, even if you do not need it.
You have two option: static linking and dynamic linking.
If you want to dynamic link your c++ project to TensorFlow, all you need is a --whole-archive linker flag. The necessary header files are provided by a pip install.
Generating the library is basically
bazel build -c opt --copt=-mfpmath=both --config=cuda //tensorflow:libtensorflow.so
bazel build -c opt --copt=-mfpmath=both --config=cuda //tensorflow:libtensorflow_cc.so
Having everything in place it is easy to run a TensorFlow graph in C, C++, Go (GitHub project). See the linked project for these working examples in C, C++, Go.
When building against the shared library, the headers I use are in $PROJECT_HOME/bazel-genfiles.
Adding $PROJECT_HOME/bazel-genfiles to the linker header list should be enough.

How to run C++ library with OpenCV on the other computer (linux)?

I wrote a small project using C++, OpenCV 2.2 and g++ in Ubuntu 11.04. I need to make a library (.so would be better), but I want it to run on the other computer, without OpenCV installed.
I've tried to build dynamic library using -shared and -fPIC flags for g++, and copied OpenCV .so libs to the working directory. Actually I need only core and feature2d, but actually it requested lot's of other libs, including highgui, which also has many dependencies.
I tried static linking, using -Wl,-Bstatic flags, but also unsuccessfully.
Did someone has the same problems? I would appreciate any kind of help.
It is possible to build OpenCV without dependencies from system libraries. To turn of all the dependencies for OpenCV 2.2 on Linux you can run cmake with following arguments:
cmake -DWITH_1394=OFF -DWITH_CUDA=OFF -DWITH_EIGEN2=OFF -DWITH_FFMPEG=OFF -DWITH_GSTREAMER=OFF -DWITH_GTK=OFF -DWITH_OPENEXR=OFF -DWITH_PVAPI=OFF -DWITH_QT=OFF -DWITH_TBB=OFF -DWITH_UNICAP=OFF -DWITH_V4L=OFF -DWITH_XINE=OFF -DUSE_IPP=OFF -DOPENCV_BUILD_3RDPARTY_LIBS=ON ..
But in this case you will not be able to use many of functions form highgui module:
video reading and writing
working with camera
all functions working with GUI (like imshow)