OmniSciDB  eb3a3d0a03
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Transform.h
Go to the documentation of this file.
1 /*
2  * Copyright 2021 OmniSci, 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 namespace spatial_type {
21 
22 // ST_Transform
23 class Transform : public Codegen {
24  public:
25  Transform(const Analyzer::GeoOperator* geo_operator,
26  const Catalog_Namespace::Catalog* catalog)
27  : Codegen(geo_operator, catalog)
29  dynamic_cast<const Analyzer::GeoTransformOperator*>(geo_operator)) {
30  CHECK_EQ(operator_->size(), size_t(1)); // geo input expr
32  const auto& ti = geo_operator->get_type_info();
33  if (ti.get_notnull()) {
34  is_nullable_ = false;
35  } else {
36  is_nullable_ = true;
37  }
38  }
39 
40  size_t size() const override { return 1; }
41 
42  SQLTypeInfo getNullType() const override { return SQLTypeInfo(kBOOLEAN); }
43 
44  inline static bool isUtm(unsigned const srid) {
45  return (32601 <= srid && srid <= 32660) || (32701 <= srid && srid <= 32760);
46  }
47 
48  std::tuple<std::vector<llvm::Value*>, llvm::Value*> codegenLoads(
49  const std::vector<llvm::Value*>& arg_lvs,
50  const std::vector<llvm::Value*>& pos_lvs,
51  CgenState* cgen_state) override {
52  CHECK_EQ(pos_lvs.size(), size());
53  const auto geo_operand = getOperand(0);
54  const auto& operand_ti = geo_operand->get_type_info();
55  CHECK(operand_ti.is_geometry() && operand_ti.get_type() == kPOINT);
56 
57  if (dynamic_cast<const Analyzer::ColumnVar*>(geo_operand)) {
58  CHECK_EQ(arg_lvs.size(), size_t(1)); // col_byte_stream
60  arg_lvs.front(), pos_lvs.front(), operand_ti, cgen_state);
61  return std::make_tuple(std::vector<llvm::Value*>{arr_load_lvs.buffer},
62  arr_load_lvs.is_null);
63  } else if (dynamic_cast<const Analyzer::GeoConstant*>(geo_operand)) {
64  CHECK_EQ(arg_lvs.size(), size_t(2)); // ptr, size
65 
66  // nulls not supported, and likely compressed, so require a new buffer for the
67  // transformation
69  return std::make_tuple(std::vector<llvm::Value*>{arg_lvs.front()}, nullptr);
70  } else {
71  CHECK(arg_lvs.size() == size_t(1) ||
72  arg_lvs.size() == size_t(2)); // ptr or ptr, size
73  // coming from a temporary, can modify the memory pointer directly
75  auto& builder = cgen_state->ir_builder_;
76 
77  const auto is_null = builder.CreateICmp(
78  llvm::CmpInst::ICMP_EQ,
79  arg_lvs.front(),
80  llvm::ConstantPointerNull::get( // TODO: check ptr address space
81  operand_ti.get_compression() == kENCODING_GEOINT
82  ? llvm::Type::getInt32PtrTy(cgen_state->context_)
83  : llvm::Type::getDoublePtrTy(cgen_state->context_)));
84  return std::make_tuple(std::vector<llvm::Value*>{arg_lvs.front()}, is_null);
85  }
86  UNREACHABLE();
87  return std::make_tuple(std::vector<llvm::Value*>{}, nullptr);
88  }
89 
90  std::vector<llvm::Value*> codegen(const std::vector<llvm::Value*>& args,
91  CodeGenerator::NullCheckCodegen* nullcheck_codegen,
92  CgenState* cgen_state,
93  const CompilationOptions& co) override {
94  CHECK_EQ(args.size(), size_t(1));
95 
96  const auto geo_operand = getOperand(0);
97  const auto& operand_ti = geo_operand->get_type_info();
98  auto& builder = cgen_state->ir_builder_;
99 
100  llvm::Value* arr_buff_ptr = args.front();
101  if (operand_ti.get_compression() == kENCODING_GEOINT) {
102  // decompress
103  auto new_arr_ptr =
104  builder.CreateAlloca(llvm::Type::getDoubleTy(cgen_state->context_),
105  cgen_state->llInt(int32_t(2)),
106  getName() + "_Array");
107  auto compressed_arr_ptr = builder.CreateBitCast(
108  arr_buff_ptr, llvm::Type::getInt32PtrTy(cgen_state->context_));
109  // x coord
110  auto x_coord_lv = cgen_state->emitExternalCall(
111  "decompress_x_coord_geoint",
112  llvm::Type::getDoubleTy(cgen_state->context_),
113  {builder.CreateLoad(builder.CreateGEP(compressed_arr_ptr, cgen_state->llInt(0)),
114  "compressed_x_coord")});
115  builder.CreateStore(x_coord_lv,
116  builder.CreateGEP(new_arr_ptr, cgen_state->llInt(0)));
117  auto y_coord_lv = cgen_state->emitExternalCall(
118  "decompress_y_coord_geoint",
119  llvm::Type::getDoubleTy(cgen_state->context_),
120  {builder.CreateLoad(builder.CreateGEP(compressed_arr_ptr, cgen_state->llInt(1)),
121  "compressed_y_coord")});
122  builder.CreateStore(y_coord_lv,
123  builder.CreateGEP(new_arr_ptr, cgen_state->llInt(1)));
124  arr_buff_ptr = new_arr_ptr;
125  } else if (!can_transform_in_place_) {
126  auto new_arr_ptr =
127  builder.CreateAlloca(llvm::Type::getDoubleTy(cgen_state->context_),
128  cgen_state->llInt(int32_t(2)),
129  getName() + "_Array");
130  const auto arr_buff_ptr_cast = builder.CreateBitCast(
131  arr_buff_ptr, llvm::Type::getDoublePtrTy(cgen_state->context_));
132 
133  builder.CreateStore(
134  builder.CreateLoad(builder.CreateGEP(arr_buff_ptr_cast, cgen_state->llInt(0))),
135  builder.CreateGEP(new_arr_ptr, cgen_state->llInt(0)));
136  builder.CreateStore(
137  builder.CreateLoad(builder.CreateGEP(arr_buff_ptr_cast, cgen_state->llInt(1))),
138  builder.CreateGEP(new_arr_ptr, cgen_state->llInt(1)));
139  arr_buff_ptr = new_arr_ptr;
140  }
141  CHECK(arr_buff_ptr->getType() == llvm::Type::getDoublePtrTy(cgen_state->context_));
142 
143  auto const srid_in = static_cast<unsigned>(transform_operator_->getInputSRID());
144  auto const srid_out = static_cast<unsigned>(transform_operator_->getOutputSRID());
145  if (srid_in == srid_out) {
146  // noop
147  return {args.front()};
148  }
149 
150  // transform in place
151  std::string transform_function_prefix{""};
152  std::vector<llvm::Value*> transform_args;
153 
154  if (srid_out == 900913) {
155  if (srid_in == 4326) {
156  transform_function_prefix = "transform_4326_900913_";
157  } else if (isUtm(srid_in)) {
158  transform_function_prefix = "transform_utm_900913_";
159  transform_args.push_back(cgen_state->llInt(srid_in));
160  } else {
161  throw std::runtime_error("Unsupported input SRID " + std::to_string(srid_in) +
162  " for output SRID " + std::to_string(srid_out));
163  }
164  } else if (srid_out == 4326) {
165  if (srid_in == 900913) {
166  transform_function_prefix = "transform_900913_4326_";
167  } else if (isUtm(srid_in)) {
168  transform_function_prefix = "transform_utm_4326_";
169  transform_args.push_back(cgen_state->llInt(srid_in));
170  } else {
171  throw std::runtime_error("Unsupported input SRID " + std::to_string(srid_in) +
172  " for output SRID " + std::to_string(srid_out));
173  }
174  } else if (isUtm(srid_out)) {
175  if (srid_in == 4326) {
176  transform_function_prefix = "transform_4326_utm_";
177  } else if (srid_in == 900913) {
178  transform_function_prefix = "transform_900913_utm_";
179  } else {
180  throw std::runtime_error("Unsupported input SRID " + std::to_string(srid_in) +
181  " for output SRID " + std::to_string(srid_out));
182  }
183  transform_args.push_back(cgen_state->llInt(srid_out));
184  } else {
185  throw std::runtime_error("Unsupported output SRID for ST_Transform: " +
186  std::to_string(srid_out));
187  }
188  CHECK(!transform_function_prefix.empty());
189 
190  auto x_coord_ptr_lv =
191  builder.CreateGEP(arr_buff_ptr, cgen_state->llInt(0), "x_coord_ptr");
192  transform_args.push_back(builder.CreateLoad(x_coord_ptr_lv, "x_coord"));
193  auto y_coord_ptr_lv =
194  builder.CreateGEP(arr_buff_ptr, cgen_state->llInt(1), "y_coord_ptr");
195  transform_args.push_back(builder.CreateLoad(y_coord_ptr_lv, "y_coord"));
196  if (co.device_type == ExecutorDeviceType::GPU) {
197  auto fn_x = cgen_state->module_->getFunction(transform_function_prefix + 'x');
198  CHECK(fn_x);
199  cgen_state->maybeCloneFunctionRecursive(fn_x);
200  CHECK(!fn_x->isDeclaration());
201 
202  auto gpu_functions_to_replace = cgen_state->gpuFunctionsToReplace(fn_x);
203  for (const auto& fcn_name : gpu_functions_to_replace) {
204  cgen_state->replaceFunctionForGpu(fcn_name, fn_x);
205  }
206  verify_function_ir(fn_x);
207  auto transform_call = builder.CreateCall(fn_x, transform_args);
208  builder.CreateStore(transform_call, x_coord_ptr_lv);
209 
210  auto fn_y = cgen_state->module_->getFunction(transform_function_prefix + 'y');
211  CHECK(fn_y);
212  cgen_state->maybeCloneFunctionRecursive(fn_y);
213  CHECK(!fn_y->isDeclaration());
214 
215  gpu_functions_to_replace = cgen_state->gpuFunctionsToReplace(fn_y);
216  for (const auto& fcn_name : gpu_functions_to_replace) {
217  cgen_state->replaceFunctionForGpu(fcn_name, fn_y);
218  }
219  verify_function_ir(fn_y);
220  transform_call = builder.CreateCall(fn_y, transform_args);
221  builder.CreateStore(transform_call, y_coord_ptr_lv);
222  } else {
223  builder.CreateStore(
224  cgen_state->emitCall(transform_function_prefix + 'x', transform_args),
225  x_coord_ptr_lv);
226  builder.CreateStore(
227  cgen_state->emitCall(transform_function_prefix + 'y', transform_args),
228  y_coord_ptr_lv);
229  }
230  auto ret = arr_buff_ptr;
231  const auto& geo_ti = transform_operator_->get_type_info();
232 
233  if (is_nullable_) {
234  CHECK(nullcheck_codegen);
235  ret = nullcheck_codegen->finalize(
236  llvm::ConstantPointerNull::get(
237  geo_ti.get_compression() == kENCODING_GEOINT
238  ? llvm::PointerType::get(llvm::Type::getInt32Ty(cgen_state->context_),
239  0)
240  : llvm::PointerType::get(llvm::Type::getDoubleTy(cgen_state->context_),
241  0)),
242  ret);
243  }
244  return {ret,
245  cgen_state->llInt(static_cast<int32_t>(
246  geo_ti.get_compression() == kENCODING_GEOINT ? 8 : 16))};
247  }
248 
249  private:
251  bool can_transform_in_place_{false};
252 };
253 
254 } // namespace spatial_type
std::vector< llvm::Value * > codegen(const std::vector< llvm::Value * > &args, CodeGenerator::NullCheckCodegen *nullcheck_codegen, CgenState *cgen_state, const CompilationOptions &co) override
Definition: Transform.h:90
#define CHECK_EQ(x, y)
Definition: Logger.h:217
class for a per-database catalog. also includes metadata for the current database and the current use...
Definition: Catalog.h:102
#define const
llvm::Value * emitExternalCall(const std::string &fname, llvm::Type *ret_type, const std::vector< llvm::Value * > args, const std::vector< llvm::Attribute::AttrKind > &fnattrs={}, const bool has_struct_return=false)
Definition: CgenState.h:228
llvm::IRBuilder ir_builder_
Definition: CgenState.h:340
Transform(const Analyzer::GeoOperator *geo_operator, const Catalog_Namespace::Catalog *catalog)
Definition: Transform.h:25
#define UNREACHABLE()
Definition: Logger.h:253
std::string to_string(char const *&&v)
void verify_function_ir(const llvm::Function *func)
llvm::LLVMContext & context_
Definition: CgenState.h:338
CONSTEXPR DEVICE bool is_null(const T &value)
const SQLTypeInfo & get_type_info() const
Definition: Analyzer.h:78
size_t size() const override
Definition: Transform.h:40
SQLTypeInfo getNullType() const override
Definition: Transform.h:42
static ArrayLoadCodegen codegenGeoArrayLoadAndNullcheck(llvm::Value *byte_stream, llvm::Value *pos, const SQLTypeInfo &ti, CgenState *cgen_state)
Definition: GeoIR.cpp:23
static bool isUtm(unsigned const srid)
Definition: Transform.h:44
const Analyzer::GeoOperator * operator_
Definition: Codegen.h:70
size_t size() const
Definition: Analyzer.cpp:3629
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) override
Definition: Transform.h:48
llvm::ConstantInt * llInt(const T v) const
Definition: CgenState.h:307
const Analyzer::GeoTransformOperator * transform_operator_
Definition: Transform.h:250
#define CHECK(condition)
Definition: Logger.h:209
virtual const Analyzer::Expr * getOperand(const size_t index)
Definition: Codegen.cpp:63
std::string getName() const
Definition: Codegen.h:39