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 @@ -113,14 +113,35 @@ createBodyOfOp(Op &op, Fortran::lower::AbstractConverter &converter, mlir::Location &loc, const Fortran::parser::OmpClauseList *clauses = nullptr, - bool outerCombined = false) { + bool outerCombined = false, const SmallVector &args = {}, const Fortran::parser::Expr *expr = nullptr) { fir::FirOpBuilder &firOpBuilder = converter.getFirOpBuilder(); - firOpBuilder.createBlock(&op.getRegion()); + if(args.size()){ + if constexpr (std::is_same_v){ + SmallVector argType; + argType.push_back(converter.genType(Fortran::common::Reference(*args[0]))); + firOpBuilder.createBlock(&op.getRegion(), {}, argType, converter.genLocation(args[0]->name())); + int argIndex = 0; + for (auto &arg : args){ + fir::ExtendedValue exval = op.getRegion().front().getArgument(argIndex); + converter.bindSymbol(*arg, exval); + } + } + } + else{ + firOpBuilder.createBlock(&op.getRegion()); + } auto &block = op.getRegion().back(); firOpBuilder.setInsertionPointToStart(&block); - // Ensure the block is well-formed. - firOpBuilder.create(loc); - // Reset the insertion point to the start of the first block. + // Ensure the block is well-formed. + 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. if (clauses && !outerCombined) @@ -571,7 +592,41 @@ 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; + SmallVector symbolVector; + // 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); + symbolVector.push_back(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, false, symbolVector, &assignmentStmtExpr); +} static void genOMP(Fortran::lower::AbstractConverter &converter, Fortran::lower::pft::Evaluation &eval, @@ -583,6 +638,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