Skip to content

Commit f299e79

Browse files
committed
[mlir][OpenACC] indicate if destruction is needed in generatePrivateInit
1 parent 8e9ab67 commit f299e79

File tree

4 files changed

+26
-10
lines changed

4 files changed

+26
-10
lines changed

flang/include/flang/Optimizer/OpenACC/Support/FIROpenACCTypeInterfaces.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,8 @@ struct OpenACCMappableModel
5757
mlir::Location loc,
5858
mlir::TypedValue<mlir::acc::MappableType> var,
5959
llvm::StringRef varName,
60-
mlir::ValueRange extents,
61-
mlir::Value initVal) const;
60+
mlir::ValueRange extents, mlir::Value initVal,
61+
bool &needsDestroy) const;
6262
};
6363

6464
} // namespace fir::acc

flang/lib/Lower/OpenACC.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -978,15 +978,18 @@ static RecipeOp genRecipeOp(
978978
auto mappableTy = mlir::dyn_cast<mlir::acc::MappableType>(ty);
979979
assert(mappableTy &&
980980
"Expected that all variable types are considered mappable");
981+
bool needsDestroy = false;
981982
auto retVal = mappableTy.generatePrivateInit(
982983
builder, loc,
983984
mlir::cast<mlir::TypedValue<mlir::acc::MappableType>>(
984985
initBlock->getArgument(0)),
985986
initName,
986987
initBlock->getArguments().take_back(initBlock->getArguments().size() - 1),
987-
initValue);
988+
initValue, needsDestroy);
988989
mlir::acc::YieldOp::create(builder, loc,
989990
retVal ? retVal : initBlock->getArgument(0));
991+
// TODO: insert destruction when needed.
992+
(void)needsDestroy;
990993
return recipe;
991994
}
992995

flang/lib/Optimizer/OpenACC/Support/FIROpenACCTypeInterfaces.cpp

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -548,7 +548,8 @@ template <typename Ty>
548548
mlir::Value OpenACCMappableModel<Ty>::generatePrivateInit(
549549
mlir::Type type, mlir::OpBuilder &builder, mlir::Location loc,
550550
mlir::TypedValue<mlir::acc::MappableType> var, llvm::StringRef varName,
551-
mlir::ValueRange extents, mlir::Value initVal) const {
551+
mlir::ValueRange extents, mlir::Value initVal, bool &needsDestroy) const {
552+
needsDestroy = false;
552553
mlir::Value retVal;
553554
mlir::Type unwrappedTy = fir::unwrapRefType(type);
554555
mlir::ModuleOp mod = builder.getInsertionBlock()
@@ -615,9 +616,11 @@ mlir::Value OpenACCMappableModel<Ty>::generatePrivateInit(
615616
mlir::Value firClass =
616617
fir::EmboxOp::create(builder, loc, boxTy, allocatedScalar);
617618
fir::StoreOp::create(builder, loc, firClass, retVal);
619+
needsDestroy = true;
618620
} else if (mlir::isa<fir::SequenceType>(innerTy)) {
619621
hlfir::Entity source = hlfir::Entity{var};
620-
auto [temp, cleanup] = hlfir::createTempFromMold(loc, firBuilder, source);
622+
auto [temp, cleanupFlag] =
623+
hlfir::createTempFromMold(loc, firBuilder, source);
621624
if (fir::isa_ref_type(type)) {
622625
// When the temp is created - it is not a reference - thus we can
623626
// end up with a type inconsistency. Therefore ensure storage is created
@@ -636,6 +639,9 @@ mlir::Value OpenACCMappableModel<Ty>::generatePrivateInit(
636639
} else {
637640
retVal = temp;
638641
}
642+
// If heap was allocated, a destroy is required later.
643+
if (cleanupFlag)
644+
needsDestroy = true;
639645
} else {
640646
TODO(loc, "Unsupported boxed type for OpenACC private-like recipe");
641647
}
@@ -667,23 +673,23 @@ template mlir::Value
667673
OpenACCMappableModel<fir::BaseBoxType>::generatePrivateInit(
668674
mlir::Type type, mlir::OpBuilder &builder, mlir::Location loc,
669675
mlir::TypedValue<mlir::acc::MappableType> var, llvm::StringRef varName,
670-
mlir::ValueRange extents, mlir::Value initVal) const;
676+
mlir::ValueRange extents, mlir::Value initVal, bool &needsDestroy) const;
671677

672678
template mlir::Value
673679
OpenACCMappableModel<fir::ReferenceType>::generatePrivateInit(
674680
mlir::Type type, mlir::OpBuilder &builder, mlir::Location loc,
675681
mlir::TypedValue<mlir::acc::MappableType> var, llvm::StringRef varName,
676-
mlir::ValueRange extents, mlir::Value initVal) const;
682+
mlir::ValueRange extents, mlir::Value initVal, bool &needsDestroy) const;
677683

678684
template mlir::Value OpenACCMappableModel<fir::HeapType>::generatePrivateInit(
679685
mlir::Type type, mlir::OpBuilder &builder, mlir::Location loc,
680686
mlir::TypedValue<mlir::acc::MappableType> var, llvm::StringRef varName,
681-
mlir::ValueRange extents, mlir::Value initVal) const;
687+
mlir::ValueRange extents, mlir::Value initVal, bool &needsDestroy) const;
682688

683689
template mlir::Value
684690
OpenACCMappableModel<fir::PointerType>::generatePrivateInit(
685691
mlir::Type type, mlir::OpBuilder &builder, mlir::Location loc,
686692
mlir::TypedValue<mlir::acc::MappableType> var, llvm::StringRef varName,
687-
mlir::ValueRange extents, mlir::Value initVal) const;
693+
mlir::ValueRange extents, mlir::Value initVal, bool &needsDestroy) const;
688694

689695
} // namespace fir::acc

mlir/include/mlir/Dialect/OpenACC/OpenACCTypeInterfaces.td

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,12 @@ def OpenACC_MappableTypeInterface : TypeInterface<"MappableType"> {
274274
The `initVal` can be empty - it is primarily needed for reductions
275275
to ensure the variable is also initialized with appropriate value.
276276

277+
The `needsDestroy` out-parameter is set by implementations to indicate
278+
that destruction code must be generated after the returned private
279+
variable usages, typically in the destroy region of recipe operations
280+
(for example, when heap allocations or temporaries requiring cleanup
281+
are created during initialization).
282+
277283
If the return value is empty, it means that recipe body was not
278284
successfully generated.
279285
}],
@@ -284,7 +290,8 @@ def OpenACC_MappableTypeInterface : TypeInterface<"MappableType"> {
284290
"::mlir::TypedValue<::mlir::acc::MappableType>":$var,
285291
"::llvm::StringRef":$varName,
286292
"::mlir::ValueRange":$extents,
287-
"::mlir::Value":$initVal),
293+
"::mlir::Value":$initVal,
294+
"bool &":$needsDestroy),
288295
/*methodBody=*/"",
289296
/*defaultImplementation=*/[{
290297
return {};

0 commit comments

Comments
 (0)