forked from ClickHouse/ClickHouse
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNative.cpp
More file actions
197 lines (165 loc) · 7.31 KB
/
Native.cpp
File metadata and controls
197 lines (165 loc) · 7.31 KB
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
#include <DataTypes/Native.h>
#if USE_EMBEDDED_COMPILER
# include <DataTypes/DataTypeNullable.h>
# include <Columns/ColumnConst.h>
# include <Columns/ColumnNullable.h>
namespace DB
{
namespace ErrorCodes
{
extern const int NOT_IMPLEMENTED;
extern const int LOGICAL_ERROR;
}
bool typeIsSigned(const IDataType & type)
{
WhichDataType data_type(type);
return data_type.isNativeInt() || data_type.isFloat() || data_type.isEnum() || data_type.isDate32();
}
llvm::Type * toNullableType(llvm::IRBuilderBase & builder, llvm::Type * type)
{
auto * is_null_type = builder.getInt1Ty();
return llvm::StructType::get(type, is_null_type);
}
bool canBeNativeType(const IDataType & type)
{
WhichDataType data_type(type);
if (data_type.isNullable())
{
const auto & data_type_nullable = static_cast<const DataTypeNullable&>(type);
return canBeNativeType(*data_type_nullable.getNestedType());
}
return data_type.isNativeInt() || data_type.isNativeUInt() || data_type.isNativeFloat() || data_type.isDate()
|| data_type.isDate32() || data_type.isDateTime() || data_type.isTime() || data_type.isEnum();
}
bool canBeNativeType(const DataTypePtr & type)
{
return canBeNativeType(*type);
}
llvm::Type * toNativeType(llvm::IRBuilderBase & builder, const IDataType & type)
{
WhichDataType data_type(type);
if (data_type.isNullable())
{
const auto & data_type_nullable = static_cast<const DataTypeNullable&>(type);
auto * nested_type = toNativeType(builder, *data_type_nullable.getNestedType());
return toNullableType(builder, nested_type);
}
/// LLVM doesn't have unsigned types, it has unsigned instructions.
if (data_type.isInt8() || data_type.isUInt8())
return builder.getInt8Ty();
if (data_type.isInt16() || data_type.isUInt16() || data_type.isDate())
return builder.getInt16Ty();
if (data_type.isInt32() || data_type.isUInt32() || data_type.isDate32() || data_type.isDateTime() || data_type.isTime())
return builder.getInt32Ty();
if (data_type.isInt64() || data_type.isUInt64())
return builder.getInt64Ty();
if (data_type.isFloat32())
return builder.getFloatTy();
if (data_type.isFloat64())
return builder.getDoubleTy();
if (data_type.isEnum8())
return builder.getInt8Ty();
if (data_type.isEnum16())
return builder.getInt16Ty();
throw Exception(ErrorCodes::LOGICAL_ERROR, "Invalid cast to native type");
}
llvm::Type * toNativeType(llvm::IRBuilderBase & builder, const DataTypePtr & type)
{
return toNativeType(builder, *type);
}
llvm::Value * nativeBoolCast(llvm::IRBuilderBase & b, const DataTypePtr & from_type, llvm::Value * value)
{
if (from_type->isNullable())
{
auto * inner = nativeBoolCast(b, removeNullable(from_type), b.CreateExtractValue(value, {0}));
return b.CreateAnd(b.CreateNot(b.CreateExtractValue(value, {1})), inner);
}
auto * zero = llvm::Constant::getNullValue(value->getType());
if (value->getType()->isIntegerTy())
return b.CreateICmpNE(value, zero);
if (value->getType()->isFloatingPointTy())
return b.CreateFCmpUNE(value, zero);
throw Exception(ErrorCodes::NOT_IMPLEMENTED, "Cannot cast non-number {} to bool", from_type->getName());
}
llvm::Value * nativeBoolCast(llvm::IRBuilderBase & b, const ValueWithType & value_with_type)
{
return nativeBoolCast(b, value_with_type.type, value_with_type.value);
}
llvm::Value * nativeCast(llvm::IRBuilderBase & b, const DataTypePtr & from_type, llvm::Value * value, const DataTypePtr & to_type)
{
if (from_type->equals(*to_type))
{
return value;
}
if (from_type->isNullable() && to_type->isNullable())
{
auto * inner = nativeCast(b, removeNullable(from_type), b.CreateExtractValue(value, {0}), to_type);
return b.CreateInsertValue(inner, b.CreateExtractValue(value, {1}), {1});
}
if (from_type->isNullable())
{
return nativeCast(b, removeNullable(from_type), b.CreateExtractValue(value, {0}), to_type);
}
if (to_type->isNullable())
{
auto * to_native_type = toNativeType(b, to_type);
auto * inner = nativeCast(b, from_type, value, removeNullable(to_type));
return b.CreateInsertValue(llvm::Constant::getNullValue(to_native_type), inner, {0});
}
auto * from_native_type = toNativeType(b, from_type);
auto * to_native_type = toNativeType(b, to_type);
if (from_native_type == to_native_type)
return value;
if (from_native_type->isIntegerTy() && to_native_type->isFloatingPointTy())
return typeIsSigned(*from_type) ? b.CreateSIToFP(value, to_native_type) : b.CreateUIToFP(value, to_native_type);
if (from_native_type->isFloatingPointTy() && to_native_type->isIntegerTy())
return typeIsSigned(*to_type) ? b.CreateFPToSI(value, to_native_type) : b.CreateFPToUI(value, to_native_type);
if (from_native_type->isIntegerTy() && from_native_type->isIntegerTy())
return b.CreateIntCast(value, to_native_type, typeIsSigned(*from_type));
if (to_native_type->isFloatingPointTy() && to_native_type->isFloatingPointTy())
return b.CreateFPCast(value, to_native_type);
throw Exception(ErrorCodes::LOGICAL_ERROR,
"Invalid cast to native value from type {} to type {}",
from_type->getName(),
to_type->getName());
}
llvm::Value * nativeCast(llvm::IRBuilderBase & b, const ValueWithType & value, const DataTypePtr & to_type)
{
return nativeCast(b, value.type, value.value, to_type);
}
llvm::Constant * getColumnNativeValue(llvm::IRBuilderBase & builder, const DataTypePtr & column_type, const IColumn & column, size_t index)
{
if (const auto * constant = typeid_cast<const ColumnConst *>(&column))
return getColumnNativeValue(builder, column_type, constant->getDataColumn(), 0);
auto * type = toNativeType(builder, column_type);
WhichDataType column_data_type(column_type);
if (column_data_type.isNullable())
{
const auto & nullable_data_type = assert_cast<const DataTypeNullable &>(*column_type);
const auto & nullable_column = assert_cast<const ColumnNullable &>(column);
auto * value = getColumnNativeValue(builder, nullable_data_type.getNestedType(), nullable_column.getNestedColumn(), index);
auto * is_null = llvm::ConstantInt::get(type->getContainedType(1), nullable_column.isNullAt(index));
return llvm::ConstantStruct::get(static_cast<llvm::StructType *>(type), value, is_null);
}
if (column_data_type.isFloat32())
{
return llvm::ConstantFP::get(type, assert_cast<const ColumnVector<Float32> &>(column).getElement(index));
}
if (column_data_type.isFloat64())
{
return llvm::ConstantFP::get(type, assert_cast<const ColumnVector<Float64> &>(column).getElement(index));
}
if (column_data_type.isNativeUInt() || column_data_type.isDate() || column_data_type.isDateTime() || column_data_type.isTime())
{
return llvm::ConstantInt::get(type, column.getUInt(index));
}
if (column_data_type.isNativeInt() || column_data_type.isEnum() || column_data_type.isDate32())
{
return llvm::ConstantInt::get(type, column.getInt(index));
}
throw Exception(ErrorCodes::LOGICAL_ERROR,
"Cannot get native value for column with type {}",
column_type->getName());
}
}
#endif