CIRCT 24.0.0git
Loading...
Searching...
No Matches
SimplifyRefs.cpp
Go to the documentation of this file.
1//===- SimplifyRefs.cpp - moore.concat_ref and queue reference lowering -- ===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file defines the SimplifyRefs pass.
10// It has three purposes:
11// - To disassemble the moore.concat_ref. Which is tricky to lower
12// directly. For example, disassemble "{a, b} = c" onto "a = c[7:3]"
13// and "b = c[2:0]".
14// - To eliminate moore.dyn_queue_ref_element in the case where the reference
15// immediately has a value assigned via blocking assignment, replacing it with
16// moore.queue.set. Queue element references are tricky to lower into LLHD,
17// so it's best to get rid of them.
18// - To rewrite assignments to ExtractOp expressions on a packed struct,
19// e.g. "s[7:0] = v", into an assignment to a concatenation of the struct's
20// (possibly further sliced) fields.
21//
22//===----------------------------------------------------------------------===//
23
26#include "mlir/IR/IRMapping.h"
27#include "mlir/Transforms/DialectConversion.h"
28
29namespace circt {
30namespace moore {
31#define GEN_PASS_DEF_SIMPLIFYREFS
32#include "circt/Dialect/Moore/MoorePasses.h.inc"
33} // namespace moore
34} // namespace circt
35
36using namespace circt;
37using namespace moore;
38using namespace mlir;
39
40namespace {
41
42// A helper function for collecting the non-concatRef operands of concatRef.
43static void collectOperands(Value operand, SmallVectorImpl<Value> &operands,
44 ConversionPatternRewriter &rewriter) {
45 if (auto concatRefOp = operand.getDefiningOp<ConcatRefOp>()) {
46 // Assuming the assignment is the only user, erase the op now.
47 if (std::distance(concatRefOp->getUsers().begin(),
48 concatRefOp->getUsers().end()) == 1) {
49 rewriter.eraseOp(concatRefOp);
50 }
51 for (auto nestedOperand : concatRefOp.getValues())
52 collectOperands(nestedOperand, operands, rewriter);
53
54 } else
55 operands.push_back(operand);
56}
57
58template <typename OpTy>
59struct ConcatRefLowering : public OpConversionPattern<OpTy> {
60 using OpConversionPattern<OpTy>::OpConversionPattern;
61 using OpAdaptor = typename OpTy::Adaptor;
62
63 LogicalResult
64 matchAndRewrite(OpTy op, OpAdaptor adaptor,
65 ConversionPatternRewriter &rewriter) const override {
66 // Use to collect the operands of concatRef.
67 SmallVector<Value, 4> operands;
68 collectOperands(op.getDst(), operands, rewriter);
69 auto srcWidth =
70 cast<UnpackedType>(op.getSrc().getType()).getBitSize().value();
71
72 // Disassemble assignments with the LHS is concatRef. And create new
73 // corresponding assignments using non-concatRef LHS.
74 for (auto operand : operands) {
75 auto type = cast<WrapperType>(operand.getType()).getNestedType();
76 auto width = type.getBitSize().value();
77
78 rewriter.setInsertionPoint(op);
79 // FIXME: Need to estimate whether the bits range is from large to
80 // small or vice versa. Like "logic [7:0] or [0:7]".
81
82 // Only able to correctly handle the situation like "[7:0]" now.
83 auto extract = ExtractOp::create(rewriter, op.getLoc(), type, op.getSrc(),
84 srcWidth - width);
85
86 // Update the real bit width of RHS of assignment. Like "c" the above
87 // description mentioned.
88 srcWidth = srcWidth - width;
89
90 // Clone the original op (preserving any extra operand, e.g. a delay on
91 // the delayed assign variants) and remap dst/src to the leaf ref and its
92 // extracted slice.
93 IRMapping mapping;
94 mapping.map(op.getDst(), operand);
95 mapping.map(op.getSrc(), Value(extract));
96 rewriter.clone(*op.getOperation(), mapping);
97 }
98 rewriter.eraseOp(op);
99 return success();
100 }
101};
102
103struct FieldInfo {
104 Value field = nullptr;
105 uint32_t size = 0;
106 uint32_t offset = 0;
107};
108
109// A helper function that recursively collects the members of a struct.
110static LogicalResult collectFields(Value structRef,
111 SmallVector<FieldInfo> &fields,
112 ConversionPatternRewriter &rewriter,
113 uint32_t initialOffset = 0) {
114 auto structRefType = cast<WrapperType>(structRef.getType());
115 auto structType = cast<StructType>(structRefType.getNestedType());
116 uint32_t offset = initialOffset;
117 // Visit fields in reverse order (declaration order is MSB-first)
118 for (auto &member : llvm::reverse(structType.getMembers())) {
119 auto fieldRef = StructExtractRefOp::create(
120 rewriter, structRef.getLoc(), structRefType.withNestedType(member.type),
121 member.name, structRef);
122
123 auto fieldSize = member.type.getBitSize();
124 if (!fieldSize)
125 return mlir::emitError(structRef.getLoc())
126 << "unsupported: field with unknown size in struct flattening";
127
128 if (isa<StructType>(member.type)) {
129 auto result = collectFields(fieldRef, fields, rewriter, offset);
130 if (failed(result))
131 return result;
132 } else if (isa<UnionType>(member.type)) {
133 return mlir::emitError(structRef.getLoc())
134 << "unsupported: union member in struct flattening";
135 } else {
136 fields.push_back({fieldRef, *fieldSize, offset});
137 }
138 offset += *fieldSize;
139 }
140 return success();
141}
142
143template <typename OpTy>
144struct StructExtractLowering : public OpConversionPattern<OpTy> {
145 using OpConversionPattern<OpTy>::OpConversionPattern;
146 using OpAdaptor = typename OpTy::Adaptor;
147
148 LogicalResult
149 matchAndRewrite(OpTy op, OpAdaptor adaptor,
150 ConversionPatternRewriter &rewriter) const override {
151 Value dst = op.getDst();
152
153 // We are specifically matching cases in which the LHS of the assignment is
154 // an ExtractRef operation on a struct.
155 auto extractRefOp = dyn_cast<ExtractRefOp>(dst.getDefiningOp());
156 auto baseStructType = dyn_cast_if_present<StructType>(
157 extractRefOp.getInput().getType().getNestedType());
158 if (!baseStructType)
159 return success();
160
161 // Get the boundaries of the ExtractOp.
162 uint32_t extractedLow = extractRefOp.getLowBit();
163 auto targetWidth =
164 cast<WrapperType>(dst.getType()).getNestedType().getBitSize();
165 if (!targetWidth)
166 return mlir::emitError(op.getLoc())
167 << "unsupported: found field with unknown size in struct "
168 "flattening";
169
170 rewriter.setInsertionPoint(op);
171 Location loc = op.getLoc();
172
173 // Collect all the fields of the struct.
174 SmallVector<FieldInfo> fields;
175 if (failed(collectFields(extractRefOp.getInput(), fields, rewriter))) {
176 return failure();
177 }
178
179 // Select only the fields revelant to the ExtractOp
180 SmallVector<Value> relevantFields;
181 uint32_t offsetInStruct = extractedLow;
182 uint32_t remaining = *targetWidth;
183 for (auto &field : fields) {
184 if (remaining == 0)
185 break;
186
187 if (field.offset <= offsetInStruct &&
188 field.offset + field.size > offsetInStruct) {
189 uint32_t offsetInField = offsetInStruct - field.offset;
190 uint32_t remainingInField = field.size - offsetInField;
191 uint32_t sizeToExtract = std::min(remaining, remainingInField);
192
193 auto fieldRefType = cast<WrapperType>(field.field.getType());
194 auto fieldType = fieldRefType.getNestedType();
195 auto extractType =
196 IntType::get(rewriter.getContext(), sizeToExtract,
197 cast<PackedType>(fieldType).getDomain());
198 auto newLhs = ExtractRefOp::create(
199 rewriter, loc, fieldRefType.withNestedType(extractType),
200 field.field, offsetInField);
201 relevantFields.push_back(newLhs);
202
203 offsetInStruct += sizeToExtract;
204 remaining -= sizeToExtract;
205 }
206 }
207
208 if (relevantFields.size() == 0)
209 return mlir::emitError(op.getLoc()) << "Struct extract is out of range";
210
211 // Reverse the fields order to match Concat order
212 std::reverse(relevantFields.begin(), relevantFields.end());
213
214 Value finalRef =
215 relevantFields.size() == 1
216 ? relevantFields.front()
217 : Value(ConcatRefOp::create(rewriter, loc, relevantFields));
218
219 IRMapping mapping;
220 mapping.map(op.getDst(), finalRef);
221 rewriter.clone(*op.getOperation(), mapping);
222
223 rewriter.eraseOp(op);
224 rewriter.eraseOp(extractRefOp);
225 return success();
226 }
227};
228
229struct QueueRefLowering : public OpConversionPattern<DynQueueRefElementOp> {
230 using OpConversionPattern<DynQueueRefElementOp>::OpConversionPattern;
231
232 LogicalResult
233 matchAndRewrite(DynQueueRefElementOp op, OpAdaptor adaptor,
234 ConversionPatternRewriter &rewriter) const override {
235 // For now, we only support using a queue reference in the LHS of a blocking
236 // assignment op.
237 for (auto *consumer : op->getUsers()) {
238 if (isa<BlockingAssignOp>(consumer)) {
239
240 auto assignOp = cast<BlockingAssignOp>(consumer);
241 // Replace BlockingAssignOp with a queue.set operation to the index.
242 rewriter.setInsertionPoint(consumer);
243 moore::QueueSetOp::create(rewriter, op->getLoc(), op.getInput(),
244 op.getIndex(), assignOp.getSrc());
245
246 rewriter.eraseOp(assignOp);
247 } else {
248 return mlir::emitError(op.getLoc())
249 << "Queue element reference couldn't be reduced to setting the "
250 "value at an index: consuming op "
251 << consumer << " is not supported";
252 }
253 }
254
255 rewriter.eraseOp(op);
256
257 return success();
258 }
259};
260
261struct AssocArrayRefLowering
262 : public OpConversionPattern<AssocArrayExtractRefOp> {
263 using OpConversionPattern<AssocArrayExtractRefOp>::OpConversionPattern;
264
265 LogicalResult
266 matchAndRewrite(AssocArrayExtractRefOp op, OpAdaptor adaptor,
267 ConversionPatternRewriter &rewriter) const override {
268 for (auto *consumer : op->getUsers()) {
269 if (isa<BlockingAssignOp>(consumer)) {
270 auto assignOp = cast<BlockingAssignOp>(consumer);
271 rewriter.setInsertionPoint(consumer);
272 moore::AssocArraySetOp::create(rewriter, op->getLoc(), op.getInput(),
273 op.getIndex(), assignOp.getSrc());
274 rewriter.eraseOp(assignOp);
275 } else {
276 return mlir::emitError(op.getLoc())
277 << "Associative array element reference couldn't be reduced "
278 "to setting the value at an index: consuming op "
279 << consumer << " is not supported";
280 }
281 }
282
283 rewriter.eraseOp(op);
284 return success();
285 }
286};
287
288struct SimplifyRefsPass
289 : public circt::moore::impl::SimplifyRefsBase<SimplifyRefsPass> {
290 void runOnOperation() override;
291};
292
293} // namespace
294
295std::unique_ptr<mlir::Pass> circt::moore::createSimplifyRefsPass() {
296 return std::make_unique<SimplifyRefsPass>();
297}
298
299void SimplifyRefsPass::runOnOperation() {
300 MLIRContext &context = getContext();
302
303 target.addDynamicallyLegalOp<ContinuousAssignOp, BlockingAssignOp,
304 NonBlockingAssignOp, DelayedContinuousAssignOp,
305 DelayedNonBlockingAssignOp>([](auto op) {
306 auto extractRefOp =
307 op->getOperand(0).template getDefiningOp<ExtractRefOp>();
308 if (!extractRefOp)
309 return true;
310 auto nestedType = extractRefOp.getInput().getType().getNestedType();
311 return !isa<StructType>(nestedType);
312 });
313
314 target.markUnknownOpDynamicallyLegal([](Operation *) { return true; });
315
316 RewritePatternSet extractRefOnStructPatterns(&context);
317 extractRefOnStructPatterns
318 .add<StructExtractLowering<ContinuousAssignOp>,
319 StructExtractLowering<BlockingAssignOp>,
320 StructExtractLowering<NonBlockingAssignOp>,
321 StructExtractLowering<DelayedContinuousAssignOp>,
322 StructExtractLowering<DelayedNonBlockingAssignOp>>(&context);
323
324 if (failed(applyFullConversion(getOperation(), target,
325 std::move(extractRefOnStructPatterns)))) {
326 signalPassFailure();
327 return;
328 }
329
330 target.addDynamicallyLegalOp<ContinuousAssignOp, BlockingAssignOp,
331 NonBlockingAssignOp, DelayedContinuousAssignOp,
332 DelayedNonBlockingAssignOp>([](auto op) {
333 return !op->getOperand(0).template getDefiningOp<ConcatRefOp>();
334 });
335
336 RewritePatternSet concatRefPatterns(&context);
337 concatRefPatterns.add<ConcatRefLowering<ContinuousAssignOp>,
338 ConcatRefLowering<BlockingAssignOp>,
339 ConcatRefLowering<NonBlockingAssignOp>,
340 ConcatRefLowering<DelayedContinuousAssignOp>,
341 ConcatRefLowering<DelayedNonBlockingAssignOp>>(
342 &context);
343
344 if (failed(applyFullConversion(getOperation(), target,
345 std::move(concatRefPatterns)))) {
346 signalPassFailure();
347 return;
348 }
349
350 // Once we have removed ConcatRefOps, attempt to rewrite any queue element
351 // references to queue.set
352 RewritePatternSet queueRefPatterns(&context);
353 target.addIllegalOp<DynQueueRefElementOp>();
354 queueRefPatterns.add<QueueRefLowering>(&context);
355 if (failed(applyPartialConversion(getOperation(), target,
356 std::move(queueRefPatterns))))
357 signalPassFailure();
358
359 // Once we have removed AssocArrayExtractRefOps, attempt to rewrite any
360 // associative array element references to assoc_array.set
361 RewritePatternSet assocArrayRefPatterns(&context);
362 target.addIllegalOp<AssocArrayExtractRefOp>();
363 assocArrayRefPatterns.add<AssocArrayRefLowering>(&context);
364 if (failed(applyPartialConversion(getOperation(), target,
365 std::move(assocArrayRefPatterns))))
366 signalPassFailure();
367}
static std::unique_ptr< Context > context
static Attribute collectFields(MLIRContext *context, ArrayRef< Attribute > operands)
false return failure()
Parse an optional keyword or string and set instance into 'result'.
return success()
std::unique_ptr< mlir::Pass > createSimplifyRefsPass()
The InstanceGraph op interface, see InstanceGraphInterface.td for more details.
A named and typed field of an aggregate type.
Definition FieldInfo.h:27