Page MenuHomePhabricator

Propeller code layout optimizations
Needs ReviewPublic

Authored by rahmanl on Sep 25 2019, 11:48 PM.

Details

Reviewers
ruiu
espindola
Summary

Propeller code layout optimizations

This is part of the Propeller framework to do post link code layout optimizations. Please see the RFC here: https://groups.google.com/forum/#!msg/llvm-dev/ef3mKzAdJ7U/1shV64BYBAAJ and the detailed RFC doc here: https://github.com/google/llvm-propeller/blob/plo-dev/Propeller_RFC.pdf

This is the last patch in the series of patches for propeller and depends on the previous propeller patches (D68062, D68063, and D68065)

This patch allows Propeller to performs intra-function basic block reordering based on the extended TSP model (https://arxiv.org/abs/1809.04676) and function reordering based on the HFSort (https://dl.acm.org/citation.cfm?id=3049858). It also adds the capability of splitting functions.

Specifically, Propeller.cpp in D68062 passes on the generated CFG profiles to routines in PropellerBBReordering.cpp and PropellerFuncReordering.cpp to compute a global ordering of all basic blocks.

The high-level description of the BB reordering and Function reordering algorithms are as follows.

ExtTSP Basic Block Reordering Algorithm

The ExtTSP (extended TSP) metric provides a score for every ordering of basic blocks in a function, by combining the gains from fall-throughs and short jumps.
Given an ordering of the basic blocks, for a function f, the ExtTSP score is computed as follows:

Sum_{e in f} Freq(e) * weight(e),
where weight(e) is computed as follows:

  • 1 if Distance(Src[e] , Sink[e]) = 0 (i.e. fallthrough)
  • 0.1 * (1 - Distance(Src[e] , Sink[e]) /1024) if Src[e] < Sink[e] and 0 < Distance(Src[e], Sink[e]) < 1024 (i.e. short forward jump)
  • 0.1 * (1 - Distance(Src[e] , Sink[e]) /640) if Src[e] > Sink[e] and 0 < Distance(Src[e], Sink[e]) < 640 (i.e. short backward jump)
  • 0 otherwise

In short, it computes a weighted sum of frequencies of all edges in the control flow graph. Each edge gets its weight depending on whether the given ordering makes the edge a fallthrough, a short forward jump, or a short backward jump.

Although this problem is NP-hard like the regular TSP, an iterative greedy basic-block-chaining algorithm is used to find a close to optimal solution. This algorithm is described as follows.

Starting with one basic block sequence (BB chain) for every basic block, the algorithm iteratively joins BB chains together in order to maximize the extended TSP score of all the chains.

Initially, it finds all mutually-forced edges in the profiled CFG. These are all the edges which are -- based on the profile -- the only (executed) outgoing edge from their source node and the only (executed) incoming edges to their sink nodes. Next, the source and sink of all mutually-forced edges are attached together as fallthrough edges.

Then, at every iteration, the algorithm tries to merge a pair of BB chains which leads to the highest gain in the ExtTSP score. The algorithm extends the search space by considering splitting short (less than 128 bytes in binary size) BB chains into two chains and then merging these two chains with the other chain in four ways. After every merge, the new merge gains are updated. The algorithm repeats joining BB chains until no additional can be achieved. At this step, it sorts all the existing chains in decreasing order of their execution density, i.e., the total profiled frequency of the chain divided by its binary size.

HFSort Function Reordering Algorithm

The HFSort function reordering algorithm computes an optimal ordering. It goes through all functions in decreasing order of their execution density and for each one, finds its most likely caller (the function which calls it the most) and places the caller’s cluster right before the callee’s cluster. After all functions are considered, the HFSort algorithm orders all function clusters in decreasing order of their total execution density.

Function Splitting

After BB reordering and function reordering, for every function, Propeller goes through the BB chains in the computed BB order for that function, and for each chain, places the basic blocks of that chain at the hot or cold part of the layout depending on whether the chain’s total frequency is zero or not.
The ExtTSP BB reordering algorithm orders the BB chains decreasing order of their execution density. As a result, cold basic blocks are naturally placed at the end of the computed BB layout of every function.

Diff Detail

Event Timeline

rahmanl created this revision.Sep 25 2019, 11:48 PM
Herald added a project: Restricted Project. · View Herald Transcript
Bigcheese added inline comments.Sep 26 2019, 5:35 PM
lld/ELF/PropellerFuncOrdering.cpp
86

Could you use unique_ptr instead? I don't see where these are deleted.

111

Dead code.

117–118

Dead code.

121

This should just be a function call. Using operator overloading here gains very little, and costs losing a useful function name which had to be replaced with a comment.

ruiu added a comment.Sep 26 2019, 9:54 PM

Let me start with high-level comments:

  • Your code looks unnecessarily different from existing code from the coding style viewpoint. Please read other files and follow the same coding style.
  • Please run clang-format-diff to format this patch.
  • It lacks comments. In particular, it needs a file comment explaining the algorithm. Once you successfully explain the algorithm, each function/data should become more self-explanatory.
lld/ELF/PropellerBBReordering.cpp
2

Please add a file comment describing the algorithm. An example of it is https://github.com/llvm/llvm-project/blob/master/lld/ELF/ICF.cpp. Just like that file, please explain the algorithm.

26

Define lld::toString(NodeChain &) instead of overloading operator<<.

lld/ELF/PropellerBBReordering.h
2

Add the standard file header comment.

13–19

It is not permitted to import identifiers from std by the LLVM coding style.

21

Instead of doing this, add #include "lld/Common/LLVM.h" just like other files do.

36

We use // comments.

40

Is there any reason you can't use std::vector? std::list is generally slower than std::vector for most operations, though inserting/removing a new element in a middle of a long list is faster.

43

Please read the other files in the same directory and follow the same style, so that this code doesn't unecessarily look different. This should be uint32_t Size = 0;

110

Why do you need a virtual dtor?

lld/ELF/PropellerFuncOrdering.h
25

Do you need to define a dtor?

26–28

Please run clang-format-diff on this patch.

@Bigcheese Friendly ping from a poor soul coming from D46228:) (Since email ping+devmeeting ping does not work)

rahmanl updated this revision to Diff 222871.Oct 2 2019, 10:45 AM

This update addresses the previous comments including:

1- Adding standard header comments.
2- Adding more comments to the code.
3- Fixing variable and function names to lower case.
rahmanl marked 15 inline comments as done.Oct 2 2019, 10:47 AM
rahmanl updated this revision to Diff 222925.Oct 2 2019, 3:49 PM

This update adds one more test which exercises the different flavors of layout optimization in propeller by toggling reorder-bb, reorder-funcs, and split-funcs.

ruiu added inline comments.Oct 3 2019, 1:32 AM
lld/ELF/PropellerBBReordering.cpp
10

infrastructure

28

nit: add two spaces so that the second line of a list item is properly indented.

81–82

What function are you using from stdio.h?

121

You can use the foreach-style for loop here: for (std::pair<int64_t, std::unique_ptr<NodeChain>> Elem : Chains)

193

src is they are ...?

620–623

It took for a while to find this line. This is the output of this ordering algorithm, right? Is there any other output from this algorithm?

lld/ELF/PropellerBBReordering.h
45–49

Because they are always initialized by the constructor, they shouldn't have default values.

55–60

In C++, it is generally preferred to initialize members from the beginning using an initializer list instead of updating members after they are constructed. So,

NodeChain(const ELFCFGNode *Node) : DelegateNode(Node), Size(Node->ShSize), Freq(Node->Freq) {
  Nodes.push_back(Node);
}
66–68

Do you really have to define these functions, given that you can just access Nodes to do the same thing?

lld/ELF/PropellerFuncOrdering.cpp
3–10

Formatting doesn't seem correct.

rahmanl updated this revision to Diff 223990.Oct 8 2019, 11:05 PM

This update addresses previous comments from @ruiu.
Most importantly, it removes the propeller-align-basic-block functionality since it is not fit with the current code. I will add this functionality in a later patch.

rahmanl marked 6 inline comments as done.Oct 8 2019, 11:08 PM