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().ends_with(dropSuffix);
81 getOperation().emitWarning()
82 <<
"predicate is uninitialized. No debug information is stripped.";
86 auto stripLocsOnOp = [
this](Operation *op) {
88 updateLocArray(op,
"arg_locs");
89 updateLocArray(op,
"result_locs");
90 updateLocArray(op,
"port_locs");
97 SmallVector<Operation *> topLevelOpsToWalk;
98 for (
auto &op : getOperation().getOps()) {
100 if (op.getNumRegions() != 0) {
101 topLevelOpsToWalk.push_back(&op);
109 parallelForEach(&getContext(), topLevelOpsToWalk, [&](Operation *toplevelOp) {
110 toplevelOp->walk([&](Operation *op) {
113 for (Region ®ion : op->getRegions())
114 for (Block &block : region.getBlocks())
115 for (BlockArgument &arg : block.getArguments())
124 const std::function<
bool(mlir::Location)> &pred) {
125 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.