MLIR-AIE 1.4.5.dev
AIETargetCppTxn.cpp
Go to the documentation of this file.
1//===- AIETargetCppTxn.cpp - Generate C++ TXN builder ----------*- C++ -*-===//
2//
3// Copyright (C) 2026 Advanced Micro Devices, Inc.
4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
5//
6//===----------------------------------------------------------------------===//
7//
8// aie-translate target that lowers an aie.runtime_sequence's npu transaction
9// ops into a standalone C++ function (via convert-aiex-to-emitc +
10// translateToCpp). The input is expected to be already lowered to npu ops, the
11// same precondition as the aie-npu-to-binary target; this is its
12// runtime-parameterizable C++ counterpart.
13//
14//===----------------------------------------------------------------------===//
15
19
20#include "mlir/Dialect/ControlFlow/IR/ControlFlowOps.h"
21#include "mlir/IR/Dominance.h"
22#include "mlir/Pass/PassManager.h"
23#include "mlir/Target/Cpp/CppEmitter.h"
24#include "mlir/Transforms/CSE.h"
25#include "mlir/Transforms/GreedyPatternRewriteDriver.h"
26
27using namespace mlir;
28
29LogicalResult xilinx::AIE::AIETranslateNpuToCpp(ModuleOp module,
30 raw_ostream &output,
31 bool foldDDRAddrOffset,
32 bool emitDispatchShim) {
33 // A staged sequence repeats the same guard arithmetic at every use of a
34 // scalar (each tap re-derives and re-checks its shape). CSE merges the
35 // duplicated arithmetic and folding drops the guards that became constant.
36 // Scoped to the sequences: the rest of the device is not this target's to
37 // rewrite.
38 MLIRContext *ctx = module.getContext();
39 RewritePatternSet patterns(ctx);
40 cf::AssertOp::getCanonicalizationPatterns(patterns, ctx);
41 FrozenRewritePatternSet frozen(std::move(patterns));
42 IRRewriter rewriter(ctx);
43 DominanceInfo domInfo;
44 module.walk([&](AIE::RuntimeSequenceOp seq) {
45 eliminateCommonSubExpressions(rewriter, domInfo, seq.getBody());
46 SmallVector<Operation *> ops;
47 seq.getBody().walk([&](Operation *op) { ops.push_back(op); });
48 (void)applyOpPatternsGreedily(ops, frozen);
49 });
50
51 PassManager pm(ctx);
52 pm.addPass(xilinx::createConvertAIEXToEmitCPass(foldDDRAddrOffset,
53 emitDispatchShim));
54 if (failed(pm.run(module)))
55 return failure();
56 return emitc::translateToCpp(module, output);
57}
mlir::LogicalResult AIETranslateNpuToCpp(mlir::ModuleOp module, llvm::raw_ostream &output, bool foldDDRAddrOffset=true, bool emitDispatchShim=false)
Emit a C++ builder that reconstructs the TXN stream at runtime.
std::unique_ptr< mlir::OperationPass< mlir::ModuleOp > > createConvertAIEXToEmitCPass()