OmniSciDB  a5dc49c757
 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 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 #pragma once
17 
19 namespace spatial_type {
20 
21 // ST_Transform
22 class Transform : public Codegen {
23  public:
24  Transform(const Analyzer::GeoOperator* geo_operator)
25  : Codegen(geo_operator)
27  dynamic_cast<const Analyzer::GeoTransformOperator*>(geo_operator)) {
28  CHECK_EQ(operator_->size(), size_t(1)); // geo input expr
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  size_t size() const override { return 1; }
39 
40  SQLTypeInfo getNullType() const override { return SQLTypeInfo(kBOOLEAN); }
41 
42  inline static bool isUtm(unsigned const srid) {
43  return (32601 <= srid && srid <= 32660) || (32701 <= srid && srid <= 32760);
44  }
45 
46  std::tuple<std::vector<llvm::Value*>, llvm::Value*> codegenLoads(
47  const std::vector<llvm::Value*>& arg_lvs,
48  const std::vector<llvm::Value*>& pos_lvs,
49  CgenState* cgen_state) override {
50  CHECK_EQ(pos_lvs.size(), size());
51  const auto geo_operand = getOperand(0);
52  const auto& operand_ti = geo_operand->get_type_info();
53  CHECK(operand_ti.is_geometry() && operand_ti.get_type() == kPOINT);
54 
55  if (dynamic_cast<const Analyzer::ColumnVar*>(geo_operand)) {
56  CHECK_EQ(arg_lvs.size(), size_t(1)); // col_byte_stream
58  arg_lvs.front(), pos_lvs.front(), operand_ti, cgen_state);
59  return std::make_tuple(std::vector<llvm::Value*>{arr_load_lvs.buffer},
60  arr_load_lvs.is_null);
61  } else if (dynamic_cast<const Analyzer::GeoConstant*>(geo_operand)) {
62  CHECK_EQ(arg_lvs.size(), size_t(2)); // ptr, size
63 
64  // nulls not supported, and likely compressed, so require a new buffer for the
65  // transformation
67  return std::make_tuple(std::vector<llvm::Value*>{arg_lvs.front()}, nullptr);
68  } else {
69  CHECK(arg_lvs.size() == size_t(1) ||
70  arg_lvs.size() == size_t(2)); // ptr or ptr, size
71  // coming from a temporary, can modify the memory pointer directly
73  char const* const fname = pointIsNullFunctionName(operand_ti);
74  llvm::Value* const is_null = cgen_state->emitCall(fname, {arg_lvs.front()});
75  return std::make_tuple(std::vector<llvm::Value*>{arg_lvs.front()}, is_null);
76  }
77  }
78 
79  std::vector<llvm::Value*> codegen(const std::vector<llvm::Value*>& args,
80  CodeGenerator::NullCheckCodegen* nullcheck_codegen,
81  CgenState* cgen_state,
82  const CompilationOptions& co) override {
83  CHECK_EQ(args.size(), size_t(1));
84 
85  const auto geo_operand = getOperand(0);
86  const auto& operand_ti = geo_operand->get_type_info();
87  auto& builder = cgen_state->ir_builder_;
88 
89  llvm::Value* arr_buff_ptr = args.front();
90  if (operand_ti.get_compression() == kENCODING_GEOINT) {
91  // decompress
92  auto new_arr_ptr =
93  builder.CreateAlloca(llvm::Type::getDoubleTy(cgen_state->context_),
94  cgen_state->llInt(int32_t(2)),
95  getName() + "_Array");
96  auto compressed_arr_ptr = builder.CreateBitCast(
97  arr_buff_ptr, llvm::Type::getInt32PtrTy(cgen_state->context_));
98  // x coord
99  auto* gep = builder.CreateGEP(
100  compressed_arr_ptr->getType()->getScalarType()->getPointerElementType(),
101  compressed_arr_ptr,
102  cgen_state->llInt(0));
103  auto x_coord_lv = cgen_state->emitExternalCall(
104  "decompress_x_coord_geoint",
105  llvm::Type::getDoubleTy(cgen_state->context_),
106  {builder.CreateLoad(
107  gep->getType()->getPointerElementType(), gep, "compressed_x_coord")});
108  builder.CreateStore(
109  x_coord_lv,
110  builder.CreateGEP(
111  new_arr_ptr->getType()->getScalarType()->getPointerElementType(),
112  new_arr_ptr,
113  cgen_state->llInt(0)));
114  gep = builder.CreateGEP(
115  compressed_arr_ptr->getType()->getScalarType()->getPointerElementType(),
116  compressed_arr_ptr,
117  cgen_state->llInt(1));
118  auto y_coord_lv = cgen_state->emitExternalCall(
119  "decompress_y_coord_geoint",
120  llvm::Type::getDoubleTy(cgen_state->context_),
121  {builder.CreateLoad(
122  gep->getType()->getPointerElementType(), gep, "compressed_y_coord")});
123  builder.CreateStore(
124  y_coord_lv,
125  builder.CreateGEP(
126  new_arr_ptr->getType()->getScalarType()->getPointerElementType(),
127  new_arr_ptr,
128  cgen_state->llInt(1)));
129  arr_buff_ptr = new_arr_ptr;
130  } else if (!can_transform_in_place_) {
131  auto new_arr_ptr =
132  builder.CreateAlloca(llvm::Type::getDoubleTy(cgen_state->context_),
133  cgen_state->llInt(int32_t(2)),
134  getName() + "_Array");
135  const auto arr_buff_ptr_cast = builder.CreateBitCast(
136  arr_buff_ptr, llvm::Type::getDoublePtrTy(cgen_state->context_));
137 
138  auto* gep = builder.CreateGEP(
139  arr_buff_ptr_cast->getType()->getScalarType()->getPointerElementType(),
140  arr_buff_ptr_cast,
141  cgen_state->llInt(0));
142  builder.CreateStore(
143  builder.CreateLoad(gep->getType()->getPointerElementType(), gep),
144  builder.CreateGEP(
145  new_arr_ptr->getType()->getScalarType()->getPointerElementType(),
146  new_arr_ptr,
147  cgen_state->llInt(0)));
148  gep = builder.CreateGEP(
149  arr_buff_ptr_cast->getType()->getScalarType()->getPointerElementType(),
150  arr_buff_ptr_cast,
151  cgen_state->llInt(1));
152  builder.CreateStore(
153  builder.CreateLoad(gep->getType()->getPointerElementType(), gep),
154  builder.CreateGEP(
155  new_arr_ptr->getType()->getScalarType()->getPointerElementType(),
156  new_arr_ptr,
157  cgen_state->llInt(1)));
158  arr_buff_ptr = new_arr_ptr;
159  }
160  CHECK(arr_buff_ptr->getType() == llvm::Type::getDoublePtrTy(cgen_state->context_));
161 
162  auto const srid_in = static_cast<unsigned>(transform_operator_->getInputSRID());
163  auto const srid_out = static_cast<unsigned>(transform_operator_->getOutputSRID());
164  if (srid_in == srid_out) {
165  // noop
166  return {args.front()};
167  }
168 
169  // transform in place
170  std::string transform_function_prefix{""};
171  std::vector<llvm::Value*> transform_args;
172 
173  if (srid_out == 900913) {
174  if (srid_in == 4326) {
175  transform_function_prefix = "transform_4326_900913_";
176  } else if (isUtm(srid_in)) {
177  transform_function_prefix = "transform_utm_900913_";
178  transform_args.push_back(cgen_state->llInt(srid_in));
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  } else if (srid_out == 4326) {
184  if (srid_in == 900913) {
185  transform_function_prefix = "transform_900913_4326_";
186  } else if (isUtm(srid_in)) {
187  transform_function_prefix = "transform_utm_4326_";
188  transform_args.push_back(cgen_state->llInt(srid_in));
189  } else {
190  throw std::runtime_error("Unsupported input SRID " + std::to_string(srid_in) +
191  " for output SRID " + std::to_string(srid_out));
192  }
193  } else if (isUtm(srid_out)) {
194  if (srid_in == 4326) {
195  transform_function_prefix = "transform_4326_utm_";
196  } else if (srid_in == 900913) {
197  transform_function_prefix = "transform_900913_utm_";
198  } else {
199  throw std::runtime_error("Unsupported input SRID " + std::to_string(srid_in) +
200  " for output SRID " + std::to_string(srid_out));
201  }
202  transform_args.push_back(cgen_state->llInt(srid_out));
203  } else {
204  throw std::runtime_error("Unsupported output SRID for ST_Transform: " +
205  std::to_string(srid_out));
206  }
207  CHECK(!transform_function_prefix.empty());
208 
209  auto x_coord_ptr_lv = builder.CreateGEP(
210  arr_buff_ptr->getType()->getScalarType()->getPointerElementType(),
211  arr_buff_ptr,
212  cgen_state->llInt(0),
213  "x_coord_ptr");
214  transform_args.push_back(builder.CreateLoad(
215  x_coord_ptr_lv->getType()->getPointerElementType(), x_coord_ptr_lv, "x_coord"));
216  auto y_coord_ptr_lv = builder.CreateGEP(
217  arr_buff_ptr->getType()->getScalarType()->getPointerElementType(),
218  arr_buff_ptr,
219  cgen_state->llInt(1),
220  "y_coord_ptr");
221  transform_args.push_back(builder.CreateLoad(
222  y_coord_ptr_lv->getType()->getPointerElementType(), y_coord_ptr_lv, "y_coord"));
224  auto fn_x = cgen_state->module_->getFunction(transform_function_prefix + 'x');
225  CHECK(fn_x);
226  cgen_state->maybeCloneFunctionRecursive(fn_x);
227  CHECK(!fn_x->isDeclaration());
228 
229  auto gpu_functions_to_replace = cgen_state->gpuFunctionsToReplace(fn_x);
230  for (const auto& fcn_name : gpu_functions_to_replace) {
231  cgen_state->replaceFunctionForGpu(fcn_name, fn_x);
232  }
233  verify_function_ir(fn_x);
234  auto transform_call = builder.CreateCall(fn_x, transform_args);
235  builder.CreateStore(transform_call, x_coord_ptr_lv);
236 
237  auto fn_y = cgen_state->module_->getFunction(transform_function_prefix + 'y');
238  CHECK(fn_y);
239  cgen_state->maybeCloneFunctionRecursive(fn_y);
240  CHECK(!fn_y->isDeclaration());
241 
242  gpu_functions_to_replace = cgen_state->gpuFunctionsToReplace(fn_y);
243  for (const auto& fcn_name : gpu_functions_to_replace) {
244  cgen_state->replaceFunctionForGpu(fcn_name, fn_y);
245  }
246  verify_function_ir(fn_y);
247  transform_call = builder.CreateCall(fn_y, transform_args);
248  builder.CreateStore(transform_call, y_coord_ptr_lv);
249  } else {
250  builder.CreateStore(
251  cgen_state->emitCall(transform_function_prefix + 'x', transform_args),
252  x_coord_ptr_lv);
253  builder.CreateStore(
254  cgen_state->emitCall(transform_function_prefix + 'y', transform_args),
255  y_coord_ptr_lv);
256  }
257  auto ret = arr_buff_ptr;
258  const auto& geo_ti = transform_operator_->get_type_info();
259 
260  if (is_nullable_) {
261  CHECK(nullcheck_codegen);
262  ret = nullcheck_codegen->finalize(
263  llvm::ConstantPointerNull::get(
264  geo_ti.get_compression() == kENCODING_GEOINT
265  ? llvm::PointerType::get(llvm::Type::getInt32Ty(cgen_state->context_),
266  0)
267  : llvm::PointerType::get(llvm::Type::getDoubleTy(cgen_state->context_),
268  0)),
269  ret);
270  }
271  return {ret,
272  cgen_state->llInt(static_cast<int32_t>(
273  geo_ti.get_compression() == kENCODING_GEOINT ? 8 : 16))};
274  }
275 
276  private:
279 };
280 
281 } // 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:79
#define CHECK_EQ(x, y)
Definition: Logger.h:301
int32_t getInputSRID() const
Definition: Analyzer.h:3338
void maybeCloneFunctionRecursive(llvm::Function *fn)
Definition: CgenState.cpp:181
llvm::IRBuilder ir_builder_
Definition: CgenState.h:384
static char const * pointIsNullFunctionName(SQLTypeInfo const &)
Definition: Codegen.cpp:69
std::string to_string(char const *&&v)
llvm::Module * module_
Definition: CgenState.h:373
void verify_function_ir(const llvm::Function *func)
llvm::LLVMContext & context_
Definition: CgenState.h:382
CONSTEXPR DEVICE bool is_null(const T &value)
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.cpp:395
void replaceFunctionForGpu(const std::string &fcn_to_replace, llvm::Function *fn)
Definition: CgenState.cpp:329
std::vector< std::string > gpuFunctionsToReplace(llvm::Function *fn)
Definition: CgenState.cpp:306
const SQLTypeInfo & get_type_info() const
Definition: Analyzer.h:79
llvm::Value * emitCall(const std::string &fname, const std::vector< llvm::Value * > &args)
Definition: CgenState.cpp:217
int32_t getOutputSRID() const
Definition: Analyzer.h:3340
ExecutorDeviceType device_type
Transform(const Analyzer::GeoOperator *geo_operator)
Definition: Transform.h:24
size_t size() const override
Definition: Transform.h:38
SQLTypeInfo getNullType() const override
Definition: Transform.h:40
static ArrayLoadCodegen codegenGeoArrayLoadAndNullcheck(llvm::Value *byte_stream, llvm::Value *pos, const SQLTypeInfo &ti, CgenState *cgen_state)
Definition: GeoIR.cpp:25
static bool isUtm(unsigned const srid)
Definition: Transform.h:42
const Analyzer::GeoOperator * operator_
Definition: Codegen.h:69
size_t size() const
Definition: Analyzer.cpp:4211
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:46
llvm::ConstantInt * llInt(const T v) const
Definition: CgenState.h:249
const Analyzer::GeoTransformOperator * transform_operator_
Definition: Transform.h:277
llvm::Value * finalize(llvm::Value *null_lv, llvm::Value *notnull_lv)
Definition: IRCodegen.cpp:1530
#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