OmniSciDB  d2f719934e
 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, const Catalog_Namespace::Catalog *catalog)
 
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, const Catalog_Namespace::Catalog *catalog)
 
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, const Catalog_Namespace::Catalog *catalog)
 

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_
 
const Catalog_Namespace::Catalogcat_
 
bool is_nullable_ {true}
 

Detailed Description

Definition at line 23 of file Transform.h.

Constructor & Destructor Documentation

spatial_type::Transform::Transform ( const Analyzer::GeoOperator geo_operator,
const Catalog_Namespace::Catalog catalog 
)
inline

Definition at line 25 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_.

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  }
Codegen(const Analyzer::GeoOperator *geo_operator, const Catalog_Namespace::Catalog *catalog)
Definition: Codegen.h:28
#define CHECK_EQ(x, y)
Definition: Logger.h:219
const SQLTypeInfo & get_type_info() const
Definition: Analyzer.h:77
const Analyzer::GeoOperator * operator_
Definition: Codegen.h:70
size_t size() const
Definition: Analyzer.cpp:3644
const Analyzer::GeoTransformOperator * transform_operator_
Definition: Transform.h:250
#define CHECK(condition)
Definition: Logger.h:211

+ 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 90 of file Transform.h.

References CHECK_EQ, CgenState::context_, CgenState::emitExternalCall(), spatial_type::Codegen::getName(), spatial_type::Codegen::getOperand(), CgenState::ir_builder_, kENCODING_GEOINT, and CgenState::llInt().

93  {
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"));
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  }
#define CHECK_EQ(x, y)
Definition: Logger.h:219
int32_t getInputSRID() const
Definition: Analyzer.h:1965
void maybeCloneFunctionRecursive(llvm::Function *fn)
Definition: CgenState.cpp:144
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
std::string to_string(char const *&&v)
llvm::Module * module_
Definition: CgenState.h:329
void verify_function_ir(const llvm::Function *func)
llvm::LLVMContext & context_
Definition: CgenState.h:338
void replaceFunctionForGpu(const std::string &fcn_to_replace, llvm::Function *fn)
Definition: CgenState.cpp:280
std::vector< std::string > gpuFunctionsToReplace(llvm::Function *fn)
Definition: CgenState.cpp:257
const SQLTypeInfo & get_type_info() const
Definition: Analyzer.h:77
llvm::Value * emitCall(const std::string &fname, const std::vector< llvm::Value * > &args)
Definition: CgenState.cpp:180
int32_t getOutputSRID() const
Definition: Analyzer.h:1967
ExecutorDeviceType device_type
static bool isUtm(unsigned const srid)
Definition: Transform.h:44
llvm::ConstantInt * llInt(const T v) const
Definition: CgenState.h:307
const Analyzer::GeoTransformOperator * transform_operator_
Definition: Transform.h:250
llvm::Value * finalize(llvm::Value *null_lv, llvm::Value *notnull_lv)
Definition: IRCodegen.cpp:1447
#define CHECK(condition)
Definition: Logger.h:211
virtual const Analyzer::Expr * getOperand(const size_t index)
Definition: Codegen.cpp:63
std::string getName() const
Definition: Codegen.h:39

+ 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 48 of file Transform.h.

References can_transform_in_place_, CHECK, CHECK_EQ, CodeGenerator::codegenGeoArrayLoadAndNullcheck(), CgenState::context_, spatial_type::Codegen::getOperand(), CgenState::ir_builder_, is_null(), spatial_type::Codegen::is_nullable_, kENCODING_GEOINT, kPOINT, size(), and UNREACHABLE.

51  {
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  }
#define CHECK_EQ(x, y)
Definition: Logger.h:219
llvm::IRBuilder ir_builder_
Definition: CgenState.h:340
#define UNREACHABLE()
Definition: Logger.h:255
llvm::LLVMContext & context_
Definition: CgenState.h:338
CONSTEXPR DEVICE bool is_null(const T &value)
size_t size() 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:23
#define CHECK(condition)
Definition: Logger.h:211
virtual const Analyzer::Expr * getOperand(const size_t index)
Definition: Codegen.cpp:63

+ Here is the call graph for this function:

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

Implements spatial_type::Codegen.

Definition at line 42 of file Transform.h.

References kBOOLEAN.

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

Definition at line 44 of file Transform.h.

Referenced by RelAlgTranslator::translateGeoFunctionArg().

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

+ Here is the caller graph for this function:

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

Implements spatial_type::Codegen.

Definition at line 40 of file Transform.h.

Referenced by codegenLoads().

40 { 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 251 of file Transform.h.

Referenced by codegenLoads().

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

Definition at line 250 of file Transform.h.

Referenced by Transform().


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