Page MenuHomePhabricator

Add a module pass for order file instrumentation
ClosedPublic

Authored by manmanren on Jan 30 2019, 9:53 AM.

Details

Summary

The basic idea of the pass is to use a circular buffer to log the execution ordering of the functions. We only log the function when it is first executed. We use a 8-byte hash to log the function symbol name.

In this pass, we add three global variables:
(1) an order file buffer: a circular buffer at its own llvm section.
(2) a bitmap for each module: one byte for each function to say if the function is already executed.
(3) a global index to the order file buffer.

At the function prologue, if the function has not been executed (by checking the bitmap), log the function hash, then atomically increase the index.

Diff Detail

Repository
rL LLVM

Event Timeline

manmanren created this revision.Jan 30 2019, 9:53 AM

Have not looked at it in details, but it looks like the support for the new PM is missing. Can that be added?

There is a copy of InstrProfData.inc in compiler-rt, and that one needs to be kept in sync with the one in compiler side.

include/llvm/ProfileData/InstrProfData.inc
682 ↗(On Diff #184316)

should probably remove mentioning of Pika here.

696 ↗(On Diff #184316)

Same here.

748 ↗(On Diff #184316)

the #ifndef is not needed. The whole macro definition section is already preprocessing guarded.

754 ↗(On Diff #184316)

The #else part is not needed either.

lib/Transforms/Instrumentation/InstrOrderFile.cpp
39 ↗(On Diff #184316)

Why is it called 'write mapping'? Sounds a little confusing.

42 ↗(On Diff #184316)

Since mapping file is already needed, should this option has a default value?

manmanren updated this revision to Diff 184626.Jan 31 2019, 3:16 PM

Add support for the new pass manager

manmanren updated this revision to Diff 184631.Jan 31 2019, 3:18 PM

Address some review comments

davidxl added inline comments.Jan 31 2019, 4:26 PM
lib/Passes/PassBuilder.cpp
751 ↗(On Diff #184631)

The pass should probably be moved after partial inlining and eliminateAvailableExternallyPass. Partial inlining may create new functions, and it is also unnecessary to instrument availableExternally functions.

lib/Transforms/Instrumentation/InstrOrderFile.cpp
80 ↗(On Diff #184631)

Should define macro in InstProfData.inc file.

The name of the symbol is also too generic. Need to prefix with __llvm_...

87 ↗(On Diff #184631)

Same here.

147 ↗(On Diff #184631)

Why not having a runtime interface to flush the buffer here?

manmanren updated this revision to Diff 185799.Feb 7 2019, 10:13 AM

I think I have addressed most of the comments. There is one comment which I am not sure.

Thanks for reviewing David!

Manman

manmanren marked an inline comment as done.Feb 7 2019, 10:16 AM
manmanren added inline comments.
lib/Transforms/Instrumentation/InstrOrderFile.cpp
147 ↗(On Diff #184631)

Can you give an example of a runtime interface? Are you referring to helpers in another file maybe InstrProfiling.cpp?

davidxl added inline comments.Feb 8 2019, 9:01 AM
lib/Transforms/Instrumentation/InstrOrderFile.cpp
147 ↗(On Diff #184631)

something in compiler-rt like '__llvm_profile_orderfile_buffer_flush' which append the buffer data when the buffer overflows.

This can be done later if it is a needed in practice.

davidxl accepted this revision.Feb 8 2019, 9:12 AM

lgtm.

LLVM part looks ok. Do you have plan to hook it up with Clang driver? It is also good to add support in llvm-profdata to support converting the raw mapping+dumping into linker order file format.

This revision is now accepted and ready to land.Feb 8 2019, 9:12 AM
This revision was automatically updated to reflect the committed changes.
Herald added a project: Restricted Project. · View Herald TranscriptFeb 28 2019, 12:13 PM

This broke the windows LLDB bot:

http://lab.llvm.org:8011/builders/lldb-x64-windows-ninja/builds/2182

It looks like it's not the only broken one either.

thakis added a subscriber: thakis.Jun 16 2021, 7:01 PM

Hi,

is there any documentation on how to use this? I see that clang has a -forder-file-instrumentation switch to enable this. But what do I do after I enabled it? Run the instrumented program and then…? From the name, I'm guessing maybe the idea is that this will produce an order file?

Part of why I'm asking is because I'm trying to teach lld/mac about section$start$ symbols, but with a candidate patch I'm still getting undefined symbol: section$start$__DATA$__llvm_orderfile. It's well possible my patch is wrong, but I don't see what's supposed to create the __llvm_orderfile section (https://bugs.chromium.org/p/chromium/issues/detail?id=1220128#c10 has the candidate patch).

Thanks,
Nico

is there any documentation on how to use this? I see that clang has a -forder-file-instrumentation switch to enable this. But what do I do after I enabled it? Run the instrumented program and then…? From the name, I'm guessing maybe the idea is that this will produce an order file?

Looks like compiler-rt/test/profile/instrprof-order-file.test gives a demo for how this is supposed to be used: clang -forder-file-instrumentation -O1 compiler-rt/test/profile/Inputs/instrprof-order-file-2.c compiler-rt/test/profile/Inputs/instrprof-order-file.c -mllvm -orderfile-write-mapping="mapping.txt", i.e. it needs an -mllvm flag to make it have an effect.

is there any documentation on how to use this? I see that clang has a -forder-file-instrumentation switch to enable this. But what do I do after I enabled it? Run the instrumented program and then…? From the name, I'm guessing maybe the idea is that this will produce an order file?

I don't think I wrote a doc on this. Yes 'compiler-rt/test/profile/instrprof-order-file.test' shows how to use it, build the app with -forder-file-instrumentation and -mllvm -orderfile-write-mapping, then run the app, use the mapping file to decode the profile.

The profile should be in default.profraw.order. But you can change its name via:

  • Writes to the file with the last name given to \a *
  • __llvm_profile_set_filename(),
  • or if it hasn't been called, the \c LLVM_PROFILE_FILE environment variable,
  • or if that's not set, the last name set to INSTR_PROF_PROFILE_NAME_VAR,
  • or if that's not set, \c "default.profraw".

Hope this helps!
Manman

Looks like compiler-rt/test/profile/instrprof-order-file.test gives a demo for how this is supposed to be used: clang -forder-file-instrumentation -O1 compiler-rt/test/profile/Inputs/instrprof-order-file-2.c compiler-rt/test/profile/Inputs/instrprof-order-file.c -mllvm -orderfile-write-mapping="mapping.txt", i.e. it needs an -mllvm flag to make it have an effect.

Hi,

is there any documentation on how to use this? I see that clang has a -forder-file-instrumentation switch to enable this. But what do I do after I enabled it? Run the instrumented program and then…? From the name, I'm guessing maybe the idea is that this will produce an order file?

Part of why I'm asking is because I'm trying to teach lld/mac about section$start$ symbols, but with a candidate patch I'm still getting undefined symbol: section$start$__DATA$__llvm_orderfile. It's well possible my patch is wrong, but I don't see what's supposed to create the __llvm_orderfile section (https://bugs.chromium.org/p/chromium/issues/detail?id=1220128#c10 has the candidate patch).

Thanks,
Nico