[flang-commits] [flang] [Flang] Store only options in FLANG_COMPILER_OPTIONS_STRING (PR #201278)

Thirumalai Shaktivel via flang-commits flang-commits at lists.llvm.org
Sun Jun 7 23:38:30 PDT 2026


https://github.com/Thirumalai-Shaktivel updated https://github.com/llvm/llvm-project/pull/201278

>From d8756449d8d175be90038d23cbd5f8240417b39b Mon Sep 17 00:00:00 2001
From: Thirumalai-Shaktivel <thirumalaishaktivel at gmail.com>
Date: Wed, 3 Jun 2026 12:28:05 +0530
Subject: [PATCH 1/2] [Flang] Store only options in
 FLANG_COMPILER_OPTIONS_STRING

Previously, FLANG_COMPILER_OPTIONS_STRING stored every argument passed
to the flang driver, including input file names. The GNU extension
compiler_options() is documented to return only the options, not the
input files. Including the input files also caused the string to exceed
ARG_MAX on large builds, producing:

    posix_spawn failed: Argument list too long

Use the driver's parsed InputArgList to filter out OPT_INPUT arguments,
preserving all options and their values (e.g. `-I /path`, `-o file`).

Fixes: https://github.com/llvm/llvm-project/issues/170651
---
 flang/test/Driver/compiler-options.f90 |  2 +-
 flang/tools/flang-driver/driver.cpp    | 25 ++++++++++++++++---------
 2 files changed, 17 insertions(+), 10 deletions(-)

diff --git a/flang/test/Driver/compiler-options.f90 b/flang/test/Driver/compiler-options.f90
index b9ecbbda227d1..f511ad0ccf184 100644
--- a/flang/test/Driver/compiler-options.f90
+++ b/flang/test/Driver/compiler-options.f90
@@ -1,6 +1,6 @@
 ! RUN: %flang -S -emit-llvm -o - %s | FileCheck %s
 ! Test communication of COMPILER_OPTIONS from flang to flang -fc1.
-! CHECK: [[OPTSVAR:@_QQclX[0-9a-f]+]] = {{[a-z]+}} constant [[[OPTSLEN:[0-9]+]] x i8] c"{{.*}}flang{{(\.exe)?}} {{.*}}-S -emit-llvm -o - {{.*}}compiler-options.f90"
+! CHECK: [[OPTSVAR:@_QQclX[0-9A-F]+]] = {{[a-z]+}} constant [[[OPTSLEN:[0-9]+]] x i8] c"-S -emit-llvm -o -"
 program main
     use ISO_FORTRAN_ENV, only: compiler_options
     implicit none
diff --git a/flang/tools/flang-driver/driver.cpp b/flang/tools/flang-driver/driver.cpp
index 1ce50e0e49cd6..e298b6e95860c 100644
--- a/flang/tools/flang-driver/driver.cpp
+++ b/flang/tools/flang-driver/driver.cpp
@@ -23,6 +23,7 @@
 #include "clang/Basic/DiagnosticIDs.h"
 #include "clang/Basic/DiagnosticOptions.h"
 #include "clang/Driver/Compilation.h"
+#include "clang/Options/Options.h"
 #include "llvm/ADT/ArrayRef.h"
 #include "llvm/ADT/IntrusiveRefCntPtr.h"
 #include "llvm/Option/ArgList.h"
@@ -174,22 +175,28 @@ int main(int argc, const char **argv) {
   if (rejectAssemblyInputs(c->getInputArgs(), diags))
     return 1;
 
-  // Set the environment variable, FLANG_COMPILER_OPTIONS_STRING, to contain all
-  // the compiler options. This is intended for the frontend driver,
-  // flang -fc1, to enable the implementation of the COMPILER_OPTIONS
-  // intrinsic. To this end, the frontend driver requires the list of the
-  // original compiler options, which is not available through other means.
+  // Set the environment variable, FLANG_COMPILER_OPTIONS_STRING, to contain
+  // the compiler options (excluding input file names and the program name).
+  // This is intended for the frontend driver, flang -fc1, to enable the
+  // implementation of the COMPILER_OPTIONS intrinsic. To this end, the
+  // frontend driver requires the list of the original compiler options,
+  // which is not available through other means.
   // TODO: This way of passing information between the compiler and frontend
   // drivers is discouraged. We should find a better way not involving env
   // variables.
   std::string compilerOptsGathered;
   llvm::raw_string_ostream os(compilerOptsGathered);
-  for (int i = 0; i < argc; ++i) {
-    os << argv[i];
-    if (i < argc - 1) {
+  const llvm::opt::InputArgList &argList = c->getInputArgs();
+  bool first = true;
+  for (const auto *arg : argList) {
+    if (arg->getOption().matches(clang::options::OPT_INPUT))
+      continue;
+    if (!first)
       os << ' ';
-    }
+    os << arg->getAsString(argList);
+    first = false;
   }
+
 #ifdef _WIN32
   _putenv_s("FLANG_COMPILER_OPTIONS_STRING", compilerOptsGathered.c_str());
 #else

>From 598cdf1cced3b6966a715895248a6d61f340ff97 Mon Sep 17 00:00:00 2001
From: Thirumalai-Shaktivel <thirumalaishaktivel at gmail.com>
Date: Mon, 8 Jun 2026 11:56:46 +0530
Subject: [PATCH 2/2] Address the review comments

---
 flang/test/Driver/compiler-options.f90 |  2 +-
 flang/tools/flang-driver/driver.cpp    | 14 +++++++-------
 2 files changed, 8 insertions(+), 8 deletions(-)

diff --git a/flang/test/Driver/compiler-options.f90 b/flang/test/Driver/compiler-options.f90
index f511ad0ccf184..92efbc0044b35 100644
--- a/flang/test/Driver/compiler-options.f90
+++ b/flang/test/Driver/compiler-options.f90
@@ -1,6 +1,6 @@
 ! RUN: %flang -S -emit-llvm -o - %s | FileCheck %s
 ! Test communication of COMPILER_OPTIONS from flang to flang -fc1.
-! CHECK: [[OPTSVAR:@_QQclX[0-9A-F]+]] = {{[a-z]+}} constant [[[OPTSLEN:[0-9]+]] x i8] c"-S -emit-llvm -o -"
+! CHECK: [[OPTSVAR:@_QQclX[0-9A-Fa-f]+]] = {{[a-z]+}} constant [[[OPTSLEN:[0-9]+]] x i8] c"-S -emit-llvm -o -"
 program main
     use ISO_FORTRAN_ENV, only: compiler_options
     implicit none
diff --git a/flang/tools/flang-driver/driver.cpp b/flang/tools/flang-driver/driver.cpp
index e298b6e95860c..59e524677fea5 100644
--- a/flang/tools/flang-driver/driver.cpp
+++ b/flang/tools/flang-driver/driver.cpp
@@ -188,13 +188,13 @@ int main(int argc, const char **argv) {
   llvm::raw_string_ostream os(compilerOptsGathered);
   const llvm::opt::InputArgList &argList = c->getInputArgs();
   bool first = true;
-  for (const auto *arg : argList) {
-    if (arg->getOption().matches(clang::options::OPT_INPUT))
-      continue;
-    if (!first)
-      os << ' ';
-    os << arg->getAsString(argList);
-    first = false;
+  for (const llvm::opt::Arg *arg : argList) {
+    if (!arg->getOption().matches(clang::options::OPT_INPUT)) {
+      if (!first)
+        os << ' ';
+      os << arg->getAsString(argList);
+      first = false;
+    }
   }
 
 #ifdef _WIN32



More information about the flang-commits mailing list