Skip to content

[memprof] Use std::optional (NFC) #88366

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions llvm/lib/ProfileData/InstrProfReader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
#include <cstdint>
#include <limits>
#include <memory>
#include <optional>
#include <system_error>
#include <utility>
#include <vector>
Expand Down Expand Up @@ -1506,13 +1507,11 @@ IndexedInstrProfReader::getMemProfRecord(const uint64_t FuncNameHash) {

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

// Check that all frame ids were successfully converted to frames.
if (HasFrameMappingError) {
if (LastUnmappedFrameId) {
return make_error<InstrProfError>(instrprof_error::hash_mismatch,
"memprof frame not found for frame id " +
Twine(LastUnmappedFrameId));
Twine(*LastUnmappedFrameId));
}
return Record;
}
Expand Down
9 changes: 4 additions & 5 deletions llvm/unittests/ProfileData/InstrProfTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include "llvm/Testing/Support/Error.h"
#include "gtest/gtest.h"
#include <cstdarg>
#include <optional>

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

memprof::FrameId LastUnmappedFrameId = 0;
bool HasFrameMappingError = false;
std::optional<memprof::FrameId> LastUnmappedFrameId;
auto IdToFrameCallback = [&](const memprof::FrameId Id) {
auto Iter = IdToFrameMap.find(Id);
if (Iter == IdToFrameMap.end()) {
LastUnmappedFrameId = Id;
HasFrameMappingError = true;
return memprof::Frame(0, 0, 0, false);
}
return Iter->second;
};

const memprof::MemProfRecord WantRecord(IndexedMR, IdToFrameCallback);
ASSERT_FALSE(HasFrameMappingError)
<< "could not map frame id: " << LastUnmappedFrameId;
ASSERT_FALSE(LastUnmappedFrameId.has_value())
<< "could not map frame id: " << *LastUnmappedFrameId;
EXPECT_THAT(WantRecord, EqualsRecord(Record));
}

Expand Down