Clang does not produce instrumentation results

I am trying to instrument a very simple code with clang. The code is as following:

#include <iostream>
int sub(int x) { return x - 2;}
int main() {
   return 0;

I try to compile the code with clang++-12 -O0 ./test.cpp -Xclang -fprofile-instrument=llvm -Xclang -fprofile-instrument-path=./test.profraw. The compilation works and generates an object file with the following symbols:

00000000004002a8 l    d  .interp	0000000000000000              .interp
00000000004002c4 l    d	0000000000000000    
00000000004002e8 l    d  .note.ABI-tag	0000000000000000              .note.ABI-tag
0000000000400308 l    d  .gnu.hash	0000000000000000              .gnu.hash
0000000000400338 l    d  .dynsym	0000000000000000              .dynsym
0000000000400428 l    d  .dynstr	0000000000000000              .dynstr
0000000000400530 l    d  .gnu.version	0000000000000000              .gnu.version
0000000000400548 l    d  .gnu.version_r	0000000000000000              .gnu.version_r
0000000000400588 l    d  .rela.dyn	0000000000000000              .rela.dyn
00000000004005d0 l    d  .rela.plt	0000000000000000              .rela.plt
0000000000401000 l    d  .init	0000000000000000              .init
0000000000401020 l    d  .plt	0000000000000000              .plt
0000000000401090 l    d  .text	0000000000000000              .text
00000000004012f8 l    d  .fini	0000000000000000              .fini
0000000000402000 l    d  .rodata	0000000000000000              .rodata
0000000000402034 l    d  __llvm_prf_names	0000000000000000              __llvm_prf_names
0000000000402094 l    d  .eh_frame_hdr	0000000000000000              .eh_frame_hdr
00000000004020e8 l    d  .eh_frame	0000000000000000              .eh_frame
0000000000403dd8 l    d  .init_array	0000000000000000              .init_array
0000000000403de8 l    d  .fini_array	0000000000000000              .fini_array
0000000000403df0 l    d  .dynamic	0000000000000000              .dynamic
0000000000403ff0 l    d  .got	0000000000000000              .got
0000000000404000 l    d  .got.plt	0000000000000000              .got.plt
0000000000404048 l    d  .data	0000000000000000              .data
0000000000404058 l    d  __llvm_prf_cnts	0000000000000000              __llvm_prf_cnts
0000000000404078 l    d  __llvm_prf_data	0000000000000000              __llvm_prf_data
0000000000404140 l    d  .bss	0000000000000000              .bss
0000000000000000 l    d  .comment	0000000000000000              .comment
0000000000000000 l    df *ABS*	0000000000000000              test.cpp
00000000004010e0 l     F .text	000000000000001f              _GLOBAL__sub_I_test.cpp
0000000000404251 l     O .bss	0000000000000001              _ZStL8__ioinit
0000000000401090 l     F .text	000000000000004c              __cxx_global_var_init
0000000000000000 l    df *ABS*	0000000000000000              crtstuff.c
0000000000401140 l     F .text	0000000000000000              deregister_tm_clones
0000000000401170 l     F .text	0000000000000000              register_tm_clones
00000000004011b0 l     F .text	0000000000000000              __do_global_dtors_aux
0000000000404250 l     O .bss	0000000000000001              completed.0
0000000000403de8 l     O .fini_array	0000000000000000              __do_global_dtors_aux_fini_array_entry
00000000004011e0 l     F .text	0000000000000000              frame_dummy
0000000000403dd8 l     O .init_array	0000000000000000              __frame_dummy_init_array_entry
0000000000000000 l    df *ABS*	0000000000000000              crtstuff.c
000000000040222c l     O .eh_frame	0000000000000000              __FRAME_END__
0000000000000000 l    df *ABS*	0000000000000000              
0000000000402094 l       .eh_frame_hdr	0000000000000000              __GNU_EH_FRAME_HDR
0000000000403df0 l     O .dynamic	0000000000000000              _DYNAMIC
0000000000403de8 l       .init_array	0000000000000000              __init_array_end
0000000000403dd8 l       .init_array	0000000000000000              __init_array_start
0000000000404000 l     O .got.plt	0000000000000000              _GLOBAL_OFFSET_TABLE_
0000000000404138 g       __llvm_prf_data	0000000000000000              _edata
0000000000404048  w      .data	0000000000000000              data_start
0000000000402000 g     O .rodata	0000000000000004              _IO_stdin_used
0000000000401220 g     F .text	000000000000005c              main
0000000000401030       F *UND*	0000000000000000              _ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_@@GLIBCXX_3.4
0000000000404050 g     O .data	0000000000000000              .hidden __dso_handle
00000000004012f8 g     F .fini	0000000000000000              .hidden _fini
0000000000401130 g     F .text	0000000000000005              .hidden _dl_relocate_static_pie
0000000000000000       F *UND*	0000000000000000              __cxa_atexit@@GLIBC_2.2.5
0000000000402008 g     O .rodata	0000000000000008              __llvm_profile_raw_version
0000000000402010 g     O .rodata	0000000000000024              __llvm_profile_filename
0000000000401100 g     F .text	000000000000002f              _start
0000000000000000       F *UND*	0000000000000000              _ZNSolsEPFRSoS_E@@GLIBCXX_3.4
0000000000401000 g     F .init	0000000000000000              .hidden _init
0000000000404058 g     O __llvm_prf_cnts	0000000000000000              .hidden __TMC_END__
0000000000404140 g     O .bss	0000000000000110              _ZSt4cout@@GLIBCXX_3.4
0000000000404048 g       .data	0000000000000000              __data_start
0000000000404258 g       .bss	0000000000000000              _end
0000000000404138 g       .bss	0000000000000000              __bss_start
0000000000000000       F *UND*	0000000000000000              _ZNSt8ios_base4InitC1Ev@@GLIBCXX_3.4
0000000000401280 g     F .text	0000000000000065              __libc_csu_init
00000000004011f0 g     F .text	0000000000000023              _Z3subi
0000000000000000       F *UND*	0000000000000000              _ZNSolsEi@@GLIBCXX_3.4
00000000004012f0 g     F .text	0000000000000005              __libc_csu_fini
0000000000000000       F *UND*	0000000000000000              __libc_start_main@@GLIBC_2.2.5
0000000000000000  w      *UND*	0000000000000000              __gmon_start__
0000000000401080       F *UND*	0000000000000000              _ZNSt8ios_base4InitD1Ev@@GLIBCXX_3.4

From the symbols, it looks like it does work since it inserts __llvm_prf_data and the related functions for instrumentation. However, I can’t find the test.profraw file generated. Even if I pre-create the file, no content is written to the file.

I wonder if I miss anything in order to retrieve the instrumentation results. I appreciate any comment on this issue. Thank you so much!

Edit: I found that I can get the instrumentation results with clang++-12 -O0 /home/devkon/clang-exp/test.cpp -fprofile-generate=./test.profraw. I know that without -Xclang, we are using clang as a driver and it invokes a binary executable. However, what I would like to do is to use the clang frontend compiler for instrumentation. Is there anything I am missing? Or for the current situation the clang frontend compiler (compiler instance) simply doesn’t support the instrumentation?

So I figured out that both commands are correct. The reason that only the driver mode works is because the binary file generate by the frontend mode would not invoke the linker correctly. Only in the driver mode will the driver.cpp source code will help configure the correct arguments for the linker and thus produce the correct binary that will produce the instrumentation results.