-
Notifications
You must be signed in to change notification settings - Fork 111
/
llvm_propeller_code_layout_scorer.cc
54 lines (44 loc) · 2.15 KB
/
llvm_propeller_code_layout_scorer.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include "llvm_propeller_code_layout_scorer.h"
#include <algorithm>
#include <cstdlib>
#include "llvm_propeller_options.pb.h"
namespace devtools_crosstool_autofdo {
// The ext-tsp score calculation [1] is described as follows:
// 1- If edge is a fallthrough:
// edge.weight_ * fallthrough_weight
// 2- If edge is a forward jump:
// edge.weight_ * forward_jump_weight *
// (1 - src_sink_distance / forward_jump_distance)
// 3- If edge is a backward jump:
// edge.weight_ * backward_jump_weight *
// (1 - src_sink_distance / backward_jump_distance)
//
// [1] Newell A, Pupyrev S. Improved basic block reordering.
// IEEE Transactions on Computers. 2020 Mar 30;69(12):1784-94.
PropellerCodeLayoutScorer::PropellerCodeLayoutScorer(
const PropellerCodeLayoutParameters ¶ms)
: code_layout_params_(params) {}
// Returns the score for one edge, given its source to sink direction and
// distance in the layout.
double PropellerCodeLayoutScorer::GetEdgeScore(const CFGEdge &edge,
int src_sink_distance) const {
// Approximate callsites to be in the middle of the source basic block.
if (edge.IsCall()) src_sink_distance += edge.src()->size() / 2;
if (edge.IsReturn()) src_sink_distance += edge.sink()->size() / 2;
if (src_sink_distance == 0 && edge.IsBranchOrFallthrough())
return edge.weight() * code_layout_params_.fallthrough_weight();
double absolute_src_sink_distance =
static_cast<double>(std::abs(src_sink_distance));
if (src_sink_distance > 0 &&
absolute_src_sink_distance < code_layout_params_.forward_jump_distance())
return edge.weight() * code_layout_params_.forward_jump_weight() *
(1.0 - absolute_src_sink_distance /
code_layout_params_.forward_jump_distance());
if (src_sink_distance < 0 &&
absolute_src_sink_distance < code_layout_params_.backward_jump_distance())
return edge.weight() * code_layout_params_.backward_jump_weight() *
(1.0 - absolute_src_sink_distance /
code_layout_params_.backward_jump_distance());
return 0;
}
} // namespace devtools_crosstool_autofdo