Skip to content

Commit f5c9e03

Browse files
Add test cases and fix
1 parent 571ad19 commit f5c9e03

File tree

3 files changed

+72
-19
lines changed

3 files changed

+72
-19
lines changed

src/optimization/ValueExpr.cpp

Lines changed: 30 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,24 @@ std::shared_ptr<ValueExpr> ValueBinaryOp::replaceLocal(const Value& value, std::
2727

2828
void ValueBinaryOp::expand(ExpandedExprs& exprs)
2929
{
30-
auto leftNum = left->getInteger();
31-
auto rightNum = right->getInteger();
30+
ExpandedExprs leftEE, rightEE;
31+
left->expand(leftEE);
32+
right->expand(rightEE);
33+
34+
auto getInteger = [](const std::pair<bool, std::shared_ptr<ValueExpr>> &v) {
35+
std::function<Optional<int>(const int&)> addSign = [&](const int& num) {
36+
return make_optional(v.first ? num : -num);
37+
};
38+
return v.second->getInteger() & addSign;
39+
};
40+
41+
auto leftNum = (leftEE.size() == 1) ? getInteger(leftEE[0]) : Optional<int>();
42+
auto rightNum = (rightEE.size() == 1) ? getInteger(rightEE[0]) : Optional<int>();
43+
44+
auto append = [](ExpandedExprs &ee1, ExpandedExprs &ee2) {
45+
ee1.insert(ee1.end(), ee2.begin(), ee2.end());
46+
};
47+
3248
if(leftNum && rightNum)
3349
{
3450
int l = leftNum.value_or(0);
@@ -63,42 +79,40 @@ void ValueBinaryOp::expand(ExpandedExprs& exprs)
6379
{
6480
case BinaryOp::Add:
6581
{
66-
left->expand(exprs);
67-
right->expand(exprs);
82+
append(exprs, leftEE);
83+
append(exprs, rightEE);
6884
break;
6985
}
7086
case BinaryOp::Sub:
7187
{
72-
left->expand(exprs);
88+
append(exprs, leftEE);
7389

74-
ExpandedExprs temp;
75-
right->expand(temp);
76-
for(auto& e : temp)
90+
for(auto& e : rightEE)
7791
{
7892
e.first = !e.first;
7993
}
80-
exprs.insert(exprs.end(), temp.begin(), temp.end());
94+
append(exprs, rightEE);
8195
break;
8296
}
8397
case BinaryOp::Mul:
8498
{
8599
if(leftNum || rightNum)
86100
{
87101
int num = 0;
88-
std::shared_ptr<ValueExpr> expr = nullptr;
102+
ExpandedExprs *ee = nullptr;
89103
if(leftNum)
90104
{
91105
num = leftNum.value_or(0);
92-
expr = right;
106+
ee = &rightEE;
93107
}
94108
else
95109
{
96110
num = rightNum.value_or(0);
97-
expr = left;
111+
ee = &leftEE;
98112
}
99113
for(int i = 0; i < num; i++)
100114
{
101-
exprs.push_back(std::make_pair(true, expr));
115+
append(exprs, *ee);
102116
}
103117
}
104118
else
@@ -148,10 +162,11 @@ std::string ValueBinaryOp::to_string() const
148162
return "(" + left->to_string() + " " + opStr + " " + right->to_string() + ")";
149163
}
150164

151-
std::shared_ptr<ValueExpr> optimizations::makeValueBinaryOpFromLocal(Value& left, ValueBinaryOp::BinaryOp binOp, Value& right)
165+
std::shared_ptr<ValueExpr> optimizations::makeValueBinaryOpFromLocal(
166+
Value& left, ValueBinaryOp::BinaryOp binOp, Value& right)
152167
{
153168
return std::make_shared<ValueBinaryOp>(
154-
std::make_shared<ValueTerm>(left), binOp, std::make_shared<ValueTerm>(right));
169+
std::make_shared<ValueTerm>(left), binOp, std::make_shared<ValueTerm>(right));
155170
}
156171

157172
bool ValueTerm::operator==(const ValueExpr& other) const
@@ -198,4 +213,3 @@ std::string ValueTerm::to_string() const
198213
{
199214
return value.to_string();
200215
}
201-

src/optimization/ValueExpr.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ namespace vc4c
1818
class ValueExpr
1919
{
2020
public:
21-
// (signed, value)
21+
// signed, value
2222
using ExpandedExprs = std::vector<std::pair<bool, std::shared_ptr<ValueExpr>>>;
2323

2424
virtual ~ValueExpr() = default;

test/TestOptimizationSteps.cpp

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
#include "optimization/Flags.h"
1818
#include "periphery/VPM.h"
1919

20+
#include "optimization/ValueExpr.h"
21+
2022
#include <cmath>
2123

2224
#include "log.h"
@@ -1990,7 +1992,6 @@ void TestOptimizationSteps::testCombineDMALoads()
19901992
using namespace vc4c::intermediate;
19911993

19921994
auto testCombineDMALoadsSub = [&](Module& module, Method& inputMethod, Configuration& config, DataType vectorType) {
1993-
// TODO: Add a case that the first argument of vload16 is a variable.
19941995

19951996
const int numOfLoads = 3;
19961997
periphery::VPRDMASetup expectedDMASetup(0, vectorType.getVectorWidth() % 16, numOfLoads, 1, 0);
@@ -2074,7 +2075,7 @@ void TestOptimizationSteps::testCombineDMALoads()
20742075
const std::string vload16f = "_Z7vload16jPU3AS1Kf";
20752076
// vload8(size_t, const float*)
20762077
const std::string vload8f = "_Z6vload8jPU3AS1Kf";
2077-
// vload16(size_t, const float*)
2078+
// vload16(size_t, const uchar*)
20782079
const std::string vload16uc = "_Z7vload16jPU3AS1Kh";
20792080

20802081
Configuration config{};
@@ -2126,4 +2127,42 @@ void TestOptimizationSteps::testCombineDMALoads()
21262127

21272128
testCombineDMALoadsSub(module, inputMethod, config, Uchar16);
21282129
}
2130+
2131+
{
2132+
// vload16f * 3
2133+
2134+
Module module{config};
2135+
Method inputMethod(module);
2136+
2137+
auto inIt = inputMethod.createAndInsertNewBlock(inputMethod.end(), "%dummy").walkEnd();
2138+
auto in = assign(inIt, TYPE_INT32, "%in") = UNIFORM_REGISTER;
2139+
auto offset1 = assign(inIt, TYPE_INT32, "%offset1") = 42_val;
2140+
auto offset2 = assign(inIt, TYPE_INT32, "%offset2") = offset1 + 1_val;
2141+
auto offset3 = assign(inIt, TYPE_INT32, "%offset3") = offset1 + 2_val;
2142+
2143+
putMethodCall(inputMethod, inIt, Float16, vload16f, {offset3, in});
2144+
putMethodCall(inputMethod, inIt, Float16, vload16f, {offset2, in});
2145+
putMethodCall(inputMethod, inIt, Float16, vload16f, {offset1, in});
2146+
2147+
testCombineDMALoadsSub(module, inputMethod, config, Float16);
2148+
}
2149+
2150+
{
2151+
// ValueExpr::expand
2152+
2153+
Literal l(2);
2154+
Value a(l, TYPE_INT32);
2155+
Value b = 3_val;
2156+
std::shared_ptr<ValueExpr> expr(new ValueBinaryOp(
2157+
makeValueBinaryOpFromLocal(a, ValueBinaryOp::BinaryOp::Add, b),
2158+
ValueBinaryOp::BinaryOp::Sub,
2159+
std::make_shared<ValueTerm>(1_val)));
2160+
ValueExpr::ExpandedExprs expanded;
2161+
expr->expand(expanded);
2162+
2163+
TEST_ASSERT_EQUALS(1, expanded.size());
2164+
2165+
auto n = expanded[0].second->getInteger();
2166+
TEST_ASSERT_EQUALS(4, n.value_or(0));
2167+
}
21292168
}

0 commit comments

Comments
 (0)