diff --git a/flang/lib/Lower/OpenMP.cpp b/flang/lib/Lower/OpenMP.cpp --- a/flang/lib/Lower/OpenMP.cpp +++ b/flang/lib/Lower/OpenMP.cpp @@ -109,12 +109,11 @@ } template -static void -createBodyOfOp(Op &op, Fortran::lower::AbstractConverter &converter, - mlir::Location &loc, - const Fortran::parser::OmpClauseList *clauses = nullptr, - const Fortran::semantics::Symbol *arg = nullptr, - bool outerCombined = false) { +static void createBodyOfOp( + Op &op, Fortran::lower::AbstractConverter &converter, mlir::Location &loc, + const Fortran::parser::OmpClauseList *clauses = nullptr, + const Fortran::semantics::Symbol *arg = nullptr, bool outerCombined = false, + const Fortran::parser::Expr *expr = nullptr) { fir::FirOpBuilder &firOpBuilder = converter.getFirOpBuilder(); // If an argument for the region is provided then create the block with that // argument. Also update the symbol's address with the mlir argument value. @@ -134,10 +133,14 @@ if constexpr (std::is_same_v) { mlir::ValueRange results; firOpBuilder.create(loc, results); + } else if constexpr (std::is_same_v) { + Fortran::lower::StatementContext stmtCtx; + auto result = fir::getBase( + converter.genExprValue(*Fortran::semantics::GetExpr(*expr), stmtCtx)); + firOpBuilder.create(loc, result); } else { firOpBuilder.create(loc); } - // Reset the insertion point to the start of the first block. firOpBuilder.setInsertionPointToStart(&block); // Handle privatization. Do not privatize if this is the outer operation. @@ -730,7 +733,44 @@ firOpBuilder.create(currentLocation, from_address, to_address, hint, memory_order); } - +static void +genOmpAtomicUpdate(Fortran::lower::AbstractConverter &converter, + Fortran::lower::pft::Evaluation &eval, + const Fortran::parser::OmpAtomicUpdate &atomicUpdate) { + auto &firOpBuilder = converter.getFirOpBuilder(); + auto currentLocation = converter.getCurrentLocation(); + mlir::Value address; + const Fortran::semantics::Symbol *symbolPtr; + // If no hint clause is specified, the effect is as if + // hint(omp_sync_hint_none) had been specified. + mlir::IntegerAttr hint = nullptr; + mlir::omp::ClauseMemoryOrderKindAttr memory_order = nullptr; + const Fortran::parser::OmpAtomicClauseList &rightHandClauseList = + std::get<2>(atomicUpdate.t); + const Fortran::parser::OmpAtomicClauseList &leftHandClauseList = + std::get<0>(atomicUpdate.t); + const auto &assignmentStmtExpr = + std::get(std::get<3>(atomicUpdate.t).statement.t); + const auto &assignmentStmtVariable = std::get( + std::get<3>(atomicUpdate.t).statement.t); + if (auto varDesignator = std::get_if< + Fortran::common::Indirection>( + &assignmentStmtVariable.u)) { + if (const auto *name = getDesignatorNameIfDataRef(varDesignator->value())) { + address = converter.getSymbolAddress(*name->symbol); + symbolPtr = name->symbol; + } + } + genOmpAtomicHintAndMemoryOrderClauses(converter, leftHandClauseList, hint, + memory_order); + genOmpAtomicHintAndMemoryOrderClauses(converter, rightHandClauseList, hint, + memory_order); + auto atomicUpdateOp = firOpBuilder.create( + currentLocation, address, hint, memory_order); + createBodyOfOp(atomicUpdateOp, converter, + currentLocation, nullptr, symbolPtr, + false, &assignmentStmtExpr); +} static void genOMP(Fortran::lower::AbstractConverter &converter, Fortran::lower::pft::Evaluation &eval, @@ -742,6 +782,9 @@ [&](const Fortran::parser::OmpAtomicWrite &atomicWrite) { genOmpAtomicWrite(converter, eval, atomicWrite); }, + [&](const Fortran::parser::OmpAtomicUpdate &atomicUpdate) { + genOmpAtomicUpdate(converter, eval, atomicUpdate); + }, [&](const auto &) { TODO(converter.getCurrentLocation(), "Atomic update & capture"); diff --git a/flang/test/Lower/OpenMP/atomic03.f90 b/flang/test/Lower/OpenMP/atomic03.f90 new file mode 100644 --- /dev/null +++ b/flang/test/Lower/OpenMP/atomic03.f90 @@ -0,0 +1,50 @@ +! This test checks lowering of atomic update construct +! RUN: bbc -fopenmp -emit-fir %s -o - | \ +! RUN: FileCheck %s --check-prefix=FIRDialect + +!FIRDialect: %[[VAR_X:.*]] = fir.alloca i32 {bindc_name = "x", uniq_name = "_QFEx"} +!FIRDialect: %[[VAR_Y:.*]] = fir.alloca i32 {bindc_name = "y", uniq_name = "_QFEy"} +!FIRDialect: %[[VAR_Z:.*]] = fir.alloca i32 {bindc_name = "z", uniq_name = "_QFEz"} +!FIRDialect: omp.atomic.update memory_order(seq_cst) %1 : !fir.ref { +!FIRDialect: ^bb0(%[[ARG_1:.*]]: i32): +!FIRDialect: {{.*}} = arith.constant 1 : i32 +!FIRDialect: %[[RESULT_1:.*]] = arith.addi %[[ARG_1]], {{.*}} : i32 +!FIRDialect: omp.yield(%[[RESULT_1]] : i32) +!FIRDialect: } +!FIRDialect: omp.atomic.update memory_order(relaxed) hint(uncontended) %[[VAR_X]] : !fir.ref { +!FIRDialect: ^bb0(%[[ARG_2:.*]]: i32): +!FIRDialect: %{{.*}} = arith.constant 1 : i32 +!FIRDialect: %[[RESULT_2:.*]] = arith.subi %[[ARG_2]], {{.*}} : i32 +!FIRDialect: omp.yield(%[[RESULT_2]] : i32) +!FIRDialect:} +!FIRDialect: omp.atomic.update memory_order(release) hint(contended) %[[VAR_Z]] : !fir.ref { +!FIRDialect: ^bb0(%[[ARG_3:.*]]: i32): +!FIRDialect: {{.*}} = arith.constant 10 : i32 +!FIRDialect: %[[RESULT_3:.*]] = arith.muli {{.*}}, %[[ARG_3]] : i32 +!FIRDialect: omp.yield(%[[RESULT_3]] : i32) +!FIRDialect: } +!FIRDialect: omp.atomic.update memory_order(relaxed) %[[VAR_Y]] : !fir.ref { +!FIRDialect: ^bb0(%[[ARG_4:.*]]: i32): +!FIRDialect: {{.*}} = fir.load %[[VAR_X]] : !fir.ref +!FIRDialect: {{.*}} = fir.load %[[VAR_Z]] : !fir.ref +!FIRDialect: {{.*}} = arith.cmpi sgt, {{.*}}, %[[ARG_4]] : i32 +!FIRDialect: {{.*}} = arith.select {{.*}}, {{.*}}, %[[ARG_4]] : i32 +!FIRDialect: {{.*}} = arith.cmpi sgt, {{.*}}, {{.*}} : i32 +!FIRDialect: %[[RESULT_4:.*]] = arith.select {{.*}}, {{.*}}, {{.*}} : i32 +!FIRDialect: omp.yield(%[[RESULT_4]] : i32) +!FIRDialect: } +!FIRDialect: return +!FIRDialect: } + +program OmpAtomic + use omp_lib + integer :: x, y, z + !$omp atomic update seq_cst + y = y + 1 + !$omp atomic relaxed update hint(omp_sync_hint_uncontended) + x = x - 1 + !$omp atomic release update hint(omp_sync_hint_contended) + z = z * 10 + !$omp atomic relaxed update + y = max(x, y, z) +end program OmpAtomic