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 @@ -373,6 +373,7 @@ BranchSample BranchCounter; void recordRangeCount(uint64_t Start, uint64_t End, uint64_t Repeat) { + assert(Start <= End && "invalid instruction range"); RangeCounter[{Start, End}] += Repeat; } void recordBranchCount(uint64_t Source, uint64_t Target, uint64_t Repeat) { 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 @@ -422,8 +422,11 @@ Token.split(Addresses, "/"); uint64_t Src; uint64_t Dst; - Addresses[0].substr(2).getAsInteger(16, Src); - Addresses[1].substr(2).getAsInteger(16, Dst); + + // Stop at broken LBR records. + if (Addresses[0].substr(2).getAsInteger(16, Src) || + Addresses[1].substr(2).getAsInteger(16, Dst)) + break; bool SrcIsInternal = Binary->addressIsCode(Src); bool DstIsInternal = Binary->addressIsCode(Dst); 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 @@ -183,6 +183,7 @@ for (auto Item : Ranges) { uint64_t Begin = Item.first.first; uint64_t End = Item.first.second; + assert(Begin <= End && "invalid instruction range"); uint64_t Count = Item.second; if (Boundaries.find(Begin) == Boundaries.end()) Boundaries[Begin] = BoundaryPoint(); @@ -194,7 +195,7 @@ } uint64_t BeginAddress = UINT64_MAX; - int Count = 0; + uint64_t Count = 0; for (auto Item : Boundaries) { uint64_t Address = Item.first; BoundaryPoint &Point = Item.second; @@ -208,6 +209,7 @@ assert((BeginAddress != UINT64_MAX) && "First boundary point cannot be 'end' point"); DisjointRanges[{BeginAddress, Address}] = Count; + assert(Count >= Point.EndCount && "mismatched live ranges"); Count -= Point.EndCount; BeginAddress = Address + 1; }