LLZK 2.1.1
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
62 mlir::ChangeResult insert(const SourceRef &rhs);
63
66 std::pair<SourceRefLatticeValue, mlir::ChangeResult>
67 translate(const TranslationMap &translation) const;
68
75 mlir::FailureOr<std::pair<SourceRefLatticeValue, mlir::ChangeResult>>
77
80 mlir::FailureOr<std::pair<SourceRefLatticeValue, mlir::ChangeResult>>
81 extract(const std::vector<SourceRefIndex> &indices) const;
82
83protected:
86 mlir::ChangeResult translateScalar(const TranslationMap &translation);
87
90 virtual mlir::FailureOr<std::pair<SourceRefLatticeValue, mlir::ChangeResult>>
92 llvm::function_ref<mlir::FailureOr<SourceRef>(const SourceRef &)> transform
93 ) const;
94};
95
98public:
100 // mlir::Value is used for read-like operations that create references in their results,
101 // mlir::Operation* is used for write-like operations that reference values as their destinations
102 using ValueTy = llvm::PointerUnion<mlir::Value, mlir::Operation *>;
103 using Ref2Val = mlir::DenseMap<SourceRef, mlir::DenseSet<ValueTy>>;
104
105 /* Static utilities */
106
111 static mlir::FailureOr<SourceRef> getSourceRef(mlir::Value val);
113
114 using AbstractSparseLattice::AbstractSparseLattice;
115
116 mlir::ChangeResult join(const AbstractSparseLattice &rhs) override;
117 mlir::ChangeResult meet(const AbstractSparseLattice &rhs) override;
118 void print(mlir::raw_ostream &os) const override;
119
120 const LatticeValue &getValue() const { return value; }
121
122 mlir::ChangeResult setValue(const LatticeValue &newValue);
123 mlir::ChangeResult setValue(const SourceRef &ref);
124
125private:
126 LatticeValue value;
127};
128
129} // namespace llzk
130
131namespace llvm {
132class raw_ostream;
133
134raw_ostream &operator<<(raw_ostream &os, llvm::PointerUnion<mlir::Value, mlir::Operation *> ptr);
135} // 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 & operator=(const SourceRefLatticeValue &)=default
SourceRefLatticeValue(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.
SourceRefLatticeValue(const SourceRefLatticeValue &)=default
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.
SourceRefLatticeValue & operator=(SourceRefLatticeValue &&)=default
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