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 1 commit
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
3 changes: 3 additions & 0 deletions clang/include/clang/Driver/Options.td
Original file line number Diff line number Diff line change
Expand Up @@ -6488,6 +6488,9 @@ defm stack_arrays : BoolOptionWithoutMarshalling<"f", "stack-arrays",
defm loop_versioning : BoolOptionWithoutMarshalling<"f", "version-loops-for-stride",
PosFlag<SetTrue, [], [ClangOption], "Create unit-strided versions of loops">,
NegFlag<SetFalse, [], [ClangOption], "Do not create unit-strided loops (default)">>;

def fcuda : Flag<["-"], "fcuda">, Group<f_Group>,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if we also want to wire it to clang's -x cuda option. It might be good to use the same flags for clang/flang in makefiles/cmake. Though, I am not sure if the meaning of the two options is exactly the same. Maybe someone else knows.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't quite understand what actually -fcuda enables. Could the flag be more descriptive and the help text expanded? Also, what's the equivalent in Clang? We ought to keep both drivers in sync.

In particular, if this is something specific to Flang then I would avoid generic names like -fcuda.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I switch to use the -x cuda option which has a similar meaning.

HelpText<"Enable CUDA">;
} // let Visibility = [FC1Option, FlangOption]

def J : JoinedOrSeparate<["-"], "J">,
Expand Down
1 change: 1 addition & 0 deletions clang/lib/Driver/ToolChains/Flang.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ void Flang::addFortranDialectOptions(const ArgList &Args,
options::OPT_fopenmp,
options::OPT_fopenmp_version_EQ,
options::OPT_fopenacc,
options::OPT_fcuda,
options::OPT_finput_charset_EQ,
options::OPT_fimplicit_none,
options::OPT_fno_implicit_none,
Expand Down
6 changes: 6 additions & 0 deletions flang/lib/Frontend/CompilerInvocation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -877,6 +877,12 @@ static bool parseDialectArgs(CompilerInvocation &res, llvm::opt::ArgList &args,
if (args.hasArg(clang::driver::options::OPT_flarge_sizes))
res.getDefaultKinds().set_sizeIntegerKind(8);

// -fcuda
if (args.hasArg(clang::driver::options::OPT_fcuda)) {
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
13 changes: 13 additions & 0 deletions flang/test/Driver/cuda-option.f90
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
! Test -fcuda option
! RUN: %flang -fc1 -cpp -fcuda -fdebug-unparse %s -o - | FileCheck %s
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add a RUN line without enabling CUDA? Otherwise it's hard to see what's being tested and what the impact of enabling CUDA is.

Copy link
Contributor Author

@clementval clementval Mar 12, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Without it, it would just fail during parsing. Do you want a run line that check the failure?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added


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
1 change: 1 addition & 0 deletions flang/test/Driver/driver-help-hidden.f90
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
! CHECK-NEXT: -fbackslash Specify that backslash in string introduces an escape character
! CHECK-NEXT: -fcolor-diagnostics Enable colors in diagnostics
! CHECK-NEXT: -fconvert=<value> Set endian conversion of data for unformatted files
! CHECK-NEXT: -fcuda Enable CUDA
! CHECK-NEXT: -fdefault-double-8 Set the default double precision kind to an 8 byte wide type
! CHECK-NEXT: -fdefault-integer-8 Set the default integer and logical kind to an 8 byte wide type
! CHECK-NEXT: -fdefault-real-8 Set the default real kind to an 8 byte wide type
Expand Down
2 changes: 2 additions & 0 deletions flang/test/Driver/driver-help.f90
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
! HELP-NEXT: -fbackslash Specify that backslash in string introduces an escape character
! HELP-NEXT: -fcolor-diagnostics Enable colors in diagnostics
! HELP-NEXT: -fconvert=<value> Set endian conversion of data for unformatted files
! HELP-NEXT: -fcuda Enable CUDA
! HELP-NEXT: -fdefault-double-8 Set the default double precision kind to an 8 byte wide type
! HELP-NEXT: -fdefault-integer-8 Set the default integer and logical kind to an 8 byte wide type
! HELP-NEXT: -fdefault-real-8 Set the default real kind to an 8 byte wide type
Expand Down Expand Up @@ -165,6 +166,7 @@
! HELP-FC1-NEXT: -fbackslash Specify that backslash in string introduces an escape character
! HELP-FC1-NEXT: -fcolor-diagnostics Enable colors in diagnostics
! HELP-FC1-NEXT: -fconvert=<value> Set endian conversion of data for unformatted files
! HELP-FC1-NEXT: -fcuda Enable CUDA
! HELP-FC1-NEXT: -fdebug-dump-all Dump symbols and the parse tree after the semantic checks
! HELP-FC1-NEXT: -fdebug-dump-parse-tree-no-sema
! HELP-FC1-NEXT: Dump the parse tree (skips the semantic checks)
Expand Down