LLZK 0.1.0
Veridise's ZK Language IR
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
22
23#include <llvm/Support/Debug.h>
24
25#define DEBUG_TYPE "llzk-signal-equivalence"
26
27using namespace mlir;
28using namespace llzk::component;
29
30namespace llzk {
31
33
34Value replaceReadWithWrite(Value v) {
35 if (!v.getDefiningOp()) {
36 return v;
37 }
38 if (auto read = dyn_cast<FieldReadOp>(v.getDefiningOp())) {
39 // Traverse backwards through the block until we find a write
40 for (Operation *cur = read; cur != nullptr; cur = cur->getPrevNode()) {
41 if (auto write = dyn_cast<FieldWriteOp>(cur)) {
42 // Return the written value
43 return write.getVal();
44 }
45 }
46 }
47 return v;
48}
49
51 v1 = replaceReadWithWrite(v1);
52 v2 = replaceReadWithWrite(v2);
53 LLVM_DEBUG(llvm::outs() << "Asking for equivalence between " << v1 << " and " << v2 << "\n");
54 if (equivalentSignals.isEquivalent(v1, v2)) {
55 return true;
56 }
57
58 Operation *o1 = v1.getDefiningOp();
59 Operation *o2 = v2.getDefiningOp();
60
61 if (o1 == nullptr || o2 == nullptr) {
62 return false;
63 }
64
65 if (o1->getName() != o2->getName()) {
66 return false;
67 }
68
69 if (o1->getNumOperands() != o2->getNumOperands()) {
70 return false;
71 }
72
73 for (size_t i = 0; i < o1->getNumOperands(); i++) {
74 if (!areSignalsEquivalent(o1->getOperand(i), o2->getOperand(i))) {
75 return false;
76 }
77 }
78
79 equivalentSignals.unionSets(v1, v2);
80 return true;
81}
82} // namespace llzk
Value replaceReadWithWrite(Value v)