@@ -537,8 +537,7 @@ Error DataAggregator::preprocessProfile(BinaryContext &BC) {
537537
538538heatmap:
539539 // Sort parsed traces for faster processing.
540- if (!opts::BasicAggregation)
541- llvm::sort (Traces, llvm::less_first ());
540+ llvm::sort (Traces, llvm::less_first ());
542541
543542 if (!opts::HeatmapMode)
544543 return Error::success ();
@@ -883,13 +882,9 @@ DataAggregator::getFallthroughsInTrace(BinaryFunction &BF, const Trace &Trace,
883882
884883 // Adjust FromBB if the first LBR is a return from the last instruction in
885884 // the previous block (that instruction should be a call).
886- if (IsReturn) {
887- if (From)
888- FromBB = BF.getBasicBlockContainingOffset (From - 1 );
889- else
890- LLVM_DEBUG (dbgs () << " return to the function start: " << Trace << ' \n ' );
891- } else if (Trace.Branch == Trace::EXTERNAL && From == FromBB->getOffset () &&
892- !FromBB->isEntryPoint () && !FromBB->isLandingPad ()) {
885+ if (Trace.Branch != Trace::FT_ONLY && !BF.containsAddress (Trace.Branch ) &&
886+ From == FromBB->getOffset () &&
887+ (IsReturn ? From : !(FromBB->isEntryPoint () || FromBB->isLandingPad ()))) {
893888 const BinaryBasicBlock *PrevBB =
894889 BF.getLayout ().getBlock (FromBB->getIndex () - 1 );
895890 if (PrevBB->getSuccessor (FromBB->getLabel ())) {
@@ -1228,12 +1223,14 @@ ErrorOr<Location> DataAggregator::parseLocationOrOffset() {
12281223std::error_code DataAggregator::parseAggregatedLBREntry () {
12291224 enum AggregatedLBREntry : char {
12301225 INVALID = 0 ,
1231- EVENT_NAME, // E
1232- TRACE, // T
1233- SAMPLE, // S
1234- BRANCH, // B
1235- FT, // F
1236- FT_EXTERNAL_ORIGIN // f
1226+ EVENT_NAME, // E
1227+ TRACE, // T
1228+ RETURN, // R
1229+ SAMPLE, // S
1230+ BRANCH, // B
1231+ FT, // F
1232+ FT_EXTERNAL_ORIGIN, // f
1233+ FT_EXTERNAL_RETURN // r
12371234 } Type = INVALID;
12381235
12391236 // / The number of fields to parse, set based on \p Type.
@@ -1261,20 +1258,22 @@ std::error_code DataAggregator::parseAggregatedLBREntry() {
12611258
12621259 Type = StringSwitch<AggregatedLBREntry>(Str)
12631260 .Case (" T" , TRACE)
1261+ .Case (" R" , RETURN)
12641262 .Case (" S" , SAMPLE)
12651263 .Case (" E" , EVENT_NAME)
12661264 .Case (" B" , BRANCH)
12671265 .Case (" F" , FT)
12681266 .Case (" f" , FT_EXTERNAL_ORIGIN)
1267+ .Case (" r" , FT_EXTERNAL_RETURN)
12691268 .Default (INVALID);
12701269
12711270 if (Type == INVALID) {
1272- reportError (" expected T, S, E, B, F or f " );
1271+ reportError (" expected T, R, S, E, B, F, f or r " );
12731272 return make_error_code (llvm::errc::io_error);
12741273 }
12751274
12761275 using SSI = StringSwitch<int >;
1277- AddrNum = SSI (Str).Case (" T" , 3 ).Case (" S" , 1 ).Case (" E" , 0 ).Default (2 );
1276+ AddrNum = SSI (Str).Cases (" T" , " R " , 3 ).Case (" S" , 1 ).Case (" E" , 0 ).Default (2 );
12781277 CounterNum = SSI (Str).Case (" B" , 2 ).Case (" E" , 0 ).Default (1 );
12791278 }
12801279
@@ -1331,17 +1330,30 @@ std::error_code DataAggregator::parseAggregatedLBREntry() {
13311330 if (ToFunc)
13321331 ToFunc->setHasProfileAvailable ();
13331332
1334- // / For legacy fall-through types, adjust locations to match Trace container.
1335- if (Type == FT || Type == FT_EXTERNAL_ORIGIN) {
1333+ // / For fall-through types, adjust locations to match Trace container.
1334+ if (Type == FT || Type == FT_EXTERNAL_ORIGIN || Type == FT_EXTERNAL_RETURN ) {
13361335 Addr[2 ] = Location (Addr[1 ]->Offset ); // Trace To
13371336 Addr[1 ] = Location (Addr[0 ]->Offset ); // Trace From
1338- // Put a magic value into Trace Branch to differentiate from a full trace.
1339- Addr[0 ] = Location (Type == FT ? Trace::FT_ONLY : Trace::FT_EXTERNAL_ORIGIN);
1337+ // Put a magic value into Trace Branch to differentiate from a full trace:
1338+ if (Type == FT)
1339+ Addr[0 ] = Location (Trace::FT_ONLY);
1340+ else if (Type == FT_EXTERNAL_ORIGIN)
1341+ Addr[0 ] = Location (Trace::FT_EXTERNAL_ORIGIN);
1342+ else if (Type == FT_EXTERNAL_RETURN)
1343+ Addr[0 ] = Location (Trace::FT_EXTERNAL_RETURN);
1344+ else
1345+ llvm_unreachable (" Unexpected fall-through type" );
13401346 }
13411347
1342- // / For legacy branch type, mark Trace To to differentite from a full trace.
1343- if (Type == BRANCH) {
1348+ // / For branch type, mark Trace To to differentiate from a full trace.
1349+ if (Type == BRANCH)
13441350 Addr[2 ] = Location (Trace::BR_ONLY);
1351+
1352+ if (Type == RETURN) {
1353+ if (!Addr[0 ]->Offset )
1354+ Addr[0 ]->Offset = Trace::FT_EXTERNAL_RETURN;
1355+ else
1356+ Returns.emplace (Addr[0 ]->Offset );
13451357 }
13461358
13471359 // / Record a trace.
@@ -1602,6 +1614,7 @@ void DataAggregator::processBranchEvents() {
16021614 NamedRegionTimer T (" processBranch" , " Processing branch events" ,
16031615 TimerGroupName, TimerGroupDesc, opts::TimeAggregator);
16041616
1617+ Returns.emplace (Trace::FT_EXTERNAL_RETURN);
16051618 for (const auto &[Trace, Info] : Traces) {
16061619 bool IsReturn = checkReturn (Trace.Branch );
16071620 // Ignore returns.
0 commit comments