aboutsummaryrefslogtreecommitdiffstats
path: root/eval/src/vespa/eval/instruction/remove_trivial_dimension_optimizer.cpp
blob: e983cd782cb817b688b5bbd9a55a4a0b0902bf0a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include "remove_trivial_dimension_optimizer.h"
#include "replace_type_function.h"
#include <vespa/eval/eval/value_type.h>

namespace vespalib::eval {

using namespace tensor_function;

namespace {

bool is_trivial_dim_list(const ValueType &type, const std::vector<vespalib::string> &dim_list) {
    size_t npos = ValueType::Dimension::npos;
    for (const vespalib::string &dim: dim_list) {
        size_t idx = type.dimension_index(dim);
        if ((idx == npos) || (type.dimensions()[idx].size != 1)) {
            return false;
        }
    }
    return true;
}

} // namespace vespalib::eval::<unnamed>

const TensorFunction &
RemoveTrivialDimensionOptimizer::optimize(const TensorFunction &expr, Stash &stash)
{
    if (auto reduce = as<Reduce>(expr)) {
        const TensorFunction &child = reduce->child();
        if (expr.result_type().has_dimensions() &&
            aggr::is_ident(reduce->aggr()) &&
            is_trivial_dim_list(child.result_type(), reduce->dimensions()) &&
            (expr.result_type().cell_type() == child.result_type().cell_type()))
        {
            return ReplaceTypeFunction::create_compact(expr.result_type(), child, stash);
        }
    }
    return expr;
}

} // namespace vespalib::eval