aboutsummaryrefslogtreecommitdiffstats
path: root/eval/src/vespa/eval/eval/interpreted_function.cpp
blob: d51b8ae0b39f475ac5c852b4fa41814b490f0943 (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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include "interpreted_function.h"
#include "node_visitor.h"
#include "node_traverser.h"
#include "tensor_nodes.h"
#include "make_tensor_function.h"
#include "optimize_tensor_function.h"
#include "compile_tensor_function.h"
#include <vespa/vespalib/util/classname.h>
#include <vespa/eval/eval/llvm/compile_cache.h>
#include <vespa/eval/eval/llvm/addr_to_symbol.h>
#include <vespa/vespalib/util/benchmark_timer.h>
#include <vespa/vespalib/util/stringfmt.h>
#include <set>

namespace vespalib::eval {

namespace {

const Function *get_lambda(const nodes::Node &node) {
    if (auto ptr = nodes::as<nodes::TensorMap>(node)) {
        return &ptr->lambda();
    }
    if (auto ptr = nodes::as<nodes::TensorJoin>(node)) {
        return &ptr->lambda();
    }
    if (auto ptr = nodes::as<nodes::TensorMerge>(node)) {
        return &ptr->lambda();
    }
    return nullptr;
}

void my_nop(InterpretedFunction::State &, uint64_t) {}

} // namespace vespalib::<unnamed>


InterpretedFunction::State::State(const ValueBuilderFactory &factory_in)
    : factory(factory_in),
      params(nullptr),
      stash(),
      stack(),
      program_offset(0),
      if_cnt(0)
{
}

InterpretedFunction::State::~State() = default;

void
InterpretedFunction::State::init(const LazyParams &params_in) {
    params = &params_in;
    stash.clear();
    stack.clear();
    program_offset = 0;
    if_cnt = 0;
}

InterpretedFunction::Context::Context(const InterpretedFunction &ifun)
    : _state(ifun._factory)
{
}

vespalib::string
InterpretedFunction::Instruction::resolve_symbol() const
{
    if (function == nullptr) {
        return "<inject_param>";
    }
    return addr_to_symbol((const void *)function);
}

InterpretedFunction::Instruction
InterpretedFunction::Instruction::nop()
{
    return Instruction(my_nop);
}

InterpretedFunction::InterpretedFunction(const ValueBuilderFactory &factory, const TensorFunction &function, CTFMetaData *meta)
    : _program(),
      _stash(),
      _factory(factory)
{
    _program = compile_tensor_function(factory, function, _stash, meta);
}

InterpretedFunction::InterpretedFunction(const ValueBuilderFactory &factory, const nodes::Node &root, const NodeTypes &types)
    : _program(),
      _stash(),
      _factory(factory)
{
    const TensorFunction &plain_fun = make_tensor_function(factory, root, types, _stash);
    const TensorFunction &optimized = optimize_tensor_function(factory, plain_fun, _stash);
    _program = compile_tensor_function(factory, optimized, _stash, nullptr);
}

InterpretedFunction::~InterpretedFunction() = default;

const Value &
InterpretedFunction::eval(Context &ctx, const LazyParams &params) const
{
    State &state = ctx._state;
    state.init(params);
    while (state.program_offset < _program.size()) {
        _program[state.program_offset++].perform(state);
    }
    assert(state.stack.size() == 1);
    return state.stack.back();
}

double
InterpretedFunction::estimate_cost_us(const std::vector<double> &params, double budget) const
{
    Context ctx(*this);
    SimpleParams lazy_params(params);
    auto actual = [&](){eval(ctx, lazy_params);};
    return BenchmarkTimer::benchmark(actual, budget) * 1000.0 * 1000.0;
}

Function::Issues
InterpretedFunction::detect_issues(const Function &function)
{
    struct NotSupported : NodeTraverser {
        std::vector<vespalib::string> issues;
        bool open(const nodes::Node &) override { return true; }
        void close(const nodes::Node &node) override {
            auto lambda = get_lambda(node);
            if (lambda && CompiledFunction::detect_issues(*lambda)) {
                issues.push_back(make_string("lambda function that cannot be compiled within %s",
                                getClassName(node).c_str()));
            }
        }
    } checker;
    function.root().traverse(checker);
    return Function::Issues(std::move(checker.issues));
}

InterpretedFunction::EvalSingle::EvalSingle(const ValueBuilderFactory &factory, Instruction op, const LazyParams &params)
    : _state(factory),
      _op(op)
{
    _state.params = &params;
}

const Value &
InterpretedFunction::EvalSingle::eval(const std::vector<Value::CREF> &stack)
{
    _state.stash.clear();
    _state.stack = stack;
    _op.perform(_state);
    assert(_state.stack.size() == 1);
    return _state.stack.back();
}

}