|
16 | 16 | #include "aie/Targets/AIERT.h" |
17 | 17 |
|
18 | 18 | #include "llvm/Support/Debug.h" |
| 19 | +#include "llvm/Support/Format.h" |
| 20 | +#include <llvm/ADT/APInt.h> |
19 | 21 |
|
20 | 22 | extern "C" { |
21 | 23 | #include "xaiengine/xaiegbl_defs.h" |
@@ -47,8 +49,132 @@ struct TransactionBinaryOperation { |
47 | 49 | cmd.Size = size; |
48 | 50 | } |
49 | 51 | }; |
| 52 | + |
| 53 | +// A ControlPacketOperation encapsulates a parsed control packet including |
| 54 | +// stream header, control packet header, and optional data payload. |
| 55 | +struct ControlPacketOperation { |
| 56 | + uint32_t streamHeader; // word 0: parity + pkt_type + pkt_id |
| 57 | + uint32_t packetHeader; // word 1: parity + stream_id + opcode + beats + addr |
| 58 | + std::vector<uint32_t> data; // payload words (if opcode is write/blockwrite) |
| 59 | + |
| 60 | + // Decoded fields |
| 61 | + uint8_t pktType; |
| 62 | + uint8_t pktId; |
| 63 | + uint8_t streamId; |
| 64 | + uint8_t opcode; |
| 65 | + uint8_t beats; |
| 66 | + uint32_t address; |
| 67 | +}; |
| 68 | + |
50 | 69 | } // namespace |
51 | 70 |
|
| 71 | +// Check even parity bit (bit 31) |
| 72 | +// The parity bit (bit 31) is set to 1 if the popcount of bits[30:0] is even |
| 73 | +static bool checkParity(uint32_t word) { |
| 74 | + uint32_t p = 0; |
| 75 | + uint32_t w = word & 0x7FFFFFFF; // mask off parity bit |
| 76 | + while (w) { |
| 77 | + p += w & 1; |
| 78 | + w >>= 1; |
| 79 | + } |
| 80 | + // Parity bit should be 1 if popcount is even, 0 if odd |
| 81 | + bool expectedParity = (p % 2) == 0; |
| 82 | + bool actualParity = (word >> 31) & 1; |
| 83 | + return expectedParity == actualParity; |
| 84 | +} |
| 85 | + |
| 86 | +// Parse a control packet binary blob. On success return a vector of parsed |
| 87 | +// control packet operations. On failure return std::nullopt. |
| 88 | +static std::optional<std::vector<ControlPacketOperation>> |
| 89 | +parseControlPacket(const std::vector<uint8_t> &data) { |
| 90 | + std::vector<ControlPacketOperation> ops; |
| 91 | + |
| 92 | + size_t i = 0; |
| 93 | + |
| 94 | + auto requireBytes = [&](size_t offset, size_t length) -> bool { |
| 95 | + if (offset + length > data.size()) { |
| 96 | + llvm::errs() << "Control packet binary truncated\n"; |
| 97 | + return false; |
| 98 | + } |
| 99 | + return true; |
| 100 | + }; |
| 101 | + |
| 102 | + auto read32 = [&](size_t offset) -> uint32_t { |
| 103 | + uint32_t value; |
| 104 | + std::memcpy(&value, data.data() + offset, sizeof(uint32_t)); |
| 105 | + return value; |
| 106 | + }; |
| 107 | + |
| 108 | + while (i + 8 <= data.size()) { // Need at least 2 words (stream hdr + pkt hdr) |
| 109 | + ControlPacketOperation op; |
| 110 | + |
| 111 | + // Read stream header (word 0) |
| 112 | + op.streamHeader = read32(i); |
| 113 | + |
| 114 | + // Read control packet header (word 1) |
| 115 | + op.packetHeader = read32(i + 4); |
| 116 | + |
| 117 | + // Verify parity |
| 118 | + if (!checkParity(op.streamHeader)) { |
| 119 | + llvm::errs() << "Stream header parity check failed at offset " << i << "\n"; |
| 120 | + return std::nullopt; |
| 121 | + } |
| 122 | + if (!checkParity(op.packetHeader)) { |
| 123 | + llvm::errs() << "Packet header parity check failed at offset " << i + 4 << "\n"; |
| 124 | + return std::nullopt; |
| 125 | + } |
| 126 | + |
| 127 | + // Decode stream header fields |
| 128 | + op.pktType = (op.streamHeader >> 12) & 0x7; |
| 129 | + op.pktId = op.streamHeader & 0xFF; |
| 130 | + |
| 131 | + // Decode control packet header fields |
| 132 | + op.streamId = (op.packetHeader >> 24) & 0x7F; |
| 133 | + op.opcode = (op.packetHeader >> 22) & 0x3; |
| 134 | + op.beats = (op.packetHeader >> 20) & 0x3; |
| 135 | + op.address = op.packetHeader & 0xFFFFF; |
| 136 | + |
| 137 | + i += 8; // consumed 2 words |
| 138 | + |
| 139 | + LLVM_DEBUG(llvm::dbgs() << "Control packet at offset " << (i - 8) |
| 140 | + << ": opcode=" << static_cast<int>(op.opcode) |
| 141 | + << " stream_id=" << static_cast<int>(op.streamId) |
| 142 | + << " addr=0x" << llvm::format("%05X", op.address) |
| 143 | + << " beats=" << static_cast<int>(op.beats) << "\n"); |
| 144 | + |
| 145 | + // Read data payload if present (opcode 0x0=write or 0x2=blockwrite) |
| 146 | + if (op.opcode == 0x0 || op.opcode == 0x2) { |
| 147 | + uint32_t numDataWords = op.beats + 1; |
| 148 | + if (!requireBytes(i, numDataWords * 4)) { |
| 149 | + llvm::errs() << "Truncated data payload\n"; |
| 150 | + return std::nullopt; |
| 151 | + } |
| 152 | + |
| 153 | + op.data.resize(numDataWords); |
| 154 | + for (uint32_t j = 0; j < numDataWords; j++) { |
| 155 | + op.data[j] = read32(i + j * 4); |
| 156 | + } |
| 157 | + i += numDataWords * 4; |
| 158 | + |
| 159 | + LLVM_DEBUG(llvm::dbgs() << " Data: ["; |
| 160 | + for (size_t j = 0; j < op.data.size(); j++) { |
| 161 | + if (j > 0) llvm::dbgs() << ", "; |
| 162 | + llvm::dbgs() << op.data[j]; |
| 163 | + } |
| 164 | + llvm::dbgs() << "]\n"); |
| 165 | + } |
| 166 | + |
| 167 | + ops.push_back(std::move(op)); |
| 168 | + } |
| 169 | + |
| 170 | + if (i != data.size()) { |
| 171 | + llvm::errs() << "Warning: " << (data.size() - i) |
| 172 | + << " bytes remaining after parsing control packets\n"; |
| 173 | + } |
| 174 | + |
| 175 | + return ops; |
| 176 | +} |
| 177 | + |
52 | 178 | // Parse a TXN binary blob. On success return the number of columns from the |
53 | 179 | // header and a vector of parsed operations. On failure return std::nullopt. |
54 | 180 | static std::optional<int> |
@@ -451,6 +577,93 @@ xilinx::AIE::convertTransactionBinaryToMLIR(mlir::MLIRContext *ctx, |
451 | 577 | return module; |
452 | 578 | } |
453 | 579 |
|
| 580 | +// Convert (disassemble) a control packet binary to MLIR. On success return a |
| 581 | +// new ModuleOp containing a DeviceOp containing a runtime sequence with the |
| 582 | +// control packet binary encoded as a sequence of aiex.control_packet |
| 583 | +// operations. On failure return std::nullopt. |
| 584 | +std::optional<mlir::ModuleOp> |
| 585 | +xilinx::AIE::convertControlPacketBinaryToMLIR(mlir::MLIRContext *ctx, |
| 586 | + std::vector<uint8_t> &binary) { |
| 587 | + |
| 588 | + // parse the binary |
| 589 | + auto maybeOps = parseControlPacket(binary); |
| 590 | + if (!maybeOps) { |
| 591 | + llvm::errs() << "Failed to parse control packet binary\n"; |
| 592 | + return std::nullopt; |
| 593 | + } |
| 594 | + std::vector<ControlPacketOperation> operations = *maybeOps; |
| 595 | + |
| 596 | + auto loc = mlir::UnknownLoc::get(ctx); |
| 597 | + |
| 598 | + // create a new ModuleOp and set the insertion point |
| 599 | + auto module = ModuleOp::create(loc); |
| 600 | + OpBuilder builder(module.getBodyRegion()); |
| 601 | + builder.setInsertionPointToStart(module.getBody()); |
| 602 | + |
| 603 | + // Create aie.device - default to npu1_1col |
| 604 | + // Note: Control packets don't have device info in the binary format |
| 605 | + auto device = builder.create<DeviceOp>(loc, AIEDevice::npu1_1col, |
| 606 | + StringAttr::get(builder.getContext())); |
| 607 | + device.getRegion().emplaceBlock(); |
| 608 | + DeviceOp::ensureTerminator(device.getBodyRegion(), builder, loc); |
| 609 | + builder.setInsertionPointToStart(device.getBody()); |
| 610 | + |
| 611 | + // Create tiles and set controller_id attributes based on packet info |
| 612 | + // Group operations by (pktType, pktId) to determine which tile they target |
| 613 | + std::map<std::pair<uint8_t, uint8_t>, std::pair<uint32_t, uint32_t>> tileMap; |
| 614 | + for (const auto &op : operations) { |
| 615 | + auto key = std::make_pair(op.pktType, op.pktId); |
| 616 | + if (tileMap.find(key) == tileMap.end()) { |
| 617 | + // Extract col/row from address using target model |
| 618 | + const AIETargetModel &targetModel = device.getTargetModel(); |
| 619 | + uint32_t colInt = (op.address >> targetModel.getColumnShift()) & 0x1f; |
| 620 | + uint32_t rowInt = (op.address >> targetModel.getRowShift()) & 0x1f; |
| 621 | + tileMap[key] = std::make_pair(colInt, rowInt); |
| 622 | + |
| 623 | + // Create tile and set controller_id attribute |
| 624 | + auto tile = TileOp::getOrCreate(builder, device, colInt, rowInt); |
| 625 | + auto packetInfoAttr = AIE::PacketInfoAttr::get( |
| 626 | + builder.getContext(), op.pktType, op.pktId); |
| 627 | + tile->setAttr("controller_id", packetInfoAttr); |
| 628 | + } |
| 629 | + } |
| 630 | + |
| 631 | + // Create aiex.runtime_sequence |
| 632 | + std::string seq_name = "configure"; |
| 633 | + StringAttr seq_sym_name = builder.getStringAttr(seq_name); |
| 634 | + auto seq = builder.create<AIEX::RuntimeSequenceOp>(loc, seq_sym_name); |
| 635 | + seq.getBody().push_back(new Block); |
| 636 | + |
| 637 | + // Create control packet ops |
| 638 | + builder.setInsertionPointToStart(&seq.getBody().front()); |
| 639 | + for (const auto &op : operations) { |
| 640 | + IntegerAttr lengthAttr; |
| 641 | + DenseI32ArrayAttr dataAttr; |
| 642 | + |
| 643 | + if (op.opcode == 0x0 || op.opcode == 0x2) { |
| 644 | + // Write or blockwrite - has data payload |
| 645 | + SmallVector<int32_t> dataVec; |
| 646 | + for (uint32_t val : op.data) { |
| 647 | + dataVec.push_back(static_cast<int32_t>(val)); |
| 648 | + } |
| 649 | + dataAttr = DenseI32ArrayAttr::get(ctx, ArrayRef<int32_t>(dataVec)); |
| 650 | + } else if (op.opcode == 0x1) { |
| 651 | + // Read - has length but no data |
| 652 | + lengthAttr = builder.getI32IntegerAttr(op.beats + 1); |
| 653 | + } |
| 654 | + |
| 655 | + builder.create<AIEX::NpuControlPacketOp>( |
| 656 | + loc, |
| 657 | + builder.getUI32IntegerAttr(op.address), |
| 658 | + lengthAttr, |
| 659 | + builder.getI32IntegerAttr(op.opcode), |
| 660 | + builder.getI32IntegerAttr(op.streamId), |
| 661 | + dataAttr); |
| 662 | + } |
| 663 | + |
| 664 | + return module; |
| 665 | +} |
| 666 | + |
454 | 667 | static LogicalResult convertAIEToConfiguration(AIE::DeviceOp device, |
455 | 668 | StringRef clElfDir, |
456 | 669 | OutputType outputType) { |
|
0 commit comments