21 namespace spatial_type {
31 size_t size() const final {
return 1; }
36 const std::vector<llvm::Value*>& arg_lvs,
37 const std::vector<llvm::Value*>& pos_lvs,
42 const auto& operand_ti = operand->get_type_info();
44 std::string size_fn_name =
"array_size";
46 size_fn_name +=
"_nullable";
49 auto& builder = cgen_state->ir_builder_;
51 std::vector<llvm::Value*> operand_lvs;
53 if (dynamic_cast<const Analyzer::ColumnVar*>(operand)) {
54 for (
size_t i = 0; i < arg_lvs.size(); i++) {
57 cgen_state->emitExternalCall(
"array_buff",
58 llvm::Type::getInt8PtrTy(cgen_state->context_),
59 {lv, pos_lvs.front()});
60 auto const is_coords = (i == 0);
62 array_buff_lv = builder.CreateBitCast(
63 array_buff_lv, llvm::Type::getInt32PtrTy(cgen_state->context_));
65 operand_lvs.push_back(array_buff_lv);
66 const auto ptr_type = llvm::dyn_cast_or_null<llvm::PointerType>(lv->getType());
68 const auto elem_type = ptr_type->getPointerElementType();
70 auto const shift =
log2_bytes(is_coords ? 1 : 4);
71 std::vector<llvm::Value*> array_sz_args{
72 lv, pos_lvs.front(), cgen_state->llInt(shift)};
74 array_sz_args.push_back(
75 cgen_state->llInt(static_cast<int32_t>(inline_int_null_value<int32_t>())));
77 operand_lvs.push_back(cgen_state->emitExternalCall(
78 size_fn_name,
get_int_type(32, cgen_state->context_), array_sz_args));
81 for (
size_t i = 0; i < arg_lvs.size(); i++) {
82 auto arg_lv = arg_lvs[i];
83 if (i > 0 && arg_lv->getType()->isPointerTy()) {
84 arg_lv = builder.CreateBitCast(arg_lv,
85 llvm::Type::getInt32PtrTy(cgen_state->context_));
87 operand_lvs.push_back(arg_lv);
91 size_t(2 * operand_ti.get_physical_coord_cols()));
95 llvm::Value* null_check_operand_lv{
nullptr};
97 null_check_operand_lv = operand_lvs[1];
98 if (null_check_operand_lv->getType() !=
99 llvm::Type::getInt32Ty(cgen_state->context_)) {
100 CHECK(null_check_operand_lv->getType() ==
101 llvm::Type::getInt64Ty(cgen_state->context_));
104 null_check_operand_lv = builder.CreateTrunc(
105 null_check_operand_lv, llvm::Type::getInt32Ty(cgen_state->context_));
109 return std::make_tuple(operand_lvs, null_check_operand_lv);
112 std::vector<llvm::Value*>
codegen(
const std::vector<llvm::Value*>&
args,
116 std::string func_name =
"ST_Centroid";
117 const auto& ret_ti = operator_->get_type_info();
118 CHECK(ret_ti.is_geometry() && ret_ti.get_type() ==
kPOINT);
119 const auto& operand_ti = getOperand(0)->get_type_info();
121 auto& builder = cgen_state->ir_builder_;
124 auto elem_ty = llvm::Type::getDoubleTy(cgen_state->context_);
125 llvm::ArrayType* arr_type = llvm::ArrayType::get(elem_ty, 2);
126 auto pt_local_storage_lv =
127 builder.CreateAlloca(arr_type,
nullptr, func_name +
"_Local_Storage");
129 llvm::Value* pt_compressed_local_storage_lv{NULL};
132 auto elem_ty = llvm::Type::getInt32Ty(cgen_state->context_);
133 llvm::ArrayType* arr_type = llvm::ArrayType::get(elem_ty, 2);
134 pt_compressed_local_storage_lv = builder.CreateAlloca(
135 arr_type,
nullptr, func_name +
"_Compressed_Local_Storage");
140 auto operand_lvs =
args;
143 operand_lvs.push_back(
145 operand_lvs.push_back(cgen_state->llInt(operand_ti.get_input_srid()));
146 auto output_srid = operand_ti.get_output_srid();
147 if (
const auto srid_override = operator_->getOutputSridOverride()) {
148 output_srid = *srid_override;
150 operand_lvs.push_back(cgen_state->llInt(output_srid));
152 auto idx_lv = cgen_state->llInt(0);
153 auto pt_local_storage_gep = llvm::GetElementPtrInst::CreateInBounds(
154 pt_local_storage_lv->getType()->getScalarType()->getPointerElementType(),
158 builder.GetInsertBlock());
160 operand_lvs.push_back(pt_local_storage_gep);
162 cgen_state->emitExternalCall(
163 func_name, llvm::Type::getVoidTy(cgen_state->context_), operand_lvs);
165 llvm::Value* ret_coords = pt_local_storage_lv;
170 auto x_ptr = builder.CreateGEP(
171 pt_local_storage_lv->getType()->getScalarType()->getPointerElementType(),
173 {cgen_state->llInt(0), cgen_state->llInt(0)},
175 auto x_lv = builder.CreateLoad(x_ptr->getType()->getPointerElementType(), x_ptr);
176 auto compressed_x_lv =
177 cgen_state->emitExternalCall(
"compress_x_coord_geoint",
178 llvm::Type::getInt32Ty(cgen_state->context_),
180 auto compressed_x_ptr =
181 builder.CreateGEP(pt_compressed_local_storage_lv->getType()
183 ->getPointerElementType(),
184 pt_compressed_local_storage_lv,
185 {cgen_state->llInt(0), cgen_state->llInt(0)},
187 builder.CreateStore(compressed_x_lv, compressed_x_ptr);
189 auto y_ptr = builder.CreateGEP(
190 pt_local_storage_lv->getType()->getScalarType()->getPointerElementType(),
192 {cgen_state->llInt(0), cgen_state->llInt(1)},
194 auto y_lv = builder.CreateLoad(y_ptr->getType()->getPointerElementType(), y_ptr);
195 auto compressed_y_lv =
196 cgen_state->emitExternalCall(
"compress_y_coord_geoint",
197 llvm::Type::getInt32Ty(cgen_state->context_),
199 auto compressed_y_ptr =
200 builder.CreateGEP(pt_compressed_local_storage_lv->getType()
202 ->getPointerElementType(),
203 pt_compressed_local_storage_lv,
204 {cgen_state->llInt(0), cgen_state->llInt(1)},
206 builder.CreateStore(compressed_y_lv, compressed_y_ptr);
208 ret_coords = pt_compressed_local_storage_lv;
214 ? llvm::Type::getInt32PtrTy(cgen_state->context_)
215 : llvm::
Type::getDoublePtrTy(cgen_state->context_);
216 ret_coords = builder.CreateBitCast(ret_coords, ret_ty);
219 CHECK(nullcheck_codegen);
220 ret_coords = nullcheck_codegen->finalize(
221 llvm::ConstantPointerNull::get(
223 ? llvm::PointerType::get(llvm::Type::getInt32Ty(cgen_state->context_),
225 : llvm::PointerType::get(llvm::
Type::getDoubleTy(cgen_state->context_),
int32_t get_compression_scheme(const SQLTypeInfo &ti)
llvm::Type * get_int_type(const int width, llvm::LLVMContext &context)
std::vector< llvm::Value * > codegen(const std::vector< llvm::Value * > &args, CodeGenerator::NullCheckCodegen *nullcheck_codegen, CgenState *cgen_state, const CompilationOptions &co) final
std::string suffix(SQLTypes type)
const SQLTypeInfo & get_type_info() const
Centroid(const Analyzer::GeoOperator *geo_operator)
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
const Analyzer::GeoOperator * operator_
size_t size() const final
virtual const Analyzer::Expr * getOperand(const size_t index)
uint32_t log2_bytes(const uint32_t bytes)
SQLTypeInfo getNullType() const final