c++clangllvmpapi

Unable to link PAPI library with opt llvm


I am working on a project where I need to generate just the bitcode using clang, run some optimization passes using opt and then create an executable and measure its hardware counters.

I am able to link through clang directly using:

clang -g -O0 -w -I/opt/apps/papi/5.3.0/include -Wl,-rpath,$PAPI_LIB -L$PAPI_LIB \
    -lpapi /scratch/02681/user/papi_helper.c prog.c -o a.out

However now I want to link it after using the front end of clang and applying optimization passes using opt.

I am trying the following way:

clang -g -O0 -w -c -emit-llvm -I/opt/apps/papi/5.3.0/include -Wl,-rpath,$PAPI_LIB -L$PAPI_LIB \
    -lpapi /scratch/02681/user/papi_helper.c prog.c -o prog.o

llvm-link prog.o papi_helper.o -o prog-link.o

// run optimization passes
opt -licm prog-link.o -o prog-opt.o

llc -filetype=obj prog-opt.o -o prog-exec.o

clang prog-exec.o

After going through the above process I get the following error:

undefined reference to `PAPI_event_code_to_name'

It's not able to resolve papi functions. Thanks in advance for any help.


Solution

  • Clearly, you need to add -lpapi to the last clang invocation. How else the linker would know about libpapi?