From 0ee9e3ae7eb82d81edf72305f818bb6cd8e640c1 Mon Sep 17 00:00:00 2001 From: Valentin Clement Date: Fri, 27 Oct 2023 15:33:56 -0700 Subject: [PATCH] [flang][openacc][openmp] Set correct location on atomic operations The location set on atomic operations in both OpenMP and OpenACC was completly off. The real location needs to be created from the source CharBlock of the parse tree node of the respective atomic statement. This patch updates locations in lowering for atomic operations. --- flang/lib/Lower/DirectivesCommon.h | 65 ++++++++++++-------------- flang/lib/Lower/OpenACC.cpp | 14 ++++-- flang/lib/Lower/OpenMP.cpp | 19 ++++++-- flang/test/Lower/OpenACC/locations.f90 | 52 +++++++++++++++++++++ 4 files changed, 104 insertions(+), 46 deletions(-) diff --git a/flang/lib/Lower/DirectivesCommon.h b/flang/lib/Lower/DirectivesCommon.h index 1b231ee1b891b..33b8198a2518b 100644 --- a/flang/lib/Lower/DirectivesCommon.h +++ b/flang/lib/Lower/DirectivesCommon.h @@ -132,10 +132,9 @@ static inline void genOmpAccAtomicCaptureStatement( mlir::Value toAddress, [[maybe_unused]] const AtomicListT *leftHandClauseList, [[maybe_unused]] const AtomicListT *rightHandClauseList, - mlir::Type elementType) { + mlir::Type elementType, mlir::Location loc) { // Generate `atomic.read` operation for atomic assigment statements fir::FirOpBuilder &firOpBuilder = converter.getFirOpBuilder(); - mlir::Location currentLocation = converter.getCurrentLocation(); if constexpr (std::is_same()) { @@ -151,12 +150,11 @@ static inline void genOmpAccAtomicCaptureStatement( genOmpAtomicHintAndMemoryOrderClauses(converter, *rightHandClauseList, hint, memoryOrder); firOpBuilder.create( - currentLocation, fromAddress, toAddress, - mlir::TypeAttr::get(elementType), hint, memoryOrder); + loc, fromAddress, toAddress, mlir::TypeAttr::get(elementType), hint, + memoryOrder); } else { firOpBuilder.create( - currentLocation, fromAddress, toAddress, - mlir::TypeAttr::get(elementType)); + loc, fromAddress, toAddress, mlir::TypeAttr::get(elementType)); } } @@ -166,11 +164,10 @@ template static inline void genOmpAccAtomicWriteStatement( Fortran::lower::AbstractConverter &converter, mlir::Value lhsAddr, mlir::Value rhsExpr, [[maybe_unused]] const AtomicListT *leftHandClauseList, - [[maybe_unused]] const AtomicListT *rightHandClauseList, + [[maybe_unused]] const AtomicListT *rightHandClauseList, mlir::Location loc, mlir::Value *evaluatedExprValue = nullptr) { // Generate `atomic.write` operation for atomic assignment statements fir::FirOpBuilder &firOpBuilder = converter.getFirOpBuilder(); - mlir::Location currentLocation = converter.getCurrentLocation(); if constexpr (std::is_same()) { @@ -184,11 +181,10 @@ static inline void genOmpAccAtomicWriteStatement( if (rightHandClauseList) genOmpAtomicHintAndMemoryOrderClauses(converter, *rightHandClauseList, hint, memoryOrder); - firOpBuilder.create(currentLocation, lhsAddr, - rhsExpr, hint, memoryOrder); + firOpBuilder.create(loc, lhsAddr, rhsExpr, hint, + memoryOrder); } else { - firOpBuilder.create(currentLocation, lhsAddr, - rhsExpr); + firOpBuilder.create(loc, lhsAddr, rhsExpr); } } @@ -200,7 +196,7 @@ static inline void genOmpAccAtomicUpdateStatement( mlir::Type varType, const Fortran::parser::Variable &assignmentStmtVariable, const Fortran::parser::Expr &assignmentStmtExpr, [[maybe_unused]] const AtomicListT *leftHandClauseList, - [[maybe_unused]] const AtomicListT *rightHandClauseList, + [[maybe_unused]] const AtomicListT *rightHandClauseList, mlir::Location loc, mlir::Operation *atomicCaptureOp = nullptr) { // Generate `atomic.update` operation for atomic assignment statements fir::FirOpBuilder &firOpBuilder = converter.getFirOpBuilder(); @@ -302,7 +298,7 @@ static inline void genOmpAccAtomicUpdateStatement( /// Processes an atomic construct with write clause. template void genOmpAccAtomicWrite(Fortran::lower::AbstractConverter &converter, - const AtomicT &atomicWrite) { + const AtomicT &atomicWrite, mlir::Location loc) { const AtomicListT *rightHandClauseList = nullptr; const AtomicListT *leftHandClauseList = nullptr; if constexpr (std::is_same void genOmpAccAtomicRead(Fortran::lower::AbstractConverter &converter, - const AtomicT &atomicRead) { + const AtomicT &atomicRead, mlir::Location loc) { const AtomicListT *rightHandClauseList = nullptr; const AtomicListT *leftHandClauseList = nullptr; if constexpr (std::is_same(loc, toAddress.getType(), fromAddress); genOmpAccAtomicCaptureStatement(converter, fromAddress, toAddress, leftHandClauseList, rightHandClauseList, - elementType); + elementType, loc); } /// Processes an atomic construct with update clause. template void genOmpAccAtomicUpdate(Fortran::lower::AbstractConverter &converter, - const AtomicT &atomicUpdate) { + const AtomicT &atomicUpdate, mlir::Location loc) { const AtomicListT *rightHandClauseList = nullptr; const AtomicListT *leftHandClauseList = nullptr; if constexpr (std::is_same( converter, lhsAddr, varType, assignmentStmtVariable, assignmentStmtExpr, - leftHandClauseList, rightHandClauseList); + leftHandClauseList, rightHandClauseList, loc); } /// Processes an atomic construct with no clause - which implies update clause. template void genOmpAtomic(Fortran::lower::AbstractConverter &converter, - const AtomicT &atomicConstruct) { + const AtomicT &atomicConstruct, mlir::Location loc) { const AtomicListT &atomicClauseList = std::get(atomicConstruct.t); const auto &assignmentStmtExpr = std::get( @@ -420,15 +415,14 @@ void genOmpAtomic(Fortran::lower::AbstractConverter &converter, // the update clause is specified (for both OpenMP and OpenACC). genOmpAccAtomicUpdateStatement( converter, lhsAddr, varType, assignmentStmtVariable, assignmentStmtExpr, - &atomicClauseList, nullptr); + &atomicClauseList, nullptr, loc); } /// Processes an atomic construct with capture clause. template void genOmpAccAtomicCapture(Fortran::lower::AbstractConverter &converter, - const AtomicT &atomicCapture) { + const AtomicT &atomicCapture, mlir::Location loc) { fir::FirOpBuilder &firOpBuilder = converter.getFirOpBuilder(); - mlir::Location currentLocation = converter.getCurrentLocation(); const Fortran::parser::AssignmentStmt &stmt1 = std::get(atomicCapture.t).v.statement; @@ -480,11 +474,10 @@ void genOmpAccAtomicCapture(Fortran::lower::AbstractConverter &converter, memoryOrder); genOmpAtomicHintAndMemoryOrderClauses(converter, rightHandClauseList, hint, memoryOrder); - atomicCaptureOp = firOpBuilder.create( - currentLocation, hint, memoryOrder); - } else { atomicCaptureOp = - firOpBuilder.create(currentLocation); + firOpBuilder.create(loc, hint, memoryOrder); + } else { + atomicCaptureOp = firOpBuilder.create(loc); } firOpBuilder.createBlock(&(atomicCaptureOp->getRegion(0))); @@ -499,11 +492,11 @@ void genOmpAccAtomicCapture(Fortran::lower::AbstractConverter &converter, genOmpAccAtomicCaptureStatement( converter, stmt1RHSArg, stmt1LHSArg, /*leftHandClauseList=*/nullptr, - /*rightHandClauseList=*/nullptr, elementType); + /*rightHandClauseList=*/nullptr, elementType, loc); genOmpAccAtomicUpdateStatement( converter, stmt1RHSArg, stmt2VarType, stmt2Var, stmt2Expr, /*leftHandClauseList=*/nullptr, - /*rightHandClauseList=*/nullptr, atomicCaptureOp); + /*rightHandClauseList=*/nullptr, loc, atomicCaptureOp); } else { // Atomic capture construct is of the form [capture-stmt, write-stmt] const Fortran::semantics::SomeExpr &fromExpr = @@ -512,11 +505,11 @@ void genOmpAccAtomicCapture(Fortran::lower::AbstractConverter &converter, genOmpAccAtomicCaptureStatement( converter, stmt1RHSArg, stmt1LHSArg, /*leftHandClauseList=*/nullptr, - /*rightHandClauseList=*/nullptr, elementType); + /*rightHandClauseList=*/nullptr, elementType, loc); genOmpAccAtomicWriteStatement( converter, stmt1RHSArg, stmt2RHSArg, /*leftHandClauseList=*/nullptr, - /*rightHandClauseList=*/nullptr); + /*rightHandClauseList=*/nullptr, loc); } } else { // Atomic capture construct is of the form [update-stmt, capture-stmt] @@ -527,19 +520,19 @@ void genOmpAccAtomicCapture(Fortran::lower::AbstractConverter &converter, genOmpAccAtomicCaptureStatement( converter, stmt1LHSArg, stmt2LHSArg, /*leftHandClauseList=*/nullptr, - /*rightHandClauseList=*/nullptr, elementType); + /*rightHandClauseList=*/nullptr, elementType, loc); firOpBuilder.setInsertionPointToStart(&block); genOmpAccAtomicUpdateStatement( converter, stmt1LHSArg, stmt1VarType, stmt1Var, stmt1Expr, /*leftHandClauseList=*/nullptr, - /*rightHandClauseList=*/nullptr, atomicCaptureOp); + /*rightHandClauseList=*/nullptr, loc, atomicCaptureOp); } firOpBuilder.setInsertionPointToEnd(&block); if constexpr (std::is_same()) { - firOpBuilder.create(currentLocation); + firOpBuilder.create(loc); } else { - firOpBuilder.create(currentLocation); + firOpBuilder.create(loc); } firOpBuilder.setInsertionPointToStart(&block); } diff --git a/flang/lib/Lower/OpenACC.cpp b/flang/lib/Lower/OpenACC.cpp index 5c29c78141730..74c51761f1bd1 100644 --- a/flang/lib/Lower/OpenACC.cpp +++ b/flang/lib/Lower/OpenACC.cpp @@ -3287,25 +3287,29 @@ static void genACC(Fortran::lower::AbstractConverter &converter, Fortran::lower::pft::Evaluation &eval, const Fortran::parser::OpenACCAtomicConstruct &atomicConstruct) { + + mlir::Location loc = converter.genLocation(atomicConstruct.source); std::visit( Fortran::common::visitors{ [&](const Fortran::parser::AccAtomicRead &atomicRead) { Fortran::lower::genOmpAccAtomicRead(converter, atomicRead); + void>(converter, atomicRead, + loc); }, [&](const Fortran::parser::AccAtomicWrite &atomicWrite) { Fortran::lower::genOmpAccAtomicWrite< - Fortran::parser::AccAtomicWrite, void>(converter, atomicWrite); + Fortran::parser::AccAtomicWrite, void>(converter, atomicWrite, + loc); }, [&](const Fortran::parser::AccAtomicUpdate &atomicUpdate) { Fortran::lower::genOmpAccAtomicUpdate< - Fortran::parser::AccAtomicUpdate, void>(converter, - atomicUpdate); + Fortran::parser::AccAtomicUpdate, void>(converter, atomicUpdate, + loc); }, [&](const Fortran::parser::AccAtomicCapture &atomicCapture) { Fortran::lower::genOmpAccAtomicCapture< Fortran::parser::AccAtomicCapture, void>(converter, - atomicCapture); + atomicCapture, loc); }, }, atomicConstruct.u); diff --git a/flang/lib/Lower/OpenMP.cpp b/flang/lib/Lower/OpenMP.cpp index 0faaae6c08e04..1b9a03f74ac47 100644 --- a/flang/lib/Lower/OpenMP.cpp +++ b/flang/lib/Lower/OpenMP.cpp @@ -3060,29 +3060,38 @@ genOMP(Fortran::lower::AbstractConverter &converter, std::visit( Fortran::common::visitors{ [&](const Fortran::parser::OmpAtomicRead &atomicRead) { + mlir::Location loc = converter.genLocation(atomicRead.source); Fortran::lower::genOmpAccAtomicRead< Fortran::parser::OmpAtomicRead, - Fortran::parser::OmpAtomicClauseList>(converter, atomicRead); + Fortran::parser::OmpAtomicClauseList>(converter, atomicRead, + loc); }, [&](const Fortran::parser::OmpAtomicWrite &atomicWrite) { + mlir::Location loc = converter.genLocation(atomicWrite.source); Fortran::lower::genOmpAccAtomicWrite< Fortran::parser::OmpAtomicWrite, - Fortran::parser::OmpAtomicClauseList>(converter, atomicWrite); + Fortran::parser::OmpAtomicClauseList>(converter, atomicWrite, + loc); }, [&](const Fortran::parser::OmpAtomic &atomicConstruct) { + mlir::Location loc = converter.genLocation(atomicConstruct.source); Fortran::lower::genOmpAtomic( - converter, atomicConstruct); + converter, atomicConstruct, loc); }, [&](const Fortran::parser::OmpAtomicUpdate &atomicUpdate) { + mlir::Location loc = converter.genLocation(atomicUpdate.source); Fortran::lower::genOmpAccAtomicUpdate< Fortran::parser::OmpAtomicUpdate, - Fortran::parser::OmpAtomicClauseList>(converter, atomicUpdate); + Fortran::parser::OmpAtomicClauseList>(converter, atomicUpdate, + loc); }, [&](const Fortran::parser::OmpAtomicCapture &atomicCapture) { + mlir::Location loc = converter.genLocation(atomicCapture.source); Fortran::lower::genOmpAccAtomicCapture< Fortran::parser::OmpAtomicCapture, - Fortran::parser::OmpAtomicClauseList>(converter, atomicCapture); + Fortran::parser::OmpAtomicClauseList>(converter, atomicCapture, + loc); }, }, atomicConstruct.u); diff --git a/flang/test/Lower/OpenACC/locations.f90 b/flang/test/Lower/OpenACC/locations.f90 index 19788f9f6d1aa..031d8eda48acd 100644 --- a/flang/test/Lower/OpenACC/locations.f90 +++ b/flang/test/Lower/OpenACC/locations.f90 @@ -111,4 +111,56 @@ subroutine if_clause_expr_location(arr) !CHECK-NEXT: } loc("{{.*}}locations.f90":99:11) end subroutine + subroutine atomic_read_loc() + integer(4) :: x + integer(8) :: y + + !$acc atomic read + y = x + end + !CHECK: acc.atomic.read {{.*}} loc("{{.*}}locations.f90":118:11) + + subroutine atomic_capture_loc() + implicit none + integer :: k, v, i + + k = 1 + v = 0 + + !$acc atomic capture + v = k + k = (i + 1) * 3.14 + !$acc end atomic + +! CHECK: acc.atomic.capture { +! CHECK: acc.atomic.read {{.*}} loc("{{.*}}locations.f90":130:11) +! CHECK: acc.atomic.write {{.*}} loc("{{.*}}locations.f90":130:11) +! CHECK: } loc("{{.*}}locations.f90":130:11) + + end subroutine + + subroutine atomic_update_loc() + implicit none + integer :: x, y, z + + !$acc atomic + y = y + 1 +! CHECK: acc.atomic.update %{{.*}} : !fir.ref { +! CHECK: ^bb0(%{{.*}}: i32 loc("{{.*}}locations.f90":142:3)): +! CHECK: } loc("{{.*}}locations.f90":142:3) + + !$acc atomic update + z = x * z + + ! %3 = fir.load %0 : !fir.ref loc("/local/home/vclement/llvm-project/flang/test/Lower/OpenACC/locations.f90":142:3) + ! acc.atomic.update %2 : !fir.ref { + ! ^bb0(%arg0: i32 loc("/local/home/vclement/llvm-project/flang/test/Lower/OpenACC/locations.f90":142:3)): + ! %4 = arith.muli %3, %arg0 : i32 loc("/local/home/vclement/llvm-project/flang/test/Lower/OpenACC/locations.f90":142:3) + ! acc.yield %4 : i32 loc("/local/home/vclement/llvm-project/flang/test/Lower/OpenACC/locations.f90":142:3) + ! } loc("/local/home/vclement/llvm-project/flang/test/Lower/OpenACC/locations.f90":142:3) + end subroutine + + end module + +