Fletchgen
The Fletcher Design Generator
basic_types.h
1 // Copyright 2018-2019 Delft University of Technology
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #pragma once
16 
17 #include <arrow/api.h>
18 #include <cerata/api.h>
19 #include <fletcher/common.h>
20 #include <string>
21 #include <memory>
22 
23 namespace fletchgen {
24 
26 namespace meta {
28 constexpr char ARRAY_DATA[] = "fletchgen_array_data";
30 constexpr char COUNT[] = "fletchgen_count";
32 constexpr char LAST[] = "fletchgen_last";
33 }
34 
35 using cerata::Type;
36 using cerata::Node;
37 using cerata::ClockDomain;
38 using cerata::TypeMapper;
39 using cerata::Parameter;
40 
41 // Generate declaration for basic types corresponding to and in the manner of Arrow's types
42 #define BIT_DECL_FACTORY(NAME) std::shared_ptr<Type> NAME();
43 
45 #define BIT_FACTORY(NAME) \
46  std::shared_ptr<Type> NAME() { \
47  static std::shared_ptr<Type> result = bit(); \
48  return result; \
49  }
50 
52 #define VEC_DECL_FACTORY(NAME, WIDTH) std::shared_ptr<Type> NAME();
53 
55 #define VEC_FACTORY(NAME, WIDTH) \
56  std::shared_ptr<Type> NAME() { \
57  static std::shared_ptr<Type> result = vector(#NAME, WIDTH); \
58  return result; \
59  }
60 
62 #define PARAM_DECL_FACTORY(NAME, VALUE) std::shared_ptr<Parameter> NAME(int64_t value = VALUE, \
63  const std::string& prefix = "");
64 
66 #define PARAM_FACTORY(NAME) \
67 std::shared_ptr<Parameter> NAME(int64_t value, const std::string& prefix) { \
68  auto name = std::string(#NAME); \
69  for (auto &ch : name) ch = std::toupper(ch); \
70  if (!prefix.empty()) {name = prefix + "_" + name;} \
71  auto result = parameter(name, cerata::integer(), intl(value)); \
72  return result; \
73 }
74 
75 // Arrow equivalent Cerata types:
76 BIT_DECL_FACTORY(validity)
77 VEC_DECL_FACTORY(bool_, 1)
78 VEC_DECL_FACTORY(int8, 8)
79 VEC_DECL_FACTORY(uint8, 8)
80 VEC_DECL_FACTORY(int16, 16)
81 VEC_DECL_FACTORY(uint16, 16)
82 VEC_DECL_FACTORY(int32, 32)
83 VEC_DECL_FACTORY(uint32, 32)
84 VEC_DECL_FACTORY(int64, 64)
85 VEC_DECL_FACTORY(uint64, 64)
86 VEC_DECL_FACTORY(float8, 8)
87 VEC_DECL_FACTORY(float16, 16)
88 VEC_DECL_FACTORY(float32, 32)
89 VEC_DECL_FACTORY(float64, 64)
90 VEC_DECL_FACTORY(date32, 32)
91 VEC_DECL_FACTORY(date64, 64)
92 VEC_DECL_FACTORY(date32, 32)
93 VEC_DECL_FACTORY(date64, 64)
94 VEC_DECL_FACTORY(time32, 32)
95 VEC_DECL_FACTORY(time64, 64)
96 VEC_DECL_FACTORY(timestamp, 64)
97 VEC_DECL_FACTORY(decimal128, 128)
98 VEC_DECL_FACTORY(utf8c, 8)
99 VEC_DECL_FACTORY(byte, 8)
100 VEC_DECL_FACTORY(offset, 32)
101 
103 std::shared_ptr<ClockDomain> kernel_cd();
105 std::shared_ptr<ClockDomain> bus_cd();
107 std::shared_ptr<Type> cr();
109 std::shared_ptr<Type> valid(int width = 1, bool on_primitive = false);
111 std::shared_ptr<Type> ready(int width = 1, bool on_primitive = false);
113 std::shared_ptr<Type> data(int width);
115 std::shared_ptr<Type> length(int width);
117 std::shared_ptr<Type> count(int width);
119 std::shared_ptr<Type> dvalid(int width = 1, bool on_primitive = false);
121 std::shared_ptr<Type> last(int width = 1, bool on_primitive = false);
122 
131 std::shared_ptr<Type> ConvertFixedWidthType(const std::shared_ptr<arrow::DataType> &arrow_type, int epc = 1);
132 
139 std::optional<cerata::Port *> GetClockResetPort(cerata::Graph *graph, const ClockDomain &domain);
140 
141 
147 int GetFixedWidthTypeBitWidth(const arrow::DataType &arrow_type);
148 
149 } // namespace fletchgen
constexpr char LAST[]
Key to mark the last field in Arrow data streams.
Definition: basic_types.h:32
constexpr char ARRAY_DATA[]
Key for automated type mapping.
Definition: basic_types.h:28
constexpr char COUNT[]
Key to mark the count field in Arrow data streams.
Definition: basic_types.h:30
Contains all classes and functions related to Fletchgen.
Definition: array.cc:29
std::shared_ptr< ClockDomain > kernel_cd()
Fletcher accelerator clock domain.
Definition: basic_types.cc:62
std::shared_ptr< Type > valid(int width, bool on_primitive)
Fletcher valid.
Definition: basic_types.cc:81
std::shared_ptr< Type > cr()
Fletcher clock/reset;.
Definition: basic_types.cc:73
std::shared_ptr< Type > data(int width)
Fletcher data.
Definition: basic_types.cc:98
std::shared_ptr< Type > ConvertFixedWidthType(const std::shared_ptr< arrow::DataType > &arrow_type, int epc)
Convert a fixed-width arrow::DataType to a fixed-width Fletcher Type.
Definition: basic_types.cc:148
std::shared_ptr< Type > ready(int width, bool on_primitive)
Fletcher ready.
Definition: basic_types.cc:89
std::shared_ptr< Type > dvalid(int width, bool on_primitive)
Fletcher dvalid.
Definition: basic_types.cc:121
std::shared_ptr< Type > length(int width)
Fletcher length.
Definition: basic_types.cc:106
std::shared_ptr< Type > count(int width)
Fletcher count.
Definition: basic_types.cc:113
int GetFixedWidthTypeBitWidth(const arrow::DataType &arrow_type)
Returns the bit-width of a fixed-width Arrow type. Throws if it's not a fixed-width type.
Definition: basic_types.cc:140
std::shared_ptr< ClockDomain > bus_cd()
Fletcher bus clock domain.
Definition: basic_types.cc:67
std::shared_ptr< Type > last(int width, bool on_primitive)
Fletcher last.
Definition: basic_types.cc:129
std::optional< cerata::Port * > GetClockResetPort(cerata::Graph *graph, const ClockDomain &domain)
Return the clock/reset port of a graph for a specific clock domain, if it exists.
Definition: basic_types.cc:181