Jlm
Loading...
Searching...
No Matches
NodeReductionTests.cpp
Go to the documentation of this file.
1/*
2 * Copyright 2024 Nico Reißmann <nico.reissmann@gmail.com>
3 * See COPYING for terms of redistribution.
4 */
5
6#include <gtest/gtest.h>
7
13#include <jlm/llvm/ir/types.hpp>
17#include <jlm/rvsdg/graph.hpp>
19#include <jlm/rvsdg/view.hpp>
21
22namespace jlm::llvm
23{
24
25TEST(NodeReductionTests, MultipleReductionsPerRegion)
26{
27 using namespace jlm::rvsdg;
28
29 // Arrange
30 const auto bitType = BitType::Create(32);
31 const auto memoryStateType = MemoryStateType::Create();
32
33 LlvmRvsdgModule rvsdgModule(util::FilePath(""), "", "");
34 auto & graph = rvsdgModule.Rvsdg();
35
36 auto & sizeArgument = GraphImport::Create(graph, bitType, "size");
37
38 auto testStructuralNode = TestStructuralNode::create(&graph.GetRootRegion(), 1);
39 auto & subregion = *testStructuralNode->subregion(0);
40 auto inputVar = testStructuralNode->addInputWithArguments(sizeArgument);
41
42 auto allocaResults = AllocaOperation::create(bitType, inputVar.argument[0], 4);
43
44 auto & c3 = IntegerConstantOperation::Create(subregion, BitValueRepresentation(32, 3));
45 auto storeResults =
46 StoreNonVolatileOperation::Create(allocaResults[0], c3.output(0), { allocaResults[1] }, 4);
47 auto loadResults =
48 LoadNonVolatileOperation::Create(allocaResults[0], { storeResults[0] }, bitType, 4);
49
50 const auto c5 = &IntegerConstantOperation::Create(subregion, BitValueRepresentation(32, 5));
51 auto & eqNode = IntegerEqOperation::createNode(32, *loadResults[0], *c5->output(0));
52
53 auto outputVar = testStructuralNode->addOutputWithResults({ eqNode.output(0) });
54
55 GraphExport::Create(*outputVar.output, "sum");
56
57 view(graph, stdout);
58
59 // Act
60 NodeReduction nodeReduction;
63 nodeReduction.Run(rvsdgModule, statisticsCollector);
64
65 view(graph, stdout);
66
67 // Assert
68 // We expect that two reductions are applied:
69 // 1. NormalizeLoadStore - This ensures that the stored constant value is directly forwarded to
70 // the add operation
71 // 2. Constant folding on the IntegerEqOperation node
72 // The result is that a single constant node with value 8 is left in the graph.
73 EXPECT_EQ(graph.GetRootRegion().numNodes(), 1u);
74
75 auto constantNode = TryGetOwnerNode<SimpleNode>(*outputVar.result[0]->origin());
76 auto constantOperation =
77 dynamic_cast<const IntegerConstantOperation *>(&constantNode->GetOperation());
78 EXPECT_EQ(constantOperation->Representation().to_uint(), 0u);
79
80 auto & statistics = *statisticsCollector.CollectedStatistics().begin();
81 auto & nodeReductionStatistics = dynamic_cast<const NodeReduction::Statistics &>(statistics);
82
83 EXPECT_EQ(nodeReductionStatistics.GetNumIterations(graph.GetRootRegion()).value(), 1u);
84 EXPECT_EQ(nodeReductionStatistics.GetNumIterations(subregion), 2u);
85 EXPECT_EQ(nodeReductionStatistics.getNumRegions(), 2u);
86 EXPECT_EQ(nodeReductionStatistics.getTotalIterations(), 3u);
87 EXPECT_EQ(nodeReductionStatistics.getMaxIterationsPerRegion(), 2u);
88 EXPECT_EQ(nodeReductionStatistics.getReductionCounters().numLoadNonVolatileReductions, 1u);
89}
90
91}
static jlm::util::StatisticsCollector statisticsCollector
static std::vector< rvsdg::Output * > create(std::shared_ptr< const rvsdg::Type > allocatedType, rvsdg::Output *count, const size_t alignment)
Definition alloca.hpp:131
static rvsdg::Node & Create(rvsdg::Region &region, IntegerValueRepresentation representation)
static rvsdg::Node & createNode(const size_t numBits, rvsdg::Output &operand1, rvsdg::Output &operand2)
static std::unique_ptr< llvm::ThreeAddressCode > Create(const Variable *address, const Variable *state, std::shared_ptr< const rvsdg::Type > loadedType, size_t alignment)
Definition Load.hpp:447
static std::shared_ptr< const MemoryStateType > Create()
Definition types.cpp:379
void Run(rvsdg::RvsdgModule &rvsdgModule, util::StatisticsCollector &statisticsCollector) override
Perform RVSDG transformation.
static std::unique_ptr< llvm::ThreeAddressCode > Create(const Variable *address, const Variable *value, const Variable *state, size_t alignment)
Definition Store.hpp:325
Graph & Rvsdg() noexcept
StatisticsRange CollectedStatistics() const noexcept
Global memory state passed between functions.
TEST(ControlOperationsTests, foldConstants)