11 #include "mlir/IR/BuiltinOps.h"
12 #include "mlir/IR/Operation.h"
13 #include "mlir/IR/Threading.h"
14 #include "mlir/Pass/Pass.h"
15 #include "llvm/ADT/SmallVector.h"
19 template <
typename OpOrBlockArgument>
21 if (op->getLoc() != newLoc)
26 struct StripDebugInfoWithPred
27 :
public circt::StripDebugInfoWithPredBase<StripDebugInfoWithPred> {
28 StripDebugInfoWithPred(
const std::function<
bool(mlir::Location)> &pred)
30 void runOnOperation()
override;
33 mlir::Location getStrippedLoc(Location loc) {
38 if (
auto fusedLoc = loc.dyn_cast<FusedLoc>()) {
39 SmallVector<mlir::Location> newLocations;
40 newLocations.reserve(fusedLoc.getLocations().size());
41 for (
auto loc : fusedLoc.getLocations())
42 newLocations.push_back(getStrippedLoc(loc));
43 return FusedLoc::get(&getContext(), newLocations, fusedLoc.getMetadata());
50 void updateLocArray(Operation *op, StringRef attributeName) {
51 SmallVector<Attribute> newLocs;
52 if (
auto resLocs = op->getAttrOfType<ArrayAttr>(attributeName)) {
54 for (
auto loc : resLocs.getAsRange<LocationAttr>()) {
55 auto newLoc = getStrippedLoc(loc);
56 changed |= newLoc != loc;
57 newLocs.push_back(newLoc);
60 op->setAttr(attributeName,
ArrayAttr::get(&getContext(), newLocs));
65 std::function<bool(mlir::Location)> pred;
69 void StripDebugInfoWithPred::runOnOperation() {
72 if (!pred && !dropSuffix.empty()) {
73 pred = [&](mlir::Location loc) {
74 if (
auto fileLoc = loc.dyn_cast<FileLineColLoc>())
75 return fileLoc.getFilename().getValue().endswith(dropSuffix);
81 getOperation().emitWarning()
82 <<
"predicate is uninitialized. No debug information is stripped.";
87 &getContext(), getOperation().getOps(), [&](Operation &toplevelOp) {
88 toplevelOp.walk([&](Operation *op) {
90 updateLocArray(op,
"argLocs");
91 updateLocArray(op,
"resultLocs");
92 updateLocArray(op,
"portLocs");
94 for (Region ®ion : op->getRegions())
95 for (Block &block : region.getBlocks())
96 for (BlockArgument &arg : block.getArguments())
105 const std::function<
bool(mlir::Location)> &pred) {
106 return std::make_unique<StripDebugInfoWithPred>(pred);
static void updateLocIfChanged(OpOrBlockArgument *op, Location newLoc)
Direction get(bool isOutput)
Returns an output direction if isOutput is true, otherwise returns an input direction.
This file defines an intermediate representation for circuits acting as an abstraction for constraint...
std::unique_ptr< mlir::Pass > createStripDebugInfoWithPredPass(const std::function< bool(mlir::Location)> &pred)
Creates a pass to strip debug information from a function.