OmniSciDB  72c90bc290
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
PointAccessors.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 
17 #pragma once
18 
20 
21 namespace spatial_type {
22 
23 // ST_X and ST_Y
24 class PointAccessors : public Codegen {
25  public:
26  PointAccessors(const Analyzer::GeoOperator* geo_operator) : Codegen(geo_operator) {
27  CHECK_EQ(operator_->size(), size_t(1));
28  }
29 
30  size_t size() const final { return 1; }
31 
32  SQLTypeInfo getNullType() const final { return SQLTypeInfo(kBOOLEAN); }
33 
34  // returns arguments lvs and null lv
35  std::tuple<std::vector<llvm::Value*>, llvm::Value*> codegenLoads(
36  const std::vector<llvm::Value*>& arg_lvs,
37  const std::vector<llvm::Value*>& pos_lvs,
38  CgenState* cgen_state) final {
39  CHECK_EQ(pos_lvs.size(), size());
40  auto operand = getOperand(0);
41  CHECK(operand);
42  const auto& geo_ti = operand->get_type_info();
43  CHECK(geo_ti.is_geometry());
44  auto& builder = cgen_state->ir_builder_;
45 
46  llvm::Value* array_buff_ptr{nullptr};
47  llvm::Value* is_null{nullptr};
48  if (arg_lvs.size() == 1) {
49  if (dynamic_cast<const Analyzer::GeoExpr*>(operand)) {
50  const auto ptr_type =
51  llvm::dyn_cast<llvm::PointerType>(arg_lvs.front()->getType());
52  CHECK(ptr_type);
53  const auto is_null_lv =
54  builder.CreateICmp(llvm::CmpInst::ICMP_EQ,
55  arg_lvs.front(),
56  llvm::ConstantPointerNull::get(ptr_type));
57  return std::make_tuple(arg_lvs, is_null_lv);
58  }
59  // col byte stream, get the array buffer ptr and is null attributes and cache
61  arg_lvs.front(), pos_lvs.front(), geo_ti, cgen_state);
62  array_buff_ptr = arr_load_lvs.buffer;
63  is_null = arr_load_lvs.is_null;
64  } else {
65  // ptr and size
66  CHECK_EQ(arg_lvs.size(), size_t(2));
67  if (dynamic_cast<const Analyzer::GeoOperator*>(operand)) {
68  // null check will be if the ptr is a nullptr
69  is_null = builder.CreateICmp(
70  llvm::CmpInst::ICMP_EQ,
71  arg_lvs.front(),
72  llvm::ConstantPointerNull::get( // TODO: check ptr address space
73  geo_ti.get_compression() == kENCODING_GEOINT
74  ? llvm::Type::getInt32PtrTy(cgen_state->context_)
75  : llvm::Type::getDoublePtrTy(cgen_state->context_)));
76  }
77 
78  // TODO: nulls from other types not yet supported
79  array_buff_ptr = arg_lvs.front();
80  }
81  CHECK(array_buff_ptr) << operator_->toString();
82  if (!is_null) {
83  is_nullable_ = false;
84  }
85  return std::make_tuple(std::vector<llvm::Value*>{array_buff_ptr}, is_null);
86  }
87 
88  std::vector<llvm::Value*> codegen(const std::vector<llvm::Value*>& args,
89  CodeGenerator::NullCheckCodegen* nullcheck_codegen,
90  CgenState* cgen_state,
91  const CompilationOptions& co) final {
92  CHECK_EQ(args.size(), size_t(1));
93  const auto array_buff_ptr = args.front();
94 
95  const auto& geo_ti = getOperand(0)->get_type_info();
96  CHECK(geo_ti.is_geometry());
97  auto& builder = cgen_state->ir_builder_;
98 
99  const bool is_x = operator_->getName() == "ST_X";
100  const std::string expr_name = is_x ? "x" : "y";
101 
102  llvm::Value* coord_lv;
103  if (geo_ti.get_compression() == kENCODING_GEOINT) {
104  auto compressed_arr_ptr = builder.CreateBitCast(
105  array_buff_ptr, llvm::Type::getInt32PtrTy(cgen_state->context_));
106  auto coord_index = is_x ? cgen_state->llInt(0) : cgen_state->llInt(1);
107  auto coord_lv_ptr = builder.CreateGEP(
108  compressed_arr_ptr->getType()->getScalarType()->getPointerElementType(),
109  compressed_arr_ptr,
110  coord_index,
111  expr_name + "_coord_ptr");
112  auto compressed_coord_lv =
113  builder.CreateLoad(coord_lv_ptr->getType()->getPointerElementType(),
114  coord_lv_ptr,
115  expr_name + "_coord_compressed");
116 
117  coord_lv =
118  cgen_state->emitExternalCall("decompress_" + expr_name + "_coord_geoint",
119  llvm::Type::getDoubleTy(cgen_state->context_),
120  {compressed_coord_lv});
121  } else {
122  auto coord_arr_ptr = builder.CreateBitCast(
123  array_buff_ptr, llvm::Type::getDoublePtrTy(cgen_state->context_));
124  auto coord_index = is_x ? cgen_state->llInt(0) : cgen_state->llInt(1);
125  auto coord_lv_ptr = builder.CreateGEP(
126  coord_arr_ptr->getType()->getScalarType()->getPointerElementType(),
127  coord_arr_ptr,
128  coord_index,
129  expr_name + "_coord_ptr");
130  coord_lv = builder.CreateLoad(coord_lv_ptr->getType()->getPointerElementType(),
131  coord_lv_ptr,
132  expr_name + "_coord");
133  }
134 
135  auto ret = coord_lv;
136  if (is_nullable_) {
137  CHECK(nullcheck_codegen);
138  ret = nullcheck_codegen->finalize(cgen_state->inlineFpNull(SQLTypeInfo(kDOUBLE)),
139  ret);
140  }
141  const auto key = operator_->toString();
142  CHECK(cgen_state->geo_target_cache_.insert(std::make_pair(key, ret)).second);
143  return {ret};
144  }
145 };
146 
147 } // namespace spatial_type
#define CHECK_EQ(x, y)
Definition: Logger.h:301
PointAccessors(const Analyzer::GeoOperator *geo_operator)
size_t size() const final
llvm::Value * buffer
Definition: CgenState.h:36
CONSTEXPR DEVICE bool is_null(const T &value)
const std::string & getName() const
Definition: Analyzer.h:3167
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
SQLTypeInfo getNullType() const final
const SQLTypeInfo & get_type_info() const
Definition: Analyzer.h:79
static ArrayLoadCodegen codegenGeoArrayLoadAndNullcheck(llvm::Value *byte_stream, llvm::Value *pos, const SQLTypeInfo &ti, CgenState *cgen_state)
Definition: GeoIR.cpp:23
const Analyzer::GeoOperator * operator_
Definition: Codegen.h:67
size_t size() const
Definition: Analyzer.cpp:4182
std::vector< llvm::Value * > codegen(const std::vector< llvm::Value * > &args, CodeGenerator::NullCheckCodegen *nullcheck_codegen, CgenState *cgen_state, const CompilationOptions &co) final
#define CHECK(condition)
Definition: Logger.h:291
virtual const Analyzer::Expr * getOperand(const size_t index)
Definition: Codegen.cpp:64
std::string toString() const override
Definition: Analyzer.cpp:4154