This repository was archived by the owner on Feb 20, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 502
/
Copy pathvm.cpp
2701 lines (2354 loc) · 110 KB
/
vm.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "execution/vm/vm.h"
#include <numeric>
#include <string>
#include "execution/sql/value.h"
#include "execution/util/memory.h"
#include "execution/vm/bytecode_function_info.h"
#include "execution/vm/bytecode_handlers.h"
#include "execution/vm/module.h"
#include "loggers/execution_logger.h"
namespace noisepage::execution::vm {
/**
* An execution frame where all function's local variables and parameters live
* for the duration of the function's lifetime.
*/
class VM::Frame {
friend class VM;
public:
Frame(uint8_t *frame_data, std::size_t frame_size) : frame_data_(frame_data), frame_size_(frame_size) {
NOISEPAGE_ASSERT(frame_data_ != nullptr, "Frame data cannot be null");
NOISEPAGE_ASSERT(frame_size_ >= 0, "Frame size must be >= 0");
(void)frame_size_;
}
void *PtrToLocalAt(const LocalVar local) const {
EnsureInFrame(local);
return frame_data_ + local.GetOffset();
}
/**
* Access the local variable at the given index in the fame. @em index is an encoded LocalVar that
* contains both the byte offset of the variable to load and the access mode, i.e., whether the
* local variable is accessed accessed by address or value.
* @tparam T The type of the variable the user expects.
* @param index The encoded index into the frame where the variable is.
* @return The value of the variable. Note that this is copied!
*/
template <typename T>
T LocalAt(uint32_t index) const { // NOLINT (clang tidy doesn't like const unsigned long instantiation)
LocalVar local = LocalVar::Decode(index);
const auto val = reinterpret_cast<uintptr_t>(PtrToLocalAt(local));
if (local.GetAddressMode() == LocalVar::AddressMode::Value) {
return *reinterpret_cast<T *>(val);
}
return (T)(val); // NOLINT (both static/reinterpret cast semantics)
}
private:
#ifndef NDEBUG
// Ensure the local variable is valid
void EnsureInFrame(LocalVar var) const {
if (var.GetOffset() >= frame_size_) {
std::string error_msg =
fmt::format("Accessing local at offset {}, beyond frame of size {}", var.GetOffset(), frame_size_);
EXECUTION_LOG_ERROR("{}", error_msg);
throw std::runtime_error(error_msg);
}
}
#else
void EnsureInFrame(UNUSED_ATTRIBUTE LocalVar var) const {}
#endif
private:
uint8_t *frame_data_;
std::size_t frame_size_;
};
// ---------------------------------------------------------
// Virtual Machine
// ---------------------------------------------------------
// The maximum amount of stack to use. If the function requires more than 16K
// bytes, acquire space from the heap.
static constexpr const uint32_t MAX_STACK_ALLOC_SIZE = 1ull << 14ull;
// A soft-maximum amount of stack to use. If a function's frame requires more
// than 4K (the soft max), try the stack and fallback to heap. If the function
// requires less, use the stack.
static constexpr const uint32_t SOFT_MAX_STACK_ALLOC_SIZE = 1ull << 12ull;
VM::VM(const Module *module) : module_(module) {}
// static
void VM::InvokeFunction(const Module *module, const FunctionId func_id, const uint8_t args[]) {
// The function's info
const FunctionInfo *func_info = module->GetFuncInfoById(func_id);
NOISEPAGE_ASSERT(func_info != nullptr, "Function doesn't exist in module!");
const std::size_t frame_size = func_info->GetFrameSize();
// Let's try to get some space
bool used_heap = false;
uint8_t *raw_frame = nullptr;
if (frame_size > MAX_STACK_ALLOC_SIZE) {
used_heap = true;
raw_frame = static_cast<uint8_t *>(util::Memory::MallocAligned(frame_size, alignof(uint64_t)));
} else if (frame_size > SOFT_MAX_STACK_ALLOC_SIZE) {
// TODO(pmenon): Check stack before allocation
raw_frame = static_cast<uint8_t *>(alloca(frame_size));
} else {
raw_frame = static_cast<uint8_t *>(alloca(frame_size));
}
// Copy args into frame
std::memcpy(raw_frame + func_info->GetParamsStartPos(), args, func_info->GetParamsSize());
// Let's go!
VM vm(module);
Frame frame(raw_frame, frame_size);
vm.Interpret(module->GetBytecodeModule()->AccessBytecodeForFunctionRaw(*func_info), &frame);
// Done. Now, let's cleanup.
if (used_heap) {
std::free(raw_frame);
}
}
namespace {
template <typename T>
inline ALWAYS_INLINE T Read(const uint8_t **ip) {
static_assert(std::is_arithmetic_v<T>,
"Read() should only be used to read primitive arithmetic types "
"directly from the bytecode instruction stream");
auto ret = *reinterpret_cast<const T *>(*ip);
(*ip) += sizeof(T);
return ret;
}
template <typename T>
inline ALWAYS_INLINE T Peek(const uint8_t **ip) {
static_assert(std::is_integral_v<T>,
"Peek() should only be used to read primitive arithmetic types "
"directly from the bytecode instruction stream");
return *reinterpret_cast<const T *>(*ip);
}
} // namespace
void VM::Interpret(const uint8_t *ip, Frame *frame) { // NOLINT
static void *kDispatchTable[] = {
#define ENTRY(name, ...) &&op_##name,
BYTECODE_LIST(ENTRY)
#undef ENTRY
};
#ifdef TPL_DEBUG_TRACE_INSTRUCTIONS
#define DEBUG_TRACE_INSTRUCTIONS(op) \
do { \
auto bytecode = Bytecodes::FromByte(op); \
bytecode_counts_[op]++; \
EXECUTION_LOG_DEBUG("{0:p}: {1:s}", ip - sizeof(std::underlying_type_t<Bytecode>), Bytecodes::ToString(bytecode)); \
} while (false)
#else
#define DEBUG_TRACE_INSTRUCTIONS(op) (void)op
#endif
// TODO(pmenon): Should these READ/PEEK macros take in a vm::OperandType so
// that we can infer primitive types using traits? This minimizes number of
// changes if the underlying offset/bytecode/register sizes changes?
#define PEEK_JMP_OFFSET() Peek<int32_t>(&ip) /* NOLINT */
#define READ_IMM1() Read<int8_t>(&ip) /* NOLINT */
#define READ_IMM2() Read<int16_t>(&ip) /* NOLINT */
#define READ_IMM4() Read<int32_t>(&ip) /* NOLINT */
#define READ_IMM8() Read<int64_t>(&ip) /* NOLINT */
#define READ_IMM4F() Read<float>(&ip) /* NOLINT */
#define READ_IMM8F() Read<double>(&ip) /* NOLINT */
#define READ_UIMM2() Read<uint16_t>(&ip) /* NOLINT */
#define READ_UIMM4() Read<uint32_t>(&ip) /* NOLINT */
#define READ_JMP_OFFSET() READ_IMM4() /* NOLINT */
#define READ_LOCAL_ID() Read<uint32_t>(&ip) /* NOLINT */
#define READ_STATIC_LOCAL_ID() Read<uint32_t>(&ip) /* NOLINT */
#define READ_OP() Read<std::underlying_type_t<Bytecode>>(&ip) /* NOLINT */
#define READ_FUNC_ID() READ_UIMM2() /* NOLINT */
#define OP(name) op_##name
#define DISPATCH_NEXT() \
do { \
auto op = READ_OP(); \
DEBUG_TRACE_INSTRUCTIONS(op); \
goto *kDispatchTable[op]; \
} while (false)
/*****************************************************************************
*
* Below this comment begins the primary section of TPL's register-based
* virtual machine (VM) dispatch area. The VM uses indirect threaded
* interpretation; each bytecode handler's label is statically generated and
* stored in @ref kDispatchTable at server compile time. Bytecode handler
* logic is written as a case using the CASE_OP macro. Handlers can read from
* and write to registers using the local execution frame's register file
* (i.e., through @ref Frame::LocalAt()).
*
* Upon entry, the instruction pointer (IP) points to the first bytecode of
* function that is running. The READ_* macros can be used to directly read
* values from the bytecode stream. The READ_* macros read values from the
* bytecode stream and advance the IP whereas the PEEK_* macros do only the
* former, leaving the IP unmodified.
*
* IMPORTANT:
* ----------
* Bytecode handler code here should only be simple register/IP manipulation
* (i.e., reading from and writing to registers). Actual full-blown bytecode
* logic must be implemented externally and invoked from stubs here. This is a
* strict requirement necessary because it makes code generation to LLVM much
* simpler.
*
****************************************************************************/
// Jump to the first instruction
DISPATCH_NEXT();
// -------------------------------------------------------
// Primitive comparison operations
// -------------------------------------------------------
#define DO_GEN_COMPARISON(op, type) \
OP(op##_##type) : { \
auto *dest = frame->LocalAt<bool *>(READ_LOCAL_ID()); \
auto lhs = frame->LocalAt<type>(READ_LOCAL_ID()); \
auto rhs = frame->LocalAt<type>(READ_LOCAL_ID()); \
Op##op##_##type(dest, lhs, rhs); \
DISPATCH_NEXT(); \
}
#define GEN_COMPARISON_TYPES(type, ...) \
DO_GEN_COMPARISON(GreaterThan, type) \
DO_GEN_COMPARISON(GreaterThanEqual, type) \
DO_GEN_COMPARISON(Equal, type) \
DO_GEN_COMPARISON(LessThan, type) \
DO_GEN_COMPARISON(LessThanEqual, type) \
DO_GEN_COMPARISON(NotEqual, type)
ALL_TYPES(GEN_COMPARISON_TYPES)
#undef GEN_COMPARISON_TYPES
#undef DO_GEN_COMPARISON
// -------------------------------------------------------
// Primitive arithmetic
// -------------------------------------------------------
#define DO_GEN_ARITHMETIC_OP(op, test, type) \
OP(op##_##type) : { \
auto *dest = frame->LocalAt<type *>(READ_LOCAL_ID()); \
auto lhs = frame->LocalAt<type>(READ_LOCAL_ID()); \
auto rhs = frame->LocalAt<type>(READ_LOCAL_ID()); \
if ((test) && rhs == 0u) { \
/* TODO(pmenon): Proper error */ \
EXECUTION_LOG_ERROR("Division by zero error!"); \
} \
Op##op##_##type(dest, lhs, rhs); \
DISPATCH_NEXT(); \
}
#define GEN_ARITHMETIC_OP(type, ...) \
DO_GEN_ARITHMETIC_OP(Add, false, type) \
DO_GEN_ARITHMETIC_OP(Sub, false, type) \
DO_GEN_ARITHMETIC_OP(Mul, false, type) \
DO_GEN_ARITHMETIC_OP(Div, true, type) \
DO_GEN_ARITHMETIC_OP(Mod, true, type)
ALL_NUMERIC_TYPES(GEN_ARITHMETIC_OP)
#undef GEN_ARITHMETIC_OP
#undef DO_GEN_ARITHMETIC_OP
// -------------------------------------------------------
// Arithmetic negation
// -------------------------------------------------------
#define GEN_NEG_OP(type, ...) \
OP(Neg##_##type) : { \
auto *dest = frame->LocalAt<type *>(READ_LOCAL_ID()); \
auto input = frame->LocalAt<type>(READ_LOCAL_ID()); \
OpNeg##_##type(dest, input); \
DISPATCH_NEXT(); \
}
ALL_NUMERIC_TYPES(GEN_NEG_OP)
#undef GEN_NEG_OP
// -------------------------------------------------------
// Bitwise operations
// -------------------------------------------------------
#define DO_GEN_BIT_OP(op, type) \
OP(op##_##type) : { \
auto *dest = frame->LocalAt<type *>(READ_LOCAL_ID()); \
auto lhs = frame->LocalAt<type>(READ_LOCAL_ID()); \
auto rhs = frame->LocalAt<type>(READ_LOCAL_ID()); \
Op##op##_##type(dest, lhs, rhs); \
DISPATCH_NEXT(); \
}
#define DO_GEN_NEG_OP(type, ...) \
OP(BitNeg##_##type) : { \
auto *dest = frame->LocalAt<type *>(READ_LOCAL_ID()); \
auto input = frame->LocalAt<type>(READ_LOCAL_ID()); \
OpBitNeg##_##type(dest, input); \
DISPATCH_NEXT(); \
}
#define GEN_BIT_OP(type, ...) \
DO_GEN_BIT_OP(BitAnd, type) \
DO_GEN_BIT_OP(BitOr, type) \
DO_GEN_BIT_OP(BitXor, type) \
DO_GEN_NEG_OP(type)
INT_TYPES(GEN_BIT_OP)
#undef GEN_BIT_OP
#undef GEN_NEG_OP
#undef DO_GEN_BIT_OP
OP(Not) : {
auto *dest = frame->LocalAt<bool *>(READ_LOCAL_ID());
auto input = frame->LocalAt<bool>(READ_LOCAL_ID());
OpNot(dest, input);
DISPATCH_NEXT();
}
OP(NotSql) : {
auto *dest = frame->LocalAt<sql::BoolVal *>(READ_LOCAL_ID());
auto *input = frame->LocalAt<sql::BoolVal *>(READ_LOCAL_ID());
OpNotSql(dest, input);
DISPATCH_NEXT();
}
// -------------------------------------------------------
// Jumps
// -------------------------------------------------------
OP(Jump) : {
auto skip = PEEK_JMP_OFFSET();
if (LIKELY(OpJump())) {
ip += skip;
}
DISPATCH_NEXT();
}
OP(JumpIfTrue) : {
auto cond = frame->LocalAt<bool>(READ_LOCAL_ID());
auto skip = PEEK_JMP_OFFSET();
if (OpJumpIfTrue(cond)) {
ip += skip;
} else {
READ_JMP_OFFSET();
}
DISPATCH_NEXT();
}
OP(JumpIfFalse) : {
auto cond = frame->LocalAt<bool>(READ_LOCAL_ID());
auto skip = PEEK_JMP_OFFSET();
if (OpJumpIfFalse(cond)) {
ip += skip;
} else {
READ_JMP_OFFSET();
}
DISPATCH_NEXT();
}
// -------------------------------------------------------
// Low-level memory operations
// -------------------------------------------------------
OP(IsNullPtr) : {
auto *result = frame->LocalAt<bool *>(READ_LOCAL_ID());
auto *input_ptr = frame->LocalAt<const void *>(READ_LOCAL_ID());
OpIsNullPtr(result, input_ptr);
DISPATCH_NEXT();
}
OP(IsNotNullPtr) : {
auto *result = frame->LocalAt<bool *>(READ_LOCAL_ID());
auto *input_ptr = frame->LocalAt<const void *>(READ_LOCAL_ID());
OpIsNotNullPtr(result, input_ptr);
DISPATCH_NEXT();
}
#define GEN_DEREF(type, size) \
OP(Deref##size) : { \
auto *dest = frame->LocalAt<type *>(READ_LOCAL_ID()); \
auto *src = frame->LocalAt<type *>(READ_LOCAL_ID()); \
OpDeref##size(dest, src); \
DISPATCH_NEXT(); \
}
GEN_DEREF(int8_t, 1);
GEN_DEREF(int16_t, 2);
GEN_DEREF(int32_t, 4);
GEN_DEREF(int64_t, 8);
#undef GEN_DEREF
OP(DerefN) : {
auto *dest = frame->LocalAt<byte *>(READ_LOCAL_ID());
auto *src = frame->LocalAt<byte *>(READ_LOCAL_ID());
auto len = READ_UIMM4();
OpDerefN(dest, src, len);
DISPATCH_NEXT();
}
#define GEN_ASSIGN(type, size) \
OP(Assign##size) : { \
auto *dest = frame->LocalAt<type *>(READ_LOCAL_ID()); \
auto src = frame->LocalAt<type>(READ_LOCAL_ID()); \
OpAssign##size(dest, src); \
DISPATCH_NEXT(); \
} \
OP(AssignImm##size) : { \
auto *dest = frame->LocalAt<type *>(READ_LOCAL_ID()); \
OpAssignImm##size(dest, READ_IMM##size()); \
DISPATCH_NEXT(); \
}
GEN_ASSIGN(int8_t, 1);
GEN_ASSIGN(int16_t, 2);
GEN_ASSIGN(int32_t, 4);
GEN_ASSIGN(int64_t, 8);
#undef GEN_ASSIGN
OP(AssignImm4F) : {
auto *dest = frame->LocalAt<float *>(READ_LOCAL_ID());
OpAssignImm4F(dest, READ_IMM4F());
DISPATCH_NEXT();
}
OP(AssignImm8F) : {
auto *dest = frame->LocalAt<double *>(READ_LOCAL_ID());
OpAssignImm8F(dest, READ_IMM8F());
DISPATCH_NEXT();
}
OP(Lea) : {
auto **dest = frame->LocalAt<byte **>(READ_LOCAL_ID());
auto *src = frame->LocalAt<byte *>(READ_LOCAL_ID());
auto offset = READ_UIMM4();
OpLea(dest, src, offset);
DISPATCH_NEXT();
}
OP(LeaScaled) : {
auto **dest = frame->LocalAt<byte **>(READ_LOCAL_ID());
auto *src = frame->LocalAt<byte *>(READ_LOCAL_ID());
auto index = frame->LocalAt<uint32_t>(READ_LOCAL_ID());
auto scale = READ_UIMM4();
auto offset = READ_UIMM4();
OpLeaScaled(dest, src, index, scale, offset);
DISPATCH_NEXT();
}
OP(Call) : {
ip = ExecuteCall(ip, frame);
DISPATCH_NEXT();
}
OP(Return) : {
OpReturn();
return;
}
// -------------------------------------------------------
// Execution Context
// -------------------------------------------------------
OP(ExecutionContextAddRowsAffected) : {
auto *exec_ctx = frame->LocalAt<exec::ExecutionContext *>(READ_LOCAL_ID());
auto rows_affected = frame->LocalAt<int32_t>(READ_LOCAL_ID());
OpExecutionContextAddRowsAffected(exec_ctx, rows_affected);
DISPATCH_NEXT();
}
OP(ExecutionContextGetMemoryPool) : {
auto *memory_pool = frame->LocalAt<sql::MemoryPool **>(READ_LOCAL_ID());
auto *exec_ctx = frame->LocalAt<exec::ExecutionContext *>(READ_LOCAL_ID());
OpExecutionContextGetMemoryPool(memory_pool, exec_ctx);
DISPATCH_NEXT();
}
OP(ExecutionContextRegisterHook) : {
auto *exec_ctx = frame->LocalAt<exec::ExecutionContext *>(READ_LOCAL_ID());
auto idx = frame->LocalAt<uint32_t>(READ_LOCAL_ID());
auto fn_id = READ_FUNC_ID();
auto fn = reinterpret_cast<exec::ExecutionContext::HookFn>(module_->GetRawFunctionImpl(fn_id));
OpExecutionContextRegisterHook(exec_ctx, idx, fn);
DISPATCH_NEXT();
}
OP(ExecutionContextClearHooks) : {
auto *exec_ctx = frame->LocalAt<exec::ExecutionContext *>(READ_LOCAL_ID());
OpExecutionContextClearHooks(exec_ctx);
DISPATCH_NEXT();
}
OP(ExecutionContextInitHooks) : {
auto *exec_ctx = frame->LocalAt<exec::ExecutionContext *>(READ_LOCAL_ID());
auto size = frame->LocalAt<uint32_t>(READ_LOCAL_ID());
OpExecutionContextInitHooks(exec_ctx, size);
DISPATCH_NEXT();
}
OP(ExecutionContextGetTLS) : {
auto *thread_state_container = frame->LocalAt<sql::ThreadStateContainer **>(READ_LOCAL_ID());
auto *exec_ctx = frame->LocalAt<exec::ExecutionContext *>(READ_LOCAL_ID());
OpExecutionContextGetTLS(thread_state_container, exec_ctx);
DISPATCH_NEXT();
}
OP(ExecutionContextStartResourceTracker) : {
auto *exec_ctx = frame->LocalAt<exec::ExecutionContext *>(READ_LOCAL_ID());
auto metrics_component = static_cast<metrics::MetricsComponent>(frame->LocalAt<uint64_t>(READ_LOCAL_ID()));
OpExecutionContextStartResourceTracker(exec_ctx, metrics_component);
DISPATCH_NEXT();
}
OP(ExecutionContextSetMemoryUseOverride) : {
auto *exec_ctx = frame->LocalAt<exec::ExecutionContext *>(READ_LOCAL_ID());
auto size = frame->LocalAt<uint32_t>(READ_LOCAL_ID());
OpExecutionContextSetMemoryUseOverride(exec_ctx, size);
DISPATCH_NEXT();
}
OP(ExecutionContextEndResourceTracker) : {
auto *exec_ctx = frame->LocalAt<exec::ExecutionContext *>(READ_LOCAL_ID());
auto *name = frame->LocalAt<sql::StringVal *>(READ_LOCAL_ID());
OpExecutionContextEndResourceTracker(exec_ctx, *name);
DISPATCH_NEXT();
}
OP(ExecutionContextStartPipelineTracker) : {
auto *exec_ctx = frame->LocalAt<exec::ExecutionContext *>(READ_LOCAL_ID());
auto pipeline_id = execution::pipeline_id_t{frame->LocalAt<uint32_t>(READ_LOCAL_ID())};
OpExecutionContextStartPipelineTracker(exec_ctx, pipeline_id);
DISPATCH_NEXT();
}
OP(ExecutionContextEndPipelineTracker) : {
auto *exec_ctx = frame->LocalAt<exec::ExecutionContext *>(READ_LOCAL_ID());
auto query_id = execution::query_id_t{frame->LocalAt<uint32_t>(READ_LOCAL_ID())};
auto pipeline_id = execution::pipeline_id_t{frame->LocalAt<uint32_t>(READ_LOCAL_ID())};
auto *ouvec = frame->LocalAt<selfdriving::ExecOUFeatureVector *>(READ_LOCAL_ID());
OpExecutionContextEndPipelineTracker(exec_ctx, query_id, pipeline_id, ouvec);
DISPATCH_NEXT();
}
OP(ExecOUFeatureVectorRecordFeature) : {
auto *ouvec = frame->LocalAt<selfdriving::ExecOUFeatureVector *>(READ_LOCAL_ID());
auto pipeline_id = execution::pipeline_id_t{frame->LocalAt<uint32_t>(READ_LOCAL_ID())};
auto feature_id = execution::feature_id_t{frame->LocalAt<uint32_t>(READ_LOCAL_ID())};
auto feature_attribute =
static_cast<selfdriving::ExecutionOperatingUnitFeatureAttribute>(frame->LocalAt<uint32_t>(READ_LOCAL_ID()));
auto mode =
static_cast<selfdriving::ExecutionOperatingUnitFeatureUpdateMode>(frame->LocalAt<uint32_t>(READ_LOCAL_ID()));
auto value = frame->LocalAt<uint32_t>(READ_LOCAL_ID());
OpExecOUFeatureVectorRecordFeature(ouvec, pipeline_id, feature_id, feature_attribute, mode, value);
DISPATCH_NEXT();
}
OP(ExecOUFeatureVectorInitialize) : {
auto *exec_ctx = frame->LocalAt<exec::ExecutionContext *>(READ_LOCAL_ID());
auto *ouvec = frame->LocalAt<selfdriving::ExecOUFeatureVector *>(READ_LOCAL_ID());
auto pipeline_id = execution::pipeline_id_t{frame->LocalAt<uint32_t>(READ_LOCAL_ID())};
auto is_parallel = frame->LocalAt<bool>(READ_LOCAL_ID());
OpExecOUFeatureVectorInitialize(exec_ctx, ouvec, pipeline_id, is_parallel);
DISPATCH_NEXT();
}
OP(ExecOUFeatureVectorFilter) : {
auto *ouvec = frame->LocalAt<selfdriving::ExecOUFeatureVector *>(READ_LOCAL_ID());
auto type = static_cast<selfdriving::ExecutionOperatingUnitType>(frame->LocalAt<uint32_t>(READ_LOCAL_ID()));
OpExecOUFeatureVectorFilter(ouvec, type);
DISPATCH_NEXT();
}
OP(ExecOUFeatureVectorReset) : {
auto *ouvec = frame->LocalAt<selfdriving::ExecOUFeatureVector *>(READ_LOCAL_ID());
OpExecOUFeatureVectorReset(ouvec);
DISPATCH_NEXT();
}
OP(RegisterThreadWithMetricsManager) : {
auto *exec_ctx = frame->LocalAt<exec::ExecutionContext *>(READ_LOCAL_ID());
OpRegisterThreadWithMetricsManager(exec_ctx);
DISPATCH_NEXT();
}
OP(EnsureTrackersStopped) : {
auto *exec_ctx = frame->LocalAt<exec::ExecutionContext *>(READ_LOCAL_ID());
OpEnsureTrackersStopped(exec_ctx);
DISPATCH_NEXT();
}
OP(AggregateMetricsThread) : {
auto *exec_ctx = frame->LocalAt<exec::ExecutionContext *>(READ_LOCAL_ID());
OpAggregateMetricsThread(exec_ctx);
DISPATCH_NEXT();
}
OP(ThreadStateContainerAccessCurrentThreadState) : {
auto *state = frame->LocalAt<byte **>(READ_LOCAL_ID());
auto *thread_state_container = frame->LocalAt<sql::ThreadStateContainer *>(READ_LOCAL_ID());
OpThreadStateContainerAccessCurrentThreadState(state, thread_state_container);
DISPATCH_NEXT();
}
OP(ThreadStateContainerIterate) : {
auto *thread_state_container = frame->LocalAt<sql::ThreadStateContainer *>(READ_LOCAL_ID());
auto ctx = frame->LocalAt<void *>(READ_LOCAL_ID());
auto iterate_fn_id = READ_FUNC_ID();
auto iterate_fn =
reinterpret_cast<sql::ThreadStateContainer::IterateFn>(module_->GetRawFunctionImpl(iterate_fn_id));
OpThreadStateContainerIterate(thread_state_container, ctx, iterate_fn);
DISPATCH_NEXT();
}
OP(ThreadStateContainerReset) : {
auto *thread_state_container = frame->LocalAt<sql::ThreadStateContainer *>(READ_LOCAL_ID());
auto size = frame->LocalAt<uint32_t>(READ_LOCAL_ID());
auto init_fn_id = READ_FUNC_ID();
auto destroy_fn_id = READ_FUNC_ID();
auto *ctx = frame->LocalAt<void *>(READ_LOCAL_ID());
auto init_fn = reinterpret_cast<sql::ThreadStateContainer::InitFn>(module_->GetRawFunctionImpl(init_fn_id));
auto destroy_fn =
reinterpret_cast<sql::ThreadStateContainer::DestroyFn>(module_->GetRawFunctionImpl(destroy_fn_id));
OpThreadStateContainerReset(thread_state_container, size, init_fn, destroy_fn, ctx);
DISPATCH_NEXT();
}
OP(ThreadStateContainerClear) : {
auto *thread_state_container = frame->LocalAt<sql::ThreadStateContainer *>(READ_LOCAL_ID());
OpThreadStateContainerClear(thread_state_container);
DISPATCH_NEXT();
}
// -------------------------------------------------------
// Table Vector and Vector Projection Iterator (VPI) ops
// -------------------------------------------------------
OP(TableVectorIteratorInit) : {
auto *iter = frame->LocalAt<sql::TableVectorIterator *>(READ_LOCAL_ID());
auto exec_ctx = frame->LocalAt<exec::ExecutionContext *>(READ_LOCAL_ID());
auto table_oid = frame->LocalAt<uint32_t>(READ_LOCAL_ID());
auto col_oids = frame->LocalAt<uint32_t *>(READ_LOCAL_ID());
auto num_oids = READ_UIMM4();
OpTableVectorIteratorInit(iter, exec_ctx, table_oid, col_oids, num_oids);
DISPATCH_NEXT();
}
OP(TableVectorIteratorPerformInit) : {
auto *iter = frame->LocalAt<sql::TableVectorIterator *>(READ_LOCAL_ID());
OpTableVectorIteratorPerformInit(iter);
DISPATCH_NEXT();
}
OP(TableVectorIteratorNext) : {
auto *has_more = frame->LocalAt<bool *>(READ_LOCAL_ID());
auto *iter = frame->LocalAt<sql::TableVectorIterator *>(READ_LOCAL_ID());
OpTableVectorIteratorNext(has_more, iter);
DISPATCH_NEXT();
}
OP(TableVectorIteratorFree) : {
auto *iter = frame->LocalAt<sql::TableVectorIterator *>(READ_LOCAL_ID());
OpTableVectorIteratorFree(iter);
DISPATCH_NEXT();
}
OP(TableVectorIteratorGetVPINumTuples) : {
auto *num_tuples_vpi = frame->LocalAt<uint32_t *>(READ_LOCAL_ID());
auto *iter = frame->LocalAt<sql::TableVectorIterator *>(READ_LOCAL_ID());
OpTableVectorIteratorGetVPINumTuples(num_tuples_vpi, iter);
DISPATCH_NEXT();
}
OP(TableVectorIteratorGetVPI) : {
auto *vpi = frame->LocalAt<sql::VectorProjectionIterator **>(READ_LOCAL_ID());
auto *iter = frame->LocalAt<sql::TableVectorIterator *>(READ_LOCAL_ID());
OpTableVectorIteratorGetVPI(vpi, iter);
DISPATCH_NEXT();
}
OP(ParallelScanTable) : {
auto table_oid = frame->LocalAt<uint32_t>(READ_LOCAL_ID());
auto col_oids = frame->LocalAt<uint32_t *>(READ_LOCAL_ID());
auto num_oids = READ_UIMM4();
auto query_state = frame->LocalAt<void *>(READ_LOCAL_ID());
auto *exec_context = frame->LocalAt<exec::ExecutionContext *>(READ_LOCAL_ID());
auto scan_fn_id = READ_FUNC_ID();
auto scan_fn = reinterpret_cast<sql::TableVectorIterator::ScanFn>(module_->GetRawFunctionImpl(scan_fn_id));
OpParallelScanTable(table_oid, col_oids, num_oids, query_state, exec_context, scan_fn);
DISPATCH_NEXT();
}
// -------------------------------------------------------
// VPI iteration operations
// -------------------------------------------------------
OP(VPIInit) : {
auto *iter = frame->LocalAt<sql::VectorProjectionIterator *>(READ_LOCAL_ID());
auto *vector_projection = frame->LocalAt<sql::VectorProjection *>(READ_LOCAL_ID());
OpVPIInit(iter, vector_projection);
DISPATCH_NEXT();
}
OP(VPIInitWithList) : {
auto *iter = frame->LocalAt<sql::VectorProjectionIterator *>(READ_LOCAL_ID());
auto *vector_projection = frame->LocalAt<sql::VectorProjection *>(READ_LOCAL_ID());
auto *tid_list = frame->LocalAt<sql::TupleIdList *>(READ_LOCAL_ID());
OpVPIInitWithList(iter, vector_projection, tid_list);
DISPATCH_NEXT();
}
OP(VPIIsFiltered) : {
auto *is_filtered = frame->LocalAt<bool *>(READ_LOCAL_ID());
auto *iter = frame->LocalAt<sql::VectorProjectionIterator *>(READ_LOCAL_ID());
OpVPIIsFiltered(is_filtered, iter);
DISPATCH_NEXT();
}
OP(VPIGetSelectedRowCount) : {
auto *count = frame->LocalAt<uint32_t *>(READ_LOCAL_ID());
auto *iter = frame->LocalAt<sql::VectorProjectionIterator *>(READ_LOCAL_ID());
OpVPIGetSelectedRowCount(count, iter);
DISPATCH_NEXT();
}
OP(VPIGetVectorProjection) : {
auto *vector_projection = frame->LocalAt<sql::VectorProjection **>(READ_LOCAL_ID());
auto *iter = frame->LocalAt<sql::VectorProjectionIterator *>(READ_LOCAL_ID());
OpVPIGetVectorProjection(vector_projection, iter);
DISPATCH_NEXT();
}
OP(VPIHasNext) : {
auto *has_more = frame->LocalAt<bool *>(READ_LOCAL_ID());
auto *iter = frame->LocalAt<sql::VectorProjectionIterator *>(READ_LOCAL_ID());
OpVPIHasNext(has_more, iter);
DISPATCH_NEXT();
}
OP(VPIHasNextFiltered) : {
auto *has_more = frame->LocalAt<bool *>(READ_LOCAL_ID());
auto *iter = frame->LocalAt<sql::VectorProjectionIterator *>(READ_LOCAL_ID());
OpVPIHasNextFiltered(has_more, iter);
DISPATCH_NEXT();
}
OP(VPIAdvance) : {
auto *iter = frame->LocalAt<sql::VectorProjectionIterator *>(READ_LOCAL_ID());
OpVPIAdvance(iter);
DISPATCH_NEXT();
}
OP(VPIAdvanceFiltered) : {
auto *iter = frame->LocalAt<sql::VectorProjectionIterator *>(READ_LOCAL_ID());
OpVPIAdvanceFiltered(iter);
DISPATCH_NEXT();
}
OP(VPISetPosition) : {
auto *iter = frame->LocalAt<sql::VectorProjectionIterator *>(READ_LOCAL_ID());
auto index = frame->LocalAt<uint32_t>(READ_LOCAL_ID());
OpVPISetPosition(iter, index);
DISPATCH_NEXT();
}
OP(VPISetPositionFiltered) : {
auto *iter = frame->LocalAt<sql::VectorProjectionIterator *>(READ_LOCAL_ID());
auto index = frame->LocalAt<uint32_t>(READ_LOCAL_ID());
OpVPISetPositionFiltered(iter, index);
DISPATCH_NEXT();
}
OP(VPIMatch) : {
auto *iter = frame->LocalAt<sql::VectorProjectionIterator *>(READ_LOCAL_ID());
auto match = frame->LocalAt<bool>(READ_LOCAL_ID());
OpVPIMatch(iter, match);
DISPATCH_NEXT();
}
OP(VPIReset) : {
auto *iter = frame->LocalAt<sql::VectorProjectionIterator *>(READ_LOCAL_ID());
OpVPIReset(iter);
DISPATCH_NEXT();
}
OP(VPIResetFiltered) : {
auto *iter = frame->LocalAt<sql::VectorProjectionIterator *>(READ_LOCAL_ID());
OpVPIResetFiltered(iter);
DISPATCH_NEXT();
}
OP(VPIFree) : {
auto *iter = frame->LocalAt<sql::VectorProjectionIterator *>(READ_LOCAL_ID());
OpVPIFree(iter);
DISPATCH_NEXT();
}
OP(VPIGetSlot) : {
auto *slot = frame->LocalAt<storage::TupleSlot *>(READ_LOCAL_ID());
auto *vpi = frame->LocalAt<sql::VectorProjectionIterator *>(READ_LOCAL_ID());
OpVPIGetSlot(slot, vpi);
DISPATCH_NEXT();
}
// -------------------------------------------------------
// VPI element access
// -------------------------------------------------------
#define GEN_VPI_ACCESS(NAME, CPP_TYPE) \
OP(VPIGet##NAME) : { \
auto *result = frame->LocalAt<CPP_TYPE *>(READ_LOCAL_ID()); \
auto *vpi = frame->LocalAt<sql::VectorProjectionIterator *>(READ_LOCAL_ID()); \
auto col_idx = READ_UIMM4(); \
OpVPIGet##NAME(result, vpi, col_idx); \
DISPATCH_NEXT(); \
} \
OP(VPIGet##NAME##Null) : { \
auto *result = frame->LocalAt<CPP_TYPE *>(READ_LOCAL_ID()); \
auto *vpi = frame->LocalAt<sql::VectorProjectionIterator *>(READ_LOCAL_ID()); \
auto col_idx = READ_UIMM4(); \
OpVPIGet##NAME##Null(result, vpi, col_idx); \
DISPATCH_NEXT(); \
} \
OP(VPISet##NAME) : { \
auto *vpi = frame->LocalAt<sql::VectorProjectionIterator *>(READ_LOCAL_ID()); \
auto *input = frame->LocalAt<CPP_TYPE *>(READ_LOCAL_ID()); \
auto col_idx = READ_UIMM4(); \
OpVPISet##NAME(vpi, input, col_idx); \
DISPATCH_NEXT(); \
} \
OP(VPISet##NAME##Null) : { \
auto *vpi = frame->LocalAt<sql::VectorProjectionIterator *>(READ_LOCAL_ID()); \
auto *input = frame->LocalAt<CPP_TYPE *>(READ_LOCAL_ID()); \
auto col_idx = READ_UIMM4(); \
OpVPISet##NAME##Null(vpi, input, col_idx); \
DISPATCH_NEXT(); \
}
GEN_VPI_ACCESS(Bool, sql::BoolVal)
GEN_VPI_ACCESS(TinyInt, sql::Integer)
GEN_VPI_ACCESS(SmallInt, sql::Integer)
GEN_VPI_ACCESS(Integer, sql::Integer)
GEN_VPI_ACCESS(BigInt, sql::Integer)
GEN_VPI_ACCESS(Real, sql::Real)
GEN_VPI_ACCESS(Double, sql::Real)
GEN_VPI_ACCESS(Decimal, sql::DecimalVal)
GEN_VPI_ACCESS(Date, sql::DateVal)
GEN_VPI_ACCESS(Timestamp, sql::TimestampVal)
GEN_VPI_ACCESS(String, sql::StringVal)
#undef GEN_VPI_ACCESS
OP(VPIGetPointer) : {
auto result = frame->LocalAt<byte **>(READ_LOCAL_ID());
auto vpi = frame->LocalAt<sql::VectorProjectionIterator *>(READ_LOCAL_ID());
auto col_idx = READ_UIMM4();
OpVPIGetPointer(result, vpi, col_idx);
DISPATCH_NEXT();
}
// ------------------------------------------------------
// Hashing
// ------------------------------------------------------
#define GEN_HASH(NAME, CPP_TYPE) \
OP(Hash##NAME) : { \
auto *hash_val = frame->LocalAt<hash_t *>(READ_LOCAL_ID()); \
auto *input = frame->LocalAt<CPP_TYPE *>(READ_LOCAL_ID()); \
auto seed = frame->LocalAt<const hash_t>(READ_LOCAL_ID()); \
OpHash##NAME(hash_val, input, seed); \
DISPATCH_NEXT(); \
}
GEN_HASH(Int, sql::Integer)
GEN_HASH(Bool, sql::BoolVal)
GEN_HASH(Real, sql::Real)
GEN_HASH(Date, sql::DateVal)
GEN_HASH(Timestamp, sql::TimestampVal)
GEN_HASH(String, sql::StringVal)
#undef GEN_HASH
OP(HashCombine) : {
auto *hash_val = frame->LocalAt<hash_t *>(READ_LOCAL_ID());
auto new_hash_val = frame->LocalAt<hash_t>(READ_LOCAL_ID());
OpHashCombine(hash_val, new_hash_val);
DISPATCH_NEXT();
}
// ------------------------------------------------------
// Filter Manager
// ------------------------------------------------------
OP(FilterManagerInit) : {
auto *filter_manager = frame->LocalAt<sql::FilterManager *>(READ_LOCAL_ID());
auto *exec_context = frame->LocalAt<exec::ExecutionContext *>(READ_LOCAL_ID());
OpFilterManagerInit(filter_manager, exec_context->GetExecutionSettings());
DISPATCH_NEXT();
}
OP(FilterManagerStartNewClause) : {
auto *filter_manager = frame->LocalAt<sql::FilterManager *>(READ_LOCAL_ID());
OpFilterManagerStartNewClause(filter_manager);
DISPATCH_NEXT();
}
OP(FilterManagerInsertFilter) : {
auto *filter_manager = frame->LocalAt<sql::FilterManager *>(READ_LOCAL_ID());
auto func_id = READ_FUNC_ID();
auto fn = reinterpret_cast<sql::FilterManager::MatchFn>(module_->GetRawFunctionImpl(func_id));
OpFilterManagerInsertFilter(filter_manager, fn);
DISPATCH_NEXT();
}
OP(FilterManagerRunFilters) : {
auto *filter_manager = frame->LocalAt<sql::FilterManager *>(READ_LOCAL_ID());
auto *vpi = frame->LocalAt<sql::VectorProjectionIterator *>(READ_LOCAL_ID());
auto *exec_ctx = frame->LocalAt<exec::ExecutionContext *>(READ_LOCAL_ID());
OpFilterManagerRunFilters(filter_manager, vpi, exec_ctx);
DISPATCH_NEXT();
}
OP(FilterManagerFree) : {
auto *filter_manager = frame->LocalAt<sql::FilterManager *>(READ_LOCAL_ID());
OpFilterManagerFree(filter_manager);
DISPATCH_NEXT();
}
// ------------------------------------------------------
// Vector Filter Executor
// ------------------------------------------------------
#define GEN_VEC_FILTER(BYTECODE) \
OP(BYTECODE) : { \
auto *exec_ctx = frame->LocalAt<exec::ExecutionContext *>(READ_LOCAL_ID()); \
auto *vector_projection = frame->LocalAt<sql::VectorProjection *>(READ_LOCAL_ID()); \
auto left_col_idx = frame->LocalAt<uint32_t>(READ_LOCAL_ID()); \
auto right_col_idx = frame->LocalAt<uint32_t>(READ_LOCAL_ID()); \
auto *tid_list = frame->LocalAt<sql::TupleIdList *>(READ_LOCAL_ID()); \
Op##BYTECODE(exec_ctx->GetExecutionSettings(), vector_projection, left_col_idx, right_col_idx, tid_list); \
DISPATCH_NEXT(); \
} \
OP(BYTECODE##Val) : { \
auto *exec_ctx = frame->LocalAt<exec::ExecutionContext *>(READ_LOCAL_ID()); \
auto *vector_projection = frame->LocalAt<sql::VectorProjection *>(READ_LOCAL_ID()); \
auto left_col_idx = frame->LocalAt<uint32_t>(READ_LOCAL_ID()); \
auto right_val = frame->LocalAt<sql::Val *>(READ_LOCAL_ID()); \
auto *tid_list = frame->LocalAt<sql::TupleIdList *>(READ_LOCAL_ID()); \
Op##BYTECODE##Val(exec_ctx->GetExecutionSettings(), vector_projection, left_col_idx, right_val, tid_list); \
DISPATCH_NEXT(); \
}
GEN_VEC_FILTER(VectorFilterEqual)
GEN_VEC_FILTER(VectorFilterGreaterThan)
GEN_VEC_FILTER(VectorFilterGreaterThanEqual)
GEN_VEC_FILTER(VectorFilterLessThan)
GEN_VEC_FILTER(VectorFilterLessThanEqual)
GEN_VEC_FILTER(VectorFilterNotEqual)
GEN_VEC_FILTER(VectorFilterLike)
GEN_VEC_FILTER(VectorFilterNotLike)
#undef GEN_VEC_FILTER
// -------------------------------------------------------
// SQL Value Creation.
// -------------------------------------------------------
OP(ForceBoolTruth) : {
auto *result = frame->LocalAt<bool *>(READ_LOCAL_ID());
auto *sql_bool = frame->LocalAt<sql::BoolVal *>(READ_LOCAL_ID());
OpForceBoolTruth(result, sql_bool);
DISPATCH_NEXT();
}
OP(InitSqlNull) : {
auto *sql_null = frame->LocalAt<sql::Val *>(READ_LOCAL_ID());
OpInitSqlNull(sql_null);
DISPATCH_NEXT();
}
OP(InitBool) : {
auto *sql_bool = frame->LocalAt<sql::BoolVal *>(READ_LOCAL_ID());
auto val = frame->LocalAt<bool>(READ_LOCAL_ID());
OpInitBool(sql_bool, val);
DISPATCH_NEXT();
}
OP(InitInteger) : {
auto *sql_int = frame->LocalAt<sql::Integer *>(READ_LOCAL_ID());
auto val = frame->LocalAt<int32_t>(READ_LOCAL_ID());
OpInitInteger(sql_int, val);
DISPATCH_NEXT();
}
OP(InitInteger64) : {
auto *sql_int = frame->LocalAt<sql::Integer *>(READ_LOCAL_ID());
auto val = frame->LocalAt<int64_t>(READ_LOCAL_ID());
OpInitInteger64(sql_int, val);
DISPATCH_NEXT();
}
OP(InitReal) : {