Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
164 changes: 164 additions & 0 deletions jlm/llvm/opt/alias-analyses/AgnosticModRefSummarizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,161 @@ class AgnosticModRefSummary final : public ModRefSummary
AgnosticModRefSet AllMemoryNodes_;
};

static std::vector<AgnosticModRefSummarizer::Statistics::MemoryStateSummary>
collectMemoryStateDistribution(
const rvsdg::Graph & rvsdg,
const AgnosticModRefSummary & modRefSummary)
{
std::function<void(
const rvsdg::Region &,
const rvsdg::LambdaNode *,
std::vector<AgnosticModRefSummarizer::Statistics::MemoryStateSummary> &)>
collect =
[&](const rvsdg::Region & region,
const rvsdg::LambdaNode * lambdaNode,
std::vector<AgnosticModRefSummarizer::Statistics::MemoryStateSummary> & summaries)
{
for (auto & node : region.Nodes())
{
rvsdg::MatchTypeOrFail(
node,
[&](const rvsdg::PhiNode & phiNode)
{
JLM_ASSERT(lambdaNode == nullptr);
collect(*phiNode.subregion(), lambdaNode, summaries);
},
[&](const rvsdg::DeltaNode &)
{
JLM_ASSERT(lambdaNode == nullptr);
// Nothing needs to be done
},
[&](const rvsdg::LambdaNode & n)
{
JLM_ASSERT(lambdaNode == nullptr);
auto & lambdaEntryModRefSet = modRefSummary.GetLambdaEntryModRef(n);
auto & lambdaExitModRefSet = modRefSummary.GetLambdaExitModRef(n);
JLM_ASSERT(&lambdaEntryModRefSet == &lambdaExitModRefSet);
summaries.push_back({ &n, &n, lambdaEntryModRefSet.getModRefNodes().size() });

collect(*n.subregion(), &n, summaries);
},
[&](const rvsdg::ThetaNode & thetaNode)
{
JLM_ASSERT(lambdaNode != nullptr);
summaries.push_back(
{ lambdaNode,
&thetaNode,
modRefSummary.GetThetaModRef(thetaNode).getModRefNodes().size() });

collect(*thetaNode.subregion(), lambdaNode, summaries);
},
[&](const rvsdg::GammaNode & gammaNode)
{
JLM_ASSERT(lambdaNode != nullptr);
auto & gammaEntryModRefSet = modRefSummary.GetGammaEntryModRef(gammaNode);
auto & gammaExitModRefSet = modRefSummary.GetGammaEntryModRef(gammaNode);
JLM_ASSERT(&gammaEntryModRefSet == &gammaExitModRefSet);
summaries.push_back(
{ lambdaNode, &gammaNode, gammaEntryModRefSet.getModRefNodes().size() });

for (auto & subregion : gammaNode.Subregions())
collect(subregion, lambdaNode, summaries);
},
[&](const rvsdg::SimpleNode & simpleNode)
{
MatchTypeWithDefault(
simpleNode.GetOperation(),
[&](const StoreOperation &)
{
JLM_ASSERT(lambdaNode != nullptr);
summaries.push_back(
{ lambdaNode,
&simpleNode,
modRefSummary.GetSimpleNodeModRef(simpleNode).getModRefNodes().size() });
},
[&](const LoadOperation &)
{
JLM_ASSERT(lambdaNode != nullptr);
summaries.push_back(
{ lambdaNode,
&simpleNode,
modRefSummary.GetSimpleNodeModRef(simpleNode).getModRefNodes().size() });
},
[&](const MemCpyOperation &)
{
JLM_ASSERT(lambdaNode != nullptr);
summaries.push_back(
{ lambdaNode,
&simpleNode,
modRefSummary.GetSimpleNodeModRef(simpleNode).getModRefNodes().size() });
},
[&](const MemMoveOperation &)
{
JLM_ASSERT(lambdaNode != nullptr);
summaries.push_back(
{ lambdaNode,
&simpleNode,
modRefSummary.GetSimpleNodeModRef(simpleNode).getModRefNodes().size() });
},
[&](const MemSetOperation &)
{
JLM_ASSERT(lambdaNode != nullptr);
summaries.push_back(
{ lambdaNode,
&simpleNode,
modRefSummary.GetSimpleNodeModRef(simpleNode).getModRefNodes().size() });
},
[&](const FreeOperation &)
{
JLM_ASSERT(lambdaNode != nullptr);
summaries.push_back(
{ lambdaNode,
&simpleNode,
modRefSummary.GetSimpleNodeModRef(simpleNode).getModRefNodes().size() });
},
[&](const AllocaOperation &)
{
JLM_ASSERT(lambdaNode != nullptr);
summaries.push_back(
{ lambdaNode,
&simpleNode,
modRefSummary.GetSimpleNodeModRef(simpleNode).getModRefNodes().size() });
},
[&](const MallocOperation &)
{
JLM_ASSERT(lambdaNode != nullptr);
summaries.push_back(
{ lambdaNode,
&simpleNode,
modRefSummary.GetSimpleNodeModRef(simpleNode).getModRefNodes().size() });
},
[&](const CallOperation &)
{
JLM_ASSERT(lambdaNode != nullptr);
summaries.push_back(
{ lambdaNode,
&simpleNode,
modRefSummary.GetSimpleNodeModRef(simpleNode).getModRefNodes().size() });
},
[&](const MemoryStateOperation &)
{
JLM_ASSERT(lambdaNode != nullptr);
// Nothing needs to be done
},
[&]()
{
// Any remaining type of node should not involve any memory states
JLM_ASSERT(!hasMemoryState(node));
});
});
}
};

std::vector<AgnosticModRefSummarizer::Statistics::MemoryStateSummary> summaries;
collect(rvsdg.GetRootRegion(), nullptr, summaries);
return summaries;
}

AgnosticModRefSummarizer::AgnosticModRefSummarizer() = default;

AgnosticModRefSummarizer::~AgnosticModRefSummarizer() = default;
Expand All @@ -152,6 +307,15 @@ AgnosticModRefSummarizer::SummarizeModRefs(
AnnotateRegion(rvsdgModule.Rvsdg().GetRootRegion());

statistics->StopCollecting();

// Perform the collection of the memory state distribution AFTER we invoked StopCollecting() such
// that it does not count into the timing measurements
if (statisticsCollector.IsDemanded(statistics->GetId()))
{
const auto summaries = collectMemoryStateDistribution(rvsdgModule.Rvsdg(), *ModRefSummary_);
statistics->addMemoryStateDistribution(summaries);
}

statisticsCollector.CollectDemandedStatistics(std::move(statistics));

return std::move(ModRefSummary_);
Expand Down
51 changes: 51 additions & 0 deletions jlm/llvm/opt/alias-analyses/AgnosticModRefSummarizer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,8 @@ class AgnosticModRefSummarizer final : public ModRefSummarizer
class AgnosticModRefSummarizer::Statistics final : public util::Statistics
{
public:
static constexpr auto memoryStateDistributionLabel = "MemoryStateDistribution";

Statistics(
const util::FilePath & sourceFile,
const util::StatisticsCollector & statisticsCollector,
Expand Down Expand Up @@ -166,6 +168,55 @@ class AgnosticModRefSummarizer::Statistics final : public util::Statistics
GetTimer(Label::Timer).stop();
}

struct MemoryStateSummary
{
MemoryStateSummary(
const rvsdg::LambdaNode * lambdaNode,
const rvsdg::Node * node,
const size_t numMemoryStates)
: lambdaNode(lambdaNode),
node(node),
numMemoryStates(numMemoryStates)
{}

const rvsdg::LambdaNode * lambdaNode = nullptr;
const rvsdg::Node * node = nullptr;
size_t numMemoryStates = 0;
};

void
addMemoryStateDistribution(const std::vector<MemoryStateSummary> & memoryStateSummaries)
{
auto toString = [](const MemoryStateSummary & memoryStateSummary)
{
constexpr char separator = '-';
return util::strfmt(
memoryStateSummary.lambdaNode->DebugString(),
separator,
memoryStateSummary.node->DebugString(),
separator,
"(",
memoryStateSummary.node->region()->getRegionId(),
":",
memoryStateSummary.node->GetNodeId(),
")",
separator,
memoryStateSummary.numMemoryStates);
};

size_t n = 0;
std::string summaryStr;
for (auto & summary : memoryStateSummaries)
{
summaryStr += toString(summary);
if (n != memoryStateSummaries.size() - 1)
summaryStr += ",";
n++;
}

AddMeasurement(memoryStateDistributionLabel, summaryStr);
}

static std::unique_ptr<Statistics>
Create(
const util::FilePath & sourceFile,
Expand Down
25 changes: 16 additions & 9 deletions jlm/llvm/opt/alias-analyses/AgnosticModRefSummarizerTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -825,27 +825,34 @@ TEST(AgnosticModRefSummarizerTests, TestMemcpy)

TEST(AgnosticModRefSummarizerTests, TestStatistics)
{
using namespace jlm::llvm;
using namespace jlm::llvm::aa;
using namespace jlm::util;

// Arrange
jlm::llvm::LoadTest1 test;
LoadTest1 test;
auto pointsToGraph = RunAndersen(test.module());

jlm::util::StatisticsCollectorSettings statisticsCollectorSettings(
{ jlm::util::Statistics::Id::AgnosticModRefSummarizer });
jlm::util::StatisticsCollector statisticsCollector(statisticsCollectorSettings);
StatisticsCollectorSettings statisticsCollectorSettings(
{ Statistics::Id::AgnosticModRefSummarizer });
StatisticsCollector statisticsCollector(statisticsCollectorSettings);

// Act
jlm::llvm::aa::AgnosticModRefSummarizer::Create(
test.module(),
*pointsToGraph,
statisticsCollector);
AgnosticModRefSummarizer::Create(test.module(), *pointsToGraph, statisticsCollector);

// Assert
EXPECT_EQ(statisticsCollector.NumCollectedStatistics(), 1u);

auto & statistics = dynamic_cast<const jlm::llvm::aa::AgnosticModRefSummarizer::Statistics &>(
auto & statistics = dynamic_cast<const AgnosticModRefSummarizer::Statistics &>(
*statisticsCollector.CollectedStatistics().begin());

EXPECT_EQ(statistics.GetSourceFile(), test.module().SourceFileName());
EXPECT_EQ(statistics.NumPointsToGraphMemoryNodes(), 2u);
EXPECT_NE(statistics.GetTime(), 0u);

auto memoryStateDistribution = std::get_if<std::string>(&statistics.GetMeasurement(
std::string(AgnosticModRefSummarizer::Statistics::memoryStateDistributionLabel)));
EXPECT_EQ(
*memoryStateDistribution,
"LAMBDA[f]-LAMBDA[f]-(0:0)-2,LAMBDA[f]-Load[ptr]-(1:0)-2,LAMBDA[f]-Load[bit32]-(1:1)-2");
}
Loading