diff --git a/llvm/tools/llvm-profgen/PerfReader.h b/llvm/tools/llvm-profgen/PerfReader.h --- a/llvm/tools/llvm-profgen/PerfReader.h +++ b/llvm/tools/llvm-profgen/PerfReader.h @@ -596,7 +596,7 @@ /// Load symbols and disassemble the code of a given binary. void loadBinary(const StringRef BinaryPath); void updateBinaryAddress(const MMapEvent &Event); - ProfiledBinary *getBinary() const { return Binary; } + std::shared_ptr getBinary() const { return Binary; } PerfScriptType getPerfScriptType() const { return PerfType; } // Entry of the reader to parse multiple perf traces void parsePerfTraces(cl::list &PerfTraceFilenames); @@ -635,7 +635,7 @@ // overlap computation for AutoFDO, or unwind for CSSPGO(hybrid sample). virtual void generateRawProfile() = 0; - ProfiledBinary *Binary = nullptr; + std::shared_ptr Binary; ContextSampleCounterMap SampleCounters; // Samples with the repeating time generated by the perf reader diff --git a/llvm/tools/llvm-profgen/PerfReader.cpp b/llvm/tools/llvm-profgen/PerfReader.cpp --- a/llvm/tools/llvm-profgen/PerfReader.cpp +++ b/llvm/tools/llvm-profgen/PerfReader.cpp @@ -296,7 +296,7 @@ void PerfReaderBase::loadBinary(const StringRef BinaryPath) { // Call to load the binary in the ctor of ProfiledBinary. - Binary = new ProfiledBinary(BinaryPath); + Binary = std::make_shared(BinaryPath); // Initialize the base address to preferred address. Binary->setBaseAddress(Binary->getPreferredBaseAddress()); } @@ -401,15 +401,15 @@ void HybridPerfReader::printUnwinderOutput() { outs() << "Binary(" << Binary->getName().str() << ")'s Range Counter:\n"; - printRangeCounter(SampleCounters, Binary); + printRangeCounter(SampleCounters, Binary.get()); outs() << "\nBinary(" << Binary->getName().str() << ")'s Branch Counter:\n"; - printBranchCounter(SampleCounters, Binary); + printBranchCounter(SampleCounters, Binary.get()); } void HybridPerfReader::unwindSamples() { for (const auto &Item : AggregatedSamples) { const HybridSample *Sample = dyn_cast(Item.first.getPtr()); - VirtualUnwinder Unwinder(&SampleCounters, Binary); + VirtualUnwinder Unwinder(&SampleCounters, Binary.get()); Unwinder.unwind(Sample, Item.second); } @@ -571,7 +571,8 @@ // 0x4005c8/0x4005dc/P/-/-/0 0x40062f/0x4005b0/P/-/-/0 ... // ... 0x4005c8/0x4005dc/P/-/-/0 # LBR Entries // - std::shared_ptr Sample = std::make_shared(Binary); + std::shared_ptr Sample = + std::make_shared(Binary.get()); // Parsing call stack and populate into HybridSample.CallStack if (!extractCallstack(TraceIt, Sample->CallStack)) { diff --git a/llvm/tools/llvm-profgen/ProfileGenerator.h b/llvm/tools/llvm-profgen/ProfileGenerator.h --- a/llvm/tools/llvm-profgen/ProfileGenerator.h +++ b/llvm/tools/llvm-profgen/ProfileGenerator.h @@ -25,10 +25,11 @@ class ProfileGenerator { public: - ProfileGenerator(ProfiledBinary *B) : Binary(B){}; + ProfileGenerator(std::shared_ptr B) : Binary(B){}; virtual ~ProfileGenerator() = default; static std::unique_ptr - create(ProfiledBinary *Binary, const ContextSampleCounterMap &SampleCounters, + create(std::shared_ptr Binary, + const ContextSampleCounterMap &SampleCounters, enum PerfScriptType SampleType); virtual void generateProfile() = 0; // Use SampleProfileWriter to serialize profile map @@ -58,7 +59,7 @@ // Used by SampleProfileWriter StringMap ProfileMap; - ProfiledBinary *Binary = nullptr; + std::shared_ptr Binary; }; class CSProfileGenerator : public ProfileGenerator { @@ -66,7 +67,7 @@ const ContextSampleCounterMap &SampleCounters; public: - CSProfileGenerator(ProfiledBinary *Binary, + CSProfileGenerator(std::shared_ptr Binary, const ContextSampleCounterMap &Counters) : ProfileGenerator(Binary), SampleCounters(Counters){}; @@ -232,7 +233,7 @@ class PseudoProbeCSProfileGenerator : public CSProfileGenerator { public: - PseudoProbeCSProfileGenerator(ProfiledBinary *Binary, + PseudoProbeCSProfileGenerator(std::shared_ptr Binary, const ContextSampleCounterMap &Counters) : CSProfileGenerator(Binary, Counters) {} void generateProfile() override; diff --git a/llvm/tools/llvm-profgen/ProfileGenerator.cpp b/llvm/tools/llvm-profgen/ProfileGenerator.cpp --- a/llvm/tools/llvm-profgen/ProfileGenerator.cpp +++ b/llvm/tools/llvm-profgen/ProfileGenerator.cpp @@ -74,7 +74,7 @@ int CSProfileGenerator::MaxContextDepth = -1; std::unique_ptr -ProfileGenerator::create(ProfiledBinary *Binary, +ProfileGenerator::create(std::shared_ptr Binary, const ContextSampleCounterMap &SampleCounters, enum PerfScriptType SampleType) { std::unique_ptr ProfileGenerator; @@ -279,7 +279,7 @@ if (Count == 0) continue; - InstructionPointer IP(Binary, RangeBegin, true); + InstructionPointer IP(Binary.get(), RangeBegin, true); // Disjoint ranges may have range in the middle of two instr, // e.g. If Instr1 at Addr1, and Instr2 at Addr2, disjoint range @@ -457,7 +457,7 @@ const ProbeBasedCtxKey *CtxKey = dyn_cast(CI.first.getPtr()); SmallVector ContextStrStack; - extractPrefixContextStack(ContextStrStack, CtxKey->Probes, Binary); + extractPrefixContextStack(ContextStrStack, CtxKey->Probes, Binary.get()); // Fill in function body samples from probes, also infer caller's samples // from callee's probe populateBodySamplesWithProbes(CI.second.RangeCounter, ContextStrStack); @@ -481,7 +481,7 @@ if (Count == 0) continue; - InstructionPointer IP(Binary, RangeBegin, true); + InstructionPointer IP(Binary.get(), RangeBegin, true); // Disjoint ranges may have range in the middle of two instr, // e.g. If Instr1 at Addr1, and Instr2 at Addr2, disjoint range