Skip to content

Commit 508159e

Browse files
committed
Show ascii art of the CFG for cloning.
1 parent a7c64ca commit 508159e

File tree

3 files changed

+71
-40
lines changed

3 files changed

+71
-40
lines changed

llvm/include/llvm/CodeGen/BasicBlockSectionsProfileReader.h

Lines changed: 31 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,39 @@
2929
namespace llvm {
3030

3131
// This structure represents a unique ID for every block specified in the
32-
// profile.
32+
// input profile.
3333
struct ProfileBBID {
34+
// Basic block id associated with `MachineBasicBlock::BBID`.
3435
unsigned BBID;
36+
// The clone id associated with the block. This is zero for the original
37+
// block. For the cloned ones, it is equal to 1 + index of the associated
38+
// path in `RawFunctionProfile::ClonePaths`.
3539
unsigned CloneID;
3640
};
3741

42+
// This struct represents the cluster information for a machine basic block,
43+
// which is specifed by a unique ID. This templated struct is used for both the
44+
// raw input profile (as `BBProfle<ProfileBBID>`) and the processed profile
45+
// after applying the clonings (as `BBProfile<unsigned>`).
46+
template <typename BBIDType> struct BBProfile {
47+
// Basic block ID.
48+
BBIDType BasicBlockID;
49+
// Cluster ID this basic block belongs to.
50+
unsigned ClusterID;
51+
// Position of basic block within the cluster.
52+
unsigned PositionInCluster;
53+
};
54+
55+
// This represents the raw input profile for one function.
56+
struct RawFunctionProfile {
57+
// BB Cluster information specified by `ProfileBBID`s (before cloning).
58+
SmallVector<BBProfile<ProfileBBID>> RawBBProfiles;
59+
// Paths to clone. A path a -> b -> c -> d implies cloning b, c, and d along
60+
// the edge a -> b (a is not cloned). The index of the path in this vector
61+
// determines the `ProfileBBID::CloneID` of the cloned blocks in that path.
62+
SmallVector<SmallVector<unsigned>> ClonePaths;
63+
};
64+
3865
// Provides DenseMapInfo for ProfileBBID.
3966
template <> struct DenseMapInfo<ProfileBBID> {
4067
static inline ProfileBBID getEmptyKey() {
@@ -56,26 +83,6 @@ template <> struct DenseMapInfo<ProfileBBID> {
5683
}
5784
};
5885

59-
// This struct represents the cluster information for a machine basic block,
60-
// which is specifed by a unique ID.
61-
template <typename BBIDType> struct BBProfile {
62-
// Basic block ID.
63-
BBIDType BasicBlockID;
64-
// Cluster ID this basic block belongs to.
65-
unsigned ClusterID;
66-
// Position of basic block within the cluster.
67-
unsigned PositionInCluster;
68-
};
69-
70-
// This represents the profile for one function.
71-
struct RawFunctionProfile {
72-
// BB Cluster information specified by `ProfileBBID`s (before cloning).
73-
SmallVector<BBProfile<ProfileBBID>> RawBBProfiles;
74-
// Paths to clone. A path a -> b -> c -> d implies cloning b, c, and d along
75-
// the edge a -> b.
76-
SmallVector<SmallVector<unsigned>> ClonePaths;
77-
};
78-
7986
class BasicBlockSectionsProfileReader : public ImmutablePass {
8087
public:
8188
static char ID;
@@ -110,7 +117,7 @@ class BasicBlockSectionsProfileReader : public ImmutablePass {
110117
getRawProfileForFunction(StringRef FuncName) const;
111118

112119
// Initializes the FunctionNameToDIFilename map for the current module and
113-
// then reads the profile for matching functions.
120+
// then reads the profile for the matching functions.
114121
bool doInitialization(Module &M) override;
115122

116123
private:
@@ -150,7 +157,7 @@ class BasicBlockSectionsProfileReader : public ImmutablePass {
150157
// empty string if no debug info is available.
151158
StringMap<SmallString<128>> FunctionNameToDIFilename;
152159

153-
// This encapsulates the BB cluster information for the whole program.
160+
// This contains the BB cluster information for the whole program.
154161
//
155162
// For every function name, it contains the cloning and cluster information
156163
// for (all or some of) its basic blocks. The cluster information for every
@@ -159,7 +166,7 @@ class BasicBlockSectionsProfileReader : public ImmutablePass {
159166
StringMap<RawFunctionProfile> RawProgramProfile;
160167

161168
// Some functions have alias names. We use this map to find the main alias
162-
// name for which we have mapping in ProgramBBClusterInfo.
169+
// name which appears in RawProgramProfile as a key.
163170
StringMap<StringRef> FuncAliasMap;
164171
};
165172

llvm/lib/CodeGen/BasicBlockSections.cpp

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -315,13 +315,15 @@ bool BasicBlockSections::runOnMachineFunction(MachineFunction &MF) {
315315
MF.getName());
316316
if (!HasProfile)
317317
return true;
318-
// TODO: Apply the path cloning profile.
319318
for (const BBProfile<ProfileBBID> &BBP : RawProfile.RawBBProfiles) {
320-
assert(!BBP.BasicBlockID.CloneID && "Path cloning is not supported yet.");
321-
BBProfilesByBBID.try_emplace(BBP.BasicBlockID.BBID,
322-
BBProfile<unsigned>{BBP.BasicBlockID.BBID,
323-
BBP.ClusterID,
324-
BBP.PositionInCluster});
319+
// TODO: Apply the path cloning profile.
320+
assert(!BBP.BasicBlockID.CloneID && "Path cloning is not supported yet");
321+
const auto [I, Inserted] = BBProfilesByBBID.try_emplace(
322+
BBP.BasicBlockID.BBID,
323+
BBProfile<unsigned>{BBP.BasicBlockID.BBID, BBP.ClusterID,
324+
BBP.PositionInCluster});
325+
(void)I;
326+
assert(Inserted && "Duplicate BBID found in profile");
325327
}
326328
}
327329

llvm/lib/CodeGen/BasicBlockSectionsProfileReader.cpp

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -85,18 +85,39 @@ BasicBlockSectionsProfileReader::getRawProfileForFunction(
8585
// clone basic blocks along a path. The cloned blocks are then specified in the
8686
// cluster information.
8787
// The following profile lists two cloning paths (starting with 'p') for
88-
// function bar and places the total 11 blocks within two clusters. Each cloned
88+
// function bar and places the total 9 blocks within two clusters. Each cloned
8989
// block is identified by its original block id, along with its clone id. A
90-
// block cloned multiple times (2 in this example) appears with distinct clone
91-
// ids (2.1 and 2.2).
92-
// ---------------------------
90+
// block cloned multiple times appears with distinct clone ids. The CFG for bar
91+
// is shown below before and after cloning with final clusters labeled.
9392
//
9493
// f main
9594
// f bar
96-
// p 1 2 3
97-
// p 4 2 5
98-
// c 2 3 5 6 7
99-
// c 1 2.1 3.1 4 2.2 5.1
95+
// p 1 3 4
96+
// p 4 2
97+
// c 1 3.1 4.1 6
98+
// c 0 2 3 4 2.1 5
99+
// ****************************************************************************
100+
// function bar before and after cloning with basic block clusters shown.
101+
// ****************************************************************************
102+
// .... ..............
103+
// 0 -------+ : 0 :---->: 1 ---> 3.1 :
104+
// | | : | : :........ | :
105+
// v v : v : : v :
106+
// +--> 2 --> 5 1 ~~~~~~~> : 2 : : 4.1: clsuter 1
107+
// | | | : | : : | :
108+
// | v | : v ....... : v :
109+
// | 3 <------+ : 3 <--+ : : 6 :
110+
// | | : | | : :....:
111+
// | v : v | :
112+
// +--- 4 ---> 6 : 4 | :
113+
// : | | :
114+
// : v | :
115+
// :2.1---+ : cluster 2
116+
// : | ......:
117+
// : v :
118+
// : 5 :
119+
// ....
120+
// ****************************************************************************
100121
Error BasicBlockSectionsProfileReader::ReadV1Profile() {
101122
auto FI = RawProgramProfile.end();
102123

@@ -121,7 +142,7 @@ Error BasicBlockSectionsProfileReader::ReadV1Profile() {
121142
S.split(Values, ' ');
122143
switch (Specifier) {
123144
case '@':
124-
break;
145+
continue;
125146
case 'm': // Module name speicifer.
126147
if (Values.size() != 1) {
127148
return createProfileParseError(Twine("invalid module name value: '") +
@@ -169,7 +190,7 @@ Error BasicBlockSectionsProfileReader::ReadV1Profile() {
169190
// Skip the profile when we the profile iterator (FI) refers to the
170191
// past-the-end element.
171192
if (FI == RawProgramProfile.end())
172-
break;
193+
continue;
173194
// Reset current cluster position.
174195
CurrentPosition = 0;
175196
for (auto BasicBlockIDStr : Values) {
@@ -210,6 +231,7 @@ Error BasicBlockSectionsProfileReader::ReadV1Profile() {
210231
return createProfileParseError(Twine("invalid specifier: '") +
211232
Twine(Specifier) + "'");
212233
}
234+
llvm_unreachable("should not break from this switch statement");
213235
}
214236
return Error::success();
215237
}

0 commit comments

Comments
 (0)