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

#include <Centroid.h>

+ Inheritance diagram for spatial_type::Centroid:
+ Collaboration diagram for spatial_type::Centroid:

Public Member Functions

 Centroid (const Analyzer::GeoOperator *geo_operator, const Catalog_Namespace::Catalog *catalog)
 
size_t size () const final
 
SQLTypeInfo getNullType () const final
 
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
 
std::vector< llvm::Value * > codegen (const std::vector< llvm::Value * > &args, CodeGenerator::NullCheckCodegen *nullcheck_codegen, CgenState *cgen_state, const CompilationOptions &co) final
 
- 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 ()
 

Additional Inherited Members

- Static Public Member Functions inherited from spatial_type::Codegen
static std::unique_ptr< Codegeninit (const Analyzer::GeoOperator *geo_operator, const Catalog_Namespace::Catalog *catalog)
 
- 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 Centroid.h.

Constructor & Destructor Documentation

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

Definition at line 25 of file Centroid.h.

References CHECK_EQ, Analyzer::Expr::get_type_info(), spatial_type::Codegen::is_nullable_, spatial_type::Codegen::operator_, and Analyzer::GeoOperator::size().

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  }
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

+ Here is the call graph for this function:

Member Function Documentation

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

Implements spatial_type::Codegen.

Definition at line 116 of file Centroid.h.

References run_benchmark_import::args, CHECK, Geospatial::get_compression_scheme(), kENCODING_GEOINT, kPOINT, and spatial_type::suffix().

119  {
120  std::string func_name = "ST_Centroid";
121  const auto& ret_ti = operator_->get_type_info();
122  CHECK(ret_ti.is_geometry() && ret_ti.get_type() == kPOINT);
123  const auto& operand_ti = getOperand(0)->get_type_info();
124 
125  auto& builder = cgen_state->ir_builder_;
126 
127  // Allocate local storage for centroid point
128  auto elem_ty = llvm::Type::getDoubleTy(cgen_state->context_);
129  llvm::ArrayType* arr_type = llvm::ArrayType::get(elem_ty, 2);
130  auto pt_local_storage_lv =
131  builder.CreateAlloca(arr_type, nullptr, func_name + "_Local_Storage");
132 
133  llvm::Value* pt_compressed_local_storage_lv{NULL};
134  // Allocate local storage for compressed centroid point
135  if (ret_ti.get_compression() == kENCODING_GEOINT) {
136  auto elem_ty = llvm::Type::getInt32Ty(cgen_state->context_);
137  llvm::ArrayType* arr_type = llvm::ArrayType::get(elem_ty, 2);
138  pt_compressed_local_storage_lv = builder.CreateAlloca(
139  arr_type, nullptr, func_name + "_Compressed_Local_Storage");
140  }
141 
142  func_name += spatial_type::suffix(operand_ti.get_type());
143 
144  auto operand_lvs = args;
145 
146  // push back ic, isr, osr for now
147  operand_lvs.push_back(
148  cgen_state->llInt(Geospatial::get_compression_scheme(operand_ti))); // ic
149  operand_lvs.push_back(cgen_state->llInt(operand_ti.get_input_srid())); // in srid
150  operand_lvs.push_back(cgen_state->llInt(operand_ti.get_output_srid())); // out srid
151 
152  auto idx_lv = cgen_state->llInt(0);
153  auto pt_local_storage_gep = llvm::GetElementPtrInst::CreateInBounds(
154  pt_local_storage_lv, {idx_lv, idx_lv}, "", builder.GetInsertBlock());
155  // Pass local storage to centroid function
156  operand_lvs.push_back(pt_local_storage_gep);
157  CHECK(ret_ti.get_type() == kPOINT);
158  cgen_state->emitExternalCall(
159  func_name, llvm::Type::getVoidTy(cgen_state->context_), operand_lvs);
160 
161  llvm::Value* ret_coords = pt_local_storage_lv;
162  if (ret_ti.get_compression() == kENCODING_GEOINT) {
163  // Compress centroid point if requested
164  // Take values out of local storage, compress, store in compressed local storage
165 
166  auto x_ptr = builder.CreateGEP(
167  pt_local_storage_lv, {cgen_state->llInt(0), cgen_state->llInt(0)}, "x_ptr");
168  auto x_lv = builder.CreateLoad(x_ptr);
169  auto compressed_x_lv =
170  cgen_state->emitExternalCall("compress_x_coord_geoint",
171  llvm::Type::getInt32Ty(cgen_state->context_),
172  {x_lv});
173  auto compressed_x_ptr =
174  builder.CreateGEP(pt_compressed_local_storage_lv,
175  {cgen_state->llInt(0), cgen_state->llInt(0)},
176  "compressed_x_ptr");
177  builder.CreateStore(compressed_x_lv, compressed_x_ptr);
178 
179  auto y_ptr = builder.CreateGEP(
180  pt_local_storage_lv, {cgen_state->llInt(0), cgen_state->llInt(1)}, "y_ptr");
181  auto y_lv = builder.CreateLoad(y_ptr);
182  auto compressed_y_lv =
183  cgen_state->emitExternalCall("compress_y_coord_geoint",
184  llvm::Type::getInt32Ty(cgen_state->context_),
185  {y_lv});
186  auto compressed_y_ptr =
187  builder.CreateGEP(pt_compressed_local_storage_lv,
188  {cgen_state->llInt(0), cgen_state->llInt(1)},
189  "compressed_y_ptr");
190  builder.CreateStore(compressed_y_lv, compressed_y_ptr);
191 
192  ret_coords = pt_compressed_local_storage_lv;
193  } else {
194  CHECK(ret_ti.get_compression() == kENCODING_NONE);
195  }
196 
197  auto ret_ty = ret_ti.get_compression() == kENCODING_GEOINT
198  ? llvm::Type::getInt32PtrTy(cgen_state->context_)
199  : llvm::Type::getDoublePtrTy(cgen_state->context_);
200  ret_coords = builder.CreateBitCast(ret_coords, ret_ty);
201 
202  if (is_nullable_) {
203  CHECK(nullcheck_codegen);
204  ret_coords = nullcheck_codegen->finalize(
205  llvm::ConstantPointerNull::get(
206  ret_ti.get_compression() == kENCODING_GEOINT
207  ? llvm::PointerType::get(llvm::Type::getInt32Ty(cgen_state->context_),
208  0)
209  : llvm::PointerType::get(llvm::Type::getDoubleTy(cgen_state->context_),
210  0)),
211  ret_coords);
212  }
213 
214  return {ret_coords,
215  cgen_state->llInt(ret_ti.get_compression() == kENCODING_GEOINT ? 8 : 16)};
216  }
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
int32_t get_compression_scheme(const SQLTypeInfo &ti)
Definition: Compression.cpp:23
std::string suffix(SQLTypes type)
Definition: Codegen.cpp:68
llvm::LLVMContext & context_
Definition: CgenState.h:338
const SQLTypeInfo & get_type_info() const
Definition: Analyzer.h:77
const Analyzer::GeoOperator * operator_
Definition: Codegen.h:70
llvm::ConstantInt * llInt(const T v) const
Definition: CgenState.h:307
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

+ Here is the call graph for this function:

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

Implements spatial_type::Codegen.

Definition at line 37 of file Centroid.h.

References CHECK, CHECK_EQ, get_int_type(), spatial_type::Codegen::getOperand(), i, spatial_type::Codegen::is_nullable_, kENCODING_GEOINT, log2_bytes(), and size().

40  {
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  const uint32_t coords_elem_sz_bytes =
52  operand_ti.get_compression() == kENCODING_GEOINT ? 1 : 8;
53  auto& builder = cgen_state->ir_builder_;
54 
55  std::vector<llvm::Value*> operand_lvs;
56  // iterate over column inputs
57  if (dynamic_cast<const Analyzer::ColumnVar*>(operand)) {
58  for (size_t i = 0; i < arg_lvs.size(); i++) {
59  auto lv = arg_lvs[i];
60  auto array_buff_lv =
61  cgen_state->emitExternalCall("array_buff",
62  llvm::Type::getInt8PtrTy(cgen_state->context_),
63  {lv, pos_lvs.front()});
64  if (i > 0) {
65  array_buff_lv = builder.CreateBitCast(
66  array_buff_lv, llvm::Type::getInt32PtrTy(cgen_state->context_));
67  }
68  operand_lvs.push_back(array_buff_lv);
69  const auto ptr_type = llvm::dyn_cast_or_null<llvm::PointerType>(lv->getType());
70  CHECK(ptr_type);
71  const auto elem_type = ptr_type->getElementType();
72  CHECK(elem_type);
73  std::vector<llvm::Value*> array_sz_args{
74  lv,
75  pos_lvs.front(),
76  cgen_state->llInt(log2_bytes(i == 0 ? coords_elem_sz_bytes : 4))};
77  if (is_nullable_) { // TODO: should we do this for all arguments, or just points?
78  array_sz_args.push_back(
79  cgen_state->llInt(static_cast<int32_t>(inline_int_null_value<int32_t>())));
80  }
81  operand_lvs.push_back(cgen_state->emitExternalCall(
82  size_fn_name, get_int_type(32, cgen_state->context_), array_sz_args));
83  }
84  } else {
85  for (size_t i = 0; i < arg_lvs.size(); i++) {
86  auto arg_lv = arg_lvs[i];
87  if (i > 0 && arg_lv->getType()->isPointerTy()) {
88  arg_lv = builder.CreateBitCast(arg_lv,
89  llvm::Type::getInt32PtrTy(cgen_state->context_));
90  }
91  operand_lvs.push_back(arg_lv);
92  }
93  }
94  CHECK_EQ(operand_lvs.size(),
95  size_t(2 * operand_ti.get_physical_coord_cols())); // array ptr and size
96 
97  // note that this block is the only one that differs from Area/Perimeter
98  // use the points array size argument for nullability
99  llvm::Value* null_check_operand_lv{nullptr};
100  if (is_nullable_) {
101  null_check_operand_lv = operand_lvs[1];
102  if (null_check_operand_lv->getType() !=
103  llvm::Type::getInt32Ty(cgen_state->context_)) {
104  CHECK(null_check_operand_lv->getType() ==
105  llvm::Type::getInt64Ty(cgen_state->context_));
106  // Geos functions come out 64-bit, cast down to 32 for now
107 
108  null_check_operand_lv = builder.CreateTrunc(
109  null_check_operand_lv, llvm::Type::getInt32Ty(cgen_state->context_));
110  }
111  }
112 
113  return std::make_tuple(operand_lvs, null_check_operand_lv);
114  }
#define CHECK_EQ(x, y)
Definition: Logger.h:219
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
llvm::Type * get_int_type(const int width, llvm::LLVMContext &context)
llvm::LLVMContext & context_
Definition: CgenState.h:338
size_t size() const final
Definition: Centroid.h:33
llvm::ConstantInt * llInt(const T v) const
Definition: CgenState.h:307
#define CHECK(condition)
Definition: Logger.h:211
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

+ Here is the call graph for this function:

SQLTypeInfo spatial_type::Centroid::getNullType ( ) const
inlinefinalvirtual

Implements spatial_type::Codegen.

Definition at line 35 of file Centroid.h.

References kINT.

35 { return SQLTypeInfo(kINT); }
Definition: sqltypes.h:45
size_t spatial_type::Centroid::size ( ) const
inlinefinalvirtual

Implements spatial_type::Codegen.

Definition at line 33 of file Centroid.h.

Referenced by codegenLoads().

33 { return 1; }

+ Here is the caller graph for this function:


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