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 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919
|
//===- ModuleTranslation.cpp - MLIR to LLVM conversion --------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
// This file implements the translation between an MLIR LLVM dialect module and
// the corresponding LLVMIR module. It only handles core LLVM IR operations.
//
//===----------------------------------------------------------------------===//
#include "mlir/Target/LLVMIR/ModuleTranslation.h"
#include "AttrKindDetail.h"
#include "DebugTranslation.h"
#include "LoopAnnotationTranslation.h"
#include "mlir/Analysis/TopologicalSortUtils.h"
#include "mlir/Dialect/DLTI/DLTI.h"
#include "mlir/Dialect/LLVMIR/LLVMDialect.h"
#include "mlir/Dialect/LLVMIR/LLVMInterfaces.h"
#include "mlir/Dialect/LLVMIR/Transforms/DIExpressionLegalization.h"
#include "mlir/Dialect/LLVMIR/Transforms/LegalizeForExport.h"
#include "mlir/Dialect/OpenMP/OpenMPDialect.h"
#include "mlir/Dialect/OpenMP/OpenMPInterfaces.h"
#include "mlir/IR/AttrTypeSubElements.h"
#include "mlir/IR/Attributes.h"
#include "mlir/IR/BuiltinOps.h"
#include "mlir/IR/BuiltinTypes.h"
#include "mlir/IR/DialectResourceBlobManager.h"
#include "mlir/IR/RegionGraphTraits.h"
#include "mlir/Support/LLVM.h"
#include "mlir/Target/LLVMIR/LLVMTranslationInterface.h"
#include "mlir/Target/LLVMIR/TypeToLLVM.h"
#include "llvm/ADT/PostOrderIterator.h"
#include "llvm/ADT/SetVector.h"
#include "llvm/ADT/StringExtras.h"
#include "llvm/ADT/TypeSwitch.h"
#include "llvm/Frontend/OpenMP/OMPIRBuilder.h"
#include "llvm/IR/BasicBlock.h"
#include "llvm/IR/CFG.h"
#include "llvm/IR/Constants.h"
#include "llvm/IR/DerivedTypes.h"
#include "llvm/IR/IRBuilder.h"
#include "llvm/IR/InlineAsm.h"
#include "llvm/IR/IntrinsicsNVPTX.h"
#include "llvm/IR/LLVMContext.h"
#include "llvm/IR/MDBuilder.h"
#include "llvm/IR/Module.h"
#include "llvm/IR/Verifier.h"
#include "llvm/Support/Debug.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/Transforms/Utils/BasicBlockUtils.h"
#include "llvm/Transforms/Utils/Cloning.h"
#include "llvm/Transforms/Utils/ModuleUtils.h"
#include <optional>
#define DEBUG_TYPE "llvm-dialect-to-llvm-ir"
using namespace mlir;
using namespace mlir::LLVM;
using namespace mlir::LLVM::detail;
extern llvm::cl::opt<bool> UseNewDbgInfoFormat;
#include "mlir/Dialect/LLVMIR/LLVMConversionEnumsToLLVM.inc"
namespace {
/// A customized inserter for LLVM's IRBuilder that captures all LLVM IR
/// instructions that are created for future reference.
///
/// This is intended to be used with the `CollectionScope` RAII object:
///
/// llvm::IRBuilder<..., InstructionCapturingInserter> builder;
/// {
/// InstructionCapturingInserter::CollectionScope scope(builder);
/// // Call IRBuilder methods as usual.
///
/// // This will return a list of all instructions created by the builder,
/// // in order of creation.
/// builder.getInserter().getCapturedInstructions();
/// }
/// // This will return an empty list.
/// builder.getInserter().getCapturedInstructions();
///
/// The capturing functionality is _disabled_ by default for performance
/// consideration. It needs to be explicitly enabled, which is achieved by
/// creating a `CollectionScope`.
class InstructionCapturingInserter : public llvm::IRBuilderCallbackInserter {
public:
/// Constructs the inserter.
InstructionCapturingInserter()
: llvm::IRBuilderCallbackInserter([this](llvm::Instruction *instruction) {
if (LLVM_LIKELY(enabled))
capturedInstructions.push_back(instruction);
}) {}
/// Returns the list of LLVM IR instructions captured since the last cleanup.
ArrayRef<llvm::Instruction *> getCapturedInstructions() const {
return capturedInstructions;
}
/// Clears the list of captured LLVM IR instructions.
void clearCapturedInstructions() { capturedInstructions.clear(); }
/// RAII object enabling the capture of created LLVM IR instructions.
class CollectionScope {
public:
/// Creates the scope for the given inserter.
CollectionScope(llvm::IRBuilderBase &irBuilder, bool isBuilderCapturing);
/// Ends the scope.
~CollectionScope();
ArrayRef<llvm::Instruction *> getCapturedInstructions() {
if (!inserter)
return {};
return inserter->getCapturedInstructions();
}
private:
/// Back reference to the inserter.
InstructionCapturingInserter *inserter = nullptr;
/// List of instructions in the inserter prior to this scope.
SmallVector<llvm::Instruction *> previouslyCollectedInstructions;
/// Whether the inserter was enabled prior to this scope.
bool wasEnabled;
};
/// Enable or disable the capturing mechanism.
void setEnabled(bool enabled = true) { this->enabled = enabled; }
private:
/// List of captured instructions.
SmallVector<llvm::Instruction *> capturedInstructions;
/// Whether the collection is enabled.
bool enabled = false;
};
using CapturingIRBuilder =
llvm::IRBuilder<llvm::ConstantFolder, InstructionCapturingInserter>;
} // namespace
InstructionCapturingInserter::CollectionScope::CollectionScope(
llvm::IRBuilderBase &irBuilder, bool isBuilderCapturing) {
if (!isBuilderCapturing)
return;
auto &capturingIRBuilder = static_cast<CapturingIRBuilder &>(irBuilder);
inserter = &capturingIRBuilder.getInserter();
wasEnabled = inserter->enabled;
if (wasEnabled)
previouslyCollectedInstructions.swap(inserter->capturedInstructions);
inserter->setEnabled(true);
}
InstructionCapturingInserter::CollectionScope::~CollectionScope() {
if (!inserter)
return;
previouslyCollectedInstructions.swap(inserter->capturedInstructions);
// If collection was enabled (likely in another, surrounding scope), keep
// the instructions collected in this scope.
if (wasEnabled) {
llvm::append_range(inserter->capturedInstructions,
previouslyCollectedInstructions);
}
inserter->setEnabled(wasEnabled);
}
/// Translates the given data layout spec attribute to the LLVM IR data layout.
/// Only integer, float, pointer and endianness entries are currently supported.
static FailureOr<llvm::DataLayout>
translateDataLayout(DataLayoutSpecInterface attribute,
const DataLayout &dataLayout,
std::optional<Location> loc = std::nullopt) {
if (!loc)
loc = UnknownLoc::get(attribute.getContext());
// Translate the endianness attribute.
std::string llvmDataLayout;
llvm::raw_string_ostream layoutStream(llvmDataLayout);
for (DataLayoutEntryInterface entry : attribute.getEntries()) {
auto key = llvm::dyn_cast_if_present<StringAttr>(entry.getKey());
if (!key)
continue;
if (key.getValue() == DLTIDialect::kDataLayoutEndiannessKey) {
auto value = cast<StringAttr>(entry.getValue());
bool isLittleEndian =
value.getValue() == DLTIDialect::kDataLayoutEndiannessLittle;
layoutStream << "-" << (isLittleEndian ? "e" : "E");
layoutStream.flush();
continue;
}
if (key.getValue() == DLTIDialect::kDataLayoutProgramMemorySpaceKey) {
auto value = cast<IntegerAttr>(entry.getValue());
uint64_t space = value.getValue().getZExtValue();
// Skip the default address space.
if (space == 0)
continue;
layoutStream << "-P" << space;
layoutStream.flush();
continue;
}
if (key.getValue() == DLTIDialect::kDataLayoutGlobalMemorySpaceKey) {
auto value = cast<IntegerAttr>(entry.getValue());
uint64_t space = value.getValue().getZExtValue();
// Skip the default address space.
if (space == 0)
continue;
layoutStream << "-G" << space;
layoutStream.flush();
continue;
}
if (key.getValue() == DLTIDialect::kDataLayoutAllocaMemorySpaceKey) {
auto value = cast<IntegerAttr>(entry.getValue());
uint64_t space = value.getValue().getZExtValue();
// Skip the default address space.
if (space == 0)
continue;
layoutStream << "-A" << space;
layoutStream.flush();
continue;
}
if (key.getValue() == DLTIDialect::kDataLayoutStackAlignmentKey) {
auto value = cast<IntegerAttr>(entry.getValue());
uint64_t alignment = value.getValue().getZExtValue();
// Skip the default stack alignment.
if (alignment == 0)
continue;
layoutStream << "-S" << alignment;
layoutStream.flush();
continue;
}
emitError(*loc) << "unsupported data layout key " << key;
return failure();
}
// Go through the list of entries to check which types are explicitly
// specified in entries. Where possible, data layout queries are used instead
// of directly inspecting the entries.
for (DataLayoutEntryInterface entry : attribute.getEntries()) {
auto type = llvm::dyn_cast_if_present<Type>(entry.getKey());
if (!type)
continue;
// Data layout for the index type is irrelevant at this point.
if (isa<IndexType>(type))
continue;
layoutStream << "-";
LogicalResult result =
llvm::TypeSwitch<Type, LogicalResult>(type)
.Case<IntegerType, Float16Type, Float32Type, Float64Type,
Float80Type, Float128Type>([&](Type type) -> LogicalResult {
if (auto intType = dyn_cast<IntegerType>(type)) {
if (intType.getSignedness() != IntegerType::Signless)
return emitError(*loc)
<< "unsupported data layout for non-signless integer "
<< intType;
layoutStream << "i";
} else {
layoutStream << "f";
}
uint64_t size = dataLayout.getTypeSizeInBits(type);
uint64_t abi = dataLayout.getTypeABIAlignment(type) * 8u;
uint64_t preferred =
dataLayout.getTypePreferredAlignment(type) * 8u;
layoutStream << size << ":" << abi;
if (abi != preferred)
layoutStream << ":" << preferred;
return success();
})
.Case([&](LLVMPointerType type) {
layoutStream << "p" << type.getAddressSpace() << ":";
uint64_t size = dataLayout.getTypeSizeInBits(type);
uint64_t abi = dataLayout.getTypeABIAlignment(type) * 8u;
uint64_t preferred =
dataLayout.getTypePreferredAlignment(type) * 8u;
uint64_t index = *dataLayout.getTypeIndexBitwidth(type);
layoutStream << size << ":" << abi << ":" << preferred << ":"
<< index;
return success();
})
.Default([loc](Type type) {
return emitError(*loc)
<< "unsupported type in data layout: " << type;
});
if (failed(result))
return failure();
}
layoutStream.flush();
StringRef layoutSpec(llvmDataLayout);
if (layoutSpec.starts_with("-"))
layoutSpec = layoutSpec.drop_front();
return llvm::DataLayout(layoutSpec);
}
/// Builds a constant of a sequential LLVM type `type`, potentially containing
/// other sequential types recursively, from the individual constant values
/// provided in `constants`. `shape` contains the number of elements in nested
/// sequential types. Reports errors at `loc` and returns nullptr on error.
static llvm::Constant *
buildSequentialConstant(ArrayRef<llvm::Constant *> &constants,
ArrayRef<int64_t> shape, llvm::Type *type,
Location loc) {
if (shape.empty()) {
llvm::Constant *result = constants.front();
constants = constants.drop_front();
return result;
}
llvm::Type *elementType;
if (auto *arrayTy = dyn_cast<llvm::ArrayType>(type)) {
elementType = arrayTy->getElementType();
} else if (auto *vectorTy = dyn_cast<llvm::VectorType>(type)) {
elementType = vectorTy->getElementType();
} else {
emitError(loc) << "expected sequential LLVM types wrapping a scalar";
return nullptr;
}
SmallVector<llvm::Constant *, 8> nested;
nested.reserve(shape.front());
for (int64_t i = 0; i < shape.front(); ++i) {
nested.push_back(buildSequentialConstant(constants, shape.drop_front(),
elementType, loc));
if (!nested.back())
return nullptr;
}
if (shape.size() == 1 && type->isVectorTy())
return llvm::ConstantVector::get(nested);
return llvm::ConstantArray::get(
llvm::ArrayType::get(elementType, shape.front()), nested);
}
/// Returns the first non-sequential type nested in sequential types.
static llvm::Type *getInnermostElementType(llvm::Type *type) {
do {
if (auto *arrayTy = dyn_cast<llvm::ArrayType>(type)) {
type = arrayTy->getElementType();
} else if (auto *vectorTy = dyn_cast<llvm::VectorType>(type)) {
type = vectorTy->getElementType();
} else {
return type;
}
} while (true);
}
/// Convert a dense elements attribute to an LLVM IR constant using its raw data
/// storage if possible. This supports elements attributes of tensor or vector
/// type and avoids constructing separate objects for individual values of the
/// innermost dimension. Constants for other dimensions are still constructed
/// recursively. Returns null if constructing from raw data is not supported for
/// this type, e.g., element type is not a power-of-two-sized primitive. Reports
/// other errors at `loc`.
static llvm::Constant *
convertDenseElementsAttr(Location loc, DenseElementsAttr denseElementsAttr,
llvm::Type *llvmType,
const ModuleTranslation &moduleTranslation) {
if (!denseElementsAttr)
return nullptr;
llvm::Type *innermostLLVMType = getInnermostElementType(llvmType);
if (!llvm::ConstantDataSequential::isElementTypeCompatible(innermostLLVMType))
return nullptr;
ShapedType type = denseElementsAttr.getType();
if (type.getNumElements() == 0)
return nullptr;
// Check that the raw data size matches what is expected for the scalar size.
// TODO: in theory, we could repack the data here to keep constructing from
// raw data.
// TODO: we may also need to consider endianness when cross-compiling to an
// architecture where it is different.
int64_t elementByteSize = denseElementsAttr.getRawData().size() /
denseElementsAttr.getNumElements();
if (8 * elementByteSize != innermostLLVMType->getScalarSizeInBits())
return nullptr;
// Compute the shape of all dimensions but the innermost. Note that the
// innermost dimension may be that of the vector element type.
bool hasVectorElementType = isa<VectorType>(type.getElementType());
int64_t numAggregates =
denseElementsAttr.getNumElements() /
(hasVectorElementType ? 1
: denseElementsAttr.getType().getShape().back());
ArrayRef<int64_t> outerShape = type.getShape();
if (!hasVectorElementType)
outerShape = outerShape.drop_back();
// Handle the case of vector splat, LLVM has special support for it.
if (denseElementsAttr.isSplat() &&
(isa<VectorType>(type) || hasVectorElementType)) {
llvm::Constant *splatValue = LLVM::detail::getLLVMConstant(
innermostLLVMType, denseElementsAttr.getSplatValue<Attribute>(), loc,
moduleTranslation);
llvm::Constant *splatVector =
llvm::ConstantDataVector::getSplat(0, splatValue);
SmallVector<llvm::Constant *> constants(numAggregates, splatVector);
ArrayRef<llvm::Constant *> constantsRef = constants;
return buildSequentialConstant(constantsRef, outerShape, llvmType, loc);
}
if (denseElementsAttr.isSplat())
return nullptr;
// In case of non-splat, create a constructor for the innermost constant from
// a piece of raw data.
std::function<llvm::Constant *(StringRef)> buildCstData;
if (isa<TensorType>(type)) {
auto vectorElementType = dyn_cast<VectorType>(type.getElementType());
if (vectorElementType && vectorElementType.getRank() == 1) {
buildCstData = [&](StringRef data) {
return llvm::ConstantDataVector::getRaw(
data, vectorElementType.getShape().back(), innermostLLVMType);
};
} else if (!vectorElementType) {
buildCstData = [&](StringRef data) {
return llvm::ConstantDataArray::getRaw(data, type.getShape().back(),
innermostLLVMType);
};
}
} else if (isa<VectorType>(type)) {
buildCstData = [&](StringRef data) {
return llvm::ConstantDataVector::getRaw(data, type.getShape().back(),
innermostLLVMType);
};
}
if (!buildCstData)
return nullptr;
// Create innermost constants and defer to the default constant creation
// mechanism for other dimensions.
SmallVector<llvm::Constant *> constants;
int64_t aggregateSize = denseElementsAttr.getType().getShape().back() *
(innermostLLVMType->getScalarSizeInBits() / 8);
constants.reserve(numAggregates);
for (unsigned i = 0; i < numAggregates; ++i) {
StringRef data(denseElementsAttr.getRawData().data() + i * aggregateSize,
aggregateSize);
constants.push_back(buildCstData(data));
}
ArrayRef<llvm::Constant *> constantsRef = constants;
return buildSequentialConstant(constantsRef, outerShape, llvmType, loc);
}
/// Convert a dense resource elements attribute to an LLVM IR constant using its
/// raw data storage if possible. This supports elements attributes of tensor or
/// vector type and avoids constructing separate objects for individual values
/// of the innermost dimension. Constants for other dimensions are still
/// constructed recursively. Returns nullptr on failure and emits errors at
/// `loc`.
static llvm::Constant *convertDenseResourceElementsAttr(
Location loc, DenseResourceElementsAttr denseResourceAttr,
llvm::Type *llvmType, const ModuleTranslation &moduleTranslation) {
assert(denseResourceAttr && "expected non-null attribute");
llvm::Type *innermostLLVMType = getInnermostElementType(llvmType);
if (!llvm::ConstantDataSequential::isElementTypeCompatible(
innermostLLVMType)) {
emitError(loc, "no known conversion for innermost element type");
return nullptr;
}
ShapedType type = denseResourceAttr.getType();
assert(type.getNumElements() > 0 && "Expected non-empty elements attribute");
AsmResourceBlob *blob = denseResourceAttr.getRawHandle().getBlob();
if (!blob) {
emitError(loc, "resource does not exist");
return nullptr;
}
ArrayRef<char> rawData = blob->getData();
// Check that the raw data size matches what is expected for the scalar size.
// TODO: in theory, we could repack the data here to keep constructing from
// raw data.
// TODO: we may also need to consider endianness when cross-compiling to an
// architecture where it is different.
int64_t numElements = denseResourceAttr.getType().getNumElements();
int64_t elementByteSize = rawData.size() / numElements;
if (8 * elementByteSize != innermostLLVMType->getScalarSizeInBits()) {
emitError(loc, "raw data size does not match element type size");
return nullptr;
}
// Compute the shape of all dimensions but the innermost. Note that the
// innermost dimension may be that of the vector element type.
bool hasVectorElementType = isa<VectorType>(type.getElementType());
int64_t numAggregates =
numElements / (hasVectorElementType
? 1
: denseResourceAttr.getType().getShape().back());
ArrayRef<int64_t> outerShape = type.getShape();
if (!hasVectorElementType)
outerShape = outerShape.drop_back();
// Create a constructor for the innermost constant from a piece of raw data.
std::function<llvm::Constant *(StringRef)> buildCstData;
if (isa<TensorType>(type)) {
auto vectorElementType = dyn_cast<VectorType>(type.getElementType());
if (vectorElementType && vectorElementType.getRank() == 1) {
buildCstData = [&](StringRef data) {
return llvm::ConstantDataVector::getRaw(
data, vectorElementType.getShape().back(), innermostLLVMType);
};
} else if (!vectorElementType) {
buildCstData = [&](StringRef data) {
return llvm::ConstantDataArray::getRaw(data, type.getShape().back(),
innermostLLVMType);
};
}
} else if (isa<VectorType>(type)) {
buildCstData = [&](StringRef data) {
return llvm::ConstantDataVector::getRaw(data, type.getShape().back(),
innermostLLVMType);
};
}
if (!buildCstData) {
emitError(loc, "unsupported dense_resource type");
return nullptr;
}
// Create innermost constants and defer to the default constant creation
// mechanism for other dimensions.
SmallVector<llvm::Constant *> constants;
int64_t aggregateSize = denseResourceAttr.getType().getShape().back() *
(innermostLLVMType->getScalarSizeInBits() / 8);
constants.reserve(numAggregates);
for (unsigned i = 0; i < numAggregates; ++i) {
StringRef data(rawData.data() + i * aggregateSize, aggregateSize);
constants.push_back(buildCstData(data));
}
ArrayRef<llvm::Constant *> constantsRef = constants;
return buildSequentialConstant(constantsRef, outerShape, llvmType, loc);
}
/// Create an LLVM IR constant of `llvmType` from the MLIR attribute `attr`.
/// This currently supports integer, floating point, splat and dense element
/// attributes and combinations thereof. Also, an array attribute with two
/// elements is supported to represent a complex constant. In case of error,
/// report it to `loc` and return nullptr.
llvm::Constant *mlir::LLVM::detail::getLLVMConstant(
llvm::Type *llvmType, Attribute attr, Location loc,
const ModuleTranslation &moduleTranslation) {
if (!attr)
return llvm::UndefValue::get(llvmType);
if (auto *structType = dyn_cast<::llvm::StructType>(llvmType)) {
auto arrayAttr = dyn_cast<ArrayAttr>(attr);
if (!arrayAttr || arrayAttr.size() != 2) {
emitError(loc, "expected struct type to be a complex number");
return nullptr;
}
llvm::Type *elementType = structType->getElementType(0);
llvm::Constant *real =
getLLVMConstant(elementType, arrayAttr[0], loc, moduleTranslation);
if (!real)
return nullptr;
llvm::Constant *imag =
getLLVMConstant(elementType, arrayAttr[1], loc, moduleTranslation);
if (!imag)
return nullptr;
return llvm::ConstantStruct::get(structType, {real, imag});
}
// For integer types, we allow a mismatch in sizes as the index type in
// MLIR might have a different size than the index type in the LLVM module.
if (auto intAttr = dyn_cast<IntegerAttr>(attr))
return llvm::ConstantInt::get(
llvmType,
intAttr.getValue().sextOrTrunc(llvmType->getIntegerBitWidth()));
if (auto floatAttr = dyn_cast<FloatAttr>(attr)) {
const llvm::fltSemantics &sem = floatAttr.getValue().getSemantics();
// Special case for 8-bit floats, which are represented by integers due to
// the lack of native fp8 types in LLVM at the moment. Additionally, handle
// targets (like AMDGPU) that don't implement bfloat and convert all bfloats
// to i16.
unsigned floatWidth = APFloat::getSizeInBits(sem);
if (llvmType->isIntegerTy(floatWidth))
return llvm::ConstantInt::get(llvmType,
floatAttr.getValue().bitcastToAPInt());
if (llvmType !=
llvm::Type::getFloatingPointTy(llvmType->getContext(),
floatAttr.getValue().getSemantics())) {
emitError(loc, "FloatAttr does not match expected type of the constant");
return nullptr;
}
return llvm::ConstantFP::get(llvmType, floatAttr.getValue());
}
if (auto funcAttr = dyn_cast<FlatSymbolRefAttr>(attr))
return llvm::ConstantExpr::getBitCast(
moduleTranslation.lookupFunction(funcAttr.getValue()), llvmType);
if (auto splatAttr = dyn_cast<SplatElementsAttr>(attr)) {
llvm::Type *elementType;
uint64_t numElements;
bool isScalable = false;
if (auto *arrayTy = dyn_cast<llvm::ArrayType>(llvmType)) {
elementType = arrayTy->getElementType();
numElements = arrayTy->getNumElements();
} else if (auto *fVectorTy = dyn_cast<llvm::FixedVectorType>(llvmType)) {
elementType = fVectorTy->getElementType();
numElements = fVectorTy->getNumElements();
} else if (auto *sVectorTy = dyn_cast<llvm::ScalableVectorType>(llvmType)) {
elementType = sVectorTy->getElementType();
numElements = sVectorTy->getMinNumElements();
isScalable = true;
} else {
llvm_unreachable("unrecognized constant vector type");
}
// Splat value is a scalar. Extract it only if the element type is not
// another sequence type. The recursion terminates because each step removes
// one outer sequential type.
bool elementTypeSequential =
isa<llvm::ArrayType, llvm::VectorType>(elementType);
llvm::Constant *child = getLLVMConstant(
elementType,
elementTypeSequential ? splatAttr
: splatAttr.getSplatValue<Attribute>(),
loc, moduleTranslation);
if (!child)
return nullptr;
if (llvmType->isVectorTy())
return llvm::ConstantVector::getSplat(
llvm::ElementCount::get(numElements, /*Scalable=*/isScalable), child);
if (llvmType->isArrayTy()) {
auto *arrayType = llvm::ArrayType::get(elementType, numElements);
if (child->isZeroValue()) {
return llvm::ConstantAggregateZero::get(arrayType);
} else {
if (llvm::ConstantDataSequential::isElementTypeCompatible(
elementType)) {
// TODO: Handle all compatible types. This code only handles integer.
if (isa<llvm::IntegerType>(elementType)) {
if (llvm::ConstantInt *ci = dyn_cast<llvm::ConstantInt>(child)) {
if (ci->getBitWidth() == 8) {
SmallVector<int8_t> constants(numElements, ci->getZExtValue());
return llvm::ConstantDataArray::get(elementType->getContext(),
constants);
}
if (ci->getBitWidth() == 16) {
SmallVector<int16_t> constants(numElements, ci->getZExtValue());
return llvm::ConstantDataArray::get(elementType->getContext(),
constants);
}
if (ci->getBitWidth() == 32) {
SmallVector<int32_t> constants(numElements, ci->getZExtValue());
return llvm::ConstantDataArray::get(elementType->getContext(),
constants);
}
if (ci->getBitWidth() == 64) {
SmallVector<int64_t> constants(numElements, ci->getZExtValue());
return llvm::ConstantDataArray::get(elementType->getContext(),
constants);
}
}
}
}
// std::vector is used here to accomodate large number of elements that
// exceed SmallVector capacity.
std::vector<llvm::Constant *> constants(numElements, child);
return llvm::ConstantArray::get(arrayType, constants);
}
}
}
// Try using raw elements data if possible.
if (llvm::Constant *result =
convertDenseElementsAttr(loc, dyn_cast<DenseElementsAttr>(attr),
llvmType, moduleTranslation)) {
return result;
}
if (auto denseResourceAttr = dyn_cast<DenseResourceElementsAttr>(attr)) {
return convertDenseResourceElementsAttr(loc, denseResourceAttr, llvmType,
moduleTranslation);
}
// Fall back to element-by-element construction otherwise.
if (auto elementsAttr = dyn_cast<ElementsAttr>(attr)) {
assert(elementsAttr.getShapedType().hasStaticShape());
assert(!elementsAttr.getShapedType().getShape().empty() &&
"unexpected empty elements attribute shape");
SmallVector<llvm::Constant *, 8> constants;
constants.reserve(elementsAttr.getNumElements());
llvm::Type *innermostType = getInnermostElementType(llvmType);
for (auto n : elementsAttr.getValues<Attribute>()) {
constants.push_back(
getLLVMConstant(innermostType, n, loc, moduleTranslation));
if (!constants.back())
return nullptr;
}
ArrayRef<llvm::Constant *> constantsRef = constants;
llvm::Constant *result = buildSequentialConstant(
constantsRef, elementsAttr.getShapedType().getShape(), llvmType, loc);
assert(constantsRef.empty() && "did not consume all elemental constants");
return result;
}
if (auto stringAttr = dyn_cast<StringAttr>(attr)) {
return llvm::ConstantDataArray::get(
moduleTranslation.getLLVMContext(),
ArrayRef<char>{stringAttr.getValue().data(),
stringAttr.getValue().size()});
}
emitError(loc, "unsupported constant value");
return nullptr;
}
ModuleTranslation::ModuleTranslation(Operation *module,
std::unique_ptr<llvm::Module> llvmModule)
: mlirModule(module), llvmModule(std::move(llvmModule)),
debugTranslation(
std::make_unique<DebugTranslation>(module, *this->llvmModule)),
loopAnnotationTranslation(std::make_unique<LoopAnnotationTranslation>(
*this, *this->llvmModule)),
typeTranslator(this->llvmModule->getContext()),
iface(module->getContext()) {
assert(satisfiesLLVMModule(mlirModule) &&
"mlirModule should honor LLVM's module semantics.");
}
ModuleTranslation::~ModuleTranslation() {
if (ompBuilder)
ompBuilder->finalize();
}
void ModuleTranslation::forgetMapping(Region ®ion) {
SmallVector<Region *> toProcess;
toProcess.push_back(®ion);
while (!toProcess.empty()) {
Region *current = toProcess.pop_back_val();
for (Block &block : *current) {
blockMapping.erase(&block);
for (Value arg : block.getArguments())
valueMapping.erase(arg);
for (Operation &op : block) {
for (Value value : op.getResults())
valueMapping.erase(value);
if (op.hasSuccessors())
branchMapping.erase(&op);
if (isa<LLVM::GlobalOp>(op))
globalsMapping.erase(&op);
if (isa<LLVM::CallOp>(op))
callMapping.erase(&op);
llvm::append_range(
toProcess,
llvm::map_range(op.getRegions(), [](Region &r) { return &r; }));
}
}
}
}
/// Get the SSA value passed to the current block from the terminator operation
/// of its predecessor.
static Value getPHISourceValue(Block *current, Block *pred,
unsigned numArguments, unsigned index) {
Operation &terminator = *pred->getTerminator();
if (isa<LLVM::BrOp>(terminator))
return terminator.getOperand(index);
#ifndef NDEBUG
llvm::SmallPtrSet<Block *, 4> seenSuccessors;
for (unsigned i = 0, e = terminator.getNumSuccessors(); i < e; ++i) {
Block *successor = terminator.getSuccessor(i);
auto branch = cast<BranchOpInterface>(terminator);
SuccessorOperands successorOperands = branch.getSuccessorOperands(i);
assert(
(!seenSuccessors.contains(successor) || successorOperands.empty()) &&
"successors with arguments in LLVM branches must be different blocks");
seenSuccessors.insert(successor);
}
#endif
// For instructions that branch based on a condition value, we need to take
// the operands for the branch that was taken.
if (auto condBranchOp = dyn_cast<LLVM::CondBrOp>(terminator)) {
// For conditional branches, we take the operands from either the "true" or
// the "false" branch.
return condBranchOp.getSuccessor(0) == current
? condBranchOp.getTrueDestOperands()[index]
: condBranchOp.getFalseDestOperands()[index];
}
if (auto switchOp = dyn_cast<LLVM::SwitchOp>(terminator)) {
// For switches, we take the operands from either the default case, or from
// the case branch that was taken.
if (switchOp.getDefaultDestination() == current)
return switchOp.getDefaultOperands()[index];
for (const auto &i : llvm::enumerate(switchOp.getCaseDestinations()))
if (i.value() == current)
return switchOp.getCaseOperands(i.index())[index];
}
if (auto invokeOp = dyn_cast<LLVM::InvokeOp>(terminator)) {
return invokeOp.getNormalDest() == current
? invokeOp.getNormalDestOperands()[index]
: invokeOp.getUnwindDestOperands()[index];
}
llvm_unreachable(
"only branch, switch or invoke operations can be terminators "
"of a block that has successors");
}
/// Connect the PHI nodes to the results of preceding blocks.
void mlir::LLVM::detail::connectPHINodes(Region ®ion,
const ModuleTranslation &state) {
// Skip the first block, it cannot be branched to and its arguments correspond
// to the arguments of the LLVM function.
for (Block &bb : llvm::drop_begin(region)) {
llvm::BasicBlock *llvmBB = state.lookupBlock(&bb);
auto phis = llvmBB->phis();
auto numArguments = bb.getNumArguments();
assert(numArguments == std::distance(phis.begin(), phis.end()));
for (auto [index, phiNode] : llvm::enumerate(phis)) {
for (auto *pred : bb.getPredecessors()) {
// Find the LLVM IR block that contains the converted terminator
// instruction and use it in the PHI node. Note that this block is not
// necessarily the same as state.lookupBlock(pred), some operations
// (in particular, OpenMP operations using OpenMPIRBuilder) may have
// split the blocks.
llvm::Instruction *terminator =
state.lookupBranch(pred->getTerminator());
assert(terminator && "missing the mapping for a terminator");
phiNode.addIncoming(state.lookupValue(getPHISourceValue(
&bb, pred, numArguments, index)),
terminator->getParent());
}
}
}
}
llvm::CallInst *mlir::LLVM::detail::createIntrinsicCall(
llvm::IRBuilderBase &builder, llvm::Intrinsic::ID intrinsic,
ArrayRef<llvm::Value *> args, ArrayRef<llvm::Type *> tys) {
llvm::Module *module = builder.GetInsertBlock()->getModule();
llvm::Function *fn = llvm::Intrinsic::getDeclaration(module, intrinsic, tys);
return builder.CreateCall(fn, args);
}
llvm::CallInst *mlir::LLVM::detail::createIntrinsicCall(
llvm::IRBuilderBase &builder, ModuleTranslation &moduleTranslation,
Operation *intrOp, llvm::Intrinsic::ID intrinsic, unsigned numResults,
ArrayRef<unsigned> overloadedResults, ArrayRef<unsigned> overloadedOperands,
ArrayRef<unsigned> immArgPositions,
ArrayRef<StringLiteral> immArgAttrNames) {
assert(immArgPositions.size() == immArgAttrNames.size() &&
"LLVM `immArgPositions` and MLIR `immArgAttrNames` should have equal "
"length");
// Map operands and attributes to LLVM values.
auto operands = moduleTranslation.lookupValues(intrOp->getOperands());
SmallVector<llvm::Value *> args(immArgPositions.size() + operands.size());
for (auto [immArgPos, immArgName] :
llvm::zip(immArgPositions, immArgAttrNames)) {
auto attr = llvm::cast<TypedAttr>(intrOp->getAttr(immArgName));
assert(attr.getType().isIntOrFloat() && "expected int or float immarg");
auto *type = moduleTranslation.convertType(attr.getType());
args[immArgPos] = LLVM::detail::getLLVMConstant(
type, attr, intrOp->getLoc(), moduleTranslation);
}
unsigned opArg = 0;
for (auto &arg : args) {
if (!arg)
arg = operands[opArg++];
}
// Resolve overloaded intrinsic declaration.
SmallVector<llvm::Type *> overloadedTypes;
for (unsigned overloadedResultIdx : overloadedResults) {
if (numResults > 1) {
// More than one result is mapped to an LLVM struct.
overloadedTypes.push_back(moduleTranslation.convertType(
llvm::cast<LLVM::LLVMStructType>(intrOp->getResult(0).getType())
.getBody()[overloadedResultIdx]));
} else {
overloadedTypes.push_back(
moduleTranslation.convertType(intrOp->getResult(0).getType()));
}
}
for (unsigned overloadedOperandIdx : overloadedOperands)
overloadedTypes.push_back(args[overloadedOperandIdx]->getType());
llvm::Module *module = builder.GetInsertBlock()->getModule();
llvm::Function *llvmIntr =
llvm::Intrinsic::getDeclaration(module, intrinsic, overloadedTypes);
return builder.CreateCall(llvmIntr, args);
}
/// Given a single MLIR operation, create the corresponding LLVM IR operation
/// using the `builder`.
LogicalResult ModuleTranslation::convertOperation(Operation &op,
llvm::IRBuilderBase &builder,
bool recordInsertions) {
const LLVMTranslationDialectInterface *opIface = iface.getInterfaceFor(&op);
if (!opIface)
return op.emitError("cannot be converted to LLVM IR: missing "
"`LLVMTranslationDialectInterface` registration for "
"dialect for op: ")
<< op.getName();
InstructionCapturingInserter::CollectionScope scope(builder,
recordInsertions);
if (failed(opIface->convertOperation(&op, builder, *this)))
return op.emitError("LLVM Translation failed for operation: ")
<< op.getName();
return convertDialectAttributes(&op, scope.getCapturedInstructions());
}
/// Convert block to LLVM IR. Unless `ignoreArguments` is set, emit PHI nodes
/// to define values corresponding to the MLIR block arguments. These nodes
/// are not connected to the source basic blocks, which may not exist yet. Uses
/// `builder` to construct the LLVM IR. Expects the LLVM IR basic block to have
/// been created for `bb` and included in the block mapping. Inserts new
/// instructions at the end of the block and leaves `builder` in a state
/// suitable for further insertion into the end of the block.
LogicalResult ModuleTranslation::convertBlockImpl(Block &bb,
bool ignoreArguments,
llvm::IRBuilderBase &builder,
bool recordInsertions) {
builder.SetInsertPoint(lookupBlock(&bb));
auto *subprogram = builder.GetInsertBlock()->getParent()->getSubprogram();
// Before traversing operations, make block arguments available through
// value remapping and PHI nodes, but do not add incoming edges for the PHI
// nodes just yet: those values may be defined by this or following blocks.
// This step is omitted if "ignoreArguments" is set. The arguments of the
// first block have been already made available through the remapping of
// LLVM function arguments.
if (!ignoreArguments) {
auto predecessors = bb.getPredecessors();
unsigned numPredecessors =
std::distance(predecessors.begin(), predecessors.end());
for (auto arg : bb.getArguments()) {
auto wrappedType = arg.getType();
if (!isCompatibleType(wrappedType))
return emitError(bb.front().getLoc(),
"block argument does not have an LLVM type");
llvm::Type *type = convertType(wrappedType);
llvm::PHINode *phi = builder.CreatePHI(type, numPredecessors);
mapValue(arg, phi);
}
}
// Traverse operations.
for (auto &op : bb) {
// Set the current debug location within the builder.
builder.SetCurrentDebugLocation(
debugTranslation->translateLoc(op.getLoc(), subprogram));
if (failed(convertOperation(op, builder, recordInsertions)))
return failure();
// Set the branch weight metadata on the translated instruction.
if (auto iface = dyn_cast<BranchWeightOpInterface>(op))
setBranchWeightsMetadata(iface);
}
return success();
}
/// A helper method to get the single Block in an operation honoring LLVM's
/// module requirements.
static Block &getModuleBody(Operation *module) {
return module->getRegion(0).front();
}
/// A helper method to decide if a constant must not be set as a global variable
/// initializer. For an external linkage variable, the variable with an
/// initializer is considered externally visible and defined in this module, the
/// variable without an initializer is externally available and is defined
/// elsewhere.
static bool shouldDropGlobalInitializer(llvm::GlobalValue::LinkageTypes linkage,
llvm::Constant *cst) {
return (linkage == llvm::GlobalVariable::ExternalLinkage && !cst) ||
linkage == llvm::GlobalVariable::ExternalWeakLinkage;
}
/// Sets the runtime preemption specifier of `gv` to dso_local if
/// `dsoLocalRequested` is true, otherwise it is left unchanged.
static void addRuntimePreemptionSpecifier(bool dsoLocalRequested,
llvm::GlobalValue *gv) {
if (dsoLocalRequested)
gv->setDSOLocal(true);
}
/// Create named global variables that correspond to llvm.mlir.global
/// definitions. Convert llvm.global_ctors and global_dtors ops.
LogicalResult ModuleTranslation::convertGlobals() {
// Mapping from compile unit to its respective set of global variables.
DenseMap<llvm::DICompileUnit *, SmallVector<llvm::Metadata *>> allGVars;
for (auto op : getModuleBody(mlirModule).getOps<LLVM::GlobalOp>()) {
llvm::Type *type = convertType(op.getType());
llvm::Constant *cst = nullptr;
if (op.getValueOrNull()) {
// String attributes are treated separately because they cannot appear as
// in-function constants and are thus not supported by getLLVMConstant.
if (auto strAttr = dyn_cast_or_null<StringAttr>(op.getValueOrNull())) {
cst = llvm::ConstantDataArray::getString(
llvmModule->getContext(), strAttr.getValue(), /*AddNull=*/false);
type = cst->getType();
} else if (!(cst = getLLVMConstant(type, op.getValueOrNull(), op.getLoc(),
*this))) {
return failure();
}
}
auto linkage = convertLinkageToLLVM(op.getLinkage());
auto addrSpace = op.getAddrSpace();
// LLVM IR requires constant with linkage other than external or weak
// external to have initializers. If MLIR does not provide an initializer,
// default to undef.
bool dropInitializer = shouldDropGlobalInitializer(linkage, cst);
if (!dropInitializer && !cst)
cst = llvm::UndefValue::get(type);
else if (dropInitializer && cst)
cst = nullptr;
auto *var = new llvm::GlobalVariable(
*llvmModule, type, op.getConstant(), linkage, cst, op.getSymName(),
/*InsertBefore=*/nullptr,
op.getThreadLocal_() ? llvm::GlobalValue::GeneralDynamicTLSModel
: llvm::GlobalValue::NotThreadLocal,
addrSpace);
if (std::optional<mlir::SymbolRefAttr> comdat = op.getComdat()) {
auto selectorOp = cast<ComdatSelectorOp>(
SymbolTable::lookupNearestSymbolFrom(op, *comdat));
var->setComdat(comdatMapping.lookup(selectorOp));
}
if (op.getUnnamedAddr().has_value())
var->setUnnamedAddr(convertUnnamedAddrToLLVM(*op.getUnnamedAddr()));
if (op.getSection().has_value())
var->setSection(*op.getSection());
addRuntimePreemptionSpecifier(op.getDsoLocal(), var);
std::optional<uint64_t> alignment = op.getAlignment();
if (alignment.has_value())
var->setAlignment(llvm::MaybeAlign(alignment.value()));
var->setVisibility(convertVisibilityToLLVM(op.getVisibility_()));
globalsMapping.try_emplace(op, var);
// Add debug information if present.
if (op.getDbgExpr()) {
llvm::DIGlobalVariableExpression *diGlobalExpr =
debugTranslation->translateGlobalVariableExpression(op.getDbgExpr());
llvm::DIGlobalVariable *diGlobalVar = diGlobalExpr->getVariable();
var->addDebugInfo(diGlobalExpr);
// There is no `globals` field in DICompileUnitAttr which can be directly
// assigned to DICompileUnit. We have to build the list by looking at the
// dbgExpr of all the GlobalOps. The scope of the variable is used to get
// the DICompileUnit in which to add it. But for the languages that
// support modules, the scope hierarchy can be
// variable -> module -> compile unit
// If a variable scope points to the module then we use the scope of the
// module to get the compile unit.
// Global variables are also used for things like static local variables
// in C and local variables with the save attribute in Fortran. The scope
// of the variable is the parent function. We use the compile unit of the
// parent function in this case.
llvm::DIScope *scope = diGlobalVar->getScope();
if (auto *mod = dyn_cast_if_present<llvm::DIModule>(scope))
scope = mod->getScope();
else if (auto *sp = dyn_cast_if_present<llvm::DISubprogram>(scope))
scope = sp->getUnit();
// Get the compile unit (scope) of the the global variable.
if (llvm::DICompileUnit *compileUnit =
dyn_cast_if_present<llvm::DICompileUnit>(scope)) {
// Update the compile unit with this incoming global variable expression
// during the finalizing step later.
allGVars[compileUnit].push_back(diGlobalExpr);
}
}
}
// Convert global variable bodies. This is done after all global variables
// have been created in LLVM IR because a global body may refer to another
// global or itself. So all global variables need to be mapped first.
for (auto op : getModuleBody(mlirModule).getOps<LLVM::GlobalOp>()) {
if (Block *initializer = op.getInitializerBlock()) {
llvm::IRBuilder<> builder(llvmModule->getContext());
[[maybe_unused]] int numConstantsHit = 0;
[[maybe_unused]] int numConstantsErased = 0;
DenseMap<llvm::ConstantAggregate *, int> constantAggregateUseMap;
for (auto &op : initializer->without_terminator()) {
if (failed(convertOperation(op, builder)))
return emitError(op.getLoc(), "fail to convert global initializer");
auto *cst = dyn_cast<llvm::Constant>(lookupValue(op.getResult(0)));
if (!cst)
return emitError(op.getLoc(), "unemittable constant value");
// When emitting an LLVM constant, a new constant is created and the old
// constant may become dangling and take space. We should remove the
// dangling constants to avoid memory explosion especially for constant
// arrays whose number of elements is large.
// Because multiple operations may refer to the same constant, we need
// to count the number of uses of each constant array and remove it only
// when the count becomes zero.
if (auto *agg = dyn_cast<llvm::ConstantAggregate>(cst)) {
numConstantsHit++;
Value result = op.getResult(0);
int numUsers = std::distance(result.use_begin(), result.use_end());
auto [iterator, inserted] =
constantAggregateUseMap.try_emplace(agg, numUsers);
if (!inserted) {
// Key already exists, update the value
iterator->second += numUsers;
}
}
// Scan the operands of the operation to decrement the use count of
// constants. Erase the constant if the use count becomes zero.
for (Value v : op.getOperands()) {
auto cst = dyn_cast<llvm::ConstantAggregate>(lookupValue(v));
if (!cst)
continue;
auto iter = constantAggregateUseMap.find(cst);
assert(iter != constantAggregateUseMap.end() && "constant not found");
iter->second--;
if (iter->second == 0) {
// NOTE: cannot call removeDeadConstantUsers() here because it
// may remove the constant which has uses not be converted yet.
if (cst->user_empty()) {
cst->destroyConstant();
numConstantsErased++;
}
constantAggregateUseMap.erase(iter);
}
}
}
ReturnOp ret = cast<ReturnOp>(initializer->getTerminator());
llvm::Constant *cst =
cast<llvm::Constant>(lookupValue(ret.getOperand(0)));
auto *global = cast<llvm::GlobalVariable>(lookupGlobal(op));
if (!shouldDropGlobalInitializer(global->getLinkage(), cst))
global->setInitializer(cst);
// Try to remove the dangling constants again after all operations are
// converted.
for (auto it : constantAggregateUseMap) {
auto cst = it.first;
cst->removeDeadConstantUsers();
if (cst->user_empty()) {
cst->destroyConstant();
numConstantsErased++;
}
}
LLVM_DEBUG(llvm::dbgs()
<< "Convert initializer for " << op.getName() << "\n";
llvm::dbgs() << numConstantsHit << " new constants hit\n";
llvm::dbgs()
<< numConstantsErased << " dangling constants erased\n";);
}
}
// Convert llvm.mlir.global_ctors and dtors.
for (Operation &op : getModuleBody(mlirModule)) {
auto ctorOp = dyn_cast<GlobalCtorsOp>(op);
auto dtorOp = dyn_cast<GlobalDtorsOp>(op);
if (!ctorOp && !dtorOp)
continue;
auto range = ctorOp ? llvm::zip(ctorOp.getCtors(), ctorOp.getPriorities())
: llvm::zip(dtorOp.getDtors(), dtorOp.getPriorities());
auto appendGlobalFn =
ctorOp ? llvm::appendToGlobalCtors : llvm::appendToGlobalDtors;
for (auto symbolAndPriority : range) {
llvm::Function *f = lookupFunction(
cast<FlatSymbolRefAttr>(std::get<0>(symbolAndPriority)).getValue());
appendGlobalFn(*llvmModule, f,
cast<IntegerAttr>(std::get<1>(symbolAndPriority)).getInt(),
/*Data=*/nullptr);
}
}
for (auto op : getModuleBody(mlirModule).getOps<LLVM::GlobalOp>())
if (failed(convertDialectAttributes(op, {})))
return failure();
// Finally, update the compile units their respective sets of global variables
// created earlier.
for (const auto &[compileUnit, globals] : allGVars) {
compileUnit->replaceGlobalVariables(
llvm::MDTuple::get(getLLVMContext(), globals));
}
return success();
}
/// Attempts to add an attribute identified by `key`, optionally with the given
/// `value` to LLVM function `llvmFunc`. Reports errors at `loc` if any. If the
/// attribute has a kind known to LLVM IR, create the attribute of this kind,
/// otherwise keep it as a string attribute. Performs additional checks for
/// attributes known to have or not have a value in order to avoid assertions
/// inside LLVM upon construction.
static LogicalResult checkedAddLLVMFnAttribute(Location loc,
llvm::Function *llvmFunc,
StringRef key,
StringRef value = StringRef()) {
auto kind = llvm::Attribute::getAttrKindFromName(key);
if (kind == llvm::Attribute::None) {
llvmFunc->addFnAttr(key, value);
return success();
}
if (llvm::Attribute::isIntAttrKind(kind)) {
if (value.empty())
return emitError(loc) << "LLVM attribute '" << key << "' expects a value";
int64_t result;
if (!value.getAsInteger(/*Radix=*/0, result))
llvmFunc->addFnAttr(
llvm::Attribute::get(llvmFunc->getContext(), kind, result));
else
llvmFunc->addFnAttr(key, value);
return success();
}
if (!value.empty())
return emitError(loc) << "LLVM attribute '" << key
<< "' does not expect a value, found '" << value
<< "'";
llvmFunc->addFnAttr(kind);
return success();
}
/// Attaches the attributes listed in the given array attribute to `llvmFunc`.
/// Reports error to `loc` if any and returns immediately. Expects `attributes`
/// to be an array attribute containing either string attributes, treated as
/// value-less LLVM attributes, or array attributes containing two string
/// attributes, with the first string being the name of the corresponding LLVM
/// attribute and the second string beings its value. Note that even integer
/// attributes are expected to have their values expressed as strings.
static LogicalResult
forwardPassthroughAttributes(Location loc, std::optional<ArrayAttr> attributes,
llvm::Function *llvmFunc) {
if (!attributes)
return success();
for (Attribute attr : *attributes) {
if (auto stringAttr = dyn_cast<StringAttr>(attr)) {
if (failed(
checkedAddLLVMFnAttribute(loc, llvmFunc, stringAttr.getValue())))
return failure();
continue;
}
auto arrayAttr = dyn_cast<ArrayAttr>(attr);
if (!arrayAttr || arrayAttr.size() != 2)
return emitError(loc)
<< "expected 'passthrough' to contain string or array attributes";
auto keyAttr = dyn_cast<StringAttr>(arrayAttr[0]);
auto valueAttr = dyn_cast<StringAttr>(arrayAttr[1]);
if (!keyAttr || !valueAttr)
return emitError(loc)
<< "expected arrays within 'passthrough' to contain two strings";
if (failed(checkedAddLLVMFnAttribute(loc, llvmFunc, keyAttr.getValue(),
valueAttr.getValue())))
return failure();
}
return success();
}
LogicalResult ModuleTranslation::convertOneFunction(LLVMFuncOp func) {
// Clear the block, branch value mappings, they are only relevant within one
// function.
blockMapping.clear();
valueMapping.clear();
branchMapping.clear();
llvm::Function *llvmFunc = lookupFunction(func.getName());
// Add function arguments to the value remapping table.
for (auto [mlirArg, llvmArg] :
llvm::zip(func.getArguments(), llvmFunc->args()))
mapValue(mlirArg, &llvmArg);
// Check the personality and set it.
if (func.getPersonality()) {
llvm::Type *ty = llvm::PointerType::getUnqual(llvmFunc->getContext());
if (llvm::Constant *pfunc = getLLVMConstant(ty, func.getPersonalityAttr(),
func.getLoc(), *this))
llvmFunc->setPersonalityFn(pfunc);
}
if (std::optional<StringRef> section = func.getSection())
llvmFunc->setSection(*section);
if (func.getArmStreaming())
llvmFunc->addFnAttr("aarch64_pstate_sm_enabled");
else if (func.getArmLocallyStreaming())
llvmFunc->addFnAttr("aarch64_pstate_sm_body");
else if (func.getArmStreamingCompatible())
llvmFunc->addFnAttr("aarch64_pstate_sm_compatible");
if (func.getArmNewZa())
llvmFunc->addFnAttr("aarch64_new_za");
else if (func.getArmInZa())
llvmFunc->addFnAttr("aarch64_in_za");
else if (func.getArmOutZa())
llvmFunc->addFnAttr("aarch64_out_za");
else if (func.getArmInoutZa())
llvmFunc->addFnAttr("aarch64_inout_za");
else if (func.getArmPreservesZa())
llvmFunc->addFnAttr("aarch64_preserves_za");
if (auto targetCpu = func.getTargetCpu())
llvmFunc->addFnAttr("target-cpu", *targetCpu);
if (auto tuneCpu = func.getTuneCpu())
llvmFunc->addFnAttr("tune-cpu", *tuneCpu);
if (auto targetFeatures = func.getTargetFeatures())
llvmFunc->addFnAttr("target-features", targetFeatures->getFeaturesString());
if (auto attr = func.getVscaleRange())
llvmFunc->addFnAttr(llvm::Attribute::getWithVScaleRangeArgs(
getLLVMContext(), attr->getMinRange().getInt(),
attr->getMaxRange().getInt()));
if (auto unsafeFpMath = func.getUnsafeFpMath())
llvmFunc->addFnAttr("unsafe-fp-math", llvm::toStringRef(*unsafeFpMath));
if (auto noInfsFpMath = func.getNoInfsFpMath())
llvmFunc->addFnAttr("no-infs-fp-math", llvm::toStringRef(*noInfsFpMath));
if (auto noNansFpMath = func.getNoNansFpMath())
llvmFunc->addFnAttr("no-nans-fp-math", llvm::toStringRef(*noNansFpMath));
if (auto approxFuncFpMath = func.getApproxFuncFpMath())
llvmFunc->addFnAttr("approx-func-fp-math",
llvm::toStringRef(*approxFuncFpMath));
if (auto noSignedZerosFpMath = func.getNoSignedZerosFpMath())
llvmFunc->addFnAttr("no-signed-zeros-fp-math",
llvm::toStringRef(*noSignedZerosFpMath));
if (auto denormalFpMath = func.getDenormalFpMath())
llvmFunc->addFnAttr("denormal-fp-math", *denormalFpMath);
if (auto denormalFpMathF32 = func.getDenormalFpMathF32())
llvmFunc->addFnAttr("denormal-fp-math-f32", *denormalFpMathF32);
if (auto fpContract = func.getFpContract())
llvmFunc->addFnAttr("fp-contract", *fpContract);
// Add function attribute frame-pointer, if found.
if (FramePointerKindAttr attr = func.getFramePointerAttr())
llvmFunc->addFnAttr("frame-pointer",
LLVM::framePointerKind::stringifyFramePointerKind(
(attr.getFramePointerKind())));
// First, create all blocks so we can jump to them.
llvm::LLVMContext &llvmContext = llvmFunc->getContext();
for (auto &bb : func) {
auto *llvmBB = llvm::BasicBlock::Create(llvmContext);
llvmBB->insertInto(llvmFunc);
mapBlock(&bb, llvmBB);
}
// Then, convert blocks one by one in topological order to ensure defs are
// converted before uses.
auto blocks = getBlocksSortedByDominance(func.getBody());
for (Block *bb : blocks) {
CapturingIRBuilder builder(llvmContext);
if (failed(convertBlockImpl(*bb, bb->isEntryBlock(), builder,
/*recordInsertions=*/true)))
return failure();
}
// After all blocks have been traversed and values mapped, connect the PHI
// nodes to the results of preceding blocks.
detail::connectPHINodes(func.getBody(), *this);
// Finally, convert dialect attributes attached to the function.
return convertDialectAttributes(func, {});
}
LogicalResult ModuleTranslation::convertDialectAttributes(
Operation *op, ArrayRef<llvm::Instruction *> instructions) {
for (NamedAttribute attribute : op->getDialectAttrs())
if (failed(iface.amendOperation(op, instructions, attribute, *this)))
return failure();
return success();
}
/// Converts memory effect attributes from `func` and attaches them to
/// `llvmFunc`.
static void convertFunctionMemoryAttributes(LLVMFuncOp func,
llvm::Function *llvmFunc) {
if (!func.getMemory())
return;
MemoryEffectsAttr memEffects = func.getMemoryAttr();
// Add memory effects incrementally.
llvm::MemoryEffects newMemEffects =
llvm::MemoryEffects(llvm::MemoryEffects::Location::ArgMem,
convertModRefInfoToLLVM(memEffects.getArgMem()));
newMemEffects |= llvm::MemoryEffects(
llvm::MemoryEffects::Location::InaccessibleMem,
convertModRefInfoToLLVM(memEffects.getInaccessibleMem()));
newMemEffects |=
llvm::MemoryEffects(llvm::MemoryEffects::Location::Other,
convertModRefInfoToLLVM(memEffects.getOther()));
llvmFunc->setMemoryEffects(newMemEffects);
}
/// Converts function attributes from `func` and attaches them to `llvmFunc`.
static void convertFunctionAttributes(LLVMFuncOp func,
llvm::Function *llvmFunc) {
if (func.getNoInlineAttr())
llvmFunc->addFnAttr(llvm::Attribute::NoInline);
if (func.getAlwaysInlineAttr())
llvmFunc->addFnAttr(llvm::Attribute::AlwaysInline);
if (func.getOptimizeNoneAttr())
llvmFunc->addFnAttr(llvm::Attribute::OptimizeNone);
if (func.getConvergentAttr())
llvmFunc->addFnAttr(llvm::Attribute::Convergent);
if (func.getNoUnwindAttr())
llvmFunc->addFnAttr(llvm::Attribute::NoUnwind);
if (func.getWillReturnAttr())
llvmFunc->addFnAttr(llvm::Attribute::WillReturn);
convertFunctionMemoryAttributes(func, llvmFunc);
}
FailureOr<llvm::AttrBuilder>
ModuleTranslation::convertParameterAttrs(LLVMFuncOp func, int argIdx,
DictionaryAttr paramAttrs) {
llvm::AttrBuilder attrBuilder(llvmModule->getContext());
auto attrNameToKindMapping = getAttrNameToKindMapping();
for (auto namedAttr : paramAttrs) {
auto it = attrNameToKindMapping.find(namedAttr.getName());
if (it != attrNameToKindMapping.end()) {
llvm::Attribute::AttrKind llvmKind = it->second;
llvm::TypeSwitch<Attribute>(namedAttr.getValue())
.Case<TypeAttr>([&](auto typeAttr) {
attrBuilder.addTypeAttr(llvmKind, convertType(typeAttr.getValue()));
})
.Case<IntegerAttr>([&](auto intAttr) {
attrBuilder.addRawIntAttr(llvmKind, intAttr.getInt());
})
.Case<UnitAttr>([&](auto) { attrBuilder.addAttribute(llvmKind); });
} else if (namedAttr.getNameDialect()) {
if (failed(iface.convertParameterAttr(func, argIdx, namedAttr, *this)))
return failure();
}
}
return attrBuilder;
}
LogicalResult ModuleTranslation::convertFunctionSignatures() {
// Declare all functions first because there may be function calls that form a
// call graph with cycles, or global initializers that reference functions.
for (auto function : getModuleBody(mlirModule).getOps<LLVMFuncOp>()) {
llvm::FunctionCallee llvmFuncCst = llvmModule->getOrInsertFunction(
function.getName(),
cast<llvm::FunctionType>(convertType(function.getFunctionType())));
llvm::Function *llvmFunc = cast<llvm::Function>(llvmFuncCst.getCallee());
llvmFunc->setLinkage(convertLinkageToLLVM(function.getLinkage()));
llvmFunc->setCallingConv(convertCConvToLLVM(function.getCConv()));
mapFunction(function.getName(), llvmFunc);
addRuntimePreemptionSpecifier(function.getDsoLocal(), llvmFunc);
// Convert function attributes.
convertFunctionAttributes(function, llvmFunc);
// Convert function_entry_count attribute to metadata.
if (std::optional<uint64_t> entryCount = function.getFunctionEntryCount())
llvmFunc->setEntryCount(entryCount.value());
// Convert result attributes.
if (ArrayAttr allResultAttrs = function.getAllResultAttrs()) {
DictionaryAttr resultAttrs = cast<DictionaryAttr>(allResultAttrs[0]);
FailureOr<llvm::AttrBuilder> attrBuilder =
convertParameterAttrs(function, -1, resultAttrs);
if (failed(attrBuilder))
return failure();
llvmFunc->addRetAttrs(*attrBuilder);
}
// Convert argument attributes.
for (auto [argIdx, llvmArg] : llvm::enumerate(llvmFunc->args())) {
if (DictionaryAttr argAttrs = function.getArgAttrDict(argIdx)) {
FailureOr<llvm::AttrBuilder> attrBuilder =
convertParameterAttrs(function, argIdx, argAttrs);
if (failed(attrBuilder))
return failure();
llvmArg.addAttrs(*attrBuilder);
}
}
// Forward the pass-through attributes to LLVM.
if (failed(forwardPassthroughAttributes(
function.getLoc(), function.getPassthrough(), llvmFunc)))
return failure();
// Convert visibility attribute.
llvmFunc->setVisibility(convertVisibilityToLLVM(function.getVisibility_()));
// Convert the comdat attribute.
if (std::optional<mlir::SymbolRefAttr> comdat = function.getComdat()) {
auto selectorOp = cast<ComdatSelectorOp>(
SymbolTable::lookupNearestSymbolFrom(function, *comdat));
llvmFunc->setComdat(comdatMapping.lookup(selectorOp));
}
if (auto gc = function.getGarbageCollector())
llvmFunc->setGC(gc->str());
if (auto unnamedAddr = function.getUnnamedAddr())
llvmFunc->setUnnamedAddr(convertUnnamedAddrToLLVM(*unnamedAddr));
if (auto alignment = function.getAlignment())
llvmFunc->setAlignment(llvm::MaybeAlign(*alignment));
// Translate the debug information for this function.
debugTranslation->translate(function, *llvmFunc);
}
return success();
}
LogicalResult ModuleTranslation::convertFunctions() {
// Convert functions.
for (auto function : getModuleBody(mlirModule).getOps<LLVMFuncOp>()) {
// Do not convert external functions, but do process dialect attributes
// attached to them.
if (function.isExternal()) {
if (failed(convertDialectAttributes(function, {})))
return failure();
continue;
}
if (failed(convertOneFunction(function)))
return failure();
}
return success();
}
LogicalResult ModuleTranslation::convertComdats() {
for (auto comdatOp : getModuleBody(mlirModule).getOps<ComdatOp>()) {
for (auto selectorOp : comdatOp.getOps<ComdatSelectorOp>()) {
llvm::Module *module = getLLVMModule();
if (module->getComdatSymbolTable().contains(selectorOp.getSymName()))
return emitError(selectorOp.getLoc())
<< "comdat selection symbols must be unique even in different "
"comdat regions";
llvm::Comdat *comdat = module->getOrInsertComdat(selectorOp.getSymName());
comdat->setSelectionKind(convertComdatToLLVM(selectorOp.getComdat()));
comdatMapping.try_emplace(selectorOp, comdat);
}
}
return success();
}
void ModuleTranslation::setAccessGroupsMetadata(AccessGroupOpInterface op,
llvm::Instruction *inst) {
if (llvm::MDNode *node = loopAnnotationTranslation->getAccessGroups(op))
inst->setMetadata(llvm::LLVMContext::MD_access_group, node);
}
llvm::MDNode *
ModuleTranslation::getOrCreateAliasScope(AliasScopeAttr aliasScopeAttr) {
auto [scopeIt, scopeInserted] =
aliasScopeMetadataMapping.try_emplace(aliasScopeAttr, nullptr);
if (!scopeInserted)
return scopeIt->second;
llvm::LLVMContext &ctx = llvmModule->getContext();
auto dummy = llvm::MDNode::getTemporary(ctx, std::nullopt);
// Convert the domain metadata node if necessary.
auto [domainIt, insertedDomain] = aliasDomainMetadataMapping.try_emplace(
aliasScopeAttr.getDomain(), nullptr);
if (insertedDomain) {
llvm::SmallVector<llvm::Metadata *, 2> operands;
// Placeholder for self-reference.
operands.push_back(dummy.get());
if (StringAttr description = aliasScopeAttr.getDomain().getDescription())
operands.push_back(llvm::MDString::get(ctx, description));
domainIt->second = llvm::MDNode::get(ctx, operands);
// Self-reference for uniqueness.
domainIt->second->replaceOperandWith(0, domainIt->second);
}
// Convert the scope metadata node.
assert(domainIt->second && "Scope's domain should already be valid");
llvm::SmallVector<llvm::Metadata *, 3> operands;
// Placeholder for self-reference.
operands.push_back(dummy.get());
operands.push_back(domainIt->second);
if (StringAttr description = aliasScopeAttr.getDescription())
operands.push_back(llvm::MDString::get(ctx, description));
scopeIt->second = llvm::MDNode::get(ctx, operands);
// Self-reference for uniqueness.
scopeIt->second->replaceOperandWith(0, scopeIt->second);
return scopeIt->second;
}
llvm::MDNode *ModuleTranslation::getOrCreateAliasScopes(
ArrayRef<AliasScopeAttr> aliasScopeAttrs) {
SmallVector<llvm::Metadata *> nodes;
nodes.reserve(aliasScopeAttrs.size());
for (AliasScopeAttr aliasScopeAttr : aliasScopeAttrs)
nodes.push_back(getOrCreateAliasScope(aliasScopeAttr));
return llvm::MDNode::get(getLLVMContext(), nodes);
}
void ModuleTranslation::setAliasScopeMetadata(AliasAnalysisOpInterface op,
llvm::Instruction *inst) {
auto populateScopeMetadata = [&](ArrayAttr aliasScopeAttrs, unsigned kind) {
if (!aliasScopeAttrs || aliasScopeAttrs.empty())
return;
llvm::MDNode *node = getOrCreateAliasScopes(
llvm::to_vector(aliasScopeAttrs.getAsRange<AliasScopeAttr>()));
inst->setMetadata(kind, node);
};
populateScopeMetadata(op.getAliasScopesOrNull(),
llvm::LLVMContext::MD_alias_scope);
populateScopeMetadata(op.getNoAliasScopesOrNull(),
llvm::LLVMContext::MD_noalias);
}
llvm::MDNode *ModuleTranslation::getTBAANode(TBAATagAttr tbaaAttr) const {
return tbaaMetadataMapping.lookup(tbaaAttr);
}
void ModuleTranslation::setTBAAMetadata(AliasAnalysisOpInterface op,
llvm::Instruction *inst) {
ArrayAttr tagRefs = op.getTBAATagsOrNull();
if (!tagRefs || tagRefs.empty())
return;
// LLVM IR currently does not support attaching more than one TBAA access tag
// to a memory accessing instruction. It may be useful to support this in
// future, but for the time being just ignore the metadata if MLIR operation
// has multiple access tags.
if (tagRefs.size() > 1) {
op.emitWarning() << "TBAA access tags were not translated, because LLVM "
"IR only supports a single tag per instruction";
return;
}
llvm::MDNode *node = getTBAANode(cast<TBAATagAttr>(tagRefs[0]));
inst->setMetadata(llvm::LLVMContext::MD_tbaa, node);
}
void ModuleTranslation::setBranchWeightsMetadata(BranchWeightOpInterface op) {
DenseI32ArrayAttr weightsAttr = op.getBranchWeightsOrNull();
if (!weightsAttr)
return;
llvm::Instruction *inst = isa<CallOp>(op) ? lookupCall(op) : lookupBranch(op);
assert(inst && "expected the operation to have a mapping to an instruction");
SmallVector<uint32_t> weights(weightsAttr.asArrayRef());
inst->setMetadata(
llvm::LLVMContext::MD_prof,
llvm::MDBuilder(getLLVMContext()).createBranchWeights(weights));
}
LogicalResult ModuleTranslation::createTBAAMetadata() {
llvm::LLVMContext &ctx = llvmModule->getContext();
llvm::IntegerType *offsetTy = llvm::IntegerType::get(ctx, 64);
// Walk the entire module and create all metadata nodes for the TBAA
// attributes. The code below relies on two invariants of the
// `AttrTypeWalker`:
// 1. Attributes are visited in post-order: Since the attributes create a DAG,
// this ensures that any lookups into `tbaaMetadataMapping` for child
// attributes succeed.
// 2. Attributes are only ever visited once: This way we don't leak any
// LLVM metadata instances.
AttrTypeWalker walker;
walker.addWalk([&](TBAARootAttr root) {
tbaaMetadataMapping.insert(
{root, llvm::MDNode::get(ctx, llvm::MDString::get(ctx, root.getId()))});
});
walker.addWalk([&](TBAATypeDescriptorAttr descriptor) {
SmallVector<llvm::Metadata *> operands;
operands.push_back(llvm::MDString::get(ctx, descriptor.getId()));
for (TBAAMemberAttr member : descriptor.getMembers()) {
operands.push_back(tbaaMetadataMapping.lookup(member.getTypeDesc()));
operands.push_back(llvm::ConstantAsMetadata::get(
llvm::ConstantInt::get(offsetTy, member.getOffset())));
}
tbaaMetadataMapping.insert({descriptor, llvm::MDNode::get(ctx, operands)});
});
walker.addWalk([&](TBAATagAttr tag) {
SmallVector<llvm::Metadata *> operands;
operands.push_back(tbaaMetadataMapping.lookup(tag.getBaseType()));
operands.push_back(tbaaMetadataMapping.lookup(tag.getAccessType()));
operands.push_back(llvm::ConstantAsMetadata::get(
llvm::ConstantInt::get(offsetTy, tag.getOffset())));
if (tag.getConstant())
operands.push_back(
llvm::ConstantAsMetadata::get(llvm::ConstantInt::get(offsetTy, 1)));
tbaaMetadataMapping.insert({tag, llvm::MDNode::get(ctx, operands)});
});
mlirModule->walk([&](AliasAnalysisOpInterface analysisOpInterface) {
if (auto attr = analysisOpInterface.getTBAATagsOrNull())
walker.walk(attr);
});
return success();
}
void ModuleTranslation::setLoopMetadata(Operation *op,
llvm::Instruction *inst) {
LoopAnnotationAttr attr =
TypeSwitch<Operation *, LoopAnnotationAttr>(op)
.Case<LLVM::BrOp, LLVM::CondBrOp>(
[](auto branchOp) { return branchOp.getLoopAnnotationAttr(); });
if (!attr)
return;
llvm::MDNode *loopMD =
loopAnnotationTranslation->translateLoopAnnotation(attr, op);
inst->setMetadata(llvm::LLVMContext::MD_loop, loopMD);
}
llvm::Type *ModuleTranslation::convertType(Type type) {
return typeTranslator.translateType(type);
}
/// A helper to look up remapped operands in the value remapping table.
SmallVector<llvm::Value *> ModuleTranslation::lookupValues(ValueRange values) {
SmallVector<llvm::Value *> remapped;
remapped.reserve(values.size());
for (Value v : values)
remapped.push_back(lookupValue(v));
return remapped;
}
llvm::OpenMPIRBuilder *ModuleTranslation::getOpenMPBuilder() {
if (!ompBuilder) {
ompBuilder = std::make_unique<llvm::OpenMPIRBuilder>(*llvmModule);
ompBuilder->initialize();
// Flags represented as top-level OpenMP dialect attributes are set in
// `OpenMPDialectLLVMIRTranslationInterface::amendOperation()`. Here we set
// the default configuration.
ompBuilder->setConfig(llvm::OpenMPIRBuilderConfig(
/* IsTargetDevice = */ false, /* IsGPU = */ false,
/* OpenMPOffloadMandatory = */ false,
/* HasRequiresReverseOffload = */ false,
/* HasRequiresUnifiedAddress = */ false,
/* HasRequiresUnifiedSharedMemory = */ false,
/* HasRequiresDynamicAllocators = */ false));
}
return ompBuilder.get();
}
llvm::DILocation *ModuleTranslation::translateLoc(Location loc,
llvm::DILocalScope *scope) {
return debugTranslation->translateLoc(loc, scope);
}
llvm::DIExpression *
ModuleTranslation::translateExpression(LLVM::DIExpressionAttr attr) {
return debugTranslation->translateExpression(attr);
}
llvm::DIGlobalVariableExpression *
ModuleTranslation::translateGlobalVariableExpression(
LLVM::DIGlobalVariableExpressionAttr attr) {
return debugTranslation->translateGlobalVariableExpression(attr);
}
llvm::Metadata *ModuleTranslation::translateDebugInfo(LLVM::DINodeAttr attr) {
return debugTranslation->translate(attr);
}
llvm::RoundingMode
ModuleTranslation::translateRoundingMode(LLVM::RoundingMode rounding) {
return convertRoundingModeToLLVM(rounding);
}
llvm::fp::ExceptionBehavior ModuleTranslation::translateFPExceptionBehavior(
LLVM::FPExceptionBehavior exceptionBehavior) {
return convertFPExceptionBehaviorToLLVM(exceptionBehavior);
}
llvm::NamedMDNode *
ModuleTranslation::getOrInsertNamedModuleMetadata(StringRef name) {
return llvmModule->getOrInsertNamedMetadata(name);
}
void ModuleTranslation::StackFrame::anchor() {}
static std::unique_ptr<llvm::Module>
prepareLLVMModule(Operation *m, llvm::LLVMContext &llvmContext,
StringRef name) {
m->getContext()->getOrLoadDialect<LLVM::LLVMDialect>();
auto llvmModule = std::make_unique<llvm::Module>(name, llvmContext);
// ModuleTranslation can currently only construct modules in the old debug
// info format, so set the flag accordingly.
llvmModule->setNewDbgInfoFormatFlag(false);
if (auto dataLayoutAttr =
m->getDiscardableAttr(LLVM::LLVMDialect::getDataLayoutAttrName())) {
llvmModule->setDataLayout(cast<StringAttr>(dataLayoutAttr).getValue());
} else {
FailureOr<llvm::DataLayout> llvmDataLayout(llvm::DataLayout(""));
if (auto iface = dyn_cast<DataLayoutOpInterface>(m)) {
if (DataLayoutSpecInterface spec = iface.getDataLayoutSpec()) {
llvmDataLayout =
translateDataLayout(spec, DataLayout(iface), m->getLoc());
}
} else if (auto mod = dyn_cast<ModuleOp>(m)) {
if (DataLayoutSpecInterface spec = mod.getDataLayoutSpec()) {
llvmDataLayout =
translateDataLayout(spec, DataLayout(mod), m->getLoc());
}
}
if (failed(llvmDataLayout))
return nullptr;
llvmModule->setDataLayout(*llvmDataLayout);
}
if (auto targetTripleAttr =
m->getDiscardableAttr(LLVM::LLVMDialect::getTargetTripleAttrName()))
llvmModule->setTargetTriple(cast<StringAttr>(targetTripleAttr).getValue());
return llvmModule;
}
std::unique_ptr<llvm::Module>
mlir::translateModuleToLLVMIR(Operation *module, llvm::LLVMContext &llvmContext,
StringRef name, bool disableVerification) {
if (!satisfiesLLVMModule(module)) {
module->emitOpError("can not be translated to an LLVMIR module");
return nullptr;
}
std::unique_ptr<llvm::Module> llvmModule =
prepareLLVMModule(module, llvmContext, name);
if (!llvmModule)
return nullptr;
LLVM::ensureDistinctSuccessors(module);
LLVM::legalizeDIExpressionsRecursively(module);
ModuleTranslation translator(module, std::move(llvmModule));
llvm::IRBuilder<> llvmBuilder(llvmContext);
// Convert module before functions and operations inside, so dialect
// attributes can be used to change dialect-specific global configurations via
// `amendOperation()`. These configurations can then influence the translation
// of operations afterwards.
if (failed(translator.convertOperation(*module, llvmBuilder)))
return nullptr;
if (failed(translator.convertComdats()))
return nullptr;
if (failed(translator.convertFunctionSignatures()))
return nullptr;
if (failed(translator.convertGlobals()))
return nullptr;
if (failed(translator.createTBAAMetadata()))
return nullptr;
// Convert other top-level operations if possible.
for (Operation &o : getModuleBody(module).getOperations()) {
if (!isa<LLVM::LLVMFuncOp, LLVM::GlobalOp, LLVM::GlobalCtorsOp,
LLVM::GlobalDtorsOp, LLVM::ComdatOp>(&o) &&
!o.hasTrait<OpTrait::IsTerminator>() &&
failed(translator.convertOperation(o, llvmBuilder))) {
return nullptr;
}
}
// Operations in function bodies with symbolic references must be converted
// after the top-level operations they refer to are declared, so we do it
// last.
if (failed(translator.convertFunctions()))
return nullptr;
// Once we've finished constructing elements in the module, we should convert
// it to use the debug info format desired by LLVM.
// See https://llvm.org/docs/RemoveDIsDebugInfo.html
translator.llvmModule->setIsNewDbgInfoFormat(UseNewDbgInfoFormat);
if (!disableVerification &&
llvm::verifyModule(*translator.llvmModule, &llvm::errs()))
return nullptr;
return std::move(translator.llvmModule);
}
|