@@ -73,64 +73,39 @@ cl::opt<bool> StaleMatchingJoinIslands(
7373
7474cl::opt<unsigned > StaleMatchingCostBlockInc (
7575 " stale-matching-cost-block-inc" ,
76- cl::desc (" The cost of increasing a block's count by one." ), cl::init(110 ),
76+ cl::desc (" The cost of increasing a block count by one." ), cl::init(150 ),
7777 cl::ReallyHidden, cl::cat(BoltOptCategory));
7878
7979cl::opt<unsigned > StaleMatchingCostBlockDec (
8080 " stale-matching-cost-block-dec" ,
81- cl::desc (" The cost of decreasing a block's count by one." ), cl::init(100 ),
81+ cl::desc (" The cost of decreasing a block count by one." ), cl::init(150 ),
8282 cl::ReallyHidden, cl::cat(BoltOptCategory));
8383
84- cl::opt<unsigned > StaleMatchingCostBlockEntryInc (
85- " stale-matching-cost-block-entry-inc" ,
86- cl::desc (" The cost of increasing the entry block's count by one." ),
87- cl::init(110 ), cl::ReallyHidden, cl::cat(BoltOptCategory));
88-
89- cl::opt<unsigned > StaleMatchingCostBlockEntryDec (
90- " stale-matching-cost-block-entry-dec" ,
91- cl::desc (" The cost of decreasing the entry block's count by one." ),
92- cl::init(100 ), cl::ReallyHidden, cl::cat(BoltOptCategory));
93-
94- cl::opt<unsigned > StaleMatchingCostBlockZeroInc (
95- " stale-matching-cost-block-zero-inc" ,
96- cl::desc (" The cost of increasing a count of zero-weight block by one." ),
97- cl::init(10 ), cl::Hidden, cl::cat(BoltOptCategory));
98-
99- cl::opt<unsigned > StaleMatchingCostBlockUnknownInc (
100- " stale-matching-cost-block-unknown-inc" ,
101- cl::desc (" The cost of increasing an unknown block's count by one." ),
102- cl::init(10 ), cl::ReallyHidden, cl::cat(BoltOptCategory));
103-
10484cl::opt<unsigned > StaleMatchingCostJumpInc (
10585 " stale-matching-cost-jump-inc" ,
106- cl::desc (" The cost of increasing a jump's count by one." ), cl::init(100 ),
86+ cl::desc (" The cost of increasing a jump count by one." ), cl::init(150 ),
10787 cl::ReallyHidden, cl::cat(BoltOptCategory));
10888
109- cl::opt<unsigned > StaleMatchingCostJumpFTInc (
110- " stale-matching-cost-jump-ft-inc" ,
111- cl::desc (" The cost of increasing a fall-through jump's count by one." ),
112- cl::init(100 ), cl::ReallyHidden, cl::cat(BoltOptCategory));
113-
11489cl::opt<unsigned > StaleMatchingCostJumpDec (
11590 " stale-matching-cost-jump-dec" ,
116- cl::desc (" The cost of decreasing a jump's count by one." ), cl::init(110 ),
91+ cl::desc (" The cost of decreasing a jump count by one." ), cl::init(150 ),
11792 cl::ReallyHidden, cl::cat(BoltOptCategory));
11893
119- cl::opt<unsigned > StaleMatchingCostJumpFTDec (
120- " stale-matching-cost-jump-ft-dec " ,
121- cl::desc (" The cost of decreasing a fall-through jump's count by one." ),
122- cl::init(110 ), cl::ReallyHidden, cl::cat(BoltOptCategory));
94+ cl::opt<unsigned > StaleMatchingCostBlockUnknownInc (
95+ " stale-matching-cost-block-unknown-inc " ,
96+ cl::desc (" The cost of increasing an unknown block count by one." ),
97+ cl::init(1 ), cl::ReallyHidden, cl::cat(BoltOptCategory));
12398
12499cl::opt<unsigned > StaleMatchingCostJumpUnknownInc (
125100 " stale-matching-cost-jump-unknown-inc" ,
126- cl::desc (" The cost of increasing an unknown jump's count by one." ),
127- cl::init(50 ), cl::ReallyHidden, cl::cat(BoltOptCategory));
101+ cl::desc (" The cost of increasing an unknown jump count by one." ),
102+ cl::init(140 ), cl::ReallyHidden, cl::cat(BoltOptCategory));
128103
129104cl::opt<unsigned > StaleMatchingCostJumpUnknownFTInc (
130105 " stale-matching-cost-jump-unknown-ft-inc" ,
131106 cl::desc (
132- " The cost of increasing an unknown fall-through jump's count by one." ),
133- cl::init(5 ), cl::ReallyHidden, cl::cat(BoltOptCategory));
107+ " The cost of increasing an unknown fall-through jump count by one." ),
108+ cl::init(3 ), cl::ReallyHidden, cl::cat(BoltOptCategory));
134109
135110} // namespace opts
136111
@@ -145,7 +120,8 @@ struct BlendedBlockHash {
145120 using ValueOffset = Bitfield::Element<uint16_t , 0 , 16 >;
146121 using ValueOpcode = Bitfield::Element<uint16_t , 16 , 16 >;
147122 using ValueInstr = Bitfield::Element<uint16_t , 32 , 16 >;
148- using ValueNeighbor = Bitfield::Element<uint16_t , 48 , 16 >;
123+ using ValuePred = Bitfield::Element<uint8_t , 48 , 8 >;
124+ using ValueSucc = Bitfield::Element<uint8_t , 56 , 8 >;
149125
150126public:
151127 explicit BlendedBlockHash () {}
@@ -154,7 +130,8 @@ struct BlendedBlockHash {
154130 Offset = Bitfield::get<ValueOffset>(Hash);
155131 OpcodeHash = Bitfield::get<ValueOpcode>(Hash);
156132 InstrHash = Bitfield::get<ValueInstr>(Hash);
157- NeighborHash = Bitfield::get<ValueNeighbor>(Hash);
133+ PredHash = Bitfield::get<ValuePred>(Hash);
134+ SuccHash = Bitfield::get<ValueSucc>(Hash);
158135 }
159136
160137 // / Combine the blended hash into uint64_t.
@@ -163,7 +140,8 @@ struct BlendedBlockHash {
163140 Bitfield::set<ValueOffset>(Hash, Offset);
164141 Bitfield::set<ValueOpcode>(Hash, OpcodeHash);
165142 Bitfield::set<ValueInstr>(Hash, InstrHash);
166- Bitfield::set<ValueNeighbor>(Hash, NeighborHash);
143+ Bitfield::set<ValuePred>(Hash, PredHash);
144+ Bitfield::set<ValueSucc>(Hash, SuccHash);
167145 return Hash;
168146 }
169147
@@ -175,7 +153,8 @@ struct BlendedBlockHash {
175153 " incorrect blended hash distance computation" );
176154 uint64_t Dist = 0 ;
177155 // Account for NeighborHash
178- Dist += NeighborHash == BBH.NeighborHash ? 0 : 1 ;
156+ Dist += SuccHash == BBH.SuccHash ? 0 : 1 ;
157+ Dist += PredHash == BBH.PredHash ? 0 : 1 ;
179158 Dist <<= 16 ;
180159 // Account for InstrHash
181160 Dist += InstrHash == BBH.InstrHash ? 0 : 1 ;
@@ -192,9 +171,10 @@ struct BlendedBlockHash {
192171 // / (Strong) Hash of the basic block instructions, including opcodes and
193172 // / operands.
194173 uint16_t InstrHash{0 };
195- // / Hash of the (loose) basic block together with (loose) hashes of its
196- // / successors and predecessors.
197- uint16_t NeighborHash{0 };
174+ // / (Loose) Hashes of the predecessors of the basic block.
175+ uint8_t PredHash{0 };
176+ // / (Loose) Hashes of the successors of the basic block.
177+ uint8_t SuccHash{0 };
198178};
199179
200180// / The object is used to identify and match basic blocks in a BinaryFunction
@@ -252,41 +232,43 @@ void BinaryFunction::computeBlockHashes() const {
252232
253233 std::vector<BlendedBlockHash> BlendedHashes (BasicBlocks.size ());
254234 std::vector<uint64_t > OpcodeHashes (BasicBlocks.size ());
255- // Initialize hash components
235+ // Initialize hash components.
256236 for (size_t I = 0 ; I < BasicBlocks.size (); I++) {
257237 const BinaryBasicBlock *BB = BasicBlocks[I];
258238 assert (BB->getIndex () == I && " incorrect block index" );
259239 BlendedHashes[I].Offset = BB->getOffset ();
260- // Hashing complete instructions
240+ // Hashing complete instructions.
261241 std::string InstrHashStr = hashBlock (
262242 BC, *BB, [&](const MCOperand &Op) { return hashInstOperand (BC, Op); });
263243 uint64_t InstrHash = std::hash<std::string>{}(InstrHashStr);
264- BlendedHashes[I].InstrHash = hash_64_to_16 (InstrHash);
265- // Hashing opcodes
266- std::string OpcodeHashStr =
267- hashBlock (BC, *BB, [](const MCOperand &Op) { return std::string (); });
244+ BlendedHashes[I].InstrHash = (uint16_t )hash_value (InstrHash);
245+ // Hashing opcodes.
246+ std::string OpcodeHashStr = hashBlockLoose (BC, *BB);
268247 OpcodeHashes[I] = std::hash<std::string>{}(OpcodeHashStr);
269- BlendedHashes[I].OpcodeHash = hash_64_to_16 (OpcodeHashes[I]);
248+ BlendedHashes[I].OpcodeHash = ( uint16_t ) hash_value (OpcodeHashes[I]);
270249 }
271250
272- // Initialize neighbor hash
251+ // Initialize neighbor hash.
273252 for (size_t I = 0 ; I < BasicBlocks.size (); I++) {
274253 const BinaryBasicBlock *BB = BasicBlocks[I];
275- uint64_t Hash = OpcodeHashes[I];
276- // Append hashes of successors
254+ // Append hashes of successors.
255+ uint64_t Hash = 0 ;
277256 for (BinaryBasicBlock *SuccBB : BB->successors ()) {
278257 uint64_t SuccHash = OpcodeHashes[SuccBB->getIndex ()];
279258 Hash = hashing::detail::hash_16_bytes (Hash, SuccHash);
280259 }
281- // Append hashes of predecessors
260+ BlendedHashes[I].SuccHash = (uint8_t )hash_value (Hash);
261+
262+ // Append hashes of predecessors.
263+ Hash = 0 ;
282264 for (BinaryBasicBlock *PredBB : BB->predecessors ()) {
283265 uint64_t PredHash = OpcodeHashes[PredBB->getIndex ()];
284266 Hash = hashing::detail::hash_16_bytes (Hash, PredHash);
285267 }
286- BlendedHashes[I].NeighborHash = hash_64_to_16 (Hash);
268+ BlendedHashes[I].PredHash = ( uint8_t ) hash_value (Hash);
287269 }
288270
289- // Assign hashes
271+ // Assign hashes.
290272 for (size_t I = 0 ; I < BasicBlocks.size (); I++) {
291273 const BinaryBasicBlock *BB = BasicBlocks[I];
292274 BB->setHash (BlendedHashes[I].combine ());
@@ -409,20 +391,22 @@ void matchWeightsByHashes(BinaryContext &BC,
409391 const FlowBlock *MatchedBlock = Matcher.matchBlock (YamlHash);
410392 if (MatchedBlock != nullptr ) {
411393 MatchedBlocks[YamlBB.Index ] = MatchedBlock;
412- LLVM_DEBUG (dbgs () << " Matched yaml block with bid = " << YamlBB.Index
413- << " and hash = " << Twine::utohexstr (YamlBB.Hash )
414- << " to BB with index = " << MatchedBlock->Index - 1
394+ BlendedBlockHash BinHash = BlendedHashes[MatchedBlock->Index - 1 ];
395+ LLVM_DEBUG (dbgs () << " Matched yaml block (bid = " << YamlBB.Index << " )"
396+ << " with hash " << Twine::utohexstr (YamlBB.Hash )
397+ << " to BB (index = " << MatchedBlock->Index - 1 << " )"
398+ << " with hash " << Twine::utohexstr (BinHash.combine ())
415399 << " \n " );
416400 // Update matching stats accounting for the matched block.
417- BlendedBlockHash BinHash = BlendedHashes[MatchedBlock->Index - 1 ];
418401 if (Matcher.isHighConfidenceMatch (BinHash, YamlHash)) {
419402 ++BC.Stats .NumMatchedBlocks ;
420403 BC.Stats .MatchedSampleCount += YamlBB.ExecCount ;
404+ LLVM_DEBUG (dbgs () << " exact match\n " );
421405 }
422406 } else {
423407 LLVM_DEBUG (
424- dbgs () << " Couldn't match yaml block with bid = " << YamlBB.Index
425- << " and hash = " << Twine::utohexstr (YamlBB.Hash ) << " \n " );
408+ dbgs () << " Couldn't match yaml block ( bid = " << YamlBB.Index << " ) "
409+ << " with hash " << Twine::utohexstr (YamlBB.Hash ) << " \n " );
426410 }
427411
428412 // Update matching stats.
@@ -575,16 +559,15 @@ void applyInference(FlowFunction &Func) {
575559 Params.JoinIslands = opts::StaleMatchingJoinIslands;
576560
577561 Params.CostBlockInc = opts::StaleMatchingCostBlockInc;
562+ Params.CostBlockEntryInc = opts::StaleMatchingCostBlockInc;
578563 Params.CostBlockDec = opts::StaleMatchingCostBlockDec;
579- Params.CostBlockEntryInc = opts::StaleMatchingCostBlockEntryInc;
580- Params.CostBlockEntryDec = opts::StaleMatchingCostBlockEntryDec;
581- Params.CostBlockZeroInc = opts::StaleMatchingCostBlockZeroInc;
564+ Params.CostBlockEntryDec = opts::StaleMatchingCostBlockDec;
582565 Params.CostBlockUnknownInc = opts::StaleMatchingCostBlockUnknownInc;
583566
584567 Params.CostJumpInc = opts::StaleMatchingCostJumpInc;
585- Params.CostJumpFTInc = opts::StaleMatchingCostJumpFTInc ;
568+ Params.CostJumpFTInc = opts::StaleMatchingCostJumpInc ;
586569 Params.CostJumpDec = opts::StaleMatchingCostJumpDec;
587- Params.CostJumpFTDec = opts::StaleMatchingCostJumpFTDec ;
570+ Params.CostJumpFTDec = opts::StaleMatchingCostJumpDec ;
588571 Params.CostJumpUnknownInc = opts::StaleMatchingCostJumpUnknownInc;
589572 Params.CostJumpUnknownFTInc = opts::StaleMatchingCostJumpUnknownFTInc;
590573
0 commit comments