This is an archive of the discontinued LLVM Phabricator instance.

[MachO] Port call graph profile section and directive
ClosedPublic

Authored by lgrey on Oct 20 2021, 10:38 AM.

Details

Summary

This ports the .cg_profile assembly directive and call graph profile section generation to MachO from COFF/ELF. Due to MachO section naming rules, the section is called __LLVM,__cg_profile rather than .llvm.call-graph-profile as in COFF/ELF. Support for llvm-readobj is included to facilitate testing.

Corresponding LLD change is D112164

Diff Detail

Event Timeline

lgrey created this revision.Oct 20 2021, 10:38 AM
lgrey requested review of this revision.Oct 20 2021, 10:38 AM
Herald added a project: Restricted Project. · View Herald TranscriptOct 20 2021, 10:38 AM
lgrey edited the summary of this revision. (Show Details)Oct 21 2021, 9:42 AM
thakis accepted this revision.EditedJan 11 2022, 12:58 PM

Nice! lgtm. One bikeshed comment about naming below (in two places, but about the same name).

I'll note: It looks like there's no attribute to tell the linker "omit this section from the final executable" – but segment name __LLVM _almost_ has this effect. The exception is that if someone were to:

  • compile with -fprofile-use= (ie do a PGO build)
  • pass -Wl,-bitcode_process_mode,data to ld64
  • NOT pass -Wl,-dead_strip to ld64

…then they'd end up with an __LLVM,__cgprofile in their binary:

% ~/src/llvm-project/out/gn/bin/clang baz.o bar.o -O2 -isysroot $(xcrun -show-sdk-path) -Wl,-bitcode_process_mode,data -Wl,-dead_strip 
thakis@Nicos-MacBook-Pro pgotest % otool -vl a.out | grep __LLVM
thakis@Nicos-MacBook-Pro pgotest % ~/src/llvm-project/out/gn/bin/clang baz.o bar.o -O2 -isysroot -isysroot $(xcrun -show-sdk-path) -Wl,-bitcode_process_mode,data     
thakis@Nicos-MacBook-Pro pgotest % otool -vl a.out | grep -C2 __LLVM
      cmd LC_SEGMENT_64
  cmdsize 152
  segname __LLVM
   vmaddr 0x000000010000c000
   vmsize 0x0000000000004000
--
Section
  sectname __cg_profile
   segname __LLVM
      addr 0x000000010000c000
      size 0x0000000000000020

(.o files generated by adding -c to this line https://github.com/nico/hack/blob/main/pgotest/build.sh#L21)

This seems like a very unlikely sequence of events:

  • -bitcode_process_mode is undocumented
  • people who use flags as fancy as this likely also use -dead_strip

And there doesn't seem to be a better way to go about this – as far as I can tell, Mach-O doesn't have anything like IMAGE_SCN_LNK_REMOVE. Finally, the __cgprofile section is very small anyways (ref D112655).

llvm/lib/MC/MCMachOStreamer.cpp
539

(see below for name)

542

who owns this?

…oh, I see:
http://llvm-cs.pcc.me.uk/lib/MC/MCFragment.cpp#242

But is that owning?

http://llvm-cs.pcc.me.uk/include/llvm/MC/MCSection.h#50
http://llvm-cs.pcc.me.uk/include/llvm/ADT/ilist.h#307
http://llvm-cs.pcc.me.uk/include/llvm/ADT/ilist.h#41

Huh, subtle. Someone should probably change the API to use unique_ptrs one day. Bug I guess people familiar with these classes just know this.

llvm/lib/MC/MachObjectWriter.cpp
763

nit: Maybe call this CGProfileSection, else it looks very similar to Asm.CGProfile

(would also match COFF)

llvm/test/MC/MachO/cgprofile.s
45

nit: add newline

This revision is now accepted and ready to land.Jan 11 2022, 12:58 PM
lgrey updated this revision to Diff 399069.Jan 11 2022, 1:43 PM
lgrey marked 2 inline comments as done.

Variable renames, added missing newline

This revision was automatically updated to reflect the committed changes.