1 //===- Utils.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 associated deallocs that can be linked to our allocation nodes (if 19 /// any). 20 Operation *mlir::findDealloc(Value allocValue) { 21 auto userIt = llvm::find_if(allocValue.getUsers(), [&](Operation *user) { 22 auto effectInterface = dyn_cast<MemoryEffectOpInterface>(user); 23 if (!effectInterface) 24 return false; 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 return llvm::any_of(effects, [&](MemoryEffects::EffectInstance &it) { 30 return isa<MemoryEffects::Free>(it.getEffect()); 31 }); 32 }); 33 // Assign the associated dealloc operation (if any). 34 return userIt != allocValue.user_end() ? *userIt : nullptr; 35 } 36