OmniSciDB  c0231cc57d
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
DateTimeIR.cpp
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 #include "CodeGenerator.h"
18 
19 #include "DateTimeUtils.h"
20 #include "Execute.h"
21 
22 using namespace DateTimeUtils;
23 
24 namespace {
25 
27  switch (field) {
28  case kEPOCH:
29  return "extract_epoch";
30  case kDATEEPOCH:
31  return "extract_dateepoch";
32  case kQUARTERDAY:
33  return "extract_quarterday";
34  case kHOUR:
35  return "extract_hour";
36  case kMINUTE:
37  return "extract_minute";
38  case kSECOND:
39  return "extract_second";
40  case kMILLISECOND:
41  return "extract_millisecond";
42  case kMICROSECOND:
43  return "extract_microsecond";
44  case kNANOSECOND:
45  return "extract_nanosecond";
46  case kDOW:
47  return "extract_dow";
48  case kISODOW:
49  return "extract_isodow";
50  case kDAY:
51  return "extract_day";
52  case kWEEK:
53  return "extract_week_monday";
54  case kWEEK_SUNDAY:
55  return "extract_week_sunday";
56  case kWEEK_SATURDAY:
57  return "extract_week_saturday";
58  case kDOY:
59  return "extract_day_of_year";
60  case kMONTH:
61  return "extract_month";
62  case kQUARTER:
63  return "extract_quarter";
64  case kYEAR:
65  return "extract_year";
66  case kUNKNOWN_FIELD:
67  UNREACHABLE();
68  return "";
69  }
70  UNREACHABLE();
71  return "";
72 }
73 
74 } // namespace
75 
76 llvm::Value* CodeGenerator::codegen(const Analyzer::ExtractExpr* extract_expr,
77  const CompilationOptions& co) {
78  AUTOMATIC_IR_METADATA(cgen_state_);
79  auto from_expr = codegen(extract_expr->get_from_expr(), true, co).front();
80  const int32_t extract_field{extract_expr->get_field()};
81  const auto& extract_expr_ti = extract_expr->get_from_expr()->get_type_info();
82  if (extract_field == kEPOCH) {
83  CHECK(extract_expr_ti.get_type() == kTIMESTAMP ||
84  extract_expr_ti.get_type() == kDATE);
85  if (from_expr->getType()->isIntegerTy(32)) {
86  from_expr =
87  cgen_state_->ir_builder_.CreateCast(llvm::Instruction::CastOps::SExt,
88  from_expr,
89  get_int_type(64, cgen_state_->context_));
90  return from_expr;
91  }
92  }
93  CHECK(from_expr->getType()->isIntegerTy(64));
94  if (extract_expr_ti.is_high_precision_timestamp()) {
95  from_expr = codegenExtractHighPrecisionTimestamps(
96  from_expr, extract_expr_ti, extract_expr->get_field());
97  }
98  if (!extract_expr_ti.is_high_precision_timestamp() &&
99  is_subsecond_extract_field(extract_expr->get_field())) {
100  from_expr =
101  extract_expr_ti.get_notnull()
102  ? cgen_state_->ir_builder_.CreateMul(
103  from_expr,
104  cgen_state_->llInt(
106  : cgen_state_->emitCall(
107  "mul_int64_t_nullable_lhs",
108  {from_expr,
109  cgen_state_->llInt(
111  cgen_state_->inlineIntNull(extract_expr_ti)});
112  }
113  const auto extract_fname = get_extract_function_name(extract_expr->get_field());
114  if (!extract_expr_ti.get_notnull()) {
115  llvm::BasicBlock* extract_nullcheck_bb{nullptr};
116  llvm::PHINode* extract_nullcheck_value{nullptr};
117  {
118  DiamondCodegen null_check(cgen_state_->ir_builder_.CreateICmp(
119  llvm::ICmpInst::ICMP_EQ,
120  from_expr,
121  cgen_state_->inlineIntNull(extract_expr_ti)),
122  executor(),
123  false,
124  "extract_nullcheck",
125  nullptr,
126  false);
127  // generate a phi node depending on whether we got a null or not
128  extract_nullcheck_bb = llvm::BasicBlock::Create(
129  cgen_state_->context_, "extract_nullcheck_bb", cgen_state_->current_func_);
130 
131  // update the blocks created by diamond codegen to point to the newly created phi
132  // block
133  cgen_state_->ir_builder_.SetInsertPoint(null_check.cond_true_);
134  cgen_state_->ir_builder_.CreateBr(extract_nullcheck_bb);
135  cgen_state_->ir_builder_.SetInsertPoint(null_check.cond_false_);
136  auto extract_call =
137  cgen_state_->emitExternalCall(extract_fname,
138  get_int_type(64, cgen_state_->context_),
139  std::vector<llvm::Value*>{from_expr});
140  cgen_state_->ir_builder_.CreateBr(extract_nullcheck_bb);
141 
142  cgen_state_->ir_builder_.SetInsertPoint(extract_nullcheck_bb);
143  extract_nullcheck_value = cgen_state_->ir_builder_.CreatePHI(
144  get_int_type(64, cgen_state_->context_), 2, "extract_value");
145  extract_nullcheck_value->addIncoming(extract_call, null_check.cond_false_);
146  extract_nullcheck_value->addIncoming(cgen_state_->inlineIntNull(extract_expr_ti),
147  null_check.cond_true_);
148  }
149 
150  // diamond codegen will set the insert point in its destructor. override it to
151  // continue using the extract nullcheck bb
152  CHECK(extract_nullcheck_bb);
153  cgen_state_->ir_builder_.SetInsertPoint(extract_nullcheck_bb);
154  CHECK(extract_nullcheck_value);
155  return extract_nullcheck_value;
156  } else {
157  return cgen_state_->emitExternalCall(extract_fname,
158  get_int_type(64, cgen_state_->context_),
159  std::vector<llvm::Value*>{from_expr});
160  }
161 }
162 
163 llvm::Value* CodeGenerator::codegen(const Analyzer::DateaddExpr* dateadd_expr,
164  const CompilationOptions& co) {
166  const auto& dateadd_expr_ti = dateadd_expr->get_type_info();
167  CHECK(dateadd_expr_ti.get_type() == kTIMESTAMP || dateadd_expr_ti.get_type() == kDATE);
168  auto datetime = codegen(dateadd_expr->get_datetime_expr(), true, co).front();
169  CHECK(datetime->getType()->isIntegerTy(64));
170  auto number = codegen(dateadd_expr->get_number_expr(), true, co).front();
171 
172  const auto& datetime_ti = dateadd_expr->get_datetime_expr()->get_type_info();
173  std::vector<llvm::Value*> dateadd_args{
174  cgen_state_->llInt(static_cast<int32_t>(dateadd_expr->get_field())),
175  number,
176  datetime};
177  std::string dateadd_fname{"DateAdd"};
178  if (is_subsecond_dateadd_field(dateadd_expr->get_field()) ||
179  dateadd_expr_ti.is_high_precision_timestamp()) {
180  dateadd_fname += "HighPrecision";
181  dateadd_args.push_back(
182  cgen_state_->llInt(static_cast<int32_t>(datetime_ti.get_dimension())));
183  }
184  if (!datetime_ti.get_notnull()) {
185  dateadd_args.push_back(cgen_state_->inlineIntNull(datetime_ti));
186  dateadd_fname += "Nullable";
187  }
188  return cgen_state_->emitExternalCall(dateadd_fname,
190  dateadd_args,
191  {llvm::Attribute::NoUnwind,
192  llvm::Attribute::ReadNone,
193  llvm::Attribute::Speculatable});
194 }
195 
196 llvm::Value* CodeGenerator::codegen(const Analyzer::DatediffExpr* datediff_expr,
197  const CompilationOptions& co) {
199  auto start = codegen(datediff_expr->get_start_expr(), true, co).front();
200  CHECK(start->getType()->isIntegerTy(64));
201  auto end = codegen(datediff_expr->get_end_expr(), true, co).front();
202  CHECK(end->getType()->isIntegerTy(32) || end->getType()->isIntegerTy(64));
203  const auto& start_ti = datediff_expr->get_start_expr()->get_type_info();
204  const auto& end_ti = datediff_expr->get_end_expr()->get_type_info();
205  std::vector<llvm::Value*> datediff_args{
206  cgen_state_->llInt(static_cast<int32_t>(datediff_expr->get_field())), start, end};
207  std::string datediff_fname{"DateDiff"};
208  if (start_ti.is_high_precision_timestamp() || end_ti.is_high_precision_timestamp()) {
209  datediff_fname += "HighPrecision";
210  datediff_args.push_back(
211  cgen_state_->llInt(static_cast<int32_t>(start_ti.get_dimension())));
212  datediff_args.push_back(
213  cgen_state_->llInt(static_cast<int32_t>(end_ti.get_dimension())));
214  }
215  const auto& ret_ti = datediff_expr->get_type_info();
216  if (!start_ti.get_notnull() || !end_ti.get_notnull()) {
217  datediff_args.push_back(cgen_state_->inlineIntNull(ret_ti));
218  datediff_fname += "Nullable";
219  }
221  datediff_fname, get_int_type(64, cgen_state_->context_), datediff_args);
222 }
223 
224 llvm::Value* CodeGenerator::codegen(const Analyzer::DatetruncExpr* datetrunc_expr,
225  const CompilationOptions& co) {
227  auto from_expr = codegen(datetrunc_expr->get_from_expr(), true, co).front();
228  const auto& datetrunc_expr_ti = datetrunc_expr->get_from_expr()->get_type_info();
229  CHECK(from_expr->getType()->isIntegerTy(64));
230  DatetruncField const field = datetrunc_expr->get_field();
231  if (datetrunc_expr_ti.is_high_precision_timestamp()) {
232  return codegenDateTruncHighPrecisionTimestamps(from_expr, datetrunc_expr_ti, field);
233  }
234  static_assert(dtSECOND + 1 == dtMILLISECOND, "Please keep these consecutive.");
235  static_assert(dtMILLISECOND + 1 == dtMICROSECOND, "Please keep these consecutive.");
236  static_assert(dtMICROSECOND + 1 == dtNANOSECOND, "Please keep these consecutive.");
237  if (dtSECOND <= field && field <= dtNANOSECOND) {
238  return cgen_state_->ir_builder_.CreateCast(llvm::Instruction::CastOps::SExt,
239  from_expr,
241  }
242  std::unique_ptr<CodeGenerator::NullCheckCodegen> nullcheck_codegen;
243  const bool is_nullable = !datetrunc_expr_ti.get_notnull();
244  if (is_nullable) {
245  nullcheck_codegen = std::make_unique<NullCheckCodegen>(
246  cgen_state_, executor(), from_expr, datetrunc_expr_ti, "date_trunc_nullcheck");
247  }
248  char const* const fname = datetrunc_fname_lookup.at(field);
249  auto ret = cgen_state_->emitExternalCall(
250  fname, get_int_type(64, cgen_state_->context_), {from_expr});
251  if (is_nullable) {
252  ret = nullcheck_codegen->finalize(ll_int(NULL_BIGINT, cgen_state_->context_), ret);
253  }
254  return ret;
255 }
256 
258  llvm::Value* ts_lv,
259  const SQLTypeInfo& ti,
260  const ExtractField& field) {
263  CHECK(ts_lv->getType()->isIntegerTy(64));
264  if (is_subsecond_extract_field(field)) {
265  const auto result =
267  if (result.first == kMULTIPLY) {
268  return ti.get_notnull()
269  ? cgen_state_->ir_builder_.CreateMul(
270  ts_lv, cgen_state_->llInt(static_cast<int64_t>(result.second)))
272  "mul_int64_t_nullable_lhs",
273  {ts_lv,
274  cgen_state_->llInt(static_cast<int64_t>(result.second)),
275  cgen_state_->inlineIntNull(ti)});
276  } else if (result.first == kDIVIDE) {
277  return ti.get_notnull()
278  ? cgen_state_->ir_builder_.CreateSDiv(
279  ts_lv, cgen_state_->llInt(static_cast<int64_t>(result.second)))
281  "floor_div_nullable_lhs",
282  {ts_lv,
283  cgen_state_->llInt(static_cast<int64_t>(result.second)),
284  cgen_state_->inlineIntNull(ti)});
285  } else {
286  return ts_lv;
287  }
288  }
289  return ti.get_notnull()
290  ? cgen_state_->ir_builder_.CreateSDiv(
291  ts_lv,
292  cgen_state_->llInt(static_cast<int64_t>(
293  get_timestamp_precision_scale(ti.get_dimension()))))
294  : cgen_state_->emitCall(
295  "floor_div_nullable_lhs",
296  {ts_lv,
297  cgen_state_->llInt(get_timestamp_precision_scale(ti.get_dimension())),
298  cgen_state_->inlineIntNull(ti)});
299 }
300 
302  llvm::Value* ts_lv,
303  const SQLTypeInfo& ti,
304  const DatetruncField& field) {
305  // Only needed for i in { 0, 3, 6, 9 }.
306  constexpr int64_t pow10[10]{1, 0, 0, 1000, 0, 0, 1000000, 0, 0, 1000000000};
309  CHECK(ts_lv->getType()->isIntegerTy(64));
310  bool const is_nullable = !ti.get_notnull();
311  static_assert(dtSECOND + 1 == dtMILLISECOND, "Please keep these consecutive.");
312  static_assert(dtMILLISECOND + 1 == dtMICROSECOND, "Please keep these consecutive.");
313  static_assert(dtMICROSECOND + 1 == dtNANOSECOND, "Please keep these consecutive.");
314  if (dtSECOND <= field && field <= dtNANOSECOND) {
315  unsigned const start_dim = ti.get_dimension(); // 0, 3, 6, 9
316  unsigned const trunc_dim = (field - dtSECOND) * 3; // 0, 3, 6, 9
317  if (start_dim <= trunc_dim) {
318  return ts_lv; // Truncating to an equal or higher precision has no effect.
319  }
320  int64_t const dscale = pow10[start_dim - trunc_dim]; // 1e3, 1e6, 1e9
321  if (is_nullable) {
322  ts_lv = cgen_state_->emitCall(
323  "floor_div_nullable_lhs",
324  {ts_lv, cgen_state_->llInt(dscale), cgen_state_->inlineIntNull(ti)});
325  return cgen_state_->emitCall(
326  "mul_int64_t_nullable_lhs",
327  {ts_lv, cgen_state_->llInt(dscale), cgen_state_->inlineIntNull(ti)});
328  } else {
329  ts_lv = cgen_state_->ir_builder_.CreateSDiv(ts_lv, cgen_state_->llInt(dscale));
330  return cgen_state_->ir_builder_.CreateMul(ts_lv, cgen_state_->llInt(dscale));
331  }
332  }
333  int64_t const scale = pow10[ti.get_dimension()];
334  ts_lv = is_nullable
336  "floor_div_nullable_lhs",
337  {ts_lv, cgen_state_->llInt(scale), cgen_state_->inlineIntNull(ti)})
338  : cgen_state_->ir_builder_.CreateSDiv(ts_lv, cgen_state_->llInt(scale));
339 
340  std::unique_ptr<CodeGenerator::NullCheckCodegen> nullcheck_codegen;
341  if (is_nullable) {
342  nullcheck_codegen = std::make_unique<NullCheckCodegen>(
343  cgen_state_, executor(), ts_lv, ti, "date_trunc_hp_nullcheck");
344  }
345  char const* const fname = datetrunc_fname_lookup.at(field);
346  ts_lv = cgen_state_->emitExternalCall(
347  fname, get_int_type(64, cgen_state_->context_), {ts_lv});
348  if (is_nullable) {
349  ts_lv =
350  nullcheck_codegen->finalize(ll_int(NULL_BIGINT, cgen_state_->context_), ts_lv);
351  }
352 
353  return is_nullable
354  ? cgen_state_->emitCall(
355  "mul_int64_t_nullable_lhs",
356  {ts_lv, cgen_state_->llInt(scale), cgen_state_->inlineIntNull(ti)})
357  : cgen_state_->ir_builder_.CreateMul(ts_lv, cgen_state_->llInt(scale));
358 }
const Expr * get_from_expr() const
Definition: Analyzer.h:1305
constexpr int64_t get_extract_timestamp_precision_scale(const ExtractField field)
Definition: DateTimeUtils.h:81
CgenState * cgen_state_
#define NULL_BIGINT
ExtractField get_field() const
Definition: Analyzer.h:1304
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:217
llvm::ConstantInt * ll_int(const T v, llvm::LLVMContext &context)
llvm::IRBuilder ir_builder_
Definition: CgenState.h:441
const char * get_extract_function_name(ExtractField field)
Definition: DateTimeIR.cpp:26
#define UNREACHABLE()
Definition: Logger.h:266
llvm::Value * codegenExtractHighPrecisionTimestamps(llvm::Value *, const SQLTypeInfo &, const ExtractField &)
Definition: DateTimeIR.cpp:257
DatetruncField get_field() const
Definition: Analyzer.h:1429
const std::pair< SQLOps, int64_t > get_extract_high_precision_adjusted_scale(const ExtractField &field, const int32_t dimen)
llvm::Type * get_int_type(const int width, llvm::LLVMContext &context)
const rapidjson::Value & field(const rapidjson::Value &obj, const char field[]) noexcept
Definition: JsonAccessors.h:31
llvm::LLVMContext & context_
Definition: CgenState.h:439
DatetruncField
Definition: DateTruncate.h:27
llvm::ConstantInt * inlineIntNull(const SQLTypeInfo &)
Definition: CgenState.cpp:64
#define AUTOMATIC_IR_METADATA(CGENSTATE)
const Expr * get_start_expr() const
Definition: Analyzer.h:1388
const SQLTypeInfo & get_type_info() const
Definition: Analyzer.h:82
llvm::Value * emitCall(const std::string &fname, const std::vector< llvm::Value * > &args)
Definition: CgenState.cpp:219
std::vector< llvm::Value * > codegen(const Analyzer::Expr *, const bool fetch_columns, const CompilationOptions &)
Definition: IRCodegen.cpp:30
Definition: sqltypes.h:67
constexpr std::array< char const *, dtINVALID > datetrunc_fname_lookup
Definition: DateTruncate.h:49
const Expr * get_from_expr() const
Definition: Analyzer.h:1430
const Expr * get_datetime_expr() const
Definition: Analyzer.h:1347
HOST DEVICE int get_dimension() const
Definition: sqltypes.h:406
ExtractField
DateaddField get_field() const
Definition: Analyzer.h:1345
constexpr bool is_subsecond_dateadd_field(const DateaddField field)
Definition: DateTimeUtils.h:99
llvm::ConstantInt * llInt(const T v) const
Definition: CgenState.h:306
const Expr * get_end_expr() const
Definition: Analyzer.h:1389
#define CHECK(condition)
Definition: Logger.h:222
constexpr int64_t get_timestamp_precision_scale(const int32_t dimen)
Definition: DateTimeUtils.h:51
bool is_high_precision_timestamp() const
Definition: sqltypes.h:1010
llvm::Value * codegenDateTruncHighPrecisionTimestamps(llvm::Value *, const SQLTypeInfo &, const DatetruncField &)
Definition: DateTimeIR.cpp:301
HOST DEVICE bool get_notnull() const
Definition: sqltypes.h:411
DatetruncField get_field() const
Definition: Analyzer.h:1387
constexpr bool is_subsecond_extract_field(const ExtractField &field)
Definition: DateTimeUtils.h:95
const Expr * get_number_expr() const
Definition: Analyzer.h:1346
Executor * executor() const