Unable to perform Profile based optimization(PBO) in gcc - c++

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

Related

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

g++ -g option is not working only in makefile

Weirdly -g in my makefile is not working. Possibly its using different g++ version?
Here's my makefile
.default: all
all: linkedlist
clean:
rm -f linkedlist *.o
linkedlist: main.o list.o
g++ -Wall -Werror --std=c++14 -g -O -o $# $^
%.0: %.cpp
g++ -Wall -Werror --std=c++14 -g -O -c $^
Here's the output:
╰ make
c++ -c -o main.o main.cpp
c++ -c -o list.o list.cpp
g++ -Wall -Werror --std=c++14 -g -O -o linkedlist main.o list.o
But it doesn't work with my lldb. However if i generate it manually
g++ -Wall -Werror -g --std=c++14 -O -o linkedlist list.cpp main.cpp, my debugger works and I also notice that it generates linkedlist.dSYM folder (with the makefile it doesn't). I'm not sure but I think before I updated my xcode last week, I never saw .dSYM file when generating binaries with -g.
Any idea why?
Cheers
G++ Version:
╰ g++ --version
Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/c++/4.2.1
Apple clang version 11.0.3 (clang-1103.0.32.59)
Target: x86_64-apple-darwin19.4.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin
You have a mistake in your Makefile
%.0: %.cpp
should be
%.o: %.cpp
The letter 'o', not the number zero.
Because of this mistake you were using the default rules for compiling C++ code, and those didn't include the -g option, and potentially are using a different compiler, c++ vs g++.

Compiling with -static causes undefined references to functions in other libraries

I'm trying to statically link glibc in order to run my application on an older OS, but when I use the -static flag I get "undefined reference" errors for other libraries I'm using that I don't get without using -static. How do I fix this issue?
My Makefile produces the following commands:
g++ -static -Wall -O3 -w -std=c++11 -I/storage/home/PA/libs -I/storage/home/PA/libs/xerces -fopenmp -c Utilities.cpp
gcc -static -Wall -O3 -w -std=c++11 -I/storage/home/PA/libs -I/storage/home/PA/libs/xerces -fopenmp -c ccvt.c
gcc -static -Wall -O3 -w -std=c++11 -I/storage/home/PA/libs -I/storage/home/PA/libs/xerces -fopenmp -c client.c
g++ -static -Wall -O3 -w -std=c++11 -I/storage/home/PA/libs -I/storage/home/PA/libs/xerces -fopenmp -c XML_Params.cpp
g++ -static -Wall -O3 -w -std=c++11 -I/storage/home/PA/libs -I/storage/home/PA/libs/xerces -fopenmp -c main.cpp
g++ -static -Wall -O3 -std=c++11 -L/storage/home/PA/libs/gsl -fopenmp -lgsl -lgslcblas -lm -L/storage/home/PA/libs/xerces -lxerces-c -o App main.o Utilities.o XML_Params.o ccvt.o client.o
After the last line I get a huge wall of errors complaining about undefined references to Xerces and gsl functions. However, if I remove the -static from the makefile, everything builds fine. What is the proper way to link these libraries when I'm using -static?
according to gcc manual:
-llibrary
It makes a difference where in the command you write this option; the
linker searches and processes libraries and object files in the order
they are specified. Thus, foo.o -lz bar.o searches library z after
file foo.o but before bar.o. If bar.o refers to functions in z,
those functions may not be loaded.
Move -lxerces after *.o might solve your problem.
I think you don't need to add -static except for the last line, correct me if i'm wrong.

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?

Makefile Error - Linker input file unused

I'm trying to run a simple makefile that looks like this:
T=-ansi -pedantic -Wall -Werror
a.out: test.o extra.o
gcc $(T) -c test.o extra.o
test.o: test.c test.h
gcc $(T) -c test.c
extra.o: extra.c extra.h
gcc $(T) -c extra.c
clean:
rm *.o a.out
But I seem to be getting warnings telling me that "linker input file unused because linking not done"
I tried removing the "-c" from the a.out directive, after the gcc, but that produced to give me more problems. I'm not sure how to go about proceeding from here, any ideas/input would be much appreciated.
EDIT: I'm running the program by "make -T", also removing the -c from a.out, causes the error" invalid symbol index"
You need to remove the -c from the a.out command:
a.out: test.o extra.o
gcc $(T) test.o extra.o
or, better:
a.out: test.o extra.o
gcc $(T) -o $# test.o extra.o
or, still better:
extra: test.o extra.o
gcc $(T) -o $# test.o extra.o
The error message is normally because you specify something like -lm on a command line with -c, but you're not doing that here. OTOH, you are listing object files with the -c option — that'll generate the warning:
gcc -ansi -pedantic -Wall -Werror -c test.o extra.o
^ this one, here
Those .o files are the linker inputs, but you're not linking. Drop the -c and you will generate a.out. That should proceed OK.
I think this is the first time I've seen a makefile used to build a.out. It is unusual in the extreme — not precisely wrong, but definitely not the way you normally use make. It has built-in rules to build programs from single source files, such as example from example.c. Normally, you give a program a meaningful name based on one of the source files. Note that creating a program test is usually a bad idea; there is a standard test command built into the typical shell and confusion is rampant.