Skip to content

Commit 171d8c4

Browse files
committed
[Flang][OpenMP][MLIR] Fix memory leak caused by D149368 causing sanitizer error and fix iterator invalidation error
This patch fixes two issues introduced by the D149368 patch, one is a memory leak from using the removeFromParent rather than eraseFromParent (the erase also had to be moved to not create use after deletes). And the other is a possible iterator invalidation bug, better to be safe than sorry.
1 parent fd4bac4 commit 171d8c4

File tree

2 files changed

+18
-14
lines changed

2 files changed

+18
-14
lines changed

flang/lib/Optimizer/Transforms/OMPEarlyOutlining.cpp

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -127,14 +127,18 @@ class OMPEarlyOutliningPass
127127
llvm::SetVector<mlir::Value> inputs;
128128
mlir::Region &targetRegion = targetOp.getRegion();
129129
mlir::getUsedValuesDefinedAbove(targetRegion, inputs);
130-
130+
131131
// filter out declareTarget and map entries which are specially handled
132132
// at the moment, so we do not wish these to end up as function arguments
133133
// which would just be more noise in the IR.
134-
for (mlir::Value value : inputs)
135-
if (mlir::isa_and_nonnull<mlir::omp::MapInfoOp>(value.getDefiningOp()) ||
136-
isAddressOfGlobalDeclareTarget(value))
137-
inputs.remove(value);
134+
for (llvm::SetVector<mlir::Value>::iterator iter = inputs.begin(); iter != inputs.end();) {
135+
if (mlir::isa_and_nonnull<mlir::omp::MapInfoOp>(iter->getDefiningOp()) ||
136+
isAddressOfGlobalDeclareTarget(*iter)) {
137+
iter = inputs.erase(iter);
138+
} else {
139+
++iter;
140+
}
141+
}
138142

139143
// Create new function and initialize
140144
mlir::FunctionType funcType = builder.getFunctionType(

mlir/lib/Target/LLVMIR/Dialect/OpenMP/OpenMPToLLVMIRTranslation.cpp

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1989,6 +1989,15 @@ handleDeclareTargetMapVar(llvm::ArrayRef<Value> mapOperands,
19891989
user->replaceUsesOfWith(mapOpValue, load);
19901990
}
19911991
}
1992+
1993+
// A global has already been generated by this stage for device
1994+
// that's now dead after the remapping, we can remove it now,
1995+
// as we have replaced all usages with the new ref pointer.
1996+
if (llvm::GlobalValue *unusedGlobalVal =
1997+
dyn_cast<llvm::GlobalValue>(mapOpValue)) {
1998+
unusedGlobalVal->dropAllReferences();
1999+
unusedGlobalVal->eraseFromParent();
2000+
}
19922001
}
19932002
}
19942003
}
@@ -2182,15 +2191,6 @@ convertDeclareTargetAttr(Operation *op, mlir::omp::DeclareTargetAttr attribute,
21822191
generatedRefs, /*OpenMPSimd*/ false, targetTriple, gVal->getType(),
21832192
/*GlobalInitializer*/ nullptr,
21842193
/*VariableLinkage*/ nullptr);
2185-
// A global has already been generated by this stage, unlike Clang, so
2186-
// this needs to be specially removed here for device when we're
2187-
// anything but a To clause specified variable with no unified shared
2188-
// memory.
2189-
if (llvm::GlobalValue *llvmVal =
2190-
llvmModule->getNamedValue(mangledName)) {
2191-
llvmVal->removeFromParent();
2192-
llvmVal->dropAllReferences();
2193-
}
21942194
}
21952195
}
21962196
}

0 commit comments

Comments
 (0)