[clang] [flang] [flang][Driver] Accept (and ignore) some gfortran-specific options (PR #165579)

Tarun Prabhu via cfe-commits cfe-commits at lists.llvm.org
Wed Oct 29 08:36:03 PDT 2025


https://github.com/tarunprabhu updated https://github.com/llvm/llvm-project/pull/165579

>From 95baa7db0f943863c56326453adfb8a0197c4ed6 Mon Sep 17 00:00:00 2001
From: Tarun Prabhu <tarun.prabhu at gmail.com>
Date: Tue, 28 Oct 2025 19:35:27 -0600
Subject: [PATCH] [flang][Driver] Accept (and ignore) some gfortran-specific
 options

Enable the clang_ignored_gcc_optimization_f_group in flang. These options are
accepted by clang, but ignored after emitting a warning message. flang's
behavior now mirrors both clang and gfortran.

Fixes #158436
---
 clang/include/clang/Driver/Options.td |   3 +-
 clang/lib/Driver/ToolChains/Flang.cpp |   8 ++
 flang/test/Driver/flang-f-opts.f90    | 125 +++++++++++++++++++++++++-
 3 files changed, 132 insertions(+), 4 deletions(-)

diff --git a/clang/include/clang/Driver/Options.td b/clang/include/clang/Driver/Options.td
index cb5cb888c6da7..402cadab53e0e 100644
--- a/clang/include/clang/Driver/Options.td
+++ b/clang/include/clang/Driver/Options.td
@@ -316,7 +316,8 @@ def mno_mpx : Flag<["-"], "mno-mpx">, Group<clang_ignored_legacy_options_Group>;
 
 // Group that ignores all gcc optimizations that won't be implemented
 def clang_ignored_gcc_optimization_f_Group : OptionGroup<
-  "<clang_ignored_gcc_optimization_f_Group>">, Group<f_Group>, Flags<[Ignored]>;
+  "<clang_ignored_gcc_optimization_f_Group>">,
+  Group<f_Group>, Flags<[Ignored]>, Visibility<[ClangOption, FlangOption]>;
 
 class DiagnosticOpts<string base>
   : KeyPathAndMacro<"DiagnosticOpts->", base, "DIAG_"> {}
diff --git a/clang/lib/Driver/ToolChains/Flang.cpp b/clang/lib/Driver/ToolChains/Flang.cpp
index 88bce181d40d2..b1e7643e8fd97 100644
--- a/clang/lib/Driver/ToolChains/Flang.cpp
+++ b/clang/lib/Driver/ToolChains/Flang.cpp
@@ -952,6 +952,14 @@ void Flang::ConstructJob(Compilation &C, const JobAction &JA,
     if (const Arg *A = Args.getLastArg(Opt))
       D.Diag(diag::warn_drv_invalid_argument_for_flang) << A->getSpelling();
 
+  // Warn about options that are ignored by flang. These are options that are
+  // accepted by gfortran, but have no equivalent in flang.
+  for (const Arg *A :
+       Args.filtered(options::OPT_clang_ignored_gcc_optimization_f_Group)) {
+    D.Diag(diag::warn_ignored_gcc_optimization) << A->getAsString(Args);
+    A->claim();
+  }
+
   const InputInfo &Input = Inputs[0];
   types::ID InputType = Input.getType();
 
diff --git a/flang/test/Driver/flang-f-opts.f90 b/flang/test/Driver/flang-f-opts.f90
index 9ef0abaa176f0..e29d7c1d5e671 100644
--- a/flang/test/Driver/flang-f-opts.f90
+++ b/flang/test/Driver/flang-f-opts.f90
@@ -14,19 +14,21 @@
 ! RUN: %flang -### -S -fprofile-use=%S %s 2>&1 | FileCheck -check-prefix=CHECK-PROFILE-USE-DIR %s
 ! CHECK-PROFILE-USE-DIR: "-fprofile-use={{.*}}"
 
+! ------------------------------------------------------------------------------
 ! RUN: %flang -### -fbuiltin %s 2>&1 \
 ! RUN:     | FileCheck %s -check-prefix=WARN-BUILTIN
 ! WARN-BUILTIN: warning: '-fbuiltin' is not valid for Fortran
-
+!
 ! RUN: %flang -### -fno-builtin %s 2>&1 \
 ! RUN:     | FileCheck %s -check-prefix=WARN-NO-BUILTIN
 ! WARN-NO-BUILTIN: warning: '-fno-builtin' is not valid for Fortran
-
+!
 ! RUN: %flang -### -fbuiltin -fno-builtin %s 2>&1 \
 ! RUN:     | FileCheck %s -check-prefix=WARN-BUILTIN-MULTIPLE
 ! WARN-BUILTIN-MULTIPLE: warning: '-fbuiltin' is not valid for Fortran
 ! WARN-BUILTIN-MULTIPLE: warning: '-fno-builtin' is not valid for Fortran
-
+!
+!-------------------------------------------------------------------------------
 ! When emitting an error with a suggestion, ensure that the diagnostic message
 ! uses '-Xflang' instead of '-Xclang'. This is typically emitted when an option
 ! that is available for `flang -fc1` is passed to `flang`. We use -complex-range
@@ -43,3 +45,120 @@
 ! RUN:     | FileCheck %s -check-prefix UNKNOWN-NO-SUGGEST
 !
 ! UNKNOWN-NO-SUGGEST: error: unknown argument: '-not-an-option'{{$}}
+!
+!-------------------------------------------------------------------------------
+! The options in the command below are gfortran-specific optimization flags that
+! are accepted by flang's driver but ignored. Check that the correct warning
+! message is displayed when these are used.
+!
+! RUN: %flang -### %s                                                       \
+! RUN:     -finline-limit=1000                                              \
+! RUN:     -finline-limit                                                   \
+! RUN:     -fexpensive-optimizations                                        \
+! RUN:     -fno-expensive-optimizations                                     \
+! RUN:     -fno-defer-pop                                                   \
+! RUN:     -fkeep-inline-functions                                          \
+! RUN:     -fno-keep-inline-functions                                       \
+! RUN:     -freorder-blocks                                                 \
+! RUN:     -ffloat-store                                                    \
+! RUN:     -fgcse                                                           \
+! RUN:     -fivopts                                                         \
+! RUN:     -fprefetch-loop-arrays                                           \
+! RUN:     -fprofile-correction                                             \
+! RUN:     -fprofile-values                                                 \
+! RUN:     -fschedule-insns                                                 \
+! RUN:     -fsignaling-nans                                                 \
+! RUN:     -fstrength-reduce                                                \
+! RUN:     -ftracer                                                         \
+! RUN:     -funroll-all-loops                                               \
+! RUN:     -funswitch-loops                                                 \
+! RUN:     -falign-labels                                                   \
+! RUN:     -falign-labels=100                                               \
+! RUN:     -falign-jumps                                                    \
+! RUN:     -falign-jumps=100                                                \
+! RUN:     -fbranch-count-reg                                               \
+! RUN:     -fcaller-saves                                                   \
+! RUN:     -fno-default-inline                                              \
+! RUN:     -fgcse-after-reload                                              \
+! RUN:     -fgcse-las                                                       \
+! RUN:     -fgcse-sm                                                        \
+! RUN:     -fipa-cp                                                         \
+! RUN:     -finline-functions-called-once                                   \
+! RUN:     -fmodulo-sched                                                   \
+! RUN:     -fmodulo-sched-allow-regmoves                                    \
+! RUN:     -fpeel-loops                                                     \
+! RUN:     -frename-registers                                               \
+! RUN:     -fschedule-insns2                                                \
+! RUN:     -fsingle-precision-constant                                      \
+! RUN:     -funsafe-loop-optimizations                                      \
+! RUN:     -fuse-linker-plugin                                              \
+! RUN:     -fvect-cost-model                                                \
+! RUN:     -fvariable-expansion-in-unroller                                 \
+! RUN:     -fweb                                                            \
+! RUN:     -fwhole-program                                                  \
+! RUN:     -fcaller-saves                                                   \
+! RUN:     -freorder-blocks                                                 \
+! RUN:     -ffat-lto-objects                                                \
+! RUN:     -fmerge-constants                                                \
+! RUN:     -finline-small-functions                                         \
+! RUN:     -ftree-dce                                                       \
+! RUN:     -ftree-ter                                                       \
+! RUN:     -ftree-vrp                                                       \
+! RUN:     -fno-devirtualize                                                \
+! RUN:     -fno-devirtualize-speculatively 2>&1                             \
+! RUN:     | FileCheck --check-prefix=CHECK-WARNING %s
+! CHECK-WARNING-DAG: optimization flag '-finline-limit=1000' is not supported
+! CHECK-WARNING-DAG: optimization flag '-finline-limit' is not supported
+! CHECK-WARNING-DAG: optimization flag '-fexpensive-optimizations' is not supported
+! CHECK-WARNING-DAG: optimization flag '-fno-expensive-optimizations' is not supported
+! CHECK-WARNING-DAG: optimization flag '-fno-defer-pop' is not supported
+! CHECK-WARNING-DAG: optimization flag '-fkeep-inline-functions' is not supported
+! CHECK-WARNING-DAG: optimization flag '-fno-keep-inline-functions' is not supported
+! CHECK-WARNING-DAG: optimization flag '-freorder-blocks' is not supported
+! CHECK-WARNING-DAG: optimization flag '-ffloat-store' is not supported
+! CHECK-WARNING-DAG: optimization flag '-fgcse' is not supported
+! CHECK-WARNING-DAG: optimization flag '-fivopts' is not supported
+! CHECK-WARNING-DAG: optimization flag '-fprefetch-loop-arrays' is not supported
+! CHECK-WARNING-DAG: optimization flag '-fprofile-correction' is not supported
+! CHECK-WARNING-DAG: optimization flag '-fprofile-values' is not supported
+! CHECK-WARNING-DAG: optimization flag '-fschedule-insns' is not supported
+! CHECK-WARNING-DAG: optimization flag '-fsignaling-nans' is not supported
+! CHECK-WARNING-DAG: optimization flag '-fstrength-reduce' is not supported
+! CHECK-WARNING-DAG: optimization flag '-ftracer' is not supported
+! CHECK-WARNING-DAG: optimization flag '-funroll-all-loops' is not supported
+! CHECK-WARNING-DAG: optimization flag '-funswitch-loops' is not supported
+! CHECK-WARNING-DAG: optimization flag '-falign-labels' is not supported
+! CHECK-WARNING-DAG: optimization flag '-falign-labels=100' is not supported
+! CHECK-WARNING-DAG: optimization flag '-falign-jumps' is not supported
+! CHECK-WARNING-DAG: optimization flag '-falign-jumps=100' is not supported
+! CHECK-WARNING-DAG: optimization flag '-fbranch-count-reg' is not supported
+! CHECK-WARNING-DAG: optimization flag '-fcaller-saves' is not supported
+! CHECK-WARNING-DAG: optimization flag '-fno-default-inline' is not supported
+! CHECK-WARNING-DAG: optimization flag '-fgcse-after-reload' is not supported
+! CHECK-WARNING-DAG: optimization flag '-fgcse-las' is not supported
+! CHECK-WARNING-DAG: optimization flag '-fgcse-sm' is not supported
+! CHECK-WARNING-DAG: optimization flag '-fipa-cp' is not supported
+! CHECK-WARNING-DAG: optimization flag '-finline-functions-called-once' is not supported
+! CHECK-WARNING-DAG: optimization flag '-fmodulo-sched' is not supported
+! CHECK-WARNING-DAG: optimization flag '-fmodulo-sched-allow-regmoves' is not supported
+! CHECK-WARNING-DAG: optimization flag '-fpeel-loops' is not supported
+! CHECK-WARNING-DAG: optimization flag '-frename-registers' is not supported
+! CHECK-WARNING-DAG: optimization flag '-fschedule-insns2' is not supported
+! CHECK-WARNING-DAG: optimization flag '-fsingle-precision-constant' is not supported
+! CHECK-WARNING-DAG: optimization flag '-funsafe-loop-optimizations' is not supported
+! CHECK-WARNING-DAG: optimization flag '-fuse-linker-plugin' is not supported
+! CHECK-WARNING-DAG: optimization flag '-fvect-cost-model' is not supported
+! CHECK-WARNING-DAG: optimization flag '-fvariable-expansion-in-unroller' is not supported
+! CHECK-WARNING-DAG: optimization flag '-fweb' is not supported
+! CHECK-WARNING-DAG: optimization flag '-fwhole-program' is not supported
+! CHECK-WARNING-DAG: optimization flag '-fcaller-saves' is not supported
+! CHECK-WARNING-DAG: optimization flag '-freorder-blocks' is not supported
+! CHECK-WARNING-DAG: optimization flag '-fmerge-constants' is not supported
+! CHECK-WARNING-DAG: optimization flag '-finline-small-functions' is not supported
+! CHECK-WARNING-DAG: optimization flag '-ftree-dce' is not supported
+! CHECK-WARNING-DAG: optimization flag '-ftree-ter' is not supported
+! CHECK-WARNING-DAG: optimization flag '-ftree-vrp' is not supported
+! CHECK-WARNING-DAG: optimization flag '-fno-devirtualize' is not supported
+! CHECK-WARNING-DAG: optimization flag '-fno-devirtualize-speculatively' is not supported
+!
+! ------------------------------------------------------------------------------



More information about the cfe-commits mailing list