OmniSciDB  72c90bc290
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
PointN.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 namespace spatial_type {
22 
23 class PointN : public Codegen {
24  public:
25  PointN(const Analyzer::GeoOperator* geo_operator) : Codegen(geo_operator) {
26  CHECK_EQ(operator_->size(), size_t(2));
27  // set is nullable to true, because an index outside of the linestring will return
28  // null
29  // note we could probably just set this based on the operator type, as the operator
30  // type needs to match
31  this->is_nullable_ = true;
32  }
33 
34  std::unique_ptr<CodeGenerator::NullCheckCodegen> getNullCheckCodegen(
35  llvm::Value* null_lv,
36  CgenState* cgen_state,
37  Executor* executor) final {
38  if (isNullable()) {
39  CHECK(null_lv);
40  return std::make_unique<CodeGenerator::NullCheckCodegen>(
41  cgen_state, executor, null_lv, getNullType(), getName() + "_nullcheck");
42  } else {
43  return nullptr;
44  }
45  }
46 
47  size_t size() const final { return 2; }
48 
49  SQLTypeInfo getNullType() const final {
50  // nullability is the expression `linestring is null OR size within bounds`
51  return SQLTypeInfo(kBOOLEAN);
52  }
53 
54  // returns arguments lvs and null lv
55  std::tuple<std::vector<llvm::Value*>, llvm::Value*> codegenLoads(
56  const std::vector<llvm::Value*>& arg_lvs,
57  const std::vector<llvm::Value*>& pos_lvs,
58  CgenState* cgen_state) final {
59  CHECK_EQ(pos_lvs.size(), size());
60  CHECK_EQ(pos_lvs.front(), pos_lvs.back());
61  auto operand = getOperand(0);
62  CHECK(operand);
63  const auto& geo_ti = operand->get_type_info();
64  CHECK(geo_ti.get_type() == kLINESTRING);
65 
66  auto& builder = cgen_state->ir_builder_;
67 
68  std::vector<llvm::Value*> array_operand_lvs;
69  CHECK(!arg_lvs.empty());
70  auto index_lv = builder.CreateMul(
71  builder.CreateSub(arg_lvs.back(), cgen_state->llInt(static_cast<int32_t>(1))),
72  cgen_state->llInt(static_cast<int32_t>(2)));
73  llvm::Value* is_null_lv{nullptr};
74  if (arg_lvs.size() == 2) {
75  // col byte stream from column on disk
76  array_operand_lvs.push_back(
77  cgen_state->emitExternalCall("array_buff",
78  llvm::Type::getInt8PtrTy(cgen_state->context_),
79  {arg_lvs.front(), pos_lvs.front()}));
80  const bool is_nullable = !geo_ti.get_notnull();
81  std::string size_fn_name = "array_size";
82  if (is_nullable) {
83  size_fn_name += "_nullable";
84  }
85 
86  uint32_t elem_sz = 1; // TINYINT coords array
87  std::vector<llvm::Value*> array_sz_args{
88  arg_lvs.front(), pos_lvs.front(), cgen_state->llInt(log2_bytes(elem_sz))};
89  if (is_nullable) {
90  array_sz_args.push_back(
91  cgen_state->llInt(static_cast<int32_t>(inline_int_null_value<int32_t>())));
92  }
93  array_operand_lvs.push_back(cgen_state->emitExternalCall(
94  size_fn_name, get_int_type(32, cgen_state->context_), array_sz_args));
95 
96  auto geo_size_lv = array_operand_lvs.back();
97  // convert the index to a byte index
98  const auto outside_linestring_bounds_lv = builder.CreateNot(builder.CreateICmp(
99  llvm::ICmpInst::ICMP_SLT,
100  builder.CreateMul(index_lv, cgen_state->llInt(static_cast<int32_t>(8))),
101  geo_size_lv));
102  outside_linestring_bounds_lv->setName("outside_linestring_bounds");
103  const auto input_is_null_lv = builder.CreateICmp(
104  llvm::ICmpInst::ICMP_EQ,
105  geo_size_lv,
106  cgen_state->llInt(static_cast<int32_t>(inline_int_null_value<int32_t>())));
107  input_is_null_lv->setName("input_is_null");
108  is_null_lv = builder.CreateOr(outside_linestring_bounds_lv, input_is_null_lv);
109  } else {
110  CHECK_EQ(arg_lvs.size(), size_t(3)); // ptr, size, index
111  array_operand_lvs.push_back(arg_lvs[0]);
112  array_operand_lvs.push_back(arg_lvs[1]);
113 
114  const auto geo_size_lv = arg_lvs[1];
115  // TODO: bounds indices are 64 bits but should be 32 bits, as array length is
116  // limited to 32 bits
117  is_null_lv = builder.CreateNot(
118  builder.CreateICmp(llvm::ICmpInst::ICMP_SLT, index_lv, geo_size_lv));
119  }
120  array_operand_lvs.push_back(index_lv);
121  return std::make_tuple(array_operand_lvs, is_null_lv);
122  }
123 
124  std::vector<llvm::Value*> codegen(const std::vector<llvm::Value*>& args,
125  CodeGenerator::NullCheckCodegen* nullcheck_codegen,
126  CgenState* cgen_state,
127  const CompilationOptions& co) final {
128  CHECK_EQ(args.size(), size_t(3)); // ptr, size, index
129  const auto& geo_ti = getOperand(0)->get_type_info();
130  CHECK(geo_ti.is_geometry());
131 
132  llvm::Value* array_buff_cast{nullptr};
133 
134  auto& builder = cgen_state->ir_builder_;
135  if (geo_ti.get_compression() == kENCODING_GEOINT) {
136  array_buff_cast = builder.CreateBitCast(
137  args.front(), llvm::Type::getInt32PtrTy(cgen_state->context_));
138  } else {
139  array_buff_cast = builder.CreateBitCast(
140  args.front(), llvm::Type::getDoublePtrTy(cgen_state->context_));
141  }
142 
143  const auto index_lv = args.back();
144  auto array_offset_lv = builder.CreateGEP(
145  array_buff_cast->getType()->getScalarType()->getPointerElementType(),
146  array_buff_cast,
147  index_lv,
148  operator_->getName() + "_Offset");
149  CHECK(nullcheck_codegen);
150  auto ret_lv = nullcheck_codegen->finalize(
151  llvm::ConstantPointerNull::get(
152  geo_ti.get_compression() == kENCODING_GEOINT
153  ? llvm::PointerType::get(llvm::Type::getInt32Ty(cgen_state->context_), 0)
154  : llvm::PointerType::get(llvm::Type::getDoubleTy(cgen_state->context_),
155  0)),
156  array_offset_lv);
157  const auto geo_size_lv = args[1];
158  return {ret_lv, geo_size_lv};
159  }
160 };
161 
162 } // namespace spatial_type
#define CHECK_EQ(x, y)
Definition: Logger.h:301
std::unique_ptr< CodeGenerator::NullCheckCodegen > getNullCheckCodegen(llvm::Value *null_lv, CgenState *cgen_state, Executor *executor) final
Definition: PointN.h:34
llvm::Type * get_int_type(const int width, llvm::LLVMContext &context)
SQLTypeInfo getNullType() const final
Definition: PointN.h:49
std::vector< llvm::Value * > codegen(const std::vector< llvm::Value * > &args, CodeGenerator::NullCheckCodegen *nullcheck_codegen, CgenState *cgen_state, const CompilationOptions &co) final
Definition: PointN.h:124
auto isNullable() const
Definition: Codegen.h:32
const Analyzer::GeoOperator * operator_
Definition: Codegen.h:67
size_t size() const
Definition: Analyzer.cpp:4182
size_t size() const final
Definition: PointN.h:47
#define CHECK(condition)
Definition: Logger.h:291
virtual const Analyzer::Expr * getOperand(const size_t index)
Definition: Codegen.cpp:64
std::string getName() const
Definition: Codegen.h:36
uint32_t log2_bytes(const uint32_t bytes)
Definition: Execute.h:198
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
Definition: PointN.h:55
PointN(const Analyzer::GeoOperator *geo_operator)
Definition: PointN.h:25