@@ -37,6 +37,20 @@ class BalancedPartitioningTest : public ::testing::Test {
3737 Ids.push_back (N.Id );
3838 return Ids;
3939 }
40+
41+ static testing::Matcher<BPFunctionNode> NodeIdIs (BPFunctionNode::IDT Id) {
42+ return Field (" Id" , &BPFunctionNode::Id, Id);
43+ };
44+
45+ static testing::Matcher<BPFunctionNode>
46+ NodeBucketIs (std::optional<uint32_t > Bucket) {
47+ return Field (" Bucket" , &BPFunctionNode::Bucket, Bucket);
48+ };
49+
50+ static testing::Matcher<BPFunctionNode>
51+ NodeIs (BPFunctionNode::IDT Id, std::optional<uint32_t > Bucket) {
52+ return AllOf (NodeIdIs (Id), NodeBucketIs (Bucket));
53+ };
4054};
4155
4256TEST_F (BalancedPartitioningTest, Basic) {
@@ -48,11 +62,6 @@ TEST_F(BalancedPartitioningTest, Basic) {
4862
4963 Bp.run (Nodes);
5064
51- auto NodeIs = [](BPFunctionNode::IDT Id, std::optional<uint32_t > Bucket) {
52- return AllOf (Field (" Id" , &BPFunctionNode::Id, Id),
53- Field (" Bucket" , &BPFunctionNode::Bucket, Bucket));
54- };
55-
5665 EXPECT_THAT (Nodes,
5766 UnorderedElementsAre (NodeIs (0 , 0 ), NodeIs (1 , 1 ), NodeIs (2 , 2 ),
5867 NodeIs (3 , 3 ), NodeIs (4 , 4 )));
@@ -79,8 +88,7 @@ TEST_F(BalancedPartitioningTest, Large) {
7988
8089 Bp.run (Nodes);
8190
82- EXPECT_THAT (
83- Nodes, Each (Not (Field (" Bucket" , &BPFunctionNode::Bucket, std::nullopt ))));
91+ EXPECT_THAT (Nodes, Each (Not (NodeBucketIs (std::nullopt ))));
8492 EXPECT_THAT (getIds (Nodes), UnorderedElementsAreArray (OrigIds));
8593}
8694
@@ -97,4 +105,55 @@ TEST_F(BalancedPartitioningTest, MoveGain) {
97105 30 .f );
98106}
99107
108+ TEST_F (BalancedPartitioningTest, Weight1) {
109+ std::vector<BPFunctionNode::UtilityNodeT> UNs = {
110+ BPFunctionNode::UtilityNodeT (0 , 100 ),
111+ BPFunctionNode::UtilityNodeT (1 , 100 ),
112+ BPFunctionNode::UtilityNodeT (2 , 100 ),
113+ BPFunctionNode::UtilityNodeT (3 , 1 ),
114+ BPFunctionNode::UtilityNodeT (4 , 1 ),
115+ };
116+ std::vector<BPFunctionNode> Nodes = {
117+ BPFunctionNode (0 , {UNs[0 ], UNs[3 ]}), BPFunctionNode (1 , {UNs[1 ], UNs[3 ]}),
118+ BPFunctionNode (2 , {UNs[2 ], UNs[3 ]}), BPFunctionNode (3 , {UNs[0 ], UNs[4 ]}),
119+ BPFunctionNode (4 , {UNs[1 ], UNs[4 ]}), BPFunctionNode (5 , {UNs[2 ], UNs[4 ]}),
120+ };
121+
122+ Bp.run (Nodes);
123+
124+ // Check that nodes that share important UNs are ordered together
125+ auto NodesRef = ArrayRef (Nodes);
126+ auto Groups = {NodesRef.slice (0 , 2 ), NodesRef.slice (2 , 2 ),
127+ NodesRef.slice (4 , 2 )};
128+ EXPECT_THAT (Groups, UnorderedElementsAre (
129+ UnorderedElementsAre (NodeIdIs (0 ), NodeIdIs (3 )),
130+ UnorderedElementsAre (NodeIdIs (1 ), NodeIdIs (4 )),
131+ UnorderedElementsAre (NodeIdIs (2 ), NodeIdIs (5 ))));
132+ }
133+
134+ TEST_F (BalancedPartitioningTest, Weight2) {
135+ std::vector<BPFunctionNode::UtilityNodeT> UNs = {
136+ BPFunctionNode::UtilityNodeT (0 , 1 ),
137+ BPFunctionNode::UtilityNodeT (1 , 1 ),
138+ BPFunctionNode::UtilityNodeT (2 , 1 ),
139+ BPFunctionNode::UtilityNodeT (3 , 100 ),
140+ BPFunctionNode::UtilityNodeT (4 , 100 ),
141+ };
142+ std::vector<BPFunctionNode> Nodes = {
143+ BPFunctionNode (0 , {UNs[0 ], UNs[3 ]}), BPFunctionNode (1 , {UNs[1 ], UNs[4 ]}),
144+ BPFunctionNode (2 , {UNs[2 ], UNs[3 ]}), BPFunctionNode (3 , {UNs[0 ], UNs[4 ]}),
145+ BPFunctionNode (4 , {UNs[1 ], UNs[3 ]}), BPFunctionNode (5 , {UNs[2 ], UNs[4 ]}),
146+ };
147+
148+ Bp.run (Nodes);
149+
150+ // Check that nodes that share important UNs are ordered together
151+ auto NodesRef = ArrayRef (Nodes);
152+ auto Groups = {NodesRef.slice (0 , 3 ), NodesRef.slice (3 , 3 )};
153+ EXPECT_THAT (Groups,
154+ UnorderedElementsAre (
155+ UnorderedElementsAre (NodeIdIs (0 ), NodeIdIs (2 ), NodeIdIs (4 )),
156+ UnorderedElementsAre (NodeIdIs (1 ), NodeIdIs (3 ), NodeIdIs (5 ))));
157+ }
158+
100159} // end namespace llvm
0 commit comments