Skip to content

[flang][cuda] Enable cuda with -x cuda option #84944

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions flang/lib/Frontend/CompilerInvocation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -581,6 +581,8 @@ static bool parseFrontendArgs(FrontendOptions &opts, llvm::opt::ArgList &args,
// pre-processed inputs.
.Case("f95", Language::Fortran)
.Case("f95-cpp-input", Language::Fortran)
// CUDA Fortran
.Case("cuda", Language::Fortran)
.Default(Language::Unknown);

// Flang's intermediate representations.
Expand Down Expand Up @@ -877,6 +879,13 @@ static bool parseDialectArgs(CompilerInvocation &res, llvm::opt::ArgList &args,
if (args.hasArg(clang::driver::options::OPT_flarge_sizes))
res.getDefaultKinds().set_sizeIntegerKind(8);

// -x cuda
auto language = args.getLastArgValue(clang::driver::options::OPT_x);
if (language.equals("cuda")) {
res.getFrontendOpts().features.Enable(
Fortran::common::LanguageFeature::CUDA);
}

// -fopenmp and -fopenacc
if (args.hasArg(clang::driver::options::OPT_fopenacc)) {
res.getFrontendOpts().features.Enable(
Expand Down
11 changes: 8 additions & 3 deletions flang/lib/Frontend/FrontendAction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,14 @@ bool FrontendAction::beginSourceFile(CompilerInstance &ci,
invoc.collectMacroDefinitions();
}

// Enable CUDA Fortran if source file is *.cuf/*.CUF.
invoc.getFortranOpts().features.Enable(Fortran::common::LanguageFeature::CUDA,
getCurrentInput().getIsCUDAFortran());
if (!invoc.getFortranOpts().features.IsEnabled(
Fortran::common::LanguageFeature::CUDA)) {
// Enable CUDA Fortran if source file is *.cuf/*.CUF and not already
// enabled.
invoc.getFortranOpts().features.Enable(
Fortran::common::LanguageFeature::CUDA,
getCurrentInput().getIsCUDAFortran());
}

// Decide between fixed and free form (if the user didn't express any
// preference, use the file extension to decide)
Expand Down
15 changes: 15 additions & 0 deletions flang/test/Driver/cuda-option.f90
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
! Test -fcuda option
! RUN: %flang -fc1 -cpp -x cuda -fdebug-unparse %s -o - | FileCheck %s
! RUN: not %flang -fc1 -cpp %s -o - 2>&1 | FileCheck %s --check-prefix=ERROR
program main
#if _CUDA
integer :: var = _CUDA
#endif
integer, device :: dvar
end program

! CHECK-LABEL: PROGRAM main
! CHECK: INTEGER :: var = 1
! CHECK: INTEGER, DEVICE :: dvar

! ERROR: cuda-option.f90:8:19: error: expected end of statement