LLZK 2.0.0
An open-source IR for Zero Knowledge (ZK) circuits
Loading...
Searching...
No Matches
LightweightSignalEquivalenceAnalysis.cpp
Go to the documentation of this file.
1//===- LightweightSignalEquivalenceAnalysis.cpp ---------------------------===//
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// Adapted from mlir/lib/Analysis/DataFlow/SparseAnalysis.cpp.
9// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
10// See https://llvm.org/LICENSE.txt for license information.
11// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
12//
13//===----------------------------------------------------------------------===//
18//===----------------------------------------------------------------------===//
19
21
23
24#include <llvm/Support/Debug.h>
25
26#define DEBUG_TYPE "llzk-signal-equivalence"
27
28using namespace mlir;
29using namespace llzk::component;
30
31namespace llzk {
32
34
35Value replaceReadWithWrite(Value v) {
36 if (!v.getDefiningOp()) {
37 return v;
38 }
39 if (auto read = dyn_cast<MemberReadOp>(v.getDefiningOp())) {
40 // Traverse backwards through the block until we find a write
41 for (Operation *cur = read; cur != nullptr; cur = cur->getPrevNode()) {
42 if (auto write = dyn_cast<MemberWriteOp>(cur)) {
43 // Return the written value
44 return write.getVal();
45 }
46 }
47 }
48 return v;
49}
50
52 v1 = replaceReadWithWrite(v1);
53 v2 = replaceReadWithWrite(v2);
54 LLVM_DEBUG(llvm::outs() << "Asking for equivalence between " << v1 << " and " << v2 << "\n");
55 if (equivalentSignals.isEquivalent(v1, v2)) {
56 return true;
57 }
58
59 Operation *o1 = v1.getDefiningOp();
60 Operation *o2 = v2.getDefiningOp();
61
62 if (o1 == nullptr || o2 == nullptr) {
63 return false;
64 }
65
66 if (o1->getName() != o2->getName()) {
67 return false;
68 }
69
70 if (o1->getNumOperands() != o2->getNumOperands()) {
71 return false;
72 }
73
74 for (size_t i = 0; i < o1->getNumOperands(); i++) {
75 if (!areSignalsEquivalent(o1->getOperand(i), o2->getOperand(i))) {
76 return false;
77 }
78 }
79
80 equivalentSignals.unionSets(v1, v2);
81 return true;
82}
83} // namespace llzk
Value replaceReadWithWrite(Value v)