diff --git a/mlir/include/mlir/Conversion/BufferizationToMemRef/BufferizationToMemRef.h b/mlir/include/mlir/Conversion/BufferizationToMemRef/BufferizationToMemRef.h --- a/mlir/include/mlir/Conversion/BufferizationToMemRef/BufferizationToMemRef.h +++ b/mlir/include/mlir/Conversion/BufferizationToMemRef/BufferizationToMemRef.h @@ -9,21 +9,16 @@ #ifndef MLIR_CONVERSION_BUFFERIZATIONTOMEMREF_BUFFERIZATIONTOMEMREF_H #define MLIR_CONVERSION_BUFFERIZATIONTOMEMREF_BUFFERIZATIONTOMEMREF_H +#include "mlir/Pass/Pass.h" #include namespace mlir { -class Pass; -class RewritePatternSet; +class ModuleOp; #define GEN_PASS_DECL_CONVERTBUFFERIZATIONTOMEMREF #include "mlir/Conversion/Passes.h.inc" -/// Collect a set of patterns to convert memory-related operations from the -/// Bufferization dialect to the MemRef dialect. -void populateBufferizationToMemRefConversionPatterns( - RewritePatternSet &patterns); - -std::unique_ptr createBufferizationToMemRefPass(); +std::unique_ptr> createBufferizationToMemRefPass(); } // namespace mlir #endif // MLIR_CONVERSION_BUFFERIZATIONTOMEMREF_BUFFERIZATIONTOMEMREF_H diff --git a/mlir/include/mlir/Conversion/Passes.td b/mlir/include/mlir/Conversion/Passes.td --- a/mlir/include/mlir/Conversion/Passes.td +++ b/mlir/include/mlir/Conversion/Passes.td @@ -167,7 +167,8 @@ // BufferizationToMemRef //===----------------------------------------------------------------------===// -def ConvertBufferizationToMemRef : Pass<"convert-bufferization-to-memref"> { +def ConvertBufferizationToMemRef : Pass<"convert-bufferization-to-memref", + "mlir::ModuleOp"> { let summary = "Convert operations from the Bufferization dialect to the " "MemRef dialect"; let description = [{ @@ -195,7 +196,10 @@ }]; let constructor = "mlir::createBufferizationToMemRefPass()"; - let dependentDialects = ["arith::ArithDialect", "memref::MemRefDialect"]; + let dependentDialects = [ + "arith::ArithDialect", "memref::MemRefDialect", "scf::SCFDialect", + "func::FuncDialect" + ]; } //===----------------------------------------------------------------------===// diff --git a/mlir/lib/Conversion/BufferizationToMemRef/BufferizationToMemRef.cpp b/mlir/lib/Conversion/BufferizationToMemRef/BufferizationToMemRef.cpp --- a/mlir/lib/Conversion/BufferizationToMemRef/BufferizationToMemRef.cpp +++ b/mlir/lib/Conversion/BufferizationToMemRef/BufferizationToMemRef.cpp @@ -15,7 +15,9 @@ #include "mlir/Dialect/Arith/IR/Arith.h" #include "mlir/Dialect/Bufferization/IR/Bufferization.h" +#include "mlir/Dialect/Func/IR/FuncOps.h" #include "mlir/Dialect/MemRef/IR/MemRef.h" +#include "mlir/Dialect/SCF/IR/SCF.h" #include "mlir/IR/BuiltinTypes.h" #include "mlir/Pass/Pass.h" #include "mlir/Support/LogicalResult.h" @@ -77,12 +79,317 @@ return success(); } }; -} // namespace -void mlir::populateBufferizationToMemRefConversionPatterns( - RewritePatternSet &patterns) { - patterns.add(patterns.getContext()); -} +/// The DeallocOpConversion transforms all bufferization dealloc operations into +/// memref dealloc operations potentially guarded by scf if operations. +/// Additionally, memref extract_aligned_pointer_as_index and arith operations +/// are inserted to compute the guard conditions. We distinguish multiple cases +/// to provide an overall more efficient lowering. In the general case, a helper +/// func is created to avoid quadratic code size explosion (relative to the +/// number of operands of the dealloc operation). For examples of each case, +/// refer to the documentation of the member functions of this class. +class DeallocOpConversion + : public OpConversionPattern { + + /// Lower a simple case avoiding the helper function. Ideally, static analysis + /// can provide enough aliasing information to split the dealloc operations up + /// into this simple case as much as possible before running this pass. + /// + /// Example: + /// ``` + /// %0 = bufferization.dealloc (%arg0 : memref<2xf32>) if (%arg1) + /// ``` + /// is lowered to + /// ``` + /// scf.if %arg1 { + /// memref.dealloc %arg0 : memref<2xf32> + /// } + /// %0 = arith.constant false + /// ``` + LogicalResult + rewriteOneMemrefNoRetainCase(bufferization::DeallocOp op, OpAdaptor adaptor, + ConversionPatternRewriter &rewriter) const { + rewriter.create(op.getLoc(), adaptor.getConditions()[0], + [&](OpBuilder &builder, Location loc) { + builder.create( + loc, adaptor.getMemrefs()[0]); + builder.create(loc); + }); + rewriter.replaceOpWithNewOp(op, + rewriter.getBoolAttr(false)); + return success(); + } + + /// Lowering that supports all features the dealloc operation has to offer. It + /// computes the base pointer of each memref (as an index), stores them in a + /// new memref and passes it to the helper function generated in + /// 'buildDeallocationHelperFunction'. The two return values are used as + /// condition for the scf if operation containing the memref deallocate and as + /// replacement for the original bufferization dealloc respectively. + /// + /// Example: + /// ``` + /// %0:2 = bufferization.dealloc (%arg0, %arg1 : memref<2xf32>, memref<5xf32>) + /// if (%arg3, %arg4) retain (%arg2 : memref<1xf32>) + /// ``` + /// lowers to (simplified): + /// ``` + /// %c0 = arith.constant 0 : index + /// %c1 = arith.constant 1 : index + /// %alloc = memref.alloc() : memref<2xindex> + /// %alloc_0 = memref.alloc() : memref<1xindex> + /// %intptr = memref.extract_aligned_pointer_as_index %arg0 + /// memref.store %intptr, %alloc[%c0] : memref<2xindex> + /// %intptr_1 = memref.extract_aligned_pointer_as_index %arg1 + /// memref.store %intptr_1, %alloc[%c1] : memref<2xindex> + /// %intptr_2 = memref.extract_aligned_pointer_as_index %arg2 + /// memref.store %intptr_2, %alloc_0[%c0] : memref<1xindex> + /// %cast = memref.cast %alloc : memref<2xindex> to memref + /// %cast_4 = memref.cast %alloc_0 : memref<1xindex> to memref + /// %0:2 = call @dealloc_helper(%cast, %cast_4, %c0) + /// %1 = arith.andi %0#0, %arg3 : i1 + /// %2 = arith.andi %0#1, %arg3 : i1 + /// scf.if %1 { + /// memref.dealloc %arg0 : memref<2xf32> + /// } + /// %3:2 = call @dealloc_helper(%cast, %cast_4, %c1) + /// %4 = arith.andi %3#0, %arg4 : i1 + /// %5 = arith.andi %3#1, %arg4 : i1 + /// scf.if %4 { + /// memref.dealloc %arg1 : memref<5xf32> + /// } + /// memref.dealloc %alloc : memref<2xindex> + /// memref.dealloc %alloc_0 : memref<1xindex> + /// // replace %0#0 with %2 + /// // replace %0#1 with %5 + /// ``` + LogicalResult rewriteGeneralCase(bufferization::DeallocOp op, + OpAdaptor adaptor, + ConversionPatternRewriter &rewriter) const { + // Allocate two memrefs holding the base pointer indices of the list of + // memrefs to be deallocated and the ones to be retained. These can then be + // passed to the helper function and the for-loops can iterate over them. + // Without storing them to memrefs, we could not use for-loops but only a + // completely unrolled version of it, potentially leading to code-size + // blow-up. + Value toDeallocMemref = rewriter.create( + op.getLoc(), MemRefType::get({(int64_t)adaptor.getMemrefs().size()}, + rewriter.getIndexType())); + Value toRetainMemref = rewriter.create( + op.getLoc(), MemRefType::get({(int64_t)adaptor.getRetained().size()}, + rewriter.getIndexType())); + + auto getConstValue = [&](uint64_t value) -> Value { + return rewriter.create(op.getLoc(), + rewriter.getIndexAttr(value)); + }; + + // Extract the base pointers of the memrefs as indices to check for aliasing + // at runtime. + for (auto [i, toDealloc] : llvm::enumerate(adaptor.getMemrefs())) { + Value memrefAsIdx = + rewriter.create(op.getLoc(), + toDealloc); + rewriter.create(op.getLoc(), memrefAsIdx, + toDeallocMemref, getConstValue(i)); + } + for (auto [i, toRetain] : llvm::enumerate(adaptor.getRetained())) { + Value memrefAsIdx = + rewriter.create(op.getLoc(), + toRetain); + rewriter.create(op.getLoc(), memrefAsIdx, toRetainMemref, + getConstValue(i)); + } + + // Cast the allocated memrefs to dynamic shape because we want only one + // helper function no matter how many operands the bufferization.dealloc + // has. + Value castedDeallocMemref = rewriter.create( + op->getLoc(), + MemRefType::get({ShapedType::kDynamic}, rewriter.getIndexType()), + toDeallocMemref); + Value castedRetainMemref = rewriter.create( + op->getLoc(), + MemRefType::get({ShapedType::kDynamic}, rewriter.getIndexType()), + toRetainMemref); + + SmallVector replacements; + for (unsigned i = 0, e = adaptor.getMemrefs().size(); i < e; ++i) { + auto callOp = rewriter.create( + op.getLoc(), deallocHelperFunc, + SmallVector{castedDeallocMemref, castedRetainMemref, + getConstValue(i)}); + Value shouldDealloc = rewriter.create( + op.getLoc(), callOp.getResult(0), adaptor.getConditions()[i]); + Value ownership = rewriter.create( + op.getLoc(), callOp.getResult(1), adaptor.getConditions()[i]); + replacements.push_back(ownership); + rewriter.create( + op.getLoc(), shouldDealloc, [&](OpBuilder &builder, Location loc) { + builder.create(loc, adaptor.getMemrefs()[i]); + builder.create(loc); + }); + } + + // Deallocate above allocated memrefs again to avoid memory leaks. + // Deallocation will not be run on code after this stage. + rewriter.create(op.getLoc(), toDeallocMemref); + rewriter.create(op.getLoc(), toRetainMemref); + + rewriter.replaceOp(op, replacements); + return success(); + } + +public: + DeallocOpConversion(MLIRContext *context, func::FuncOp deallocHelperFunc) + : OpConversionPattern(context), + deallocHelperFunc(deallocHelperFunc) {} + + LogicalResult + matchAndRewrite(bufferization::DeallocOp op, OpAdaptor adaptor, + ConversionPatternRewriter &rewriter) const override { + // Lower the trivial case. + if (adaptor.getMemrefs().empty()) + return rewriter.eraseOp(op), success(); + + if (adaptor.getMemrefs().size() == 1 && adaptor.getRetained().empty()) + return rewriteOneMemrefNoRetainCase(op, adaptor, rewriter); + + return rewriteGeneralCase(op, adaptor, rewriter); + } + + /// Build a helper function per compilation unit that can be called at + /// bufferization dealloc sites to determine aliasing and ownership. + /// + /// The generated function takes two memrefs of indices and one index value as + /// arguments and returns two boolean values: + /// * The first memref argument A should contain the result of the + /// extract_aligned_pointer_as_index operation applied to the memrefs to be + /// deallocated + /// * The second memref argument B should contain the result of the + /// extract_aligned_pointer_as_index operation applied to the memrefs to be + /// retained + /// * The index argument I represents the currently processed index of + /// memref A and is needed because aliasing with all previously deallocated + /// memrefs has to be checked to avoid double deallocation + /// * The first result indicates whether the memref at position I should be + /// deallocated + /// * The second result provides the updated ownership value corresponding + /// the the memref at position I + /// + /// This helper function is supposed to be called for each element in the list + /// of memrefs to be deallocated to determine the deallocation need and new + /// ownership indicator, but does not perform the deallocation itself. + /// + /// The first scf for loop in the body computes whether the memref at index I + /// aliases with any memref in the list of retained memrefs. + /// The second loop additionally checks whether one of the previously + /// deallocated memrefs aliases with the currently processed one. + /// + /// Generated code: + /// ``` + /// func.func @dealloc_helper(%arg0: memref, + /// %arg1: memref, + /// %arg2: index) -> (i1, i1) { + /// %c0 = arith.constant 0 : index + /// %c1 = arith.constant 1 : index + /// %true = arith.constant true + /// %dim = memref.dim %arg1, %c0 : memref + /// %0 = memref.load %arg0[%arg2] : memref + /// %1 = scf.for %i = %c0 to %dim step %c1 iter_args(%arg4 = %true) -> (i1){ + /// %4 = memref.load %arg1[%i] : memref + /// %5 = arith.cmpi ne, %4, %0 : index + /// %6 = arith.andi %arg4, %5 : i1 + /// scf.yield %6 : i1 + /// } + /// %2 = scf.for %i = %c0 to %arg2 step %c1 iter_args(%arg4 = %1) -> (i1) { + /// %4 = memref.load %arg0[%i] : memref + /// %5 = arith.cmpi ne, %4, %0 : index + /// %6 = arith.andi %arg4, %5 : i1 + /// scf.yield %6 : i1 + /// } + /// %3 = arith.xori %1, %true : i1 + /// return %2, %3 : i1, i1 + /// } + /// ``` + static func::FuncOp + buildDeallocationHelperFunction(OpBuilder &builder, Location loc, + SymbolTable &symbolTable) { + Type idxType = builder.getIndexType(); + Type memrefArgType = MemRefType::get({ShapedType::kDynamic}, idxType); + SmallVector argTypes{memrefArgType, memrefArgType, idxType}; + builder.clearInsertionPoint(); + + // Generate the func operation itself. + auto helperFuncOp = func::FuncOp::create( + loc, "dealloc_helper", + builder.getFunctionType(argTypes, + {builder.getI1Type(), builder.getI1Type()})); + symbolTable.insert(helperFuncOp); + auto &block = helperFuncOp.getFunctionBody().emplaceBlock(); + block.addArguments(argTypes, SmallVector(argTypes.size(), loc)); + + builder.setInsertionPointToStart(&block); + Value toDeallocMemref = helperFuncOp.getArguments()[0]; + Value toRetainMemref = helperFuncOp.getArguments()[1]; + Value idxArg = helperFuncOp.getArguments()[2]; + + // Insert some prerequisites. + Value c0 = builder.create(loc, builder.getIndexAttr(0)); + Value c1 = builder.create(loc, builder.getIndexAttr(1)); + Value trueValue = + builder.create(loc, builder.getBoolAttr(true)); + Value toRetainSize = builder.create(loc, toRetainMemref, c0); + Value toDealloc = + builder.create(loc, toDeallocMemref, idxArg); + + // Build the first for loop that computes aliasing with retained memrefs. + Value noRetainAlias = + builder + .create( + loc, c0, toRetainSize, c1, trueValue, + [&](OpBuilder &builder, Location loc, Value i, + ValueRange iterArgs) { + Value retainValue = + builder.create(loc, toRetainMemref, i); + Value doesntAlias = builder.create( + loc, arith::CmpIPredicate::ne, retainValue, toDealloc); + Value yieldValue = builder.create( + loc, iterArgs[0], doesntAlias); + builder.create(loc, yieldValue); + }) + .getResult(0); + + // Build the second for loop that adds aliasing with previously deallocated + // memrefs. + Value noAlias = + builder + .create( + loc, c0, idxArg, c1, noRetainAlias, + [&](OpBuilder &builder, Location loc, Value i, + ValueRange iterArgs) { + Value prevDeallocValue = + builder.create(loc, toDeallocMemref, i); + Value doesntAlias = builder.create( + loc, arith::CmpIPredicate::ne, prevDeallocValue, + toDealloc); + Value yieldValue = builder.create( + loc, iterArgs[0], doesntAlias); + builder.create(loc, yieldValue); + }) + .getResult(0); + + Value ownership = + builder.create(loc, noRetainAlias, trueValue); + builder.create(loc, SmallVector{noAlias, ownership}); + + return helperFuncOp; + } + +private: + func::FuncOp deallocHelperFunc; +}; +} // namespace namespace { struct BufferizationToMemRefPass @@ -90,12 +397,30 @@ BufferizationToMemRefPass() = default; void runOnOperation() override { + ModuleOp module = cast(getOperation()); + OpBuilder builder = + OpBuilder::atBlockBegin(&module.getBodyRegion().front()); + SymbolTable symbolTable(module); + + // Build dealloc helper function if there are deallocs. + func::FuncOp helperFuncOp; + getOperation()->walk([&](bufferization::DeallocOp deallocOp) { + if (deallocOp.getMemrefs().size() > 1 || + !deallocOp.getRetained().empty()) { + helperFuncOp = DeallocOpConversion::buildDeallocationHelperFunction( + builder, getOperation()->getLoc(), symbolTable); + return WalkResult::interrupt(); + } + return WalkResult::advance(); + }); + RewritePatternSet patterns(&getContext()); - populateBufferizationToMemRefConversionPatterns(patterns); + patterns.add(patterns.getContext()); + patterns.add(patterns.getContext(), helperFuncOp); ConversionTarget target(getContext()); - target.addLegalDialect(); - target.addLegalOp(); + target.addLegalDialect(); target.addIllegalDialect(); if (failed(applyPartialConversion(getOperation(), target, @@ -105,6 +430,7 @@ }; } // namespace -std::unique_ptr mlir::createBufferizationToMemRefPass() { +std::unique_ptr> +mlir::createBufferizationToMemRefPass() { return std::make_unique(); } diff --git a/mlir/lib/Conversion/BufferizationToMemRef/CMakeLists.txt b/mlir/lib/Conversion/BufferizationToMemRef/CMakeLists.txt --- a/mlir/lib/Conversion/BufferizationToMemRef/CMakeLists.txt +++ b/mlir/lib/Conversion/BufferizationToMemRef/CMakeLists.txt @@ -9,6 +9,10 @@ LINK_LIBS PUBLIC MLIRBufferizationDialect + MLIRSCFDialect + MLIRFuncDialect + MLIRArithDialect + MLIRMemRefDialect MLIRPass MLIRTransforms ) diff --git a/mlir/test/Conversion/BufferizationToMemRef/bufferization-to-memref.mlir b/mlir/test/Conversion/BufferizationToMemRef/bufferization-to-memref.mlir --- a/mlir/test/Conversion/BufferizationToMemRef/bufferization-to-memref.mlir +++ b/mlir/test/Conversion/BufferizationToMemRef/bufferization-to-memref.mlir @@ -66,3 +66,93 @@ memref.dealloc %arg0 : memref> return %1 : memref> } +// ----- + +// CHECK-LABEL: func @conversion_dealloc_empty +func.func @conversion_dealloc_empty() { + // CHECK-NEXT: return + bufferization.dealloc + return +} + +// ----- + +// CHECK-NOT: func @deallocHelper +// CHECK-LABEL: func @conversion_dealloc_simple +// CHECK-SAME: [[ARG0:%.+]]: memref<2xf32> +// CHECK-SAME: [[ARG1:%.+]]: i1 +func.func @conversion_dealloc_simple(%arg0: memref<2xf32>, %arg1: i1) -> i1 { + %0 = bufferization.dealloc (%arg0 : memref<2xf32>) if (%arg1) + return %0 : i1 +} + +// CHECk: scf.if [[ARG1]] { +// CHECk-NEXT: memref.dealloc [[ARG0]] : memref<2xf32> +// CHECk-NEXT: } +// CHECk-NEXT: [[FALSE:%.+]] = arith.constant false +// CHECk-NEXT: return [[FALSE]] : i1 + +// ----- + +func.func @conversion_dealloc_multiple_memrefs_and_retained(%arg0: memref<2xf32>, %arg1: memref<5xf32>, %arg2: memref<1xf32>, %arg3: i1, %arg4: i1) -> (i1, i1) { + %0:2 = bufferization.dealloc (%arg0, %arg1 : memref<2xf32>, memref<5xf32>) if (%arg3, %arg4) retain (%arg2 : memref<1xf32>) + return %0#0, %0#1 : i1, i1 +} + +// CHECK-LABEL: func @conversion_dealloc_multiple_memrefs_and_retained +// CHECK-SAME: [[ARG0:%.+]]: memref<2xf32>, +// CHECK-SAME: [[ARG1:%.+]]: memref<5xf32>, +// CHECK-SAME: [[ARG2:%.+]]: memref<1xf32>, +// CHECK-SAME: [[ARG3:%.+]]: i1, +// CHECK-SAME: [[ARG4:%.+]]: i1 +// CHECK: [[TO_DEALLOC_MR:%.+]] = memref.alloc() : memref<2xindex> +// CHECK: [[TO_RETAIN_MR:%.+]] = memref.alloc() : memref<1xindex> +// CHECK-DAG: [[V0:%.+]] = memref.extract_aligned_pointer_as_index [[ARG0]] +// CHECK-DAG: [[C0:%.+]] = arith.constant 0 : index +// CHECK-DAG: memref.store [[V0]], [[TO_DEALLOC_MR]][[[C0]]] +// CHECK-DAG: [[V1:%.+]] = memref.extract_aligned_pointer_as_index [[ARG1]] +// CHECK-DAG: [[C1:%.+]] = arith.constant 1 : index +// CHECK-DAG: memref.store [[V1]], [[TO_DEALLOC_MR]][[[C1]]] +// CHECK-DAG: [[V2:%.+]] = memref.extract_aligned_pointer_as_index [[ARG2]] +// CHECK-DAG: [[C0:%.+]] = arith.constant 0 : index +// CHECK-DAG: memref.store [[V2]], [[TO_RETAIN_MR]][[[C0]]] +// CHECK-DAG: [[CAST_DEALLOC:%.+]] = memref.cast [[TO_DEALLOC_MR]] : memref<2xindex> to memref +// CHECK-DAG: [[CAST_RETAIN:%.+]] = memref.cast [[TO_RETAIN_MR]] : memref<1xindex> to memref +// CHECK-DAG: [[C0:%.+]] = arith.constant 0 : index +// CHECK: [[RES0:%.+]]:2 = call @dealloc_helper([[CAST_DEALLOC]], [[CAST_RETAIN]], [[C0]]) +// CHECK: [[SHOULD_DEALLOC_0:%.+]] = arith.andi [[RES0]]#0, [[ARG3]] +// CHECK: [[OWNERSHIP0:%.+]] = arith.andi [[RES0]]#1, [[ARG3]] +// CHECK: scf.if [[SHOULD_DEALLOC_0]] { +// CHECK: memref.dealloc %arg0 +// CHECK: } +// CHECK: [[C1:%.+]] = arith.constant 1 : index +// CHECK: [[RES1:%.+]]:2 = call @dealloc_helper([[CAST_DEALLOC]], [[CAST_RETAIN]], [[C1]]) +// CHECK: [[SHOULD_DEALLOC_1:%.+]] = arith.andi [[RES1:%.+]]#0, [[ARG4]] +// CHECK: [[OWNERSHIP1:%.+]] = arith.andi [[RES1:%.+]]#1, [[ARG4]] +// CHECK: scf.if [[SHOULD_DEALLOC_1]] +// CHECK: memref.dealloc [[ARG1]] +// CHECK: } +// CHECK: memref.dealloc [[TO_DEALLOC_MR]] +// CHECK: memref.dealloc [[TO_RETAIN_MR]] +// CHECK: return [[OWNERSHIP0]], [[OWNERSHIP1]] + +// CHECK: func @dealloc_helper +// CHECK-SAME: [[ARG0:%.+]]: memref, [[ARG1:%.+]]: memref +// CHECK-SAME: [[ARG2:%.+]]: index +// CHECK-SAME: -> (i1, i1) +// CHECK: [[TO_RETAIN_SIZE:%.+]] = memref.dim [[ARG1]], %c0 +// CHECK: [[TO_DEALLOC:%.+]] = memref.load [[ARG0]][[[ARG2]]] : memref +// CHECK-NEXT: [[NO_RETAIN_ALIAS:%.+]] = scf.for [[ITER:%.+]] = %c0 to [[TO_RETAIN_SIZE]] step %c1 iter_args([[ITER_ARG:%.+]] = %true) -> (i1) { +// CHECK-NEXT: [[RETAIN_VAL:%.+]] = memref.load [[ARG1]][[[ITER]]] : memref +// CHECK-NEXT: [[DOES_ALIAS:%.+]] = arith.cmpi ne, [[RETAIN_VAL]], [[TO_DEALLOC]] : index +// CHECK-NEXT: [[AGG_DOES_ALIAS:%.+]] = arith.andi [[ITER_ARG]], [[DOES_ALIAS]] : i1 +// CHECK-NEXT: scf.yield [[AGG_DOES_ALIAS]] : i1 +// CHECK-NEXT: } +// CHECK-NEXT: [[SHOULD_DEALLOC:%.+]] = scf.for [[ITER:%.+]] = %c0 to [[ARG2]] step %c1 iter_args([[ITER_ARG:%.+]] = [[NO_RETAIN_ALIAS]]) -> (i1) { +// CHECK-NEXT: [[OTHER_DEALLOC_VAL:%.+]] = memref.load [[ARG0]][[[ITER]]] : memref +// CHECK-NEXT: [[DOES_ALIAS:%.+]] = arith.cmpi ne, [[OTHER_DEALLOC_VAL]], [[TO_DEALLOC]] : index +// CHECK-NEXT: [[AGG_DOES_ALIAS:%.+]] = arith.andi [[ITER_ARG]], [[DOES_ALIAS]] : i1 +// CHECK-NEXT: scf.yield [[AGG_DOES_ALIAS]] : i1 +// CHECK-NEXT: } +// CHECK-NEXT: [[OWNERSHIP:%.+]] = arith.xori [[NO_RETAIN_ALIAS]], %true : i1 +// CHECK-NEXT: return [[SHOULD_DEALLOC]], [[OWNERSHIP]] : i1, i1 diff --git a/utils/bazel/llvm-project-overlay/mlir/BUILD.bazel b/utils/bazel/llvm-project-overlay/mlir/BUILD.bazel --- a/utils/bazel/llvm-project-overlay/mlir/BUILD.bazel +++ b/utils/bazel/llvm-project-overlay/mlir/BUILD.bazel @@ -11689,6 +11689,7 @@ ":IR", ":MemRefDialect", ":Pass", + ":SCFDialect", ":Support", ":Transforms", ],