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
|
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
#include "compile_tensor_function.h"
#include "tensor_function.h"
#include <vespa/vespalib/util/classname.h>
namespace vespalib::eval {
namespace {
using vespalib::getClassName;
using State = InterpretedFunction::State;
using Instruction = InterpretedFunction::Instruction;
void op_skip(State &state, uint64_t param) {
state.program_offset += param;
}
void op_skip_if_false(State &state, uint64_t param) {
++state.if_cnt;
if (!state.peek(0).as_bool()) {
state.program_offset += param;
}
state.stack.pop_back();
}
struct Frame {
const TensorFunction &node;
std::vector<TensorFunction::Child::CREF> children;
size_t child_idx;
Frame(const TensorFunction &node_in) : node(node_in), children(), child_idx(0) { node.push_children(children); }
bool has_next_child() const { return (child_idx < children.size()); }
const TensorFunction &next_child() { return children[child_idx++].get().get(); }
};
struct ProgramCompiler {
CTFContext ctx;
std::vector<Frame> stack;
std::vector<Instruction> prog;
ProgramCompiler(const CTFContext &ctx_in)
: ctx(ctx_in), stack(), prog() {}
~ProgramCompiler();
void add_meta(const TensorFunction &node, const Instruction &instr, CTFMetaData *nested) {
ctx.meta->steps.emplace_back(getClassName(node), instr.resolve_symbol());
if (nested) {
ctx.meta->steps.back().nested = nested->extract();
}
}
void maybe_add_meta(const TensorFunction &node, const Instruction &instr) {
if (ctx.meta != nullptr) {
add_meta(node, instr, nullptr);
}
}
void append(const std::vector<Instruction> &other_prog) {
prog.insert(prog.end(), other_prog.begin(), other_prog.end());
}
void open(const TensorFunction &node) {
if (auto if_node = as<tensor_function::If>(node)) {
append(compile_tensor_function(if_node->cond(), ctx));
maybe_add_meta(node, Instruction(op_skip_if_false));
auto true_prog = compile_tensor_function(if_node->true_child(), ctx);
maybe_add_meta(node, Instruction(op_skip));
auto false_prog = compile_tensor_function(if_node->false_child(), ctx);
true_prog.emplace_back(op_skip, false_prog.size());
prog.emplace_back(op_skip_if_false, true_prog.size());
append(true_prog);
append(false_prog);
} else {
stack.emplace_back(node);
}
}
void close(const TensorFunction &node) {
if (ctx.meta == nullptr) {
prog.push_back(node.compile_self(ctx));
} else {
CTFMetaData sub_meta;
CTFContext sub_context = ctx;
sub_context.meta = &sub_meta;
prog.push_back(node.compile_self(sub_context));
add_meta(node, prog.back(), &sub_meta);
}
}
std::vector<Instruction> compile(const TensorFunction &function) {
open(function);
while (!stack.empty()) {
if (stack.back().has_next_child()) {
open(stack.back().next_child());
} else {
close(stack.back().node);
stack.pop_back();
}
}
return std::move(prog);
}
};
ProgramCompiler::~ProgramCompiler() = default;
} // namespace vespalib::eval::<unnamed>
CTFMetaData::~CTFMetaData() = default;
std::vector<Instruction> compile_tensor_function(const TensorFunction &function, const CTFContext &ctx) {
ProgramCompiler compiler(ctx);
return compiler.compile(function);
}
} // namespace vespalib::eval
|