[PATCH] Use auto profiler to set branch weights

Diego Novillo dnovillo at google.com
Tue Oct 29 15:11:32 PDT 2013


Hi chandlerc,

This adds a new scalar pass that reads a file with samples generated
by 'perf' during runtime. The samples read from the profile are
incorporated and emmited as IR metadata reflecting that profile.

The profile file is assumed to have been generated by an external
profile source. The profile information is converted into IR metadata,
which is later used by the analysis routines to estimate block
frequencies, edge weights and other related data.

External profile information files have no fixed format, each profiler
is free to define its own. This includes both the on-disk representation
of the profile and the kind of profile information stored in the file.
A common kind of profile is based on sampling (e.g., perf), which
essentially counts how many times each line of the program has been
executed during the run.

The AutoProfile pass is organized as a scalar transformation. On
startup, it reads the file given in -auto-profile-file to determine what
kind of profile it contains.  This file is assumed to contain profile
information for the whole application. The profile data in the file is
read and incorporated into the internal state of the corresponding
profiler.

To facilitate testing, I've organized the profilers to support two file
formats: text and native. The native format is whatever on-disk
representation the profiler wants to support, I think this will mostly
be bitcode files, but it could be anything the profiler wants to
support. To do this, every profiler must implement the
AutoProfiler::loadNative() function.

The text format is mostly meant for debugging. Records are separated by
newlines, but each profiler is free to interpret records as it sees fit.
Profilers must implement the AutoProfiler::loadText() function.

Finally, the pass will call AutoProfiler::emitAnnotations() for each
function in the current translation unit. This function needs to
translate the loaded profile into IR metadata, which the analyzer will
later be able to use.

This patch implements the first steps towards the above design. I've
implemented a sample-based flat profiler. The format of the profile is
fairly simplistic. Each sampled function contains a list of relative
line locations (from the start of the function) together with a count
representing how many samples were collected at that line during
execution. I generate this profile using perf and a separate converter
tool.

Currently, I have only implemented a text format for these profiles. I
am interested in initial feedback to the whole approach before I send
the other parts of the implementation for review.

This patch implements:

- The AutoProfile pass.
- The base AutoProfiler class with the core interface.
- A SampleBasedProfiler using the above interface. The profiler
  generates metadata autoprofile.samples on every IR instruction that
  matches the profiles.
- A text loader class to assist the implementation of
  AutoProfiler::loadText().
- Basic unit test for the pass.

Additionally, the patch uses profile information to compute branch
weights based on instruction samples.

This patch converts instruction samples into branch weights. It
does a fairly simplistic conversion:

Given a multi-way branch instruction, it calculates the weight of
each branch based on the maximum sample count gathered from each
target basic block.

Note that this assignment of branch weights is somewhat lossy and can be
misleading. If a basic block has more than one incoming branch, all the
incoming branches will get the same weight. In reality, it may be that
only one of them is the most heavily taken branch.

I will adjust this assignment in subsequent patches.

http://llvm-reviews.chandlerc.com/D2058

Files:
  include/llvm/InitializePasses.h
  include/llvm/Transforms/Scalar.h
  lib/Transforms/Scalar/AutoProfile.cpp
  lib/Transforms/Scalar/CMakeLists.txt
  lib/Transforms/Scalar/Scalar.cpp
  test/Transforms/AutoProfile/basic.ll
  test/Transforms/AutoProfile/basic.prof
  test/Transforms/AutoProfile/branch.ll
  test/Transforms/AutoProfile/branch.prof
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D2058.1.patch
Type: text/x-patch
Size: 36968 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20131029/68af3400/attachment.bin>


More information about the llvm-commits mailing list