Skip to content

Commit db9a17a

Browse files
[memprof] Use std::optional (NFC) (llvm#88366)
1 parent 3749e0d commit db9a17a

File tree

2 files changed

+8
-10
lines changed

2 files changed

+8
-10
lines changed

llvm/lib/ProfileData/InstrProfReader.cpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
#include <cstdint>
3434
#include <limits>
3535
#include <memory>
36+
#include <optional>
3637
#include <system_error>
3738
#include <utility>
3839
#include <vector>
@@ -1506,13 +1507,11 @@ IndexedInstrProfReader::getMemProfRecord(const uint64_t FuncNameHash) {
15061507

15071508
// Setup a callback to convert from frame ids to frame using the on-disk
15081509
// FrameData hash table.
1509-
memprof::FrameId LastUnmappedFrameId = 0;
1510-
bool HasFrameMappingError = false;
1510+
std::optional<memprof::FrameId> LastUnmappedFrameId;
15111511
auto IdToFrameCallback = [&](const memprof::FrameId Id) {
15121512
auto FrIter = MemProfFrameTable->find(Id);
15131513
if (FrIter == MemProfFrameTable->end()) {
15141514
LastUnmappedFrameId = Id;
1515-
HasFrameMappingError = true;
15161515
return memprof::Frame(0, 0, 0, false);
15171516
}
15181517
return *FrIter;
@@ -1521,10 +1520,10 @@ IndexedInstrProfReader::getMemProfRecord(const uint64_t FuncNameHash) {
15211520
memprof::MemProfRecord Record(*Iter, IdToFrameCallback);
15221521

15231522
// Check that all frame ids were successfully converted to frames.
1524-
if (HasFrameMappingError) {
1523+
if (LastUnmappedFrameId) {
15251524
return make_error<InstrProfError>(instrprof_error::hash_mismatch,
15261525
"memprof frame not found for frame id " +
1527-
Twine(LastUnmappedFrameId));
1526+
Twine(*LastUnmappedFrameId));
15281527
}
15291528
return Record;
15301529
}

llvm/unittests/ProfileData/InstrProfTest.cpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include "llvm/Testing/Support/Error.h"
2020
#include "gtest/gtest.h"
2121
#include <cstdarg>
22+
#include <optional>
2223

2324
using namespace llvm;
2425
using ::testing::EndsWith;
@@ -433,21 +434,19 @@ TEST_F(InstrProfTest, test_memprof) {
433434
ASSERT_THAT_ERROR(RecordOr.takeError(), Succeeded());
434435
const memprof::MemProfRecord &Record = RecordOr.get();
435436

436-
memprof::FrameId LastUnmappedFrameId = 0;
437-
bool HasFrameMappingError = false;
437+
std::optional<memprof::FrameId> LastUnmappedFrameId;
438438
auto IdToFrameCallback = [&](const memprof::FrameId Id) {
439439
auto Iter = IdToFrameMap.find(Id);
440440
if (Iter == IdToFrameMap.end()) {
441441
LastUnmappedFrameId = Id;
442-
HasFrameMappingError = true;
443442
return memprof::Frame(0, 0, 0, false);
444443
}
445444
return Iter->second;
446445
};
447446

448447
const memprof::MemProfRecord WantRecord(IndexedMR, IdToFrameCallback);
449-
ASSERT_FALSE(HasFrameMappingError)
450-
<< "could not map frame id: " << LastUnmappedFrameId;
448+
ASSERT_FALSE(LastUnmappedFrameId.has_value())
449+
<< "could not map frame id: " << *LastUnmappedFrameId;
451450
EXPECT_THAT(WantRecord, EqualsRecord(Record));
452451
}
453452

0 commit comments

Comments
 (0)