Skip to content

Commit

Permalink
[flang][openacc] Lower the bind clause on acc routine
Browse files Browse the repository at this point in the history
Lower the bind clause to the corresponding attribute

Depends on D158120

Reviewed By: razvanlupusoru

Differential Revision: https://reviews.llvm.org/D158121
  • Loading branch information
clementval committed Aug 16, 2023
1 parent e2f6891 commit b1d0f5f
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 1 deletion.
34 changes: 33 additions & 1 deletion flang/lib/Lower/OpenACC.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2905,8 +2905,25 @@ static void genACC(Fortran::lower::AbstractConverter &converter,
llvm_unreachable("unsupported declarative directive");
}

template <typename R, typename T>
std::optional<R>
GetConstExpr(Fortran::semantics::SemanticsContext &semanticsContext,
const T &x) {
using DefaultCharConstantType = Fortran::evaluate::Ascii;
if (const auto *expr{Fortran::semantics::GetExpr(semanticsContext, x)}) {
const auto foldExpr{Fortran::evaluate::Fold(
semanticsContext.foldingContext(), Fortran::common::Clone(*expr))};
if constexpr (std::is_same_v<R, std::string>) {
return Fortran::evaluate::GetScalarConstantValue<DefaultCharConstantType>(
foldExpr);
}
}
return std::nullopt;
}

static void
genACC(Fortran::lower::AbstractConverter &converter,
Fortran::semantics::SemanticsContext &semanticsContext,
Fortran::lower::pft::Evaluation &eval,
const Fortran::parser::OpenACCRoutineConstruct &routineConstruct) {
fir::FirOpBuilder &builder = converter.getFirOpBuilder();
Expand Down Expand Up @@ -2955,6 +2972,21 @@ genACC(Fortran::lower::AbstractConverter &converter,
routineOp.setWorkerAttr(builder.getUnitAttr());
} else if (std::get_if<Fortran::parser::AccClause::Nohost>(&clause.u)) {
routineOp.setNohostAttr(builder.getUnitAttr());
} else if (const auto *bindClause =
std::get_if<Fortran::parser::AccClause::Bind>(&clause.u)) {
if (const auto *name =
std::get_if<Fortran::parser::Name>(&bindClause->v.u)) {
routineOp.setBindName(
builder.getStringAttr(converter.mangleName(*name->symbol)));
} else if (const auto charExpr =
std::get_if<Fortran::parser::ScalarDefaultCharExpr>(
&bindClause->v.u)) {
const std::optional<std::string> bindName =
GetConstExpr<std::string>(semanticsContext, *charExpr);
if (!bindName)
routineOp.emitError("Could not retrieve the bind name");
routineOp.setBindName(builder.getStringAttr(*bindName));
}
}
}

Expand Down Expand Up @@ -3025,7 +3057,7 @@ void Fortran::lower::genOpenACCDeclarativeConstruct(
},
[&](const Fortran::parser::OpenACCRoutineConstruct
&routineConstruct) {
genACC(converter, eval, routineConstruct);
genACC(converter, semanticsContext, eval, routineConstruct);
},
},
accDeclConstruct.u);
Expand Down
17 changes: 17 additions & 0 deletions flang/test/Lower/OpenACC/acc-routine.f90
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

! RUN: bbc -fopenacc -emit-fir %s -o - | FileCheck %s

! 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)
! CHECK: acc.routine @acc_routine_5 func(@_QPacc_routine6) nohost
! CHECK: acc.routine @acc_routine_4 func(@_QPacc_routine5) worker
Expand Down Expand Up @@ -51,3 +53,18 @@ subroutine acc_routine7()
end subroutine

! CHECK-LABEL: func.func @_QPacc_routine7() attributes {acc.routine_info = #acc.routine_info<[@acc_routine_6]>}

subroutine acc_routine8()
!$acc routine bind("routine8_")
end subroutine

! CHECK-LABEL: func.func @_QPacc_routine8() attributes {acc.routine_info = #acc.routine_info<[@acc_routine_7]>}

subroutine acc_routine9a()
end subroutine

subroutine acc_routine9()
!$acc routine bind(acc_routine9a)
end subroutine

! CHECK-LABEL: func.func @_QPacc_routine9() attributes {acc.routine_info = #acc.routine_info<[@acc_routine_8]>}

0 comments on commit b1d0f5f

Please sign in to comment.