Skip to content

Commit 8a8ef1c

Browse files
authored
[flang][cuda] Enable cuda with -x cuda option (llvm#84944)
Flang driver was already able to enable the CUDA language feature base on the file extension but there was no command line option. This PR adds one.
1 parent f1015d1 commit 8a8ef1c

File tree

3 files changed

+32
-3
lines changed

3 files changed

+32
-3
lines changed

flang/lib/Frontend/CompilerInvocation.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -581,6 +581,8 @@ static bool parseFrontendArgs(FrontendOptions &opts, llvm::opt::ArgList &args,
581581
// pre-processed inputs.
582582
.Case("f95", Language::Fortran)
583583
.Case("f95-cpp-input", Language::Fortran)
584+
// CUDA Fortran
585+
.Case("cuda", Language::Fortran)
584586
.Default(Language::Unknown);
585587

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

882+
// -x cuda
883+
auto language = args.getLastArgValue(clang::driver::options::OPT_x);
884+
if (language.equals("cuda")) {
885+
res.getFrontendOpts().features.Enable(
886+
Fortran::common::LanguageFeature::CUDA);
887+
}
888+
880889
// -fopenmp and -fopenacc
881890
if (args.hasArg(clang::driver::options::OPT_fopenacc)) {
882891
res.getFrontendOpts().features.Enable(

flang/lib/Frontend/FrontendAction.cpp

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,9 +86,14 @@ bool FrontendAction::beginSourceFile(CompilerInstance &ci,
8686
invoc.collectMacroDefinitions();
8787
}
8888

89-
// Enable CUDA Fortran if source file is *.cuf/*.CUF.
90-
invoc.getFortranOpts().features.Enable(Fortran::common::LanguageFeature::CUDA,
91-
getCurrentInput().getIsCUDAFortran());
89+
if (!invoc.getFortranOpts().features.IsEnabled(
90+
Fortran::common::LanguageFeature::CUDA)) {
91+
// Enable CUDA Fortran if source file is *.cuf/*.CUF and not already
92+
// enabled.
93+
invoc.getFortranOpts().features.Enable(
94+
Fortran::common::LanguageFeature::CUDA,
95+
getCurrentInput().getIsCUDAFortran());
96+
}
9297

9398
// Decide between fixed and free form (if the user didn't express any
9499
// preference, use the file extension to decide)

flang/test/Driver/cuda-option.f90

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
! Test -fcuda option
2+
! RUN: %flang -fc1 -cpp -x cuda -fdebug-unparse %s -o - | FileCheck %s
3+
! RUN: not %flang -fc1 -cpp %s -o - 2>&1 | FileCheck %s --check-prefix=ERROR
4+
program main
5+
#if _CUDA
6+
integer :: var = _CUDA
7+
#endif
8+
integer, device :: dvar
9+
end program
10+
11+
! CHECK-LABEL: PROGRAM main
12+
! CHECK: INTEGER :: var = 1
13+
! CHECK: INTEGER, DEVICE :: dvar
14+
15+
! ERROR: cuda-option.f90:8:19: error: expected end of statement

0 commit comments

Comments
 (0)