LLZK 2.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 {}) {}
45 virtual ~SourceRefLatticeValue() = default;
46
47 // Create an empty array of the given shape.
48 explicit SourceRefLatticeValue(mlir::ArrayRef<int64_t> shape) : Base(shape) {}
49
50 const SourceRef &getSingleValue() const {
51 ensure(isSingleValue(), "not a single value");
52 return *getScalarValue().begin();
53 }
54
58 mlir::ChangeResult insert(const SourceRef &rhs);
59
62 std::pair<SourceRefLatticeValue, mlir::ChangeResult>
63 translate(const TranslationMap &translation) const;
64
71 mlir::FailureOr<std::pair<SourceRefLatticeValue, mlir::ChangeResult>>
73
76 mlir::FailureOr<std::pair<SourceRefLatticeValue, mlir::ChangeResult>>
77 extract(const std::vector<SourceRefIndex> &indices) const;
78
79protected:
82 mlir::ChangeResult translateScalar(const TranslationMap &translation);
83
86 virtual mlir::FailureOr<std::pair<SourceRefLatticeValue, mlir::ChangeResult>>
88 llvm::function_ref<mlir::FailureOr<SourceRef>(const SourceRef &)> transform
89 ) const;
90};
91
94public:
96 // mlir::Value is used for read-like operations that create references in their results,
97 // mlir::Operation* is used for write-like operations that reference values as their destinations
98 using ValueTy = llvm::PointerUnion<mlir::Value, mlir::Operation *>;
99 using Ref2Val = mlir::DenseMap<SourceRef, mlir::DenseSet<ValueTy>>;
100
101 /* Static utilities */
102
107 static mlir::FailureOr<SourceRef> getSourceRef(mlir::Value val);
109
110 using AbstractSparseLattice::AbstractSparseLattice;
111
112 mlir::ChangeResult join(const AbstractSparseLattice &rhs) override;
113 mlir::ChangeResult meet(const AbstractSparseLattice &rhs) override;
114 void print(mlir::raw_ostream &os) const override;
115
116 const LatticeValue &getValue() const { return value; }
117
118 mlir::ChangeResult setValue(const LatticeValue &newValue);
119 mlir::ChangeResult setValue(const SourceRef &ref);
120
121private:
122 LatticeValue value;
123};
124
125} // namespace llzk
126
127namespace llvm {
128class raw_ostream;
129
130raw_ostream &operator<<(raw_ostream &os, llvm::PointerUnion<mlir::Value, mlir::Operation *> ptr);
131} // namespace llvm
This file implements sparse data-flow analysis using the data-flow analysis framework.
A value at a given point of the SourceRefLattice.
virtual ~SourceRefLatticeValue()=default
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.
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::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.
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:132
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