diff --git a/flang/lib/Lower/OpenMP.cpp b/flang/lib/Lower/OpenMP.cpp index 42fad0d483d19..67b88d2033aa2 100644 --- a/flang/lib/Lower/OpenMP.cpp +++ b/flang/lib/Lower/OpenMP.cpp @@ -562,13 +562,13 @@ static void genObjectList(const Fortran::parser::OmpObjectList &objectList, static mlir::Value getIfClauseOperand(Fortran::lower::AbstractConverter &converter, Fortran::lower::StatementContext &stmtCtx, - const Fortran::parser::OmpClause::If *ifClause) { + const Fortran::parser::OmpClause::If *ifClause, + mlir::Location clauseLocation) { fir::FirOpBuilder &firOpBuilder = converter.getFirOpBuilder(); - mlir::Location currentLocation = converter.getCurrentLocation(); auto &expr = std::get(ifClause->v.t); mlir::Value ifVal = fir::getBase( converter.genExprValue(*Fortran::semantics::GetExpr(expr), stmtCtx)); - return firOpBuilder.createConvert(currentLocation, firOpBuilder.getI1Type(), + return firOpBuilder.createConvert(clauseLocation, firOpBuilder.getI1Type(), ifVal); } @@ -768,6 +768,7 @@ static void createBodyOfTargetOp( static void createTargetOp(Fortran::lower::AbstractConverter &converter, const Fortran::parser::OmpClauseList &opClauseList, const llvm::omp::Directive &directive, + mlir::Location currentLocation, Fortran::lower::pft::Evaluation *eval = nullptr) { Fortran::lower::StatementContext stmtCtx; fir::FirOpBuilder &firOpBuilder = converter.getFirOpBuilder(); @@ -782,16 +783,15 @@ static void createTargetOp(Fortran::lower::AbstractConverter &converter, SmallVector useDeviceSymbols; /// Check for unsupported map operand types. - auto checkType = [](auto currentLocation, mlir::Type type) { + auto checkType = [](mlir::Location location, mlir::Type type) { if (auto refType = type.dyn_cast()) type = refType.getElementType(); if (auto boxType = type.dyn_cast_or_null()) if (!boxType.getElementType().isa()) - TODO(currentLocation, "OMPD_target_data MapOperand BoxType"); + TODO(location, "OMPD_target_data MapOperand BoxType"); }; - auto addMapClause = [&](const auto &mapClause, - mlir::Location ¤tLocation) { + auto addMapClause = [&](const auto &mapClause, mlir::Location &location) { auto mapType = std::get( std::get>(mapClause->v.t) ->t); @@ -839,7 +839,7 @@ static void createTargetOp(Fortran::lower::AbstractConverter &converter, if (Fortran::parser::Unwrap(ompObject) || Fortran::parser::Unwrap( ompObject)) - TODO(currentLocation, + TODO(location, "OMPD_target_data for Array Expressions or Structure Components"); } genObjectList(std::get(mapClause->v.t), @@ -866,10 +866,11 @@ static void createTargetOp(Fortran::lower::AbstractConverter &converter, }; for (const Fortran::parser::OmpClause &clause : opClauseList.v) { - mlir::Location currentLocation = converter.genLocation(clause.source); + mlir::Location clauseLocation = converter.genLocation(clause.source); if (const auto &ifClause = std::get_if(&clause.u)) { - ifClauseOperand = getIfClauseOperand(converter, stmtCtx, ifClause); + ifClauseOperand = + getIfClauseOperand(converter, stmtCtx, ifClause, clauseLocation); } else if (const auto &deviceClause = std::get_if(&clause.u)) { if (auto deviceModifier = std::get< @@ -877,7 +878,7 @@ static void createTargetOp(Fortran::lower::AbstractConverter &converter, deviceClause->v.t)) { if (deviceModifier == Fortran::parser::OmpDeviceClause::DeviceModifier::Ancestor) { - TODO(currentLocation, "OMPD_target Device Modifier Ancestor"); + TODO(clauseLocation, "OMPD_target Device Modifier Ancestor"); } } if (const auto *deviceExpr = Fortran::semantics::GetExpr( @@ -902,9 +903,9 @@ static void createTargetOp(Fortran::lower::AbstractConverter &converter, addUseDeviceClause(devAddrClause->v, deviceAddrOperands); } else if (const auto &mapClause = std::get_if(&clause.u)) { - addMapClause(mapClause, currentLocation); + addMapClause(mapClause, clauseLocation); } else { - TODO(currentLocation, "OMPD_target unhandled clause"); + TODO(clauseLocation, "OMPD_target unhandled clause"); } } @@ -912,7 +913,6 @@ static void createTargetOp(Fortran::lower::AbstractConverter &converter, mapTypes.end()); mlir::ArrayAttr mapTypesArrayAttr = ArrayAttr::get(firOpBuilder.getContext(), mapTypesAttr); - mlir::Location currentLocation = converter.getCurrentLocation(); if (directive == llvm::omp::Directive::OMPD_target) { auto targetOp = firOpBuilder.create( @@ -948,28 +948,29 @@ static void genOMP(Fortran::lower::AbstractConverter &converter, fir::FirOpBuilder &firOpBuilder = converter.getFirOpBuilder(); const Fortran::parser::OmpClauseList &opClauseList = std::get(simpleStandaloneConstruct.t); + mlir::Location currentLocation = converter.genLocation(directive.source); switch (directive.v) { default: break; case llvm::omp::Directive::OMPD_barrier: - firOpBuilder.create(converter.getCurrentLocation()); + firOpBuilder.create(currentLocation); break; case llvm::omp::Directive::OMPD_taskwait: - firOpBuilder.create(converter.getCurrentLocation()); + firOpBuilder.create(currentLocation); break; case llvm::omp::Directive::OMPD_taskyield: - firOpBuilder.create(converter.getCurrentLocation()); + firOpBuilder.create(currentLocation); break; case llvm::omp::Directive::OMPD_target_data: case llvm::omp::Directive::OMPD_target_enter_data: case llvm::omp::Directive::OMPD_target_exit_data: - createTargetOp(converter, opClauseList, directive.v); + createTargetOp(converter, opClauseList, directive.v, currentLocation); break; case llvm::omp::Directive::OMPD_target_update: - TODO(converter.getCurrentLocation(), "OMPD_target_update"); + TODO(currentLocation, "OMPD_target_update"); case llvm::omp::Directive::OMPD_ordered: - TODO(converter.getCurrentLocation(), "OMPD_ordered"); + TODO(currentLocation, "OMPD_ordered"); } } @@ -1126,9 +1127,11 @@ createCombinedParallelOp(Fortran::lower::AbstractConverter &converter, // 1. default // Note: rest of the clauses are handled when the inner operation is created for (const Fortran::parser::OmpClause &clause : opClauseList.v) { + mlir::Location clauseLocation = converter.genLocation(clause.source); if (const auto &ifClause = std::get_if(&clause.u)) { - ifClauseOperand = getIfClauseOperand(converter, stmtCtx, ifClause); + ifClauseOperand = + getIfClauseOperand(converter, stmtCtx, ifClause, clauseLocation); } else if (const auto &numThreadsClause = std::get_if( &clause.u)) { @@ -1162,7 +1165,7 @@ genOMP(Fortran::lower::AbstractConverter &converter, const auto &endBlockDirective = std::get(blockConstruct.t); fir::FirOpBuilder &firOpBuilder = converter.getFirOpBuilder(); - mlir::Location currentLocation = converter.getCurrentLocation(); + mlir::Location currentLocation = converter.genLocation(blockDirective.source); Fortran::lower::StatementContext stmtCtx; llvm::ArrayRef argTy; @@ -1176,9 +1179,11 @@ genOMP(Fortran::lower::AbstractConverter &converter, const auto &opClauseList = std::get(beginBlockDirective.t); for (const auto &clause : opClauseList.v) { + mlir::Location clauseLocation = converter.genLocation(clause.source); if (const auto &ifClause = std::get_if(&clause.u)) { - ifClauseOperand = getIfClauseOperand(converter, stmtCtx, ifClause); + ifClauseOperand = + getIfClauseOperand(converter, stmtCtx, ifClause, clauseLocation); } else if (const auto &numThreadsClause = std::get_if( &clause.u)) { @@ -1342,11 +1347,13 @@ genOMP(Fortran::lower::AbstractConverter &converter, createBodyOfOp(taskGroupOp, converter, currentLocation, eval, &opClauseList); } else if (blockDirective.v == llvm::omp::OMPD_target) { - createTargetOp(converter, opClauseList, blockDirective.v, &eval); + createTargetOp(converter, opClauseList, blockDirective.v, currentLocation, + &eval); } else if (blockDirective.v == llvm::omp::OMPD_target_data) { - createTargetOp(converter, opClauseList, blockDirective.v, &eval); + createTargetOp(converter, opClauseList, blockDirective.v, currentLocation, + &eval); } else { - TODO(converter.getCurrentLocation(), "Unhandled block directive"); + TODO(currentLocation, "Unhandled block directive"); } } @@ -1689,7 +1696,6 @@ static void genOMP(Fortran::lower::AbstractConverter &converter, const Fortran::parser::OpenMPLoopConstruct &loopConstruct) { fir::FirOpBuilder &firOpBuilder = converter.getFirOpBuilder(); - mlir::Location currentLocation = converter.getCurrentLocation(); llvm::SmallVector lowerBound, upperBound, step, linearVars, linearStepVars, reductionVars, alignedVars, nontemporalVars; mlir::Value scheduleChunkClauseOperand, ifClauseOperand; @@ -1701,17 +1707,20 @@ static void genOMP(Fortran::lower::AbstractConverter &converter, const auto &loopOpClauseList = std::get( std::get(loopConstruct.t).t); + const auto &beginLoopDirective = + std::get(loopConstruct.t); + mlir::Location currentLocation = + converter.genLocation(beginLoopDirective.source); const auto ompDirective = - std::get( - std::get(loopConstruct.t).t) - .v; + std::get(beginLoopDirective.t).v; + if (llvm::omp::OMPD_parallel_do == ompDirective) { createCombinedParallelOp( converter, eval, std::get(loopConstruct.t)); } else if (llvm::omp::OMPD_do != ompDirective && llvm::omp::OMPD_simd != ompDirective) { - TODO(converter.getCurrentLocation(), "Construct enclosing do loop"); + TODO(currentLocation, "Construct enclosing do loop"); } DataSharingProcessor dsp(converter, loopOpClauseList, eval); @@ -1721,8 +1730,7 @@ static void genOMP(Fortran::lower::AbstractConverter &converter, auto *doConstructEval = &eval.getFirstNestedEvaluation(); if (doConstructEval->getIf() ->IsDoConcurrent()) { - TODO(converter.getCurrentLocation(), - "Do Concurrent in Worksharing loop construct"); + TODO(currentLocation, "Do Concurrent in Worksharing loop construct"); } std::int64_t collapseValue = @@ -1760,6 +1768,7 @@ static void genOMP(Fortran::lower::AbstractConverter &converter, } while (collapseValue > 0); for (const auto &clause : loopOpClauseList.v) { + mlir::Location clauseLocation = converter.genLocation(clause.source); if (const auto &scheduleClause = std::get_if(&clause.u)) { if (const auto &chunkExpr = @@ -1772,7 +1781,8 @@ static void genOMP(Fortran::lower::AbstractConverter &converter, } } else if (const auto &ifClause = std::get_if(&clause.u)) { - ifClauseOperand = getIfClauseOperand(converter, stmtCtx, ifClause); + ifClauseOperand = + getIfClauseOperand(converter, stmtCtx, ifClause, clauseLocation); } else if (const auto &reductionClause = std::get_if( &clause.u)) { diff --git a/flang/test/Lower/OpenMP/location.f90 b/flang/test/Lower/OpenMP/location.f90 new file mode 100644 index 0000000000000..442ca44c93da2 --- /dev/null +++ b/flang/test/Lower/OpenMP/location.f90 @@ -0,0 +1,68 @@ +! This test checks location of OpenMP constructs and clauses + +!RUN: %flang_fc1 -emit-fir -fopenmp -mmlir --mlir-print-debuginfo %s -o - | FileCheck %s + +!CHECK-LABEL: sub_parallel +subroutine sub_parallel() + print *, x +!CHECK: omp.parallel { + !$omp parallel + print *, x +!CHECK: omp.terminator loc(#[[PAR_LOC:.*]]) +!CHECK: } loc(#[[PAR_LOC]]) + !$omp end parallel + print *, x +end + +!CHECK-LABEL: sub_target +subroutine sub_target() + print *, x +!CHECK: omp.target {{.*}} { + !$omp target + print *, x +!CHECK: omp.terminator loc(#[[TAR_LOC:.*]]) +!CHECK: } loc(#[[TAR_LOC]]) + !$omp end target + print *, x +end + +!CHECK-LABEL: sub_loop +subroutine sub_loop() +!CHECK: omp.wsloop {{.*}} { + !$omp do + do i=1,10 + print *, i +!CHECK: omp.yield loc(#[[LOOP_LOC:.*]]) +!CHECK: } loc(#[[LOOP_LOC]]) + end do + !$omp end do +end + +!CHECK-LABEL: sub_standalone +subroutine sub_standalone() + !CHECK: omp.barrier loc(#[[BAR_LOC:.*]]) + !$omp barrier + !CHECK: omp.taskwait loc(#[[TW_LOC:.*]]) + !$omp taskwait + !CHECK: omp.taskyield loc(#[[TY_LOC:.*]]) + !$omp taskyield +end + +subroutine sub_if(c) + logical(kind=4) :: c + !CHECK: %[[CVT:.*]] = fir.convert %{{.*}} : (!fir.logical<4>) -> i1 loc(#[[IF_LOC:.*]]) + !CHECK: omp.task if(%[[CVT]]) + !$omp task if(c) + print *, "Task" + !$omp end task + !CHECK: } loc(#[[TASK_LOC:.*]]) +end subroutine + +!CHECK: #[[PAR_LOC]] = loc("{{.*}}location.f90":9:9) +!CHECK: #[[TAR_LOC]] = loc("{{.*}}location.f90":21:9) +!CHECK: #[[LOOP_LOC]] = loc("{{.*}}location.f90":32:9) +!CHECK: #[[BAR_LOC]] = loc("{{.*}}location.f90":44:9) +!CHECK: #[[TW_LOC]] = loc("{{.*}}location.f90":46:9) +!CHECK: #[[TY_LOC]] = loc("{{.*}}location.f90":48:9) +!CHECK: #[[IF_LOC]] = loc("{{.*}}location.f90":55:14) +!CHECK: #[[TASK_LOC]] = loc("{{.*}}location.f90":55:9)