llvm.org GIT mirror llvm / c44d843
Revert r271089 and r271090. It was triggering an msan bot. Revert "[IRPGO] Set the function entry count metadata." This reverts commit r271090. Revert "[IRPGO] Centralize the function attribute inliner hint logic. NFC." This reverts commit r271089. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@271091 91177308-0d34-0410-b5e6-96231b3b80d8 Sean Silva 3 years ago
2 changed file(s) with 44 addition(s) and 34 deletion(s). Raw diff Collapse all Expand all
457457 public:
458458 PGOUseFunc(Function &Func, Module *Modu, BranchProbabilityInfo *BPI = nullptr,
459459 BlockFrequencyInfo *BFI = nullptr)
460 : F(Func), M(Modu), FuncInfo(Func, false, BPI, BFI) {}
460 : F(Func), M(Modu), FuncInfo(Func, false, BPI, BFI),
461 FreqAttr(FFA_Normal) {}
461462
462463 // Read counts for the instrumented BB from profile.
463464 bool readCounters(IndexedInstrProfReader *PGOReader);
471472 // Annotate the indirect call sites.
472473 void annotateIndirectCallSites();
473474
475 // The hotness of the function from the profile count.
476 enum FuncFreqAttr { FFA_Normal, FFA_Cold, FFA_Hot };
477
478 // Return the function hotness from the profile.
479 FuncFreqAttr getFuncFreqAttr() const { return FreqAttr; }
480
474481 // Return the profile record for this function;
475482 InstrProfRecord &getProfileRecord() { return ProfileRecord; }
476
477 // The entry count of this function.
478 uint64_t EntryCount;
479
480 // The maximum count value of any BB in this function.
481 uint64_t MaxBBCount;
482483
483484 private:
484485 Function &F;
491492 return FuncInfo.getBBInfo(BB);
492493 }
493494
495 // The maximum count value in the profile. This is only used in PGO use
496 // compilation.
497 uint64_t ProgramMaxCount;
498
494499 // ProfileRecord for this function.
495500 InstrProfRecord ProfileRecord;
501
502 // Function hotness info derived from profile.
503 FuncFreqAttr FreqAttr;
496504
497505 // Find the Instrumented BB and set the value.
498506 void setInstrumentedCounts(const std::vector &CountFromProfile);
503511
504512 // Return FuncName string;
505513 const std::string getFuncName() const { return FuncInfo.FuncName; }
514
515 // Set the hot/cold inline hints based on the count values.
516 // FIXME: This function should be removed once the functionality in
517 // the inliner is implemented.
518 void markFunctionAttributes(uint64_t EntryCount, uint64_t MaxCount) {
519 if (ProgramMaxCount == 0)
520 return;
521 // Threshold of the hot functions.
522 const BranchProbability HotFunctionThreshold(1, 100);
523 // Threshold of the cold functions.
524 const BranchProbability ColdFunctionThreshold(2, 10000);
525 if (EntryCount >= HotFunctionThreshold.scale(ProgramMaxCount))
526 FreqAttr = FFA_Hot;
527 else if (MaxCount <= ColdFunctionThreshold.scale(ProgramMaxCount))
528 FreqAttr = FFA_Cold;
529 }
506530 };
507531
508532 // Visit all the edges and assign the count value for the instrumented
602626 getBBInfo(nullptr).UnknownCountInEdge = 2;
603627
604628 setInstrumentedCounts(CountFromProfile);
629 ProgramMaxCount = PGOReader->getMaximumFunctionCount();
605630 return true;
606631 }
607632
665690 }
666691
667692 DEBUG(dbgs() << "Populate counts in " << NumPasses << " passes.\n");
668
669 EntryCount = getBBInfo(&*F.begin()).CountValue;
670 MaxBBCount = 0;
693 // Assert every BB has a valid counter.
694 uint64_t FuncEntryCount = getBBInfo(&*F.begin()).CountValue;
695 uint64_t FuncMaxCount = FuncEntryCount;
671696 for (auto &BB : F) {
672 // Assert every BB has a valid counter.
673697 assert(getBBInfo(&BB).CountValid && "BB count is not valid");
674 MaxBBCount = std::max(MaxBBCount, getBBInfo(&BB).CountValue);
675 }
698 uint64_t Count = getBBInfo(&BB).CountValue;
699 if (Count > FuncMaxCount)
700 FuncMaxCount = Count;
701 }
702 markFunctionAttributes(FuncEntryCount, FuncMaxCount);
676703
677704 DEBUG(FuncInfo.dumpInfo("after reading profile."));
678705 }
860887 std::vector HotFunctions;
861888 std::vector ColdFunctions;
862889 InstrProfSummaryBuilder Builder(ProfileSummaryBuilder::DefaultCutoffs);
863 uint64_t ProgramMaxCount = PGOReader->getMaximumFunctionCount();
864 bool HasProgramMaxCount = ProgramMaxCount != 0;
865890 for (auto &F : M) {
866891 if (F.isDeclaration())
867892 continue;
869894 auto *BFI = LookupBFI(F);
870895 PGOUseFunc Func(F, &M, BPI, BFI);
871896 setPGOCountOnFunc(Func, PGOReader.get());
872 F.setEntryCount(Func.EntryCount);
873897 if (!Func.getProfileRecord().Counts.empty())
874898 Builder.addRecord(Func.getProfileRecord());
875
876 if (!HasProgramMaxCount)
877 continue;
878 // Set the hot/cold inline hints based on the count values.
879 // FIXME: This should be removed once the functionality in
880 // the inliner is implemented.
881 const BranchProbability HotFunctionThreshold(1, 100);
882 const BranchProbability ColdFunctionThreshold(2, 10000);
883 if (Func.EntryCount >= HotFunctionThreshold.scale(ProgramMaxCount))
899 PGOUseFunc::FuncFreqAttr FreqAttr = Func.getFuncFreqAttr();
900 if (FreqAttr == PGOUseFunc::FFA_Cold)
901 ColdFunctions.push_back(&F);
902 else if (FreqAttr == PGOUseFunc::FFA_Hot)
884903 HotFunctions.push_back(&F);
885 else if (Func.MaxBBCount <= ColdFunctionThreshold.scale(ProgramMaxCount))
886 ColdFunctions.push_back(&F);
887904 }
888905 M.setProfileSummary(Builder.getSummary()->getMD(M.getContext()));
889
890906 // Set function hotness attribute from the profile.
891 // We have to apply these attributes at the end because their presence
892 // can affect the BranchProbabilityInfo of any callers, resulting in an
893 // inconsistent MST between prof-gen and prof-use.
894907 for (auto &F : HotFunctions) {
895908 F->addFnAttr(llvm::Attribute::InlineHint);
896909 DEBUG(dbgs() << "Set inline attribute to function: " << F->getName()
2020 ; GEN: @__profn_test_br_1 = private constant [9 x i8] c"test_br_1"
2121
2222 define i32 @test_br_1(i32 %i) {
23 ; USE-LABEL: @test_br_1
24 ; USE-SAME: !prof ![[FUNC_ENTRY_COUNT:[0-9]+]]
2523 entry:
2624 ; GEN: entry:
2725 ; GEN-NOT: llvm.instrprof.increment
4543 }
4644 ; USE-DAG: {{![0-9]+}} = !{i32 1, !"ProfileSummary", {{![0-9]+}}}
4745 ; USE-DAG: {{![0-9]+}} = !{!"DetailedSummary", {{![0-9]+}}}
48 ; USE-DAG: ![[FUNC_ENTRY_COUNT]] = !{!"function_entry_count", i64 3}