How to compile spec_cpu2006 with self-defined IR in LLVM? - llvm

I have written an IR for tracking object address, now I need to compile spec_cpu2006 with my IR.
My running script for compiling general c++ code as follows:
Some variables
source_file = source + '.cpp'
obj = source + '.llvm'
opt_obj = source + '-opt.llvm'
executable = source + '-instrumented'
Running script
clang++ -std=c++11 -static -g -S -emit-llvm -o obj source_file
opt -disable-inlining -S -load=full_trace.so -fulltrace obj -o opt_obj
llvm-link -o full.llvm opt_obj + profile-func/trace_logger.llvm
llc -O0 -disable-fp-elim -filetype=asm -o full.s full.llvm
g++ -static -O0 -fno-inline -o ' + executable + ' full.s -lm -lz
./executable
Is there a way I can do this?

Related

Override -shared option in g++

I was building some Cython extensions, and have to link it against a static library (it has CUDA code in them, so have to be static):
running build_ext
building 'k3lib' extension
gcc -pthread -B /home/kelvin/anaconda3/envs/torch/compiler_compat -Wl,--sysroot=/ -Wsign-compare -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -fPIC -I/home/kelvin/repos/tools/include -I/home/kelvin/anaconda3/envs/torch/include/python3.8 -c main.cpp -o build/temp.linux-x86_64-3.8/main.o -O3 -march=native
cc1plus: warning: command line option ‘-Wstrict-prototypes’ is valid for C/ObjC but not for C++
g++ -pthread -shared -B /home/kelvin/anaconda3/envs/torch/compiler_compat -L/home/kelvin/anaconda3/envs/torch/lib -Wl,-rpath=/home/kelvin/anaconda3/envs/torch/lib -Wl,--no-as-needed -Wl,--sysroot=/ build/temp.linux-x86_64-3.8/main.o /home/kelvin/repos/tools/include/libk2.a -L/home/kelvin/repos/tools/include -lk2 -o build/lib.linux-x86_64-3.8/k3lib.cpython-38-x86_64-linux-gnu.so -static -Wl,-Bstatic -flinker-output=exec
However, Cython's g++ compile command includes the options -shared -fPIC by default. I tried a number of options at the end of the command via this setup file (the static library is at $(LOCAL_INCLUDE)/libk2.a):
includes = [os.getenv("LOCAL_INCLUDE")]
ext_modules = [
Extension("k3lib", sources=["main.pyx"],
libraries=["k2"], include_dirs=includes, library_dirs=includes, language="c++",
extra_compile_args=["-O3", "-march=native"], extra_objects=[f"{includes[0]}/libk2.a"],
extra_link_args=['-static', '-Wl,-Bstatic', '-flinker-output=exec'])
]
#extra_objects=[f"{includes[0]}/libk2.a"]
#extra_link_args=['-static']
setup(name="k3lib", ext_modules=cythonize(ext_modules, language_level="3"))
Still, g++ thinks that I want to build a shared library, and thus the error message. Is there a way to override the -shared option? I'm planning to go into Cython's files and edit them myself, but was wondering is there a simpler way?
Context: I was following this question on SO but can't replicate their success.

developerstudio12.6 CC on Solaris intel i386, compiler do not show errors or warnings but fails

Trying to compile C++ program using developerstudio12.6 CC on Solaris intel i386.
compiler do not show errors or warnings but fails.
Possible to figure why CC do not show any warnings or errors - what is possibly going wrong?
developerstudio12.6/bin/CC -mt -xtarget=native -m32 -g -errwarn=%all -O -DNDEBUG -c xml_test.cc -I<some include> -I<some other include> -o xml_test.o
echo $?
2
Using -verbose=template => similar output, no debug lines
Using -# or verbose=diags=> Huge amount of output but no error lines
### CC: Note: NLSPATH = `/opt/developerstudio12.6/bin/../lib/locale/%L/LC_MESSAGES/%N.cat:/opt/developerstudio12.6/bin/../../lib/locale/%L/LC_MESSAGES/%N.cat`
### command line files and options (expanded):
### -mt=yes -xtarget=native -xchip=broadwell -xcache=32/64/8/2:256/64/8/2:30720/64/20/24 -xarch=avx2_i -m32 -xdebuginfo=line,param,variable,tagtype,codetag,decl -xglobalize=yes -xpatchpadding=fix -Qoption driver -dconditional=yes -xkeep_unref=funcs,vars -verbose=diags -O3 -DNDEBUG -c xml_test.cc -I<some include> -I<some include> -oxml_test.o
/opt/developerstudio12.6/lib/compilers/bin/ccfe -D__SunOS_5_11 -D__SunOS_RELEASE=0x051100 -D__SUNPRO_CC=0x5150 -D__unix -D__SVR4__ -D__svr4__ -D__SVR4 -D__sun -D__sun__ -D__SunOS -D__i386 -D__i386__ -D__ORDER_LITTLE_ENDIAN__=1234 -D__ORDER_BIG_ENDIAN__=4321 -D__BYTE_ORDER__=__ORDER_LITTLE_ENDIAN__ -D__BUILTIN_VA_ARG_INCR -Dunix -Dsun -Di386 -D__FP_FAST_FMA__ -D__FP_FAST_FMAF__ -D_REENTRANT -D__SUN_PREFETCH -D__SUNPRO_CC_COMPAT=5 -I<some include> -I<some include> -I-xbuiltin -I/opt/developerstudio12.6/lib/compilers/include/CC/Cstd -I/opt/developerstudio12.6/lib/compilers/include/CC -I/opt/developerstudio12.6/lib/compilers/include/cc -DNDEBUG -ptf /tmp/ccfe.1620226479.15666.03.%1.%2 -ptx /opt/developerstudio12.6/bin/CC -ptk "-mt=yes -xtarget=native -m32 -xdebuginfo=line,param,decl,variable,tagtype,codetag -xglobalize=yes -xpatchpadding=fix -xkeep_unref=funcs,vars -verbose=diags -O3 -DNDEBUG -c -I<some include> -I<some include> " -compat=5 -xglobalize=yes -xdebuginfo=line,param,variable,tagtype,codetag,decl -instlib=/opt/developerstudio12.6/lib/compilers/libCstd.a -xdbggen=dwarf+usedonly+incl+line+param+variable+tagtype+codetag+decl -xF=%none -xbuiltin=%default -xldscope=global -xivdep=loop -xdepend -O3 -xarrayloc xml_test.cc -ptb xml_test.o -o /tmp/ccfe.1620226479.15666.01.ir 2> /tmp/ccfe.1620226479.15666.02.err
/opt/developerstudio12.6/lib/compilers/stdlibfilt -stderr < /tmp/ccfe.1620226479.15666.02.err
Your NLSPATH environment variable is incorrect.
/opt/developerstudio12.6/bin/../../lib/locale/%L/LC_MESSAGES/%N.cat refers to /opt/lib/locale/%L/LC_MESSAGES/%N.cat instead of /opt/developerstudio12.6/lib/locale/%L/LC_MESSAGES/%N.cat because you have an extra .. element in it.
I fixed the issue just a while back by adding -filt=%none to the compiler options:
-mt -xtarget=native -m32 -filt=%none -g2 -verbose=template -errtags -library=iostream

Linking object files to executable not working

I am trying to compile an example from dlib.net using g++. I find that directly compiling the example into an executable works fine using:
g++ -std=c++11 -O3 -I/usr/lib /usr/lib/dlib/all/source.cpp -lpthread -lX11 optimization_ex.cpp -o optimiation_ex
But when I compile the source into object files first (1) and link later (2),
g++ -std=c++11 -O3 -I/usr/lib -c /usr/lib/dlib/all/source.cpp -lpthread -lX11 -o /usr/lib/dlib/all/source.o
g++ -std=c++11 -O3 -I/usr/lib -c optimization_ex.cpp -lpthread -lX11 -o optimization_ex.o
g++ /usr/lib/dlib/all/source.o optimization_ex.o -o optimization_ex
Then the executable cannot be compiled and g++ complains about undefined references.
What is going on behind this behavior? And how can I link the executable from the object files?

R package with c++ code failed installation, No DLL was created

I'm currently working on an R package which uses C++ code and includes external libraries (dlib, boost and an optimization library developed in the group). We're using Rcpp to integrate R and C++, but the problem is the package always fails to compile, and none of the similar questions I've found out there have worked for me.
The report generated by R CMD check is:
* installing *source* package 'IRTppExperimental' ...
** libs
*** arch - i386
c:/Rtools/mingw_32/bin/g++ -std=c++0x -I"C:/PROGRA~1/R/R-33~1.1/include" -DNDEBUG -I"C:/Users/Camilo/Documents/R/win-library/3.3/Rcpp/include" -I"d:/Compiler/gcc-4.9.3/local330/include" -I../src/include -O2 -Wall -mtune=core2 -c RcppExports.cpp -o RcppExports.o
c:/Rtools/mingw_32/bin/g++ -std=c++0x -I"C:/PROGRA~1/R/R-33~1.1/include" -DNDEBUG -I"C:/Users/Camilo/Documents/R/win-library/3.3/Rcpp/include" -I"d:/Compiler/gcc-4.9.3/local330/include" -I../src/include -O2 -Wall -mtune=core2 -c rcpp_hello_world.cpp -o rcpp_hello_world.o
c:/Rtools/mingw_32/bin/g++ -std=c++0x -I"C:/PROGRA~1/R/R-33~1.1/include" -DNDEBUG -I"C:/Users/Camilo/Documents/R/win-library/3.3/Rcpp/include" -I"d:/Compiler/gcc-4.9.3/local330/include" -I../src/include -O2 -Wall -mtune=core2 -c test_multi.cpp -o test_multi.o
c:/Rtools/mingw_32/bin/g++ -shared -s -static-libgcc -o IRTppExperimental.dll tmp.def RcppExports.o rcpp_hello_world.o test_multi.o -LC:/PROGRA~1/R/R-33~1.1/bin/i386 -lRlapack -LC:/PROGRA~1/R/R-33~1.1/bin/i386 -lRblas IRTppExperimental.dll -L../src/include -Ld:/Compiler/gcc-4.9.3/local330/lib/i386 -Ld:/Compiler/gcc-4.9.3/local330/lib -LC:/PROGRA~1/R/R-33~1.1/bin/i386 -lR
g++.exe: error: IRTppExperimental.dll: No such file or directory
no DLL was created
ERROR: compilation failed for package 'IRTppExperimental'
* removing 'C:/Users/Camilo/Documents/UNAL/MIRT/Tests/Temps/IRTppExperimental.Rcheck/IRTppExperimental'
And the Makevars/Makevars.win files are as follows:
INCFOLDER = ../src/include
PKG_LIBS += $(LAPACK_LIBS) $(BLAS_LIBS) $(FPICFLAGS) $(SHLIB)
PKG_LIBS += -L$(INCFOLDER)
PKG_CXXFLAGS+=-I$(INCFOLDER)
CXXFLAGS+="-fno-stack-protector"
CXX_STD = CXX11
Lastly, the NAMESPACE file reads:
exportPattern("^[[:alpha:]]+")
importFrom(Rcpp,sourceCpp)
useDynLib(IRTppExperimental)
All the c++ source files are inside the src folder, including the external libraries. The package was created using the Rcpp.package.skeleton function, and used the compileAtributes function to create the RcppExports source files.
EDIT: As pointed out by Coatless, here is github link of the project: https://github.com/SICSresearch/IRTpp/tree/Uni-Multi-Merging

Unable to perform Profile based optimization(PBO) in gcc

I am unable to optimize my code with PBO options (-fprofile-generate & -fprofile-use) in gcc.
I am first compiling my code like following :
gcc -fprofile-generate test.c -o test
After generating profile data(.gcda file), I am re-compiling my code like following:
gcc -fprofile-use test.c -o test
But I am not able to see any difference in the assembly code between optimized and un-optimized code. Both looks same.
Should I also use optimization flags( e.g. -O1, -O2) along with -fprofile-generate/-fprofile-use to generate the optimized code.
I'm not sure, but if I remember correctly, PBO must be specified manually at the linkage:
gcc -O3 -fprofile-generate -c -MMD -MP -MF test.d -MT test.d -o test.o src/test.c
gcc -O3 -fprofile-generate -o test test.o
then:
gcc -O3 -fprofile-use -c -MMD -MP -MF test.d -MT test.d -o test.o src/test.c
gcc -O3 -fprofile-use -o test test.o