diff --git a/compilers/concrete-compiler/compiler/include/concretelang/Analysis/TypeInferenceAnalysis.h b/compilers/concrete-compiler/compiler/include/concretelang/Analysis/TypeInferenceAnalysis.h index 0851766de..9818d4288 100644 --- a/compilers/concrete-compiler/compiler/include/concretelang/Analysis/TypeInferenceAnalysis.h +++ b/compilers/concrete-compiler/compiler/include/concretelang/Analysis/TypeInferenceAnalysis.h @@ -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(funcOp)) + funcOp = funcOp->getParentOp(); + + assert(funcOp); + + dumpStateForOp(funcOp, 0); + } + TypeResolver &resolver; };