LLZK 0.1.0
Veridise's ZK Language IR
Loading...
Searching...
No Matches
ConstrainRefLattice.h
Go to the documentation of this file.
1//===-- ConstrainRefLattice.h -----------------------------------*- C++ -*-===//
2//
3// Part of the LLZK Project, under the Apache License v2.0.
4// See LICENSE.txt for license information.
5// Copyright 2025 Veridise Inc.
6// SPDX-License-Identifier: Apache-2.0
7//
8//===----------------------------------------------------------------------===//
9
10#pragma once
11
16
17namespace llzk {
18
21 std::unordered_map<ConstrainRef, ConstrainRefLatticeValue, ConstrainRef::Hash>;
22
25 : public dataflow::AbstractLatticeValue<ConstrainRefLatticeValue, ConstrainRefSet> {
28 using ScalarTy = ConstrainRefSet;
36 using ArrayTy = std::vector<std::unique_ptr<ConstrainRefLatticeValue>>;
37
38public:
39 explicit ConstrainRefLatticeValue(ScalarTy s) : Base(s) {}
40 explicit ConstrainRefLatticeValue(ConstrainRef r) : Base(ScalarTy {r}) {}
41 ConstrainRefLatticeValue() : Base(ScalarTy {}) {}
42
43 // Create an empty array of the given shape.
44 explicit ConstrainRefLatticeValue(mlir::ArrayRef<int64_t> shape) : Base(shape) {}
45
47 ensure(isSingleValue(), "not a single value");
48 return *getScalarValue().begin();
49 }
50
54 mlir::ChangeResult insert(const ConstrainRef &rhs);
55
58 std::pair<ConstrainRefLatticeValue, mlir::ChangeResult>
59 translate(const TranslationMap &translation) const;
60
67 std::pair<ConstrainRefLatticeValue, mlir::ChangeResult>
69
72 std::pair<ConstrainRefLatticeValue, mlir::ChangeResult>
73 extract(const std::vector<ConstrainRefIndex> &indices) const;
74
75protected:
78 mlir::ChangeResult translateScalar(const TranslationMap &translation);
79
82 virtual std::pair<ConstrainRefLatticeValue, mlir::ChangeResult>
83 elementwiseTransform(llvm::function_ref<ConstrainRef(const ConstrainRef &)> transform) const;
84};
85
88public:
89 using ValueMap = mlir::DenseMap<mlir::Value, ConstrainRefLatticeValue>;
90 using AbstractDenseLattice::AbstractDenseLattice;
91
92 /* Static utilities */
93
98 static mlir::FailureOr<ConstrainRef> getSourceRef(mlir::Value val);
99
100 /* Required methods */
101
103 mlir::ChangeResult join(const AbstractDenseLattice &rhs) override {
104 if (auto *r = dynamic_cast<const ConstrainRefLattice *>(&rhs)) {
105 return setValues(r->valMap);
106 }
107 llvm::report_fatal_error("invalid join lattice type");
108 return mlir::ChangeResult::NoChange;
109 }
110
112 virtual mlir::ChangeResult meet(const AbstractDenseLattice &rhs) override {
113 llvm::report_fatal_error("meet operation is not supported for ConstrainRefLattice");
114 return mlir::ChangeResult::NoChange;
115 }
116
117 void print(mlir::raw_ostream &os) const override;
118
119 /* Update utility methods */
120
121 mlir::ChangeResult setValues(const ValueMap &rhs);
122
123 mlir::ChangeResult setValue(mlir::Value v, const ConstrainRefLatticeValue &rhs) {
124 return valMap[v].setValue(rhs);
125 }
126
127 mlir::ChangeResult setValue(mlir::Value v, const ConstrainRef &ref) {
128 return valMap[v].setValue(ConstrainRefLatticeValue(ref));
129 }
130
131 ConstrainRefLatticeValue getOrDefault(mlir::Value v) const;
132
134
135 size_t size() const { return valMap.size(); }
136
137 const ValueMap &getMap() const { return valMap; }
138
139 ValueMap::iterator begin() { return valMap.begin(); }
140 ValueMap::iterator end() { return valMap.end(); }
141 ValueMap::const_iterator begin() const { return valMap.begin(); }
142 ValueMap::const_iterator end() const { return valMap.end(); }
143
144 friend mlir::raw_ostream &operator<<(mlir::raw_ostream &os, const ConstrainRefLattice &v);
145
146private:
147 ValueMap valMap;
148};
149
150} // namespace llzk
This file implements (LLZK-tailored) dense data-flow analysis using the data-flow analysis framework.
A value at a given point of the ConstrainRefLattice.
mlir::ChangeResult insert(const ConstrainRef &rhs)
Directly insert the ref into this value.
std::pair< ConstrainRefLatticeValue, mlir::ChangeResult > extract(const std::vector< ConstrainRefIndex > &indices) const
Perform an array.extract or array.read operation, depending on how many indices are provided.
mlir::ChangeResult translateScalar(const TranslationMap &translation)
Translate this value using the translation map, assuming this value is a scalar.
const ConstrainRef & getSingleValue() const
std::pair< ConstrainRefLatticeValue, mlir::ChangeResult > referenceField(SymbolLookupResult< component::FieldDefOp > fieldRef) const
Add the given fieldRef to the constrain refs contained within this value.
std::pair< ConstrainRefLatticeValue, mlir::ChangeResult > translate(const TranslationMap &translation) const
For the refs contained in this value, translate them given the translation map and return the transfo...
ConstrainRefLatticeValue(mlir::ArrayRef< int64_t > shape)
virtual std::pair< ConstrainRefLatticeValue, mlir::ChangeResult > elementwiseTransform(llvm::function_ref< ConstrainRef(const ConstrainRef &)> transform) const
Perform a recursive transformation over all elements of this value and return a new value with the mo...
A lattice for use in dense analysis.
mlir::ChangeResult join(const AbstractDenseLattice &rhs) override
Maximum upper bound.
virtual mlir::ChangeResult meet(const AbstractDenseLattice &rhs) override
Minimum lower bound.
ValueMap::const_iterator begin() const
mlir::ChangeResult setValue(mlir::Value v, const ConstrainRef &ref)
ConstrainRefLatticeValue getOrDefault(mlir::Value v) const
ConstrainRefLatticeValue getReturnValue(unsigned i) const
mlir::DenseMap< mlir::Value, ConstrainRefLatticeValue > ValueMap
static mlir::FailureOr< ConstrainRef > getSourceRef(mlir::Value val)
If val is the source of other values (i.e., a block argument from the function args or a constant),...
mlir::ChangeResult setValues(const ValueMap &rhs)
ValueMap::const_iterator end() const
void print(mlir::raw_ostream &os) const override
friend mlir::raw_ostream & operator<<(mlir::raw_ostream &os, const ConstrainRefLattice &v)
mlir::ChangeResult setValue(mlir::Value v, const ConstrainRefLatticeValue &rhs)
const ValueMap & getMap() const
Defines a reference to a llzk object within a constrain function call.
mlir::dataflow::AbstractDenseLattice AbstractDenseLattice
void ensure(bool condition, llvm::Twine errMsg)
Definition ErrorHelper.h:32
std::unordered_map< ConstrainRef, ConstrainRefLatticeValue, ConstrainRef::Hash > TranslationMap