[clang] [flang] Allow disabling of types from the command line (PR #107126)

Renaud Kauffmann via cfe-commits cfe-commits at lists.llvm.org
Tue Sep 3 15:36:02 PDT 2024


https://github.com/Renaud-K updated https://github.com/llvm/llvm-project/pull/107126

>From 8a1c51bdd290fc47cac359646aeb01623574231f Mon Sep 17 00:00:00 2001
From: Renaud-K <rkauffmann at nvidia.com>
Date: Tue, 3 Sep 2024 09:34:09 -0700
Subject: [PATCH 1/3] Allow disabling of types from the command line

---
 clang/include/clang/Driver/Options.td        |  6 ++++++
 flang/include/flang/Frontend/TargetOptions.h |  6 ++++++
 flang/include/flang/Tools/TargetSetup.h      | 11 ++++++++++-
 flang/lib/Frontend/CompilerInvocation.cpp    | 17 ++++++++++++++---
 flang/tools/bbc/bbc.cpp                      |  5 +++--
 5 files changed, 39 insertions(+), 6 deletions(-)

diff --git a/clang/include/clang/Driver/Options.td b/clang/include/clang/Driver/Options.td
index 83cf753e824845..8bc47fea5196e7 100644
--- a/clang/include/clang/Driver/Options.td
+++ b/clang/include/clang/Driver/Options.td
@@ -6761,6 +6761,12 @@ def fdefault_integer_8 : Flag<["-"],"fdefault-integer-8">, Group<f_Group>,
   HelpText<"Set the default integer and logical kind to an 8 byte wide type">;
 def fdefault_real_8 : Flag<["-"],"fdefault-real-8">, Group<f_Group>,
   HelpText<"Set the default real kind to an 8 byte wide type">;
+def fdisable_real_3 : Flag<["-"],"fdisable-real-3">, Group<f_Group>,
+  Flags<[HelpHidden]>;
+def fdisable_real_10 : Flag<["-"],"fdisable-real-10">, Group<f_Group>,
+  Flags<[HelpHidden]>;
+def fdisable_integer_16 : Flag<["-"],"fdisable-integer-16">, Group<f_Group>,
+  Flags<[HelpHidden]>;
 def flarge_sizes : Flag<["-"],"flarge-sizes">, Group<f_Group>,
   HelpText<"Use INTEGER(KIND=8) for the result type in size-related intrinsics">;
 
diff --git a/flang/include/flang/Frontend/TargetOptions.h b/flang/include/flang/Frontend/TargetOptions.h
index fa72c77a028a1c..b0b64ae583d16c 100644
--- a/flang/include/flang/Frontend/TargetOptions.h
+++ b/flang/include/flang/Frontend/TargetOptions.h
@@ -38,6 +38,12 @@ class TargetOptions {
   /// The list of target specific features to enable or disable, as written on
   /// the command line.
   std::vector<std::string> featuresAsWritten;
+
+  /// The real KINDs disabled for this target
+  std::vector<int> disabledRealKinds;
+  
+  /// The integer KINDs disabled for this target
+  std::vector<int> disabledIntegerKinds;
 };
 
 } // end namespace Fortran::frontend
diff --git a/flang/include/flang/Tools/TargetSetup.h b/flang/include/flang/Tools/TargetSetup.h
index 238d66c9241dd0..b112b6b4d80578 100644
--- a/flang/include/flang/Tools/TargetSetup.h
+++ b/flang/include/flang/Tools/TargetSetup.h
@@ -10,6 +10,7 @@
 #define FORTRAN_TOOLS_TARGET_SETUP_H
 
 #include "flang/Evaluate/target.h"
+#include "flang/Frontend/TargetOptions.h"
 #include "llvm/Target/TargetMachine.h"
 
 namespace Fortran::tools {
@@ -17,6 +18,7 @@ namespace Fortran::tools {
 [[maybe_unused]] inline static void setUpTargetCharacteristics(
     Fortran::evaluate::TargetCharacteristics &targetCharacteristics,
     const llvm::TargetMachine &targetMachine,
+    const Fortran::frontend::TargetOptions &targetOptions,
     const std::string &compilerVersion, const std::string &compilerOptions) {
 
   const llvm::Triple &targetTriple{targetMachine.getTargetTriple()};
@@ -24,7 +26,14 @@ namespace Fortran::tools {
   if (targetTriple.getArch() != llvm::Triple::ArchType::x86_64)
     targetCharacteristics.DisableType(
         Fortran::common::TypeCategory::Real, /*kind=*/10);
-
+  for (auto realKind : targetOptions.disabledRealKinds) {
+    targetCharacteristics.DisableType(
+        common::TypeCategory::Real, realKind);
+  }
+  for (auto intKind : targetOptions.disabledIntegerKinds) {
+    targetCharacteristics.DisableType(
+        common::TypeCategory::Integer, intKind);
+  }
   targetCharacteristics.set_compilerOptionsString(compilerOptions)
       .set_compilerVersionString(compilerVersion);
 
diff --git a/flang/lib/Frontend/CompilerInvocation.cpp b/flang/lib/Frontend/CompilerInvocation.cpp
index 1d73397d330178..5676947c2b1016 100644
--- a/flang/lib/Frontend/CompilerInvocation.cpp
+++ b/flang/lib/Frontend/CompilerInvocation.cpp
@@ -438,8 +438,19 @@ static void parseTargetArgs(TargetOptions &opts, llvm::opt::ArgList &args) {
   for (const llvm::opt::Arg *currentArg :
        args.filtered(clang::driver::options::OPT_target_feature))
     opts.featuresAsWritten.emplace_back(currentArg->getValue());
-}
 
+  if (args.hasArg(clang::driver::options::OPT_fdisable_real_10)) {
+    opts.disabledRealKinds.push_back(10);
+  }
+  
+  if (args.hasArg(clang::driver::options::OPT_fdisable_real_3)) {
+    opts.disabledRealKinds.push_back(3);
+  }
+  
+  if (args.hasArg(clang::driver::options::OPT_fdisable_integer_16)) {
+    opts.disabledIntegerKinds.push_back(16);
+  }
+}
 // Tweak the frontend configuration based on the frontend action
 static void setUpFrontendBasedOnAction(FrontendOptions &opts) {
   if (opts.programAction == DebugDumpParsingLog)
@@ -1529,8 +1540,8 @@ CompilerInvocation::getSemanticsCtx(
 
   std::string compilerVersion = Fortran::common::getFlangFullVersion();
   Fortran::tools::setUpTargetCharacteristics(
-      semanticsContext->targetCharacteristics(), targetMachine, compilerVersion,
-      allCompilerInvocOpts);
+      semanticsContext->targetCharacteristics(), targetMachine, getTargetOpts(),
+      compilerVersion, allCompilerInvocOpts);
   return semanticsContext;
 }
 
diff --git a/flang/tools/bbc/bbc.cpp b/flang/tools/bbc/bbc.cpp
index 736d68219581dd..279b04055798d0 100644
--- a/flang/tools/bbc/bbc.cpp
+++ b/flang/tools/bbc/bbc.cpp
@@ -18,6 +18,7 @@
 #include "flang/Common/OpenMP-features.h"
 #include "flang/Common/Version.h"
 #include "flang/Common/default-kinds.h"
+#include "flang/Frontend/TargetOptions.h"
 #include "flang/Lower/Bridge.h"
 #include "flang/Lower/PFTBuilder.h"
 #include "flang/Lower/Support/Verifier.h"
@@ -556,8 +557,8 @@ int main(int argc, char **argv) {
   std::string compilerVersion = Fortran::common::getFlangToolFullVersion("bbc");
   std::string compilerOptions = "";
   Fortran::tools::setUpTargetCharacteristics(
-      semanticsContext.targetCharacteristics(), *targetMachine, compilerVersion,
-      compilerOptions);
+      semanticsContext.targetCharacteristics(), *targetMachine,
+      {}, compilerVersion, compilerOptions);
 
   return mlir::failed(
       convertFortranSourceToMLIR(inputFilename, options, programPrefix,

>From 0c8fe7b157e03a21aee9117d93f84d7098f55ad3 Mon Sep 17 00:00:00 2001
From: Renaud-K <rkauffmann at nvidia.com>
Date: Tue, 3 Sep 2024 10:15:22 -0700
Subject: [PATCH 2/3] Applying formatting

---
 flang/include/flang/Frontend/TargetOptions.h | 2 +-
 flang/include/flang/Tools/TargetSetup.h      | 6 ++----
 flang/lib/Frontend/CompilerInvocation.cpp    | 4 ++--
 flang/tools/bbc/bbc.cpp                      | 4 ++--
 4 files changed, 7 insertions(+), 9 deletions(-)

diff --git a/flang/include/flang/Frontend/TargetOptions.h b/flang/include/flang/Frontend/TargetOptions.h
index b0b64ae583d16c..332adcbe6b6ac3 100644
--- a/flang/include/flang/Frontend/TargetOptions.h
+++ b/flang/include/flang/Frontend/TargetOptions.h
@@ -41,7 +41,7 @@ class TargetOptions {
 
   /// The real KINDs disabled for this target
   std::vector<int> disabledRealKinds;
-  
+
   /// The integer KINDs disabled for this target
   std::vector<int> disabledIntegerKinds;
 };
diff --git a/flang/include/flang/Tools/TargetSetup.h b/flang/include/flang/Tools/TargetSetup.h
index b112b6b4d80578..fcf1bdde0bcf3d 100644
--- a/flang/include/flang/Tools/TargetSetup.h
+++ b/flang/include/flang/Tools/TargetSetup.h
@@ -27,12 +27,10 @@ namespace Fortran::tools {
     targetCharacteristics.DisableType(
         Fortran::common::TypeCategory::Real, /*kind=*/10);
   for (auto realKind : targetOptions.disabledRealKinds) {
-    targetCharacteristics.DisableType(
-        common::TypeCategory::Real, realKind);
+    targetCharacteristics.DisableType(common::TypeCategory::Real, realKind);
   }
   for (auto intKind : targetOptions.disabledIntegerKinds) {
-    targetCharacteristics.DisableType(
-        common::TypeCategory::Integer, intKind);
+    targetCharacteristics.DisableType(common::TypeCategory::Integer, intKind);
   }
   targetCharacteristics.set_compilerOptionsString(compilerOptions)
       .set_compilerVersionString(compilerVersion);
diff --git a/flang/lib/Frontend/CompilerInvocation.cpp b/flang/lib/Frontend/CompilerInvocation.cpp
index 5676947c2b1016..c2b8816cd79d73 100644
--- a/flang/lib/Frontend/CompilerInvocation.cpp
+++ b/flang/lib/Frontend/CompilerInvocation.cpp
@@ -442,11 +442,11 @@ static void parseTargetArgs(TargetOptions &opts, llvm::opt::ArgList &args) {
   if (args.hasArg(clang::driver::options::OPT_fdisable_real_10)) {
     opts.disabledRealKinds.push_back(10);
   }
-  
+
   if (args.hasArg(clang::driver::options::OPT_fdisable_real_3)) {
     opts.disabledRealKinds.push_back(3);
   }
-  
+
   if (args.hasArg(clang::driver::options::OPT_fdisable_integer_16)) {
     opts.disabledIntegerKinds.push_back(16);
   }
diff --git a/flang/tools/bbc/bbc.cpp b/flang/tools/bbc/bbc.cpp
index 279b04055798d0..dcff4503f16571 100644
--- a/flang/tools/bbc/bbc.cpp
+++ b/flang/tools/bbc/bbc.cpp
@@ -557,8 +557,8 @@ int main(int argc, char **argv) {
   std::string compilerVersion = Fortran::common::getFlangToolFullVersion("bbc");
   std::string compilerOptions = "";
   Fortran::tools::setUpTargetCharacteristics(
-      semanticsContext.targetCharacteristics(), *targetMachine,
-      {}, compilerVersion, compilerOptions);
+      semanticsContext.targetCharacteristics(), *targetMachine, {},
+      compilerVersion, compilerOptions);
 
   return mlir::failed(
       convertFortranSourceToMLIR(inputFilename, options, programPrefix,

>From 0669c313fde361f779ad5af41f67e83c3d40bac5 Mon Sep 17 00:00:00 2001
From: Renaud-K <rkauffmann at nvidia.com>
Date: Tue, 3 Sep 2024 15:35:31 -0700
Subject: [PATCH 3/3] Adding -fdisable-integer-2. Adding help messages.
 Removing none style compliant braces.

---
 clang/include/clang/Driver/Options.td     |  8 +++++---
 flang/include/flang/Tools/TargetSetup.h   |  9 +++++----
 flang/lib/Frontend/CompilerInvocation.cpp | 12 ++++++------
 3 files changed, 16 insertions(+), 13 deletions(-)

diff --git a/clang/include/clang/Driver/Options.td b/clang/include/clang/Driver/Options.td
index 8bc47fea5196e7..60e1b64f150d5f 100644
--- a/clang/include/clang/Driver/Options.td
+++ b/clang/include/clang/Driver/Options.td
@@ -6762,11 +6762,13 @@ def fdefault_integer_8 : Flag<["-"],"fdefault-integer-8">, Group<f_Group>,
 def fdefault_real_8 : Flag<["-"],"fdefault-real-8">, Group<f_Group>,
   HelpText<"Set the default real kind to an 8 byte wide type">;
 def fdisable_real_3 : Flag<["-"],"fdisable-real-3">, Group<f_Group>,
-  Flags<[HelpHidden]>;
+   HelpText<"Disable real(KIND=3) from TargetCharacteristics">, Flags<[HelpHidden]>;
 def fdisable_real_10 : Flag<["-"],"fdisable-real-10">, Group<f_Group>,
-  Flags<[HelpHidden]>;
+  HelpText<"Disable real(KIND=10) from TargetCharacteristics">, Flags<[HelpHidden]>;
+def fdisable_integer_2 : Flag<["-"],"fdisable-integer-2">, Group<f_Group>,
+  HelpText<"Disable integer(KIND=2) from TargetCharacteristics">, Flags<[HelpHidden]>;
 def fdisable_integer_16 : Flag<["-"],"fdisable-integer-16">, Group<f_Group>,
-  Flags<[HelpHidden]>;
+  HelpText<"Disable integer(KIND=16) from TargetCharacteristics">, Flags<[HelpHidden]>;
 def flarge_sizes : Flag<["-"],"flarge-sizes">, Group<f_Group>,
   HelpText<"Use INTEGER(KIND=8) for the result type in size-related intrinsics">;
 
diff --git a/flang/include/flang/Tools/TargetSetup.h b/flang/include/flang/Tools/TargetSetup.h
index fcf1bdde0bcf3d..611b66f865ee19 100644
--- a/flang/include/flang/Tools/TargetSetup.h
+++ b/flang/include/flang/Tools/TargetSetup.h
@@ -26,12 +26,13 @@ namespace Fortran::tools {
   if (targetTriple.getArch() != llvm::Triple::ArchType::x86_64)
     targetCharacteristics.DisableType(
         Fortran::common::TypeCategory::Real, /*kind=*/10);
-  for (auto realKind : targetOptions.disabledRealKinds) {
+        
+  for (auto realKind : targetOptions.disabledRealKinds)
     targetCharacteristics.DisableType(common::TypeCategory::Real, realKind);
-  }
-  for (auto intKind : targetOptions.disabledIntegerKinds) {
+
+  for (auto intKind : targetOptions.disabledIntegerKinds)
     targetCharacteristics.DisableType(common::TypeCategory::Integer, intKind);
-  }
+
   targetCharacteristics.set_compilerOptionsString(compilerOptions)
       .set_compilerVersionString(compilerVersion);
 
diff --git a/flang/lib/Frontend/CompilerInvocation.cpp b/flang/lib/Frontend/CompilerInvocation.cpp
index c2b8816cd79d73..55f012df67a223 100644
--- a/flang/lib/Frontend/CompilerInvocation.cpp
+++ b/flang/lib/Frontend/CompilerInvocation.cpp
@@ -439,17 +439,17 @@ static void parseTargetArgs(TargetOptions &opts, llvm::opt::ArgList &args) {
        args.filtered(clang::driver::options::OPT_target_feature))
     opts.featuresAsWritten.emplace_back(currentArg->getValue());
 
-  if (args.hasArg(clang::driver::options::OPT_fdisable_real_10)) {
+  if (args.hasArg(clang::driver::options::OPT_fdisable_real_10))
     opts.disabledRealKinds.push_back(10);
-  }
 
-  if (args.hasArg(clang::driver::options::OPT_fdisable_real_3)) {
+  if (args.hasArg(clang::driver::options::OPT_fdisable_real_3)) 
     opts.disabledRealKinds.push_back(3);
-  }
 
-  if (args.hasArg(clang::driver::options::OPT_fdisable_integer_16)) {
+  if (args.hasArg(clang::driver::options::OPT_fdisable_integer_2))
+    opts.disabledIntegerKinds.push_back(2);
+
+  if (args.hasArg(clang::driver::options::OPT_fdisable_integer_16))
     opts.disabledIntegerKinds.push_back(16);
-  }
 }
 // Tweak the frontend configuration based on the frontend action
 static void setUpFrontendBasedOnAction(FrontendOptions &opts) {



More information about the cfe-commits mailing list