Get llvm IR after lto linking - llvm

Is there any way to get llvm IR after linking for lto? For example I have the following line:
$ clang -flto -O2 a.c main.c -fuse-ld=gold -v -save-temps
So I want to get llvm IR where file a.c and file main.c are linked in one monlithic.bc (or monlithic.o with IR). I tried to add option -Wl,-plugin-opt=save-temps but it occurs an error:
libLLVMLTO: Unknown command line argument 'save-temps'. Try:
'libLLVMLTO -help' clang: error: linker command failed with exit code
1 (use -v to see invocation)
And also is there any way to dump lto transformations of IR?

The problem was solved with newer linker and llvm (llvm-3.8 and binutils-2.25) in the following way:
$ ls
t1.c t2.c t2.h
$ clang -flto -O2 t1.c t2.c -v -fuse-ld=gold -save-temps
-Wl,-plugin-opt=save-temps -Wl,--verbose
...
$ ls
a.out a.out.bc a.out.o a.out.opt.bc t1.bc t1.c t1.i t1.o
t2.bc t2.c t2.h t2.i t2.o
$ llvm-dis a.out.bc
$ vim a.out.ll

Related

clang: error: unknown argument: '-fno-leading-underscore'

I am trying to compile my .cpp with -fno-leading-underscore option but it raises an error saying:
clang: error: unknown argument: '-fno-leading-underscore'
g++ -m32 -fno-use-cxa-atexit -nostdlib -fno-builtin -fno-rtti -fno-exceptions -fno-leading-underscore -o kernel.o -c kernel.cpp
How can I fix this I am new to Mac it used to work on Linux Mint
In your terminal, type g++ and press enter. You will probably get:
$ clang: No input files
As you can see its still clang underneath.
To fix this, first cd into /usr/local/bin and type ls. You will see the binaries inside:
g++-9
...
Next, create a symlink to it so that you can invoke g++ directly
ln -s g++-9 g++
If it still doesn't work for some reason, you can explicitly write g++-9 or whatever version you have. You can even give the full path /usr/local/bin/g++-9

SystemC on Mac error with Makefile

I need to start working on a project with SystemC. I managed to compile SystemC according to this instructions: how to use and install SystemC in terminal mac OS X?
Afterwards I adjusted the SYSTEMC_HOME variable in Makefile.config to "SYSTEMC_HOME?=~/Work/Other/systemc-2.3.1"
The problem occurs when i try to compile and run any of the provided examples.
The error I get is:
ld: unknown option: -rpath=/Users/admin/Work/Other/systemc-2.3.1/lib
clang: error: linker command failed with exit code 1
(use -void to see invocation)
The command I run is:
make -f Makefile run
And the whole output from the console:
simple_fifo admin$ make -f Makefile run
clang++ -fcolor-diagnostics -g -Wall -pedantic -Wno-long-long -Werror -L. -L.. -L /Users/admin/Work/Other/systemc-2.3.1/lib -Wl,-rpath=/Users/admin/Work/Other/systemc-2.3.1/lib -o simple_fifo.x simple_fifo.o -lsystemc -lm 2>&1 | c++filt
ld: unknown option: -rpath=/Users/admin/Work/Other/systemc-2.3.1/lib
clang: error: linker command failed with exit code 1 (use -void to see invocation)
make: *** [simple_fifo.x] Error 1
Any hints on what to look for would be highly appreciated.
= is a GNU linker feature, not Clang.
Edit examples/sysc/Makefile.rules and replace:
LDFLAG_RPATH ?= -Wl,-rpath=
with
LDFLAG_RPATH ?= -Wl,-rpath,

cc1plus: error: unrecognized command line option "-std=c++0x"

I am trying to make using g++. At first, I upgraded my gcc version by compiling the package locally, and add some environment path to my ~/.bashrc
alias gcc='/home/rescape/lib/bin/gcc'
alias g++='/home/rescape/lib/bin/g++'
export CC=/home/rescape/lib/bin/gcc
export CPP=/home/rescape/lib/bin/cpp
export CXX=/home/rescape/lib/bin/c++
And I try g++ -v in terminal:
[rescape#iZ231twjza6Z mxnet]$ g++ -v
Using built-in specs.
COLLECT_GCC=/home/rescape/lib/bin/g++
COLLECT_LTO_WRAPPER=/home/rescape/lib/libexec/gcc/x86_64-unknown-linux-gnu/4.8.0/lto-wrapper
Target: x86_64-unknown-linux-gnu
Configured with: ../configure --enable-checking=release --enable-languages=c,c++ --disable-multilib --prefix=/home/rescape/lib/
Thread model: posix
gcc version 4.8.0 (GCC)
Still, When I do make, such error message occurs:
[rescape#iZ231twjza6Z mxnet]$ make
g++ -std=c++0x -DMSHADOW_FORCE_STREAM -Wall -O3 -I./mshadow/ -I./dmlc-core/include -fPIC -Iinclude -msse3 -funroll-loops -Wno-unused-parameter -Wno-unknown-pragmas -DMSHADOW_USE_CUDA=0 -DMSHADOW_USE_CBLAS=1 -DMSHADOW_USE_MKL=0 -DMSHADOW_RABIT_PS=0 -DMSHADOW_DIST_PS=0 -DMXNET_USE_OPENCV=1 `pkg-config --cflags opencv` -fopenmp -MM -MT build/resource.o src/resource.cc >build/resource.d
cc1plus: error: unrecognized command line option "-std=c++0x"
make: *** [build/resource.o] Error 1
Any suggestions of how to fix this? Thanks!
According to this:
[rescape#iZ231twjza6Z mxnet]$ make
g++ ...
You not use CXX variable in your Makefile, so just replace g++ with CXX in your Makefile. aliases works only when you enter commands in your shell, if you type g++ something.cpp bash execute /home/bin/g++ something.cpp, that's all, bash aliasing not help if external process (in our case make) execute g++

Linking with llvm clang llvm ld main not found

$ cat test.c
int printf(const char *, ...);
int main() { printf("ok\n");}
$ clang -c test.c
$ llvm-ld test.o -o test /usr/lib/crt1.o -lSystem
$ ./test
'main' function not found in module.
$ ld test.o -o test /usr/lib/crt1.o -lSystem
$ ./test
ok
I am trying to compile the simple program with clang, and then link it with llvm-ld, my goal is to avoid GNU ld. I must be using it in a wrong way?
llvm-ld uses bitcode files as inputs to either get an executable or combine all input files into a new bitcode file. For object files you need to use lld.

How to replace llvm-ld with clang?

Summary: llvm-ld has been removed from the LLVM 3.2 release. I am trying to figure out how to use clang in its place in my build system.
Note that I figured out the answer to my own question while writing it but I am still posting it in case it is useful to anyone else. Alternative answers are also welcome.
Details:
I have a build process which first generates bitcode using clang++ -emit-llvm. Then I take the bitcode files and link them together with llvm-link. Then I apply some standard optimization passes with opt. Then I apply another custom compiler pass with opt. Then I apply the standard optimization passes again using opt a third time. Finally I take the output from the last run of opt and use llvm-link to link with appropriate libraries to generate my executable. When I tried to replace llvm-link with clang++ in this process I get the error message: file not recognized: File format not recognized
To make this question more concrete I created a simplified example of what I am trying to do. First there are two files that I want to compile and link together
test1.cpp:
#include <stdio.h>
int getNum();
int main()
{
int value = getNum();
printf("value is %d\n", value);
return 0;
}
test2.cpp
int getNum()
{
return 5;
}
I executed the following sequence of commands:
clang++ -emit-llvm -c test1.cpp test2.cpp
llvm-link -o test.bc1 test1.o test2.o
opt test.bc1 -o test.bc2 -std-compile-opts
(Note that I am currently running llvm 3.1, but I'm trying to figure out the steps that will work for llvm 3.2. I assume that I should be able to make the LLVM 3.1 version work correctly using clang instead of llvm-ld)
Then if I run:
llvm-ld test.bc2 -o a.out -native
everything is fine and a.out prints out 5.
However, if I run:
clang++ test.bc2 -o a.out
Then I get the error message:
test.bc2: file not recognized: File format not recognized clang-3:
error: linker command failed with exit code 1 (use -v to see invocation)
Obviously I know that I can produce an executable file by running clang directly on the .cpp files. But I'm wondering what the best way to integrate clang with opt is.
The test case described in the question can be compiled using the following steps:
clang++ -emit-llvm -c test1.cpp test2.cpp
llvm-link -o test.bc1 test1.o test2.o
opt test.bc1 -o test.bc2 -std-compile-opts
llc -filetype=obj test.bc2 -o test.o
clang++ test.o
This produces a working a.out file.
It seems that llc is needed to convert from bitcode to machine code which can then be processed by clang as it normally would.
In general I've found that
llvm-ld x.bc y.bc
can be replaced with
llc x.bc
llc y.bc
clang x.s y.s