1 //===- MemRefUtils.cpp - Utilities to support the MemRef dialect ----------===// 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 utilities for the MemRef dialect. 10 // 11 //===----------------------------------------------------------------------===// 12 13 #include "mlir/Dialect/MemRef/Utils/MemRefUtils.h" 14 #include "mlir/Interfaces/SideEffectInterfaces.h" 15 16 using namespace mlir; 17 18 /// Finds a single dealloc operation for the given allocated value. 19 llvm::Optional<Operation *> mlir::findDealloc(Value allocValue) { 20 Operation *dealloc = nullptr; 21 for (Operation *user : allocValue.getUsers()) { 22 auto effectInterface = dyn_cast<MemoryEffectOpInterface>(user); 23 if (!effectInterface) 24 continue; 25 // Try to find a free effect that is applied to one of our values 26 // that will be automatically freed by our pass. 27 SmallVector<MemoryEffects::EffectInstance, 2> effects; 28 effectInterface.getEffectsOnValue(allocValue, effects); 29 const bool isFree = 30 llvm::any_of(effects, [&](MemoryEffects::EffectInstance &it) { 31 return isa<MemoryEffects::Free>(it.getEffect()); 32 }); 33 if (!isFree) 34 continue; 35 // If we found > 1 dealloc, return None. 36 if (dealloc) 37 return llvm::None; 38 dealloc = user; 39 } 40 return dealloc; 41 } 42