MLIR  20.0.0git
LowerDeallocations.cpp
Go to the documentation of this file.
1 //===- LowerDeallocations.cpp - Bufferization Deallocs to MemRef pass -----===//
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 implements patterns to convert `bufferization.dealloc` operations
10 // to the MemRef dialect.
11 //
12 //===----------------------------------------------------------------------===//
13 
20 #include "mlir/IR/BuiltinTypes.h"
21 #include "mlir/Pass/Pass.h"
23 
24 namespace mlir {
25 namespace bufferization {
26 #define GEN_PASS_DEF_LOWERDEALLOCATIONS
27 #include "mlir/Dialect/Bufferization/Transforms/Passes.h.inc"
28 } // namespace bufferization
29 } // namespace mlir
30 
31 using namespace mlir;
32 
33 namespace {
34 /// The DeallocOpConversion transforms all bufferization dealloc operations into
35 /// memref dealloc operations potentially guarded by scf if operations.
36 /// Additionally, memref extract_aligned_pointer_as_index and arith operations
37 /// are inserted to compute the guard conditions. We distinguish multiple cases
38 /// to provide an overall more efficient lowering. In the general case, a helper
39 /// func is created to avoid quadratic code size explosion (relative to the
40 /// number of operands of the dealloc operation). For examples of each case,
41 /// refer to the documentation of the member functions of this class.
42 class DeallocOpConversion
43  : public OpConversionPattern<bufferization::DeallocOp> {
44 
45  /// Lower a simple case without any retained values and a single memref to
46  /// avoiding the helper function. Ideally, static analysis can provide enough
47  /// aliasing information to split the dealloc operations up into this simple
48  /// case as much as possible before running this pass.
49  ///
50  /// Example:
51  /// ```
52  /// bufferization.dealloc (%arg0 : memref<2xf32>) if (%arg1)
53  /// ```
54  /// is lowered to
55  /// ```
56  /// scf.if %arg1 {
57  /// memref.dealloc %arg0 : memref<2xf32>
58  /// }
59  /// ```
60  LogicalResult
61  rewriteOneMemrefNoRetainCase(bufferization::DeallocOp op, OpAdaptor adaptor,
62  ConversionPatternRewriter &rewriter) const {
63  assert(adaptor.getMemrefs().size() == 1 && "expected only one memref");
64  assert(adaptor.getRetained().empty() && "expected no retained memrefs");
65 
66  rewriter.replaceOpWithNewOp<scf::IfOp>(
67  op, adaptor.getConditions()[0], [&](OpBuilder &builder, Location loc) {
68  builder.create<memref::DeallocOp>(loc, adaptor.getMemrefs()[0]);
69  builder.create<scf::YieldOp>(loc);
70  });
71  return success();
72  }
73 
74  /// A special case lowering for the deallocation operation with exactly one
75  /// memref, but arbitrary number of retained values. This avoids the helper
76  /// function that the general case needs and thus also avoids storing indices
77  /// to specifically allocated memrefs. The size of the code produced by this
78  /// lowering is linear to the number of retained values.
79  ///
80  /// Example:
81  /// ```mlir
82  /// %0:2 = bufferization.dealloc (%m : memref<2xf32>) if (%cond)
83  // retain (%r0, %r1 : memref<1xf32>, memref<2xf32>)
84  /// return %0#0, %0#1 : i1, i1
85  /// ```
86  /// ```mlir
87  /// %m_base_pointer = memref.extract_aligned_pointer_as_index %m
88  /// %r0_base_pointer = memref.extract_aligned_pointer_as_index %r0
89  /// %r0_does_not_alias = arith.cmpi ne, %m_base_pointer, %r0_base_pointer
90  /// %r1_base_pointer = memref.extract_aligned_pointer_as_index %r1
91  /// %r1_does_not_alias = arith.cmpi ne, %m_base_pointer, %r1_base_pointer
92  /// %not_retained = arith.andi %r0_does_not_alias, %r1_does_not_alias : i1
93  /// %should_dealloc = arith.andi %not_retained, %cond : i1
94  /// scf.if %should_dealloc {
95  /// memref.dealloc %m : memref<2xf32>
96  /// }
97  /// %true = arith.constant true
98  /// %r0_does_alias = arith.xori %r0_does_not_alias, %true : i1
99  /// %r0_ownership = arith.andi %r0_does_alias, %cond : i1
100  /// %r1_does_alias = arith.xori %r1_does_not_alias, %true : i1
101  /// %r1_ownership = arith.andi %r1_does_alias, %cond : i1
102  /// return %r0_ownership, %r1_ownership : i1, i1
103  /// ```
104  LogicalResult rewriteOneMemrefMultipleRetainCase(
105  bufferization::DeallocOp op, OpAdaptor adaptor,
106  ConversionPatternRewriter &rewriter) const {
107  assert(adaptor.getMemrefs().size() == 1 && "expected only one memref");
108 
109  // Compute the base pointer indices, compare all retained indices to the
110  // memref index to check if they alias.
111  SmallVector<Value> doesNotAliasList;
112  Value memrefAsIdx = rewriter.create<memref::ExtractAlignedPointerAsIndexOp>(
113  op->getLoc(), adaptor.getMemrefs()[0]);
114  for (Value retained : adaptor.getRetained()) {
115  Value retainedAsIdx =
116  rewriter.create<memref::ExtractAlignedPointerAsIndexOp>(op->getLoc(),
117  retained);
118  Value doesNotAlias = rewriter.create<arith::CmpIOp>(
119  op->getLoc(), arith::CmpIPredicate::ne, memrefAsIdx, retainedAsIdx);
120  doesNotAliasList.push_back(doesNotAlias);
121  }
122 
123  // AND-reduce the list of booleans from above.
124  Value prev = doesNotAliasList.front();
125  for (Value doesNotAlias : ArrayRef(doesNotAliasList).drop_front())
126  prev = rewriter.create<arith::AndIOp>(op->getLoc(), prev, doesNotAlias);
127 
128  // Also consider the condition given by the dealloc operation and perform a
129  // conditional deallocation guarded by that value.
130  Value shouldDealloc = rewriter.create<arith::AndIOp>(
131  op->getLoc(), prev, adaptor.getConditions()[0]);
132 
133  rewriter.create<scf::IfOp>(
134  op.getLoc(), shouldDealloc, [&](OpBuilder &builder, Location loc) {
135  builder.create<memref::DeallocOp>(loc, adaptor.getMemrefs()[0]);
136  builder.create<scf::YieldOp>(loc);
137  });
138 
139  // Compute the replacement values for the dealloc operation results. This
140  // inserts an already canonicalized form of
141  // `select(does_alias_with_memref(r), memref_cond, false)` for each retained
142  // value r.
143  SmallVector<Value> replacements;
144  Value trueVal = rewriter.create<arith::ConstantOp>(
145  op->getLoc(), rewriter.getBoolAttr(true));
146  for (Value doesNotAlias : doesNotAliasList) {
147  Value aliases =
148  rewriter.create<arith::XOrIOp>(op->getLoc(), doesNotAlias, trueVal);
149  Value result = rewriter.create<arith::AndIOp>(op->getLoc(), aliases,
150  adaptor.getConditions()[0]);
151  replacements.push_back(result);
152  }
153 
154  rewriter.replaceOp(op, replacements);
155 
156  return success();
157  }
158 
159  /// Lowering that supports all features the dealloc operation has to offer. It
160  /// computes the base pointer of each memref (as an index), stores it in a
161  /// new memref helper structure and passes it to the helper function generated
162  /// in 'buildDeallocationHelperFunction'. The results are stored in two lists
163  /// (represented as memrefs) of booleans passed as arguments. The first list
164  /// stores whether the corresponding condition should be deallocated, the
165  /// second list stores the ownership of the retained values which can be used
166  /// to replace the result values of the `bufferization.dealloc` operation.
167  ///
168  /// Example:
169  /// ```
170  /// %0:2 = bufferization.dealloc (%m0, %m1 : memref<2xf32>, memref<5xf32>)
171  /// if (%cond0, %cond1)
172  /// retain (%r0, %r1 : memref<1xf32>, memref<2xf32>)
173  /// ```
174  /// lowers to (simplified):
175  /// ```
176  /// %c0 = arith.constant 0 : index
177  /// %c1 = arith.constant 1 : index
178  /// %dealloc_base_pointer_list = memref.alloc() : memref<2xindex>
179  /// %cond_list = memref.alloc() : memref<2xi1>
180  /// %retain_base_pointer_list = memref.alloc() : memref<2xindex>
181  /// %m0_base_pointer = memref.extract_aligned_pointer_as_index %m0
182  /// memref.store %m0_base_pointer, %dealloc_base_pointer_list[%c0]
183  /// %m1_base_pointer = memref.extract_aligned_pointer_as_index %m1
184  /// memref.store %m1_base_pointer, %dealloc_base_pointer_list[%c1]
185  /// memref.store %cond0, %cond_list[%c0]
186  /// memref.store %cond1, %cond_list[%c1]
187  /// %r0_base_pointer = memref.extract_aligned_pointer_as_index %r0
188  /// memref.store %r0_base_pointer, %retain_base_pointer_list[%c0]
189  /// %r1_base_pointer = memref.extract_aligned_pointer_as_index %r1
190  /// memref.store %r1_base_pointer, %retain_base_pointer_list[%c1]
191  /// %dyn_dealloc_base_pointer_list = memref.cast %dealloc_base_pointer_list :
192  /// memref<2xindex> to memref<?xindex>
193  /// %dyn_cond_list = memref.cast %cond_list : memref<2xi1> to memref<?xi1>
194  /// %dyn_retain_base_pointer_list = memref.cast %retain_base_pointer_list :
195  /// memref<2xindex> to memref<?xindex>
196  /// %dealloc_cond_out = memref.alloc() : memref<2xi1>
197  /// %ownership_out = memref.alloc() : memref<2xi1>
198  /// %dyn_dealloc_cond_out = memref.cast %dealloc_cond_out :
199  /// memref<2xi1> to memref<?xi1>
200  /// %dyn_ownership_out = memref.cast %ownership_out :
201  /// memref<2xi1> to memref<?xi1>
202  /// call @dealloc_helper(%dyn_dealloc_base_pointer_list,
203  /// %dyn_retain_base_pointer_list,
204  /// %dyn_cond_list,
205  /// %dyn_dealloc_cond_out,
206  /// %dyn_ownership_out) : (...)
207  /// %m0_dealloc_cond = memref.load %dyn_dealloc_cond_out[%c0] : memref<2xi1>
208  /// scf.if %m0_dealloc_cond {
209  /// memref.dealloc %m0 : memref<2xf32>
210  /// }
211  /// %m1_dealloc_cond = memref.load %dyn_dealloc_cond_out[%c1] : memref<2xi1>
212  /// scf.if %m1_dealloc_cond {
213  /// memref.dealloc %m1 : memref<5xf32>
214  /// }
215  /// %r0_ownership = memref.load %dyn_ownership_out[%c0] : memref<2xi1>
216  /// %r1_ownership = memref.load %dyn_ownership_out[%c1] : memref<2xi1>
217  /// memref.dealloc %dealloc_base_pointer_list : memref<2xindex>
218  /// memref.dealloc %retain_base_pointer_list : memref<2xindex>
219  /// memref.dealloc %cond_list : memref<2xi1>
220  /// memref.dealloc %dealloc_cond_out : memref<2xi1>
221  /// memref.dealloc %ownership_out : memref<2xi1>
222  /// // replace %0#0 with %r0_ownership
223  /// // replace %0#1 with %r1_ownership
224  /// ```
225  LogicalResult rewriteGeneralCase(bufferization::DeallocOp op,
226  OpAdaptor adaptor,
227  ConversionPatternRewriter &rewriter) const {
228  // Allocate two memrefs holding the base pointer indices of the list of
229  // memrefs to be deallocated and the ones to be retained. These can then be
230  // passed to the helper function and the for-loops can iterate over them.
231  // Without storing them to memrefs, we could not use for-loops but only a
232  // completely unrolled version of it, potentially leading to code-size
233  // blow-up.
234  Value toDeallocMemref = rewriter.create<memref::AllocOp>(
235  op.getLoc(), MemRefType::get({(int64_t)adaptor.getMemrefs().size()},
236  rewriter.getIndexType()));
237  Value conditionMemref = rewriter.create<memref::AllocOp>(
238  op.getLoc(), MemRefType::get({(int64_t)adaptor.getConditions().size()},
239  rewriter.getI1Type()));
240  Value toRetainMemref = rewriter.create<memref::AllocOp>(
241  op.getLoc(), MemRefType::get({(int64_t)adaptor.getRetained().size()},
242  rewriter.getIndexType()));
243 
244  auto getConstValue = [&](uint64_t value) -> Value {
245  return rewriter.create<arith::ConstantOp>(op.getLoc(),
246  rewriter.getIndexAttr(value));
247  };
248 
249  // Extract the base pointers of the memrefs as indices to check for aliasing
250  // at runtime.
251  for (auto [i, toDealloc] : llvm::enumerate(adaptor.getMemrefs())) {
252  Value memrefAsIdx =
253  rewriter.create<memref::ExtractAlignedPointerAsIndexOp>(op.getLoc(),
254  toDealloc);
255  rewriter.create<memref::StoreOp>(op.getLoc(), memrefAsIdx,
256  toDeallocMemref, getConstValue(i));
257  }
258 
259  for (auto [i, cond] : llvm::enumerate(adaptor.getConditions()))
260  rewriter.create<memref::StoreOp>(op.getLoc(), cond, conditionMemref,
261  getConstValue(i));
262 
263  for (auto [i, toRetain] : llvm::enumerate(adaptor.getRetained())) {
264  Value memrefAsIdx =
265  rewriter.create<memref::ExtractAlignedPointerAsIndexOp>(op.getLoc(),
266  toRetain);
267  rewriter.create<memref::StoreOp>(op.getLoc(), memrefAsIdx, toRetainMemref,
268  getConstValue(i));
269  }
270 
271  // Cast the allocated memrefs to dynamic shape because we want only one
272  // helper function no matter how many operands the bufferization.dealloc
273  // has.
274  Value castedDeallocMemref = rewriter.create<memref::CastOp>(
275  op->getLoc(),
276  MemRefType::get({ShapedType::kDynamic}, rewriter.getIndexType()),
277  toDeallocMemref);
278  Value castedCondsMemref = rewriter.create<memref::CastOp>(
279  op->getLoc(),
280  MemRefType::get({ShapedType::kDynamic}, rewriter.getI1Type()),
281  conditionMemref);
282  Value castedRetainMemref = rewriter.create<memref::CastOp>(
283  op->getLoc(),
284  MemRefType::get({ShapedType::kDynamic}, rewriter.getIndexType()),
285  toRetainMemref);
286 
287  Value deallocCondsMemref = rewriter.create<memref::AllocOp>(
288  op.getLoc(), MemRefType::get({(int64_t)adaptor.getMemrefs().size()},
289  rewriter.getI1Type()));
290  Value retainCondsMemref = rewriter.create<memref::AllocOp>(
291  op.getLoc(), MemRefType::get({(int64_t)adaptor.getRetained().size()},
292  rewriter.getI1Type()));
293 
294  Value castedDeallocCondsMemref = rewriter.create<memref::CastOp>(
295  op->getLoc(),
296  MemRefType::get({ShapedType::kDynamic}, rewriter.getI1Type()),
297  deallocCondsMemref);
298  Value castedRetainCondsMemref = rewriter.create<memref::CastOp>(
299  op->getLoc(),
300  MemRefType::get({ShapedType::kDynamic}, rewriter.getI1Type()),
301  retainCondsMemref);
302 
304  rewriter.create<func::CallOp>(
305  op.getLoc(), deallocHelperFuncMap.lookup(symtableOp),
306  SmallVector<Value>{castedDeallocMemref, castedRetainMemref,
307  castedCondsMemref, castedDeallocCondsMemref,
308  castedRetainCondsMemref});
309 
310  for (unsigned i = 0, e = adaptor.getMemrefs().size(); i < e; ++i) {
311  Value idxValue = getConstValue(i);
312  Value shouldDealloc = rewriter.create<memref::LoadOp>(
313  op.getLoc(), deallocCondsMemref, idxValue);
314  rewriter.create<scf::IfOp>(
315  op.getLoc(), shouldDealloc, [&](OpBuilder &builder, Location loc) {
316  builder.create<memref::DeallocOp>(loc, adaptor.getMemrefs()[i]);
317  builder.create<scf::YieldOp>(loc);
318  });
319  }
320 
321  SmallVector<Value> replacements;
322  for (unsigned i = 0, e = adaptor.getRetained().size(); i < e; ++i) {
323  Value idxValue = getConstValue(i);
324  Value ownership = rewriter.create<memref::LoadOp>(
325  op.getLoc(), retainCondsMemref, idxValue);
326  replacements.push_back(ownership);
327  }
328 
329  // Deallocate above allocated memrefs again to avoid memory leaks.
330  // Deallocation will not be run on code after this stage.
331  rewriter.create<memref::DeallocOp>(op.getLoc(), toDeallocMemref);
332  rewriter.create<memref::DeallocOp>(op.getLoc(), toRetainMemref);
333  rewriter.create<memref::DeallocOp>(op.getLoc(), conditionMemref);
334  rewriter.create<memref::DeallocOp>(op.getLoc(), deallocCondsMemref);
335  rewriter.create<memref::DeallocOp>(op.getLoc(), retainCondsMemref);
336 
337  rewriter.replaceOp(op, replacements);
338  return success();
339  }
340 
341 public:
342  DeallocOpConversion(
343  MLIRContext *context,
344  const bufferization::DeallocHelperMap &deallocHelperFuncMap)
345  : OpConversionPattern<bufferization::DeallocOp>(context),
346  deallocHelperFuncMap(deallocHelperFuncMap) {}
347 
348  LogicalResult
349  matchAndRewrite(bufferization::DeallocOp op, OpAdaptor adaptor,
350  ConversionPatternRewriter &rewriter) const override {
351  // Lower the trivial case.
352  if (adaptor.getMemrefs().empty()) {
353  Value falseVal = rewriter.create<arith::ConstantOp>(
354  op.getLoc(), rewriter.getBoolAttr(false));
355  rewriter.replaceOp(
356  op, SmallVector<Value>(adaptor.getRetained().size(), falseVal));
357  return success();
358  }
359 
360  if (adaptor.getMemrefs().size() == 1 && adaptor.getRetained().empty())
361  return rewriteOneMemrefNoRetainCase(op, adaptor, rewriter);
362 
363  if (adaptor.getMemrefs().size() == 1)
364  return rewriteOneMemrefMultipleRetainCase(op, adaptor, rewriter);
365 
367  if (!deallocHelperFuncMap.contains(symtableOp))
368  return op->emitError(
369  "library function required for generic lowering, but cannot be "
370  "automatically inserted when operating on functions");
371 
372  return rewriteGeneralCase(op, adaptor, rewriter);
373  }
374 
375 private:
376  const bufferization::DeallocHelperMap &deallocHelperFuncMap;
377 };
378 } // namespace
379 
380 namespace {
381 struct LowerDeallocationsPass
382  : public bufferization::impl::LowerDeallocationsBase<
383  LowerDeallocationsPass> {
384  void runOnOperation() override {
385  if (!isa<ModuleOp, FunctionOpInterface>(getOperation())) {
386  emitError(getOperation()->getLoc(),
387  "root operation must be a builtin.module or a function");
388  signalPassFailure();
389  return;
390  }
391 
392  bufferization::DeallocHelperMap deallocHelperFuncMap;
393  if (auto module = dyn_cast<ModuleOp>(getOperation())) {
394  OpBuilder builder =
395  OpBuilder::atBlockBegin(&module.getBodyRegion().front());
396 
397  // Build dealloc helper function if there are deallocs.
398  getOperation()->walk([&](bufferization::DeallocOp deallocOp) {
399  Operation *symtableOp =
401  if (deallocOp.getMemrefs().size() > 1 &&
402  !deallocHelperFuncMap.contains(symtableOp)) {
403  SymbolTable symbolTable(symtableOp);
404  func::FuncOp helperFuncOp =
406  builder, getOperation()->getLoc(), symbolTable);
407  deallocHelperFuncMap[symtableOp] = helperFuncOp;
408  }
409  });
410  }
411 
412  RewritePatternSet patterns(&getContext());
414  patterns, deallocHelperFuncMap);
415 
416  ConversionTarget target(getContext());
417  target.addLegalDialect<memref::MemRefDialect, arith::ArithDialect,
418  scf::SCFDialect, func::FuncDialect>();
419  target.addIllegalOp<bufferization::DeallocOp>();
420 
421  if (failed(applyPartialConversion(getOperation(), target,
422  std::move(patterns))))
423  signalPassFailure();
424  }
425 };
426 } // namespace
427 
429  OpBuilder &builder, Location loc, SymbolTable &symbolTable) {
430  Type indexMemrefType =
431  MemRefType::get({ShapedType::kDynamic}, builder.getIndexType());
432  Type boolMemrefType =
433  MemRefType::get({ShapedType::kDynamic}, builder.getI1Type());
434  SmallVector<Type> argTypes{indexMemrefType, indexMemrefType, boolMemrefType,
435  boolMemrefType, boolMemrefType};
436  builder.clearInsertionPoint();
437 
438  // Generate the func operation itself.
439  auto helperFuncOp = func::FuncOp::create(
440  loc, "dealloc_helper", builder.getFunctionType(argTypes, {}));
441  helperFuncOp.setVisibility(SymbolTable::Visibility::Private);
442  symbolTable.insert(helperFuncOp);
443  auto &block = helperFuncOp.getFunctionBody().emplaceBlock();
444  block.addArguments(argTypes, SmallVector<Location>(argTypes.size(), loc));
445 
446  builder.setInsertionPointToStart(&block);
447  Value toDeallocMemref = helperFuncOp.getArguments()[0];
448  Value toRetainMemref = helperFuncOp.getArguments()[1];
449  Value conditionMemref = helperFuncOp.getArguments()[2];
450  Value deallocCondsMemref = helperFuncOp.getArguments()[3];
451  Value retainCondsMemref = helperFuncOp.getArguments()[4];
452 
453  // Insert some prerequisites.
454  Value c0 = builder.create<arith::ConstantOp>(loc, builder.getIndexAttr(0));
455  Value c1 = builder.create<arith::ConstantOp>(loc, builder.getIndexAttr(1));
456  Value trueValue =
457  builder.create<arith::ConstantOp>(loc, builder.getBoolAttr(true));
458  Value falseValue =
459  builder.create<arith::ConstantOp>(loc, builder.getBoolAttr(false));
460  Value toDeallocSize = builder.create<memref::DimOp>(loc, toDeallocMemref, c0);
461  Value toRetainSize = builder.create<memref::DimOp>(loc, toRetainMemref, c0);
462 
463  builder.create<scf::ForOp>(
464  loc, c0, toRetainSize, c1, std::nullopt,
465  [&](OpBuilder &builder, Location loc, Value i, ValueRange iterArgs) {
466  builder.create<memref::StoreOp>(loc, falseValue, retainCondsMemref, i);
467  builder.create<scf::YieldOp>(loc);
468  });
469 
470  builder.create<scf::ForOp>(
471  loc, c0, toDeallocSize, c1, std::nullopt,
472  [&](OpBuilder &builder, Location loc, Value outerIter,
473  ValueRange iterArgs) {
474  Value toDealloc =
475  builder.create<memref::LoadOp>(loc, toDeallocMemref, outerIter);
476  Value cond =
477  builder.create<memref::LoadOp>(loc, conditionMemref, outerIter);
478 
479  // Build the first for loop that computes aliasing with retained
480  // memrefs.
481  Value noRetainAlias =
482  builder
483  .create<scf::ForOp>(
484  loc, c0, toRetainSize, c1, trueValue,
485  [&](OpBuilder &builder, Location loc, Value i,
486  ValueRange iterArgs) {
487  Value retainValue = builder.create<memref::LoadOp>(
488  loc, toRetainMemref, i);
489  Value doesAlias = builder.create<arith::CmpIOp>(
490  loc, arith::CmpIPredicate::eq, retainValue,
491  toDealloc);
492  builder.create<scf::IfOp>(
493  loc, doesAlias,
494  [&](OpBuilder &builder, Location loc) {
495  Value retainCondValue =
496  builder.create<memref::LoadOp>(
497  loc, retainCondsMemref, i);
498  Value aggregatedRetainCond =
499  builder.create<arith::OrIOp>(
500  loc, retainCondValue, cond);
501  builder.create<memref::StoreOp>(
502  loc, aggregatedRetainCond, retainCondsMemref,
503  i);
504  builder.create<scf::YieldOp>(loc);
505  });
506  Value doesntAlias = builder.create<arith::CmpIOp>(
507  loc, arith::CmpIPredicate::ne, retainValue,
508  toDealloc);
509  Value yieldValue = builder.create<arith::AndIOp>(
510  loc, iterArgs[0], doesntAlias);
511  builder.create<scf::YieldOp>(loc, yieldValue);
512  })
513  .getResult(0);
514 
515  // Build the second for loop that adds aliasing with previously
516  // deallocated memrefs.
517  Value noAlias =
518  builder
519  .create<scf::ForOp>(
520  loc, c0, outerIter, c1, noRetainAlias,
521  [&](OpBuilder &builder, Location loc, Value i,
522  ValueRange iterArgs) {
523  Value prevDeallocValue = builder.create<memref::LoadOp>(
524  loc, toDeallocMemref, i);
525  Value doesntAlias = builder.create<arith::CmpIOp>(
526  loc, arith::CmpIPredicate::ne, prevDeallocValue,
527  toDealloc);
528  Value yieldValue = builder.create<arith::AndIOp>(
529  loc, iterArgs[0], doesntAlias);
530  builder.create<scf::YieldOp>(loc, yieldValue);
531  })
532  .getResult(0);
533 
534  Value shouldDealoc = builder.create<arith::AndIOp>(loc, noAlias, cond);
535  builder.create<memref::StoreOp>(loc, shouldDealoc, deallocCondsMemref,
536  outerIter);
537  builder.create<scf::YieldOp>(loc);
538  });
539 
540  builder.create<func::ReturnOp>(loc);
541  return helperFuncOp;
542 }
543 
545  RewritePatternSet &patterns,
546  const bufferization::DeallocHelperMap &deallocHelperFuncMap) {
547  patterns.add<DeallocOpConversion>(patterns.getContext(),
548  deallocHelperFuncMap);
549 }
550 
552  return std::make_unique<LowerDeallocationsPass>();
553 }
static MLIRContext * getContext(OpFoldResult val)
IntegerAttr getIndexAttr(int64_t value)
Definition: Builders.cpp:128
FunctionType getFunctionType(TypeRange inputs, TypeRange results)
Definition: Builders.cpp:100
BoolAttr getBoolAttr(bool value)
Definition: Builders.cpp:120
IntegerType getI1Type()
Definition: Builders.cpp:77
IndexType getIndexType()
Definition: Builders.cpp:75
This class implements a pattern rewriter for use with ConversionPatterns.
void replaceOp(Operation *op, ValueRange newValues) override
PatternRewriter hook for replacing an operation.
This class describes a specific conversion target.
This class defines the main interface for locations in MLIR and acts as a non-nullable wrapper around...
Definition: Location.h:63
MLIRContext is the top-level object for a collection of MLIR operations.
Definition: MLIRContext.h:60
This class helps build Operations.
Definition: Builders.h:210
static OpBuilder atBlockBegin(Block *block, Listener *listener=nullptr)
Create a builder and set the insertion point to before the first operation in the block but still ins...
Definition: Builders.h:243
void clearInsertionPoint()
Reset the insertion point to no location.
Definition: Builders.h:382
void setInsertionPointToStart(Block *block)
Sets the insertion point to the start of the specified block.
Definition: Builders.h:434
Operation * create(const OperationState &state)
Creates an operation given the fields represented as an OperationState.
Definition: Builders.cpp:468
OpConversionPattern is a wrapper around ConversionPattern that allows for matching and rewriting agai...
A trait used to provide symbol table functionalities to a region operation.
Definition: SymbolTable.h:435
Operation is the basic unit of execution within MLIR.
Definition: Operation.h:88
Location getLoc()
The source location the operation was defined or derived from.
Definition: Operation.h:223
InFlightDiagnostic emitError(const Twine &message={})
Emit an error about fatal conditions with this operation, reporting up to any diagnostic handlers tha...
Definition: Operation.cpp:268
Operation * getParentWithTrait()
Returns the closest surrounding parent operation with trait Trait.
Definition: Operation.h:248
MLIRContext * getContext() const
Definition: PatternMatch.h:823
RewritePatternSet & add(ConstructorArg &&arg, ConstructorArgs &&...args)
Add an instance of each of the pattern types 'Ts' to the pattern list with the given arguments.
Definition: PatternMatch.h:847
OpTy replaceOpWithNewOp(Operation *op, Args &&...args)
Replace the results of the given (original) op with a new op that is created without verification (re...
Definition: PatternMatch.h:536
This class allows for representing and managing the symbol table used by operations with the 'SymbolT...
Definition: SymbolTable.h:24
@ Private
The symbol is private and may only be referenced by SymbolRefAttrs local to the operations within the...
StringAttr insert(Operation *symbol, Block::iterator insertPt={})
Insert a new symbol into the table, and rename it as necessary to avoid collisions.
Instances of the Type class are uniqued, have an immutable identifier and an optional mutable compone...
Definition: Types.h:74
This class provides an abstraction over the different types of ranges over Values.
Definition: ValueRange.h:381
This class represents an instance of an SSA value in the MLIR system, representing a computable value...
Definition: Value.h:96
std::unique_ptr< Pass > createLowerDeallocationsPass()
Creates an instance of the LowerDeallocations pass to lower bufferization.dealloc operations to the m...
void populateBufferizationDeallocLoweringPattern(RewritePatternSet &patterns, const DeallocHelperMap &deallocHelperFuncMap)
Adds the conversion pattern of the bufferization.dealloc operation to the given pattern set for use i...
func::FuncOp buildDeallocationLibraryFunction(OpBuilder &builder, Location loc, SymbolTable &symbolTable)
Construct the library function needed for the fully generic bufferization.dealloc lowering implemente...
constexpr void enumerate(std::tuple< Tys... > &tuple, CallbackT &&callback)
Definition: Matchers.h:285
Include the generated interface declarations.
InFlightDiagnostic emitError(Location loc)
Utility method to emit an error message using this location.
auto get(MLIRContext *context, Ts &&...params)
Helper method that injects context only if needed, this helps unify some of the attribute constructio...
LogicalResult applyPartialConversion(ArrayRef< Operation * > ops, const ConversionTarget &target, const FrozenRewritePatternSet &patterns, ConversionConfig config=ConversionConfig())
Below we define several entry points for operation conversion.