Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add test cases and fix
Browse files Browse the repository at this point in the history
long-long-float committed Aug 29, 2020

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
1 parent dbecf3d commit eefdda8
Showing 3 changed files with 72 additions and 19 deletions.
46 changes: 30 additions & 16 deletions src/optimization/ValueExpr.cpp
Original file line number Diff line number Diff line change
@@ -27,8 +27,24 @@ std::shared_ptr<ValueExpr> ValueBinaryOp::replaceLocal(const Value& value, std::

void ValueBinaryOp::expand(ExpandedExprs& exprs)
{
auto leftNum = left->getInteger();
auto rightNum = right->getInteger();
ExpandedExprs leftEE, rightEE;
left->expand(leftEE);
right->expand(rightEE);

auto getInteger = [](const std::pair<bool, std::shared_ptr<ValueExpr>> &v) {
std::function<Optional<int>(const int&)> addSign = [&](const int& num) {
return make_optional(v.first ? num : -num);
};
return v.second->getInteger() & addSign;
};

auto leftNum = (leftEE.size() == 1) ? getInteger(leftEE[0]) : Optional<int>();
auto rightNum = (rightEE.size() == 1) ? getInteger(rightEE[0]) : Optional<int>();

auto append = [](ExpandedExprs &ee1, ExpandedExprs &ee2) {
ee1.insert(ee1.end(), ee2.begin(), ee2.end());
};

if(leftNum && rightNum)
{
int l = leftNum.value_or(0);
@@ -63,42 +79,40 @@ void ValueBinaryOp::expand(ExpandedExprs& exprs)
{
case BinaryOp::Add:
{
left->expand(exprs);
right->expand(exprs);
append(exprs, leftEE);
append(exprs, rightEE);
break;
}
case BinaryOp::Sub:
{
left->expand(exprs);
append(exprs, leftEE);

ExpandedExprs temp;
right->expand(temp);
for(auto& e : temp)
for(auto& e : rightEE)
{
e.first = !e.first;
}
exprs.insert(exprs.end(), temp.begin(), temp.end());
append(exprs, rightEE);
break;
}
case BinaryOp::Mul:
{
if(leftNum || rightNum)
{
int num = 0;
std::shared_ptr<ValueExpr> expr = nullptr;
ExpandedExprs *ee = nullptr;
if(leftNum)
{
num = leftNum.value_or(0);
expr = right;
ee = &rightEE;
}
else
{
num = rightNum.value_or(0);
expr = left;
ee = &leftEE;
}
for(int i = 0; i < num; i++)
{
exprs.push_back(std::make_pair(true, expr));
append(exprs, *ee);
}
}
else
@@ -148,10 +162,11 @@ std::string ValueBinaryOp::to_string() const
return "(" + left->to_string() + " " + opStr + " " + right->to_string() + ")";
}

std::shared_ptr<ValueExpr> optimizations::makeValueBinaryOpFromLocal(Value& left, ValueBinaryOp::BinaryOp binOp, Value& right)
std::shared_ptr<ValueExpr> optimizations::makeValueBinaryOpFromLocal(
Value& left, ValueBinaryOp::BinaryOp binOp, Value& right)
{
return std::make_shared<ValueBinaryOp>(
std::make_shared<ValueTerm>(left), binOp, std::make_shared<ValueTerm>(right));
std::make_shared<ValueTerm>(left), binOp, std::make_shared<ValueTerm>(right));
}

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

2 changes: 1 addition & 1 deletion src/optimization/ValueExpr.h
Original file line number Diff line number Diff line change
@@ -18,7 +18,7 @@ namespace vc4c
class ValueExpr
{
public:
// (signed, value)
// signed, value
using ExpandedExprs = std::vector<std::pair<bool, std::shared_ptr<ValueExpr>>>;

virtual ~ValueExpr() = default;
43 changes: 41 additions & 2 deletions test/TestOptimizationSteps.cpp
Original file line number Diff line number Diff line change
@@ -17,6 +17,8 @@
#include "optimization/Flags.h"
#include "periphery/VPM.h"

#include "optimization/ValueExpr.h"

#include <cmath>

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

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

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

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

testCombineDMALoadsSub(module, inputMethod, config, Uchar16);
}

{
// vload16f * 3

Module module{config};
Method inputMethod(module);

auto inIt = inputMethod.createAndInsertNewBlock(inputMethod.end(), "%dummy").walkEnd();
auto in = assign(inIt, TYPE_INT32, "%in") = UNIFORM_REGISTER;
auto offset1 = assign(inIt, TYPE_INT32, "%offset1") = 42_val;
auto offset2 = assign(inIt, TYPE_INT32, "%offset2") = offset1 + 1_val;
auto offset3 = assign(inIt, TYPE_INT32, "%offset3") = offset1 + 2_val;

putMethodCall(inputMethod, inIt, Float16, vload16f, {offset3, in});
putMethodCall(inputMethod, inIt, Float16, vload16f, {offset2, in});
putMethodCall(inputMethod, inIt, Float16, vload16f, {offset1, in});

testCombineDMALoadsSub(module, inputMethod, config, Float16);
}

{
// ValueExpr::expand

Literal l(2);
Value a(l, TYPE_INT32);
Value b = 3_val;
std::shared_ptr<ValueExpr> expr(new ValueBinaryOp(
makeValueBinaryOpFromLocal(a, ValueBinaryOp::BinaryOp::Add, b),
ValueBinaryOp::BinaryOp::Sub,
std::make_shared<ValueTerm>(1_val)));
ValueExpr::ExpandedExprs expanded;
expr->expand(expanded);

TEST_ASSERT_EQUALS(1, expanded.size());

auto n = expanded[0].second->getInteger();
TEST_ASSERT_EQUALS(4, n.value_or(0));
}
}

0 comments on commit eefdda8

Please sign in to comment.