29#include <mlir/IR/BuiltinOps.h>
30#include <mlir/Pass/AnalysisManager.h>
31#include <mlir/Support/LLVM.h>
33#include <llvm/ADT/MapVector.h>
34#include <llvm/Support/SMTAPI.h>
51 : i(
Interval::Entire(f)), expr(exprRef) {}
54 : i(
Interval::Degenerate(f, singleVal)), expr(exprRef) {}
58 llvm::SMTExprRef
getExpr()
const {
return expr; }
147 llvm::SMTSolverRef solver, mlir::Operation *op,
const ExpressionValue &lhs,
162 void print(mlir::raw_ostream &os)
const;
177 llvm::SMTExprRef expr;
185 using AbstractLatticeValue::AbstractLatticeValue;
198 using ValueMap = mlir::DenseMap<mlir::Value, LatticeValue>;
201 using FieldMap = mlir::DenseMap<mlir::Value, mlir::DenseMap<mlir::StringAttr, LatticeValue>>;
207 using AbstractDenseLattice::AbstractDenseLattice;
209 mlir::ChangeResult
join(
const AbstractDenseLattice &other)
override;
211 mlir::ChangeResult
meet(
const AbstractDenseLattice &rhs)
override {
212 llvm::report_fatal_error(
"IntervalDataFlowAnalysis::meet : unsupported");
213 return mlir::ChangeResult::NoChange;
216 void print(mlir::raw_ostream &os)
const override;
218 mlir::FailureOr<LatticeValue>
getValue(mlir::Value v)
const;
219 mlir::FailureOr<LatticeValue>
getValue(mlir::Value v, mlir::StringAttr f)
const;
233 mlir::FailureOr<Interval>
findInterval(llvm::SMTExprRef expr)
const;
236 size_t size()
const {
return valMap.size(); }
240 ValueMap::iterator
begin() {
return valMap.begin(); }
241 ValueMap::iterator
end() {
return valMap.end(); }
242 ValueMap::const_iterator
begin()
const {
return valMap.begin(); }
243 ValueMap::const_iterator
end()
const {
return valMap.end(); }
261 using SymbolMap = mlir::DenseMap<ConstrainRef, llvm::SMTExprRef>;
265 mlir::DataFlowSolver &dataflowSolver, llvm::SMTSolverRef smt,
const Field &f,
266 bool propInputConstraints
268 : Base::DenseForwardDataFlowAnalysis(dataflowSolver), _dataflowSolver(dataflowSolver),
269 smtSolver(smt), field(f), propagateInputConstraints(propInputConstraints) {}
276 void visitOperation(mlir::Operation *op,
const Lattice &before, Lattice *after)
override;
285 mlir::DataFlowSolver &_dataflowSolver;
286 llvm::SMTSolverRef smtSolver;
287 SymbolMap refSymbols;
288 std::reference_wrapper<const Field> field;
289 bool propagateInputConstraints;
290 mlir::SymbolTableCollection tables;
292 void setToEntryState(Lattice *lattice)
override {
296 llvm::SMTExprRef createFeltSymbol(
const ConstrainRef &r)
const;
298 llvm::SMTExprRef createFeltSymbol(mlir::Value val)
const;
300 llvm::SMTExprRef createFeltSymbol(
const char *
name)
const;
302 bool isConstOp(mlir::Operation *op)
const {
304 felt::FeltConstantOp, mlir::arith::ConstantIndexOp, mlir::arith::ConstantIntOp>(op);
307 llvm::APSInt getConst(mlir::Operation *op)
const;
309 llvm::SMTExprRef createConstBitvectorExpr(llvm::APSInt v)
const {
310 return smtSolver->mkBitvector(v, field.get().bitWidth());
313 llvm::SMTExprRef createConstBoolExpr(
bool v)
const {
314 return smtSolver->mkBitvector(mlir::APSInt((
int)v), field.get().bitWidth());
317 bool isArithmeticOp(mlir::Operation *op)
const {
319 felt::AddFeltOp, felt::SubFeltOp, felt::MulFeltOp, felt::DivFeltOp, felt::ModFeltOp,
320 felt::NegFeltOp, felt::InvFeltOp, felt::AndFeltOp, felt::OrFeltOp, felt::XorFeltOp,
321 felt::NotFeltOp, felt::ShlFeltOp, felt::ShrFeltOp, boolean::CmpOp, boolean::AndBoolOp,
322 boolean::OrBoolOp, boolean::XorBoolOp, boolean::NotBoolOp>(op);
326 performBinaryArithmetic(mlir::Operation *op,
const LatticeValue &a,
const LatticeValue &b);
328 ExpressionValue performUnaryArithmetic(mlir::Operation *op,
const LatticeValue &a);
337 applyInterval(mlir::Operation *originalOp, Lattice *after, mlir::Value val, Interval newInterval);
340 mlir::FailureOr<std::pair<llvm::DenseSet<mlir::Value>, Interval>> getGeneralizedDecompInterval(
341 const ConstrainRefLattice *constrainRefLattice, mlir::Value lhs, mlir::Value rhs
344 bool isBoolOp(mlir::Operation *op)
const {
345 return mlir::isa<boolean::AndBoolOp, boolean::OrBoolOp, boolean::XorBoolOp, boolean::NotBoolOp>(
350 bool isConversionOp(mlir::Operation *op)
const {
351 return mlir::isa<cast::IntToFeltOp, cast::FeltToIndexOp>(op);
354 bool isApplyMapOp(mlir::Operation *op)
const {
return mlir::isa<polymorphic::ApplyMapOp>(op); }
356 bool isAssertOp(mlir::Operation *op)
const {
return mlir::isa<boolean::AssertOp>(op); }
358 bool isReadOp(mlir::Operation *op)
const {
359 return mlir::isa<component::FieldReadOp, polymorphic::ConstReadOp, array::ReadArrayOp>(op);
362 bool isWriteOp(mlir::Operation *op)
const {
363 return mlir::isa<component::FieldWriteOp, array::WriteArrayOp, array::InsertArrayOp>(op);
366 bool isArrayLengthOp(mlir::Operation *op)
const {
return mlir::isa<array::ArrayLengthOp>(op); }
368 bool isEmitOp(mlir::Operation *op)
const {
369 return mlir::isa<constrain::EmitEqualityOp, constrain::EmitContainmentOp>(op);
372 bool isCreateOp(mlir::Operation *op)
const {
373 return mlir::isa<component::CreateStructOp, array::CreateArrayOp>(op);
376 bool isExtractArrayOp(mlir::Operation *op)
const {
return mlir::isa<array::ExtractArrayOp>(op); }
378 bool isDefinitionOp(mlir::Operation *op)
const {
380 component::StructDefOp, function::FuncDefOp, component::FieldDefOp, global::GlobalDefOp,
384 bool isCallOp(mlir::Operation *op)
const {
return mlir::isa<function::CallOp>(op); }
386 bool isReturnOp(mlir::Operation *op)
const {
return mlir::isa<function::ReturnOp>(op); }
395 std::reference_wrapper<const Field>
field;
403class StructIntervals {
414 static mlir::FailureOr<StructIntervals>
compute(
418 StructIntervals si(mod, s);
420 return mlir::failure();
427 void print(mlir::raw_ostream &os,
bool withConstraints =
false,
bool printCompute =
false)
const;
430 return constrainFieldRanges;
434 return constrainSolverConstraints;
438 return computeFieldRanges;
442 return computeSolverConstraints;
445 friend mlir::raw_ostream &
operator<<(mlir::raw_ostream &os,
const StructIntervals &si) {
453 llvm::SMTSolverRef smtSolver;
455 llvm::MapVector<ConstrainRef, Interval> constrainFieldRanges, computeFieldRanges;
457 llvm::SetVector<ExpressionValue> constrainSolverConstraints, computeSolverConstraints;
475 if (mlir::failed(computeRes)) {
476 return mlir::failure();
479 return mlir::success();
486 :
public ModuleAnalysis<StructIntervals, IntervalAnalysisContext, StructIntervalAnalysis> {
498 ensure(field.has_value(),
"field not set, could not generate analysis context");
500 auto smtSolverRef = smtSolver;
501 bool prop = propagateInputConstraints;
503 std::move(smtSolverRef), field.value(), std::move(prop)
508 ensure(field.has_value(),
"field not set, could not generate analysis context");
510 .intervalDFA = intervalDFA,
511 .smtSolver = smtSolver,
512 .field = field.value(),
513 .propagateInputConstraints = propagateInputConstraints,
518 llvm::SMTSolverRef smtSolver;
520 std::optional<std::reference_wrapper<const Field>> field;
521 bool propagateInputConstraints;
528template <>
struct DenseMapInfo<
llzk::ExpressionValue> {
531 static auto emptyPtr =
reinterpret_cast<SMTExprRef
>(1);
535 static auto tombstonePtr =
reinterpret_cast<SMTExprRef
>(2);
This file defines helpers for manipulating APInts/APSInts for large numbers and operations over those...
Convenience classes for a frequent pattern of dataflow analysis used in LLZK, where an analysis is ru...
This file implements (LLZK-tailored) dense data-flow analysis using the data-flow analysis framework.
The dataflow analysis that computes the set of references that LLZK operations use and produce.
Defines a reference to a llzk object within a constrain function call.
Tracks a solver expression and an interval range for that expression.
friend ExpressionValue neg(llvm::SMTSolverRef solver, const ExpressionValue &val)
friend ExpressionValue notOp(llvm::SMTSolverRef solver, const ExpressionValue &val)
const Interval & getInterval() const
friend ExpressionValue cmp(llvm::SMTSolverRef solver, boolean::CmpOp op, const ExpressionValue &lhs, const ExpressionValue &rhs)
ExpressionValue(const Field &f, llvm::SMTExprRef exprRef)
friend ExpressionValue sub(llvm::SMTSolverRef solver, const ExpressionValue &lhs, const ExpressionValue &rhs)
friend ExpressionValue mul(llvm::SMTSolverRef solver, const ExpressionValue &lhs, const ExpressionValue &rhs)
friend ExpressionValue fallbackUnaryOp(llvm::SMTSolverRef solver, mlir::Operation *op, const ExpressionValue &val)
friend ExpressionValue boolOr(llvm::SMTSolverRef solver, const ExpressionValue &lhs, const ExpressionValue &rhs)
friend ExpressionValue div(llvm::SMTSolverRef solver, felt::DivFeltOp op, const ExpressionValue &lhs, const ExpressionValue &rhs)
ExpressionValue withInterval(const Interval &newInterval) const
Return the current expression with a new interval.
void print(mlir::raw_ostream &os) const
bool operator==(const ExpressionValue &rhs) const
friend ExpressionValue add(llvm::SMTSolverRef solver, const ExpressionValue &lhs, const ExpressionValue &rhs)
friend ExpressionValue shiftRight(llvm::SMTSolverRef solver, const ExpressionValue &lhs, const ExpressionValue &rhs)
llvm::SMTExprRef getExpr() const
friend ExpressionValue fallbackBinaryOp(llvm::SMTSolverRef solver, mlir::Operation *op, const ExpressionValue &lhs, const ExpressionValue &rhs)
Computes a solver expression based on the operation, but computes a fallback interval (which is just ...
friend ExpressionValue bitAnd(llvm::SMTSolverRef solver, const ExpressionValue &lhs, const ExpressionValue &rhs)
ExpressionValue & join(const ExpressionValue &rhs)
Fold two expressions together when overapproximating array elements.
ExpressionValue(llvm::SMTExprRef exprRef, Interval interval)
friend ExpressionValue boolXor(llvm::SMTSolverRef solver, const ExpressionValue &lhs, const ExpressionValue &rhs)
ExpressionValue(const Field &f, llvm::SMTExprRef exprRef, llvm::APSInt singleVal)
friend ExpressionValue join(llvm::SMTSolverRef solver, const ExpressionValue &lhs, const ExpressionValue &rhs)
Compute the union of the lhs and rhs intervals, and create a solver expression that constrains both s...
friend mlir::raw_ostream & operator<<(mlir::raw_ostream &os, const ExpressionValue &e)
friend ExpressionValue boolAnd(llvm::SMTSolverRef solver, const ExpressionValue &lhs, const ExpressionValue &rhs)
const Field & getField() const
friend ExpressionValue mod(llvm::SMTSolverRef solver, const ExpressionValue &lhs, const ExpressionValue &rhs)
friend ExpressionValue shiftLeft(llvm::SMTSolverRef solver, const ExpressionValue &lhs, const ExpressionValue &rhs)
friend ExpressionValue intersection(llvm::SMTSolverRef solver, const ExpressionValue &lhs, const ExpressionValue &rhs)
Compute the intersection of the lhs and rhs intervals, and create a solver expression that constrains...
friend ExpressionValue boolNot(llvm::SMTSolverRef solver, const ExpressionValue &val)
Information about the prime finite field used for the interval analysis.
static const Field & getField(const char *fieldName)
Get a Field from a given field name string.
Maps mlir::Values to LatticeValues.
friend mlir::raw_ostream & operator<<(mlir::raw_ostream &os, const IntervalAnalysisLattice &l)
const ValueMap & getMap() const
mlir::DenseMap< mlir::Value, mlir::DenseMap< mlir::StringAttr, LatticeValue > > FieldMap
llvm::SetVector< ExpressionValue > ConstraintSet
mlir::ChangeResult setInterval(llvm::SMTExprRef expr, Interval i)
IntervalAnalysisLatticeValue LatticeValue
ValueMap::iterator begin()
mlir::ChangeResult setValue(mlir::Value v, mlir::StringAttr f, ExpressionValue e)
mlir::DenseMap< mlir::Value, LatticeValue > ValueMap
mlir::ChangeResult addSolverConstraint(ExpressionValue e)
mlir::ChangeResult setValue(mlir::Value v, ExpressionValue e)
const ConstraintSet & getConstraints() const
void print(mlir::raw_ostream &os) const override
ValueMap::const_iterator end() const
mlir::DenseMap< llvm::SMTExprRef, Interval > ExpressionIntervals
mlir::ChangeResult join(const AbstractDenseLattice &other) override
mlir::FailureOr< LatticeValue > getValue(mlir::Value v) const
ValueMap::const_iterator begin() const
mlir::FailureOr< Interval > findInterval(llvm::SMTExprRef expr) const
mlir::ChangeResult meet(const AbstractDenseLattice &rhs) override
mlir::FailureOr< LatticeValue > getValue(mlir::Value v, mlir::StringAttr f) const
void visitOperation(mlir::Operation *op, const Lattice &before, Lattice *after) override
Visit an operation with the dense lattice before its execution.
void visitCallControlFlowTransfer(mlir::CallOpInterface call, dataflow::CallControlFlowAction action, const Lattice &before, Lattice *after) override
The interval analysis is intraprocedural only for now, so this control flow transfer function passes ...
llvm::SMTExprRef getOrCreateSymbol(const ConstrainRef &r)
Either return the existing SMT expression that corresponds to the ConstrainRef, or create one.
IntervalDataFlowAnalysis(mlir::DataFlowSolver &dataflowSolver, llvm::SMTSolverRef smt, const Field &f, bool propInputConstraints)
Intervals over a finite field.
mlir::DataFlowSolver solver
ModuleAnalysis(mlir::Operation *op)
ModuleIntervalAnalysis(mlir::Operation *op)
virtual ~ModuleIntervalAnalysis()=default
IntervalAnalysisContext getContext() override
Create and return a valid Context object.
void setPropagateInputConstraints(bool prop)
void initializeSolver() override
Initialize the shared dataflow solver with any common analyses required by the contained struct analy...
void setField(const Field &f)
StructAnalysis(mlir::Operation *op)
Assert that this analysis is being run on a StructDefOp and initializes the analysis with the current...
component::StructDefOp getStruct() const
mlir::ModuleOp getModule() const
void setResult(StructIntervals &&r)
mlir::LogicalResult runAnalysis(mlir::DataFlowSolver &solver, mlir::AnalysisManager &_, IntervalAnalysisContext &ctx) override
Perform the analysis and construct the Result output.
StructAnalysis(mlir::Operation *op)
Assert that this analysis is being run on a StructDefOp and initializes the analysis with the current...
virtual ~StructIntervalAnalysis()=default
const llvm::SetVector< ExpressionValue > getConstrainSolverConstraints() const
const llvm::SetVector< ExpressionValue > getComputeSolverConstraints() const
mlir::LogicalResult computeIntervals(mlir::DataFlowSolver &solver, IntervalAnalysisContext &ctx)
void print(mlir::raw_ostream &os, bool withConstraints=false, bool printCompute=false) const
const llvm::MapVector< ConstrainRef, Interval > & getConstrainIntervals() const
static mlir::FailureOr< StructIntervals > compute(mlir::ModuleOp mod, component::StructDefOp s, mlir::DataFlowSolver &solver, IntervalAnalysisContext &ctx)
Compute the struct intervals.
friend mlir::raw_ostream & operator<<(mlir::raw_ostream &os, const StructIntervals &si)
const llvm::MapVector< ConstrainRef, Interval > & getComputeIntervals() const
LLZK: This class has been ported so that it can inherit from our port of the AbstractDenseForwardData...
mlir::dataflow::AbstractDenseLattice AbstractDenseLattice
mlir::dataflow::CallControlFlowAction CallControlFlowAction
void ensure(bool condition, llvm::Twine errMsg)
ExpressionValue mod(llvm::SMTSolverRef solver, const ExpressionValue &lhs, const ExpressionValue &rhs)
static unsigned getHashValue(const llzk::ExpressionValue &e)
static SMTExprRef getTombstoneExpr()
static bool isEqual(const llzk::ExpressionValue &lhs, const llzk::ExpressionValue &rhs)
static llzk::ExpressionValue getTombstoneKey()
static llzk::ExpressionValue getEmptyKey()
static SMTExprRef getEmptyExpr()
unsigned operator()(const ExpressionValue &e) const
Parameters and shared objects to pass to child analyses.
std::reference_wrapper< const Field > field
bool doInputConstraintPropagation() const
const Field & getField() const
IntervalDataFlowAnalysis * intervalDFA
llvm::SMTExprRef getSymbol(const ConstrainRef &r)
bool propagateInputConstraints
llvm::SMTSolverRef smtSolver