LLZK 3.0.0
An open-source IR for Zero Knowledge (ZK) circuits
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 <mlir/Analysis/DataFlow/DenseAnalysis.h>
18
19#include <llvm/ADT/PointerUnion.h>
20
21namespace llzk {
22
24using TranslationMap = std::unordered_map<SourceRef, SourceRefLatticeValue, SourceRef::Hash>;
25
28 : public dataflow::AbstractLatticeValue<SourceRefLatticeValue, SourceRefSet> {
31 using ScalarTy = SourceRefSet;
39 using ArrayTy = std::vector<std::unique_ptr<SourceRefLatticeValue>>;
40
41public:
42 explicit SourceRefLatticeValue(ScalarTy s) : Base(std::move(s)) {}
43 explicit SourceRefLatticeValue(SourceRef r) : Base(ScalarTy {std::move(r)}) {}
44 SourceRefLatticeValue() : Base(ScalarTy {}) {}
49 virtual ~SourceRefLatticeValue() = default;
50
51 // Create an empty array of the given shape.
52 explicit SourceRefLatticeValue(mlir::ArrayRef<int64_t> shape) : Base(shape) {}
53
54 const SourceRef &getSingleValue() const {
55 ensure(isSingleValue(), "not a single value");
56 return *getScalarValue().begin();
57 }
58
61
65 mlir::ChangeResult insert(const SourceRef &rhs);
66
68 mlir::ChangeResult remove(const SourceRef &ref);
69
73 std::pair<SourceRefLatticeValue, mlir::ChangeResult>
74 translate(const TranslationMap &translation) const;
75
77 std::pair<SourceRefLatticeValue, mlir::ChangeResult>
78 replacePrefixes(const TranslationMap &translation) const;
79
85 mlir::ChangeResult write(
86 const std::vector<SourceRefIndex> &indices, const SourceRefLatticeValue &rhs,
87 bool joinWithExisting = false
88 );
89
96 mlir::FailureOr<std::pair<SourceRefLatticeValue, mlir::ChangeResult>>
98
102 mlir::FailureOr<std::pair<SourceRefLatticeValue, mlir::ChangeResult>>
103 referencePodRecord(mlir::StringAttr recordName) const;
104
107 mlir::FailureOr<std::pair<SourceRefLatticeValue, mlir::ChangeResult>>
108 extract(const std::vector<SourceRefIndex> &indices) const;
109
110protected:
113 mlir::ChangeResult translateScalar(const TranslationMap &translation);
114
116 mlir::ChangeResult replacePrefixesScalar(const TranslationMap &translation);
117
120 virtual mlir::FailureOr<std::pair<SourceRefLatticeValue, mlir::ChangeResult>>
122 llvm::function_ref<mlir::FailureOr<SourceRef>(const SourceRef &)> transform
123 ) const;
124};
125
128public:
130 // mlir::Value is used for read-like operations that create references in their results,
131 // mlir::Operation* is used for write-like operations that reference values as their destinations
132 using ValueTy = llvm::PointerUnion<mlir::Value, mlir::Operation *>;
133 using Ref2Val = mlir::DenseMap<SourceRef, mlir::DenseSet<ValueTy>>;
134
135 /* Static utilities */
136
141 static mlir::FailureOr<SourceRef> getSourceRef(mlir::Value val);
143
144 using AbstractSparseLattice::AbstractSparseLattice;
145
146 mlir::ChangeResult join(const AbstractSparseLattice &rhs) override;
147 mlir::ChangeResult meet(const AbstractSparseLattice &rhs) override;
148 void print(mlir::raw_ostream &os) const override;
149
150 const LatticeValue &getValue() const { return value; }
151
152 mlir::ChangeResult setValue(const LatticeValue &newValue);
153 mlir::ChangeResult setValue(const SourceRef &ref);
154
155private:
156 LatticeValue value;
157};
158
159} // namespace llzk
160
161namespace llvm {
162class raw_ostream;
163
164raw_ostream &operator<<(raw_ostream &os, llvm::PointerUnion<mlir::Value, mlir::Operation *> ptr);
165} // namespace llvm
This file provides LLZK's sparse forward data-flow analysis compatibility layer.
A value at a given point of the SourceRefLattice.
mlir::ChangeResult write(const std::vector< SourceRefIndex > &indices, const SourceRefLatticeValue &rhs, bool joinWithExisting=false)
Update the element or subarray selected by indices.
virtual ~SourceRefLatticeValue()=default
SourceRefLatticeValue & operator=(const SourceRefLatticeValue &)=default
mlir::FailureOr< std::pair< SourceRefLatticeValue, mlir::ChangeResult > > referencePodRecord(mlir::StringAttr recordName) const
Add the given pod recordName to the SourceRefs contained within this value.
mlir::ChangeResult remove(const SourceRef &ref)
Remove ref from this value's reference set or, for an array, from every element.
SourceRefLatticeValue(SourceRefLatticeValue &&)=default
mlir::ChangeResult replacePrefixesScalar(const TranslationMap &translation)
Replace matching prefixes in a scalar value without dropping unmatched references.
SourceRefLatticeValue(mlir::ArrayRef< int64_t > shape)
mlir::FailureOr< std::pair< SourceRefLatticeValue, mlir::ChangeResult > > referenceMember(SymbolLookupResult< component::MemberDefOp > memberRef) const
Add the given memberRef to the SourceRefs contained within this value.
virtual mlir::FailureOr< std::pair< SourceRefLatticeValue, mlir::ChangeResult > > elementwiseTransform(llvm::function_ref< mlir::FailureOr< SourceRef >(const SourceRef &)> transform) const
Perform a recursive transformation over all elements of this value and return a new value with the mo...
const SourceRef & getSingleValue() const
mlir::ChangeResult insert(const SourceRef &rhs)
Directly insert the ref into this value.
SourceRefLatticeValue(const SourceRefLatticeValue &)=default
std::pair< SourceRefLatticeValue, mlir::ChangeResult > translate(const TranslationMap &translation) const
Translate contained references using translation and return the transformed value.
mlir::FailureOr< 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.
mlir::ChangeResult translateScalar(const TranslationMap &translation)
Translate this value using the translation map, assuming this value is a scalar.
SourceRefLatticeValue & operator=(SourceRefLatticeValue &&)=default
std::pair< SourceRefLatticeValue, mlir::ChangeResult > replacePrefixes(const TranslationMap &translation) const
Replace matching SourceRef prefixes and leave unmatched references unchanged.
Sparse SSA-value lattice for SourceRef propagation.
mlir::ChangeResult join(const AbstractSparseLattice &rhs) override
mlir::ChangeResult setValue(const LatticeValue &newValue)
mlir::DenseMap< SourceRef, mlir::DenseSet< ValueTy > > Ref2Val
mlir::ChangeResult meet(const AbstractSparseLattice &rhs) override
static SourceRefLatticeValue getDefaultValue(ValueTy v)
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, an allocation-like op result,...
const LatticeValue & getValue() const
llvm::PointerUnion< mlir::Value, mlir::Operation * > ValueTy
SourceRefLatticeValue LatticeValue
A reference to a "source", which is the base value from which other SSA values are derived.
Definition SourceRef.h:146
raw_ostream & operator<<(raw_ostream &os, llvm::PointerUnion< mlir::Value, mlir::Operation * > ptr)
mlir::dataflow::AbstractSparseLattice AbstractSparseLattice
void ensure(bool condition, const llvm::Twine &errMsg)
std::unordered_map< SourceRef, SourceRefLatticeValue, SourceRef::Hash > TranslationMap