OmniSciDB  72c90bc290
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
PointConstructor.h
Go to the documentation of this file.
1 /*
2  * Copyright 2022 HEAVY.AI, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #pragma once
18 
20 
21 #include "Shared/SqlTypesLayout.h"
22 
23 namespace spatial_type {
24 
25 // ST_Point
26 class PointConstructor : public Codegen {
27  public:
28  PointConstructor(const Analyzer::GeoOperator* geo_operator) : Codegen(geo_operator) {
29  CHECK_EQ(operator_->size(), size_t(2));
30  const auto& ti = geo_operator->get_type_info();
31  if (ti.get_notnull()) {
32  is_nullable_ = false;
33  } else {
34  is_nullable_ = true;
35  }
36  }
37 
38  std::unique_ptr<CodeGenerator::NullCheckCodegen> getNullCheckCodegen(
39  llvm::Value* null_lv,
40  CgenState* cgen_state,
41  Executor* executor) final {
42  if (isNullable()) {
43  // do the standard nullcheck codegen, but modify the null basic block to emplace the
44  // null sentinel into the array
45  auto nullcheck_codegen = std::make_unique<CodeGenerator::NullCheckCodegen>(
46  cgen_state, executor, null_lv, getNullType(), getName() + "_nullcheck");
47 
48  auto& builder = cgen_state->ir_builder_;
50 
51  auto prev_insert_block = builder.GetInsertBlock();
52  auto crt_insert_block = nullcheck_codegen->null_check->cond_true_;
53  CHECK(crt_insert_block);
54  auto& instruction_list = crt_insert_block->getInstList();
55  CHECK_EQ(instruction_list.size(), size_t(1));
56  builder.SetInsertPoint(crt_insert_block, instruction_list.begin());
57 
58  auto x_coord_ptr = builder.CreateGEP(
59  pt_local_storage_lv_->getType()->getScalarType()->getPointerElementType(),
61  {cgen_state->llInt(0), cgen_state->llInt(0)},
62  "x_coord_ptr");
63  const auto& geo_ti = operator_->get_type_info();
64  if (geo_ti.get_compression() == kENCODING_GEOINT) {
65  // TODO: probably wrong
66  builder.CreateStore(cgen_state->llInt(inline_int_null_val(SQLTypeInfo(kINT))),
67  x_coord_ptr);
68  } else {
69  builder.CreateStore(cgen_state->llFp(static_cast<double>(NULL_ARRAY_DOUBLE)),
70  x_coord_ptr);
71  }
72  builder.SetInsertPoint(prev_insert_block);
73 
74  return nullcheck_codegen;
75  } else {
76  return nullptr;
77  }
78  }
79 
80  size_t size() const final { return 2; }
81 
82  SQLTypeInfo getNullType() const final { return SQLTypeInfo(kBOOLEAN); }
83 
84  // returns arguments lvs and null lv
85  std::tuple<std::vector<llvm::Value*>, llvm::Value*> codegenLoads(
86  const std::vector<llvm::Value*>& arg_lvs,
87  const std::vector<llvm::Value*>& pos_lvs,
88  CgenState* cgen_state) final {
89  CHECK_EQ(pos_lvs.size(),
90  size()); // note both pos arguments should be the same, as both inputs are
91  // expected to be from the same table, though this is moot in this
92  // function as position argumnts are not used here
93  CHECK_EQ(arg_lvs.size(), size_t(2));
94 
95  auto& builder = cgen_state->ir_builder_;
96 
97  llvm::Value* is_null{nullptr};
98  auto x_operand = getOperand(0);
99  const auto& x_ti = x_operand->get_type_info();
100  if (!x_ti.get_notnull()) {
101  CHECK(x_ti.is_integer() || x_ti.is_fp());
102  // TODO: centralize nullcheck logic for all sqltypes
103  is_null = x_ti.is_integer()
104  ? builder.CreateICmp(llvm::CmpInst::ICMP_EQ,
105  arg_lvs.front(),
106  cgen_state->llInt(inline_int_null_val(x_ti)))
107  : builder.CreateFCmp(llvm::FCmpInst::FCMP_OEQ,
108  arg_lvs.front(),
109  cgen_state->llFp(inline_fp_null_val(x_ti)));
110  }
111 
112  auto y_operand = getOperand(1);
113  const auto& y_ti = y_operand->get_type_info();
114  if (!y_ti.get_notnull()) {
115  auto y_is_null =
116  y_ti.is_integer()
117  ? builder.CreateICmp(llvm::CmpInst::ICMP_EQ,
118  arg_lvs.front(),
119  cgen_state->llInt(inline_int_null_val(y_ti)))
120  : builder.CreateFCmp(llvm::FCmpInst::FCMP_OEQ,
121  arg_lvs.front(),
122  cgen_state->llFp(inline_fp_null_val(y_ti)));
123  if (is_null) {
124  // the point is null if at least one of its coordinate has null value
125  is_null = builder.CreateOr(is_null, y_is_null);
126  } else {
127  is_null = y_is_null;
128  }
129  }
130 
131  if (is_nullable_ && !is_null) {
132  // if the inputs are not null, set the output to be not null
133  // TODO: we should do this in the translator and just confirm it here
134  is_nullable_ = false;
135  }
136 
137  // do the alloca before nullcheck codegen, as either way we will return a point array
138  const auto& geo_ti = operator_->get_type_info();
139  CHECK(geo_ti.get_type() == kPOINT);
140 
141  llvm::ArrayType* arr_type{nullptr};
142  if (geo_ti.get_compression() == kENCODING_GEOINT) {
143  auto elem_ty = llvm::Type::getInt32Ty(cgen_state->context_);
144  arr_type = llvm::ArrayType::get(elem_ty, 2);
145  } else {
146  CHECK(geo_ti.get_compression() == kENCODING_NONE);
147  auto elem_ty = llvm::Type::getDoubleTy(cgen_state->context_);
148  arr_type = llvm::ArrayType::get(elem_ty, 2);
149  }
151  builder.CreateAlloca(arr_type, nullptr, operator_->getName() + "_Local_Storage");
152 
153  return std::make_tuple(arg_lvs, is_null);
154  }
155 
156  std::vector<llvm::Value*> codegen(const std::vector<llvm::Value*>& args,
157  CodeGenerator::NullCheckCodegen* nullcheck_codegen,
158  CgenState* cgen_state,
159  const CompilationOptions& co) final {
160  CHECK_EQ(args.size(), size_t(2));
161 
162  const auto& geo_ti = operator_->get_type_info();
163  CHECK(geo_ti.get_type() == kPOINT);
164 
165  auto& builder = cgen_state->ir_builder_;
167 
168  const bool is_compressed = geo_ti.get_compression() == kENCODING_GEOINT;
169 
170  // store x coord
171  auto x_coord_ptr = builder.CreateGEP(
172  pt_local_storage_lv_->getType()->getScalarType()->getPointerElementType(),
174  {cgen_state->llInt(0), cgen_state->llInt(0)},
175  "x_coord_ptr");
176  if (is_compressed) {
177  auto compressed_lv =
178  cgen_state->emitExternalCall("compress_x_coord_geoint",
179  llvm::Type::getInt32Ty(cgen_state->context_),
180  {args.front()});
181  builder.CreateStore(compressed_lv, x_coord_ptr);
182  } else {
183  builder.CreateStore(args.front(), x_coord_ptr);
184  }
185 
186  // store y coord
187  auto y_coord_ptr = builder.CreateGEP(
188  pt_local_storage_lv_->getType()->getScalarType()->getPointerElementType(),
190  {cgen_state->llInt(0), cgen_state->llInt(1)},
191  "y_coord_ptr");
192  if (is_compressed) {
193  auto compressed_lv =
194  cgen_state->emitExternalCall("compress_y_coord_geoint",
195  llvm::Type::getInt32Ty(cgen_state->context_),
196  {args.back()});
197  builder.CreateStore(compressed_lv, y_coord_ptr);
198  } else {
199  builder.CreateStore(args.back(), y_coord_ptr);
200  }
201 
202  llvm::Value* ret = pt_local_storage_lv_;
203  if (is_nullable_) {
204  CHECK(nullcheck_codegen);
205  ret = nullcheck_codegen->finalize(ret, ret);
206  }
207  return {builder.CreateBitCast(ret,
208  geo_ti.get_compression() == kENCODING_GEOINT
209  ? llvm::Type::getInt32PtrTy(cgen_state->context_)
210  : llvm::Type::getDoublePtrTy(cgen_state->context_)),
211  cgen_state->llInt(static_cast<int32_t>(
212  geo_ti.get_compression() == kENCODING_GEOINT ? 8 : 16))};
213  }
214 
215  private:
216  llvm::AllocaInst* pt_local_storage_lv_{nullptr};
217 };
218 
219 } // namespace spatial_type
std::vector< llvm::Value * > codegen(const std::vector< llvm::Value * > &args, CodeGenerator::NullCheckCodegen *nullcheck_codegen, CgenState *cgen_state, const CompilationOptions &co) final
#define CHECK_EQ(x, y)
Definition: Logger.h:301
SQLTypeInfo getNullType() const final
double inline_fp_null_val(const SQL_TYPE_INFO &ti)
CONSTEXPR DEVICE bool is_null(const T &value)
const std::string & getName() const
Definition: Analyzer.h:3167
PointConstructor(const Analyzer::GeoOperator *geo_operator)
auto isNullable() const
Definition: Codegen.h:32
const SQLTypeInfo & get_type_info() const
Definition: Analyzer.h:79
std::tuple< std::vector< llvm::Value * >, llvm::Value * > codegenLoads(const std::vector< llvm::Value * > &arg_lvs, const std::vector< llvm::Value * > &pos_lvs, CgenState *cgen_state) final
llvm::AllocaInst * pt_local_storage_lv_
const Analyzer::GeoOperator * operator_
Definition: Codegen.h:67
size_t size() const
Definition: Analyzer.cpp:4182
#define NULL_ARRAY_DOUBLE
#define CHECK(condition)
Definition: Logger.h:291
virtual const Analyzer::Expr * getOperand(const size_t index)
Definition: Codegen.cpp:64
int64_t inline_int_null_val(const SQL_TYPE_INFO &ti)
std::string getName() const
Definition: Codegen.h:36
std::unique_ptr< CodeGenerator::NullCheckCodegen > getNullCheckCodegen(llvm::Value *null_lv, CgenState *cgen_state, Executor *executor) final
Definition: sqltypes.h:72