Index: include/llvm/ProfileData/InstrProf.h =================================================================== --- include/llvm/ProfileData/InstrProf.h +++ include/llvm/ProfileData/InstrProf.h @@ -598,6 +598,28 @@ InstrProfRecord() = default; InstrProfRecord(StringRef Name, uint64_t Hash, std::vector Counts) : Name(Name), Hash(Hash), Counts(std::move(Counts)) {} + InstrProfRecord(InstrProfRecord &&) = default; + InstrProfRecord(const InstrProfRecord &RHS) + : Name(RHS.Name), Hash(RHS.Hash), Counts(RHS.Counts), SIPE(RHS.SIPE), + ValueData(RHS.ValueData + ? llvm::make_unique(*RHS.ValueData) + : nullptr) {} + InstrProfRecord &operator=(InstrProfRecord &&) = default; + InstrProfRecord &operator=(const InstrProfRecord &RHS) { + Name = RHS.Name; + Hash = RHS.Hash; + Counts = RHS.Counts; + SIPE = RHS.SIPE; + if (!RHS.ValueData) { + ValueData = nullptr; + return *this; + } + if (!ValueData) + ValueData = llvm::make_unique(*RHS.ValueData); + else + *ValueData = *RHS.ValueData; + return *this; + } using ValueMapType = std::vector>; @@ -671,27 +693,37 @@ Error takeError() { return SIPE.takeError(); } private: - std::vector IndirectCallSites; - std::vector MemOPSizes; + struct ValueProfData { + std::vector IndirectCallSites; + std::vector MemOPSizes; + }; + std::unique_ptr ValueData; const std::vector & getValueSitesForKind(uint32_t ValueKind) const { + static const std::vector Empty; switch (ValueKind) { case IPVK_IndirectCallTarget: - return IndirectCallSites; + return ValueData ? ValueData->IndirectCallSites : Empty; case IPVK_MemOPSize: - return MemOPSizes; + return ValueData ? ValueData->MemOPSizes : Empty; default: llvm_unreachable("Unknown value kind!"); } - return IndirectCallSites; } std::vector & getValueSitesForKind(uint32_t ValueKind) { - return const_cast &>( - const_cast(this) - ->getValueSitesForKind(ValueKind)); + if (!ValueData) + ValueData = llvm::make_unique(); + switch (ValueKind) { + case IPVK_IndirectCallTarget: + return ValueData->IndirectCallSites; + case IPVK_MemOPSize: + return ValueData->MemOPSizes; + default: + llvm_unreachable("Unknown value kind!"); + } } // Map indirect call target name hash to name string. Index: lib/ProfileData/InstrProf.cpp =================================================================== --- lib/ProfileData/InstrProf.cpp +++ lib/ProfileData/InstrProf.cpp @@ -504,6 +504,8 @@ SIPE.addError(instrprof_error::value_site_count_mismatch); return; } + if (!ThisNumValueSites) + return; std::vector &ThisSiteRecords = getValueSitesForKind(ValueKind); std::vector &OtherSiteRecords = @@ -534,6 +536,8 @@ void InstrProfRecord::scaleValueProfData(uint32_t ValueKind, uint64_t Weight) { uint32_t ThisNumValueSites = getNumValueSites(ValueKind); + if (!ThisNumValueSites) + return; std::vector &ThisSiteRecords = getValueSitesForKind(ValueKind); for (uint32_t I = 0; I < ThisNumValueSites; I++)