23#include <mlir/IR/AsmState.h>
25#include <llvm/ADT/STLExtras.h>
26#include <llvm/ADT/SmallVector.h>
27#include <llvm/Support/Debug.h>
28#include <llvm/Support/ErrorHandling.h>
31#define GEN_PASS_DEF_INTERVALANALYSISPRINTERPASS
35#define DEBUG_TYPE "llzk-interval-analysis-pass"
45 using Base = IntervalAnalysisPrinterPassBase<PassImpl>;
48 void runOnOperation()
override {
49 markAllAnalysesPreserved();
51 auto modOp = llvm::dyn_cast<ModuleOp>(getOperation());
53 constexpr const char *msg =
"IntervalAnalysisPrinterPass error: should be run on ModuleOp!";
54 getOperation()->emitError(msg).report();
60 if (!fieldName.empty()) {
62 if (failed(fieldLookupRes)) {
65 "IntervalAnalysisPrinterPass error: unknown field \"", fieldName,
"\" specified"
70 selectedField = fieldLookupRes.value();
72 llvm::dbgs() <<
"[IntervalAnalysisPrinterPass] using explicit -field override '"
73 << selectedField.get().name() <<
"'\n";
76 selectedField = detectedField.value();
78 llvm::dbgs() <<
"[IntervalAnalysisPrinterPass] detected module field '"
79 << selectedField.get().name() <<
"' from module felt usage\n";
82 modOp->emitWarning() <<
"could not detect a unique module field; falling back to '"
83 << selectedField.get().name() <<
'\'';
85 llvm::dbgs() <<
"[IntervalAnalysisPrinterPass] no explicit or detectable module field; "
87 << selectedField.get().name() <<
"'\n";
91 auto &mia = getAnalysis<ModuleIntervalAnalysis>();
92 mia.setField(selectedField);
93 mia.setPropagateInputConstraints(propagateInputConstraints);
94 mia.setTrackUnreducedIntervals(printUnreducedIntervals);
95 auto am = getAnalysisManager();
96 mia.ensureAnalysisRun(am);
97 AsmState asmState(modOp);
99 auto printValueInterval = [
this, &asmState, &mia](raw_ostream &out,
int indent, Value value) {
100 if (llvm::isa<llzk::array::ArrayType, StructType, llzk::pod::PodType>(value.getType())) {
110 value.printAsOperand(out, asmState);
111 if (
auto opResult = llvm::dyn_cast<OpResult>(value)) {
112 out <<
" [" << opResult.getOwner()->getName().getStringRef() <<
"]";
120 auto printFunctionSSAIntervals =
121 [&printValueInterval](raw_ostream &out,
FuncDefOp fn, llvm::StringRef fnName) {
127 out.indent(4) << fnName <<
" {";
128 for (BlockArgument arg : fn.getArguments()) {
129 printValueInterval(out, 8, arg);
131 fn.walk([&](Operation *op) {
132 if (op == fn.getOperation()) {
135 for (Value result : op->getResults()) {
136 printValueInterval(out, 8, result);
140 out.indent(4) <<
'}';
144 for (
const auto &[s, si] : mia.getCurrentResults()) {
149 "could not resolve fully qualified name of struct " + Twine(structDef.getName())
151 os << fullName.value() <<
' ';
152 si.get().print(os, printSolverConstraints, printComputeIntervals, printUnreducedIntervals);
153 if (printSSAIntervals) {
154 os << fullName.value() <<
" SSAIntervals {";
155 if (printComputeIntervals) {
159 if (
auto productFn = structDef.getProductFuncOp();
160 productFn && (!structDef.getConstrainFuncOp() || printComputeIntervals)) {
Tracks a solver expression and an interval range for that expression.
const Interval & getInterval() const
bool hasUnreducedInterval() const
const UnreducedInterval & getUnreducedInterval() const
static llvm::FailureOr< std::reference_wrapper< const Field > > tryGetField(llvm::StringRef fieldName)
Get a Field from a given field name string, or failure if the field is not defined.
static const Field & getField(llvm::StringRef fieldName, EmitErrorFn errFn)
Get a Field from a given field name string.
constexpr char FUNC_NAME_COMPUTE[]
Symbol name for the witness generation (and resp.
std::reference_wrapper< const Field > FieldRef
Typealias for a stable reference to a known Field.
constexpr char FUNC_NAME_CONSTRAIN[]
llvm::raw_ostream & toStream(OutputStream val)
void ensure(bool condition, const llvm::Twine &errMsg)
constexpr char FUNC_NAME_PRODUCT[]
FailureOr< SymbolRefAttr > getPathFromTopRoot(SymbolOpInterface to, ModuleOp *foundRoot)
std::optional< std::reference_wrapper< const Field > > tryDetectSpecifiedField(mlir::Operation *root)
Try to detect a uniquely used field from the enclosing LLZK module.