@@ -103,10 +103,10 @@ struct TxnLoadPdiHeader {
103103// A ControlPacketOperation encapsulates a parsed control packet including
104104// stream header, control packet header, and optional data payload.
105105struct ControlPacketOperation {
106- uint32_t streamHeader; // word 0: parity + pkt_type + pkt_id
107- uint32_t packetHeader; // word 1: parity + stream_id + opcode + beats + addr
108- std::vector<uint32_t > data; // payload words (if opcode is write/blockwrite)
109-
106+ uint32_t streamHeader; // word 0: parity + pkt_type + pkt_id
107+ uint32_t packetHeader; // word 1: parity + stream_id + opcode + beats + addr
108+ std::vector<uint32_t > data; // payload words (if opcode is write/blockwrite)
109+
110110 // Decoded fields
111111 uint8_t pktType;
112112 uint8_t pktId;
@@ -138,90 +138,92 @@ static bool checkParity(uint32_t word) {
138138static std::optional<std::vector<ControlPacketOperation>>
139139parseControlPacket (const std::vector<uint8_t > &data) {
140140 std::vector<ControlPacketOperation> ops;
141-
141+
142142 size_t i = 0 ;
143-
143+
144144 auto requireBytes = [&](size_t offset, size_t length) -> bool {
145145 if (offset + length > data.size ()) {
146146 llvm::errs () << " Control packet binary truncated\n " ;
147147 return false ;
148148 }
149149 return true ;
150150 };
151-
151+
152152 auto read32 = [&](size_t offset) -> uint32_t {
153153 uint32_t value;
154154 std::memcpy (&value, data.data () + offset, sizeof (uint32_t ));
155155 return value;
156156 };
157-
158- while (i + 8 <= data.size ()) { // Need at least 2 words (stream hdr + pkt hdr)
157+
158+ while (i + 8 <= data.size ()) { // Need at least 2 words (stream hdr + pkt hdr)
159159 ControlPacketOperation op;
160-
160+
161161 // Read stream header (word 0)
162162 op.streamHeader = read32 (i);
163-
163+
164164 // Read control packet header (word 1)
165165 op.packetHeader = read32 (i + 4 );
166-
166+
167167 // Verify parity
168168 if (!checkParity (op.streamHeader )) {
169- llvm::errs () << " Stream header parity check failed at offset " << i << " \n " ;
169+ llvm::errs () << " Stream header parity check failed at offset " << i
170+ << " \n " ;
170171 return std::nullopt ;
171172 }
172173 if (!checkParity (op.packetHeader )) {
173- llvm::errs () << " Packet header parity check failed at offset " << i + 4 << " \n " ;
174+ llvm::errs () << " Packet header parity check failed at offset " << i + 4
175+ << " \n " ;
174176 return std::nullopt ;
175177 }
176-
178+
177179 // Decode stream header fields
178180 op.pktType = (op.streamHeader >> 12 ) & 0x7 ;
179181 op.pktId = op.streamHeader & 0xFF ;
180-
182+
181183 // Decode control packet header fields
182184 op.streamId = (op.packetHeader >> 24 ) & 0x7F ;
183185 op.opcode = (op.packetHeader >> 22 ) & 0x3 ;
184186 op.beats = (op.packetHeader >> 20 ) & 0x3 ;
185187 op.address = op.packetHeader & 0xFFFFF ;
186-
187- i += 8 ; // consumed 2 words
188-
188+
189+ i += 8 ; // consumed 2 words
190+
189191 LLVM_DEBUG (llvm::dbgs () << " Control packet at offset " << (i - 8 )
190192 << " : opcode=" << static_cast <int >(op.opcode )
191193 << " stream_id=" << static_cast <int >(op.streamId )
192194 << " addr=0x" << llvm::format (" %05X" , op.address )
193195 << " beats=" << static_cast <int >(op.beats ) << " \n " );
194-
196+
195197 // Read data payload if present (opcode 0x0=write or 0x2=blockwrite)
196198 if (op.opcode == 0x0 || op.opcode == 0x2 ) {
197199 uint32_t numDataWords = op.beats + 1 ;
198200 if (!requireBytes (i, numDataWords * 4 )) {
199201 llvm::errs () << " Truncated data payload\n " ;
200202 return std::nullopt ;
201203 }
202-
204+
203205 op.data .resize (numDataWords);
204206 for (uint32_t j = 0 ; j < numDataWords; j++) {
205207 op.data [j] = read32 (i + j * 4 );
206208 }
207209 i += numDataWords * 4 ;
208-
209- LLVM_DEBUG (llvm::dbgs () << " Data: [" ;
210- for ( size_t j = 0 ; j < op.data .size (); j++) {
211- if (j > 0 ) llvm::dbgs () << " , " ;
212- llvm::dbgs () << op. data [j] ;
213- }
214- llvm::dbgs () << " ]\n " );
210+
211+ LLVM_DEBUG (llvm::dbgs () << " Data: [" ; for ( size_t j = 0 ;
212+ j < op.data .size (); j++) {
213+ if (j > 0 )
214+ llvm::dbgs () << " , " ;
215+ llvm::dbgs () << op. data [j];
216+ } llvm::dbgs () << " ]\n " );
215217 }
216-
218+
217219 ops.push_back (std::move (op));
218220 }
219-
221+
220222 if (i != data.size ()) {
221- llvm::errs () << " Warning: " << (data.size () - i)
223+ llvm::errs () << " Warning: " << (data.size () - i)
222224 << " bytes remaining after parsing control packets\n " ;
223225 }
224-
226+
225227 return ops;
226228}
227229
@@ -899,11 +901,11 @@ xilinx::AIE::convertControlPacketBinaryToMLIR(mlir::MLIRContext *ctx,
899901 uint32_t colInt = (op.address >> targetModel.getColumnShift ()) & 0x1f ;
900902 uint32_t rowInt = (op.address >> targetModel.getRowShift ()) & 0x1f ;
901903 tileMap[key] = std::make_pair (colInt, rowInt);
902-
904+
903905 // Create tile and set controller_id attribute
904906 auto tile = TileOp::getOrCreate (builder, device, colInt, rowInt);
905- auto packetInfoAttr = AIE::PacketInfoAttr::get (
906- builder.getContext (), op.pktType , op.pktId );
907+ auto packetInfoAttr =
908+ AIE::PacketInfoAttr::get ( builder.getContext (), op.pktType , op.pktId );
907909 tile->setAttr (" controller_id" , packetInfoAttr);
908910 }
909911 }
@@ -919,7 +921,7 @@ xilinx::AIE::convertControlPacketBinaryToMLIR(mlir::MLIRContext *ctx,
919921 for (const auto &op : operations) {
920922 IntegerAttr lengthAttr;
921923 DenseI32ArrayAttr dataAttr;
922-
924+
923925 if (op.opcode == 0x0 || op.opcode == 0x2 ) {
924926 // Write or blockwrite - has data payload
925927 SmallVector<int32_t > dataVec;
@@ -931,14 +933,11 @@ xilinx::AIE::convertControlPacketBinaryToMLIR(mlir::MLIRContext *ctx,
931933 // Read - has length but no data
932934 lengthAttr = builder.getI32IntegerAttr (op.beats + 1 );
933935 }
934-
936+
935937 builder.create <AIEX::NpuControlPacketOp>(
936- loc,
937- builder.getUI32IntegerAttr (op.address ),
938- lengthAttr,
938+ loc, builder.getUI32IntegerAttr (op.address ), lengthAttr,
939939 builder.getI32IntegerAttr (op.opcode ),
940- builder.getI32IntegerAttr (op.streamId ),
941- dataAttr);
940+ builder.getI32IntegerAttr (op.streamId ), dataAttr);
942941 }
943942
944943 return module ;
0 commit comments