-
Notifications
You must be signed in to change notification settings - Fork 221
/
SPIRVToLLVMDbgTran.cpp
1850 lines (1674 loc) · 77.9 KB
/
SPIRVToLLVMDbgTran.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
//===- SPIRVToLLVMDbgTran.cpp - Converts debug info to LLVM -----*- C++ -*-===//
//
// The LLVM/SPIR-V Translator
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
// Copyright (c) 2018 Intel Corporation. All rights reserved.
//
// 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 Intel Corporation, 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 translation of debug info from SPIR-V to LLVM metadata
//
//===----------------------------------------------------------------------===//
#include "SPIRVToLLVMDbgTran.h"
#include "SPIRV.debug.h"
#include "SPIRVEntry.h"
#include "SPIRVFunction.h"
#include "SPIRVInstruction.h"
#include "SPIRVInternal.h"
#include "SPIRVReader.h"
#include "SPIRVType.h"
#include "llvm/ADT/StringExtras.h"
#include "llvm/IR/DIBuilder.h"
#include "llvm/IR/DebugProgramInstruction.h"
#include "llvm/IR/IntrinsicInst.h"
#include "llvm/IR/Module.h"
using namespace SPIRVDebug::Operand;
namespace SPIRV {
static uint64_t getDerivedSizeInBits(const DIType *Ty) {
if (auto Size = Ty->getSizeInBits())
return Size;
if (auto *DT = llvm::dyn_cast<const llvm::DIDerivedType>(Ty))
if (auto *BT = llvm::dyn_cast<const llvm::DIType>(DT->getRawBaseType()))
return getDerivedSizeInBits(BT);
return 0;
}
SPIRVToLLVMDbgTran::SPIRVToLLVMDbgTran(SPIRVModule *TBM, Module *TM,
SPIRVToLLVM *Reader)
: BM(TBM), M(TM), SPIRVReader(Reader) {
Enable = BM->hasDebugInfo();
}
void SPIRVToLLVMDbgTran::addDbgInfoVersion() {
if (!Enable)
return;
M->addModuleFlag(Module::Warning, "Debug Info Version",
DEBUG_METADATA_VERSION);
}
DIFile *
SPIRVToLLVMDbgTran::getDIFile(const std::string &FileName,
std::optional<DIFile::ChecksumInfo<StringRef>> CS,
std::optional<StringRef> Source) {
return getOrInsert(FileMap, FileName, [=]() {
SplitFileName Split(FileName);
// Use the first builder from the map to crete DIFile since it's
// relations with other debug metadata is not going through DICompileUnit
if (!Split.BaseName.empty())
return BuilderMap.begin()->second->createFile(Split.BaseName, Split.Path,
CS, Source);
return static_cast<DIFile *>(nullptr);
});
}
SPIRVExtInst *SPIRVToLLVMDbgTran::getDbgInst(const SPIRVId Id) {
SPIRVEntry *E = BM->getEntry(Id);
if (isa<OpExtInst>(E)) {
SPIRVExtInst *EI = static_cast<SPIRVExtInst *>(E);
if (EI->getExtSetKind() == SPIRV::SPIRVEIS_Debug ||
EI->getExtSetKind() == SPIRV::SPIRVEIS_OpenCL_DebugInfo_100 ||
EI->getExtSetKind() ==
SPIRV::SPIRVEIS_NonSemantic_Shader_DebugInfo_100 ||
EI->getExtSetKind() == SPIRV::SPIRVEIS_NonSemantic_Shader_DebugInfo_200)
return EI;
}
return nullptr;
}
// Check if module is generated using NonSemantic.Shader.DebugInfo.XXX
// extended instruction set
inline bool isNonSemanticDebugInfo(const SPIRVExtInstSetKind Kind) {
return (Kind == SPIRVEIS_NonSemantic_Shader_DebugInfo_100 ||
Kind == SPIRVEIS_NonSemantic_Shader_DebugInfo_200);
}
// Get integer parameter of debug instruction considering whether it's
// Literal or <id> of OpConstant instruction depending on DebugInfo
// extended instruction set kind
SPIRVWord
SPIRVToLLVMDbgTran::getConstantValueOrLiteral(const std::vector<SPIRVWord> &Ops,
const SPIRVWord Idx,
const SPIRVExtInstSetKind Kind) {
if (!isNonSemanticDebugInfo(Kind))
return Ops[Idx];
SPIRVValue *SPVConst = BM->get<SPIRVValue>(Ops[Idx]);
assert(isConstantOpCode(SPVConst->getOpCode()) &&
"NonSemantic Debug instruction's parameters must be OpConstant");
ConstantInt *Const =
cast<ConstantInt>(SPIRVReader->transValue(SPVConst, nullptr, nullptr));
return Const->getZExtValue();
}
const std::string &SPIRVToLLVMDbgTran::getString(const SPIRVId Id) {
SPIRVString *String = BM->get<SPIRVString>(Id);
assert(String && "Invalid string");
return String->getStr();
}
const std::string
SPIRVToLLVMDbgTran::getStringSourceContinued(const SPIRVId Id,
SPIRVExtInst *DebugInst) {
if (!isValidId(Id) || getDbgInst<SPIRVDebug::DebugInfoNone>(Id))
return "";
std::string Str = BM->get<SPIRVString>(Id)->getStr();
using namespace SPIRVDebug::Operand::SourceContinued;
for (auto *I : DebugInst->getContinuedInstructions()) {
std::string TmpStr =
BM->get<SPIRVString>(I->getArguments()[TextIdx])->getStr();
Str.append(TmpStr);
}
return Str;
}
void SPIRVToLLVMDbgTran::transDbgInfo(const SPIRVValue *SV, Value *V) {
// Constant samplers and composites do not have a corresponding
// SPIRVInstruction, but they may be mapped to an LLVM Instruction.
if (SV->getOpCode() == OpConstantSampler ||
SV->getOpCode() == OpConstantComposite)
return;
if (Instruction *I = dyn_cast<Instruction>(V)) {
const SPIRVInstruction *SI = static_cast<const SPIRVInstruction *>(SV);
I->setDebugLoc(transDebugScope(SI));
}
}
DIScope *SPIRVToLLVMDbgTran::getScope(const SPIRVEntry *ScopeInst) {
if (ScopeInst->getOpCode() == OpString)
return getDIFile(static_cast<const SPIRVString *>(ScopeInst)->getStr());
return transDebugInst<DIScope>(static_cast<const SPIRVExtInst *>(ScopeInst));
}
void SPIRVToLLVMDbgTran::appendToSourceLangLiteral(DICompileUnit *CompileUnit,
SPIRVWord SourceLang) {
if (!M->getModuleFlag("Source Lang Literal")) {
M->addModuleFlag(llvm::Module::Warning, "Source Lang Literal",
MDTuple::get(M->getContext(), {}));
}
auto *SourceLangLiteral =
dyn_cast<MDTuple>(M->getModuleFlag("Source Lang Literal"));
// Copy old content
SmallVector<Metadata *, 4> Nodes;
for (auto &Node : SourceLangLiteral->operands()) {
Nodes.push_back(Node);
}
// Add new entry
Nodes.push_back(MDTuple::get(
M->getContext(), SmallVector<Metadata *, 2>{
CompileUnit,
ConstantAsMetadata::get(ConstantInt::get(
Type::getInt32Ty(M->getContext()), SourceLang)),
}));
// Update
M->setModuleFlag(llvm::Module::Warning, "Source Lang Literal",
MDTuple::get(M->getContext(), Nodes));
}
DICompileUnit *
SPIRVToLLVMDbgTran::transCompilationUnit(const SPIRVExtInst *DebugInst,
const std::string CompilerVersion,
const std::string Flags) {
// Do nothing in case we have already translated the CU (e.g. during
// DebugEntryPoint translation)
if (BuilderMap[DebugInst->getId()])
return cast<DICompileUnit>(DebugInstCache[DebugInst]);
const SPIRVWordVec &Ops = DebugInst->getArguments();
using namespace SPIRVDebug::Operand::CompilationUnit;
assert(Ops.size() >= MinOperandCount && "Invalid number of operands");
// We must preserve only one Dwarf version module level metadata
// UpgradeDebugInfo from llvm/lib/IR/AutoUpgrade.cpp has already done all
// work for us during linking stage leaving a single Dwarf version in the
// module
if (!M->getModuleFlag("Dwarf Version")) {
SPIRVWord DWARFVersion = getConstantValueOrLiteral(
Ops, DWARFVersionIdx, DebugInst->getExtSetKind());
M->addModuleFlag(llvm::Module::Max, "Dwarf Version", DWARFVersion);
}
SPIRVWord SourceLang =
getConstantValueOrLiteral(Ops, LanguageIdx, DebugInst->getExtSetKind());
SPIRVWord OriginalSourceLang = SourceLang;
bool InvalidSourceLang = false;
if (DebugInst->getExtSetKind() == SPIRVEIS_NonSemantic_Shader_DebugInfo_200) {
SourceLang = convertSPIRVSourceLangToDWARFNonSemanticDbgInfo(SourceLang);
} else if (isSPIRVSourceLangValid(SourceLang)) {
SourceLang = convertSPIRVSourceLangToDWARF(SourceLang);
} else {
// Some SPIR-V producers generate invalid source language value. In such
// case the original value should be preserved in "Source Lang Literal"
// module flag for later use by LLVM IR consumers.
SourceLang = dwarf::DW_LANG_OpenCL;
InvalidSourceLang = true;
}
BuilderMap[DebugInst->getId()] = std::make_unique<DIBuilder>(*M);
assert(BuilderMap.size() != 0 && "No debug compile units");
if (isNonSemanticDebugInfo(DebugInst->getExtSetKind())) {
// Only initialize once
if (BuilderMap.size() == 1)
setBuildIdentifierAndStoragePath();
// This assertion is a guard/reminder to update the Producer argument to
// createCompileUnit() if a new DebugInfo type is ever created
assert(DebugInst->getExtSetKind() ==
SPIRVEIS_NonSemantic_Shader_DebugInfo_100 ||
DebugInst->getExtSetKind() ==
SPIRVEIS_NonSemantic_Shader_DebugInfo_200);
auto *CompileUnit = BuilderMap[DebugInst->getId()]->createCompileUnit(
SourceLang, getFile(Ops[SourceIdx]),
DebugInst->getExtSetKind() == SPIRVEIS_NonSemantic_Shader_DebugInfo_100
? CompilerVersion
: getString(Ops[ProducerIdx]),
false, Flags, 0, StoragePath,
DICompileUnit::DebugEmissionKind::FullDebug, BuildIdentifier);
if (InvalidSourceLang) {
appendToSourceLangLiteral(CompileUnit, OriginalSourceLang);
}
return CompileUnit;
}
// TODO: Remove this workaround once we switch to NonSemantic.Shader.* debug
// info by default
auto Producer = findModuleProducer();
auto *CompileUnit = BuilderMap[DebugInst->getId()]->createCompileUnit(
SourceLang, getFile(Ops[SourceIdx]), Producer, false, Flags, 0);
if (InvalidSourceLang) {
appendToSourceLangLiteral(CompileUnit, OriginalSourceLang);
}
return CompileUnit;
}
DIBasicType *SPIRVToLLVMDbgTran::transTypeBasic(const SPIRVExtInst *DebugInst) {
using namespace SPIRVDebug::Operand::TypeBasic;
const SPIRVWordVec &Ops = DebugInst->getArguments();
assert((Ops.size() == OperandCountOCL ||
Ops.size() == OperandCountNonSemantic) &&
"Invalid number of operands");
StringRef Name = getString(Ops[NameIdx]);
auto Tag = static_cast<SPIRVDebug::EncodingTag>(
getConstantValueOrLiteral(Ops, EncodingIdx, DebugInst->getExtSetKind()));
unsigned Encoding = SPIRV::DbgEncodingMap::rmap(Tag);
if (Encoding == 0)
return getDIBuilder(DebugInst).createUnspecifiedType(Name);
uint64_t Size = BM->get<SPIRVConstant>(Ops[SizeIdx])->getZExtIntValue();
return getDIBuilder(DebugInst).createBasicType(Name, Size, Encoding);
}
DIDerivedType *
SPIRVToLLVMDbgTran::transTypeQualifier(const SPIRVExtInst *DebugInst) {
using namespace SPIRVDebug::Operand::TypeQualifier;
const SPIRVWordVec &Ops = DebugInst->getArguments();
assert(Ops.size() == OperandCount && "Invalid number of operands");
DIType *BaseTy =
transDebugInst<DIType>(BM->get<SPIRVExtInst>(Ops[BaseTypeIdx]));
SPIRVWord Tag = SPIRV::DbgTypeQulifierMap::rmap(
static_cast<SPIRVDebug::TypeQualifierTag>(getConstantValueOrLiteral(
Ops, QualifierIdx, DebugInst->getExtSetKind())));
return getDIBuilder(DebugInst).createQualifiedType(Tag, BaseTy);
}
DIType *SPIRVToLLVMDbgTran::transTypePointer(const SPIRVExtInst *DebugInst) {
using namespace SPIRVDebug::Operand::TypePointer;
const SPIRVWordVec &Ops = DebugInst->getArguments();
assert(Ops.size() == OperandCount && "Invalid number of operands");
DIType *PointeeTy = nullptr;
if (BM->getEntry(Ops[BaseTypeIdx])->getOpCode() != OpTypeVoid)
PointeeTy = transDebugInst<DIType>(BM->get<SPIRVExtInst>(Ops[BaseTypeIdx]));
std::optional<unsigned> AS;
SPIRVWord SC = getConstantValueOrLiteral(Ops, StorageClassIdx,
DebugInst->getExtSetKind());
if (SC != ~0U) // all ones denote no address space
AS = SPIRSPIRVAddrSpaceMap::rmap(static_cast<SPIRVStorageClassKind>(SC));
DIType *Ty;
SPIRVWord Flags =
getConstantValueOrLiteral(Ops, FlagsIdx, DebugInst->getExtSetKind());
if (Flags & SPIRVDebug::FlagIsLValueReference)
Ty = getDIBuilder(DebugInst).createReferenceType(
dwarf::DW_TAG_reference_type, PointeeTy, 0, 0, AS);
else if (Flags & SPIRVDebug::FlagIsRValueReference)
Ty = getDIBuilder(DebugInst).createReferenceType(
dwarf::DW_TAG_rvalue_reference_type, PointeeTy, 0, 0, AS);
else
Ty = getDIBuilder(DebugInst).createPointerType(
PointeeTy, BM->getAddressingModel() * 32, 0, AS);
if (Flags & SPIRVDebug::FlagIsObjectPointer)
Ty = getDIBuilder(DebugInst).createObjectPointerType(Ty);
else if (Flags & SPIRVDebug::FlagIsArtificial)
Ty = getDIBuilder(DebugInst).createArtificialType(Ty);
return Ty;
}
DICompositeType *
SPIRVToLLVMDbgTran::transTypeArray(const SPIRVExtInst *DebugInst) {
if (DebugInst->getExtSetKind() == SPIRVEIS_NonSemantic_Shader_DebugInfo_200)
return transTypeArrayNonSemantic(DebugInst);
// TODO: figure out better naming for transTypeArrayOpenCL since
// it also handles SPIRVEIS_NonSemantic_Shader_DebugInfo_100.
// Also to consider separating OpenCL from NonSemantic, as OpenCL has several
// workarounds
return transTypeArrayOpenCL(DebugInst);
}
DICompositeType *
SPIRVToLLVMDbgTran::transTypeArrayOpenCL(const SPIRVExtInst *DebugInst) {
using namespace SPIRVDebug::Operand::TypeArray;
const SPIRVWordVec &Ops = DebugInst->getArguments();
assert(Ops.size() >= MinOperandCount && "Invalid number of operands");
DIType *BaseTy =
transNonNullDebugType(BM->get<SPIRVExtInst>(Ops[BaseTypeIdx]));
size_t TotalCount = 1;
SmallVector<llvm::Metadata *, 8> Subscripts;
// Ops looks like: { BaseType, count1|upperBound1, count2|upperBound2, ...,
// countN|upperBoundN, lowerBound1, lowerBound2, ..., lowerBoundN }
for (size_t I = ComponentCountIdx, E = Ops.size() / 2 + 1; I < E; ++I) {
if (auto *LocalVar = getDbgInst<SPIRVDebug::LocalVariable>(Ops[I])) {
auto *UpperBound = transDebugInst<DILocalVariable>(LocalVar);
SPIRVConstant *C = BM->get<SPIRVConstant>(Ops[Ops.size() / 2 + I]);
int64_t ConstantAsInt = static_cast<int64_t>(C->getZExtIntValue());
auto *LowerBound = ConstantAsMetadata::get(
ConstantInt::get(M->getContext(), APInt(64, ConstantAsInt)));
Subscripts.push_back(getDIBuilder(DebugInst).getOrCreateSubrange(
nullptr, LowerBound, UpperBound, nullptr));
continue;
}
if (const SPIRVExtInst * ExprUB, *ExprLB;
(ExprUB = getDbgInst<SPIRVDebug::Expression>(Ops[I])) &&
(ExprLB =
getDbgInst<SPIRVDebug::Expression>(Ops[Ops.size() / 2 + I]))) {
auto *UpperBound = transDebugInst<DIExpression>(ExprUB);
auto *LowerBound = transDebugInst<DIExpression>(ExprLB);
Subscripts.push_back(getDIBuilder(DebugInst).getOrCreateSubrange(
nullptr, LowerBound, UpperBound, nullptr));
continue;
}
if (!getDbgInst<SPIRVDebug::DebugInfoNone>(Ops[I])) {
SPIRVConstant *C = BM->get<SPIRVConstant>(Ops[I]);
int64_t Count = static_cast<int64_t>(C->getZExtIntValue());
// If the SPIR-V file was generated by an older version of the translator,
// Ops may not contain the LowerBound
if (Ops.size() / 2 + I < Ops.size()) {
C = BM->get<SPIRVConstant>(Ops[Ops.size() / 2 + I]);
int64_t LowerBound = static_cast<int64_t>(C->getZExtIntValue());
Subscripts.push_back(
getDIBuilder(DebugInst).getOrCreateSubrange(LowerBound, Count));
} else {
auto *CountAsMD = ConstantAsMetadata::get(
ConstantInt::get(M->getContext(), APInt(64, Count)));
Subscripts.push_back(getDIBuilder(DebugInst).getOrCreateSubrange(
CountAsMD, nullptr, nullptr, nullptr));
}
// Count = -1 means that the array is empty
TotalCount *= Count > 0 ? static_cast<size_t>(Count) : 0;
continue;
}
}
DINodeArray SubscriptArray =
getDIBuilder(DebugInst).getOrCreateArray(Subscripts);
size_t Size = getDerivedSizeInBits(BaseTy) * TotalCount;
return getDIBuilder(DebugInst).createArrayType(Size, 0 /*align*/, BaseTy,
SubscriptArray);
}
DICompositeType *
SPIRVToLLVMDbgTran::transTypeArrayNonSemantic(const SPIRVExtInst *DebugInst) {
using namespace SPIRVDebug::Operand::TypeArray;
const SPIRVWordVec &Ops = DebugInst->getArguments();
assert(Ops.size() >= MinOperandCount && "Invalid number of operands");
DIType *BaseTy =
transNonNullDebugType(BM->get<SPIRVExtInst>(Ops[BaseTypeIdx]));
size_t TotalCount = 1;
SmallVector<llvm::Metadata *, 8> Subscripts;
if (DebugInst->getExtOp() == SPIRVDebug::TypeArray) {
for (size_t I = SubrangesIdx; I < Ops.size(); ++I) {
auto *SR = transDebugInst<DISubrange>(BM->get<SPIRVExtInst>(Ops[I]));
if (auto *Count = SR->getCount().get<ConstantInt *>())
TotalCount *= Count->getSExtValue() > 0 ? Count->getSExtValue() : 0;
Subscripts.push_back(SR);
}
}
DINodeArray SubscriptArray =
getDIBuilder(DebugInst).getOrCreateArray(Subscripts);
size_t Size = getDerivedSizeInBits(BaseTy) * TotalCount;
return getDIBuilder(DebugInst).createArrayType(Size, 0 /*align*/, BaseTy,
SubscriptArray);
}
DICompositeType *
SPIRVToLLVMDbgTran::transTypeArrayDynamic(const SPIRVExtInst *DebugInst) {
using namespace SPIRVDebug::Operand::TypeArrayDynamic;
const SPIRVWordVec &Ops = DebugInst->getArguments();
assert(Ops.size() >= MinOperandCount && "Invalid number of operands");
DIType *BaseTy =
transNonNullDebugType(BM->get<SPIRVExtInst>(Ops[BaseTypeIdx]));
size_t TotalCount = 1;
SmallVector<llvm::Metadata *, 8> Subscripts;
for (size_t I = SubrangesIdx; I < Ops.size(); ++I) {
auto *SR = transDebugInst<DISubrange>(BM->get<SPIRVExtInst>(Ops[I]));
if (auto *Count = SR->getCount().get<ConstantInt *>())
TotalCount *= Count->getSExtValue() > 0 ? Count->getSExtValue() : 0;
Subscripts.push_back(SR);
}
DINodeArray SubscriptArray =
getDIBuilder(DebugInst).getOrCreateArray(Subscripts);
size_t Size = getDerivedSizeInBits(BaseTy) * TotalCount;
auto TransOperand =
[&](SPIRVWord Idx) -> PointerUnion<DIExpression *, DIVariable *> {
if (!getDbgInst<SPIRVDebug::DebugInfoNone>(Ops[Idx])) {
if (const auto *GV = getDbgInst<SPIRVDebug::GlobalVariable>(Ops[Idx]))
return transDebugInst<DIGlobalVariable>(GV);
if (const auto *LV = getDbgInst<SPIRVDebug::LocalVariable>(Ops[Idx]))
return transDebugInst<DILocalVariable>(LV);
if (const auto *DIExpr = getDbgInst<SPIRVDebug::Expression>(Ops[Idx]))
return transDebugInst<DIExpression>(DIExpr);
}
return nullptr;
};
PointerUnion<DIExpression *, DIVariable *> DataLocation =
TransOperand(DataLocationIdx);
PointerUnion<DIExpression *, DIVariable *> Associated =
TransOperand(AssociatedIdx);
PointerUnion<DIExpression *, DIVariable *> Allocated =
TransOperand(AllocatedIdx);
PointerUnion<DIExpression *, DIVariable *> Rank = TransOperand(RankIdx);
return getDIBuilder(DebugInst).createArrayType(Size, 0 /*align*/, BaseTy,
SubscriptArray, DataLocation,
Associated, Allocated, Rank);
}
DICompositeType *
SPIRVToLLVMDbgTran::transTypeVector(const SPIRVExtInst *DebugInst) {
using namespace SPIRVDebug::Operand::TypeVector;
const SPIRVWordVec &Ops = DebugInst->getArguments();
assert(Ops.size() >= MinOperandCount && "Invalid number of operands");
DIType *BaseTy =
transNonNullDebugType(BM->get<SPIRVExtInst>(Ops[BaseTypeIdx]));
SPIRVWord Count = getConstantValueOrLiteral(Ops, ComponentCountIdx,
DebugInst->getExtSetKind());
// Round up to a power of two.
// OpenCL/SYCL 3-element vectors
// occupy the same amount of memory as 4-element vectors
// Clang rounds up the memory size of vectors to a power of 2.
// Vulkan allows vec3 to have a memory size of 12, but in RenderDoc memory
// size is not derived from debug info.
uint64_t Size = getDerivedSizeInBits(BaseTy) * llvm::bit_ceil(Count);
SmallVector<llvm::Metadata *, 8> Subscripts;
Subscripts.push_back(getDIBuilder(DebugInst).getOrCreateSubrange(0, Count));
DINodeArray SubscriptArray =
getDIBuilder(DebugInst).getOrCreateArray(Subscripts);
return getDIBuilder(DebugInst).createVectorType(Size, 0 /*align*/, BaseTy,
SubscriptArray);
}
DICompositeType *
SPIRVToLLVMDbgTran::transTypeComposite(const SPIRVExtInst *DebugInst) {
using namespace SPIRVDebug::Operand::TypeComposite;
const SPIRVWordVec &Ops = DebugInst->getArguments();
assert(Ops.size() >= MinOperandCount && "Invalid number of operands");
StringRef Name = getString(Ops[NameIdx]);
DIFile *File = getFile(Ops[SourceIdx]);
SPIRVWord LineNo =
getConstantValueOrLiteral(Ops, LineIdx, DebugInst->getExtSetKind());
DIScope *ParentScope = getScope(BM->getEntry(Ops[ParentIdx]));
uint64_t Size = 0;
SPIRVEntry *SizeEntry = BM->getEntry(Ops[SizeIdx]);
if (!(SizeEntry->isExtInst(SPIRVEIS_Debug, SPIRVDebug::DebugInfoNone) ||
SizeEntry->isExtInst(SPIRVEIS_OpenCL_DebugInfo_100,
SPIRVDebug::DebugInfoNone) ||
SizeEntry->isExtInst(SPIRVEIS_NonSemantic_Shader_DebugInfo_200,
SPIRVDebug::DebugInfoNone))) {
Size = BM->get<SPIRVConstant>(Ops[SizeIdx])->getZExtIntValue();
}
uint64_t Align = 0;
DIType *DerivedFrom = nullptr;
StringRef Identifier;
SPIRVEntry *UniqId = BM->get<SPIRVEntry>(Ops[LinkageNameIdx]);
if (UniqId->getOpCode() == OpString)
Identifier = static_cast<SPIRVString *>(UniqId)->getStr();
DINode::DIFlags Flags = DINode::FlagZero;
SPIRVWord SPIRVFlags =
getConstantValueOrLiteral(Ops, FlagsIdx, DebugInst->getExtSetKind());
if (SPIRVFlags & SPIRVDebug::FlagIsFwdDecl)
Flags |= DINode::FlagFwdDecl;
if (SPIRVFlags & SPIRVDebug::FlagTypePassByValue)
Flags |= DINode::FlagTypePassByValue;
if (SPIRVFlags & SPIRVDebug::FlagTypePassByReference)
Flags |= DINode::FlagTypePassByReference;
DICompositeType *CT = nullptr;
switch (getConstantValueOrLiteral(Ops, TagIdx, DebugInst->getExtSetKind())) {
case SPIRVDebug::Class:
// TODO: should be replaced with createClassType, when bug with creating
// ClassType with llvm::dwarf::DW_TAG_struct_type tag will be fixed
CT = getDIBuilder(DebugInst).createReplaceableCompositeType(
llvm::dwarf::DW_TAG_class_type, Name, ParentScope, File, LineNo, 0,
Size, Align, Flags, Identifier);
CT = llvm::MDNode::replaceWithDistinct(llvm::TempDICompositeType(CT));
break;
case SPIRVDebug::Structure:
CT = getDIBuilder(DebugInst).createStructType(
ParentScope, Name, File, LineNo, Size, Align, Flags, DerivedFrom,
DINodeArray() /*elements*/, 0 /*RunTimeLang*/, nullptr /*VTableHolder*/,
Identifier);
break;
case SPIRVDebug::Union:
CT = getDIBuilder(DebugInst).createUnionType(
ParentScope, Name, File, LineNo, Size, Align, Flags, DINodeArray(),
0 /*RuntimrLang*/, Identifier);
break;
default:
llvm_unreachable("Unexpected composite type");
break;
}
DebugInstCache[DebugInst] = CT;
SmallVector<llvm::Metadata *, 8> EltTys;
for (size_t I = FirstMemberIdx; I < Ops.size(); ++I) {
auto *MemberInst = BM->get<SPIRVExtInst>(Ops[I]);
if (MemberInst->getExtOp() == SPIRVDebug::TypeMember) {
auto *SPVMemberInst = BM->get<SPIRVExtInst>(Ops[I]);
DINode *MemberMD =
transTypeMember(SPVMemberInst, DebugInst, cast<DIScope>(CT));
EltTys.push_back(MemberMD);
DebugInstCache[SPVMemberInst] = MemberMD;
} else if (MemberInst->getExtOp() == SPIRVDebug::TypeInheritance) {
auto *SPVMemberInst = BM->get<SPIRVExtInst>(Ops[I]);
DINode *MemberMD = transTypeInheritance(SPVMemberInst, cast<DIType>(CT));
EltTys.push_back(MemberMD);
DebugInstCache[SPVMemberInst] = MemberMD;
} else {
EltTys.emplace_back(transDebugInst(BM->get<SPIRVExtInst>(Ops[I])));
}
}
DINodeArray Elements = getDIBuilder(DebugInst).getOrCreateArray(EltTys);
getDIBuilder(DebugInst).replaceArrays(CT, Elements);
assert(CT && "Composite type translation failed.");
return CT;
}
DISubrange *
SPIRVToLLVMDbgTran::transTypeSubrange(const SPIRVExtInst *DebugInst) {
using namespace SPIRVDebug::Operand::TypeSubrange;
const SPIRVWordVec &Ops = DebugInst->getArguments();
assert((Ops.size() == MinOperandCount || Ops.size() == MaxOperandCount) &&
"Invalid number of operands");
std::vector<Metadata *> TranslatedOps(MaxOperandCount, nullptr);
auto TransOperand = [&Ops, &TranslatedOps, this](int Idx) -> void {
if (!getDbgInst<SPIRVDebug::DebugInfoNone>(Ops[Idx])) {
if (auto *GlobalVar = getDbgInst<SPIRVDebug::GlobalVariable>(Ops[Idx])) {
TranslatedOps[Idx] =
cast<Metadata>(transDebugInst<DIGlobalVariable>(GlobalVar));
} else if (auto *LocalVar =
getDbgInst<SPIRVDebug::LocalVariable>(Ops[Idx])) {
TranslatedOps[Idx] =
cast<Metadata>(transDebugInst<DILocalVariable>(LocalVar));
} else if (auto *Expr = getDbgInst<SPIRVDebug::Expression>(Ops[Idx])) {
TranslatedOps[Idx] = cast<Metadata>(transDebugInst<DIExpression>(Expr));
} else if (auto *Const = BM->get<SPIRVConstant>(Ops[Idx])) {
int64_t ConstantAsInt = static_cast<int64_t>(Const->getZExtIntValue());
TranslatedOps[Idx] = cast<Metadata>(ConstantAsMetadata::get(
ConstantInt::get(M->getContext(), APInt(64, ConstantAsInt))));
}
}
};
for (size_t Idx = 0; Idx < Ops.size(); ++Idx)
TransOperand(Idx);
return getDIBuilder(DebugInst).getOrCreateSubrange(
TranslatedOps[CountIdx], TranslatedOps[LowerBoundIdx],
TranslatedOps[UpperBoundIdx], TranslatedOps[StrideIdx]);
}
DIStringType *
SPIRVToLLVMDbgTran::transTypeString(const SPIRVExtInst *DebugInst) {
using namespace SPIRVDebug::Operand::TypeString;
const SPIRVWordVec &Ops = DebugInst->getArguments();
assert(Ops.size() >= MinOperandCount && "Invalid number of operands");
StringRef Name = getString(Ops[NameIdx]);
unsigned Encoding = 0;
if (!getDbgInst<SPIRVDebug::DebugInfoNone>((Ops[BaseTypeIdx]))) {
DIBasicType *BaseTy =
transTypeBasic(BM->get<SPIRVExtInst>(Ops[BaseTypeIdx]));
Encoding = BaseTy->getEncoding();
}
DIExpression *StrLocationExp = nullptr;
if (!getDbgInst<SPIRVDebug::DebugInfoNone>(Ops[DataLocationIdx])) {
if (const auto *DIExpr =
getDbgInst<SPIRVDebug::Expression>(Ops[DataLocationIdx]))
StrLocationExp = transDebugInst<DIExpression>(DIExpr);
}
uint64_t SizeInBits = BM->get<SPIRVConstant>(Ops[SizeIdx])->getZExtIntValue();
DIExpression *StringLengthExp = nullptr;
DIVariable *StringLengthVar = nullptr;
if (!getDbgInst<SPIRVDebug::DebugInfoNone>(Ops[LengthAddrIdx])) {
if (const auto *GV =
getDbgInst<SPIRVDebug::GlobalVariable>(Ops[LengthAddrIdx]))
StringLengthVar = transDebugInst<DIGlobalVariable>(GV);
if (const auto *LV =
getDbgInst<SPIRVDebug::LocalVariable>(Ops[LengthAddrIdx]))
StringLengthVar = transDebugInst<DILocalVariable>(LV);
if (const auto *DIExpr =
getDbgInst<SPIRVDebug::Expression>(Ops[LengthAddrIdx]))
StringLengthExp = transDebugInst<DIExpression>(DIExpr);
}
return DIStringType::get(M->getContext(), dwarf::DW_TAG_string_type, Name,
cast_or_null<Metadata>(StringLengthVar),
cast_or_null<Metadata>(StringLengthExp),
cast_or_null<Metadata>(StrLocationExp), SizeInBits,
0 /*AlignInBits*/, Encoding);
}
DINode *SPIRVToLLVMDbgTran::transTypeMember(const SPIRVExtInst *DebugInst,
const SPIRVExtInst *ParentInst,
DIScope *Scope) {
if (isNonSemanticDebugInfo(DebugInst->getExtSetKind()))
// In NonSemantic spec TypeMember doesn't have Scope parameter
return transTypeMemberNonSemantic(DebugInst, ParentInst, Scope);
return transTypeMemberOpenCL(DebugInst);
}
DINode *
SPIRVToLLVMDbgTran::transTypeMemberOpenCL(const SPIRVExtInst *DebugInst) {
using namespace SPIRVDebug::Operand::TypeMember::OpenCL;
const SPIRVWordVec &Ops = DebugInst->getArguments();
assert(Ops.size() >= MinOperandCount && "Invalid number of operands");
DIFile *File = getFile(Ops[SourceIdx]);
SPIRVWord LineNo =
getConstantValueOrLiteral(Ops, LineIdx, DebugInst->getExtSetKind());
StringRef Name = getString(Ops[NameIdx]);
DIScope *Scope = getScope(BM->getEntry(Ops[ParentIdx]));
DIType *BaseType = transNonNullDebugType(BM->get<SPIRVExtInst>(Ops[TypeIdx]));
uint64_t OffsetInBits =
BM->get<SPIRVConstant>(Ops[OffsetIdx])->getZExtIntValue();
SPIRVWord SPIRVFlags =
getConstantValueOrLiteral(Ops, FlagsIdx, DebugInst->getExtSetKind());
DINode::DIFlags Flags = DINode::FlagZero;
if ((SPIRVDebug::FlagAccess & SPIRVFlags) == SPIRVDebug::FlagIsPublic) {
Flags |= DINode::FlagPublic;
} else if (SPIRVFlags & SPIRVDebug::FlagIsProtected) {
Flags |= DINode::FlagProtected;
} else if (SPIRVFlags & SPIRVDebug::FlagIsPrivate) {
Flags |= DINode::FlagPrivate;
}
if (SPIRVFlags & SPIRVDebug::FlagIsStaticMember)
Flags |= DINode::FlagStaticMember;
if (Flags & DINode::FlagStaticMember) {
llvm::Value *Val = nullptr;
if (Ops.size() > MinOperandCount) {
SPIRVValue *ConstVal = BM->get<SPIRVValue>(Ops[ValueIdx]);
assert(isConstantOpCode(ConstVal->getOpCode()) &&
"Static member must be a constant");
Val = SPIRVReader->transValue(ConstVal, nullptr, nullptr);
}
auto Tag = M->getDwarfVersion() >= 5 ? llvm::dwarf::DW_TAG_variable
: llvm::dwarf::DW_TAG_member;
return getDIBuilder(DebugInst).createStaticMemberType(
Scope, Name, File, LineNo, BaseType, Flags,
cast_or_null<llvm::Constant>(Val), Tag);
}
uint64_t Size = BM->get<SPIRVConstant>(Ops[SizeIdx])->getZExtIntValue();
uint64_t Alignment = 0;
return getDIBuilder(DebugInst).createMemberType(Scope, Name, File, LineNo,
Size, Alignment, OffsetInBits,
Flags, BaseType);
}
DINode *
SPIRVToLLVMDbgTran::transTypeMemberNonSemantic(const SPIRVExtInst *DebugInst,
const SPIRVExtInst *ParentInst,
DIScope *Scope) {
if (!Scope)
// Will be translated later when processing TypeMember's parent
return nullptr;
using namespace SPIRVDebug::Operand::TypeMember::NonSemantic;
const SPIRVWordVec &Ops = DebugInst->getArguments();
assert(Ops.size() >= MinOperandCount && "Invalid number of operands");
DIFile *File = getFile(Ops[SourceIdx]);
SPIRVWord LineNo =
getConstantValueOrLiteral(Ops, LineIdx, DebugInst->getExtSetKind());
StringRef Name = getString(Ops[NameIdx]);
DIType *BaseType = transNonNullDebugType(BM->get<SPIRVExtInst>(Ops[TypeIdx]));
uint64_t OffsetInBits =
BM->get<SPIRVConstant>(Ops[OffsetIdx])->getZExtIntValue();
SPIRVWord SPIRVFlags =
getConstantValueOrLiteral(Ops, FlagsIdx, DebugInst->getExtSetKind());
DINode::DIFlags Flags = DINode::FlagZero;
if ((SPIRVDebug::FlagAccess & SPIRVFlags) == SPIRVDebug::FlagIsPublic) {
Flags |= DINode::FlagPublic;
} else if (SPIRVFlags & SPIRVDebug::FlagIsProtected) {
Flags |= DINode::FlagProtected;
} else if (SPIRVFlags & SPIRVDebug::FlagIsPrivate) {
Flags |= DINode::FlagPrivate;
}
if (SPIRVFlags & SPIRVDebug::FlagIsStaticMember)
Flags |= DINode::FlagStaticMember;
if (SPIRVFlags & SPIRVDebug::FlagBitField)
Flags |= DINode::FlagBitField;
if (Flags & DINode::FlagStaticMember) {
llvm::Value *Val = nullptr;
if (Ops.size() > MinOperandCount) {
SPIRVValue *ConstVal = BM->get<SPIRVValue>(Ops[ValueIdx]);
assert(isConstantOpCode(ConstVal->getOpCode()) &&
"Static member must be a constant");
Val = SPIRVReader->transValue(ConstVal, nullptr, nullptr);
}
auto Tag = M->getDwarfVersion() >= 5 ? llvm::dwarf::DW_TAG_variable
: llvm::dwarf::DW_TAG_member;
return getDIBuilder(DebugInst).createStaticMemberType(
Scope, Name, File, LineNo, BaseType, Flags,
cast_or_null<llvm::Constant>(Val), Tag);
}
uint64_t Size = BM->get<SPIRVConstant>(Ops[SizeIdx])->getZExtIntValue();
uint64_t Alignment = 0;
return getDIBuilder(ParentInst)
.createMemberType(Scope, Name, File, LineNo, Size, Alignment,
OffsetInBits, Flags, BaseType);
}
DINode *SPIRVToLLVMDbgTran::transTypeEnum(const SPIRVExtInst *DebugInst) {
using namespace SPIRVDebug::Operand::TypeEnum;
const SPIRVWordVec &Ops = DebugInst->getArguments();
assert(Ops.size() >= MinOperandCount && "Invalid number of operands");
StringRef Name = getString(Ops[NameIdx]);
DIFile *File = getFile(Ops[SourceIdx]);
SPIRVWord LineNo =
getConstantValueOrLiteral(Ops, LineIdx, DebugInst->getExtSetKind());
DIScope *Scope = getScope(BM->getEntry(Ops[ParentIdx]));
uint64_t SizeInBits = BM->get<SPIRVConstant>(Ops[SizeIdx])->getZExtIntValue();
SPIRVWord AlignInBits = 0;
SPIRVWord Flags =
getConstantValueOrLiteral(Ops, FlagsIdx, DebugInst->getExtSetKind());
if (Flags & SPIRVDebug::FlagIsFwdDecl) {
return getDIBuilder(DebugInst).createForwardDecl(
dwarf::DW_TAG_enumeration_type, Name, Scope, File, LineNo, AlignInBits,
SizeInBits);
} else {
SmallVector<llvm::Metadata *, 16> Elts;
for (size_t I = FirstEnumeratorIdx, E = Ops.size(); I < E; I += 2) {
uint64_t Val = BM->get<SPIRVConstant>(Ops[I])->getZExtIntValue();
StringRef Name = getString(Ops[I + 1]);
Elts.push_back(getDIBuilder(DebugInst).createEnumerator(Name, Val));
}
DINodeArray Enumerators = getDIBuilder(DebugInst).getOrCreateArray(Elts);
DIType *UnderlyingType = nullptr;
SPIRVEntry *E = BM->getEntry(Ops[UnderlyingTypeIdx]);
if (!isa<OpTypeVoid>(E))
UnderlyingType = transDebugInst<DIType>(static_cast<SPIRVExtInst *>(E));
return getDIBuilder(DebugInst).createEnumerationType(
Scope, Name, File, LineNo, SizeInBits, AlignInBits, Enumerators,
UnderlyingType, 0, "", UnderlyingType);
}
}
DINode *SPIRVToLLVMDbgTran::transTypeFunction(const SPIRVExtInst *DebugInst) {
using namespace SPIRVDebug::Operand::TypeFunction;
const SPIRVWordVec &Ops = DebugInst->getArguments();
assert(Ops.size() >= MinOperandCount && "Invalid number of operands");
SPIRVWord SPIRVFlags =
getConstantValueOrLiteral(Ops, FlagsIdx, DebugInst->getExtSetKind());
DINode::DIFlags Flags = DINode::FlagZero;
if (SPIRVFlags & SPIRVDebug::FlagIsLValueReference)
Flags |= llvm::DINode::FlagLValueReference;
if (SPIRVFlags & SPIRVDebug::FlagIsRValueReference)
Flags |= llvm::DINode::FlagRValueReference;
SPIRVEntry *E = BM->getEntry(Ops[ReturnTypeIdx]);
MDNode *RT = isa<OpTypeVoid>(E)
? nullptr
: transDebugInst(BM->get<SPIRVExtInst>(Ops[ReturnTypeIdx]));
SmallVector<llvm::Metadata *, 16> Elements{RT};
for (size_t I = FirstParameterIdx, E = Ops.size(); I < E; ++I) {
SPIRVEntry *P = BM->getEntry(Ops[I]);
MDNode *Param = isa<OpTypeVoid>(P)
? nullptr
: transDebugInst(BM->get<SPIRVExtInst>(Ops[I]));
Elements.push_back(Param);
}
DITypeRefArray ArgTypes =
getDIBuilder(DebugInst).getOrCreateTypeArray(Elements);
return getDIBuilder(DebugInst).createSubroutineType(ArgTypes, Flags);
}
DINode *
SPIRVToLLVMDbgTran::transTypePtrToMember(const SPIRVExtInst *DebugInst) {
using namespace SPIRVDebug::Operand::TypePtrToMember;
const SPIRVWordVec &Ops = DebugInst->getArguments();
assert(Ops.size() >= OperandCount && "Invalid number of operands");
SPIRVExtInst *Member = BM->get<SPIRVExtInst>(Ops[MemberTypeIdx]);
DIType *PointeeTy = transNonNullDebugType(Member);
SPIRVExtInst *ContainingTy = BM->get<SPIRVExtInst>(Ops[ParentIdx]);
DIType *BaseTy = transNonNullDebugType(ContainingTy);
return getDIBuilder(DebugInst).createMemberPointerType(PointeeTy, BaseTy, 0);
}
DINode *SPIRVToLLVMDbgTran::transLexicalBlock(const SPIRVExtInst *DebugInst) {
using namespace SPIRVDebug::Operand::LexicalBlock;
const SPIRVWordVec &Ops = DebugInst->getArguments();
DIScope *ParentScope = getScope(BM->getEntry(Ops[ParentIdx]));
DIFile *File = getFile(Ops[SourceIdx]);
SPIRVWord LineNo =
getConstantValueOrLiteral(Ops, LineIdx, DebugInst->getExtSetKind());
if (Ops.size() > MinOperandCount) {
StringRef Name = getString(Ops[NameIdx]);
bool InlinedNamespace = false;
if (DebugInst->getExtSetKind() ==
SPIRVEIS_NonSemantic_Shader_DebugInfo_200) {
SPIRVValue *V = BM->get<SPIRVValue>(Ops[InlineNamespaceIdx]);
Value *Var = SPIRVReader->transValue(V, nullptr, nullptr);
InlinedNamespace = cast<ConstantInt>(Var)->isOne();
}
return getDIBuilder(DebugInst).createNameSpace(ParentScope, Name,
InlinedNamespace);
}
unsigned Column = Ops[ColumnIdx];
return getDIBuilder(DebugInst).createLexicalBlock(ParentScope, File, LineNo,
Column);
}
DINode *SPIRVToLLVMDbgTran::transLexicalBlockDiscriminator(
const SPIRVExtInst *DebugInst) {
using namespace SPIRVDebug::Operand::LexicalBlockDiscriminator;
const SPIRVWordVec &Ops = DebugInst->getArguments();
DIFile *File = getFile(Ops[SourceIdx]);
SPIRVWord Disc = getConstantValueOrLiteral(Ops, DiscriminatorIdx,
DebugInst->getExtSetKind());
DIScope *ParentScope = getScope(BM->getEntry(Ops[ParentIdx]));
return getDIBuilder(DebugInst).createLexicalBlockFile(ParentScope, File,
Disc);
}
DINode *SPIRVToLLVMDbgTran::transFunction(const SPIRVExtInst *DebugInst,
bool IsMainSubprogram) {
using namespace SPIRVDebug::Operand::Function;
const SPIRVWordVec &Ops = DebugInst->getArguments();
assert(Ops.size() >= MinOperandCountNonSem && "Invalid number of operands");
if (!isNonSemanticDebugInfo(DebugInst->getExtSetKind()))
assert(Ops.size() >= MinOperandCount && "Invalid number of operands");
StringRef Name = getString(Ops[NameIdx]);
DISubroutineType *Ty =
transDebugInst<DISubroutineType>(BM->get<SPIRVExtInst>(Ops[TypeIdx]));
DIFile *File = getFile(Ops[SourceIdx]);
SPIRVWord LineNo =
getConstantValueOrLiteral(Ops, LineIdx, DebugInst->getExtSetKind());
DIScope *Scope = getScope(BM->getEntry(Ops[ParentIdx]));
StringRef LinkageName = getString(Ops[LinkageNameIdx]);
SPIRVWord SPIRVDebugFlags =
getConstantValueOrLiteral(Ops, FlagsIdx, DebugInst->getExtSetKind());
DINode::DIFlags Flags = DINode::FlagZero;
if (SPIRVDebugFlags & SPIRVDebug::FlagIsArtificial)
Flags |= llvm::DINode::FlagArtificial;
if (SPIRVDebugFlags & SPIRVDebug::FlagIsExplicit)
Flags |= llvm::DINode::FlagExplicit;
if (SPIRVDebugFlags & SPIRVDebug::FlagIsPrototyped)
Flags |= llvm::DINode::FlagPrototyped;
if (SPIRVDebugFlags & SPIRVDebug::FlagIsLValueReference)
Flags |= llvm::DINode::FlagLValueReference;
if (SPIRVDebugFlags & SPIRVDebug::FlagIsRValueReference)
Flags |= llvm::DINode::FlagRValueReference;
if ((SPIRVDebugFlags & SPIRVDebug::FlagAccess) == SPIRVDebug::FlagIsPublic)
Flags |= llvm::DINode::FlagPublic;
if (SPIRVDebugFlags & SPIRVDebug::FlagIsProtected)
Flags |= llvm::DINode::FlagProtected;
if (SPIRVDebugFlags & SPIRVDebug::FlagIsPrivate)
Flags |= llvm::DINode::FlagPrivate;
bool IsDefinition = SPIRVDebugFlags & SPIRVDebug::FlagIsDefinition;
bool IsOptimized = SPIRVDebugFlags & SPIRVDebug::FlagIsOptimized;
bool IsLocal = SPIRVDebugFlags & SPIRVDebug::FlagIsLocal;
bool IsMainSubprogramFlag =
IsMainSubprogram ||
(!isNonSemanticDebugInfo(DebugInst->getExtSetKind()) &&
BM->isEntryPoint(spv::ExecutionModelKernel, Ops[FunctionIdIdx]));
DISubprogram::DISPFlags SPFlags = DISubprogram::toSPFlags(
IsLocal, IsDefinition, IsOptimized, DISubprogram::SPFlagNonvirtual,
IsMainSubprogramFlag);
SPIRVWord ScopeLine =
getConstantValueOrLiteral(Ops, ScopeLineIdx, DebugInst->getExtSetKind());
// Function declaration descriptor
DISubprogram *FD = nullptr;
if (isNonSemanticDebugInfo(DebugInst->getExtSetKind()) &&
Ops.size() > DeclarationNonSemIdx) {
FD = transDebugInst<DISubprogram>(
BM->get<SPIRVExtInst>(Ops[DeclarationNonSemIdx]));
} else if (Ops.size() > DeclarationIdx) {
FD = transDebugInst<DISubprogram>(
BM->get<SPIRVExtInst>(Ops[DeclarationIdx]));
}
// Here we create fake array of template parameters. If it was plain nullptr,
// the template parameter operand would be removed in DISubprogram::getImpl.
// But we want it to be there, because if there is DebugTypeTemplate
// instruction refering to this function, transTypeTemplate method must be
// able to replace the template parameter operand, thus it must be in the
// operands list.
SmallVector<llvm::Metadata *, 8> Elts;
DINodeArray TParams = getDIBuilder(DebugInst).getOrCreateArray(Elts);
llvm::DITemplateParameterArray TParamsArray = TParams.get();
DISubprogram *DIS = nullptr;
if (Scope && (isa<DICompositeType>(Scope) || isa<DINamespace>(Scope)) &&
!IsDefinition)
DIS = getDIBuilder(DebugInst).createMethod(Scope, Name, LinkageName, File,
LineNo, Ty, 0, 0, nullptr, Flags,
SPFlags, TParamsArray);
else {
// Create targetFuncName mostly for Fortran trampoline function if it is
// the case
StringRef TargetFunction = "";
if (DebugInst->getExtSetKind() ==
SPIRVEIS_NonSemantic_Shader_DebugInfo_200 &&
Ops.size() > TargetFunctionNameIdx) {
TargetFunction = getString(Ops[TargetFunctionNameIdx]);
}
DIS = getDIBuilder(DebugInst).createFunction(
Scope, Name, LinkageName, File, LineNo, Ty, ScopeLine, Flags, SPFlags,
TParamsArray, FD,
/*ThrownTypes*/ nullptr,
/*Annotations*/ nullptr, TargetFunction);
}
DebugInstCache[DebugInst] = DIS;
// At this point, we don't have info about the function definition for
// NonSemantic.Shader debug info. If function definition is present, it'll be
// translated later within the function scope.
// For "default" debug info we do translate function body here.
if (!isNonSemanticDebugInfo(DebugInst->getExtSetKind()))
transFunctionBody(DIS, Ops[FunctionIdIdx]);
return DIS;