diff --git a/flang/lib/Semantics/resolve-directives.cpp b/flang/lib/Semantics/resolve-directives.cpp --- a/flang/lib/Semantics/resolve-directives.cpp +++ b/flang/lib/Semantics/resolve-directives.cpp @@ -265,6 +265,7 @@ Symbol *ResolveAcc(const parser::Name &, Symbol::Flag, Scope &); Symbol *ResolveAcc(Symbol &, Symbol::Flag, Scope &); Symbol *ResolveName(const parser::Name &, bool parentScope = false); + Symbol *ResolveFctName(const parser::Name &); Symbol *ResolveAccCommonBlockName(const parser::Name *); Symbol *DeclareOrMarkOtherAccessEntity(const parser::Name &, Symbol::Flag); Symbol *DeclareOrMarkOtherAccessEntity(Symbol &, Symbol::Flag); @@ -835,6 +836,17 @@ return prev; } +Symbol *AccAttributeVisitor::ResolveFctName(const parser::Name &name) { + Symbol *prev{currScope().FindSymbol(name.source)}; + if (!prev || (prev && prev->IsFuncResult())) { + prev = currScope().parent().FindSymbol(name.source); + } + if (prev != name.symbol) { + name.symbol = prev; + } + return prev; +} + template common::IfNoLvalue FoldExpr( evaluate::FoldingContext &foldingContext, T &&expr) { @@ -907,7 +919,7 @@ bool AccAttributeVisitor::Pre(const parser::OpenACCRoutineConstruct &x) { const auto &optName{std::get>(x.t)}; if (optName) { - if (Symbol *sym = ResolveName(*optName, true)) { + if (Symbol *sym = ResolveFctName(*optName)) { Symbol &ultimate{sym->GetUltimate()}; AddRoutineInfoToSymbol(ultimate, x); } else { diff --git a/flang/test/Lower/OpenACC/acc-routine.f90 b/flang/test/Lower/OpenACC/acc-routine.f90 --- a/flang/test/Lower/OpenACC/acc-routine.f90 +++ b/flang/test/Lower/OpenACC/acc-routine.f90 @@ -2,6 +2,7 @@ ! RUN: bbc -fopenacc -emit-fir %s -o - | FileCheck %s +! CHECK: acc.routine @acc_routine_9 func(@_QPacc_routine10) seq ! CHECK: acc.routine @acc_routine_8 func(@_QPacc_routine9) bind("_QPacc_routine9a") ! CHECK: acc.routine @acc_routine_7 func(@_QPacc_routine8) bind("routine8_") ! CHECK: acc.routine @acc_routine_6 func(@_QPacc_routine7) gang(dim = 1 : i32) @@ -68,3 +69,9 @@ end subroutine ! CHECK-LABEL: func.func @_QPacc_routine9() attributes {acc.routine_info = #acc.routine_info<[@acc_routine_8]>} + +function acc_routine10() + !$acc routine(acc_routine10) seq +end function + +! CHECK-LABEL: func.func @_QPacc_routine10() -> f32 attributes {acc.routine_info = #acc.routine_info<[@acc_routine_9]>}