From fd66b8052488ec1242c0712e934f56066b34a688 Mon Sep 17 00:00:00 2001 From: Jeff Fifield Date: Wed, 11 Sep 2024 21:27:26 -0600 Subject: [PATCH] Remove aie to ctrl pkt conversion from aie-translate --- include/aie-c/Translation.h | 3 - include/aie/Targets/AIETargets.h | 9 - lib/CAPI/Translation.cpp | 33 --- lib/Targets/AIETargetCDODirect.cpp | 343 ----------------------------- lib/Targets/AIETargets.cpp | 18 -- python/AIEMLIRModule.cpp | 17 -- python/compiler/aiecc/main.py | 1 - python/dialects/aie.py | 1 - 8 files changed, 425 deletions(-) diff --git a/include/aie-c/Translation.h b/include/aie-c/Translation.h index e326dd7917..fe025e40a2 100644 --- a/include/aie-c/Translation.h +++ b/include/aie-c/Translation.h @@ -34,9 +34,6 @@ MLIR_CAPI_EXPORTED MlirLogicalResult aieTranslateToCDODirect(MlirOperation moduleOp, MlirStringRef workDirPath, bool bigEndian, bool emitUnified, bool cdoDebug, bool aieSim, bool xaieDebug, bool enableCores); -MLIR_CAPI_EXPORTED MlirLogicalResult aieTranslateToCtrlpkt( - MlirOperation moduleOp, MlirStringRef outputFile, MlirStringRef workDirPath, - bool aieSim, bool xaieDebug, bool enableCores); MLIR_CAPI_EXPORTED MlirOperation aieTranslateBinaryToTxn(MlirContext ctx, MlirStringRef binary); diff --git a/include/aie/Targets/AIETargets.h b/include/aie/Targets/AIETargets.h index 9a6473ddb2..53e8b914a4 100644 --- a/include/aie/Targets/AIETargets.h +++ b/include/aie/Targets/AIETargets.h @@ -63,11 +63,6 @@ AIETranslateToCDODirect(mlir::ModuleOp m, llvm::StringRef workDirPath, bool bigEndian = false, bool emitUnified = false, bool cdoDebug = false, bool aieSim = false, bool xaieDebug = false, bool enableCores = true); -mlir::LogicalResult -AIETranslateToControlPackets(mlir::ModuleOp m, llvm::raw_ostream &output, - llvm::StringRef workDirPath, - bool outputBinary = false, bool aieSim = false, - bool xaieDebug = false, bool enableCores = true); #ifdef AIE_ENABLE_AIRBIN mlir::LogicalResult AIETranslateToAirbin(mlir::ModuleOp module, @@ -79,10 +74,6 @@ mlir::LogicalResult AIETranslateToAirbin(mlir::ModuleOp module, mlir::LogicalResult AIETranslateToTargetArch(mlir::ModuleOp module, llvm::raw_ostream &output); -std::optional -AIETranslateBinaryToCtrlpkt(mlir::MLIRContext *ctx, - std::vector &binary); - } // namespace AIE namespace aievec { diff --git a/lib/CAPI/Translation.cpp b/lib/CAPI/Translation.cpp index 637eed4a2c..01cd5ddefb 100644 --- a/lib/CAPI/Translation.cpp +++ b/lib/CAPI/Translation.cpp @@ -84,39 +84,6 @@ MlirLogicalResult aieTranslateToCDODirect(MlirOperation moduleOp, return wrap(status); } -MlirLogicalResult aieTranslateToCtrlpkt(MlirOperation moduleOp, - MlirStringRef outputFile, - MlirStringRef workDirPath, bool aieSim, - bool xaieDebug, bool enableCores) { - ModuleOp mod = llvm::cast(unwrap(moduleOp)); - bool outputBinary = false; - - std::string errorMessage; - auto output = openOutputFile(StringRef(outputFile.data, outputFile.length), - &errorMessage); - if (!output) { - llvm::errs() << errorMessage << "\n"; - return wrap(failure()); - } - - auto status = AIETranslateToControlPackets( - mod, output->os(), llvm::StringRef(workDirPath.data, workDirPath.length), - outputBinary, aieSim, xaieDebug, enableCores); - - std::vector diagnostics; - ScopedDiagnosticHandler handler(mod.getContext(), [&](Diagnostic &d) { - llvm::raw_string_ostream(diagnostics.emplace_back()) - << d.getLocation() << ": " << d; - }); - - if (failed(status)) - for (const auto &diagnostic : diagnostics) - std::cerr << diagnostic << "\n"; - else - output->keep(); - return wrap(status); -} - MlirOperation aieTranslateBinaryToTxn(MlirContext ctx, MlirStringRef binary) { std::vector binaryData(binary.data, binary.data + binary.length); auto mod = convertTransactionBinaryToMLIR(unwrap(ctx), binaryData); diff --git a/lib/Targets/AIETargetCDODirect.cpp b/lib/Targets/AIETargetCDODirect.cpp index b25760532a..489e98d129 100644 --- a/lib/Targets/AIETargetCDODirect.cpp +++ b/lib/Targets/AIETargetCDODirect.cpp @@ -157,219 +157,6 @@ translateToCDODirect(ModuleOp m, llvm::StringRef workDirPath, return result; } -namespace { - -// An TransactionBinaryOperation encapulates an aie-rt TnxCmd struct -struct TransactionBinaryOperation { - struct XAie_TxnCmd cmd; - TransactionBinaryOperation(XAie_TxnOpcode opc, uint32_t mask, uint64_t addr, - uint32_t value, const uint8_t *data, - uint32_t size) { - cmd.Opcode = opc; - cmd.Mask = mask; - cmd.RegOff = addr; - cmd.Value = value; - cmd.DataPtr = reinterpret_cast(data); - cmd.Size = size; - } -}; -} // namespace - -// Parse a TXN binary blob. On success return the number of columns from the -// header and a vector of parsed operations. On failure return std::nullopt. -static std::optional -parseTransactionBinary(const std::vector &data, - std::vector &ops) { - - uint32_t major = data[0]; - uint32_t minor = data[1]; - uint32_t num_cols = data[4]; - - uint32_t num_ops, txn_size; - std::memcpy(&num_ops, &data[8], 4); - std::memcpy(&txn_size, &data[12], 4); - - LLVM_DEBUG(llvm::dbgs() << "Major: " << major << "\n"); - LLVM_DEBUG(llvm::dbgs() << "Minor: " << minor << "\n"); - LLVM_DEBUG(llvm::dbgs() << "DevGen: " << data[2] << "\n"); - LLVM_DEBUG(llvm::dbgs() << "NumRows: " << data[3] << "\n"); - LLVM_DEBUG(llvm::dbgs() << "NumCols: " << num_cols << "\n"); - LLVM_DEBUG(llvm::dbgs() << "NumMemTileRows: " << data[5] << "\n"); - LLVM_DEBUG(llvm::dbgs() << "NumOps: " << num_ops << "\n"); - LLVM_DEBUG(llvm::dbgs() << "TxnSize: " << txn_size << " bytes\n"); - - size_t i = 16; - - // Convert opcode from uint8 to enum - auto convertOpcode = [](uint8_t opc) { - switch (opc) { - case 0: - return XAie_TxnOpcode::XAIE_IO_WRITE; - case 1: - return XAie_TxnOpcode::XAIE_IO_BLOCKWRITE; - case 3: - return XAie_TxnOpcode::XAIE_IO_MASKWRITE; - default: - llvm::errs() << "Unhandled opcode: " << std::to_string(opc) << "\n"; - return XAie_TxnOpcode::XAIE_IO_CUSTOM_OP_MAX; - } - }; - - // Parse the binary blob. There are two versions supported, 0.1 and 1.0. - // For both versions, build a list of TransactionBinaryOperation objects - // representing the parsed operations. - if (major == 0 && minor == 1) { - while (i < data.size()) { - - XAie_TxnOpcode opc = convertOpcode(data[i]); - LLVM_DEBUG(llvm::dbgs() << "opcode: " + std::to_string(opc) + "\n"); - - uint64_t addr = 0; - uint32_t value = 0; - uint32_t size = 0; - uint32_t mask = 0; - const uint8_t *data_ptr = nullptr; - - if (opc == XAie_TxnOpcode::XAIE_IO_WRITE) { - LLVM_DEBUG(llvm::dbgs() << "opcode: WRITE (0x00)\n"); - uint32_t addr0, addr1; - std::memcpy(&addr0, &data[i + 8], 4); - std::memcpy(&addr1, &data[i + 12], 4); - std::memcpy(&value, &data[i + 16], 4); - std::memcpy(&size, &data[i + 20], 4); - addr = static_cast(addr1) << 32 | addr0; - i += size; - } else if (opc == XAie_TxnOpcode::XAIE_IO_BLOCKWRITE) { - LLVM_DEBUG(llvm::dbgs() << "opcode: BLOCKWRITE (0x01)\n"); - std::memcpy(&addr, &data[i + 8], 4); - std::memcpy(&size, &data[i + 12], 4); - data_ptr = data.data() + i + 16; - i += size; - size = size - 16; - } else if (opc == XAie_TxnOpcode::XAIE_IO_MASKWRITE) { - LLVM_DEBUG(llvm::dbgs() << "opcode: MASKWRITE (0x03)\n"); - uint32_t addr0, addr1; - std::memcpy(&addr0, &data[i + 8], 4); - std::memcpy(&addr1, &data[i + 12], 4); - std::memcpy(&value, &data[i + 16], 4); - std::memcpy(&mask, &data[i + 20], 4); - std::memcpy(&size, &data[i + 24], 4); - addr = static_cast(addr1) << 32 | addr0; - i += size; - } else { - llvm::errs() << "Unhandled opcode: " << std::to_string(opc) << "\n"; - return std::nullopt; - } - ops.emplace_back(opc, mask, addr, value, data_ptr, size); - LLVM_DEBUG(llvm::dbgs() << "addr: " << addr << "\n"); - LLVM_DEBUG(llvm::dbgs() << "value: " << value << "\n"); - LLVM_DEBUG(llvm::dbgs() << "size: " << size << "\n"); - LLVM_DEBUG(llvm::dbgs() << "mask: " << mask << "\n"); - LLVM_DEBUG(llvm::dbgs() - << "data: " << reinterpret_cast(data_ptr) << "\n"); - } - } else if (major == 1 && minor == 0) { - while (i < data.size()) { - - XAie_TxnOpcode opc = convertOpcode(data[i]); - LLVM_DEBUG(llvm::dbgs() << "opcode: " + std::to_string(opc) + "\n"); - - uint64_t addr = 0; - uint32_t value = 0; - uint32_t size = 0; - uint32_t mask = 0; - const uint8_t *data_ptr = nullptr; - - if (opc == XAie_TxnOpcode::XAIE_IO_WRITE) { - LLVM_DEBUG(llvm::dbgs() << "opcode: WRITE (0x00)\n"); - std::memcpy(&addr, &data[i + 4], 4); - std::memcpy(&value, &data[i + 8], 4); - i += 12; - } else if (opc == XAie_TxnOpcode::XAIE_IO_BLOCKWRITE) { - LLVM_DEBUG(llvm::dbgs() << "opcode: BLOCKWRITE (0x01)\n"); - std::memcpy(&addr, &data[i + 4], 4); - std::memcpy(&size, &data[i + 8], 4); - data_ptr = data.data() + i + 12; - i += size; - size = size - 12; - } else if (opc == XAie_TxnOpcode::XAIE_IO_MASKWRITE) { - LLVM_DEBUG(llvm::dbgs() << "opcode: MASKWRITE (0x03)\n"); - std::memcpy(&addr, &data[i + 4], 4); - std::memcpy(&value, &data[i + 8], 4); - std::memcpy(&mask, &data[i + 12], 4); - i += 16; - } else { - llvm::errs() << "Unhandled opcode: " << std::to_string(opc) << "\n"; - return std::nullopt; - } - LLVM_DEBUG(llvm::dbgs() << "addr: " << addr << "\n"); - LLVM_DEBUG(llvm::dbgs() << "value: " << value << "\n"); - LLVM_DEBUG(llvm::dbgs() << "size: " << size << "\n"); - LLVM_DEBUG(llvm::dbgs() << "mask: " << mask << "\n"); - LLVM_DEBUG(llvm::dbgs() - << "data: " << reinterpret_cast(data_ptr) << "\n"); - ops.emplace_back(opc, mask, addr, value, data_ptr, size); - } - } else { - llvm::errs() << "Unsupported TXN binary version: " << major << "." << minor - << "\n"; - return std::nullopt; - } - - return num_cols; -} - -static LogicalResult generateTxn(AIERTControl &ctl, const StringRef workDirPath, - DeviceOp &targetOp, bool aieSim, - bool enableElfs, bool enableInit, - bool enableCores) { - if (enableElfs && !targetOp.getOps().empty() && - failed(ctl.addAieElfs(targetOp, workDirPath, aieSim))) - return failure(); - if (enableInit && failed(ctl.addInitConfig(targetOp))) - return failure(); - if (enableCores && !targetOp.getOps().empty() && - failed(ctl.addCoreEnable(targetOp))) - return failure(); - return success(); -} - -static LogicalResult translateToTxn(ModuleOp m, std::vector &output, - llvm::StringRef workDirPath, bool aieSim, - bool xaieDebug, bool enableCores) { - - auto devOps = m.getOps(); - if (llvm::range_size(devOps) > 1) - return m.emitError("only exactly 1 device op supported."); - - DeviceOp targetOp = *devOps.begin(); - const BaseNPUTargetModel &targetModel = - (const BaseNPUTargetModel &)targetOp.getTargetModel(); - - if (!targetModel.isNPU()) - return failure(); - - AIERTControl ctl(targetModel); - if (failed(ctl.setIOBackend(aieSim, xaieDebug))) - return failure(); - - // start collecting transations - XAie_StartTransaction(&ctl.devInst, XAIE_TRANSACTION_DISABLE_AUTO_FLUSH); - - auto result = - generateTxn(ctl, workDirPath, targetOp, aieSim, true, true, true); - if (failed(result)) - return result; - - // Export the transactions to a buffer - uint8_t *txn_ptr = XAie_ExportSerializedTransaction(&ctl.devInst, 0, 0); - XAie_TxnHeader *hdr = (XAie_TxnHeader *)txn_ptr; - std::vector txn_data(txn_ptr, txn_ptr + hdr->TxnSize); - output.swap(txn_data); - - return success(); -} - LogicalResult xilinx::AIE::AIETranslateToCDODirect( ModuleOp m, llvm::StringRef workDirPath, bool bigEndian, bool emitUnified, bool cdoDebug, bool aieSim, bool xaieDebug, bool enableCores) { @@ -378,133 +165,3 @@ LogicalResult xilinx::AIE::AIETranslateToCDODirect( return translateToCDODirect(m, workDirPath, endianness, emitUnified, cdoDebug, aieSim, xaieDebug, enableCores); } - -std::optional -xilinx::AIE::AIETranslateBinaryToCtrlpkt(mlir::MLIRContext *ctx, - std::vector &binary) { - - // parse the binary - std::vector operations; - auto c = parseTransactionBinary(binary, operations); - if (!c) { - llvm::errs() << "Failed to parse binary\n"; - return std::nullopt; - } - int columns = *c; - - auto loc = mlir::UnknownLoc::get(ctx); - - // create a new ModuleOp and set the insertion point - auto module = ModuleOp::create(loc); - OpBuilder builder(module.getBodyRegion()); - builder.setInsertionPointToStart(module.getBody()); - - // create aie.device - std::vector devices{AIEDevice::npu1_1col, AIEDevice::npu1_2col, - AIEDevice::npu1_3col, AIEDevice::npu1_4col, - AIEDevice::npu1}; - auto device = builder.create(loc, devices[columns - 1]); - device.getRegion().emplaceBlock(); - builder.setInsertionPointToStart(device.getBody()); - - // for each blockwrite in the binary, create a GlobalOp with the data - std::vector global_data; - for (auto &op : operations) { - if (op.cmd.Opcode != XAIE_IO_BLOCKWRITE) { - global_data.push_back(nullptr); - continue; - } - uint32_t size = op.cmd.Size / 4; - const uint32_t *d = reinterpret_cast(op.cmd.DataPtr); - std::vector data32(d, d + size); - - int id = 0; - std::string name = "blockwrite_data"; - while (device.lookupSymbol(name)) - name = "blockwrite_data_" + std::to_string(id++); - - MemRefType memrefType = MemRefType::get({size}, builder.getI32Type()); - TensorType tensorType = RankedTensorType::get({size}, builder.getI32Type()); - auto global = builder.create( - loc, name, builder.getStringAttr("private"), memrefType, - DenseElementsAttr::get(tensorType, data32), true, nullptr); - global_data.push_back(global); - } - - // create aiex.runtime_sequence - auto seq = builder.create(loc, nullptr); - seq.getBody().push_back(new Block); - - // create the txn ops - builder.setInsertionPointToStart(&seq.getBody().front()); - for (auto p : llvm::zip(operations, global_data)) { - auto op = std::get<0>(p); - memref::GlobalOp payload = std::get<1>(p); - - if (op.cmd.Opcode == XAie_TxnOpcode::XAIE_IO_WRITE) { - builder.create( - loc, builder.getUI32IntegerAttr(op.cmd.RegOff), nullptr, - /*opcode*/ builder.getI32IntegerAttr(0), - /*stream_id*/ builder.getI32IntegerAttr(0), - DenseI32ArrayAttr::get(ctx, ArrayRef(op.cmd.Value))); - } else if (op.cmd.Opcode == XAie_TxnOpcode::XAIE_IO_BLOCKWRITE) { - if (!std::get<1>(p).getInitialValue()) - continue; - auto blockWriteData = - dyn_cast(*std::get<1>(p).getInitialValue()); - if (!blockWriteData) { - payload.emitError( - "Global symbol initial value is not a dense int array"); - break; - } - auto blockWriteDataValues = blockWriteData.getValues(); - // Split block write data into beats of 4 or less, in int32_t. - int currAddr = op.cmd.RegOff; - for (size_t i = 0; i < blockWriteDataValues.size(); i += 4) { - auto last = std::min(blockWriteDataValues.size(), i + 4); - SmallVector splitData = - SmallVector(blockWriteDataValues.begin() + i, - blockWriteDataValues.begin() + last); - builder.create( - loc, builder.getUI32IntegerAttr(currAddr), nullptr, - /*opcode*/ builder.getI32IntegerAttr(0), - /*stream_id*/ builder.getI32IntegerAttr(0), - DenseI32ArrayAttr::get(ctx, ArrayRef(splitData))); - currAddr += splitData.size() * sizeof(int32_t); - } - - } else if (op.cmd.Opcode == XAie_TxnOpcode::XAIE_IO_MASKWRITE) { - builder.create( - loc, builder.getUI32IntegerAttr(op.cmd.RegOff), nullptr, - /*opcode*/ builder.getI32IntegerAttr(0), - /*stream_id*/ builder.getI32IntegerAttr(0), - DenseI32ArrayAttr::get(ctx, ArrayRef(op.cmd.Value))); - } else { - llvm::errs() << "Unhandled txn opcode: " << op.cmd.Opcode << "\n"; - return std::nullopt; - } - } - - return module; -} - -LogicalResult xilinx::AIE::AIETranslateToControlPackets( - ModuleOp m, llvm::raw_ostream &output, llvm::StringRef workDirPath, - bool outputBinary, bool enableSim, bool xaieDebug, bool enableCores) { - std::vector bin; - auto result = - translateToTxn(m, bin, workDirPath, enableSim, xaieDebug, enableCores); - if (failed(result)) - return result; - - if (outputBinary) { - output.write(reinterpret_cast(bin.data()), bin.size()); - return success(); - } - - auto new_module = AIETranslateBinaryToCtrlpkt(m.getContext(), bin); - if (!new_module) - return failure(); - new_module->print(output); - return success(); -} diff --git a/lib/Targets/AIETargets.cpp b/lib/Targets/AIETargets.cpp index b1b341f6c4..625f3c1a03 100644 --- a/lib/Targets/AIETargets.cpp +++ b/lib/Targets/AIETargets.cpp @@ -375,23 +375,5 @@ void registerAIETranslations() { sequenceName); }, registerDialects); - TranslateFromMLIRRegistration registrationCDOWithCtrlpkt( - "aie-generate-ctrlpkt", - "Generate control packet configuration. Use --aie-output-binary to " - "select between mlir (default) and binary output", - [](ModuleOp module, raw_ostream &output) { - SmallString<128> workDirPath_; - if (workDirPath.getNumOccurrences() == 0) { - if (llvm::sys::fs::current_path(workDirPath_)) - llvm::report_fatal_error( - "couldn't get cwd to use as work-dir-path"); - } else - workDirPath_ = workDirPath.getValue(); - LLVM_DEBUG(llvm::dbgs() << "work-dir-path: " << workDirPath_ << "\n"); - return AIETranslateToControlPackets(module, output, workDirPath_, - outputBinary, cdoAieSim, - cdoXaieDebug, cdoEnableCores); - }, - registerDialects); } } // namespace xilinx::AIE diff --git a/python/AIEMLIRModule.cpp b/python/AIEMLIRModule.cpp index fa3d214dac..8ecc3c5b92 100644 --- a/python/AIEMLIRModule.cpp +++ b/python/AIEMLIRModule.cpp @@ -107,23 +107,6 @@ PYBIND11_MODULE(_aie, m) { "emit_unified"_a = false, "cdo_debug"_a = false, "aiesim"_a = false, "xaie_debug"_a = false, "enable_cores"_a = true); - m.def( - "generate_ctrlpkt", - [](MlirOperation op, const std::string &outputFile, - const std::string &workDirPath, bool aieSim, bool xaieDebug, - bool enableCores) { - mlir::python::CollectDiagnosticsToStringScope scope( - mlirOperationGetContext(op)); - if (mlirLogicalResultIsFailure(aieTranslateToCtrlpkt( - op, {outputFile.data(), outputFile.size()}, - {workDirPath.data(), workDirPath.size()}, aieSim, xaieDebug, - enableCores))) - throw py::value_error("Failed to generate control packets because: " + - scope.takeMessage()); - }, - "module"_a, "output_file"_a, "work_dir_path"_a, "aiesim"_a = false, - "xaie_debug"_a = false, "enable_cores"_a = true); - m.def( "transaction_binary_to_mlir", [](MlirContext ctx, py::bytes bytes) { diff --git a/python/compiler/aiecc/main.py b/python/compiler/aiecc/main.py index 6642a6a8bb..5903dde182 100644 --- a/python/compiler/aiecc/main.py +++ b/python/compiler/aiecc/main.py @@ -589,7 +589,6 @@ async def process_txn(self): ) async def process_ctrlpkt(self): - from aie.dialects.aie import generate_ctrlpkt with Context(), Location.unknown(): for elf in glob.glob("*.elf"): diff --git a/python/dialects/aie.py b/python/dialects/aie.py index c810aab0a6..594a4b1e54 100644 --- a/python/dialects/aie.py +++ b/python/dialects/aie.py @@ -22,7 +22,6 @@ aie_llvm_link, generate_bcf, generate_cdo, - generate_ctrlpkt, generate_xaie, generate_control_packets, npu_instgen,