Jlm
Loading...
Searching...
No Matches
DeltaTests.cpp
Go to the documentation of this file.
1/*
2 * Copyright 2018 Nico Reißmann <nico.reissmann@gmail.com>
3 * See COPYING for terms of redistribution.
4 */
5
6#include <gtest/gtest.h>
7
12#include <jlm/rvsdg/view.hpp>
13
14TEST(DeltaTests, TestDeltaCreation)
15{
16 using namespace jlm::llvm;
17 using namespace jlm::rvsdg;
18
19 // Arrange & Act
20 auto valueType = TestType::createValueType();
21 auto pointerType = PointerType::Create();
22 jlm::llvm::LlvmRvsdgModule rvsdgModule(jlm::util::FilePath(""), "", "");
23
24 auto imp = &jlm::rvsdg::GraphImport::Create(rvsdgModule.Rvsdg(), valueType, "");
25
27 &rvsdgModule.Rvsdg().GetRootRegion(),
28 LlvmDeltaOperation::Create(valueType, "test-delta1", Linkage::externalLinkage, "", true, 4));
29 auto dep = delta1->AddContextVar(*imp).inner;
30 auto d1 = &delta1->finalize(
31 TestOperation::createNode(delta1->subregion(), { dep }, { valueType })->output(0));
32
34 &rvsdgModule.Rvsdg().GetRootRegion(),
35 LlvmDeltaOperation::Create(valueType, "test-delta2", Linkage::internalLinkage, "", false, 4));
36 auto d2 = &delta2->finalize(
37 TestOperation::createNode(delta2->subregion(), {}, { valueType })->output(0));
38
39 GraphExport::Create(*d1, "");
40 GraphExport::Create(*d2, "");
41
42 jlm::rvsdg::view(rvsdgModule.Rvsdg(), stdout);
43
44 // Assert
45 EXPECT_EQ(rvsdgModule.Rvsdg().GetRootRegion().numNodes(), 2u);
46
47 EXPECT_TRUE(delta1->constant());
48 EXPECT_EQ(*delta1->Type(), *valueType);
49
50 EXPECT_FALSE(delta2->constant());
51 EXPECT_EQ(*delta2->Type(), *valueType);
52}
53
54TEST(DeltaTests, TestRemoveDeltaInputsWhere)
55{
56 using namespace jlm::llvm;
57 using namespace jlm::rvsdg;
58
59 // Arrange
60 auto valueType = TestType::createValueType();
61 jlm::llvm::LlvmRvsdgModule rvsdgModule(jlm::util::FilePath(""), "", "");
62
63 auto x = &jlm::rvsdg::GraphImport::Create(rvsdgModule.Rvsdg(), valueType, "");
64
65 auto deltaNode = jlm::rvsdg::DeltaNode::Create(
66 &rvsdgModule.Rvsdg().GetRootRegion(),
67 LlvmDeltaOperation::Create(valueType, "delta", Linkage::externalLinkage, "", true, 4));
68 auto deltaInput0 = deltaNode->AddContextVar(*x).input;
69 auto deltaInput1 = deltaNode->AddContextVar(*x).input;
70 deltaNode->AddContextVar(*x);
71
73 { deltaNode->MapInputContextVar(*deltaInput1).inner },
74 std::vector<std::shared_ptr<const Type>>{ valueType },
75 std::vector<std::shared_ptr<const Type>>{ valueType })
76 .output(0);
77
78 deltaNode->finalize(result);
79
80 // Act & Assert
81 // Try to remove deltaInput1 even though it is used
82 auto numRemovedInputs = deltaNode->RemoveDeltaInputsWhere(
83 [&](const jlm::rvsdg::Input & input)
84 {
85 return input.index() == deltaInput1->index();
86 });
87 EXPECT_EQ(numRemovedInputs, 0u);
88 EXPECT_EQ(deltaNode->ninputs(), 3u);
89 EXPECT_EQ(deltaNode->GetContextVars().size(), 3u);
90
91 // Remove deltaInput2
92 numRemovedInputs = deltaNode->RemoveDeltaInputsWhere(
93 [&](const jlm::rvsdg::Input & input)
94 {
95 return input.index() == 2;
96 });
97 EXPECT_EQ(numRemovedInputs, 1u);
98 EXPECT_EQ(deltaNode->ninputs(), 2u);
99 EXPECT_EQ(deltaNode->GetContextVars().size(), 2u);
100 EXPECT_EQ(deltaNode->input(0), deltaInput0);
101 EXPECT_EQ(deltaNode->input(1), deltaInput1);
102
103 // Remove deltaInput0
104 numRemovedInputs = deltaNode->RemoveDeltaInputsWhere(
105 [&](const jlm::rvsdg::Input & input)
106 {
107 return input.index() == 0;
108 });
109 EXPECT_EQ(numRemovedInputs, 1u);
110 EXPECT_EQ(deltaNode->ninputs(), 1u);
111 EXPECT_EQ(deltaNode->GetContextVars().size(), 1u);
112 EXPECT_EQ(deltaNode->input(0), deltaInput1);
113 EXPECT_EQ(deltaInput1->index(), 0u);
114 EXPECT_EQ(deltaNode->MapInputContextVar(*deltaInput1).inner->index(), 0u);
115}
116
117TEST(DeltaTests, TestPruneDeltaInputs)
118{
119 using namespace jlm::llvm;
120 using namespace jlm::rvsdg;
121
122 // Arrange
123 auto valueType = TestType::createValueType();
124 jlm::llvm::LlvmRvsdgModule rvsdgModule(jlm::util::FilePath(""), "", "");
125
126 auto x = &jlm::rvsdg::GraphImport::Create(rvsdgModule.Rvsdg(), valueType, "");
127
128 auto deltaNode = jlm::rvsdg::DeltaNode::Create(
129 &rvsdgModule.Rvsdg().GetRootRegion(),
130 LlvmDeltaOperation::Create(valueType, "delta", Linkage::externalLinkage, "", true, 4));
131
132 deltaNode->AddContextVar(*x);
133 auto deltaInput1 = deltaNode->AddContextVar(*x).input;
134 deltaNode->AddContextVar(*x);
135
137 { deltaNode->MapInputContextVar(*deltaInput1).inner },
138 std::vector<std::shared_ptr<const Type>>{ valueType },
139 std::vector<std::shared_ptr<const Type>>{ valueType })
140 .output(0);
141
142 deltaNode->finalize(result);
143
144 // Act
145 auto numRemovedInputs = deltaNode->PruneDeltaInputs();
146
147 // Assert
148 EXPECT_EQ(numRemovedInputs, 2u);
149 EXPECT_EQ(deltaNode->ninputs(), 1u);
150 EXPECT_EQ(deltaNode->GetContextVars().size(), 1u);
151 EXPECT_EQ(deltaNode->input(0), deltaInput1);
152 EXPECT_EQ(deltaNode->subregion()->argument(0), deltaNode->MapInputContextVar(*deltaInput1).inner);
153 EXPECT_EQ(deltaInput1->index(), 0u);
154 EXPECT_EQ(deltaNode->MapInputContextVar(*deltaInput1).inner->index(), 0u);
155}
TEST(DeltaTests, TestDeltaCreation)
static DeltaNode * Create(rvsdg::Region *parent, std::unique_ptr< DeltaOperation > op)
Definition delta.hpp:313
static GraphImport & Create(Graph &graph, std::shared_ptr< const rvsdg::Type > type, std::string name)
Definition graph.cpp:36
Region & GetRootRegion() const noexcept
Definition graph.hpp:99
size_t index() const noexcept
Definition node.hpp:52
size_t numNodes() const noexcept
Definition region.hpp:510
Graph & Rvsdg() noexcept
Global memory state passed between functions.
std::string view(const rvsdg::Region *region)
Definition view.cpp:142
NodeType * TryGetOwnerNode(const rvsdg::Input &input) noexcept
Checks if this is an input to a node of specified type.
Definition node.hpp:872