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 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851
|
//===- 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 std;
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, "", Flags & SPIRVDebug::FlagIsEnumClass);
}
}
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;
}
void SPIRVToLLVMDbgTran::transFunctionBody(DISubprogram *DIS, SPIRVId FuncId) {
FuncMap[FuncId] = DIS;
SPIRVEntry *E = BM->getEntry(FuncId);
if (E->getOpCode() == OpFunction) {
SPIRVFunction *BF = static_cast<SPIRVFunction *>(E);
llvm::Function *F = SPIRVReader->transFunction(BF);
assert(F && "Translation of function failed!");
if (!F->hasMetadata("dbg"))
F->setMetadata("dbg", DIS);
}
}
DINode *
SPIRVToLLVMDbgTran::transFunctionDefinition(const SPIRVExtInst *DebugInst) {
using namespace SPIRVDebug::Operand::FunctionDefinition;
const SPIRVWordVec &Ops = DebugInst->getArguments();
SPIRVExtInst *Func = BM->get<SPIRVExtInst>(Ops[FunctionIdx]);
DISubprogram *LLVMFunc = cast<DISubprogram>(DebugInstCache[Func]);
transFunctionBody(LLVMFunc, Ops[DefinitionIdx]);
return nullptr;
}
DINode *SPIRVToLLVMDbgTran::transFunctionDecl(const SPIRVExtInst *DebugInst) {
using namespace SPIRVDebug::Operand::FunctionDeclaration;
const SPIRVWordVec &Ops = DebugInst->getArguments();
assert(Ops.size() == OperandCount && "Invalid number of operands");
// Scope
DIScope *Scope = getScope(BM->getEntry(Ops[ParentIdx]));
StringRef Name = getString(Ops[NameIdx]);
StringRef LinkageName = getString(Ops[LinkageNameIdx]);
DIFile *File = getFile(Ops[SourceIdx]);
SPIRVWord LineNo =
getConstantValueOrLiteral(Ops, LineIdx, DebugInst->getExtSetKind());
DISubroutineType *Ty =
transDebugInst<DISubroutineType>(BM->get<SPIRVExtInst>(Ops[TypeIdx]));
SPIRVWord SPIRVDebugFlags =
getConstantValueOrLiteral(Ops, FlagsIdx, DebugInst->getExtSetKind());
bool IsDefinition = SPIRVDebugFlags & SPIRVDebug::FlagIsDefinition;
bool IsOptimized = SPIRVDebugFlags & SPIRVDebug::FlagIsOptimized;
bool IsLocal = SPIRVDebugFlags & SPIRVDebug::FlagIsLocal;
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;
// 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;
DISubprogram::DISPFlags SPFlags =
DISubprogram::toSPFlags(IsLocal, IsDefinition, IsOptimized);
if (isa<DICompositeType>(Scope) || isa<DINamespace>(Scope))
DIS = getDIBuilder(DebugInst).createMethod(Scope, Name, LinkageName, File,
LineNo, Ty, 0, 0, nullptr, Flags,
SPFlags, TParamsArray);
else {
// Since a function declaration doesn't have any retained nodes, resolve
// the temporary placeholder for them immediately.
DIS = getDIBuilder(DebugInst).createTempFunctionFwdDecl(
Scope, Name, LinkageName, File, LineNo, Ty, 0, Flags, SPFlags,
TParamsArray);
llvm::TempMDNode FwdDecl(cast<llvm::MDNode>(DIS));
DIS = getDIBuilder(DebugInst).replaceTemporary(std::move(FwdDecl), DIS);
}
DebugInstCache[DebugInst] = DIS;
return DIS;
}
MDNode *SPIRVToLLVMDbgTran::transEntryPoint(const SPIRVExtInst *DebugInst) {
using namespace SPIRVDebug::Operand::EntryPoint;
const SPIRVWordVec &Ops = DebugInst->getArguments();
assert(Ops.size() == OperandCount && "Invalid number of operands");
SPIRVExtInst *EP = BM->get<SPIRVExtInst>(Ops[EntryPointIdx]);
SPIRVExtInst *CU = BM->get<SPIRVExtInst>(Ops[CompilationUnitIdx]);
std::string Producer = getString(Ops[CompilerSignatureIdx]);
std::string CLArgs = getString(Ops[CommandLineArgsIdx]);
DICompileUnit *C = transCompilationUnit(CU, Producer, CLArgs); // NOLINT
DebugInstCache[CU] = C;
return transFunction(EP, true /*IsMainSubprogram*/);
}
MDNode *SPIRVToLLVMDbgTran::transGlobalVariable(const SPIRVExtInst *DebugInst) {
using namespace SPIRVDebug::Operand::GlobalVariable;
const SPIRVWordVec &Ops = DebugInst->getArguments();
assert(Ops.size() >= MinOperandCount && "Invalid number of operands");
StringRef Name = getString(Ops[NameIdx]);
DIType *Ty = transNonNullDebugType(BM->get<SPIRVExtInst>(Ops[TypeIdx]));
DIFile *File = getFile(Ops[SourceIdx]);
SPIRVWord LineNo =
getConstantValueOrLiteral(Ops, LineIdx, DebugInst->getExtSetKind());
DIScope *Parent = getScope(BM->getEntry(Ops[ParentIdx]));
StringRef LinkageName = getString(Ops[LinkageNameIdx]);
DIDerivedType *StaticMemberDecl = nullptr;
if (Ops.size() > MinOperandCount) {
StaticMemberDecl = transDebugInst<DIDerivedType>(
BM->get<SPIRVExtInst>(Ops[StaticMemberDeclarationIdx]));
}
DIExpression *DIExpr = nullptr;
// Check if Ops[VariableIdx] is not being used to hold a variable operand.
// Instead it is being used to hold an Expression that holds the initial
// value of the GlobalVariable.
if (getDbgInst<SPIRVDebug::Expression>(Ops[VariableIdx]))
DIExpr =
transDebugInst<DIExpression>(BM->get<SPIRVExtInst>(Ops[VariableIdx]));
SPIRVWord Flags =
getConstantValueOrLiteral(Ops, FlagsIdx, DebugInst->getExtSetKind());
bool IsLocal = Flags & SPIRVDebug::FlagIsLocal;
bool IsDefinition = Flags & SPIRVDebug::FlagIsDefinition;
MDNode *VarDecl = nullptr;
if (IsDefinition) {
VarDecl = getDIBuilder(DebugInst).createGlobalVariableExpression(
Parent, Name, LinkageName, File, LineNo, Ty, IsLocal, IsDefinition,
DIExpr, StaticMemberDecl);
} else {
VarDecl = getDIBuilder(DebugInst).createTempGlobalVariableFwdDecl(
Parent, Name, LinkageName, File, LineNo, Ty, IsLocal, StaticMemberDecl);
// replaceAllUsesWith call makes VarDecl non-temp.
// Otherwise DIBuilder will crash at finalization.
llvm::TempMDNode TMP(VarDecl);
VarDecl = getDIBuilder(DebugInst).replaceTemporary(std::move(TMP), VarDecl);
}
// Ops[VariableIdx] was not used to hold an Expression with the initial value
// for the GlobalVariable
if (!DIExpr) {
// If the variable has no initializer Ops[VariableIdx] is OpDebugInfoNone.
// Otherwise Ops[VariableIdx] may be a global variable or a constant(C++
// static const).
if (VarDecl && !getDbgInst<SPIRVDebug::DebugInfoNone>(Ops[VariableIdx])) {
SPIRVValue *V = BM->get<SPIRVValue>(Ops[VariableIdx]);
Value *Var = SPIRVReader->transValue(V, nullptr, nullptr);
llvm::GlobalVariable *GV = dyn_cast_or_null<llvm::GlobalVariable>(Var);
if (GV && !GV->hasMetadata("dbg"))
GV->addMetadata("dbg", *VarDecl);
}
}
return VarDecl;
}
DINode *SPIRVToLLVMDbgTran::transLocalVariable(const SPIRVExtInst *DebugInst) {
using namespace SPIRVDebug::Operand::LocalVariable;
const SPIRVWordVec &Ops = DebugInst->getArguments();
assert(Ops.size() >= MinOperandCount && "Invalid number of operands");
DIScope *Scope = getScope(BM->getEntry(Ops[ParentIdx]));
StringRef Name = getString(Ops[NameIdx]);
DIFile *File = getFile(Ops[SourceIdx]);
SPIRVWord LineNo =
getConstantValueOrLiteral(Ops, LineIdx, DebugInst->getExtSetKind());
DIType *Ty = transNonNullDebugType(BM->get<SPIRVExtInst>(Ops[TypeIdx]));
DINode::DIFlags Flags = DINode::FlagZero;
SPIRVWord SPIRVFlags =
getConstantValueOrLiteral(Ops, FlagsIdx, DebugInst->getExtSetKind());
if (SPIRVFlags & SPIRVDebug::FlagIsArtificial)
Flags |= DINode::FlagArtificial;
if (SPIRVFlags & SPIRVDebug::FlagIsObjectPointer)
Flags |= DINode::FlagObjectPointer;
if (Ops.size() > ArgNumberIdx)
return getDIBuilder(DebugInst).createParameterVariable(
Scope, Name, Ops[ArgNumberIdx], File, LineNo, Ty, true, Flags);
return getDIBuilder(DebugInst).createAutoVariable(Scope, Name, File, LineNo,
Ty, true, Flags);
}
DINode *SPIRVToLLVMDbgTran::transTypedef(const SPIRVExtInst *DebugInst) {
using namespace SPIRVDebug::Operand::Typedef;
const SPIRVWordVec &Ops = DebugInst->getArguments();
assert(Ops.size() >= OperandCount && "Invalid number of operands");
DIFile *File = getFile(Ops[SourceIdx]);
SPIRVWord LineNo =
getConstantValueOrLiteral(Ops, LineIdx, DebugInst->getExtSetKind());
StringRef Alias = getString(Ops[NameIdx]);
SPIRVEntry *TypeInst = BM->getEntry(Ops[BaseTypeIdx]);
DIType *Ty = transDebugInst<DIType>(static_cast<SPIRVExtInst *>(TypeInst));
DIScope *Scope = getScope(BM->getEntry(Ops[ParentIdx]));
assert(Scope && "Typedef should have a parent scope");
return getDIBuilder(DebugInst).createTypedef(Ty, Alias, File, LineNo, Scope);
}
DINode *SPIRVToLLVMDbgTran::transTypeInheritance(const SPIRVExtInst *DebugInst,
DIType *ChildClass) {
using namespace SPIRVDebug::Operand::TypeInheritance;
// The value is used when assertions are enabled
[[maybe_unused]] unsigned OperandCount;
unsigned ParentIdx, OffsetIdx, FlagsIdx;
if (isNonSemanticDebugInfo(DebugInst->getExtSetKind())) {
if (!ChildClass) {
// Will be translated later when processing TypeMember's parent
return nullptr;
}
OperandCount = NonSemantic::OperandCount;
ParentIdx = NonSemantic::ParentIdx;
OffsetIdx = NonSemantic::OffsetIdx;
FlagsIdx = NonSemantic::FlagsIdx;
} else {
OperandCount = OpenCL::OperandCount;
ParentIdx = OpenCL::ParentIdx;
OffsetIdx = OpenCL::OffsetIdx;
FlagsIdx = OpenCL::FlagsIdx;
}
const SPIRVWordVec &Ops = DebugInst->getArguments();
assert(Ops.size() == OperandCount && "Invalid number of operands");
DIType *Parent =
transDebugInst<DIType>(BM->get<SPIRVExtInst>(Ops[ParentIdx]));
DINode::DIFlags Flags = DINode::FlagZero;
SPIRVWord SPIRVFlags =
getConstantValueOrLiteral(Ops, FlagsIdx, DebugInst->getExtSetKind());
if ((SPIRVFlags & SPIRVDebug::FlagAccess) == SPIRVDebug::FlagIsPublic)
Flags |= llvm::DINode::FlagPublic;
if ((SPIRVFlags & SPIRVDebug::FlagAccess) == SPIRVDebug::FlagIsProtected)
Flags |= llvm::DINode::FlagProtected;
if ((SPIRVFlags & SPIRVDebug::FlagAccess) == SPIRVDebug::FlagIsPrivate)
Flags |= llvm::DINode::FlagPrivate;
uint64_t OffsetVal =
BM->get<SPIRVConstant>(Ops[OffsetIdx])->getZExtIntValue();
DIType *Child;
if (isNonSemanticDebugInfo(DebugInst->getExtSetKind())) {
Child = ChildClass;
} else {
Child =
transDebugInst<DIType>(BM->get<SPIRVExtInst>(Ops[OpenCL::ChildIdx]));
}
return getDIBuilder(DebugInst).createInheritance(Child, Parent, OffsetVal, 0,
Flags);
}
DINode *
SPIRVToLLVMDbgTran::transTypeTemplateParameter(const SPIRVExtInst *DebugInst) {
using namespace SPIRVDebug::Operand::TypeTemplateParameter;
const SPIRVWordVec &Ops = DebugInst->getArguments();
assert(Ops.size() >= OperandCount && "Invalid number of operands");
StringRef Name = getString(Ops[NameIdx]);
SPIRVEntry *ActualType = BM->getEntry(Ops[TypeIdx]);
DIType *Ty = nullptr;
if (!isa<OpTypeVoid>(ActualType))
Ty = transDebugInst<DIType>(static_cast<SPIRVExtInst *>(ActualType));
DIScope *Context = nullptr;
if (!getDbgInst<SPIRVDebug::DebugInfoNone>(Ops[ValueIdx])) {
SPIRVValue *Val = BM->get<SPIRVValue>(Ops[ValueIdx]);
Value *V = SPIRVReader->transValue(Val, nullptr, nullptr);
return getDIBuilder(DebugInst).createTemplateValueParameter(
Context, Name, Ty, false, cast<Constant>(V));
}
return getDIBuilder(DebugInst).createTemplateTypeParameter(Context, Name, Ty,
false);
}
DINode *SPIRVToLLVMDbgTran::transTypeTemplateTemplateParameter(
const SPIRVExtInst *DebugInst) {
using namespace SPIRVDebug::Operand::TypeTemplateTemplateParameter;
const SPIRVWordVec &Ops = DebugInst->getArguments();
assert(Ops.size() >= OperandCount && "Invalid number of operands");
StringRef Name = getString(Ops[NameIdx]);
StringRef TemplName = getString(Ops[TemplateNameIdx]);
DIScope *Context = nullptr;
return getDIBuilder(DebugInst).createTemplateTemplateParameter(
Context, Name, nullptr, TemplName);
}
DINode *SPIRVToLLVMDbgTran::transTypeTemplateParameterPack(
const SPIRVExtInst *DebugInst) {
using namespace SPIRVDebug::Operand::TypeTemplateParameterPack;
const SPIRVWordVec &Ops = DebugInst->getArguments();
assert(Ops.size() >= MinOperandCount && "Invalid number of operands");
StringRef Name = getString(Ops[NameIdx]);
SmallVector<llvm::Metadata *, 8> Elts;
for (size_t I = FirstParameterIdx, E = Ops.size(); I < E; ++I) {
Elts.push_back(transDebugInst(BM->get<SPIRVExtInst>(Ops[I])));
}
DINodeArray Pack = getDIBuilder(DebugInst).getOrCreateArray(Elts);
DIScope *Context = nullptr;
return getDIBuilder(DebugInst).createTemplateParameterPack(Context, Name,
nullptr, Pack);
}
MDNode *SPIRVToLLVMDbgTran::transTypeTemplate(const SPIRVExtInst *DebugInst) {
using namespace SPIRVDebug::Operand::TypeTemplate;
const SPIRVWordVec &Ops = DebugInst->getArguments();
const size_t NumOps = Ops.size();
assert(NumOps >= MinOperandCount && "Invalid number of operands");
auto *Templ = BM->get<SPIRVExtInst>(Ops[TargetIdx]);
MDNode *D = transDebugInst(Templ);
SmallVector<llvm::Metadata *, 8> Elts;
for (size_t I = FirstParameterIdx; I < NumOps; ++I) {
Elts.push_back(transDebugInst(BM->get<SPIRVExtInst>(Ops[I])));
}
DINodeArray TParams = getDIBuilder(DebugInst).getOrCreateArray(Elts);
if (DICompositeType *Comp = dyn_cast<DICompositeType>(D)) {
getDIBuilder(DebugInst).replaceArrays(Comp, Comp->getElements(), TParams);
return Comp;
}
if (isa<DISubprogram>(D)) {
// This constant matches with one used in
// DISubprogram::getRawTemplateParams()
const unsigned TemplateParamsIndex = 9;
D->replaceOperandWith(TemplateParamsIndex, TParams.get());
return D;
}
llvm_unreachable("Invalid template");
}
DINode *SPIRVToLLVMDbgTran::transImportedEntry(const SPIRVExtInst *DebugInst) {
using namespace SPIRVDebug::Operand::ImportedEntity;
const SPIRVWordVec &Ops = DebugInst->getArguments();
// FIXME: 'OpenCL/bugged' version is kept because it's hard to remove it
// It's W/A for missing 2nd index in OpenCL's implementation
const SPIRVWord OffsetIdx =
static_cast<int>(isNonSemanticDebugInfo(DebugInst->getExtSetKind()));
assert(Ops.size() == (OpenCL::OperandCount - OffsetIdx) &&
"Invalid number of operands");
DIScope *Scope = getScope(BM->getEntry(Ops[OpenCL::ParentIdx - OffsetIdx]));
SPIRVWord Line = getConstantValueOrLiteral(Ops, OpenCL::LineIdx - OffsetIdx,
DebugInst->getExtSetKind());
DIFile *File = getFile(Ops[OpenCL::SourceIdx - OffsetIdx]);
auto *Entity = transDebugInst<DINode>(
BM->get<SPIRVExtInst>(Ops[OpenCL::EntityIdx - OffsetIdx]));
SPIRVWord Tag = getConstantValueOrLiteral(Ops, OpenCL::TagIdx,
DebugInst->getExtSetKind());
if (Tag == SPIRVDebug::ImportedModule) {
if (!Entity)
return getDIBuilder(DebugInst).createImportedModule(
Scope, static_cast<DIImportedEntity *>(nullptr), File, Line);
if (DIModule *DM = dyn_cast<DIModule>(Entity))
return getDIBuilder(DebugInst).createImportedModule(Scope, DM, File,
Line);
if (DIImportedEntity *IE = dyn_cast<DIImportedEntity>(Entity))
return getDIBuilder(DebugInst).createImportedModule(Scope, IE, File,
Line);
if (DINamespace *NS = dyn_cast<DINamespace>(Entity))
return getDIBuilder(DebugInst).createImportedModule(Scope, NS, File,
Line);
}
if (Tag == SPIRVDebug::ImportedDeclaration) {
StringRef Name = getString(Ops[OpenCL::NameIdx]);
if (DIGlobalVariableExpression *GVE =
dyn_cast<DIGlobalVariableExpression>(Entity))
return getDIBuilder(DebugInst).createImportedDeclaration(
Scope, GVE->getVariable(), File, Line, Name);
return getDIBuilder(DebugInst).createImportedDeclaration(Scope, Entity,
File, Line, Name);
}
llvm_unreachable("Unexpected kind of imported entity!");
}
DINode *SPIRVToLLVMDbgTran::transModule(const SPIRVExtInst *DebugInst) {
using namespace SPIRVDebug::Operand::ModuleINTEL;
const SPIRVWordVec &Ops = DebugInst->getArguments();
assert(Ops.size() >= OperandCount && "Invalid number of operands");
DIScope *Scope = getScope(BM->getEntry(Ops[ParentIdx]));
// In case we translate DebugModuleINTEL instruction (not DebugModule of
// NonSemantic.Shader.200 spec), we should not apply the rule "literals are
// not allowed for NonSemantic specification". This is a hack to avoid getting
// constant value instead of literal in such case.
const SPIRVExtInstSetKind ExtKind =
DebugInst->getExtOp() == SPIRVDebug::Instruction::ModuleINTEL
? SPIRVEIS_OpenCL_DebugInfo_100
: DebugInst->getExtSetKind();
SPIRVWord Line = getConstantValueOrLiteral(Ops, LineIdx, ExtKind);
DIFile *File = getFile(Ops[SourceIdx]);
StringRef Name = getString(Ops[NameIdx]);
StringRef ConfigMacros = getString(Ops[ConfigMacrosIdx]);
StringRef IncludePath = getString(Ops[IncludePathIdx]);
StringRef ApiNotes = getString(Ops[ApiNotesIdx]);
bool IsDecl = getConstantValueOrLiteral(Ops, IsDeclIdx, ExtKind);
return getDIBuilder(DebugInst).createModule(
Scope, Name, ConfigMacros, IncludePath, ApiNotes, File, Line, IsDecl);
}
MDNode *SPIRVToLLVMDbgTran::transExpression(const SPIRVExtInst *DebugInst) {
const SPIRVWordVec &Args = DebugInst->getArguments();
std::vector<uint64_t> Ops;
for (SPIRVId A : Args) {
using namespace SPIRVDebug::Operand::Operation;
SPIRVExtInst *O = BM->get<SPIRVExtInst>(A);
const SPIRVWordVec &Operands = O->getArguments();
auto OpCode =
static_cast<SPIRVDebug::ExpressionOpCode>(getConstantValueOrLiteral(
Operands, OpCodeIdx, DebugInst->getExtSetKind()));
Ops.push_back(SPIRV::DbgExpressionOpCodeMap::rmap(OpCode));
for (unsigned I = 1, E = Operands.size(); I < E; ++I) {
Ops.push_back(
getConstantValueOrLiteral(Operands, I, DebugInst->getExtSetKind()));
}
}
ArrayRef<uint64_t> Addr(Ops.data(), Ops.size());
return getDIBuilder(DebugInst).createExpression(Addr);
}
DIType *
SPIRVToLLVMDbgTran::transNonNullDebugType(const SPIRVExtInst *DebugInst) {
if (DebugInst->getExtOp() != SPIRVDebug::DebugInfoNone)
return transDebugInst<DIType>(DebugInst);
return getDIBuilder(DebugInst).createUnspecifiedType("SPIRV unknown type");
}
MDNode *SPIRVToLLVMDbgTran::transDebugInstImpl(const SPIRVExtInst *DebugInst) {
switch (DebugInst->getExtOp()) {
case SPIRVDebug::DebugInfoNone:
return nullptr;
case SPIRVDebug::CompilationUnit:
return transCompilationUnit(DebugInst);
case SPIRVDebug::TypeBasic:
return transTypeBasic(DebugInst);
case SPIRVDebug::TypeQualifier:
return transTypeQualifier(DebugInst);
case SPIRVDebug::TypePointer:
return transTypePointer(DebugInst);
case SPIRVDebug::TypeArray:
return transTypeArray(DebugInst);
case SPIRVDebug::TypeSubrange:
return transTypeSubrange(DebugInst);
case SPIRVDebug::TypeString:
return transTypeString(DebugInst);
case SPIRVDebug::TypeVector:
return transTypeVector(DebugInst);
case SPIRVDebug::TypeComposite:
return transTypeComposite(DebugInst);
case SPIRVDebug::TypeMember:
return transTypeMember(DebugInst);
case SPIRVDebug::TypePtrToMember:
return transTypePtrToMember(DebugInst);
case SPIRVDebug::TypeEnum:
return transTypeEnum(DebugInst);
case SPIRVDebug::TypeFunction:
return transTypeFunction(DebugInst);
case SPIRVDebug::LexicalBlock:
return transLexicalBlock(DebugInst);
case SPIRVDebug::LexicalBlockDiscriminator:
return transLexicalBlockDiscriminator(DebugInst);
case SPIRVDebug::Function:
return transFunction(DebugInst);
case SPIRVDebug::FunctionDeclaration:
return transFunctionDecl(DebugInst);
case SPIRVDebug::FunctionDefinition:
return transFunctionDefinition(DebugInst);
case SPIRVDebug::EntryPoint:
return transEntryPoint(DebugInst);
case SPIRVDebug::GlobalVariable:
return transGlobalVariable(DebugInst);
case SPIRVDebug::LocalVariable:
return transLocalVariable(DebugInst);
case SPIRVDebug::Typedef:
return transTypedef(DebugInst);
case SPIRVDebug::InlinedAt:
return transDebugInlined(DebugInst);
case SPIRVDebug::TypeInheritance:
return transTypeInheritance(DebugInst);
case SPIRVDebug::TypeTemplateParameter:
return transTypeTemplateParameter(DebugInst);
case SPIRVDebug::TypeTemplateTemplateParameter:
return transTypeTemplateTemplateParameter(DebugInst);
case SPIRVDebug::TypeTemplateParameterPack:
return transTypeTemplateParameterPack(DebugInst);
case SPIRVDebug::TypeTemplate:
return transTypeTemplate(DebugInst);
case SPIRVDebug::ImportedEntity:
return transImportedEntry(DebugInst);
case SPIRVDebug::Module:
case SPIRVDebug::ModuleINTEL:
return transModule(DebugInst);
case SPIRVDebug::Operation: // To be translated with transExpression
case SPIRVDebug::Source: // To be used by other instructions
case SPIRVDebug::SourceContinued:
case SPIRVDebug::BuildIdentifier: // To be used by transCompilationUnit
case SPIRVDebug::StoragePath: // To be used by transCompilationUnit
return nullptr;
case SPIRVDebug::Expression:
return transExpression(DebugInst);
case SPIRVDebug::TypeArrayDynamic:
return transTypeArrayDynamic(DebugInst);
default:
llvm_unreachable("Not implemented SPIR-V debug instruction!");
}
}
DbgInstPtr
SPIRVToLLVMDbgTran::transDebugIntrinsic(const SPIRVExtInst *DebugInst,
BasicBlock *BB) {
auto GetLocalVar = [&](SPIRVId Id) -> std::pair<DILocalVariable *, DebugLoc> {
auto *LV = transDebugInst<DILocalVariable>(BM->get<SPIRVExtInst>(Id));
DebugLoc DL = DILocation::get(M->getContext(), LV->getLine(),
/*Column=*/0, LV->getScope());
return std::make_pair(LV, DL);
};
auto GetValue = [&](SPIRVId Id) -> Value * {
auto *V = BM->get<SPIRVValue>(Id);
return SPIRVReader->transValue(V, BB->getParent(), BB);
};
auto GetExpression = [&](SPIRVId Id) -> DIExpression * {
return transDebugInst<DIExpression>(BM->get<SPIRVExtInst>(Id));
};
SPIRVWordVec Ops = DebugInst->getArguments();
switch (DebugInst->getExtOp()) {
case SPIRVDebug::Scope:
case SPIRVDebug::NoScope:
case SPIRVDebug::FunctionDefinition:
return nullptr;
case SPIRVDebug::Declare: {
using namespace SPIRVDebug::Operand::DebugDeclare;
auto LocalVar = GetLocalVar(Ops[DebugLocalVarIdx]);
DIBuilder &DIB = getDIBuilder(DebugInst);
if (getDbgInst<SPIRVDebug::DebugInfoNone>(Ops[VariableIdx])) {
// If we don't have the variable(e.g. alloca might be promoted by mem2reg)
// we should generate the following IR:
// call void @llvm.dbg.declare(metadata !4, metadata !14, metadata !5)
// !4 = !{}
// DIBuilder::insertDeclare doesn't allow to pass nullptr for the Storage
// parameter. To work around this limitation we create a dummy temp
// alloca, use it to create llvm.dbg.declare, and then remove the alloca.
auto *AI = new AllocaInst(Type::getInt8Ty(M->getContext()), 0, "tmp", BB);
DbgInstPtr DbgDeclare = DIB.insertDeclare(
AI, LocalVar.first, GetExpression(Ops[ExpressionIdx]),
LocalVar.second, BB);
AI->eraseFromParent();
return DbgDeclare;
}
return DIB.insertDeclare(GetValue(Ops[VariableIdx]), LocalVar.first,
GetExpression(Ops[ExpressionIdx]), LocalVar.second,
BB);
}
case SPIRVDebug::Value: {
using namespace SPIRVDebug::Operand::DebugValue;
auto LocalVar = GetLocalVar(Ops[DebugLocalVarIdx]);
Value *Val = GetValue(Ops[ValueIdx]);
DIExpression *Expr = GetExpression(Ops[ExpressionIdx]);
DbgInstPtr DbgValIntr = getDIBuilder(DebugInst).insertDbgValueIntrinsic(
Val, LocalVar.first, Expr, LocalVar.second, BB);
std::vector<ValueAsMetadata *> MDs;
for (size_t I = 0; I != Expr->getNumLocationOperands(); ++I) {
MDs.emplace_back(ValueAsMetadata::get(Val));
}
if (!MDs.empty()) {
DIArgList *AL = DIArgList::get(M->getContext(), MDs);
if (M->IsNewDbgInfoFormat) {
cast<DbgVariableRecord>(DbgValIntr.get<DbgRecord *>())
->setRawLocation(AL);
} else {
cast<DbgVariableIntrinsic>(DbgValIntr.get<Instruction *>())
->setRawLocation(AL);
}
}
return DbgValIntr;
}
default:
llvm_unreachable("Unknown debug intrinsic!");
}
}
DebugLoc SPIRVToLLVMDbgTran::transDebugScope(const SPIRVInstruction *Inst) {
unsigned Line = 0;
unsigned Col = 0;
MDNode *Scope = nullptr;
MDNode *InlinedAt = nullptr;
// If DebugLine and OpLine are both active give DebugLine priority
if (auto DL = Inst->getDebugLine()) {
using namespace SPIRVDebug::Operand::DebugLine;
SPIRVWordVec DebugLineArgs = DL->getArguments();
Line =
getConstantValueOrLiteral(DebugLineArgs, StartIdx, DL->getExtSetKind());
Col = getConstantValueOrLiteral(DebugLineArgs, ColumnStartIdx,
DL->getExtSetKind());
} else if (auto L = Inst->getLine()) {
Line = L->getLine();
Col = L->getColumn();
}
if (SPIRVEntry *S = Inst->getDebugScope()) {
using namespace SPIRVDebug::Operand::Scope;
SPIRVExtInst *DbgScope = static_cast<SPIRVExtInst *>(S);
SPIRVWordVec Ops = DbgScope->getArguments();
Scope = getScope(BM->getEntry(Ops[ScopeIdx]));
if (Ops.size() > InlinedAtIdx)
InlinedAt = transDebugInst(BM->get<SPIRVExtInst>(Ops[InlinedAtIdx]));
return DILocation::get(M->getContext(), Line, Col, Scope, InlinedAt);
}
return DebugLoc();
}
MDNode *SPIRVToLLVMDbgTran::transDebugInlined(const SPIRVExtInst *DebugInst) {
// There is a Column operand in NonSemantic.Shader.200 spec
if (DebugInst->getExtSetKind() ==
SPIRV::SPIRVEIS_NonSemantic_Shader_DebugInfo_200)
return transDebugInlinedNonSemanticShader200(DebugInst);
using namespace SPIRVDebug::Operand::InlinedAt::OpenCL;
SPIRVWordVec Ops = DebugInst->getArguments();
assert(Ops.size() >= MinOperandCount && "Invalid number of operands");
SPIRVWord Line =
getConstantValueOrLiteral(Ops, LineIdx, DebugInst->getExtSetKind());
unsigned Col = 0; // DebugInlinedAt instruction has no column operand
DILocalScope *Scope =
cast<DILocalScope>(getScope(BM->getEntry(Ops[ScopeIdx])));
DILocation *InlinedAt = nullptr;
if (Ops.size() > InlinedIdx) {
InlinedAt =
transDebugInst<DILocation>(BM->get<SPIRVExtInst>(Ops[InlinedIdx]));
}
return DILocation::getDistinct(M->getContext(), Line, Col, Scope, InlinedAt);
}
MDNode *SPIRVToLLVMDbgTran::transDebugInlinedNonSemanticShader200(
const SPIRVExtInst *DebugInst) {
using namespace SPIRVDebug::Operand::InlinedAt::NonSemantic;
SPIRVWordVec Ops = DebugInst->getArguments();
assert(Ops.size() >= MinOperandCount && "Invalid number of operands");
SPIRVWord Line =
getConstantValueOrLiteral(Ops, LineIdx, DebugInst->getExtSetKind());
unsigned Col =
getConstantValueOrLiteral(Ops, ColumnIdx, DebugInst->getExtSetKind());
DILocalScope *Scope =
cast<DILocalScope>(getScope(BM->getEntry(Ops[ScopeIdx])));
DILocation *InlinedAt = nullptr;
if (Ops.size() > InlinedIdx) {
InlinedAt =
transDebugInst<DILocation>(BM->get<SPIRVExtInst>(Ops[InlinedIdx]));
}
return DILocation::getDistinct(M->getContext(), Line, Col, Scope, InlinedAt);
}
void SPIRVToLLVMDbgTran::finalize() {
if (!Enable)
return;
for (const auto &Builder : BuilderMap)
Builder.second->finalize();
}
DIFile *SPIRVToLLVMDbgTran::getFile(const SPIRVId SourceId) {
using namespace SPIRVDebug::Operand::Source;
SPIRVExtInst *Source = BM->get<SPIRVExtInst>(SourceId);
assert(Source->getExtOp() == SPIRVDebug::Source &&
"DebugSource instruction is expected");
SPIRVWordVec SourceArgs = Source->getArguments();
assert(SourceArgs.size() >= MinOperandCount && "Invalid number of operands");
if (SourceArgs.size() == MinOperandCount)
return getDIFile(getString(SourceArgs[FileIdx]));
if (!isNonSemanticDebugInfo(Source->getExtSetKind())) {
std::string ChecksumStr =
getDbgInst<SPIRVDebug::DebugInfoNone>(SourceArgs[TextIdx])
? ""
: getString(SourceArgs[TextIdx]);
return getDIFile(getString(SourceArgs[FileIdx]),
ParseChecksum(ChecksumStr));
}
std::optional<DIFile::ChecksumInfo<StringRef>> CS;
SPIRVWord StrIdx = SourceArgs[TextIdx];
if (Source->getExtSetKind() == SPIRVEIS_NonSemantic_Shader_DebugInfo_200) {
if (SourceArgs.size() >= MaxOperandCount - 1) {
// 2 optional parameters are ChecksumKind and ChecksumValue - they should
// go together
if (!getDbgInst<SPIRVDebug::DebugInfoNone>(SourceArgs[ChecksumKind]) &&
!getDbgInst<SPIRVDebug::DebugInfoNone>(SourceArgs[ChecksumValue])) {
llvm::DIFile::ChecksumKind Kind = SPIRV::DbgChecksumKindMap::rmap(
static_cast<SPIRVDebug::FileChecksumKind>(
BM->get<SPIRVConstant>(SourceArgs[ChecksumKind])
->getZExtIntValue()));
StringRef Checksum = getString(SourceArgs[ChecksumValue]);
size_t ChecksumEndPos = Checksum.find_if_not(llvm::isHexDigit);
CS.emplace(Kind, Checksum.substr(0, ChecksumEndPos));
}
}
// Among optional parameters - text is always the last one (either 1st or
// 3rd)
if (SourceArgs.size() == MaxOperandCount ||
SourceArgs.size() == MaxOperandCount - 2)
StrIdx = SourceArgs[TextNonSemIdx];
else
StrIdx = SPIRVID_INVALID;
}
return getDIFile(getString(SourceArgs[FileIdx]), CS,
getStringSourceContinued(StrIdx, Source));
}
void SPIRVToLLVMDbgTran::setBuildIdentifierAndStoragePath() {
#ifndef NDEBUG
bool FoundBuildIdentifier{false};
bool FoundStoragePath{false};
#endif
for (SPIRVExtInst *EI : BM->getDebugInstVec()) {
if (EI->getExtOp() == SPIRVDebug::BuildIdentifier) {
using namespace SPIRVDebug::Operand::BuildIdentifier;
SPIRVWordVec BuildIdentifierArgs = EI->getArguments();
assert(BuildIdentifierArgs.size() == OperandCount &&
"Invalid number of operands");
assert(!FoundBuildIdentifier &&
"More than one BuildIdentifier instruction not allowed");
BuildIdentifier = strtoull(
getString(BuildIdentifierArgs[IdentifierIdx]).c_str(), NULL, 10);
#ifndef NDEBUG
FoundBuildIdentifier = true;
#endif
} else if (EI->getExtOp() == SPIRVDebug::StoragePath) {
using namespace SPIRVDebug::Operand::StoragePath;
SPIRVWordVec StoragePathArgs = EI->getArguments();
assert(StoragePathArgs.size() == OperandCount &&
"Invalid number of operands");
assert(!FoundStoragePath &&
"More than one StoragePath instruction not allowed");
StoragePath = getString(StoragePathArgs[PathIdx]);
#ifndef NDEBUG
FoundStoragePath = true;
#endif
}
}
assert(((FoundBuildIdentifier && FoundStoragePath) ||
(!FoundBuildIdentifier && !FoundStoragePath)) &&
"BuildIdentifier and StoragePath must both be set or both unset");
}
DIBuilder &SPIRVToLLVMDbgTran::getDIBuilder(const SPIRVExtInst *DebugInst) {
assert(BuilderMap.size() != 0 && "No debug compile units");
if (BuilderMap.size() == 1)
return *BuilderMap.begin()->second;
while (DebugInst->getExtOp() != SPIRVDebug::CompilationUnit) {
if (DebugInst->getExtOp() == SPIRVDebug::DebugInfoNone)
return *BuilderMap.begin()->second;
const SPIRVWordVec &Ops = DebugInst->getArguments();
SPIRVWord ParentScopeIdx = 0;
if (!hasDbgInstParentScopeIdx(DebugInst->getExtOp(), ParentScopeIdx,
DebugInst->getExtSetKind()))
return *BuilderMap.begin()->second;
if (SPIRVEntry *Scope = BM->getEntry(Ops[ParentScopeIdx])) {
DebugInst = static_cast<SPIRVExtInst *>(Scope);
continue;
}
return *BuilderMap.begin()->second;
}
return *BuilderMap[DebugInst->getId()];
}
SPIRVToLLVMDbgTran::SplitFileName::SplitFileName(const string &FileName) {
auto Loc = FileName.find_last_of("/\\");
if (Loc != std::string::npos) {
BaseName = FileName.substr(Loc + 1);
Path = FileName.substr(0, Loc);
} else {
BaseName = FileName;
Path = ".";
}
}
std::string SPIRVToLLVMDbgTran::findModuleProducer() {
for (const auto &I : BM->getModuleProcessedVec()) {
if (I->getProcessStr().find(SPIRVDebug::ProducerPrefix) !=
std::string::npos) {
return I->getProcessStr().substr(SPIRVDebug::ProducerPrefix.size());
}
}
return "spirv";
}
std::optional<DIFile::ChecksumInfo<StringRef>>
SPIRVToLLVMDbgTran::ParseChecksum(StringRef Text) {
// Example of "Text" variable:
// "SomeInfo//__CSK_MD5:7bb56387968a9caa6e9e35fff94eaf7b:OtherInfo"
std::optional<DIFile::ChecksumInfo<StringRef>> CS;
auto KindPos = Text.find(SPIRVDebug::ChecksumKindPrefx);
if (KindPos != StringRef::npos) {
auto ColonPos = Text.find(":", KindPos);
KindPos += string("//__").size();
auto KindStr = Text.substr(KindPos, ColonPos - KindPos);
auto Checksum = Text.substr(ColonPos).ltrim(':');
if (auto Kind = DIFile::getChecksumKind(KindStr)) {
size_t ChecksumEndPos = Checksum.find_if_not(llvm::isHexDigit);
CS.emplace(Kind.value(), Checksum.substr(0, ChecksumEndPos));
}
}
return CS;
}
} // namespace SPIRV
|