29#include <mlir/IR/BuiltinOps.h>
30#include <mlir/Pass/AnalysisManager.h>
31#include <mlir/Support/LLVM.h>
33#include <llvm/ADT/DynamicAPInt.h>
34#include <llvm/ADT/MapVector.h>
35#include <llvm/Support/SMTAPI.h>
54 : i(
Interval::Entire(f)), expr(exprRef) {}
57 : i(
Interval::Degenerate(f, singleVal)), expr(exprRef) {}
60 : i(interval), expr(exprRef) {}
62 llvm::SMTExprRef
getExpr()
const {
return expr; }
90 return solver->getBoolSort() == solver->getSort(expr);
160 llvm::SMTSolverRef solver, mlir::Operation *op,
const ExpressionValue &lhs,
175 void print(mlir::raw_ostream &os)
const;
190 llvm::SMTExprRef expr;
198 using AbstractLatticeValue::AbstractLatticeValue;
209 using ValueMap = mlir::DenseMap<mlir::Value, LatticeValue>;
212 using MemberMap = mlir::DenseMap<mlir::Value, mlir::DenseMap<mlir::StringAttr, LatticeValue>>;
218 using AbstractSparseLattice::AbstractSparseLattice;
220 mlir::ChangeResult
join(
const AbstractSparseLattice &other)
override;
222 mlir::ChangeResult
meet(
const AbstractSparseLattice &other)
override;
224 void print(mlir::raw_ostream &os)
const override;
257 using SymbolMap = mlir::DenseMap<SourceRef, llvm::SMTExprRef>;
261 mlir::DataFlowSolver &dataflowSolver, llvm::SMTSolverRef smt,
const Field &f,
262 bool propInputConstraints
265 smtSolver(smt), field(f), propagateInputConstraints(propInputConstraints) {}
268 mlir::Operation *op, mlir::ArrayRef<const Lattice *> operands,
269 mlir::ArrayRef<Lattice *> results
279 return memberReadResults;
283 return memberWriteResults;
287 mlir::DataFlowSolver &_dataflowSolver;
288 llvm::SMTSolverRef smtSolver;
289 SymbolMap refSymbols;
290 std::reference_wrapper<const Field> field;
291 bool propagateInputConstraints;
292 mlir::SymbolTableCollection
tables;
295 llvm::DenseMap<SourceRef, llvm::DenseSet<Lattice *>> memberReadResults;
297 llvm::DenseMap<SourceRef, ExpressionValue> memberWriteResults;
299 void setToEntryState(Lattice *lattice)
override {
304 llvm::SMTExprRef createFeltSymbol(
const SourceRef &r)
const;
306 llvm::SMTExprRef createFeltSymbol(mlir::Value val)
const;
308 llvm::SMTExprRef createFeltSymbol(
const char *name)
const;
310 inline bool isConstOp(mlir::Operation *op)
const {
312 felt::FeltConstantOp, mlir::arith::ConstantIndexOp, mlir::arith::ConstantIntOp>(op);
315 inline bool isBoolConstOp(mlir::Operation *op)
const {
316 if (
auto constIntOp = llvm::dyn_cast<mlir::arith::ConstantIntOp>(op)) {
317 auto valAttr = dyn_cast<mlir::IntegerAttr>(constIntOp.getValue());
318 ensure(valAttr !=
nullptr,
"arith::ConstantIntOp must have an IntegerAttr as its value");
319 return valAttr.getValue().getBitWidth() == 1;
324 llvm::DynamicAPInt getConst(mlir::Operation *op)
const;
326 inline llvm::SMTExprRef createConstBitvectorExpr(
const llvm::DynamicAPInt &v)
const {
327 return createConstBitvectorExpr(
toAPSInt(v));
330 inline llvm::SMTExprRef createConstBitvectorExpr(
const llvm::APSInt &v)
const {
331 return smtSolver->mkBitvector(v, field.get().bitWidth());
334 llvm::SMTExprRef createConstBoolExpr(
bool v)
const {
return smtSolver->mkBoolean(v); }
336 bool isArithmeticOp(mlir::Operation *op)
const {
338 felt::AddFeltOp, felt::SubFeltOp, felt::MulFeltOp, felt::DivFeltOp, felt::UnsignedModFeltOp,
339 felt::SignedModFeltOp, felt::SignedIntDivFeltOp, felt::UnsignedIntDivFeltOp,
340 felt::NegFeltOp, felt::InvFeltOp, felt::AndFeltOp, felt::OrFeltOp, felt::XorFeltOp,
341 felt::NotFeltOp, felt::ShlFeltOp, felt::ShrFeltOp, boolean::CmpOp, boolean::AndBoolOp,
342 boolean::OrBoolOp, boolean::XorBoolOp, boolean::NotBoolOp>(op);
346 performBinaryArithmetic(mlir::Operation *op,
const LatticeValue &a,
const LatticeValue &b);
348 ExpressionValue performUnaryArithmetic(mlir::Operation *op,
const LatticeValue &a);
356 void applyInterval(mlir::Operation *originalOp, mlir::Value val, Interval newInterval);
359 mlir::FailureOr<std::pair<llvm::DenseSet<mlir::Value>, Interval>>
360 getGeneralizedDecompInterval(mlir::Operation *baseOp, mlir::Value lhs, mlir::Value rhs);
362 bool isReadOp(mlir::Operation *op)
const {
363 return llvm::isa<component::MemberReadOp, polymorphic::ConstReadOp, array::ReadArrayOp>(op);
366 bool isDefinitionOp(mlir::Operation *op)
const {
368 component::StructDefOp, function::FuncDefOp, component::MemberDefOp, global::GlobalDefOp,
372 bool isReturnOp(mlir::Operation *op)
const {
return llvm::isa<function::ReturnOp>(op); }
376 const SourceRefLattice *getSourceRefLattice(mlir::Operation *baseOp, mlir::Value val);
385 std::optional<std::reference_wrapper<const Field>>
field;
391 ensure(
field.has_value(),
"field not set within context");
402template <>
struct std::hash<
llzk::IntervalAnalysisContext> {
404 return llvm::hash_combine(
405 std::hash<const llzk::IntervalDataFlowAnalysis *> {}(c.
intervalDFA),
406 std::hash<const llvm::SMTSolver *> {}(c.
smtSolver.get()),
407 std::hash<const llzk::Field *> {}(&c.
getField()),
415class StructIntervals {
426 static mlir::FailureOr<StructIntervals>
compute(
430 StructIntervals si(mod, s);
432 return mlir::failure();
440 void print(mlir::raw_ostream &os,
bool withConstraints =
false,
bool printCompute =
false)
const;
443 return constrainMemberRanges;
447 return constrainSolverConstraints;
451 return computeMemberRanges;
455 return computeSolverConstraints;
458 friend mlir::raw_ostream &
operator<<(mlir::raw_ostream &os,
const StructIntervals &si) {
466 llvm::SMTSolverRef smtSolver;
468 llvm::MapVector<SourceRef, Interval> constrainMemberRanges, computeMemberRanges;
470 llvm::SetVector<ExpressionValue> constrainSolverConstraints, computeSolverConstraints;
488 if (mlir::failed(computeRes)) {
489 return mlir::failure();
492 return mlir::success();
499 :
public ModuleAnalysis<StructIntervals, IntervalAnalysisContext, StructIntervalAnalysis> {
503 ctx.smtSolver = llvm::CreateZ3Solver();
512 ensure(ctx.hasField(),
"field not set, could not generate analysis context");
514 auto smtSolverRef = ctx.smtSolver;
515 bool prop = ctx.propagateInputConstraints;
518 std::move(smtSolverRef), ctx.getField(), std::move(prop)
523 ensure(ctx.field.has_value(),
"field not set, could not generate analysis context");
535template <>
struct DenseMapInfo<
llzk::ExpressionValue> {
538 static auto emptyPtr =
reinterpret_cast<SMTExprRef
>(1);
542 static auto tombstonePtr =
reinterpret_cast<SMTExprRef
>(2);
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.
This file implements sparse data-flow analysis using the data-flow analysis framework.
Tracks a solver expression and an interval range for that expression.
ExpressionValue(llvm::SMTExprRef exprRef, const Interval &interval)
ExpressionValue withExpression(const llvm::SMTExprRef &newExpr) const
Return the current expression with a new SMT 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)
ExpressionValue(const Field &f)
friend ExpressionValue mul(llvm::SMTSolverRef solver, const ExpressionValue &lhs, const ExpressionValue &rhs)
bool isBoolSort(llvm::SMTSolverRef solver) const
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 ...
ExpressionValue(const Field &f, llvm::SMTExprRef exprRef, const llvm::DynamicAPInt &singleVal)
friend ExpressionValue bitAnd(llvm::SMTSolverRef solver, const ExpressionValue &lhs, const ExpressionValue &rhs)
friend ExpressionValue boolXor(llvm::SMTSolverRef solver, const ExpressionValue &lhs, const ExpressionValue &rhs)
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)
ExpressionValue & join(const ExpressionValue &)
Fold two expressions together when overapproximating array elements.
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.
friend mlir::raw_ostream & operator<<(mlir::raw_ostream &os, const IntervalAnalysisLattice &l)
const LatticeValue & getValue() const
llvm::SetVector< ExpressionValue > ConstraintSet
mlir::ChangeResult setValue(const LatticeValue &val)
IntervalAnalysisLatticeValue LatticeValue
mlir::DenseMap< mlir::Value, LatticeValue > ValueMap
mlir::ChangeResult meet(const AbstractSparseLattice &other) override
mlir::ChangeResult addSolverConstraint(ExpressionValue e)
const ConstraintSet & getConstraints() const
mlir::DenseMap< mlir::Value, mlir::DenseMap< mlir::StringAttr, LatticeValue > > MemberMap
void print(mlir::raw_ostream &os) const override
mlir::ChangeResult join(const AbstractSparseLattice &other) override
mlir::ChangeResult setInterval(llvm::SMTExprRef expr, const Interval &i)
mlir::DenseMap< llvm::SMTExprRef, Interval > ExpressionIntervals
mlir::FailureOr< Interval > findInterval(llvm::SMTExprRef expr) const
mlir::LogicalResult visitOperation(mlir::Operation *op, mlir::ArrayRef< const Lattice * > operands, mlir::ArrayRef< Lattice * > results) override
Visit an operation with the lattices of its operands.
llvm::SMTExprRef getOrCreateSymbol(const SourceRef &r)
Either return the existing SMT expression that corresponds to the SourceRef, or create one.
const llvm::DenseMap< SourceRef, llvm::DenseSet< Lattice * > > & getMemberReadResults() const
const llvm::DenseMap< SourceRef, ExpressionValue > & getMemberWriteResults() const
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
void setPropagateInputConstraints(bool prop)
void initializeSolver() override
Initialize the shared dataflow solver with any common analyses required by the contained struct analy...
const IntervalAnalysisContext & getContext() const override
Return the current Context object.
void setField(const Field &f)
The dataflow analysis that computes the set of references that LLZK operations use and produce.
A reference to a "source", which is the base value from which other SSA values are derived.
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
void setResult(const IntervalAnalysisContext &ctx, StructIntervals &&r)
mlir::ModuleOp getModule() const
StructAnalysis(mlir::Operation *op)
Assert that this analysis is being run on a StructDefOp and initializes the analysis with the current...
virtual ~StructIntervalAnalysis()=default
mlir::LogicalResult runAnalysis(mlir::DataFlowSolver &solver, mlir::AnalysisManager &, const IntervalAnalysisContext &ctx) override
Perform the analysis and construct the Result output.
const llvm::MapVector< SourceRef, Interval > & getConstrainIntervals() const
const llvm::SetVector< ExpressionValue > getConstrainSolverConstraints() const
const llvm::SetVector< ExpressionValue > getComputeSolverConstraints() const
const llvm::MapVector< SourceRef, Interval > & getComputeIntervals() const
void print(mlir::raw_ostream &os, bool withConstraints=false, bool printCompute=false) const
static mlir::FailureOr< StructIntervals > compute(mlir::ModuleOp mod, component::StructDefOp s, mlir::DataFlowSolver &solver, const IntervalAnalysisContext &ctx)
Compute the struct intervals.
friend mlir::raw_ostream & operator<<(mlir::raw_ostream &os, const StructIntervals &si)
mlir::LogicalResult computeIntervals(mlir::DataFlowSolver &solver, const IntervalAnalysisContext &ctx)
mlir::SymbolTableCollection tables
LLZK: Added for use of symbol helper caching.
A sparse forward data-flow analysis for propagating SSA value lattices across the IR by implementing ...
SparseForwardDataFlowAnalysis(mlir::DataFlowSolver &s)
mlir::dataflow::AbstractSparseLattice AbstractSparseLattice
void ensure(bool condition, const llvm::Twine &errMsg)
APSInt toAPSInt(const DynamicAPInt &i)
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.
bool doInputConstraintPropagation() const
const Field & getField() const
friend bool operator==(const IntervalAnalysisContext &a, const IntervalAnalysisContext &b)=default
std::optional< std::reference_wrapper< const Field > > field
IntervalDataFlowAnalysis * intervalDFA
bool propagateInputConstraints
llvm::SMTSolverRef smtSolver
llvm::SMTExprRef getSymbol(const SourceRef &r) const
size_t operator()(const llzk::IntervalAnalysisContext &c) const