LLZK 0.1.0
Veridise's ZK Language IR
Loading...
Searching...
No Matches
SourceRefLattice.h
Go to the documentation of this file.
1//===-- SourceRefLattice.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
22using TranslationMap = std::unordered_map<SourceRef, SourceRefLatticeValue, SourceRef::Hash>;
23
26 : public dataflow::AbstractLatticeValue<SourceRefLatticeValue, SourceRefSet> {
29 using ScalarTy = SourceRefSet;
37 using ArrayTy = std::vector<std::unique_ptr<SourceRefLatticeValue>>;
38
39public:
40 explicit SourceRefLatticeValue(ScalarTy s) : Base(s) {}
41 explicit SourceRefLatticeValue(SourceRef r) : Base(ScalarTy {r}) {}
42 SourceRefLatticeValue() : Base(ScalarTy {}) {}
43 virtual ~SourceRefLatticeValue() = default;
44
45 // Create an empty array of the given shape.
46 explicit SourceRefLatticeValue(mlir::ArrayRef<int64_t> shape) : Base(shape) {}
47
48 const SourceRef &getSingleValue() const {
49 ensure(isSingleValue(), "not a single value");
50 return *getScalarValue().begin();
51 }
52
56 mlir::ChangeResult insert(const SourceRef &rhs);
57
60 std::pair<SourceRefLatticeValue, mlir::ChangeResult>
61 translate(const TranslationMap &translation) const;
62
69 std::pair<SourceRefLatticeValue, mlir::ChangeResult>
71
74 std::pair<SourceRefLatticeValue, mlir::ChangeResult>
75 extract(const std::vector<SourceRefIndex> &indices) const;
76
77protected:
80 mlir::ChangeResult translateScalar(const TranslationMap &translation);
81
84 virtual std::pair<SourceRefLatticeValue, mlir::ChangeResult>
85 elementwiseTransform(llvm::function_ref<SourceRef(const SourceRef &)> transform) const;
86};
87
90public:
91 // mlir::Value is used for read-like operations that create references in their results,
92 // mlir::Operation* is used for write-like operations that reference values as their destinations
93 using ValueTy = llvm::PointerUnion<mlir::Value, mlir::Operation *>;
94 using ValueMap = mlir::DenseMap<ValueTy, SourceRefLatticeValue>;
95 // Used to lookup MLIR values/operations from a given SourceRef (all values that a ref is
96 // referenced by)
97 using ValueSet = mlir::DenseSet<ValueTy>;
98 using Ref2Val = mlir::DenseMap<SourceRef, mlir::DenseSet<ValueTy>>;
99 using AbstractDenseLattice::AbstractDenseLattice;
100
101 /* Static utilities */
102
107 static mlir::FailureOr<SourceRef> getSourceRef(mlir::Value val);
108
109 /* Required methods */
110
112 mlir::ChangeResult join(const AbstractDenseLattice &rhs) override {
113 return setValues(static_cast<const SourceRefLattice &>(rhs).valMap);
114 }
115
117 mlir::ChangeResult meet(const AbstractDenseLattice & /*rhs*/) override {
118 llvm::report_fatal_error("meet operation is not supported for SourceRefLattice");
119 return mlir::ChangeResult::NoChange;
120 }
121
122 void print(mlir::raw_ostream &os) const override;
123
124 /* Update utility methods */
125
126 mlir::ChangeResult setValues(const ValueMap &rhs);
127
128 mlir::ChangeResult setValue(ValueTy v, const SourceRefLatticeValue &rhs);
129
130 mlir::ChangeResult setValue(ValueTy v, const SourceRef &ref);
131
133
134 SourceRefLatticeValue getReturnValue(unsigned i) const;
135
136 ValueSet lookupValues(const SourceRef &r) const;
137
138 size_t size() const { return valMap.size(); }
139
140 const ValueMap &getMap() const { return valMap; }
141
142 const Ref2Val &getRef2Val() const { return refMap; }
143
144 ValueMap::iterator begin() { return valMap.begin(); }
145 ValueMap::iterator end() { return valMap.end(); }
146 ValueMap::const_iterator begin() const { return valMap.begin(); }
147 ValueMap::const_iterator end() const { return valMap.end(); }
148
149 friend mlir::raw_ostream &operator<<(mlir::raw_ostream &os, const SourceRefLattice &v);
150
151private:
152 ValueMap valMap;
153 Ref2Val refMap;
154};
155
156} // namespace llzk
157
158namespace llvm {
159class raw_ostream;
160
161raw_ostream &operator<<(raw_ostream &os, llvm::PointerUnion<mlir::Value, mlir::Operation *> ptr);
162} // 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 SourceRefLattice.
virtual std::pair< SourceRefLatticeValue, mlir::ChangeResult > elementwiseTransform(llvm::function_ref< SourceRef(const SourceRef &)> transform) const
Perform a recursive transformation over all elements of this value and return a new value with the mo...
std::pair< SourceRefLatticeValue, mlir::ChangeResult > extract(const std::vector< SourceRefIndex > &indices) const
Perform an array.extract or array.read operation, depending on how many indices are provided.
virtual ~SourceRefLatticeValue()=default
std::pair< SourceRefLatticeValue, mlir::ChangeResult > referenceField(SymbolLookupResult< component::FieldDefOp > fieldRef) const
Add the given fieldRef to the SourceRefs contained within this value.
SourceRefLatticeValue(mlir::ArrayRef< int64_t > shape)
const SourceRef & getSingleValue() const
mlir::ChangeResult insert(const SourceRef &rhs)
Directly insert the ref into this value.
std::pair< SourceRefLatticeValue, mlir::ChangeResult > translate(const TranslationMap &translation) const
For the refs contained in this value, translate them given the translation map and return the transfo...
mlir::ChangeResult translateScalar(const TranslationMap &translation)
Translate this value using the translation map, assuming this value is a scalar.
A lattice for use in dense analysis.
mlir::ChangeResult join(const AbstractDenseLattice &rhs) override
Maximum upper bound.
const ValueMap & getMap() const
mlir::ChangeResult meet(const AbstractDenseLattice &) override
Minimum lower bound.
mlir::DenseMap< SourceRef, mlir::DenseSet< ValueTy > > Ref2Val
ValueMap::const_iterator end() const
friend mlir::raw_ostream & operator<<(mlir::raw_ostream &os, const SourceRefLattice &v)
const Ref2Val & getRef2Val() const
mlir::DenseMap< ValueTy, SourceRefLatticeValue > ValueMap
mlir::DenseSet< ValueTy > ValueSet
void print(mlir::raw_ostream &os) const override
static mlir::FailureOr< SourceRef > getSourceRef(mlir::Value val)
If val is the source of other values (i.e., a block argument from the function args or a constant),...
ValueSet lookupValues(const SourceRef &r) const
mlir::ChangeResult setValues(const ValueMap &rhs)
SourceRefLatticeValue getOrDefault(ValueTy v) const
mlir::ChangeResult setValue(ValueTy v, const SourceRefLatticeValue &rhs)
ValueMap::iterator end()
ValueMap::iterator begin()
ValueMap::const_iterator begin() const
llvm::PointerUnion< mlir::Value, mlir::Operation * > ValueTy
SourceRefLatticeValue getReturnValue(unsigned i) const
A reference to a "source", which is the base value from which other SSA values are derived.
Definition SourceRef.h:127
raw_ostream & operator<<(raw_ostream &os, llvm::PointerUnion< mlir::Value, mlir::Operation * > ptr)
mlir::dataflow::AbstractDenseLattice AbstractDenseLattice
void ensure(bool condition, const llvm::Twine &errMsg)
std::unordered_map< SourceRef, SourceRefLatticeValue, SourceRef::Hash > TranslationMap