[clang] [flang] [flang][driver] Introduce FCC_OVERRIDE_OPTIONS. (PR #140556)
Abid Qadeer via cfe-commits
cfe-commits at lists.llvm.org
Fri May 30 08:09:37 PDT 2025
https://github.com/abidh updated https://github.com/llvm/llvm-project/pull/140556
>From 5d20af48673adebc2ab3e1a6c8442f67d84f1847 Mon Sep 17 00:00:00 2001
From: Abid Qadeer <haqadeer at amd.com>
Date: Mon, 19 May 2025 15:21:25 +0100
Subject: [PATCH 1/7] [flang][driver] Introduce FCC_OVERRIDE_OPTIONS.
This PR add functionality to change flang command line using
environment variable `FCC_OVERRIDE_OPTIONS`. It is quite similar to
what `CCC_OVERRIDE_OPTIONS` does for clang.
The `FCC_OVERRIDE_OPTIONS` seemed like the most obvious name to me but
I am open to other ideas. The `applyOverrideOptions` now takes an extra
argument that is the name of the environment variable. Previously
`CCC_OVERRIDE_OPTIONS` was hardcoded.
---
clang/include/clang/Driver/Driver.h | 2 +-
clang/lib/Driver/Driver.cpp | 4 ++--
clang/tools/driver/driver.cpp | 2 +-
flang/test/Driver/fcc_override.f90 | 12 ++++++++++++
flang/tools/flang-driver/driver.cpp | 7 +++++++
5 files changed, 23 insertions(+), 4 deletions(-)
create mode 100644 flang/test/Driver/fcc_override.f90
diff --git a/clang/include/clang/Driver/Driver.h b/clang/include/clang/Driver/Driver.h
index b463dc2a93550..7ca848f11b561 100644
--- a/clang/include/clang/Driver/Driver.h
+++ b/clang/include/clang/Driver/Driver.h
@@ -879,7 +879,7 @@ llvm::Error expandResponseFiles(SmallVectorImpl<const char *> &Args,
/// See applyOneOverrideOption.
void applyOverrideOptions(SmallVectorImpl<const char *> &Args,
const char *OverrideOpts,
- llvm::StringSet<> &SavedStrings,
+ llvm::StringSet<> &SavedStrings, StringRef EnvVar,
raw_ostream *OS = nullptr);
} // end namespace driver
diff --git a/clang/lib/Driver/Driver.cpp b/clang/lib/Driver/Driver.cpp
index a648cc928afdc..a8fea35926a0d 100644
--- a/clang/lib/Driver/Driver.cpp
+++ b/clang/lib/Driver/Driver.cpp
@@ -7289,7 +7289,7 @@ static void applyOneOverrideOption(raw_ostream &OS,
void driver::applyOverrideOptions(SmallVectorImpl<const char *> &Args,
const char *OverrideStr,
llvm::StringSet<> &SavedStrings,
- raw_ostream *OS) {
+ StringRef EnvVar, raw_ostream *OS) {
if (!OS)
OS = &llvm::nulls();
@@ -7298,7 +7298,7 @@ void driver::applyOverrideOptions(SmallVectorImpl<const char *> &Args,
OS = &llvm::nulls();
}
- *OS << "### CCC_OVERRIDE_OPTIONS: " << OverrideStr << "\n";
+ *OS << "### " << EnvVar << ": " << OverrideStr << "\n";
// This does not need to be efficient.
diff --git a/clang/tools/driver/driver.cpp b/clang/tools/driver/driver.cpp
index 82f47ab973064..81964c65c2892 100644
--- a/clang/tools/driver/driver.cpp
+++ b/clang/tools/driver/driver.cpp
@@ -305,7 +305,7 @@ int clang_main(int Argc, char **Argv, const llvm::ToolContext &ToolContext) {
if (const char *OverrideStr = ::getenv("CCC_OVERRIDE_OPTIONS")) {
// FIXME: Driver shouldn't take extra initial argument.
driver::applyOverrideOptions(Args, OverrideStr, SavedStrings,
- &llvm::errs());
+ "CCC_OVERRIDE_OPTIONS", &llvm::errs());
}
std::string Path = GetExecutablePath(ToolContext.Path, CanonicalPrefixes);
diff --git a/flang/test/Driver/fcc_override.f90 b/flang/test/Driver/fcc_override.f90
new file mode 100644
index 0000000000000..55a07803fdde5
--- /dev/null
+++ b/flang/test/Driver/fcc_override.f90
@@ -0,0 +1,12 @@
+! RUN: env FCC_OVERRIDE_OPTIONS="#+-Os +-Oz +-O +-O3 +-Oignore +a +b +c xb Xa Omagic ^-### " %flang -target x86_64-unknown-linux-gnu %s -O2 b -O3 2>&1 | FileCheck %s
+! RUN: env FCC_OVERRIDE_OPTIONS="x-Werror +-g" %flang -target x86_64-unknown-linux-gnu -Werror %s -c -### 2>&1 | FileCheck %s -check-prefix=RM-WERROR
+
+! CHECK: "-fc1"
+! CHECK-NOT: "-Oignore"
+! CHECK: "-Omagic"
+! CHECK-NOT: "-Oignore"
+
+! RM-WERROR: ### FCC_OVERRIDE_OPTIONS: x-Werror +-g
+! RM-WERROR-NEXT: ### Deleting argument -Werror
+! RM-WERROR-NEXT: ### Adding argument -g at end
+! RM-WERROR-NOT: "-Werror"
diff --git a/flang/tools/flang-driver/driver.cpp b/flang/tools/flang-driver/driver.cpp
index ed52988feaa59..ad0efa3279cef 100644
--- a/flang/tools/flang-driver/driver.cpp
+++ b/flang/tools/flang-driver/driver.cpp
@@ -111,6 +111,13 @@ int main(int argc, const char **argv) {
}
}
+ llvm::StringSet<> SavedStrings;
+ // Handle FCC_OVERRIDE_OPTIONS, used for editing a command line behind the
+ // scenes.
+ if (const char *OverrideStr = ::getenv("FCC_OVERRIDE_OPTIONS"))
+ clang::driver::applyOverrideOptions(args, OverrideStr, SavedStrings,
+ "FCC_OVERRIDE_OPTIONS", &llvm::errs());
+
// Not in the frontend mode - continue in the compiler driver mode.
// Create DiagnosticsEngine for the compiler driver
>From d1f2c9b8abd2690612a4b886a7a85b8e7f57d359 Mon Sep 17 00:00:00 2001
From: Abid Qadeer <haqadeer at amd.com>
Date: Thu, 29 May 2025 11:05:57 +0100
Subject: [PATCH 2/7] Add documentation for FCC_OVERRIDE_OPTIONS.
---
flang/docs/FlangDriver.md | 25 +++++++++++++++++++++++++
1 file changed, 25 insertions(+)
diff --git a/flang/docs/FlangDriver.md b/flang/docs/FlangDriver.md
index 97744f0bee069..f93df8701e677 100644
--- a/flang/docs/FlangDriver.md
+++ b/flang/docs/FlangDriver.md
@@ -614,3 +614,28 @@ nvfortran defines `-fast` as
- `-Mcache_align`: there is no equivalent flag in Flang or Clang.
- `-Mflushz`: flush-to-zero mode - when `-ffast-math` is specified, Flang will
link to `crtfastmath.o` to ensure denormal numbers are flushed to zero.
+
+
+## FCC_OVERRIDE_OPTIONS
+
+The environment variable `FCC_OVERRIDE_OPTIONS` can be used to apply a list of
+edits to the input argument lists. The value of this environment variable is
+a space separated list of edits to perform. These edits are applied in order to
+the input argument lists. Edits should be one of the following forms:
+
+- `#`: Silence information about the changes to the command line arguments.
+
+- `^FOO`: Add `FOO` as a new argument at the beginning of the command line.
+
+- `+FOO`: Add `FOO` as a new argument at the end of the command line.
+
+- `s/XXX/YYY/`: Substitute the regular expression `XXX` with `YYY` in the
+ command line.
+
+- `xOPTION`: Removes all instances of the literal argument `OPTION`.
+
+- `XOPTION`: Removes all instances of the literal argument `OPTION`, and the
+ following argument.
+
+- `Ox`: Removes all flags matching `O` or `O[sz0-9]` and adds `Ox` at the end
+ of the command line.
\ No newline at end of file
>From d093a6ac74f8c0058e134ec55fbbf2b8edf9b477 Mon Sep 17 00:00:00 2001
From: Abid Qadeer <haqadeer at amd.com>
Date: Thu, 29 May 2025 17:28:46 +0100
Subject: [PATCH 3/7] Mention that effect on options added by the config files.
---
flang/docs/FlangDriver.md | 4 +++-
1 file changed, 3 insertions(+), 1 deletion(-)
diff --git a/flang/docs/FlangDriver.md b/flang/docs/FlangDriver.md
index f93df8701e677..0302cb1dc33b9 100644
--- a/flang/docs/FlangDriver.md
+++ b/flang/docs/FlangDriver.md
@@ -638,4 +638,6 @@ the input argument lists. Edits should be one of the following forms:
following argument.
- `Ox`: Removes all flags matching `O` or `O[sz0-9]` and adds `Ox` at the end
- of the command line.
\ No newline at end of file
+ of the command line.
+
+This environment variable does not affect the options added by the config files.
>From 9faf4d384a40514b15cc3bf270303843e8dd4822 Mon Sep 17 00:00:00 2001
From: Abid Qadeer <haqadeer at amd.com>
Date: Thu, 29 May 2025 20:36:59 +0100
Subject: [PATCH 4/7] Add a test for option from config file.
---
flang/test/Driver/Inputs/config-7.cfg | 1 +
flang/test/Driver/fcc_override.f90 | 5 +++++
2 files changed, 6 insertions(+)
create mode 100644 flang/test/Driver/Inputs/config-7.cfg
diff --git a/flang/test/Driver/Inputs/config-7.cfg b/flang/test/Driver/Inputs/config-7.cfg
new file mode 100644
index 0000000000000..2f41be663b282
--- /dev/null
+++ b/flang/test/Driver/Inputs/config-7.cfg
@@ -0,0 +1 @@
+-Werror
diff --git a/flang/test/Driver/fcc_override.f90 b/flang/test/Driver/fcc_override.f90
index 55a07803fdde5..417919b5d667a 100644
--- a/flang/test/Driver/fcc_override.f90
+++ b/flang/test/Driver/fcc_override.f90
@@ -1,5 +1,6 @@
! RUN: env FCC_OVERRIDE_OPTIONS="#+-Os +-Oz +-O +-O3 +-Oignore +a +b +c xb Xa Omagic ^-### " %flang -target x86_64-unknown-linux-gnu %s -O2 b -O3 2>&1 | FileCheck %s
! RUN: env FCC_OVERRIDE_OPTIONS="x-Werror +-g" %flang -target x86_64-unknown-linux-gnu -Werror %s -c -### 2>&1 | FileCheck %s -check-prefix=RM-WERROR
+! RUN: env FCC_OVERRIDE_OPTIONS="x-Werror" %flang --config=%S/Inputs/config-7.cfg -### %s -c 2>&1 | FileCheck %s -check-prefix=CONF
! CHECK: "-fc1"
! CHECK-NOT: "-Oignore"
@@ -10,3 +11,7 @@
! RM-WERROR-NEXT: ### Deleting argument -Werror
! RM-WERROR-NEXT: ### Adding argument -g at end
! RM-WERROR-NOT: "-Werror"
+
+! Test that FCC_OVERRIDE_OPTIONS does not affect the options from config files.
+! CONF: ### FCC_OVERRIDE_OPTIONS: x-Werror
+! CONF: "-Werror"
>From db45474fc5625223b5240aa7f7ef094d2d80d5ae Mon Sep 17 00:00:00 2001
From: Abid Qadeer <haqadeer at amd.com>
Date: Fri, 30 May 2025 10:28:49 +0100
Subject: [PATCH 5/7] Handle review comments.
---
flang/docs/FlangDriver.md | 8 ++++----
flang/test/Driver/fcc_override.f90 | 2 +-
2 files changed, 5 insertions(+), 5 deletions(-)
diff --git a/flang/docs/FlangDriver.md b/flang/docs/FlangDriver.md
index 0302cb1dc33b9..e6750c92567a4 100644
--- a/flang/docs/FlangDriver.md
+++ b/flang/docs/FlangDriver.md
@@ -618,10 +618,10 @@ nvfortran defines `-fast` as
## FCC_OVERRIDE_OPTIONS
-The environment variable `FCC_OVERRIDE_OPTIONS` can be used to apply a list of
-edits to the input argument lists. The value of this environment variable is
-a space separated list of edits to perform. These edits are applied in order to
-the input argument lists. Edits should be one of the following forms:
+The environment variable `FCC_OVERRIDE_OPTIONS` can be used to edit flang's
+command line arguments. The value of this variable is a space-separated list of
+edits to perform. The edits are applied in the order in which they appear in
+`FCC_OVERRIDE_OPTIONS`. Each edit should be one of the following forms:
- `#`: Silence information about the changes to the command line arguments.
diff --git a/flang/test/Driver/fcc_override.f90 b/flang/test/Driver/fcc_override.f90
index 417919b5d667a..71def0847f150 100644
--- a/flang/test/Driver/fcc_override.f90
+++ b/flang/test/Driver/fcc_override.f90
@@ -1,5 +1,5 @@
! RUN: env FCC_OVERRIDE_OPTIONS="#+-Os +-Oz +-O +-O3 +-Oignore +a +b +c xb Xa Omagic ^-### " %flang -target x86_64-unknown-linux-gnu %s -O2 b -O3 2>&1 | FileCheck %s
-! RUN: env FCC_OVERRIDE_OPTIONS="x-Werror +-g" %flang -target x86_64-unknown-linux-gnu -Werror %s -c -### 2>&1 | FileCheck %s -check-prefix=RM-WERROR
+! RUN: env FCC_OVERRIDE_OPTIONS="x-Werror +-g" %flang --target=x86_64-unknown-linux-gnu -Werror %s -c -### 2>&1 | FileCheck %s -check-prefix=RM-WERROR
! RUN: env FCC_OVERRIDE_OPTIONS="x-Werror" %flang --config=%S/Inputs/config-7.cfg -### %s -c 2>&1 | FileCheck %s -check-prefix=CONF
! CHECK: "-fc1"
>From 2efd653c1a575fc77d53d5db7db1cf5225f8039d Mon Sep 17 00:00:00 2001
From: Abid Qadeer <haqadeer at amd.com>
Date: Fri, 30 May 2025 11:13:29 +0100
Subject: [PATCH 6/7] Handle review comments.
---
flang/tools/flang-driver/driver.cpp | 6 +++---
1 file changed, 3 insertions(+), 3 deletions(-)
diff --git a/flang/tools/flang-driver/driver.cpp b/flang/tools/flang-driver/driver.cpp
index ad0efa3279cef..62dbfc72bb191 100644
--- a/flang/tools/flang-driver/driver.cpp
+++ b/flang/tools/flang-driver/driver.cpp
@@ -111,11 +111,11 @@ int main(int argc, const char **argv) {
}
}
- llvm::StringSet<> SavedStrings;
+ llvm::StringSet<> savedStrings;
// Handle FCC_OVERRIDE_OPTIONS, used for editing a command line behind the
// scenes.
- if (const char *OverrideStr = ::getenv("FCC_OVERRIDE_OPTIONS"))
- clang::driver::applyOverrideOptions(args, OverrideStr, SavedStrings,
+ if (const char *overrideStr = ::getenv("FCC_OVERRIDE_OPTIONS"))
+ clang::driver::applyOverrideOptions(args, overrideStr, savedStrings,
"FCC_OVERRIDE_OPTIONS", &llvm::errs());
// Not in the frontend mode - continue in the compiler driver mode.
>From f41f8ffe3d537291fd870fb649f4d4b31361c262 Mon Sep 17 00:00:00 2001
From: Abid Qadeer <haqadeer at amd.com>
Date: Fri, 30 May 2025 16:09:19 +0100
Subject: [PATCH 7/7] Replace -target with --target= to address review
comments.
---
flang/test/Driver/fcc_override.f90 | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/flang/test/Driver/fcc_override.f90 b/flang/test/Driver/fcc_override.f90
index 71def0847f150..2717d203c2ea3 100644
--- a/flang/test/Driver/fcc_override.f90
+++ b/flang/test/Driver/fcc_override.f90
@@ -1,4 +1,4 @@
-! RUN: env FCC_OVERRIDE_OPTIONS="#+-Os +-Oz +-O +-O3 +-Oignore +a +b +c xb Xa Omagic ^-### " %flang -target x86_64-unknown-linux-gnu %s -O2 b -O3 2>&1 | FileCheck %s
+! RUN: env FCC_OVERRIDE_OPTIONS="#+-Os +-Oz +-O +-O3 +-Oignore +a +b +c xb Xa Omagic ^-### " %flang --target=x86_64-unknown-linux-gnu %s -O2 b -O3 2>&1 | FileCheck %s
! RUN: env FCC_OVERRIDE_OPTIONS="x-Werror +-g" %flang --target=x86_64-unknown-linux-gnu -Werror %s -c -### 2>&1 | FileCheck %s -check-prefix=RM-WERROR
! RUN: env FCC_OVERRIDE_OPTIONS="x-Werror" %flang --config=%S/Inputs/config-7.cfg -### %s -c 2>&1 | FileCheck %s -check-prefix=CONF
More information about the cfe-commits
mailing list