Skip to content

Commit 8bc676c

Browse files
authored
[flang][openacc] Lower acc cache directive (#65673)
The cache directive is attached directly to the acc.loop operation when the directive appears in the loop. When it appears before a loop, the OpenACCCacheConstruct is saved and attached when the acc.loop is created. Directive that cannot be attached to a loop are silently discarded. Depends on #65521
1 parent 996171a commit 8bc676c

File tree

2 files changed

+45
-2
lines changed

2 files changed

+45
-2
lines changed

flang/lib/Lower/OpenACC.cpp

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3096,6 +3096,41 @@ void Fortran::lower::finalizeOpenACCRoutineAttachment(
30963096
accRoutineInfos.clear();
30973097
}
30983098

3099+
static void
3100+
genACC(Fortran::lower::AbstractConverter &converter,
3101+
Fortran::semantics::SemanticsContext &semanticsContext,
3102+
const Fortran::parser::OpenACCCacheConstruct &cacheConstruct) {
3103+
fir::FirOpBuilder &builder = converter.getFirOpBuilder();
3104+
auto loopOp = builder.getRegion().getParentOfType<mlir::acc::LoopOp>();
3105+
auto crtPos = builder.saveInsertionPoint();
3106+
if (loopOp) {
3107+
builder.setInsertionPoint(loopOp);
3108+
Fortran::lower::StatementContext stmtCtx;
3109+
llvm::SmallVector<mlir::Value> cacheOperands;
3110+
const Fortran::parser::AccObjectListWithModifier &listWithModifier =
3111+
std::get<Fortran::parser::AccObjectListWithModifier>(cacheConstruct.t);
3112+
const auto &accObjectList =
3113+
std::get<Fortran::parser::AccObjectList>(listWithModifier.t);
3114+
const auto &modifier =
3115+
std::get<std::optional<Fortran::parser::AccDataModifier>>(
3116+
listWithModifier.t);
3117+
3118+
mlir::acc::DataClause dataClause = mlir::acc::DataClause::acc_cache;
3119+
if (modifier &&
3120+
(*modifier).v == Fortran::parser::AccDataModifier::Modifier::ReadOnly)
3121+
dataClause = mlir::acc::DataClause::acc_cache_readonly;
3122+
genDataOperandOperations<mlir::acc::CacheOp>(
3123+
accObjectList, converter, semanticsContext, stmtCtx, cacheOperands,
3124+
dataClause,
3125+
/*structured=*/true, /*implicit=*/false, /*setDeclareAttr*/ false);
3126+
loopOp.getCacheOperandsMutable().append(cacheOperands);
3127+
} else {
3128+
llvm::report_fatal_error(
3129+
"could not find loop to attach OpenACC cache information.");
3130+
}
3131+
builder.restoreInsertionPoint(crtPos);
3132+
}
3133+
30993134
void Fortran::lower::genOpenACCConstruct(
31003135
Fortran::lower::AbstractConverter &converter,
31013136
Fortran::semantics::SemanticsContext &semanticsContext,
@@ -3119,8 +3154,7 @@ void Fortran::lower::genOpenACCConstruct(
31193154
genACC(converter, semanticsContext, standaloneConstruct);
31203155
},
31213156
[&](const Fortran::parser::OpenACCCacheConstruct &cacheConstruct) {
3122-
TODO(converter.genLocation(cacheConstruct.source),
3123-
"OpenACC Cache construct not lowered yet!");
3157+
genACC(converter, semanticsContext, cacheConstruct);
31243158
},
31253159
[&](const Fortran::parser::OpenACCWaitConstruct &waitConstruct) {
31263160
genACC(converter, waitConstruct);

flang/test/Lower/OpenACC/acc-loop.f90

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -305,4 +305,13 @@ program acc_loop
305305
!CHECK: acc.yield
306306
!CHECK-NEXT: }{{$}}
307307

308+
!$acc loop
309+
DO i = 1, n
310+
!$acc cache(b)
311+
a(i) = b(i)
312+
END DO
313+
314+
! CHECK: %[[CACHE:.*]] = acc.cache varPtr(%{{.*}} : !fir.ref<!fir.array<10xf32>>) bounds(%{{.*}}) -> !fir.ref<!fir.array<10xf32>> {name = "b"}
315+
! CHECK: acc.loop cache(%[[CACHE]] : !fir.ref<!fir.array<10xf32>>)
316+
308317
end program

0 commit comments

Comments
 (0)