Skip to content

[flang][driver] Introduce FCC_OVERRIDE_OPTIONS. #140556

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 9 commits into from
Jun 2, 2025

Conversation

abidh
Copy link
Contributor

@abidh abidh commented May 19, 2025

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.

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.
@llvmbot llvmbot added clang Clang issues not falling into any other category clang:driver 'clang' and 'clang++' user-facing binaries. Not 'clang-cl' flang:driver flang Flang issues not falling into any other category labels May 19, 2025
@llvmbot
Copy link
Member

llvmbot commented May 19, 2025

@llvm/pr-subscribers-clang
@llvm/pr-subscribers-flang-driver

@llvm/pr-subscribers-clang-driver

Author: Abid Qadeer (abidh)

Changes

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.


Full diff: https://github.com/llvm/llvm-project/pull/140556.diff

5 Files Affected:

  • (modified) clang/include/clang/Driver/Driver.h (+1-1)
  • (modified) clang/lib/Driver/Driver.cpp (+2-2)
  • (modified) clang/tools/driver/driver.cpp (+1-1)
  • (added) flang/test/Driver/fcc_override.f90 (+12)
  • (modified) flang/tools/flang-driver/driver.cpp (+7)
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

@tarunprabhu
Copy link
Contributor

The FCC_OVERRIDE_OPTIONS seemed like the most obvious name to me but I am open to other ideas.

Thanks Abid. Perhaps FFC_OVERRIDE_OPTIONS? It has a similar correspondence to CCC_OVERRIDE_OPTIONS as FCFLAGS does to CCFLAGS. But I don't have a strong opinion on this.

@kiranchandramohan
Copy link
Contributor

Does CCC_OVERRIDE_OPTIONS expands to Clang Compiler Commandline Override Options? If so FCC_OVERRIDE_OPTIONS expanding to Fortran Compiler Commandline Override Options seems the right replacement.

If CCC_OVERRIDE_OPTIONS expands to Clang C Compiler Override Options then FFC_OVERRIDE_OPTIONS (as suggested by @tarunprabhu) expanding to Flang Fortran Compiler Overrided Options is better.

@@ -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
Copy link
Contributor

Choose a reason for hiding this comment

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

Does +, x,X have special meanings? Is that documented anywhere?

I think we should document FCC_OVERRIDE_OPTIONS. One possible location is https://github.com/llvm/llvm-project/blob/main/flang/docs/FlangDriver.md

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, those have special meaning and they were only documented in the comments. I have now added documentation for it in the suggested file.

Copy link
Member

Choose a reason for hiding this comment

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

--target=x86_64-unknown-linux-gnu instead of the long deprecated -target

@abidh
Copy link
Contributor Author

abidh commented May 29, 2025

Does CCC_OVERRIDE_OPTIONS expands to Clang Compiler Commandline Override Options? If so FCC_OVERRIDE_OPTIONS expanding to Fortran Compiler Commandline Override Options seems the right replacement.

If CCC_OVERRIDE_OPTIONS expands to Clang C Compiler Override Options then FFC_OVERRIDE_OPTIONS (as suggested by @tarunprabhu) expanding to Flang Fortran Compiler Overrided Options is better.

The CCC_OVERRIDE_OPTIONS was introduced in a19fad as replacement of QA_OVERRIDE_GCC3_OPTIONS. I am not sure if there is a definite word on what it actually expands to. I am happy with either of them.

@tarunprabhu
Copy link
Contributor

Does CCC_OVERRIDE_OPTIONS expands to Clang Compiler Commandline Override Options? If so FCC_OVERRIDE_OPTIONS expanding to Fortran Compiler Commandline Override Options seems the right replacement.
If CCC_OVERRIDE_OPTIONS expands to Clang C Compiler Override Options then FFC_OVERRIDE_OPTIONS (as suggested by @tarunprabhu) expanding to Flang Fortran Compiler Overrided Options is better.

I am happy with either of them.

Maybe we should split the difference and call it FFF_OVERRIDE_OPTIONS. More Fortran for everyone! :-)

Copy link
Contributor

@tarunprabhu tarunprabhu left a comment

Choose a reason for hiding this comment

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

Thanks for the changes, Abid. Regarding my suggestions in the documentation, feel free to incorporate or discard them as you see fit.

I'll leave it you and @kiranchandramohan to decide what the name of the environment variable should be.

Other than that, this looks good.

Comment on lines 621 to 624
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:
Copy link
Contributor

Choose a reason for hiding this comment

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

What do you think of this?

Suggested change
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:

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks. This looks better than what I had originally. I have updated the docs.

Copy link
Member

@MaskRay MaskRay left a comment

Choose a reason for hiding this comment

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

clang driver code looks good

@@ -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
Copy link
Member

Choose a reason for hiding this comment

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

--target=x86_64-unknown-linux-gnu instead of the long deprecated -target

Copy link
Contributor

@kiranchandramohan kiranchandramohan left a comment

Choose a reason for hiding this comment

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

LGTM. Thanks.

llvm::StringSet<> SavedStrings;
// Handle FCC_OVERRIDE_OPTIONS, used for editing a command line behind the
// scenes.
if (const char *OverrideStr = ::getenv("FCC_OVERRIDE_OPTIONS"))
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
if (const char *OverrideStr = ::getenv("FCC_OVERRIDE_OPTIONS"))
if (const char *overrideStr = ::getenv("FCC_OVERRIDE_OPTIONS"))

@@ -111,6 +111,13 @@ int main(int argc, const char **argv) {
}
}

llvm::StringSet<> SavedStrings;
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
llvm::StringSet<> SavedStrings;
llvm::StringSet<> savedStrings;

@@ -0,0 +1,17 @@
! 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
Copy link
Contributor

@tarunprabhu tarunprabhu May 30, 2025

Choose a reason for hiding this comment

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

Should this also be --target?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Indeed. Thanks for catching this. Fixed now.

Copy link
Contributor

@tarunprabhu tarunprabhu left a comment

Choose a reason for hiding this comment

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

Thanks Abid. LGTM.


- `#`: Silence information about the changes to the command line arguments.

- `^FOO`: Add `FOO` as a new argument at the beginning of the command line.
Copy link
Member

Choose a reason for hiding this comment

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

For clang a.cc, this actually adds FOO after clang. The comment in clang/lib/Driver/Driver.cpp and this description probably should be made more accurate.

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 have added "right after the name of the compiler executable" in both places. Hopefully that is enough to make it clear.

@@ -614,3 +614,30 @@ 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
Copy link
Member

Choose a reason for hiding this comment

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

I noticed that clang/docs doesn't have the documentation. Perhaps we should document clang/docs/UsersManual.rst as well and reference it from FlangDriver.md?

("Each edit should be one of the following forms:" list should probably reference the clang doc)

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 will add clang documentation in a separate PR. The FCC_OVERRIDE_OPTIONS one can then probably just refer to it.

@abidh abidh merged commit de38c4e into llvm:main Jun 2, 2025
10 of 12 checks passed
@abidh
Copy link
Contributor Author

abidh commented Jun 2, 2025

The test failure in Lower/namelist.f90 was due to CHECK-NOT: bbb while bbb can appeared in compiler generated variable name so unrelated to this change. Opened #142363 to harden that testcase.

abidh added a commit to abidh/llvm-project that referenced this pull request Jun 2, 2025
As was noted in llvm#140556 (comment),
there is no documentation for `CCC_OVERRIDE_OPTIONS`. This adds the
missing documentation. The information is duplicate of what we have for
`FCC_OVERRIDE_OPTIONS` in flang. Once this goes in and available at
https://clang.llvm.org/docs/UsersManual.html then the flang
documentation can be changed to refer to it.
abidh added a commit that referenced this pull request Jun 3, 2025
As was noted in
#140556 (comment),
there is no documentation for `CCC_OVERRIDE_OPTIONS`. This adds the
missing documentation. The information is duplicate of what we have for
`FCC_OVERRIDE_OPTIONS` in flang. Once this goes in and available at
https://clang.llvm.org/docs/UsersManual.html then the flang
documentation can be changed to refer to it.
sallto pushed a commit to sallto/llvm-project that referenced this pull request Jun 3, 2025
As was noted in
llvm#140556 (comment),
there is no documentation for `CCC_OVERRIDE_OPTIONS`. This adds the
missing documentation. The information is duplicate of what we have for
`FCC_OVERRIDE_OPTIONS` in flang. Once this goes in and available at
https://clang.llvm.org/docs/UsersManual.html then the flang
documentation can be changed to refer to it.
llvm-sync bot pushed a commit to arm/arm-toolchain that referenced this pull request Jun 3, 2025
As was noted in
llvm/llvm-project#140556 (comment),
there is no documentation for `CCC_OVERRIDE_OPTIONS`. This adds the
missing documentation. The information is duplicate of what we have for
`FCC_OVERRIDE_OPTIONS` in flang. Once this goes in and available at
https://clang.llvm.org/docs/UsersManual.html then the flang
documentation can be changed to refer to it.
rorth pushed a commit to rorth/llvm-project that referenced this pull request Jun 11, 2025
As was noted in
llvm#140556 (comment),
there is no documentation for `CCC_OVERRIDE_OPTIONS`. This adds the
missing documentation. The information is duplicate of what we have for
`FCC_OVERRIDE_OPTIONS` in flang. Once this goes in and available at
https://clang.llvm.org/docs/UsersManual.html then the flang
documentation can be changed to refer to it.
DhruvSrivastavaX pushed a commit to DhruvSrivastavaX/lldb-for-aix that referenced this pull request Jun 12, 2025
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.
DhruvSrivastavaX pushed a commit to DhruvSrivastavaX/lldb-for-aix that referenced this pull request Jun 12, 2025
As was noted in
llvm#140556 (comment),
there is no documentation for `CCC_OVERRIDE_OPTIONS`. This adds the
missing documentation. The information is duplicate of what we have for
`FCC_OVERRIDE_OPTIONS` in flang. Once this goes in and available at
https://clang.llvm.org/docs/UsersManual.html then the flang
documentation can be changed to refer to it.
searlmc1 pushed a commit to ROCm/llvm-project that referenced this pull request Jun 18, 2025
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.
searlmc1 pushed a commit to ROCm/llvm-project that referenced this pull request Jun 18, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang:driver 'clang' and 'clang++' user-facing binaries. Not 'clang-cl' clang Clang issues not falling into any other category flang:driver flang Flang issues not falling into any other category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants