aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/clickhouse/src/Interpreters/JIT/CompileDAG.h
blob: 776bd0fa8ed3df4495a3dc9af0dcf3660f3eb715 (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
#pragma once

#include "clickhouse_config.h"

#if USE_EMBEDDED_COMPILER

#include <vector>

#include <Core/Types.h>

#include <Columns/IColumn.h>
#include <DataTypes/IDataType.h>
#include <Functions/IFunction.h>

namespace llvm
{
    class Value;
    class IRBuilderBase;
}

namespace DB
{

/** This class is needed to compile part of ActionsDAG.
  * For example we have expression (a + 1) + (b + 1) in actions dag.
  * It must be added into CompileDAG in order of compile evaluation.
  * Node a, Constant 1, Function add(a + 1), Input b, Constant 1, Function add(b, 1), Function add(add(a + 1), add(a + 1)).
  *
  * Compile function must be called with input_nodes_values equal to input nodes count.
  * When compile method is called added nodes are compiled in order.
  */
class CompileDAG
{
public:

    enum class CompileType
    {
        INPUT = 0,
        CONSTANT = 1,
        FUNCTION = 2,
    };

    struct Node
    {
        CompileType type;
        DataTypePtr result_type;

        /// For CONSTANT
        ColumnPtr column;

        /// For FUNCTION
        FunctionBasePtr function;
        std::vector<size_t> arguments;
    };

    ValueWithType compile(llvm::IRBuilderBase & builder, const ValuesWithType & input_nodes_values_with_type) const;

    std::string dump() const;

    UInt128 hash() const;

    void addNode(Node node)
    {
        input_nodes_count += (node.type == CompileType::INPUT);
        nodes.emplace_back(std::move(node));
    }

    inline size_t getNodesCount() const { return nodes.size(); }
    inline size_t getInputNodesCount() const { return input_nodes_count; }

    inline Node & operator[](size_t index) { return nodes[index]; }
    inline const Node & operator[](size_t index) const { return nodes[index]; }

    inline Node & front() { return nodes.front(); }
    inline const Node & front() const { return nodes.front(); }

    inline Node & back() { return nodes.back(); }
    inline const Node & back() const { return nodes.back(); }

private:
    std::vector<Node> nodes;
    size_t input_nodes_count = 0;
};

}

#endif