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
17#include <llvm/ADT/PointerUnion.h>
18
19namespace llzk {
20
23 std::unordered_map<ConstrainRef, ConstrainRefLatticeValue, ConstrainRef::Hash>;
24
27 : public dataflow::AbstractLatticeValue<ConstrainRefLatticeValue, ConstrainRefSet> {
30 using ScalarTy = ConstrainRefSet;
38 using ArrayTy = std::vector<std::unique_ptr<ConstrainRefLatticeValue>>;
39
40public:
41 explicit ConstrainRefLatticeValue(ScalarTy s) : Base(s) {}
42 explicit ConstrainRefLatticeValue(ConstrainRef r) : Base(ScalarTy {r}) {}
43 ConstrainRefLatticeValue() : Base(ScalarTy {}) {}
44 virtual ~ConstrainRefLatticeValue() = default;
45
46 // Create an empty array of the given shape.
47 explicit ConstrainRefLatticeValue(mlir::ArrayRef<int64_t> shape) : Base(shape) {}
48
50 ensure(isSingleValue(), "not a single value");
51 return *getScalarValue().begin();
52 }
53
57 mlir::ChangeResult insert(const ConstrainRef &rhs);
58
61 std::pair<ConstrainRefLatticeValue, mlir::ChangeResult>
62 translate(const TranslationMap &translation) const;
63
70 std::pair<ConstrainRefLatticeValue, mlir::ChangeResult>
72
75 std::pair<ConstrainRefLatticeValue, mlir::ChangeResult>
76 extract(const std::vector<ConstrainRefIndex> &indices) const;
77
78protected:
81 mlir::ChangeResult translateScalar(const TranslationMap &translation);
82
85 virtual std::pair<ConstrainRefLatticeValue, mlir::ChangeResult>
86 elementwiseTransform(llvm::function_ref<ConstrainRef(const ConstrainRef &)> transform) const;
87};
88
91public:
92 // mlir::Value is used for read-like operations that create references in their results,
93 // mlir::Operation* is used for write-like operations that reference values as their destinations
94 using ValueTy = llvm::PointerUnion<mlir::Value, mlir::Operation *>;
95 using ValueMap = mlir::DenseMap<ValueTy, ConstrainRefLatticeValue>;
96 // Used to lookup MLIR values/operations from a given ConstrainRef (all values that a ref is
97 // referenced by)
98 using ValueSet = mlir::DenseSet<ValueTy>;
99 using Ref2Val = mlir::DenseMap<ConstrainRef, mlir::DenseSet<ValueTy>>;
100 using AbstractDenseLattice::AbstractDenseLattice;
101
102 /* Static utilities */
103
108 static mlir::FailureOr<ConstrainRef> getSourceRef(mlir::Value val);
109
110 /* Required methods */
111
113 mlir::ChangeResult join(const AbstractDenseLattice &rhs) override {
114 if (auto *r = dynamic_cast<const ConstrainRefLattice *>(&rhs)) {
115 return setValues(r->valMap);
116 }
117 llvm::report_fatal_error("invalid join lattice type");
118 return mlir::ChangeResult::NoChange;
119 }
120
122 virtual mlir::ChangeResult meet(const AbstractDenseLattice &rhs) override {
123 llvm::report_fatal_error("meet operation is not supported for ConstrainRefLattice");
124 return mlir::ChangeResult::NoChange;
125 }
126
127 void print(mlir::raw_ostream &os) const override;
128
129 /* Update utility methods */
130
131 mlir::ChangeResult setValues(const ValueMap &rhs);
132
133 mlir::ChangeResult setValue(ValueTy v, const ConstrainRefLatticeValue &rhs);
134
135 mlir::ChangeResult setValue(ValueTy v, const ConstrainRef &ref);
136
138
140
141 ValueSet lookupValues(const ConstrainRef &r) const;
142
143 size_t size() const { return valMap.size(); }
144
145 const ValueMap &getMap() const { return valMap; }
146
147 const Ref2Val &getRef2Val() const { return refMap; }
148
149 ValueMap::iterator begin() { return valMap.begin(); }
150 ValueMap::iterator end() { return valMap.end(); }
151 ValueMap::const_iterator begin() const { return valMap.begin(); }
152 ValueMap::const_iterator end() const { return valMap.end(); }
153
154 friend mlir::raw_ostream &operator<<(mlir::raw_ostream &os, const ConstrainRefLattice &v);
155
156private:
157 ValueMap valMap;
158 Ref2Val refMap;
159};
160
161} // namespace llzk
162
163namespace llvm {
164class raw_ostream;
165
166raw_ostream &operator<<(raw_ostream &os, llvm::PointerUnion<mlir::Value, mlir::Operation *> ptr);
167} // namespace llvm
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.
virtual ~ConstrainRefLatticeValue()=default
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::DenseMap< ValueTy, ConstrainRefLatticeValue > ValueMap
mlir::ChangeResult join(const AbstractDenseLattice &rhs) override
Maximum upper bound.
const Ref2Val & getRef2Val() const
virtual mlir::ChangeResult meet(const AbstractDenseLattice &rhs) override
Minimum lower bound.
ValueMap::const_iterator begin() const
ValueSet lookupValues(const ConstrainRef &r) const
ConstrainRefLatticeValue getReturnValue(unsigned i) const
mlir::DenseSet< ValueTy > ValueSet
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(ValueTy v, const ConstrainRefLatticeValue &rhs)
ConstrainRefLatticeValue getOrDefault(ValueTy v) const
const ValueMap & getMap() const
llvm::PointerUnion< mlir::Value, mlir::Operation * > ValueTy
mlir::DenseMap< ConstrainRef, mlir::DenseSet< ValueTy > > Ref2Val
Defines a reference to a llzk object within a constrain function call.
raw_ostream & operator<<(raw_ostream &os, llvm::PointerUnion< mlir::Value, mlir::Operation * > ptr)
mlir::dataflow::AbstractDenseLattice AbstractDenseLattice
void ensure(bool condition, llvm::Twine errMsg)
Definition ErrorHelper.h:35
std::unordered_map< ConstrainRef, ConstrainRefLatticeValue, ConstrainRef::Hash > TranslationMap