forked from KhronosGroup/SPIRV-LLVM-Translator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SPIRVTypeScavenger.cpp
1076 lines (991 loc) · 42.3 KB
/
SPIRVTypeScavenger.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
//===- SPIRVTypeScavenger.cpp - Recover pointer types in opaque pointer IR ===//
//
// The LLVM/SPIR-V Translator
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
// Copyright (c) 2022 The Khronos Group Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the "Software"),
// to deal with the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following conditions:
//
// Redistributions of source code must retain the above copyright notice,
// this list of conditions and the following disclaimers.
// Redistributions in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimers in the documentation
// and/or other materials provided with the distribution.
// Neither the names of The Khronos Group, nor the names of its
// contributors may be used to endorse or promote products derived from this
// Software without specific prior written permission.
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS WITH
// THE SOFTWARE.
//
//===----------------------------------------------------------------------===//
//
// This file implements the necessary logic to recover pointer types from LLVM
// IR for the output SPIR-V file from the opaque pointers in LLVM IR.
//
// The core algorithm being implemented is rather simple, although there are
// several complications that make its implementation more difficult. At its
// core, the algorithm works like this:
//
// 1. Replace every instance of an opaque pointer type with a typed pointer that
// points to an unknown type variable.
// 2. Convert each instruction into a series of typing rules. For example,
// load i8, ptr %ptr implies that %ptr must be typedptr(i8, 0).
// 3. Based on the typing rules, resolve the type variables to concrete types.
// 4. If the typing rules produce a contradiction (e.g., i8 == i32), insert a
// synthetic bitcast to represent the bitcast that would have been present in
// a typed pointer IR.
// 5. If any type variables are unresolved at the end of the typing process,
// assign i8 to them instead.
//
// Typed pointers are represented with the TypedPointerType. Type variables are
// represented as target("typevar", i), where i is an integer to disambiguate
// between different unknown types. (It is an index into the TypeVariables and
// UnifiedTypeVars fields).
//
// Step 3 of the above algorithm is represented by unifyType, which implements a
// unification-based type algorithm. This means there exists essentially just
// four cases that need to be considered:
// * unify(type var, concrete type):
// In this case, the concrete type is assigned to the type variable
// Note: It is possible for concrete type to contain nested type variables,
// e.g., typedptr(target("typevar", 3), 4)
// * unify(type var, concrete type containing type var):
// Unification fails in this case. This can come up if you have code like
// this:
// %ptr = alloca ptr
// store ptr %ptr, ptr %ptr
// * unify(type var, type var):
// In this case, the two type variables are unified into one so that they get
// the same concrete type. This uses IntEqClasses as the implementation of the
// union-find data structure.
// * unify(concrete type, same concrete type):
// There is nothing to do in this case
// * unify(concrete type, different concrete type):
// Unification fails in this case, and a bitcast needs to be generated.
//
// Note that this algorithm does not attempt to seek a minimal set of bitcasts
// that need to be added to produce a correctly-typed program.
//
// Type rules are represented by the SPIRVTypeScavenger::TypeRule class, and
// should be constructed using the provided static methods (which are easier to
// understand than the constructor itself). Type rules boil down to the
// following categories:
// * operand I has type T
// * operand I has the same type as operand J
// * the return value has type T
// * the return value has the same type as operand I
// with any of the above operands, types, or return values potentially having a
// level of indirection. For example, the rule for an addrspacecast is that the
// return value points to the same type that its sole operand points to. The
// indirection effectively means T->getScalarType()->getPointerElementType().
// Note: When constructing type rules fixing an operand or the return to a
// particular type T, the type must be a type using typed pointers and/or
// type variables in lieu of ptr.
// Note: getTypeRules may be called twice on an instruction, so if a new type
// variable needs to be created for type rules, it needs to be saved in
// the AssociatedTypeVariables method to ensure proper functioning. This
// is particularly important if you need to use the type variable both in
// constraining the return value and an operand.
//
// Now for the complications to the above algorithm:
//
// The most notable issue is that LLVM does not allow no-op constant
// expressions to be created. This means that we have to be very careful about
// the types of constant values. As the SPIR-V translator expands most constant
// expressions into instructions, this isn't much of an issue, but where it
// really comes into play is with global variable initializers, which don't have
// that luxury. Global variable typing therefore pays careful attention to the
// type of the initializer.
//
// Constructing type variables is a slightly expensive step, so before
// attempting to create a type variable for the return of an instruction, we
// instead look through the type rules to see if we can get the type of the
// return value from one of its input operands.
//
// Recursive pointer types are not possible to express, as we make no attempt
// to recover pointer types within struct types. It is still possible for
// a type rule to suggest an infinite recursive type (consider the example
// store ptr %x, ptr %x), so we have to guard against it in unifyType.
//
//===----------------------------------------------------------------------===//
#include "SPIRVTypeScavenger.h"
#include "SPIRVInternal.h"
#include "llvm/IR/IRBuilder.h"
#include "llvm/IR/InstVisitor.h"
#include "llvm/IR/Instructions.h"
#include "llvm/IR/IntrinsicInst.h"
#include "llvm/IR/NoFolder.h"
#include "llvm/IR/Operator.h"
#include "llvm/Support/Regex.h"
#define DEBUG_TYPE "type-scavenger"
using namespace llvm;
namespace {
static inline std::optional<unsigned> isTypeVariable(Type *T) {
if (auto *TET = dyn_cast<TargetExtType>(T))
if (TET->getName() == "typevar") {
return TET->getIntParameter(0);
}
return std::nullopt;
}
/// Convert Ty to a type that can be unified with a type-variable-ified L, given
/// that either or both types may have an indirection.
/// For example, adjust(L, true, Ty, false) will extract the element type of Ty
/// for unifying with L.
/// In this method, L is expected to be a type of a value, while Ty (and the
/// return value) will use TypedPointerType instead of PointerType.
static Type *adjustIndirect(Type *L, bool LIndirect, Type *Ty, bool TIndirect) {
if (LIndirect)
Ty = cast<TypedPointerType>(Ty->getScalarType())->getElementType();
if (TIndirect) {
unsigned AS = L->getScalarType()->getPointerAddressSpace();
Ty = TypedPointerType::get(Ty, AS);
if (auto *VT = dyn_cast<VectorType>(L))
Ty = VectorType::get(Ty, VT->getElementCount());
}
return Ty;
}
/// Return the type with all inner pointer types replaced with the result of
/// calling MutatePointer(PointerAddressSpace).
template <typename Fn> Type *mutateType(Type *T, Fn MutatePointer) {
if (T->isPointerTy()) {
return MutatePointer(T->getPointerAddressSpace());
}
if (auto *VT = dyn_cast<VectorType>(T)) {
return VectorType::get(mutateType(VT->getScalarType(), MutatePointer),
VT->getElementCount());
}
if (auto *AT = dyn_cast<ArrayType>(T)) {
return ArrayType::get(mutateType(AT->getElementType(), MutatePointer),
AT->getNumElements());
}
if (auto *FT = dyn_cast<FunctionType>(T)) {
SmallVector<Type *, 4> ParamTypes;
for (Type *Inner : FT->params())
ParamTypes.push_back(mutateType(Inner, MutatePointer));
Type *ReturnTy = mutateType(FT->getReturnType(), MutatePointer);
return FunctionType::get(ReturnTy, ParamTypes, FT->isVarArg());
}
// TODO: support literal structs
return T;
}
/// Return true if the type is an opaque pointer, or contains an opaque pointer
/// that needs to be typed.
bool hasPointerType(Type *T) {
if (T->isPtrOrPtrVectorTy())
return true;
if (auto *AT = dyn_cast<ArrayType>(T))
return hasPointerType(AT->getElementType());
if (auto *FT = dyn_cast<FunctionType>(T)) {
for (Type *Inner : FT->params())
if (hasPointerType(Inner))
return true;
return hasPointerType(FT->getReturnType());
}
// TODO: literal structs
return false;
}
/// Get a type where all internal pointer types are replaced with i8*.
Type *getUnknownTyped(Type *T) {
Type *Int8Ty = Type::getInt8Ty(T->getContext());
return mutateType(
T, [=](unsigned AS) { return TypedPointerType::get(Int8Ty, AS); });
}
bool hasTypeVariable(Type *T, const unsigned TypeVarNum) {
if (auto *TPT = dyn_cast<TypedPointerType>(T))
return hasTypeVariable(TPT->getElementType(), TypeVarNum);
if (auto *VT = dyn_cast<VectorType>(T))
return hasTypeVariable(VT->getElementType(), TypeVarNum);
if (auto *AT = dyn_cast<ArrayType>(T))
return hasTypeVariable(AT->getElementType(), TypeVarNum);
if (auto *FT = dyn_cast<FunctionType>(T)) {
for (Type *Inner : FT->params())
if (hasTypeVariable(Inner, TypeVarNum))
return true;
return hasTypeVariable(FT->getReturnType(), TypeVarNum);
}
if (auto CheckNum = isTypeVariable(T)) {
return TypeVarNum == *CheckNum;
}
return false;
}
} // anonymous namespace
Type *SPIRVTypeScavenger::substituteTypeVariables(Type *T) {
if (auto *TPT = dyn_cast<TypedPointerType>(T))
return TypedPointerType::get(substituteTypeVariables(TPT->getElementType()),
TPT->getAddressSpace());
if (auto *VT = dyn_cast<VectorType>(T))
return VectorType::get(substituteTypeVariables(VT->getElementType()),
VT->getElementCount());
if (auto *AT = dyn_cast<ArrayType>(T))
return ArrayType::get(substituteTypeVariables(AT->getElementType()),
AT->getNumElements());
if (auto *FT = dyn_cast<FunctionType>(T)) {
SmallVector<Type *, 4> ParamTypes;
for (Type *Inner : FT->params())
ParamTypes.push_back(substituteTypeVariables(Inner));
Type *ReturnTy = substituteTypeVariables(FT->getReturnType());
return FunctionType::get(ReturnTy, ParamTypes, FT->isVarArg());
}
if (auto Index = isTypeVariable(T)) {
unsigned TypeVarNum = *Index;
TypeVarNum = UnifiedTypeVars.findLeader(TypeVarNum);
Type *&SubstTy = TypeVariables[TypeVarNum];
// A value in TypeVariables may itself contain type variables that need to
// be substituted. Substitute these as well.
if (SubstTy)
return SubstTy = substituteTypeVariables(SubstTy);
// Even if it's not fully resolved, return the leader of the current
// equivalence class instead. This allows for easier scanning of recursive
// type declarations.
return TargetExtType::get(T->getContext(), "typevar", {}, {TypeVarNum});
}
return T;
}
bool SPIRVTypeScavenger::unifyType(Type *T1, Type *T2) {
T1 = substituteTypeVariables(T1);
T2 = substituteTypeVariables(T2);
if (T1 == T2)
return true;
auto SetTypeVar = [&](unsigned TypeVarNum, Type *ActualTy) {
unsigned Leader = UnifiedTypeVars.findLeader(TypeVarNum);
// This method might be called with T1 as a concrete type containing
// pointers, and we want to make sure those don't leak into type variables.
// Guard against that here.
ActualTy = allocateTypeVariable(ActualTy);
// Check for recursion in type variables. Such recursive types generally
// cannot be correctly typed.
if (hasTypeVariable(ActualTy, Leader))
return false;
LLVM_DEBUG(dbgs() << "Type variable " << TypeVarNum << " is " << *ActualTy
<< "\n");
assert(!TypeVariables[Leader] && "Type was already fixed?");
TypeVariables[Leader] = ActualTy;
return true;
};
if (auto T1Num = isTypeVariable(T1)) {
if (auto T2Num = isTypeVariable(T2)) {
// Two type variables. Unify the two of them into the same type.
if (T1Num != T2Num) {
UnifiedTypeVars.join(*T1Num, *T2Num);
LLVM_DEBUG(dbgs() << "Joining typevar " << *T1Num << " and " << *T2Num
<< "\n");
}
return true;
}
return SetTypeVar(*T1Num, T2);
}
if (auto T2Num = isTypeVariable(T2)) {
// We know that T1 can't be a type variable, so the only possibility is that
// we assign T2 to T1.
return SetTypeVar(*T2Num, T1);
}
// At this point, we know that neither type is a type variable. If the two
// types have a different structure, we can't unify them.
if (auto *TPT1 = dyn_cast<TypedPointerType>(T1)) {
if (auto *TPT2 = dyn_cast<TypedPointerType>(T2)) {
if (TPT1->getAddressSpace() != TPT2->getAddressSpace())
return false;
return unifyType(TPT1->getElementType(), TPT2->getElementType());
}
return false;
}
// We can also call unifyType(ptr, T2) (this is useful for propagating types
// to return values of instructions). In such a case, the ptr type is
// equivalent to typedptr(target("typevar")), for some type variable we
// haven't yet allocated. In this use case, it suffices to know that T2 is
// also a typed pointer type, as the case where T2 is a type variable was
// handled earlier.
if (isa<PointerType>(T1)) {
if (auto *TPT2 = dyn_cast<TypedPointerType>(T2))
return TPT2->getAddressSpace() == T1->getPointerAddressSpace();
return false;
}
if (auto *FT1 = dyn_cast<FunctionType>(T1)) {
if (auto *FT2 = dyn_cast<FunctionType>(T2)) {
if (FT1->getNumParams() != FT2->getNumParams())
return false;
if (FT1->isVarArg() != FT2->isVarArg())
return false;
if (!unifyType(FT1->getReturnType(), FT2->getReturnType()))
return false;
for (const auto &[PT1, PT2] : zip(FT1->params(), FT2->params()))
if (!unifyType(PT1, PT2))
return false;
return true;
}
return false;
}
if (auto *VT1 = dyn_cast<VectorType>(T1)) {
if (auto *VT2 = dyn_cast<VectorType>(T2)) {
if (VT1->getElementCount() != VT2->getElementCount())
return false;
return unifyType(VT1->getScalarType(), VT2->getScalarType());
}
return false;
}
if (auto *AT1 = dyn_cast<ArrayType>(T1)) {
if (auto *AT2 = dyn_cast<ArrayType>(T2)) {
if (AT1->getNumElements() != AT2->getNumElements())
return false;
return unifyType(AT1->getElementType(), AT2->getElementType());
}
return false;
}
// We already established T1 != T2 earlier, so there's no way we're capable of
// unifying at this point.
return false;
}
void SPIRVTypeScavenger::typeModule(Module &M) {
// Generate corrected function types for all functions in the module.
for (auto &F : M.functions()) {
deduceFunctionType(F);
}
// Now that we have function types, type the global variables. We have
// restrictions on our ability to do typing on constant initializers, so we
// need to make sure that global variables get typed.
for (auto &GV : M.globals())
typeGlobalValue(GV, GV.hasInitializer() ? GV.getInitializer() : nullptr);
// SPIR-V doesn't support global aliases, so pass through all of the types of
// global aliasees to the global alias (this at least ensures correct typing
// of uses of the global alias).
for (auto &GA : M.aliases()) {
Type *ScavengedTy = getScavengedType(GA.getAliasee());
DeducedTypes[&GA] = ScavengedTy;
LLVM_DEBUG(dbgs() << "Type of " << GA << " is " << *ScavengedTy << "\n");
}
// Type all instructions in the module.
for (auto &F : M.functions()) {
LLVM_DEBUG(dbgs() << "Typing function " << F.getName() << "\n");
for (BasicBlock &BB : F) {
for (Instruction &I : BB) {
getTypeAfterRules(&I);
correctUseTypes(I);
}
}
}
// If there are any type variables we couldn't resolve, fallback to assigning
// them as an i8* type.
Type *Int8Ty = Type::getInt8Ty(M.getContext());
for (const auto &[TypeVarNum, TypeVar] : enumerate(TypeVariables)) {
unsigned PrimaryVar = UnifiedTypeVars.findLeader(TypeVarNum);
Type *LeaderTy = TypeVariables[PrimaryVar];
if (TypeVar)
TypeVar = substituteTypeVariables(TypeVar);
if (LeaderTy)
LeaderTy = substituteTypeVariables(LeaderTy);
assert((!TypeVar || LeaderTy == TypeVar) &&
"Inconsistent type variable unification");
if (!TypeVar) {
TypeVar = LeaderTy ? LeaderTy : Int8Ty;
}
TypeVariables[TypeVarNum] = TypeVar;
LLVM_DEBUG(dbgs() << "Type variable " << TypeVarNum << " resolved to "
<< *TypeVar << "\n");
}
return;
}
bool SPIRVTypeScavenger::typeIntrinsicCall(
CallBase &CB, SmallVectorImpl<TypeRule> &TypeRules) {
Function *TargetFn = CB.getCalledFunction();
assert(TargetFn && TargetFn->isDeclaration() &&
"Call is not an intrinsic function call");
LLVMContext &Ctx = TargetFn->getContext();
// If the type is a pointer type, replace it with a typedptr(typevar) type
// instead, using AssociatedTypeVariables.
auto GetTypeOrTypeVar = [&](Type *BaseTy) {
if (!BaseTy->isPointerTy())
return BaseTy;
Type *&AssociatedTy = AssociatedTypeVariables[&CB];
if (!AssociatedTy)
AssociatedTy = allocateTypeVariable(BaseTy);
return AssociatedTy;
};
StringRef DemangledName;
if (oclIsBuiltin(TargetFn->getName(), DemangledName) ||
isDecoratedSPIRVFunc(TargetFn, DemangledName)) {
Op OC = getSPIRVFuncOC(DemangledName);
switch (OC) {
case OpAtomicLoad:
case OpAtomicExchange:
case OpAtomicCompareExchange:
case OpAtomicIAdd:
case OpAtomicISub:
case OpAtomicFAddEXT:
case OpAtomicSMin:
case OpAtomicUMin:
case OpAtomicFMinEXT:
case OpAtomicSMax:
case OpAtomicUMax:
case OpAtomicFMaxEXT:
case OpAtomicAnd:
case OpAtomicOr:
case OpAtomicXor:
TypeRules.push_back(TypeRule::pointsTo(CB, 0, CB.getType()));
return true;
case OpAtomicStore:
TypeRules.push_back(
TypeRule::pointsTo(CB, 0, CB.getArgOperand(3)->getType()));
return true;
case OpGenericCastToPtr:
case OpGenericCastToPtrExplicit: {
Type *Ty =
cast<TypedPointerType>(getFunctionType(TargetFn)->getParamType(0))
->getElementType();
TypeRules.push_back(TypeRule::pointsTo(CB, 0, Ty));
TypeRules.push_back(TypeRule::returnsPointerTo(Ty));
return true;
}
default:
// Do nothing
break;
}
}
if (auto IntrinID = TargetFn->getIntrinsicID()) {
switch (IntrinID) {
case Intrinsic::memcpy: {
// First two parameters are pointers, but they point to the same thing
// (albeit maybe in different address spaces).
TypeRules.push_back(TypeRule::isIndirect(CB, 0, 1));
break;
}
case Intrinsic::memset:
TypeRules.push_back(TypeRule::pointsTo(CB, 0, Type::getInt8Ty(Ctx)));
break;
case Intrinsic::lifetime_start:
case Intrinsic::lifetime_end:
case Intrinsic::invariant_start:
// These intrinsics were stored as i8* as typed pointers, and the SPIR-V
// writer will expect these to be i8*, even if they can be any pointer
// type.
TypeRules.push_back(TypeRule::pointsTo(CB, 1, Type::getInt8Ty(Ctx)));
break;
case Intrinsic::invariant_end:
// This is like invariant_start with an extra string parameter in the
// beginning (so the pointer object moves to argument two).
TypeRules.push_back(TypeRule::pointsTo(CB, 0, Type::getInt8Ty(Ctx)));
TypeRules.push_back(TypeRule::pointsTo(CB, 2, Type::getInt8Ty(Ctx)));
break;
case Intrinsic::var_annotation:
// The first parameter of these is an i8*.
// (See below for notes on the latter parameters).
TypeRules.push_back(TypeRule::pointsTo(CB, 0, Type::getInt8Ty(Ctx)));
break;
case Intrinsic::ptr_annotation:
// Returns the first argument.
// (See below for notes on the latter parameters).
TypeRules.push_back(TypeRule::pointsTo(CB, 0, Type::getInt8Ty(Ctx)));
TypeRules.push_back(TypeRule::returnsPointerTo(Type::getInt8Ty(Ctx)));
break;
case Intrinsic::annotation:
// Second and third parameters are strings, which should be constants
// for global variables. Nominally, this is i8*, but we specifically
// *do not* want to insert bitcast instructions (they need to remain
// global constants).
break;
case Intrinsic::stacksave:
TypeRules.push_back(TypeRule::returnsPointerTo(Type::getInt8Ty(Ctx)));
break;
case Intrinsic::stackrestore:
TypeRules.push_back(TypeRule::pointsTo(CB, 0, Type::getInt8Ty(Ctx)));
break;
case Intrinsic::instrprof_cover:
case Intrinsic::instrprof_increment:
case Intrinsic::instrprof_increment_step:
case Intrinsic::instrprof_value_profile:
// llvm.instrprof.* intrinsics are not supported
TypeRules.push_back(TypeRule::pointsTo(CB, 0, Type::getInt8Ty(Ctx)));
break;
case Intrinsic::masked_gather: {
Type *ScalarTy = GetTypeOrTypeVar(CB.getType()->getScalarType());
TypeRules.push_back(TypeRule::pointsTo(CB, 0, ScalarTy));
if (CB.getType()->getScalarType()->isPointerTy())
TypeRules.push_back(TypeRule::propagates(CB, 3));
break;
}
case Intrinsic::masked_scatter: {
Type *ScalarTy =
GetTypeOrTypeVar(CB.getOperand(0)->getType()->getScalarType());
TypeRules.push_back(TypeRule::pointsTo(CB, 1, ScalarTy));
break;
}
default:
return false;
}
} else if (TargetFn->getName().starts_with("_Z18__spirv_ocl_printf")) {
Type *Int8Ty = Type::getInt8Ty(Ctx);
// The first argument is a string pointer. Subsequent arguments may include
// pointer-valued arguments, corresponding to %s or %p parameters.
// Therefore, all parameters need to be i8*.
for (Use &U : CB.args()) {
if (U->getType()->isPointerTy())
TypeRules.push_back(TypeRule::pointsTo(U, Int8Ty));
}
} else if (TargetFn->getName() == "__spirv_GetKernelWorkGroupSize__") {
TypeRules.push_back(TypeRule::pointsTo(CB, 1, Type::getInt8Ty(Ctx)));
} else if (TargetFn->getName() ==
"__spirv_GetKernelPreferredWorkGroupSizeMultiple__") {
TypeRules.push_back(TypeRule::pointsTo(CB, 1, Type::getInt8Ty(Ctx)));
} else if (TargetFn->getName() ==
"__spirv_GetKernelNDrangeMaxSubGroupSize__") {
TypeRules.push_back(TypeRule::pointsTo(CB, 2, Type::getInt8Ty(Ctx)));
} else if (TargetFn->getName() == "__spirv_GetKernelNDrangeSubGroupCount__") {
TypeRules.push_back(TypeRule::pointsTo(CB, 2, Type::getInt8Ty(Ctx)));
} else if (TargetFn->getName().starts_with("__spirv_EnqueueKernel__")) {
Type *DevEvent = TargetExtType::get(Ctx, "spirv.DeviceEvent");
TypeRules.push_back(TypeRule::pointsTo(CB, 4, DevEvent));
TypeRules.push_back(TypeRule::pointsTo(CB, 5, DevEvent));
TypeRules.push_back(TypeRule::pointsTo(CB, 7, Type::getInt8Ty(Ctx)));
} else if (TargetFn->getName().starts_with(
"_Z33__regcall3____builtin_invoke_simd")) {
// First argument is a function to call, subsequent arguments are parameters
// to said function.
auto *FnTy = getFunctionType(cast<Function>(CB.getArgOperand(0)));
TypeRules.push_back(TypeRule::pointsTo(CB, 0, FnTy));
typeFunctionParams(CB, FnTy, 1, true, TypeRules);
// Also apply type rules to the parameter types of the underlying function.
return false;
} else
return false;
return true;
}
void SPIRVTypeScavenger::typeFunctionParams(
CallBase &CB, FunctionType *FT, unsigned ArgStart, bool IncludeRet,
SmallVectorImpl<TypeRule> &TypeRules) {
for (const auto &[U, ArgTy] :
zip(drop_begin(CB.args(), ArgStart), FT->params())) {
if (hasPointerType(U->getType())) {
TypeRules.push_back(TypeRule::is(U, ArgTy));
}
}
if (IncludeRet) {
if (hasPointerType(CB.getType()))
TypeRules.push_back(TypeRule::returns(FT->getReturnType()));
}
}
void SPIRVTypeScavenger::typeGlobalValue(GlobalValue &GV, Constant *Init) {
auto GetNaturalType = [&](Value *C) -> Type * {
if (isa<GlobalValue>(C)) {
auto It = DeducedTypes.find(C);
if (It != DeducedTypes.end())
return It->second;
}
return getUnknownTyped(C->getType());
};
Type *Ty = GV.getValueType();
Type *MemType = nullptr;
// If the initializer is an array or vector of globals that all have the same
// type, prefer to use that type.
if (Init && (isa<ConstantArray>(Init) || isa<ConstantVector>(Init))) {
Type *InnerTy = Init->getType()->getContainedType(0);
if (InnerTy->isPointerTy()) {
Type *CommonTy = allocateTypeVariable(InnerTy);
bool Successful = true;
for (Value *Op : Init->operand_values()) {
Successful &= unifyType(CommonTy, GetNaturalType(Op));
if (!Successful)
break;
}
if (Successful) {
CommonTy = substituteTypeVariables(CommonTy);
if (isa<ConstantArray>(Init))
MemType = ArrayType::get(CommonTy, Ty->getArrayNumElements());
else
MemType = VectorType::get(CommonTy,
cast<VectorType>(Ty)->getElementCount());
}
}
}
// If there's an initializer, give it a fixed type based on the initializer.
if (Init && !MemType)
MemType = GetNaturalType(Init);
// At this point, use a fixed type based on the value type of the global value
// if we didn't compute it already.
if (!MemType)
MemType = getUnknownTyped(GV.getValueType());
Type *TypedTy = TypedPointerType::get(MemType, GV.getAddressSpace());
LLVM_DEBUG(dbgs() << "@" << GV.getName() << " has type " << *TypedTy << "\n");
DeducedTypes[&GV] = TypedTy;
}
static Type *getParamType(const AttributeList &AL, unsigned ArgNo) {
if (Type *Ty = AL.getParamByValType(ArgNo))
return Ty;
if (Type *Ty = AL.getParamStructRetType(ArgNo))
return Ty;
if (Type *Ty = AL.getParamElementType(ArgNo))
return Ty;
if (Type *Ty = AL.getParamInAllocaType(ArgNo))
return Ty;
if (Type *Ty = AL.getParamPreallocatedType(ArgNo))
return Ty;
return nullptr;
}
void SPIRVTypeScavenger::deduceFunctionType(Function &F) {
// Start by constructing a basic function type that replaces all pointer
// types in arguments (and the return type) with type variables. We may
// resolve those type variables almost immediately, but this is a starting
// point.
FunctionType *FuncTy = F.getFunctionType();
if (hasPointerType(FuncTy))
FuncTy = cast<FunctionType>(allocateTypeVariable(F.getFunctionType()));
DeducedTypes[&F] = TypedPointerType::get(FuncTy, F.getAddressSpace());
auto TypeArgument = [&](Argument *Arg, Type *T) {
[[maybe_unused]] bool Successful =
unifyType(FuncTy->getParamType(Arg->getArgNo()), T);
assert(Successful && "Unification of argument type failed?");
LLVM_DEBUG(dbgs() << " Arg " << *Arg << " is known to be " << *T << "\n");
DeducedTypes[Arg] = T;
};
// Gather a list of arguments that have unresolved type variables.
SmallVector<Argument *, 8> PointerArgs;
for (Argument &Arg : F.args()) {
DeducedTypes[&Arg] = FuncTy->getParamType(Arg.getArgNo());
if (hasPointerType(Arg.getType()))
PointerArgs.push_back(&Arg);
}
// Get any arguments from attributes where possible.
for (Argument *Arg : PointerArgs) {
Type *Ty = getParamType(F.getAttributes(), Arg->getArgNo());
if (Ty)
TypeArgument(Arg, TypedPointerType::get(
Ty, Arg->getType()->getPointerAddressSpace()));
}
// The first non-sret argument of block_invoke functions is the block capture
// struct, which should be passed as an i8*.
static const Regex BlockInvokeRegex(
"^(__.+)?_block_invoke(_[0-9]+)?(_kernel)?$");
if (BlockInvokeRegex.match(F.getName())) {
for (Argument *Arg : PointerArgs) {
if (!Arg->hasAttribute(Attribute::StructRet)) {
TypeArgument(Arg, getUnknownTyped(Arg->getType()));
break;
}
}
}
// If the function is a mangled name, try to recover types from the Itanium
// name mangling. Do this only for function types that without bodies, where
// existing code can propagate types to the parameters.
// TODO: Investigate if target extension types and the specially-handled
// SPIR-V intrinsics renders this code unnecessary.
if (F.isDeclaration() && F.getName().starts_with("_Z")) {
if (F.getName().starts_with("_Z")) {
SmallVector<Type *, 8> ParamTypes;
if (getParameterTypes(&F, ParamTypes)) {
for (Argument *Arg : PointerArgs) {
if (auto *Ty =
dyn_cast<TypedPointerType>(ParamTypes[Arg->getArgNo()]))
TypeArgument(Arg, Ty);
}
}
}
}
LLVM_DEBUG(dbgs() << "Type of @" << F.getName() << " is "
<< *substituteTypeVariables(FuncTy) << "\n");
}
/// Certain constant types (null, undef, and poison) will get their type from
/// the use of the constant. We discover the type of the use by inserting a
/// synthetic bitcast instruction before the use. For these types, we need to
/// have special handling in a few places, and this indicates that it needs to
/// be done.
static bool doesNotImplyType(Value *V) {
return isa<ConstantPointerNull>(V) || isa<UndefValue>(V);
}
Type *SPIRVTypeScavenger::getTypeAfterRules(Value *V) {
auto *Ty = V->getType();
if (!hasPointerType(Ty))
return Ty;
// Don't try to store null, undef, or poison in our type map. We'll call these
// i8* by default; if any use has a different type, a bitcast will be added
// later.
if (doesNotImplyType(V)) {
return getUnknownTyped(Ty);
}
// Check if we've already deduced a type for the value.
Type *KnownType = DeducedTypes.lookup(V);
if (KnownType)
return substituteTypeVariables(KnownType);
assert(
!isa<GlobalValue>(V) && !isa<Argument>(V) &&
"Globals and arguments must be fully handled before calling this method");
// All constants will have their pointer types handled as i8*.
if (!isa<Instruction>(V))
return getUnknownTyped(Ty);
assert(!is_contained(VisitStack, V) && "Found cycle in type scavenger");
VisitStack.push_back(V);
// Try to propagate from type rules constraining the return value.
SmallVector<TypeRule, 4> TypeRules;
getTypeRules(*cast<Instruction>(V), TypeRules);
for (TypeRule &Rule : TypeRules) {
if (Rule.OpNo != RETURN_OPERAND)
continue;
// Get the target type from the rule. If it comes from an operand,
// recursively attempt to find the type from the operand (but avoid any
// cycles).
Type *TargetTy;
if (auto *UsedTy = dyn_cast<Type *>(Rule.Target)) {
TargetTy = allocateTypeVariable(UsedTy);
} else {
Value *Arg = cast<Use *>(Rule.Target)->get();
if (is_contained(VisitStack, Arg))
continue;
Value *Source = cast<Use *>(Rule.Target)->get();
// If the source argument is null, undef, or poison, then move on to
// another rule to give better type hints.
if (doesNotImplyType(Source))
continue;
TargetTy = substituteTypeVariables(getTypeAfterRules(Source));
}
// If the argument is a null pointer, try another operand instead.
if (!TargetTy)
continue;
KnownType =
adjustIndirect(Ty, Rule.LhsIndirect, TargetTy, Rule.RhsIndirect);
// Make sure that the type is consistent with the type format of Ty.
if (!unifyType(Ty, KnownType))
KnownType = nullptr;
break;
}
// If we still haven't gotten a type at this point, just construct a new type
// variable and rely on later uses to recover the type.
if (!KnownType) {
LLVM_DEBUG(dbgs() << *V << " matched no typing rules\n");
KnownType = allocateTypeVariable(Ty);
}
DeducedTypes[V] = KnownType;
VisitStack.pop_back();
LLVM_DEBUG(dbgs() << "Assigned type " << *KnownType << " to " << *V << "\n");
return KnownType;
}
void SPIRVTypeScavenger::getTypeRules(Instruction &I,
SmallVectorImpl<TypeRule> &TypeRules) {
auto GetAssociatedTypeVariable = [&](Type *T) {
Type *&TypeVar = AssociatedTypeVariables[&I];
if (TypeVar)
return TypeVar;
return TypeVar = allocateTypeVariable(T);
};
if (auto *GEP = dyn_cast<GetElementPtrInst>(&I)) {
Type *GepTy = GEP->getSourceElementType();
Type *ReturnTy = GEP->getResultElementType();
if (hasPointerType(GepTy)) {
GepTy = GetAssociatedTypeVariable(GepTy);
// Iterate the indices to find the return type, based on the version of
// the type using type variables and typed pointer types instead.
ReturnTy = GepTy;
for (Use &U : drop_begin(GEP->indices()))
ReturnTy = GetElementPtrInst::getTypeAtIndex(ReturnTy, U.get());
} else {
// It's possible that ReturnTy might be a type containing a ptr. However,
// if we aren't typing the struct type specifically, then this type is
// going to be coerced by the writer to i8*, so don't allocate any type
// variables for it.
ReturnTy = getUnknownTyped(ReturnTy);
}
TypeRules.push_back(TypeRule::pointsTo(I, 0, GepTy));
TypeRules.push_back(TypeRule::returnsPointerTo(ReturnTy));
} else if (isa<LoadInst>(&I)) {
TypeRules.push_back(
TypeRule::pointsToReturn(I, LoadInst::getPointerOperandIndex()));
} else if (isa<StoreInst>(&I)) {
TypeRules.push_back(
TypeRule::pointsTo(I, StoreInst::getPointerOperandIndex(), 0U));
} else if (auto *AI = dyn_cast<AtomicCmpXchgInst>(&I)) {
TypeRules.push_back(
TypeRule::pointsTo(I, AtomicCmpXchgInst::getPointerOperandIndex(), 1));
if (hasPointerType(AI->getCompareOperand()->getType()))
TypeRules.push_back(TypeRule::is(I, 1, 2));
} else if (auto *AI = dyn_cast<AtomicRMWInst>(&I)) {
TypeRules.push_back(
TypeRule::pointsTo(I, AtomicRMWInst::getPointerOperandIndex(), 1));
if (hasPointerType(AI->getValOperand()->getType()))
TypeRules.push_back(TypeRule::propagates(I, 1));
} else if (auto *AI = dyn_cast<AllocaInst>(&I)) {
TypeRules.push_back(TypeRule::returnsPointerTo(AI->getAllocatedType()));
} else if (auto *CI = dyn_cast<ICmpInst>(&I)) {
// icmp can compare pointers. If it isn't, ignore the instruction.
if (!hasPointerType(CI->getOperand(0)->getType()))
return;
// The two pointer operands should have the same type.
TypeRules.push_back(TypeRule::is(I, 1, 0));
} else if (auto *SI = dyn_cast<SelectInst>(&I)) {
if (!hasPointerType(SI->getType()))
return;
// Both selected values should have the same type as the result.
TypeRules.push_back(TypeRule::propagates(I, 1));
TypeRules.push_back(TypeRule::propagates(I, 2));
} else if (auto *Phi = dyn_cast<PHINode>(&I)) {
if (!hasPointerType(Phi->getType()))
return;
for (Use &U : Phi->incoming_values()) {
TypeRules.push_back(TypeRule::propagates(U));
}
} else if (isa<FreezeInst>(&I)) {
if (!hasPointerType(I.getType()))
return;
TypeRules.push_back(TypeRule::propagates(I, 0));
} else if (auto *AS = dyn_cast<AddrSpaceCastInst>(&I)) {
TypeRules.push_back(TypeRule::propagatesIndirect(*AS, 0));
} else if (isa<ReturnInst>(&I)) {
if (!hasPointerType(I.getFunction()->getReturnType()))
return;
Type *ExpectedTy = getFunctionType(I.getFunction())->getReturnType();
TypeRules.push_back(TypeRule::is(0, ExpectedTy));
} else if (auto *CB = dyn_cast<CallBase>(&I)) {
// If we have an identified function for the call instruction, map the
// arguments we pass in to the argument requirements of the function.
if (Function *F = CB->getCalledFunction()) {
if (!F->isDeclaration() || !typeIntrinsicCall(*CB, TypeRules)) {
typeFunctionParams(*CB, getFunctionType(F), 0, true, TypeRules);
}
} else {
// In the case of function pointers, we need to also assert the function
// type of the call instruction itself.
// In the case of inline assembly, the inline asm type is typed as if all
// ptr parameters are i8* by the writer, so force all pointer to those
// types here.
FunctionType *FT =
cast<FunctionType>(GetAssociatedTypeVariable(CB->getFunctionType()));
if (isa<InlineAsm>(CB->getCalledOperand()))
FT = cast<FunctionType>(getUnknownTyped(CB->getFunctionType()));
else
TypeRules.push_back(TypeRule::pointsTo(CB->getCalledOperandUse(), FT));
typeFunctionParams(*CB, FT, 0, true, TypeRules);
}
} else if (isa<ExtractElementInst>(&I)) {
if (!hasPointerType(I.getType()))
return;
TypeRules.push_back(TypeRule::propagatesIndirect(I, 0));
} else if (isa<InsertElementInst>(&I)) {
if (!hasPointerType(I.getType()))
return;
TypeRules.push_back(TypeRule::propagatesIndirect(I, 0));
TypeRules.push_back(TypeRule::propagatesIndirect(I, 1));
} else if (isa<ShuffleVectorInst>(&I)) {
if (!hasPointerType(I.getType()))
return;
TypeRules.push_back(TypeRule::propagatesIndirect(I, 0));
TypeRules.push_back(TypeRule::propagatesIndirect(I, 1));
}
// TODO: Handle insertvalue, extractvalue that work with pointers (requires
// literal struct support)
}
std::pair<Use &, Type *>
SPIRVTypeScavenger::getTypeCheck(Instruction &I, const TypeRule &Rule) {
auto MakeCheck = [&](Use &U, bool UIndirect, Type *Ty, bool TIndirect) {
return std::pair<Use &, Type *>(
U, adjustIndirect(U->getType(), UIndirect, Ty, TIndirect));
};
bool LIndirect = Rule.LhsIndirect, RIndirect = Rule.RhsIndirect;
// If we have typeof(return) == typeof(operand) check, reverse the check for
// typing rules.
if (Rule.OpNo == RETURN_OPERAND) {
Use &U = *cast<Use *>(Rule.Target);
Type *Ty = getTypeAfterRules(&I);
return MakeCheck(U, RIndirect, Ty, LIndirect);
}
Type *TargetTy;
if (auto *UsedTy = dyn_cast<Type *>(Rule.Target)) {
TargetTy = UsedTy;
} else {
TargetTy = getTypeAfterRules(cast<Use *>(Rule.Target)->get());
}
Use &U = I.getOperandUse(Rule.OpNo);
return MakeCheck(U, LIndirect, TargetTy, RIndirect);
}
void SPIRVTypeScavenger::correctUseTypes(Instruction &I) {
// This represents the types of all pointer-valued operands of the
// instruction.
SmallVector<TypeRule, 4> TypeRules;
getTypeRules(I, TypeRules);
if (!TypeRules.empty())
LLVM_DEBUG(dbgs() << "Typing uses of " << I << "\n");
// Now that we've collected all the pointer-valued operands in the
// instruction, go through and insert bitcasts for any operands that have the
// wrong type, fix any deferred types whose types are now known, and merge any
// deferred types that need to have the same type.
IRBuilder<NoFolder> Builder(&I);
for (auto &Rule : TypeRules) {
// No type checking needs to happen for a returns-type rule, since there's
// no operands of this instruction to check.
if (Rule.OpNo == RETURN_OPERAND && isa<Type *>(Rule.Target))
continue;
auto [U, UsedTy] = getTypeCheck(I, Rule);
Type *SourceTy = getTypeAfterRules(U);