Skip to content

Commit 4e9f5e0

Browse files
committed
[CIR] Add new --cir-keep-aie-device pass
Extract the aie.device operation as the top-module operation. Anything else is removed. For now just keep the first aie.device.
1 parent b10665d commit 4e9f5e0

File tree

3 files changed

+50
-0
lines changed

3 files changed

+50
-0
lines changed

include/aie/CIR/CIRToAIEPasses.h

+2
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ createCIRToAIEInlineKernelLambdaPass();
3131
std::unique_ptr<mlir::OperationPass<mlir::ModuleOp>>
3232
createCIRToAIEDecaptureKernelPass();
3333

34+
std::unique_ptr<mlir::OperationPass<mlir::ModuleOp>> createCIRKeepAIEDevice();
35+
3436
/// Generate the code for registering passes.
3537
#define GEN_PASS_REGISTRATION
3638
#include "aie/CIR/CIRToAIEPasses.h.inc"

include/aie/CIR/CIRToAIEPasses.td

+16
Original file line numberDiff line numberDiff line change
@@ -89,4 +89,20 @@ def CIRToAIEDecaptureKernel : Pass<"cir-to-aie-decapture-kernel", "mlir::ModuleO
8989
];
9090
}
9191

92+
def CIRKeepAIEDevice : Pass<"cir-keep-aie-device", "mlir::ModuleOp"> {
93+
94+
let summary = "Remove everything but the first aie.device";
95+
96+
let description = [{ Extract the aie.device from a CIR module by removing everything but the first aie.device.
97+
98+
TODO: handle multiple devices.
99+
}];
100+
101+
let constructor = "xilinx::AIE::CIR::createCIRKeepAIEDevice()";
102+
let dependentDialects = [
103+
"cir::CIRDialect",
104+
"xilinx::AIE::AIEDialect",
105+
];
106+
}
107+
92108
#endif

lib/CIR/CIRToAIEPasses.cpp

+32
Original file line numberDiff line numberDiff line change
@@ -987,6 +987,34 @@ struct CIRToAIEDecaptureKernel
987987
}
988988
};
989989

990+
struct CIRKeepAIEDevice : CIRKeepAIEDeviceBase<CIRKeepAIEDevice> {
991+
void runOnOperation() override {
992+
auto module = getOperation();
993+
mlir::OpBuilder b{module};
994+
llvm::SmallVector<mlir::Operation *> opToDelete;
995+
xilinx::AIE::DeviceOp d;
996+
// Use pre-order walk for early exit to pick the first aie.device for now
997+
module->walk<mlir::WalkOrder::PreOrder>([&](mlir::Operation *op) {
998+
if (!mlir::isa<xilinx::AIE::DeviceOp>(op))
999+
return mlir::WalkResult::advance();
1000+
d = mlir::cast<xilinx::AIE::DeviceOp>(op);
1001+
return mlir::WalkResult::interrupt();
1002+
});
1003+
// Extract the aie.device from its function up to the top first
1004+
// operation of the module
1005+
if (d)
1006+
d->moveBefore(&module.front());
1007+
// Erase all the top-module operations but the aie.device
1008+
for (auto &op : module) {
1009+
if (auto dev = mlir::dyn_cast<xilinx::AIE::DeviceOp>(op))
1010+
if (dev == d)
1011+
continue;
1012+
opToDelete.push_back(&op);
1013+
}
1014+
eraseOpsAndUsers(opToDelete);
1015+
}
1016+
};
1017+
9901018
} // namespace
9911019

9921020
std::unique_ptr<mlir::OperationPass<mlir::ModuleOp>>
@@ -1008,4 +1036,8 @@ createCIRToAIEDecaptureKernelPass() {
10081036
return std::make_unique<CIRToAIEDecaptureKernel>();
10091037
}
10101038

1039+
std::unique_ptr<mlir::OperationPass<mlir::ModuleOp>> createCIRKeepAIEDevice() {
1040+
return std::make_unique<CIRKeepAIEDevice>();
1041+
}
1042+
10111043
} // namespace xilinx::AIE::CIR

0 commit comments

Comments
 (0)