Write the global section to the bytecode file.
| 1498 | |
| 1499 | /// Write the global section to the bytecode file. |
| 1500 | static LogicalResult |
| 1501 | writeGlobalSection(raw_ostream &stream, cuda_tile::ModuleOp module, |
| 1502 | StringManager &strMgr, TypeManager &typeMgr, |
| 1503 | ConstantManager &constMgr, DebugInfoWriter &debuginfo, |
| 1504 | const BytecodeWriterConfig &config) { |
| 1505 | SmallVector<char> buffer; |
| 1506 | llvm::raw_svector_ostream sectionStream(buffer); |
| 1507 | EncodingWriter sectionWriter(sectionStream); |
| 1508 | |
| 1509 | SmallVector<cuda_tile::GlobalOp> globals; |
| 1510 | for (auto globalOp : module.getOps<cuda_tile::GlobalOp>()) |
| 1511 | globals.push_back(globalOp); |
| 1512 | |
| 1513 | if (globals.empty()) |
| 1514 | return success(); |
| 1515 | |
| 1516 | sectionWriter.writeVarInt(globals.size()); |
| 1517 | |
| 1518 | static const auto kMinGlobalExtendedFields = |
| 1519 | *BytecodeVersion::fromVersion(13, 3, 0); |
| 1520 | |
| 1521 | for (cuda_tile::GlobalOp globalOp : globals) { |
| 1522 | // Global symbol visibility and the `constant` flag exist only in bytecode |
| 1523 | // 13.3+. Refuse to emit older versions if we would silently lose semantics. |
| 1524 | if (config.bytecodeVersion < kMinGlobalExtendedFields) { |
| 1525 | if (globalOp.getSymbolVisibility() != SymbolVisibility::Public) { |
| 1526 | return globalOp.emitError() << "global `" << globalOp.getSymName() |
| 1527 | << "` uses non-public symbol visibility, " |
| 1528 | "which cannot be encoded " |
| 1529 | "in bytecode version " |
| 1530 | << config.bytecodeVersion.toString() |
| 1531 | << " (requires bytecode 13.3+)"; |
| 1532 | } |
| 1533 | if (globalOp.getConstant()) { |
| 1534 | return globalOp.emitError() |
| 1535 | << "constant global `" << globalOp.getSymName() |
| 1536 | << "` cannot be encoded in bytecode version " |
| 1537 | << config.bytecodeVersion.toString() |
| 1538 | << " (the `constant` attribute requires bytecode 13.3+)"; |
| 1539 | } |
| 1540 | } |
| 1541 | |
| 1542 | // 1. Write symbol name index. |
| 1543 | sectionWriter.writeVarInt(strMgr.getStringIndex(globalOp.getSymName())); |
| 1544 | |
| 1545 | // 2. Write type index of the global's value. |
| 1546 | DenseTypedElementsAttr valueAttr = globalOp.getValue(); |
| 1547 | sectionWriter.writeVarInt(typeMgr.getTypeIndex(valueAttr.getType())); |
| 1548 | |
| 1549 | // 3. Write constant index for the global's value. |
| 1550 | uint64_t constIndex; |
| 1551 | if (failed(constMgr.addConstant(valueAttr, constIndex))) |
| 1552 | return globalOp.emitError("failed to add global constant: '") |
| 1553 | << globalOp.getSymName(); |
| 1554 | sectionWriter.writeVarInt(constIndex); |
| 1555 | |
| 1556 | // 4. Write alignment. |
| 1557 | sectionWriter.writeVarInt(globalOp.getAlignment()); |
no test coverage detected