-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathLLZKComputeConstrainToProductPass.cpp
More file actions
246 lines (202 loc) · 7.92 KB
/
LLZKComputeConstrainToProductPass.cpp
File metadata and controls
246 lines (202 loc) · 7.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
//===-- LLZKComputeConstrainToProductPass.cpp -------------------*- C++ -*-===//
//
// Part of the LLZK Project, under the Apache License v2.0.
// See LICENSE.txt for license information.
// Copyright 2025 Veridise Inc.
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//
///
/// \file
/// This file implements the `-llzk-product-program` pass.
///
//===----------------------------------------------------------------------===//
#include "llzk/Analysis/LightweightSignalEquivalenceAnalysis.h"
#include "llzk/Dialect/Function/IR/Ops.h"
#include "llzk/Dialect/Struct/IR/Ops.h"
#include "llzk/Transforms/LLZKComputeConstrainToProductPass.h"
#include "llzk/Transforms/LLZKTransformationPasses.h"
#include "llzk/Util/Constants.h"
#include "llzk/Util/SymbolHelper.h"
#include <mlir/IR/Builders.h>
#include <mlir/Transforms/InliningUtils.h>
#include <llvm/Support/Debug.h>
#include <memory>
#include <ranges>
namespace llzk {
#define GEN_PASS_DECL_COMPUTECONSTRAINTOPRODUCTPASS
#define GEN_PASS_DEF_COMPUTECONSTRAINTOPRODUCTPASS
#include "llzk/Transforms/LLZKTransformationPasses.h.inc"
} // namespace llzk
#define DEBUG_TYPE "llzk-compute-constrain-to-product-pass"
using namespace llzk::component;
using namespace llzk::function;
using namespace mlir;
using std::make_unique;
namespace llzk {
bool isValidRoot(StructDefOp root) {
FuncDefOp computeFunc = root.getComputeFuncOp();
FuncDefOp constrainFunc = root.getConstrainFuncOp();
if (!computeFunc || !constrainFunc) {
root->emitError() << "no " << FUNC_NAME_COMPUTE << "/" << FUNC_NAME_CONSTRAIN << " to align";
return false;
}
/// TODO: If root::@compute and root::@constrain are called anywhere else, this is not a valid
/// root to start aligning from (issue #241)
return true;
}
LogicalResult alignStartingAt(
component::StructDefOp root, SymbolTableCollection &tables,
LightweightSignalEquivalenceAnalysis &equivalence
) {
if (!isValidRoot(root)) {
return failure();
}
ProductAligner aligner {tables, equivalence};
if (!aligner.alignFuncs(root, root.getComputeFuncOp(), root.getConstrainFuncOp())) {
return failure();
}
for (auto s : aligner.alignedStructs) {
s.getComputeFuncOp()->erase();
s.getConstrainFuncOp()->erase();
}
return success();
}
class ComputeConstrainToProductPass
: public llzk::impl::ComputeConstrainToProductPassBase<ComputeConstrainToProductPass> {
public:
void runOnOperation() override {
ModuleOp mod = getOperation();
StructDefOp root;
SymbolTableCollection tables;
LightweightSignalEquivalenceAnalysis equivalence {
getAnalysis<LightweightSignalEquivalenceAnalysis>()
};
// Find the indicated root struct and make sure its a valid place to start aligning
mod.walk([&root, this](StructDefOp structDef) {
if (structDef.getSymName() == rootStruct) {
root = structDef;
}
});
if (failed(alignStartingAt(root, tables, equivalence))) {
signalPassFailure();
}
}
};
FuncDefOp ProductAligner::alignFuncs(StructDefOp root, FuncDefOp compute, FuncDefOp constrain) {
OpBuilder funcBuilder(compute);
// Add compute/constrain attributes
compute.walk([&funcBuilder](Operation *op) {
op->setAttr(PRODUCT_SOURCE, funcBuilder.getStringAttr(FUNC_NAME_COMPUTE));
});
constrain.walk([&funcBuilder](Operation *op) {
op->setAttr(PRODUCT_SOURCE, funcBuilder.getStringAttr(FUNC_NAME_CONSTRAIN));
});
// Create an empty @product func...
FuncDefOp productFunc = funcBuilder.create<FuncDefOp>(
funcBuilder.getFusedLoc({compute.getLoc(), constrain.getLoc()}), FUNC_NAME_PRODUCT,
compute.getFunctionType()
);
Block *entryBlock = productFunc.addEntryBlock();
funcBuilder.setInsertionPointToStart(entryBlock);
// ...with the right arguments
llvm::SmallVector<Value> args {productFunc.getArguments()};
// Add calls to @compute and @constrain...
CallOp computeCall = funcBuilder.create<CallOp>(funcBuilder.getUnknownLoc(), compute, args);
args.insert(args.begin(), computeCall->getResult(0));
CallOp constrainCall = funcBuilder.create<CallOp>(funcBuilder.getUnknownLoc(), constrain, args);
funcBuilder.create<ReturnOp>(funcBuilder.getUnknownLoc(), computeCall->getResult(0));
// ..and inline them
InlinerInterface inliner(productFunc.getContext());
if (failed(inlineCall(inliner, computeCall, compute, &compute.getBody(), true))) {
root->emitError() << "failed to inline " << FUNC_NAME_COMPUTE;
return nullptr;
}
if (failed(inlineCall(inliner, constrainCall, constrain, &constrain.getBody(), true))) {
root->emitError() << "failed to inline " << FUNC_NAME_CONSTRAIN;
return nullptr;
}
computeCall->erase();
constrainCall->erase();
// Mark the compute/constrain for deletion
alignedStructs.push_back(root);
// Make sure we can align sub-calls to @compute and @constrain
if (failed(alignCalls(productFunc))) {
return nullptr;
}
return productFunc;
}
LogicalResult ProductAligner::alignCalls(FuncDefOp product) {
// Gather up all the remaining calls to @compute and @constrain
llvm::SetVector<CallOp> computeCalls, constrainCalls;
product.walk([&](CallOp callOp) {
if (callOp.calleeIsStructCompute()) {
computeCalls.insert(callOp);
} else if (callOp.calleeIsStructConstrain()) {
constrainCalls.insert(callOp);
}
});
llvm::SetVector<std::pair<CallOp, CallOp>> alignedCalls;
// A @compute matches a @constrain if they belong to the same struct and all their input signals
// are pairwise equivalent
auto doCallsMatch = [&](CallOp compute, CallOp constrain) -> bool {
LLVM_DEBUG({
llvm::outs() << "Asking for equivalence between calls\n"
<< compute << "\nand\n"
<< constrain << "\n\n";
llvm::outs() << "In block:\n\n" << *compute->getBlock() << "\n";
});
auto computeStruct = getPrefixAsSymbolRefAttr(compute.getCallee());
auto constrainStruct = getPrefixAsSymbolRefAttr(constrain.getCallee());
if (computeStruct != constrainStruct) {
return false;
}
for (unsigned i = 0, e = compute->getNumOperands() - 1; i < e; i++) {
if (!equivalence.areSignalsEquivalent(compute->getOperand(i), constrain->getOperand(i + 1))) {
return false;
}
}
return true;
};
for (auto compute : computeCalls) {
// If there is exactly one @compute that matches a given @constrain, we can align them
auto matches = llvm::filter_to_vector(constrainCalls, [&](CallOp constrain) {
return doCallsMatch(compute, constrain);
});
if (matches.size() == 1) {
alignedCalls.insert({compute, matches[0]});
computeCalls.remove(compute);
constrainCalls.remove(matches[0]);
}
}
// TODO: If unaligned calls remain, fully inline their structs and continue instead of failing
if (!computeCalls.empty() && constrainCalls.empty()) {
product->emitError() << "failed to align some @" << FUNC_NAME_COMPUTE << " and @"
<< FUNC_NAME_CONSTRAIN;
return failure();
}
for (auto [compute, constrain] : alignedCalls) {
// If @A::@compute matches @A::@constrain, recursively align the functions in @A...
auto newRoot = compute.getCalleeTarget(tables)->get()->getParentOfType<StructDefOp>();
assert(newRoot);
FuncDefOp newProduct =
alignFuncs(newRoot, newRoot.getComputeFuncOp(), newRoot.getConstrainFuncOp());
if (!newProduct) {
return failure();
}
// ...and replace the two calls with a single call to @A::@product
OpBuilder callBuilder(compute);
CallOp newCall = callBuilder.create<CallOp>(
callBuilder.getFusedLoc({compute.getLoc(), constrain.getLoc()}), newProduct,
compute.getOperands()
);
compute->replaceAllUsesWith(newCall.getResults());
compute->erase();
constrain->erase();
}
return success();
}
std::unique_ptr<Pass> createComputeConstrainToProductPass() {
return make_unique<ComputeConstrainToProductPass>();
}
} // namespace llzk