OmniSciDB  a5dc49c757
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
spatial_type::Transform Class Reference

#include <Transform.h>

+ Inheritance diagram for spatial_type::Transform:
+ Collaboration diagram for spatial_type::Transform:

Public Member Functions

 Transform (const Analyzer::GeoOperator *geo_operator)
 
size_t size () const override
 
SQLTypeInfo getNullType () const override
 
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
 
std::vector< llvm::Value * > codegen (const std::vector< llvm::Value * > &args, CodeGenerator::NullCheckCodegen *nullcheck_codegen, CgenState *cgen_state, const CompilationOptions &co) override
 
- Public Member Functions inherited from spatial_type::Codegen
 Codegen (const Analyzer::GeoOperator *geo_operator)
 
auto isNullable () const
 
auto getTypeInfo () const
 
std::string getName () const
 
virtual std::unique_ptr
< CodeGenerator::NullCheckCodegen
getNullCheckCodegen (llvm::Value *null_lv, CgenState *cgen_state, Executor *executor)
 
virtual const Analyzer::ExprgetOperand (const size_t index)
 
virtual ~Codegen ()
 

Static Public Member Functions

static bool isUtm (unsigned const srid)
 
- Static Public Member Functions inherited from spatial_type::Codegen
static std::unique_ptr< Codegeninit (const Analyzer::GeoOperator *geo_operator)
 
static char const * pointIsNullFunctionName (SQLTypeInfo const &)
 

Private Attributes

const
Analyzer::GeoTransformOperator
transform_operator_
 
bool can_transform_in_place_ {false}
 

Additional Inherited Members

- Protected Attributes inherited from spatial_type::Codegen
const Analyzer::GeoOperatoroperator_
 
bool is_nullable_ {true}
 

Detailed Description

Definition at line 22 of file Transform.h.

Constructor & Destructor Documentation

spatial_type::Transform::Transform ( const Analyzer::GeoOperator geo_operator)
inline

Definition at line 24 of file Transform.h.

References CHECK, CHECK_EQ, Analyzer::Expr::get_type_info(), spatial_type::Codegen::is_nullable_, spatial_type::Codegen::operator_, Analyzer::GeoOperator::size(), and transform_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  }
#define CHECK_EQ(x, y)
Definition: Logger.h:301
const SQLTypeInfo & get_type_info() const
Definition: Analyzer.h:79
const Analyzer::GeoOperator * operator_
Definition: Codegen.h:69
size_t size() const
Definition: Analyzer.cpp:4211
const Analyzer::GeoTransformOperator * transform_operator_
Definition: Transform.h:277
#define CHECK(condition)
Definition: Logger.h:291
Codegen(const Analyzer::GeoOperator *geo_operator)
Definition: Codegen.h:28

+ Here is the call graph for this function:

Member Function Documentation

std::vector<llvm::Value*> spatial_type::Transform::codegen ( const std::vector< llvm::Value * > &  args,
CodeGenerator::NullCheckCodegen nullcheck_codegen,
CgenState cgen_state,
const CompilationOptions co 
)
inlineoverridevirtual

Implements spatial_type::Codegen.

Definition at line 79 of file Transform.h.

References can_transform_in_place_, CHECK, CHECK_EQ, CgenState::context_, CompilationOptions::device_type, CgenState::emitCall(), CgenState::emitExternalCall(), CodeGenerator::NullCheckCodegen::finalize(), Analyzer::Expr::get_type_info(), Analyzer::GeoTransformOperator::getInputSRID(), spatial_type::Codegen::getName(), spatial_type::Codegen::getOperand(), Analyzer::GeoTransformOperator::getOutputSRID(), GPU, CgenState::gpuFunctionsToReplace(), CgenState::ir_builder_, spatial_type::Codegen::is_nullable_, isUtm(), kENCODING_GEOINT, CgenState::llInt(), CgenState::maybeCloneFunctionRecursive(), CgenState::module_, CgenState::replaceFunctionForGpu(), to_string(), transform_operator_, and verify_function_ir().

82  {
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  }
#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
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
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
static bool isUtm(unsigned const srid)
Definition: Transform.h:42
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

+ Here is the call graph for this function:

std::tuple<std::vector<llvm::Value*>, llvm::Value*> spatial_type::Transform::codegenLoads ( const std::vector< llvm::Value * > &  arg_lvs,
const std::vector< llvm::Value * > &  pos_lvs,
CgenState cgen_state 
)
inlineoverridevirtual

Implements spatial_type::Codegen.

Definition at line 46 of file Transform.h.

References can_transform_in_place_, CHECK, CHECK_EQ, CodeGenerator::codegenGeoArrayLoadAndNullcheck(), CgenState::emitCall(), spatial_type::Codegen::getOperand(), is_null(), spatial_type::Codegen::is_nullable_, kPOINT, spatial_type::Codegen::pointIsNullFunctionName(), and size().

49  {
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  }
#define CHECK_EQ(x, y)
Definition: Logger.h:301
static char const * pointIsNullFunctionName(SQLTypeInfo const &)
Definition: Codegen.cpp:69
CONSTEXPR DEVICE bool is_null(const T &value)
llvm::Value * emitCall(const std::string &fname, const std::vector< llvm::Value * > &args)
Definition: CgenState.cpp:217
size_t size() const override
Definition: Transform.h:38
static ArrayLoadCodegen codegenGeoArrayLoadAndNullcheck(llvm::Value *byte_stream, llvm::Value *pos, const SQLTypeInfo &ti, CgenState *cgen_state)
Definition: GeoIR.cpp:25
#define CHECK(condition)
Definition: Logger.h:291
virtual const Analyzer::Expr * getOperand(const size_t index)
Definition: Codegen.cpp:64

+ Here is the call graph for this function:

SQLTypeInfo spatial_type::Transform::getNullType ( ) const
inlineoverridevirtual

Implements spatial_type::Codegen.

Definition at line 40 of file Transform.h.

References kBOOLEAN.

40 { return SQLTypeInfo(kBOOLEAN); }
static bool spatial_type::Transform::isUtm ( unsigned const  srid)
inlinestatic

Definition at line 42 of file Transform.h.

Referenced by codegen(), and RelAlgTranslator::translateGeoFunctionArg().

42  {
43  return (32601 <= srid && srid <= 32660) || (32701 <= srid && srid <= 32760);
44  }

+ Here is the caller graph for this function:

size_t spatial_type::Transform::size ( ) const
inlineoverridevirtual

Implements spatial_type::Codegen.

Definition at line 38 of file Transform.h.

Referenced by codegenLoads().

38 { return 1; }

+ Here is the caller graph for this function:

Member Data Documentation

bool spatial_type::Transform::can_transform_in_place_ {false}
private

Definition at line 278 of file Transform.h.

Referenced by codegen(), and codegenLoads().

const Analyzer::GeoTransformOperator* spatial_type::Transform::transform_operator_
private

Definition at line 277 of file Transform.h.

Referenced by codegen(), and Transform().


The documentation for this class was generated from the following file: