forked from heterodb/pg-strom
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcodegen.c
4981 lines (4519 loc) · 130 KB
/
codegen.c
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
/*
* codegen.c
*
* Routines for xPU code generator
* ----
* Copyright 2011-2023 (C) KaiGai Kohei <[email protected]>
* Copyright 2014-2023 (C) PG-Strom Developers Team
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the PostgreSQL License.
*/
#include "pg_strom.h"
/* -------- static variables --------*/
#define DEVTYPE_INFO_NSLOTS 128
#define DEVFUNC_INFO_NSLOTS 1024
static MemoryContext devinfo_memcxt = NULL;
static volatile int devinfo_memcxt_generation = 0;
static List *devtype_info_slot[DEVTYPE_INFO_NSLOTS];
static List *devfunc_info_slot[DEVFUNC_INFO_NSLOTS];
static HTAB *devtype_rev_htable = NULL; /* lookup by TypeOpCode */
static HTAB *devfunc_rev_htable = NULL; /* lookup by FuncOpCode */
/* -------- static declarations -------- */
#define TYPE_OPCODE(NAME,EXTENSION,FLAGS) \
static uint32_t devtype_##NAME##_hash(bool isnull, Datum value);
#include "xpu_opcodes.h"
#define TYPE_OPCODE(NAME,EXTENSION,FLAGS) \
{ EXTENSION, #NAME, TypeOpCode__##NAME, \
DEVKIND__ANY | (FLAGS), \
devtype_##NAME##_hash, \
sizeof(xpu_##NAME##_t), \
__alignof__(xpu_##NAME##_t), \
sizeof(kvec_##NAME##_t) },
static struct {
const char *type_extension;
const char *type_name;
TypeOpCode type_code;
uint32_t type_flags;
devtype_hashfunc_f type_hashfunc;
int type_sizeof;
int type_alignof;
int kvec_sizeof;
} devtype_catalog[] = {
#include "xpu_opcodes.h"
/* alias device data types */
{NULL, NULL, TypeOpCode__Invalid, 0, NULL, 0, 0}
};
static struct {
const char *type_name;
const char *type_extension;
const char *base_name;
const char *base_extension;
} devtype_alias_catalog[] = {
#define TYPE_ALIAS(NAME,EXTENSION,BASE,BASE_EXTENSION) \
{#NAME, EXTENSION, #BASE, BASE_EXTENSION},
#include "xpu_opcodes.h"
{NULL,NULL,NULL,NULL}
};
static void __appendKernExpMagicAndLength(StringInfo buf, int head_pos);
static int codegen_expression_walker(codegen_context *context,
StringInfo buf,
int curr_depth,
Expr *expr);
static const char *
get_extension_name_by_object(Oid class_id, Oid object_id)
{
Oid ext_oid = getExtensionOfObject(class_id, object_id);
if (OidIsValid(ext_oid))
return get_extension_name(ext_oid);
return NULL;
}
/*
* type oid cache
*/
static Oid __type_oid_cache_int1 = UINT_MAX;
Oid
get_int1_type_oid(bool missing_ok)
{
if (__type_oid_cache_int1 == UINT_MAX)
{
const char *ext_name;
Oid type_oid;
type_oid = GetSysCacheOid2(TYPENAMENSP,
Anum_pg_type_oid,
CStringGetDatum("int1"),
ObjectIdGetDatum(PG_CATALOG_NAMESPACE));
if (OidIsValid(type_oid))
{
ext_name = get_extension_name_by_object(TypeRelationId, type_oid);
if (!ext_name || strcmp(ext_name, "pg_strom") != 0)
type_oid = InvalidOid;
}
__type_oid_cache_int1 = type_oid;
}
if (!missing_ok && !OidIsValid(__type_oid_cache_int1))
elog(ERROR, "type 'int1' is not installed");
return __type_oid_cache_int1;
}
static Oid __type_oid_cache_float2 = UINT_MAX;
Oid
get_float2_type_oid(bool missing_ok)
{
if (__type_oid_cache_float2 == UINT_MAX)
{
const char *ext_name;
Oid type_oid;
type_oid = GetSysCacheOid2(TYPENAMENSP,
Anum_pg_type_oid,
CStringGetDatum("float2"),
ObjectIdGetDatum(PG_CATALOG_NAMESPACE));
if (OidIsValid(type_oid))
{
ext_name = get_extension_name_by_object(TypeRelationId, type_oid);
if (!ext_name || strcmp(ext_name, "pg_strom") != 0)
type_oid = InvalidOid;
}
__type_oid_cache_float2 = type_oid;
}
if (!missing_ok && !OidIsValid(__type_oid_cache_float2))
elog(ERROR, "type 'float2' is not installed");
return __type_oid_cache_float2;
}
static Oid __type_oid_cache_cube = UINT_MAX;
Oid
get_cube_type_oid(bool missing_ok)
{
if (__type_oid_cache_cube == UINT_MAX)
{
Oid type_oid = InvalidOid;
CatCList *typelist;
typelist = SearchSysCacheList1(TYPENAMENSP,
CStringGetDatum("cube"));
for (int i=0; i < typelist->n_members; i++)
{
HeapTuple type_htup = &typelist->members[i]->tuple;
Form_pg_type type_form = (Form_pg_type)GETSTRUCT(type_htup);
const char *ext_name;
ext_name = get_extension_name_by_object(TypeRelationId,
type_form->oid);
if (ext_name && strcmp(ext_name, "cube") == 0)
{
type_oid = type_form->oid;
break;
}
}
ReleaseCatCacheList(typelist);
__type_oid_cache_cube = type_oid;
}
if (!missing_ok && !OidIsValid(__type_oid_cache_cube))
elog(ERROR, "type 'cube' is not installed");
return __type_oid_cache_cube;
}
/*
* build_basic_devtype_info
*/
static devtype_info *
build_basic_devtype_info(TypeCacheEntry *tcache, const char *ext_name)
{
devtype_info *dtype = NULL;
HeapTuple htup;
Form_pg_type __type;
char type_name[NAMEDATALEN+1];
Oid type_namespace;
int i;
htup = SearchSysCache1(TYPEOID, ObjectIdGetDatum(tcache->type_id));
if (!HeapTupleIsValid(htup))
elog(ERROR, "cache lookup failed for type %u", tcache->type_id);
__type = (Form_pg_type) GETSTRUCT(htup);
strcpy(type_name, NameStr(__type->typname));
type_namespace = __type->typnamespace;
ReleaseSysCache(htup);
/* built-in types must be in pg_catalog */
if (!ext_name && type_namespace != PG_CATALOG_NAMESPACE)
return NULL;
for (i=0; devtype_catalog[i].type_name != NULL; i++)
{
const char *__ext_name = devtype_catalog[i].type_extension;
const char *__type_name = devtype_catalog[i].type_name;
if ((ext_name
? (__ext_name && strcmp(ext_name, __ext_name) == 0)
: (__ext_name == NULL)) &&
strcmp(type_name, __type_name) == 0)
{
MemoryContext oldcxt = MemoryContextSwitchTo(devinfo_memcxt);
dtype = palloc0(offsetof(devtype_info, comp_subtypes[0]));
if (ext_name)
dtype->type_extension = pstrdup(ext_name);
dtype->type_code = devtype_catalog[i].type_code;
dtype->type_oid = tcache->type_id;
dtype->type_flags = devtype_catalog[i].type_flags;
dtype->type_length = tcache->typlen;
dtype->type_align = typealign_get_width(tcache->typalign);
dtype->type_byval = tcache->typbyval;
dtype->type_name = pstrdup(type_name);
dtype->type_namespace = get_type_namespace(tcache->type_id);
dtype->type_extension = (ext_name ? pstrdup(ext_name) : NULL);
dtype->type_sizeof = devtype_catalog[i].type_sizeof;
dtype->type_alignof = devtype_catalog[i].type_alignof;
dtype->kvec_sizeof = devtype_catalog[i].kvec_sizeof;
dtype->type_hashfunc = devtype_catalog[i].type_hashfunc;
/* type equality functions */
dtype->type_eqfunc = get_opcode(tcache->eq_opr);
dtype->type_cmpfunc = tcache->cmp_proc;
MemoryContextSwitchTo(oldcxt);
return dtype;
}
}
return NULL; /* not found */
}
/*
* build_composite_devtype_info
*/
static devtype_info *
build_composite_devtype_info(TypeCacheEntry *tcache, const char *ext_name)
{
TupleDesc tupdesc = lookup_rowtype_tupdesc(tcache->type_id, -1);
devtype_info **subtypes = alloca(sizeof(devtype_info *) * tupdesc->natts);
devtype_info *dtype;
MemoryContext oldcxt;
uint32_t subtype_flags = DEVKIND__ANY;
for (int j=0; j < tupdesc->natts; j++)
{
Form_pg_attribute attr = TupleDescAttr(tupdesc, j);
dtype = pgstrom_devtype_lookup(attr->atttypid);
if (!dtype)
{
ReleaseTupleDesc(tupdesc);
return NULL;
}
subtype_flags &= dtype->type_flags;
subtypes[j] = dtype;
}
ReleaseTupleDesc(tupdesc);
oldcxt = MemoryContextSwitchTo(devinfo_memcxt);
dtype = palloc0(offsetof(devtype_info,
comp_subtypes[tupdesc->natts]));
if (ext_name)
dtype->type_extension = pstrdup(ext_name);
dtype->type_code = TypeOpCode__composite;
dtype->type_oid = tcache->type_id;
dtype->type_flags = subtype_flags;
dtype->type_length = tcache->typlen;
dtype->type_align = typealign_get_width(tcache->typalign);
dtype->type_byval = tcache->typbyval;
dtype->type_extension = NULL;
dtype->type_name = "composite";
dtype->type_namespace = get_type_namespace(tcache->type_id);
dtype->type_sizeof = sizeof(xpu_composite_t);
dtype->type_alignof = __alignof__(xpu_composite_t);
dtype->kvec_sizeof = sizeof(kvec_composite_t);
dtype->type_hashfunc = NULL; //devtype_composite_hash;
dtype->type_eqfunc = get_opcode(tcache->eq_opr);
dtype->type_cmpfunc = tcache->cmp_proc;
dtype->comp_nfields = tupdesc->natts;
memcpy(dtype->comp_subtypes, subtypes,
sizeof(devtype_info *) * tupdesc->natts);
MemoryContextSwitchTo(oldcxt);
return dtype;
}
/*
* build_array_devtype_info
*/
static devtype_info *
build_array_devtype_info(TypeCacheEntry *tcache, const char *ext_name)
{
devtype_info *elem;
devtype_info *dtype;
MemoryContext oldcxt;
elem = pgstrom_devtype_lookup(tcache->typelem);
if (!elem)
return NULL;
oldcxt = MemoryContextSwitchTo(devinfo_memcxt);
dtype = palloc0(offsetof(devtype_info, comp_subtypes[0]));
if (ext_name)
dtype->type_extension = pstrdup(ext_name);
dtype->type_code = TypeOpCode__array;
dtype->type_oid = tcache->type_id;
dtype->type_flags = elem->type_flags;
dtype->type_length = tcache->typlen;
dtype->type_align = typealign_get_width(tcache->typalign);
dtype->type_byval = tcache->typbyval;
dtype->type_name = "array";
dtype->type_namespace = get_type_namespace(tcache->type_id);
dtype->type_sizeof = sizeof(xpu_array_t);
dtype->type_alignof = __alignof__(xpu_array_t);
dtype->kvec_sizeof = sizeof(kvec_array_t);
dtype->type_hashfunc = NULL; //devtype_array_hash;
dtype->type_element = elem;
/* type equality functions */
dtype->type_eqfunc = get_opcode(tcache->eq_opr);
dtype->type_cmpfunc = tcache->cmp_proc;
MemoryContextSwitchTo(oldcxt);
return dtype;
}
static Oid
get_typeoid_by_name(const char *type_name, const char *type_extension)
{
CatCList *typelist;
Oid type_oid;
typelist = SearchSysCacheList1(TYPENAMENSP,
CStringGetDatum(type_name));
for (int i=0; i < typelist->n_members; i++)
{
HeapTuple tuple = &typelist->members[i]->tuple;
Form_pg_type typeForm = (Form_pg_type) GETSTRUCT(tuple);
const char *ext_name;
type_oid = typeForm->oid;
ext_name = get_extension_name_by_object(TypeRelationId, type_oid);
if (type_extension)
{
if (ext_name && strcmp(type_extension, ext_name) == 0)
goto found;
}
else
{
if (!ext_name && typeForm->typnamespace == PG_CATALOG_NAMESPACE)
goto found;
}
}
/* not found */
type_oid = InvalidOid;
found:
ReleaseCatCacheList(typelist);
return type_oid;
}
static TypeCacheEntry *
__devtype_resolve_alias(TypeCacheEntry *tcache)
{
const char *type_name;
const char *ext_name;
/* check alias list */
type_name = get_type_name(tcache->type_id, false);
ext_name = get_extension_name_by_object(TypeRelationId,
tcache->type_id);
for (int i=0; devtype_alias_catalog[i].type_name != NULL; i++)
{
Oid __type_oid;
const char *__type_name = devtype_alias_catalog[i].type_name;
const char *__ext_name = devtype_alias_catalog[i].type_extension;
if (strcmp(type_name, __type_name) != 0)
continue;
if (__ext_name != NULL)
{
if (!ext_name || strcmp(ext_name, __ext_name) != 0)
continue;
}
else
{
Oid namespace_oid = get_type_namespace(tcache->type_id);
if (namespace_oid != PG_CATALOG_NAMESPACE)
continue;
}
/* Hmm... it looks this type is alias of the base */
__type_oid = get_typeoid_by_name(devtype_alias_catalog[i].base_name,
devtype_alias_catalog[i].base_extension);
if (!OidIsValid(__type_oid))
return NULL;
tcache = lookup_type_cache(__type_oid,
TYPECACHE_EQ_OPR |
TYPECACHE_CMP_PROC);
return __devtype_resolve_alias(tcache);
}
return tcache;
}
devtype_info *
pgstrom_devtype_lookup(Oid type_oid)
{
devtype_info *dtype;
Datum hash;
uint32_t index;
ListCell *lc;
const char *ext_name;
TypeCacheEntry *tcache;
int __generation;
if (!OidIsValid(type_oid))
return NULL; /* InvalidOid should never has device-type */
hash = hash_any((unsigned char *)&type_oid, sizeof(Oid));
index = hash % DEVTYPE_INFO_NSLOTS;
foreach (lc, devtype_info_slot[index])
{
dtype = lfirst(lc);
if (dtype->type_oid == type_oid)
{
Assert(dtype->hash == hash);
goto found;
}
}
/*
* Not found, try to build devtype_info entry
*/
do {
__generation = devinfo_memcxt_generation;
ext_name = get_extension_name_by_object(TypeRelationId, type_oid);
tcache = lookup_type_cache(type_oid,
TYPECACHE_EQ_OPR |
TYPECACHE_CMP_PROC);
/* if domain, move to the base type */
while (tcache->nextDomain)
tcache = tcache->nextDomain;
/* if aliased device type, resolve this one */
tcache = __devtype_resolve_alias(tcache);
if (OidIsValid(tcache->typelem) && tcache->typlen == -1)
{
/* array type */
dtype = build_array_devtype_info(tcache, ext_name);
}
else if (tcache->typtype == TYPTYPE_COMPOSITE)
{
/* composite type */
if (!OidIsValid(tcache->typrelid))
elog(ERROR, "Bug? wrong composite definition at %s",
format_type_be(type_oid));
dtype = build_composite_devtype_info(tcache, ext_name);
}
else if (tcache->typtype == TYPTYPE_BASE ||
tcache->typtype == TYPTYPE_RANGE)
{
/* base or range type */
dtype = build_basic_devtype_info(tcache, ext_name);
}
else
{
/* not a supported type */
dtype = NULL;
}
/* make a negative entry, if not device executable */
if (!dtype)
{
dtype = MemoryContextAllocZero(devinfo_memcxt,
sizeof(devtype_info));
dtype->type_is_negative = true;
}
dtype->type_oid = type_oid;
dtype->hash = hash;
} while (__generation != devinfo_memcxt_generation);
devtype_info_slot[index] = lappend_cxt(devinfo_memcxt,
devtype_info_slot[index], dtype);
found:
return (dtype->type_is_negative ? NULL : dtype);
}
/*
* devtype_get_name_by_opcode
*/
typedef struct {
TypeOpCode type_code;
const char *type_name;
} devtype_reverse_entry;
static const char *
devtype_get_name_by_opcode(TypeOpCode type_code)
{
devtype_reverse_entry *entry;
bool found;
if (!devtype_rev_htable)
{
HASHCTL hctl;
memset(&hctl, 0, sizeof(HASHCTL));
hctl.keysize = sizeof(TypeOpCode);
hctl.entrysize = sizeof(devtype_reverse_entry);
hctl.hcxt = devinfo_memcxt;
devtype_rev_htable = hash_create("devtype_rev_htable",
128,
&hctl,
HASH_ELEM | HASH_BLOBS | HASH_CONTEXT);
}
entry = (devtype_reverse_entry *)
hash_search(devtype_rev_htable, &type_code, HASH_ENTER, &found);
if (!found)
{
switch (type_code)
{
case TypeOpCode__composite:
entry->type_name = "composite";
break;
case TypeOpCode__array:
entry->type_name = "array";
break;
case TypeOpCode__internal:
entry->type_name = "internal";
break;
default:
entry->type_name = NULL;
for (int i=0; devtype_catalog[i].type_name != NULL; i++)
{
if (devtype_catalog[i].type_code == type_code)
{
entry->type_name = devtype_catalog[i].type_name;
break;
}
}
break;
}
}
if (!entry->type_name)
elog(ERROR, "device type opcode:%u not found", type_code);
return entry->type_name;
}
/*
* devtype_get_kvec_sizeof_by_opcode
*/
static uint32_t
devtype_get_kvec_sizeof_by_opcode(TypeOpCode type_code)
{
switch (type_code)
{
case TypeOpCode__composite:
return sizeof(kvec_composite_t);
case TypeOpCode__array:
return sizeof(kvec_array_t);
case TypeOpCode__internal:
return sizeof(kvec_internal_t);
default:
for (int i=0; devtype_catalog[i].type_name != NULL; i++)
{
if (devtype_catalog[i].type_code == type_code)
return devtype_catalog[i].kvec_sizeof;
}
break;
}
elog(ERROR, "device type opcode:%u not found", type_code);
}
/*
* Built-in device type hash functions
*/
static uint32_t
devtype_bool_hash(bool isnull, Datum value)
{
bool bval;
if (isnull)
return 0;
bval = DatumGetBool(value) ? true : false;
return hash_any((unsigned char *)&bval, sizeof(bool));
}
static inline uint32_t
__devtype_simple_hash(bool isnull, Datum value, int sz)
{
if (isnull)
return 0;
return hash_any((unsigned char *)&value, sz);
}
static uint32_t
devtype_int1_hash(bool isnull, Datum value)
{
return __devtype_simple_hash(isnull, value, sizeof(int8_t));
}
static uint32_t
devtype_int2_hash(bool isnull, Datum value)
{
return __devtype_simple_hash(isnull, value, sizeof(int16_t));
}
static uint32_t
devtype_int4_hash(bool isnull, Datum value)
{
return __devtype_simple_hash(isnull, value, sizeof(int32_t));
}
static uint32_t
devtype_int8_hash(bool isnull, Datum value)
{
return __devtype_simple_hash(isnull, value, sizeof(int64_t));
}
static uint32_t
devtype_float2_hash(bool isnull, Datum value)
{
return __devtype_simple_hash(isnull, value, sizeof(float2_t));
}
static uint32_t
devtype_float4_hash(bool isnull, Datum value)
{
return __devtype_simple_hash(isnull, value, sizeof(float4_t));
}
static uint32_t
devtype_float8_hash(bool isnull, Datum value)
{
return __devtype_simple_hash(isnull, value, sizeof(float8_t));
}
static uint32_t
devtype_numeric_hash(bool isnull, Datum value)
{
uint32_t len;
if (isnull)
return 0;
len = VARSIZE_ANY_EXHDR(value);
if (len >= sizeof(uint16_t))
{
NumericChoice *nc = (NumericChoice *)VARDATA_ANY(value);
NumericDigit *digits = NUMERIC_DIGITS(nc, nc->n_header);
int weight = NUMERIC_WEIGHT(nc, nc->n_header) + 1;
int i, ndigits = NUMERIC_NDIGITS(nc->n_header, len);
int128_t value = 0;
for (i=0; i < ndigits; i++)
{
NumericDigit dig = digits[i];
value = value * PG_NBASE + dig;
if (value < 0)
elog(ERROR, "numeric value is out of range");
}
if (NUMERIC_SIGN(nc->n_header) == NUMERIC_NEG)
value = -value;
weight = PG_DEC_DIGITS * (ndigits - weight);
/* see, set_normalized_numeric */
if (value == 0)
weight = 0;
else
{
while (value % 10 == 0)
{
value /= 10;
weight--;
}
}
return (hash_any((unsigned char *)&weight, sizeof(int16_t)) ^
hash_any((unsigned char *)&value, sizeof(int128_t)));
}
elog(ERROR, "corrupted numeric header");
}
static uint32_t
devtype_bytea_hash(bool isnull, Datum value)
{
if (isnull)
return 0;
return hash_any((unsigned char *)VARDATA_ANY(value), VARSIZE_ANY_EXHDR(value));
}
static uint32_t
devtype_text_hash(bool isnull, Datum value)
{
if (isnull)
return 0;
return hash_any((unsigned char *)VARDATA_ANY(value), VARSIZE_ANY_EXHDR(value));
}
static uint32_t
devtype_bpchar_hash(bool isnull, Datum value)
{
if (!isnull)
{
char *s = VARDATA_ANY(value);
int sz = VARSIZE_ANY_EXHDR(value);
sz = bpchartruelen(s, sz);
return hash_any((unsigned char *)s, sz);
}
return 0;
}
static uint32_t
devtype_date_hash(bool isnull, Datum value)
{
return __devtype_simple_hash(isnull, value, sizeof(DateADT));
}
static uint32_t
devtype_time_hash(bool isnull, Datum value)
{
return __devtype_simple_hash(isnull, value, sizeof(TimeADT));
}
static uint32_t
devtype_timetz_hash(bool isnull, Datum value)
{
if (!isnull)
{
TimeTzADT *tmtz = DatumGetTimeTzADTP(value);
return (hash_any((unsigned char *)&tmtz->time, sizeof(TimeADT)) ^
hash_any((unsigned char *)&tmtz->zone, sizeof(int32_t)));
}
return 0;
}
static uint32_t
devtype_timestamp_hash(bool isnull, Datum value)
{
return __devtype_simple_hash(isnull, value, sizeof(Timestamp));
}
static uint32_t
devtype_timestamptz_hash(bool isnull, Datum value)
{
return __devtype_simple_hash(isnull, value, sizeof(TimestampTz));
}
static uint32_t
devtype_interval_hash(bool isnull, Datum value)
{
if (!isnull)
{
Interval *iv = DatumGetIntervalP(value);
return hash_any((unsigned char *)iv, sizeof(Interval));
}
return 0;
}
static uint32_t
devtype_money_hash(bool isnull, Datum value)
{
return __devtype_simple_hash(isnull, value, sizeof(int64_t));
}
static uint32_t
devtype_uuid_hash(bool isnull, Datum value)
{
if (!isnull)
{
pg_uuid_t *uuid = DatumGetUUIDP(value);
return hash_any(uuid->data, UUID_LEN);
}
return 0;
}
static uint32_t
devtype_macaddr_hash(bool isnull, Datum value)
{
if (!isnull)
{
macaddr *maddr = DatumGetMacaddrP(value);
return hash_any((unsigned char *)maddr, sizeof(macaddr));
}
return 0;
}
static uint32_t
devtype_inet_hash(bool isnull, Datum value)
{
if (!isnull)
{
inet *in = DatumGetInetP(value);
int sz;
if (in->inet_data.family == PGSQL_AF_INET)
sz = offsetof(inet_struct, ipaddr[4]);
else if (in->inet_data.family == PGSQL_AF_INET6)
sz = offsetof(inet_struct, ipaddr[16]);
else
elog(ERROR, "corrupted inet data");
return hash_any((unsigned char *)&in->inet_data, sz);
}
return 0;
}
static uint32_t
__devtype_jsonb_hash(JsonbContainer *jc)
{
uint32_t hash = 0;
uint32_t nitems = JsonContainerSize(jc);
char *base = NULL;
char *data;
uint32_t datalen;
if (JsonContainerIsObject(jc))
{
base = (char *)(jc->children + 2 * nitems);
hash ^= JB_FOBJECT;
}
else
{
base = (char *)(jc->children + nitems);
hash ^= JB_FARRAY;
}
for (int j=0; j < nitems; j++)
{
uint32_t index = j;
uint32_t temp;
JEntry entry;
/* hash value for key */
if (JsonContainerIsObject(jc))
{
entry = jc->children[index];
if (!JBE_ISSTRING(entry))
elog(ERROR, "jsonb key value is not STRING");
data = base + getJsonbOffset(jc, index);
datalen = getJsonbLength(jc, index);
temp = hash_any((unsigned char *)data, datalen);
hash = ((hash << 1) | (hash >> 31)) ^ temp;
index += nitems;
}
/* hash value for element */
entry = jc->children[index];
if (JBE_ISNULL(entry))
temp = 0x01;
else if (JBE_ISSTRING(entry))
{
data = base + getJsonbOffset(jc, index);
datalen = getJsonbLength(jc, index);
temp = hash_any((unsigned char *)data, datalen);
}
else if (JBE_ISNUMERIC(entry))
{
data = base + INTALIGN(getJsonbOffset(jc, index));
temp = devtype_numeric_hash(false, PointerGetDatum(data));
}
else if (JBE_ISBOOL_TRUE(entry))
temp = 0x02;
else if (JBE_ISBOOL_FALSE(entry))
temp = 0x04;
else if (JBE_ISCONTAINER(entry))
{
data = base + INTALIGN(getJsonbOffset(jc, index));
temp = __devtype_jsonb_hash((JsonbContainer *)data);
}
else
elog(ERROR, "Unexpected jsonb entry (%08x)", entry);
hash = ((hash << 1) | (hash >> 31)) ^ temp;
}
return hash;
}
static uint32_t
devtype_jsonb_hash(bool isnull, Datum value)
{
if (!isnull)
{
JsonbContainer *jc = (JsonbContainer *) VARDATA_ANY(value);
return __devtype_jsonb_hash(jc);
}
return 0;
}
static uint32_t
devtype_geometry_hash(bool isnull, Datum value)
{
elog(ERROR, "geometry type has no device hash function");
}
static uint32_t
devtype_box2df_hash(bool isnull, Datum value)
{
elog(ERROR, "box2df type has no device hash function");
}
static uint32_t
devtype_cube_hash(bool isnull, Datum value)
{
if (isnull)
return 0;
return hash_any((unsigned char *)VARDATA_ANY(value), VARSIZE_ANY_EXHDR(value));
}
/*
* Built-in device functions/operators
*/
#define FUNC_OPCODE(SQLNAME,FN_ARGS,FN_FLAGS,DEVNAME,FUNC_COST,EXTENSION) \
{ #SQLNAME, #FN_ARGS, FN_FLAGS, FuncOpCode__##DEVNAME, FUNC_COST, EXTENSION },
#define FUNC_ALIAS(SQLNAME,FN_ARGS,FN_FLAGS,DEVNAME,FUNC_COST,EXTENSION) \
{ #SQLNAME, #FN_ARGS, FN_FLAGS, FuncOpCode__##DEVNAME, FUNC_COST, EXTENSION },
static struct {
const char *func_name;
const char *func_args;
uint32_t func_flags;
FuncOpCode func_code;
int func_cost;
const char *func_extension;
} devfunc_catalog[] = {
#include "xpu_opcodes.h"
{NULL,NULL,0,FuncOpCode__Invalid,0,NULL}
};
static devfunc_info *
pgstrom_devfunc_build(Oid func_oid, int func_nargs, Oid *func_argtypes)
{
const char *fextension;
const char *fname;
Oid fnamespace;
Oid frettype;
StringInfoData buf;
devfunc_info *dfunc = NULL;
devtype_info *dtype_rettype;
devtype_info **dtype_argtypes;
MemoryContext oldcxt;
int i, j, sz;
initStringInfo(&buf);
fname = get_func_name(func_oid);
if (!fname)
elog(ERROR, "cache lookup failed on procedure '%u'", func_oid);
fnamespace = get_func_namespace(func_oid);
frettype = get_func_rettype(func_oid);
dtype_rettype = pgstrom_devtype_lookup(frettype);
if (!dtype_rettype)
goto bailout;
dtype_argtypes = alloca(sizeof(devtype_info *) * func_nargs);
for (j=0; j < func_nargs; j++)
{
dtype_argtypes[j] = pgstrom_devtype_lookup(func_argtypes[j]);
if (!dtype_argtypes[j])
goto bailout;
}
/* we expect built-in functions are in pg_catalog namespace */
fextension = get_extension_name_by_object(ProcedureRelationId, func_oid);
if (!fextension && fnamespace != PG_CATALOG_NAMESPACE)
goto bailout;
for (i=0; devfunc_catalog[i].func_name != NULL; i++)
{
const char *__extension = devfunc_catalog[i].func_extension;
const char *__name = devfunc_catalog[i].func_name;
char *tok, *saveptr;
if (fextension != NULL
? (__extension == NULL || strcmp(fextension, __extension) != 0)
: (__extension != NULL))
continue;
if (strcmp(fname, __name) != 0)
continue;
resetStringInfo(&buf);
appendStringInfoString(&buf, devfunc_catalog[i].func_args);
for (tok = strtok_r(buf.data, "/", &saveptr), j=0;
tok != NULL && j < func_nargs;
tok = strtok_r(NULL, "/", &saveptr), j++)
{
devtype_info *dtype = dtype_argtypes[j];
tok = __trim(tok);
sz = strlen(tok);
if (sz > 4 &&
tok[0] == '_' && tok[1] == '_' &&
tok[sz-1] == '_' && tok[sz-2] == '_')
{
/* __TYPE__ means variable length argument! */
tok[sz-1] = '\0';
if (strcmp(tok+2, dtype->type_name) != 0)
break;
/* must be the last argument set */
tok = strtok_r(NULL, "/", &saveptr);
if (tok)
break;
/* check whether the following arguments are identical */