feat(compiler): Add functions for type inference debugging to TypeInferenceUtils

The main debugging function is
`TypeInferenceUtils::dumpAllState(mlir::Operation* op)` which dumps
the entire state of type inference for the function containing `op`.
This commit is contained in:
Andi Drebes
2024-02-05 11:51:39 +01:00
parent 282cacaef4
commit 3d16efb681

View File

@@ -916,6 +916,63 @@ protected:
}
}
// Prints an indentation composed of `indent` times `" "`.
void printIndent(int indent) {
for (int i = 0; i < indent; i++)
llvm::dbgs() << " ";
}
// Dumps the state of type inference for the operation `op` with an
// indentation level of `indent` as the name of the operation,
// followed by the types inferred for each operand, followed by
// `->`, followed by a dump of the state for any operation nested in
// any region of `op`.
void dumpStateForOp(mlir::Operation *op, int indent) {
const LocalInferenceState state = getCurrentInferredTypes(op);
printIndent(indent);
llvm::dbgs() << op->getName() << " {";
llvm::interleaveComma(
op->getAttrs(), llvm::dbgs(), [&](const mlir::NamedAttribute &attr) {
llvm::dbgs() << attr.getName() << " = " << attr.getValue();
});
llvm::dbgs() << "} : (";
llvm::interleaveComma(op->getOperands(), llvm::dbgs(), [&](mlir::Value v) {
llvm::dbgs() << state.find(v);
});
llvm::dbgs() << ") -> (";
llvm::interleaveComma(op->getResults(), llvm::dbgs(), [&](mlir::Value v) {
llvm::dbgs() << state.find(v);
});
llvm::dbgs() << ")\n";
for (mlir::Region &r : op->getRegions())
for (mlir::Block &b : r.getBlocks())
for (mlir::Operation &childOp : b.getOperations())
dumpStateForOp(&childOp, indent + 1);
}
// Dumps the entire state of type inference for the function
// containing the operation `op`. For each operation, this prints
// the name of the operation, followed by the types inferred for
// each operand, followed by `->`, followed by the types inferred
// for the results.
void dumpAllState(mlir::Operation *op) {
mlir::Operation *funcOp = op;
while (funcOp && !llvm::isa<mlir::func::FuncOp>(funcOp))
funcOp = funcOp->getParentOp();
assert(funcOp);
dumpStateForOp(funcOp, 0);
}
TypeResolver &resolver;
};