OmniSciDB  ca0c39ec8f
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Centroid.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 Centroid : public Codegen {
24  public:
25  Centroid(const Analyzer::GeoOperator* geo_operator,
26  const Catalog_Namespace::Catalog* catalog)
27  : Codegen(geo_operator, catalog) {
28  CHECK_EQ(operator_->size(), size_t(1));
29  const auto& ti = operator_->get_type_info();
30  is_nullable_ = !ti.get_notnull();
31  }
32 
33  size_t size() const final { return 1; }
34 
35  SQLTypeInfo getNullType() const final { return SQLTypeInfo(kINT); }
36 
37  std::tuple<std::vector<llvm::Value*>, llvm::Value*> codegenLoads(
38  const std::vector<llvm::Value*>& arg_lvs,
39  const std::vector<llvm::Value*>& pos_lvs,
40  CgenState* cgen_state) final {
41  CHECK_EQ(pos_lvs.size(), size());
42  const auto operand = getOperand(0);
43  CHECK(operand);
44  const auto& operand_ti = operand->get_type_info();
45 
46  std::string size_fn_name = "array_size";
47  if (is_nullable_) {
48  size_fn_name += "_nullable";
49  }
50 
51  auto& builder = cgen_state->ir_builder_;
52 
53  std::vector<llvm::Value*> operand_lvs;
54  // iterate over column inputs
55  if (dynamic_cast<const Analyzer::ColumnVar*>(operand)) {
56  for (size_t i = 0; i < arg_lvs.size(); i++) {
57  auto lv = arg_lvs[i];
58  auto array_buff_lv =
59  cgen_state->emitExternalCall("array_buff",
60  llvm::Type::getInt8PtrTy(cgen_state->context_),
61  {lv, pos_lvs.front()});
62  auto const is_coords = (i == 0);
63  if (!is_coords) {
64  array_buff_lv = builder.CreateBitCast(
65  array_buff_lv, llvm::Type::getInt32PtrTy(cgen_state->context_));
66  }
67  operand_lvs.push_back(array_buff_lv);
68  const auto ptr_type = llvm::dyn_cast_or_null<llvm::PointerType>(lv->getType());
69  CHECK(ptr_type);
70  const auto elem_type = ptr_type->getPointerElementType();
71  CHECK(elem_type);
72  auto const shift = log2_bytes(is_coords ? 1 : 4);
73  std::vector<llvm::Value*> array_sz_args{
74  lv, pos_lvs.front(), cgen_state->llInt(shift)};
75  if (is_nullable_) { // TODO: should we do this for all arguments, or just points?
76  array_sz_args.push_back(
77  cgen_state->llInt(static_cast<int32_t>(inline_int_null_value<int32_t>())));
78  }
79  operand_lvs.push_back(cgen_state->emitExternalCall(
80  size_fn_name, get_int_type(32, cgen_state->context_), array_sz_args));
81  }
82  } else {
83  for (size_t i = 0; i < arg_lvs.size(); i++) {
84  auto arg_lv = arg_lvs[i];
85  if (i > 0 && arg_lv->getType()->isPointerTy()) {
86  arg_lv = builder.CreateBitCast(arg_lv,
87  llvm::Type::getInt32PtrTy(cgen_state->context_));
88  }
89  operand_lvs.push_back(arg_lv);
90  }
91  }
92  CHECK_EQ(operand_lvs.size(),
93  size_t(2 * operand_ti.get_physical_coord_cols())); // array ptr and size
94 
95  // note that this block is the only one that differs from Area/Perimeter
96  // use the points array size argument for nullability
97  llvm::Value* null_check_operand_lv{nullptr};
98  if (is_nullable_) {
99  null_check_operand_lv = operand_lvs[1];
100  if (null_check_operand_lv->getType() !=
101  llvm::Type::getInt32Ty(cgen_state->context_)) {
102  CHECK(null_check_operand_lv->getType() ==
103  llvm::Type::getInt64Ty(cgen_state->context_));
104  // Geos functions come out 64-bit, cast down to 32 for now
105 
106  null_check_operand_lv = builder.CreateTrunc(
107  null_check_operand_lv, llvm::Type::getInt32Ty(cgen_state->context_));
108  }
109  }
110 
111  return std::make_tuple(operand_lvs, null_check_operand_lv);
112  }
113 
114  std::vector<llvm::Value*> codegen(const std::vector<llvm::Value*>& args,
115  CodeGenerator::NullCheckCodegen* nullcheck_codegen,
116  CgenState* cgen_state,
117  const CompilationOptions& co) final {
118  std::string func_name = "ST_Centroid";
119  const auto& ret_ti = operator_->get_type_info();
120  CHECK(ret_ti.is_geometry() && ret_ti.get_type() == kPOINT);
121  const auto& operand_ti = getOperand(0)->get_type_info();
122 
123  auto& builder = cgen_state->ir_builder_;
124 
125  // Allocate local storage for centroid point
126  auto elem_ty = llvm::Type::getDoubleTy(cgen_state->context_);
127  llvm::ArrayType* arr_type = llvm::ArrayType::get(elem_ty, 2);
128  auto pt_local_storage_lv =
129  builder.CreateAlloca(arr_type, nullptr, func_name + "_Local_Storage");
130 
131  llvm::Value* pt_compressed_local_storage_lv{NULL};
132  // Allocate local storage for compressed centroid point
133  if (ret_ti.get_compression() == kENCODING_GEOINT) {
134  auto elem_ty = llvm::Type::getInt32Ty(cgen_state->context_);
135  llvm::ArrayType* arr_type = llvm::ArrayType::get(elem_ty, 2);
136  pt_compressed_local_storage_lv = builder.CreateAlloca(
137  arr_type, nullptr, func_name + "_Compressed_Local_Storage");
138  }
139 
140  func_name += spatial_type::suffix(operand_ti.get_type());
141 
142  auto operand_lvs = args;
143 
144  // push back ic, isr, osr for now
145  operand_lvs.push_back(
146  cgen_state->llInt(Geospatial::get_compression_scheme(operand_ti))); // ic
147  operand_lvs.push_back(cgen_state->llInt(operand_ti.get_input_srid())); // in srid
148  auto output_srid = operand_ti.get_output_srid();
149  if (const auto srid_override = operator_->getOutputSridOverride()) {
150  output_srid = *srid_override;
151  }
152  operand_lvs.push_back(cgen_state->llInt(output_srid)); // out srid
153 
154  auto idx_lv = cgen_state->llInt(0);
155  auto pt_local_storage_gep = llvm::GetElementPtrInst::CreateInBounds(
156  pt_local_storage_lv->getType()->getScalarType()->getPointerElementType(),
157  pt_local_storage_lv,
158  {idx_lv, idx_lv},
159  "",
160  builder.GetInsertBlock());
161  // Pass local storage to centroid function
162  operand_lvs.push_back(pt_local_storage_gep);
163  CHECK(ret_ti.get_type() == kPOINT);
164  cgen_state->emitExternalCall(
165  func_name, llvm::Type::getVoidTy(cgen_state->context_), operand_lvs);
166 
167  llvm::Value* ret_coords = pt_local_storage_lv;
168  if (ret_ti.get_compression() == kENCODING_GEOINT) {
169  // Compress centroid point if requested
170  // Take values out of local storage, compress, store in compressed local storage
171 
172  auto x_ptr = builder.CreateGEP(
173  pt_local_storage_lv->getType()->getScalarType()->getPointerElementType(),
174  pt_local_storage_lv,
175  {cgen_state->llInt(0), cgen_state->llInt(0)},
176  "x_ptr");
177  auto x_lv = builder.CreateLoad(x_ptr->getType()->getPointerElementType(), x_ptr);
178  auto compressed_x_lv =
179  cgen_state->emitExternalCall("compress_x_coord_geoint",
180  llvm::Type::getInt32Ty(cgen_state->context_),
181  {x_lv});
182  auto compressed_x_ptr =
183  builder.CreateGEP(pt_compressed_local_storage_lv->getType()
184  ->getScalarType()
185  ->getPointerElementType(),
186  pt_compressed_local_storage_lv,
187  {cgen_state->llInt(0), cgen_state->llInt(0)},
188  "compressed_x_ptr");
189  builder.CreateStore(compressed_x_lv, compressed_x_ptr);
190 
191  auto y_ptr = builder.CreateGEP(
192  pt_local_storage_lv->getType()->getScalarType()->getPointerElementType(),
193  pt_local_storage_lv,
194  {cgen_state->llInt(0), cgen_state->llInt(1)},
195  "y_ptr");
196  auto y_lv = builder.CreateLoad(y_ptr->getType()->getPointerElementType(), y_ptr);
197  auto compressed_y_lv =
198  cgen_state->emitExternalCall("compress_y_coord_geoint",
199  llvm::Type::getInt32Ty(cgen_state->context_),
200  {y_lv});
201  auto compressed_y_ptr =
202  builder.CreateGEP(pt_compressed_local_storage_lv->getType()
203  ->getScalarType()
204  ->getPointerElementType(),
205  pt_compressed_local_storage_lv,
206  {cgen_state->llInt(0), cgen_state->llInt(1)},
207  "compressed_y_ptr");
208  builder.CreateStore(compressed_y_lv, compressed_y_ptr);
209 
210  ret_coords = pt_compressed_local_storage_lv;
211  } else {
212  CHECK(ret_ti.get_compression() == kENCODING_NONE);
213  }
214 
215  auto ret_ty = ret_ti.get_compression() == kENCODING_GEOINT
216  ? llvm::Type::getInt32PtrTy(cgen_state->context_)
217  : llvm::Type::getDoublePtrTy(cgen_state->context_);
218  ret_coords = builder.CreateBitCast(ret_coords, ret_ty);
219 
220  if (is_nullable_) {
221  CHECK(nullcheck_codegen);
222  ret_coords = nullcheck_codegen->finalize(
223  llvm::ConstantPointerNull::get(
224  ret_ti.get_compression() == kENCODING_GEOINT
225  ? llvm::PointerType::get(llvm::Type::getInt32Ty(cgen_state->context_),
226  0)
227  : llvm::PointerType::get(llvm::Type::getDoubleTy(cgen_state->context_),
228  0)),
229  ret_coords);
230  }
231 
232  return {ret_coords,
233  cgen_state->llInt(ret_ti.get_compression() == kENCODING_GEOINT ? 8 : 16)};
234  }
235 };
236 
237 } // namespace spatial_type
#define CHECK_EQ(x, y)
Definition: Logger.h:230
class for a per-database catalog. also includes metadata for the current database and the current use...
Definition: Catalog.h:132
int32_t get_compression_scheme(const SQLTypeInfo &ti)
Definition: Compression.cpp:23
llvm::Type * get_int_type(const int width, llvm::LLVMContext &context)
std::vector< llvm::Value * > codegen(const std::vector< llvm::Value * > &args, CodeGenerator::NullCheckCodegen *nullcheck_codegen, CgenState *cgen_state, const CompilationOptions &co) final
Definition: Centroid.h:114
std::string suffix(SQLTypes type)
Definition: Codegen.cpp:68
const SQLTypeInfo & get_type_info() const
Definition: Analyzer.h:82
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: Centroid.h:37
const Analyzer::GeoOperator * operator_
Definition: Codegen.h:70
size_t size() const
Definition: Analyzer.cpp:3925
size_t size() const final
Definition: Centroid.h:33
#define CHECK(condition)
Definition: Logger.h:222
Centroid(const Analyzer::GeoOperator *geo_operator, const Catalog_Namespace::Catalog *catalog)
Definition: Centroid.h:25
virtual const Analyzer::Expr * getOperand(const size_t index)
Definition: Codegen.cpp:63
uint32_t log2_bytes(const uint32_t bytes)
Definition: Execute.h:176
Definition: sqltypes.h:60
SQLTypeInfo getNullType() const final
Definition: Centroid.h:35