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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
|
// ir-legalize-types.cpp
// This file implements type legalization for the IR.
// It uses the core legalization logic in
// `legalize-types.{h,cpp}` to decide what to do with
// the types, while this file handles the actual
// rewriting of the IR to use the new types.
//
// This pass should only be applied to IR that has been
// fully specialized (no more generics/interfaces), so
// that the concrete type of everything is known.
#include "ir.h"
#include "ir-insts.h"
#include "legalize-types.h"
#include "mangle.h"
namespace Slang
{
LegalVal LegalVal::tuple(RefPtr<TuplePseudoVal> tupleVal)
{
LegalVal result;
result.flavor = LegalVal::Flavor::tuple;
result.obj = tupleVal;
return result;
}
LegalVal LegalVal::pair(RefPtr<PairPseudoVal> pairInfo)
{
LegalVal result;
result.flavor = LegalVal::Flavor::pair;
result.obj = pairInfo;
return result;
}
LegalVal LegalVal::pair(
LegalVal const& ordinaryVal,
LegalVal const& specialVal,
RefPtr<PairInfo> pairInfo)
{
if (ordinaryVal.flavor == LegalVal::Flavor::none)
return specialVal;
if (specialVal.flavor == LegalVal::Flavor::none)
return ordinaryVal;
RefPtr<PairPseudoVal> obj = new PairPseudoVal();
obj->ordinaryVal = ordinaryVal;
obj->specialVal = specialVal;
obj->pairInfo = pairInfo;
return LegalVal::pair(obj);
}
LegalVal LegalVal::implicitDeref(LegalVal const& val)
{
RefPtr<ImplicitDerefVal> implicitDerefVal = new ImplicitDerefVal();
implicitDerefVal->val = val;
LegalVal result;
result.flavor = LegalVal::Flavor::implicitDeref;
result.obj = implicitDerefVal;
return result;
}
LegalVal LegalVal::getImplicitDeref()
{
assert(flavor == Flavor::implicitDeref);
return obj.As<ImplicitDerefVal>()->val;
}
struct IRTypeLegalizationContext
{
Session* session;
IRModule* module;
IRBuilder* builder;
/// Context to use for underlying (non-IR) type legalization.
TypeLegalizationContext* typeLegalizationContext;
// When inserting new globals, put them before this one.
IRGlobalValue* insertBeforeGlobal = nullptr;
// When inserting new parameters, put them before this one.
IRParam* insertBeforeParam = nullptr;
Dictionary<IRValue*, LegalVal> mapValToLegalVal;
IRVar* insertBeforeLocalVar = nullptr;
// store local var instructions that have been replaced here, so we can free them
// when legalization has done
List<IRInst*> oldLocalVars;
};
static void registerLegalizedValue(
IRTypeLegalizationContext* context,
IRValue* irValue,
LegalVal const& legalVal)
{
context->mapValToLegalVal.Add(irValue, legalVal);
}
static void maybeRegisterLegalizedGlobal(
IRTypeLegalizationContext* context,
IRGlobalValue* irGlobalVar,
LegalVal const& legalVal)
{
// Check the mangled name of the symbol and don't register
// symbols that don't have an external name (currently
// indicated by them having an empty name string).
String mangledName = irGlobalVar->mangledName;
if (mangledName.Length() == 0)
return;
// Otherwise, register the legalized value for this symbol
// under its mangled name, so that other code can still
// find the right value(s) to use after legalization.
context->typeLegalizationContext->mapMangledNameToLegalIRValue.AddIfNotExists(mangledName, legalVal);
}
struct IRGlobalNameInfo
{
IRGlobalValue* globalVar;
UInt counter;
};
static LegalVal declareVars(
IRTypeLegalizationContext* context,
IROp op,
LegalType type,
TypeLayout* typeLayout,
LegalVarChain* varChain,
IRGlobalNameInfo* globalNameInfo);
static LegalType legalizeType(
IRTypeLegalizationContext* context,
Type* type)
{
return legalizeType(context->typeLegalizationContext, type);
}
// Legalize a type, and then expect it to
// result in a simple type.
static RefPtr<Type> legalizeSimpleType(
IRTypeLegalizationContext* context,
Type* type)
{
auto legalType = legalizeType(context, type);
switch (legalType.flavor)
{
case LegalType::Flavor::simple:
return legalType.getSimple();
default:
// TODO: need to issue a diagnostic here.
SLANG_UNEXPECTED("unexpected type case");
break;
}
}
// Take a value that is being used as an operand,
// and turn it into the equivalent legalized value.
static LegalVal legalizeOperand(
IRTypeLegalizationContext* context,
IRValue* irValue)
{
LegalVal legalVal;
if (context->mapValToLegalVal.TryGetValue(irValue, legalVal))
return legalVal;
// For now, assume that anything not covered
// by the mapping is legal as-is.
return LegalVal::simple(irValue);
}
static void getArgumentValues(
List<IRValue*> & instArgs,
LegalVal val)
{
switch (val.flavor)
{
case LegalVal::Flavor::none:
break;
case LegalVal::Flavor::simple:
instArgs.Add(val.getSimple());
break;
case LegalVal::Flavor::implicitDeref:
getArgumentValues(instArgs, val.getImplicitDeref());
break;
case LegalVal::Flavor::pair:
{
auto pairVal = val.getPair();
getArgumentValues(instArgs, pairVal->ordinaryVal);
getArgumentValues(instArgs, pairVal->specialVal);
}
break;
case LegalVal::Flavor::tuple:
{
auto tuplePsuedoVal = val.getTuple();
for (auto elem : val.getTuple()->elements)
{
getArgumentValues(instArgs, elem.val);
}
}
break;
default:
SLANG_UNEXPECTED("uhandled val flavor");
break;
}
}
static LegalVal legalizeCall(
IRTypeLegalizationContext* context,
IRCall* callInst)
{
// TODO: implement legalization of non-simple return types
auto retType = legalizeType(context, callInst->type);
SLANG_ASSERT(retType.flavor == LegalType::Flavor::simple);
List<IRValue*> instArgs;
for (auto i = 1u; i < callInst->argCount; i++)
getArgumentValues(instArgs, legalizeOperand(context, callInst->getArg(i)));
return LegalVal::simple(context->builder->emitCallInst(
callInst->type,
callInst->func.get(),
instArgs.Count(),
instArgs.Buffer()));
}
static LegalVal legalizeLoad(
IRTypeLegalizationContext* context,
LegalVal legalPtrVal)
{
switch (legalPtrVal.flavor)
{
case LegalVal::Flavor::none:
return LegalVal();
case LegalVal::Flavor::simple:
{
return LegalVal::simple(
context->builder->emitLoad(legalPtrVal.getSimple()));
}
break;
case LegalVal::Flavor::implicitDeref:
// We have turne a pointer(-like) type into its pointed-to (value)
// type, and so the operation of loading goes away; we just use
// the underlying value.
return legalPtrVal.getImplicitDeref();
case LegalVal::Flavor::pair:
{
auto ptrPairVal = legalPtrVal.getPair();
auto ordinaryVal = legalizeLoad(context, ptrPairVal->ordinaryVal);
auto specialVal = legalizeLoad(context, ptrPairVal->specialVal);
return LegalVal::pair(ordinaryVal, specialVal, ptrPairVal->pairInfo);
}
case LegalVal::Flavor::tuple:
{
// We need to emit a load for each element of
// the tuple.
auto ptrTupleVal = legalPtrVal.getTuple();
RefPtr<TuplePseudoVal> tupleVal = new TuplePseudoVal();
for (auto ee : legalPtrVal.getTuple()->elements)
{
TuplePseudoVal::Element element;
element.mangledName = ee.mangledName;
element.val = legalizeLoad(context, ee.val);
tupleVal->elements.Add(element);
}
return LegalVal::tuple(tupleVal);
}
break;
default:
SLANG_UNEXPECTED("unhandled case");
break;
}
}
static LegalVal legalizeStore(
IRTypeLegalizationContext* context,
LegalVal legalPtrVal,
LegalVal legalVal)
{
switch (legalPtrVal.flavor)
{
case LegalVal::Flavor::none:
return LegalVal();
case LegalVal::Flavor::simple:
{
context->builder->emitStore(legalPtrVal.getSimple(), legalVal.getSimple());
return legalVal;
}
break;
case LegalVal::Flavor::implicitDeref:
// TODO: what is the right behavior here?
if (legalVal.flavor == LegalVal::Flavor::implicitDeref)
return legalizeStore(context, legalPtrVal.getImplicitDeref(), legalVal.getImplicitDeref());
else
return legalizeStore(context, legalPtrVal.getImplicitDeref(), legalVal);
case LegalVal::Flavor::pair:
{
auto destPair = legalPtrVal.getPair();
auto valPair = legalVal.getPair();
legalizeStore(context, destPair->ordinaryVal, valPair->ordinaryVal);
legalizeStore(context, destPair->specialVal, valPair->specialVal);
return LegalVal();
}
case LegalVal::Flavor::tuple:
{
// We need to emit a store for each element of
// the tuple.
auto destTuple = legalPtrVal.getTuple();
auto valTuple = legalVal.getTuple();
SLANG_ASSERT(destTuple->elements.Count() == valTuple->elements.Count());
for (UInt i = 0; i < valTuple->elements.Count(); i++)
{
legalizeStore(context, destTuple->elements[i].val, valTuple->elements[i].val);
}
return legalVal;
}
break;
default:
SLANG_UNEXPECTED("unhandled case");
break;
}
}
static LegalVal legalizeFieldAddress(
IRTypeLegalizationContext* context,
LegalType type,
LegalVal legalPtrOperand,
DeclRef<Decl> fieldDeclRef)
{
auto builder = context->builder;
switch (legalPtrOperand.flavor)
{
case LegalVal::Flavor::simple:
return LegalVal::simple(
builder->emitFieldAddress(
type.getSimple(),
legalPtrOperand.getSimple(),
builder->getDeclRefVal(fieldDeclRef)));
case LegalVal::Flavor::pair:
{
String mangledFieldName = getMangledName(fieldDeclRef.getDecl());
// There are two sides, the ordinary and the special,
// and we basically just dispatch to both of them.
auto pairVal = legalPtrOperand.getPair();
auto pairInfo = pairVal->pairInfo;
auto pairElement = pairInfo->findElement(mangledFieldName);
if (!pairElement)
{
SLANG_UNEXPECTED("didn't find tuple element");
UNREACHABLE_RETURN(LegalVal());
}
// If the field we are extracting has a pair type,
// that means it exists on both the ordinary and
// special sides.
RefPtr<PairInfo> fieldPairInfo;
LegalType ordinaryType = type;
LegalType specialType = type;
if (type.flavor == LegalType::Flavor::pair)
{
auto fieldPairType = type.getPair();
fieldPairInfo = fieldPairType->pairInfo;
ordinaryType = fieldPairType->ordinaryType;
specialType = fieldPairType->specialType;
}
LegalVal ordinaryVal;
LegalVal specialVal;
if (pairElement->flags & PairInfo::kFlag_hasOrdinary)
{
// Note: the ordinary side of the pair is expected
// to be a filtered `struct` type, and so it will
// have different field declarations than the
// oridinal type. The element of the `PairInfo`
// structure stores the correct field decl-ref to use
// as `ordinaryFieldDeclRef`.
ordinaryVal = legalizeFieldAddress(
context,
ordinaryType,
pairVal->ordinaryVal,
pairElement->ordinaryFieldDeclRef);
}
if (pairElement->flags & PairInfo::kFlag_hasSpecial)
{
specialVal = legalizeFieldAddress(
context,
specialType,
pairVal->specialVal,
fieldDeclRef);
}
return LegalVal::pair(ordinaryVal, specialVal, fieldPairInfo);
}
break;
case LegalVal::Flavor::tuple:
{
String mangledFieldName = getMangledName(fieldDeclRef.getDecl());
// The operand is a tuple of pointer-like
// values, we want to extract the element
// corresponding to a field. We will handle
// this by simply returning the corresponding
// element from the operand.
auto ptrTupleInfo = legalPtrOperand.getTuple();
for (auto ee : ptrTupleInfo->elements)
{
if (ee.mangledName == mangledFieldName)
{
return ee.val;
}
}
// TODO: we can legally reach this case now
// when the field is "ordinary".
SLANG_UNEXPECTED("didn't find tuple element");
UNREACHABLE_RETURN(LegalVal());
}
default:
SLANG_UNEXPECTED("unhandled");
UNREACHABLE_RETURN(LegalVal());
}
}
static LegalVal legalizeFieldAddress(
IRTypeLegalizationContext* context,
LegalType type,
LegalVal legalPtrOperand,
LegalVal legalFieldOperand)
{
// We don't expect any legalization to affect
// the "field" argument.
auto fieldOperand = legalFieldOperand.getSimple();
assert(fieldOperand->op == kIROp_decl_ref);
auto fieldDeclRef = ((IRDeclRef*)fieldOperand)->declRef;
return legalizeFieldAddress(
context,
type,
legalPtrOperand,
fieldDeclRef);
}
static LegalVal legalizeGetElementPtr(
IRTypeLegalizationContext* context,
LegalType type,
LegalVal legalPtrOperand,
IRValue* indexOperand)
{
auto builder = context->builder;
switch (legalPtrOperand.flavor)
{
case LegalVal::Flavor::simple:
return LegalVal::simple(
builder->emitElementAddress(
type.getSimple(),
legalPtrOperand.getSimple(),
indexOperand));
case LegalVal::Flavor::pair:
{
// There are two sides, the ordinary and the special,
// and we basically just dispatch to both of them.
auto pairVal = legalPtrOperand.getPair();
auto pairInfo = pairVal->pairInfo;
LegalType ordinaryType = type;
LegalType specialType = type;
if (type.flavor == LegalType::Flavor::pair)
{
auto pairType = type.getPair();
ordinaryType = pairType->ordinaryType;
specialType = pairType->specialType;
}
LegalVal ordinaryVal = legalizeGetElementPtr(
context,
ordinaryType,
pairVal->ordinaryVal,
indexOperand);
LegalVal specialVal = legalizeGetElementPtr(
context,
specialType,
pairVal->specialVal,
indexOperand);
return LegalVal::pair(ordinaryVal, specialVal, pairInfo);
}
break;
case LegalVal::Flavor::tuple:
{
// The operand is a tuple of pointer-like
// values, we want to extract the element
// corresponding to a field. We will handle
// this by simply returning the corresponding
// element from the operand.
auto ptrTupleInfo = legalPtrOperand.getTuple();
RefPtr<TuplePseudoVal> resTupleInfo = new TuplePseudoVal();
auto tupleType = type.getTuple();
assert(tupleType);
auto elemCount = ptrTupleInfo->elements.Count();
assert(elemCount == tupleType->elements.Count());
for(UInt ee = 0; ee < elemCount; ++ee)
{
auto ptrElem = ptrTupleInfo->elements[ee];
auto elemType = tupleType->elements[ee].type;
TuplePseudoVal::Element resElem;
resElem.mangledName = ptrElem.mangledName;
resElem.val = legalizeGetElementPtr(
context,
elemType,
ptrElem.val,
indexOperand);
resTupleInfo->elements.Add(resElem);
}
return LegalVal::tuple(resTupleInfo);
}
default:
SLANG_UNEXPECTED("unhandled");
UNREACHABLE_RETURN(LegalVal());
}
}
static LegalVal legalizeGetElementPtr(
IRTypeLegalizationContext* context,
LegalType type,
LegalVal legalPtrOperand,
LegalVal legalIndexOperand)
{
// We don't expect any legalization to affect
// the "index" argument.
auto indexOperand = legalIndexOperand.getSimple();
return legalizeGetElementPtr(
context,
type,
legalPtrOperand,
indexOperand);
}
static LegalVal legalizeInst(
IRTypeLegalizationContext* context,
IRInst* inst,
LegalType type,
LegalVal const* args)
{
switch (inst->op)
{
case kIROp_Load:
return legalizeLoad(context, args[0]);
case kIROp_FieldAddress:
return legalizeFieldAddress(context, type, args[0], args[1]);
case kIROp_getElementPtr:
return legalizeGetElementPtr(context, type, args[0], args[1]);
case kIROp_Store:
return legalizeStore(context, args[0], args[1]);
case kIROp_Call:
return legalizeCall(context, (IRCall*)inst);
default:
// TODO: produce a user-visible diagnostic here
SLANG_UNEXPECTED("non-simple operand(s)!");
break;
}
}
RefPtr<VarLayout> findVarLayout(IRValue* value)
{
if (auto layoutDecoration = value->findDecoration<IRLayoutDecoration>())
return layoutDecoration->layout.As<VarLayout>();
return nullptr;
}
static LegalVal legalizeLocalVar(
IRTypeLegalizationContext* context,
IRVar* irLocalVar)
{
// Legalize the type for the variable's value
auto legalValueType = legalizeType(
context,
irLocalVar->getType()->getValueType());
RefPtr<VarLayout> varLayout = findVarLayout(irLocalVar);
RefPtr<TypeLayout> typeLayout = varLayout ? varLayout->typeLayout : nullptr;
// If we've decided to do implicit deref on the type,
// then go ahead and declare a value of the pointed-to type.
LegalType maybeSimpleType = legalValueType;
while (maybeSimpleType.flavor == LegalType::Flavor::implicitDeref)
{
maybeSimpleType = maybeSimpleType.getImplicitDeref()->valueType;
}
switch (maybeSimpleType.flavor)
{
case LegalType::Flavor::simple:
// Easy case: the type is usable as-is, and we
// should just do that.
irLocalVar->type = context->session->getPtrType(
maybeSimpleType.getSimple());
return LegalVal::simple(irLocalVar);
default:
{
context->insertBeforeLocalVar = irLocalVar;
LegalVarChain* varChain = nullptr;
LegalVarChain varChainStorage;
if (varLayout)
{
varChainStorage.next = nullptr;
varChainStorage.varLayout = varLayout;
varChain = &varChainStorage;
}
LegalVal newVal = declareVars(context, kIROp_Var, legalValueType, typeLayout, varChain, nullptr);
// Remove the old local var.
irLocalVar->removeFromParent();
// add old local var to list
context->oldLocalVars.Add(irLocalVar);
return newVal;
}
break;
}
}
static LegalVal legalizeInst(
IRTypeLegalizationContext* context,
IRInst* inst)
{
if (inst->op == kIROp_Var)
return legalizeLocalVar(context, (IRVar*)inst);
// Need to legalize all the operands.
auto argCount = inst->getArgCount();
List<LegalVal> legalArgs;
bool anyComplex = false;
for (UInt aa = 0; aa < argCount; ++aa)
{
auto oldArg = inst->getArg(aa);
auto legalArg = legalizeOperand(context, oldArg);
legalArgs.Add(legalArg);
if (legalArg.flavor != LegalVal::Flavor::simple)
anyComplex = true;
}
// Also legalize the type of the instruction
LegalType legalType = legalizeType(context, inst->type);
if (!anyComplex && legalType.flavor == LegalType::Flavor::simple)
{
// Nothing interesting happened to the operands,
// so we seem to be okay, right?
for (UInt aa = 0; aa < argCount; ++aa)
{
auto legalArg = legalArgs[aa];
inst->setArg(aa, legalArg.getSimple());
}
inst->type = legalType.getSimple();
return LegalVal::simple(inst);
}
// We have at least one "complex" operand, and we
// need to figure out what to do with it. The anwer
// will, in general, depend on what we are doing.
// We will set up the IR builder so that any new
// instructions generated will be placed after
// the location of the original instruct.
auto builder = context->builder;
builder->curBlock = inst->getParentBlock();
builder->insertBeforeInst = inst->getNextInst();
LegalVal legalVal = legalizeInst(
context,
inst,
legalType,
legalArgs.Buffer());
// After we are done, we will eliminate the
// original instruction by removing it from
// the IR.
//
// TODO: we need to add it to a list of
// instructions to be cleaned up...
inst->removeFromParent();
// The value to be used when referencing
// the original instruction will now be
// whatever value(s) we created to replace it.
return legalVal;
}
static void addParamType(IRFuncType * ftype, LegalType t)
{
switch (t.flavor)
{
case LegalType::Flavor::none:
break;
case LegalType::Flavor::simple:
ftype->paramTypes.Add(t.obj.As<Type>());
break;
case LegalType::Flavor::implicitDeref:
{
auto imp = t.obj.As<ImplicitDerefType>();
addParamType(ftype, imp->valueType);
break;
}
case LegalType::Flavor::pair:
{
auto pairInfo = t.getPair();
addParamType(ftype, pairInfo->ordinaryType);
addParamType(ftype, pairInfo->specialType);
}
break;
case LegalType::Flavor::tuple:
{
auto tup = t.obj.As<TuplePseudoType>();
for (auto & elem : tup->elements)
addParamType(ftype, elem.type);
}
break;
default:
SLANG_ASSERT(false);
}
}
static void legalizeFunc(
IRTypeLegalizationContext* context,
IRFunc* irFunc)
{
// Overwrite the function's type with
// the result of legalization.
auto newFuncType = new IRFuncType();
newFuncType->setSession(context->session);
auto oldFuncType = irFunc->type.As<IRFuncType>();
newFuncType->resultType = legalizeSimpleType(context, oldFuncType->resultType);
for (auto & paramType : oldFuncType->paramTypes)
{
auto legalParamType = legalizeType(context, paramType);
addParamType(newFuncType, legalParamType);
}
irFunc->type = newFuncType;
List<LegalVal> paramVals;
List<IRValue*> oldParams;
// we use this list to store replaced local var insts.
// these old instructions will be freed when we are done.
context->oldLocalVars.Clear();
// Go through the blocks of the function
for (auto bb = irFunc->getFirstBlock(); bb; bb = bb->getNextBlock())
{
// Legalize the parameters of the block, which may
// involve increasing the number of parameters
for (auto pp = bb->getFirstParam(); pp; pp = pp->nextParam)
{
auto legalParamType = legalizeType(context, pp->getType());
if (legalParamType.flavor != LegalType::Flavor::simple)
{
context->insertBeforeParam = pp;
context->builder->curBlock = nullptr;
auto paramVal = declareVars(context, kIROp_Param, legalParamType, nullptr, nullptr, nullptr);
paramVals.Add(paramVal);
if (pp == bb->getFirstParam())
{
bb->firstParam = pp;
while (bb->firstParam->prevParam)
bb->firstParam = bb->firstParam->prevParam;
}
bb->lastParam = pp->prevParam;
if (pp->prevParam)
pp->prevParam->nextParam = pp->nextParam;
if (pp->nextParam)
pp->nextParam->prevParam = pp->prevParam;
auto oldParam = pp;
oldParams.Add(oldParam);
registerLegalizedValue(context, oldParam, paramVal);
}
}
// Now legalize the instructions inside the block
IRInst* nextInst = nullptr;
for (auto ii = bb->getFirstInst(); ii; ii = nextInst)
{
nextInst = ii->getNextInst();
LegalVal legalVal = legalizeInst(context, ii);
registerLegalizedValue(context, ii, legalVal);
}
}
for (auto & op : oldParams)
{
SLANG_ASSERT(op->firstUse == nullptr || op->firstUse->nextUse == nullptr);
op->deallocate();
}
for (auto & lv : context->oldLocalVars)
lv->deallocate();
}
static LegalVal declareSimpleVar(
IRTypeLegalizationContext* context,
IROp op,
Type* type,
TypeLayout* typeLayout,
LegalVarChain* varChain,
IRGlobalNameInfo* globalNameInfo)
{
RefPtr<VarLayout> varLayout = createVarLayout(varChain, typeLayout);
DeclRef<VarDeclBase> varDeclRef;
if (varChain)
{
varDeclRef = varChain->varLayout->varDecl;
}
IRBuilder* builder = context->builder;
IRValue* irVar = nullptr;
LegalVal legalVarVal;
switch (op)
{
case kIROp_global_var:
{
auto globalVar = builder->createGlobalVar(type);
globalVar->removeFromParent();
globalVar->insertBefore(context->insertBeforeGlobal, builder->getModule());
// The legalization of a global variable with linkage (one that has
// a mangled name), must also have an exported name, so that code
// can link against it.
//
// For now we do something *really* simplistic, and just append
// a counter to each leaf variable generated from the original
if (globalNameInfo)
{
String mangledName = globalNameInfo->globalVar->mangledName;
if (mangledName.Length() != 0)
{
mangledName.append("L");
mangledName.append(globalNameInfo->counter++);
globalVar->mangledName = mangledName;
}
}
irVar = globalVar;
legalVarVal = LegalVal::simple(irVar);
}
break;
case kIROp_Var:
{
auto localVar = builder->emitVar(type);
localVar->removeFromParent();
localVar->insertBefore(context->insertBeforeLocalVar);
irVar = localVar;
legalVarVal = LegalVal::simple(irVar);
}
break;
case kIROp_Param:
{
auto param = builder->emitParam(type);
if (context->insertBeforeParam->prevParam)
context->insertBeforeParam->prevParam->nextParam = param;
param->prevParam = context->insertBeforeParam->prevParam;
param->nextParam = context->insertBeforeParam;
context->insertBeforeParam->prevParam = param;
irVar = param;
legalVarVal = LegalVal::simple(irVar);
}
break;
default:
SLANG_UNEXPECTED("unexpected IR opcode");
break;
}
if (irVar)
{
if (varLayout)
{
builder->addLayoutDecoration(irVar, varLayout);
}
if (varDeclRef)
{
builder->addHighLevelDeclDecoration(irVar, varDeclRef.getDecl());
}
}
return legalVarVal;
}
static LegalVal declareVars(
IRTypeLegalizationContext* context,
IROp op,
LegalType type,
TypeLayout* typeLayout,
LegalVarChain* varChain,
IRGlobalNameInfo* globalNameInfo)
{
switch (type.flavor)
{
case LegalType::Flavor::none:
return LegalVal();
case LegalType::Flavor::simple:
return declareSimpleVar(context, op, type.getSimple(), typeLayout, varChain, globalNameInfo);
break;
case LegalType::Flavor::implicitDeref:
{
// Just declare a variable of the pointed-to type,
// since we are removing the indirection.
auto val = declareVars(
context,
op,
type.getImplicitDeref()->valueType,
getDerefTypeLayout(typeLayout),
varChain,
globalNameInfo);
return LegalVal::implicitDeref(val);
}
break;
case LegalType::Flavor::pair:
{
auto pairType = type.getPair();
auto ordinaryVal = declareVars(context, op, pairType->ordinaryType, typeLayout, varChain, globalNameInfo);
auto specialVal = declareVars(context, op, pairType->specialType, typeLayout, varChain, globalNameInfo);
return LegalVal::pair(ordinaryVal, specialVal, pairType->pairInfo);
}
case LegalType::Flavor::tuple:
{
// Declare one variable for each element of the tuple
auto tupleType = type.getTuple();
RefPtr<TuplePseudoVal> tupleVal = new TuplePseudoVal();
for (auto ee : tupleType->elements)
{
auto fieldLayout = getFieldLayout(typeLayout, ee.mangledName);
RefPtr<TypeLayout> fieldTypeLayout = fieldLayout ? fieldLayout->typeLayout : nullptr;
// If we are processing layout information, then
// we need to create a new link in the chain
// of variables that will determine offsets
// for the eventual leaf fields...
LegalVarChain newVarChainStorage;
LegalVarChain* newVarChain = varChain;
if (fieldLayout)
{
newVarChainStorage.next = varChain;
newVarChainStorage.varLayout = fieldLayout;
newVarChain = &newVarChainStorage;
}
LegalVal fieldVal = declareVars(
context,
op,
ee.type,
fieldTypeLayout,
newVarChain,
globalNameInfo);
TuplePseudoVal::Element element;
element.mangledName = ee.mangledName;
element.val = fieldVal;
tupleVal->elements.Add(element);
}
return LegalVal::tuple(tupleVal);
}
break;
default:
SLANG_UNEXPECTED("unhandled");
break;
}
}
static void legalizeGlobalVar(
IRTypeLegalizationContext* context,
IRGlobalVar* irGlobalVar)
{
// Legalize the type for the variable's value
auto legalValueType = legalizeType(
context,
irGlobalVar->getType()->getValueType());
RefPtr<VarLayout> varLayout = findVarLayout(irGlobalVar);
RefPtr<TypeLayout> typeLayout = varLayout ? varLayout->typeLayout : nullptr;
switch (legalValueType.flavor)
{
case LegalType::Flavor::simple:
// Easy case: the type is usable as-is, and we
// should just do that.
irGlobalVar->type = context->session->getPtrType(
legalValueType.getSimple());
break;
default:
{
context->insertBeforeGlobal = irGlobalVar->getNextValue();
LegalVarChain* varChain = nullptr;
LegalVarChain varChainStorage;
if (varLayout)
{
varChainStorage.next = nullptr;
varChainStorage.varLayout = varLayout;
varChain = &varChainStorage;
}
IRGlobalNameInfo globalNameInfo;
globalNameInfo.globalVar = irGlobalVar;
globalNameInfo.counter = 0;
LegalVal newVal = declareVars(context, kIROp_global_var, legalValueType, typeLayout, varChain, &globalNameInfo);
// Register the new value as the replacement for the old
registerLegalizedValue(context, irGlobalVar, newVal);
// Also register the variable according to its mangled name, if any.
maybeRegisterLegalizedGlobal(context, irGlobalVar, newVal);
// Remove the old global from the module.
irGlobalVar->removeFromParent();
// TODO: actually clean up the global!
}
break;
}
}
static void legalizeGlobalConstant(
IRTypeLegalizationContext* context,
IRGlobalConstant* irGlobalConstant)
{
// Legalize the type for the variable's value
auto legalValueType = legalizeType(
context,
irGlobalConstant->getType());
switch (legalValueType.flavor)
{
case LegalType::Flavor::simple:
// Easy case: the type is usable as-is, and we
// should just do that.
irGlobalConstant->type = legalValueType.getSimple();
break;
default:
{
context->insertBeforeGlobal = irGlobalConstant->getNextValue();
IRGlobalNameInfo globalNameInfo;
globalNameInfo.globalVar = irGlobalConstant;
globalNameInfo.counter = 0;
// TODO: need to handle initializer here!
LegalVal newVal = declareVars(context, kIROp_global_constant, legalValueType, nullptr, nullptr, &globalNameInfo);
// Register the new value as the replacement for the old
registerLegalizedValue(context, irGlobalConstant, newVal);
// Also register the variable according to its mangled name, if any.
maybeRegisterLegalizedGlobal(context, irGlobalConstant, newVal);
// Remove the old global from the module.
irGlobalConstant->removeFromParent();
// TODO: actually clean up the global!
}
break;
}
}
static void legalizeGlobalValue(
IRTypeLegalizationContext* context,
IRGlobalValue* irValue)
{
switch (irValue->op)
{
case kIROp_witness_table:
// Just skip these.
break;
case kIROp_Func:
legalizeFunc(context, (IRFunc*)irValue);
break;
case kIROp_global_var:
legalizeGlobalVar(context, (IRGlobalVar*)irValue);
break;
case kIROp_global_constant:
legalizeGlobalConstant(context, (IRGlobalConstant*)irValue);
break;
default:
SLANG_UNEXPECTED("unknown global value type");
break;
}
}
static void legalizeTypes(
IRTypeLegalizationContext* context)
{
auto module = context->module;
for (auto gv = module->getFirstGlobalValue(); gv; gv = gv->getNextValue())
{
legalizeGlobalValue(context, gv);
}
}
void legalizeTypes(
TypeLegalizationContext* typeLegalizationContext,
IRModule* module)
{
auto session = module->session;
SharedIRBuilder sharedBuilderStorage;
auto sharedBuilder = &sharedBuilderStorage;
sharedBuilder->session = session;
sharedBuilder->module = module;
IRBuilder builderStorage;
auto builder = &builderStorage;
builder->sharedBuilder = sharedBuilder;
IRTypeLegalizationContext contextStorage;
auto context = &contextStorage;
context->session = session;
context->module = module;
context->builder = builder;
context->typeLegalizationContext = typeLegalizationContext;
legalizeTypes(context);
}
}
|