[flang-commits] [flang] [clang] [flang]Add support for -moutline-atomics and -mno-outline-atomics (PR #78755)

Mats Petersson via flang-commits flang-commits at lists.llvm.org
Wed Jan 31 06:33:24 PST 2024


https://github.com/Leporacanthicus updated https://github.com/llvm/llvm-project/pull/78755

>From 1bf1060607dc8d4fedb227de35271e0b0307f564 Mon Sep 17 00:00:00 2001
From: Mats Petersson <mats.petersson at arm.com>
Date: Fri, 19 Jan 2024 16:30:31 +0000
Subject: [PATCH 1/2] [flang]Add support for -moutline-atomics and
 -mno-outline-atomics

Clang has support for genarting atomic operations as functions rather
than inline atomic operations, using a target feature.

This is useful for code-size, some cases of debugging, and it also affects
the ability to inline functions from C into Fortran or the other way around
when using LTO - the inliner will check the target features to make sure
functions are compatible, and a difference in outline-atomics would disable
the inlining of an outline-atomics into a non-outline-atomics function.
---
 clang/include/clang/Driver/Options.td         |  4 ++--
 clang/lib/Driver/ToolChains/Flang.cpp         | 23 ++++++++++++++++++-
 flang/lib/Frontend/CompilerInvocation.cpp     | 15 ++++++++++++
 flang/test/Driver/aarch64-outline-atomics.f90 | 11 +++++++++
 flang/test/Driver/driver-help-hidden.f90      |  2 ++
 flang/test/Driver/driver-help.f90             |  4 ++++
 6 files changed, 56 insertions(+), 3 deletions(-)
 create mode 100644 flang/test/Driver/aarch64-outline-atomics.f90

diff --git a/clang/include/clang/Driver/Options.td b/clang/include/clang/Driver/Options.td
index d2e6c3ff721c2..b8be694bf0bf8 100644
--- a/clang/include/clang/Driver/Options.td
+++ b/clang/include/clang/Driver/Options.td
@@ -4960,10 +4960,10 @@ def mno_fmv : Flag<["-"], "mno-fmv">, Group<f_clang_Group>,
   Visibility<[ClangOption, CC1Option]>,
   HelpText<"Disable function multiversioning">;
 def moutline_atomics : Flag<["-"], "moutline-atomics">, Group<f_clang_Group>,
-  Visibility<[ClangOption, CC1Option]>,
+  Visibility<[ClangOption, CC1Option, FlangOption, FC1Option]>,
   HelpText<"Generate local calls to out-of-line atomic operations">;
 def mno_outline_atomics : Flag<["-"], "mno-outline-atomics">, Group<f_clang_Group>,
-  Visibility<[ClangOption, CC1Option]>,
+  Visibility<[ClangOption, CC1Option, FlangOption, FC1Option]>,
   HelpText<"Don't generate local calls to out-of-line atomic operations">;
 def mno_implicit_float : Flag<["-"], "mno-implicit-float">, Group<m_Group>,
   HelpText<"Don't generate implicit floating point or vector instructions">;
diff --git a/clang/lib/Driver/ToolChains/Flang.cpp b/clang/lib/Driver/ToolChains/Flang.cpp
index 03d68c3df7fb3..7394ef6f601c7 100644
--- a/clang/lib/Driver/ToolChains/Flang.cpp
+++ b/clang/lib/Driver/ToolChains/Flang.cpp
@@ -354,6 +354,27 @@ void Flang::addTargetOptions(const ArgList &Args,
     CmdArgs.push_back(Args.MakeArgString(CPU));
   }
 
+  if (Arg *A = Args.getLastArg(options::OPT_moutline_atomics,
+                               options::OPT_mno_outline_atomics)) {
+    // Option -moutline-atomics supported for AArch64 target only.
+    if (!Triple.isAArch64()) {
+      D.Diag(diag::warn_drv_moutline_atomics_unsupported_opt)
+          << Triple.getArchName() << A->getOption().getName();
+    } else {
+      if (A->getOption().matches(options::OPT_moutline_atomics)) {
+        CmdArgs.push_back("-target-feature");
+        CmdArgs.push_back("+outline-atomics");
+      } else {
+        CmdArgs.push_back("-target-feature");
+        CmdArgs.push_back("-outline-atomics");
+      }
+    }
+  } else if (Triple.isAArch64() &&
+             getToolChain().IsAArch64OutlineAtomicsDefault(Args)) {
+    CmdArgs.push_back("-target-feature");
+    CmdArgs.push_back("+outline-atomics");
+  }
+
   // Add the target features.
   switch (TC.getArch()) {
   default:
@@ -674,7 +695,7 @@ void Flang::ConstructJob(Compilation &C, const JobAction &JA,
   CmdArgs.push_back(Args.MakeArgString(TripleStr));
 
   if (isa<PreprocessJobAction>(JA)) {
-      CmdArgs.push_back("-E");
+    CmdArgs.push_back("-E");
   } else if (isa<CompileJobAction>(JA) || isa<BackendJobAction>(JA)) {
     if (JA.getType() == types::TY_Nothing) {
       CmdArgs.push_back("-fsyntax-only");
diff --git a/flang/lib/Frontend/CompilerInvocation.cpp b/flang/lib/Frontend/CompilerInvocation.cpp
index a3c41fb4611f5..74127327e798e 100644
--- a/flang/lib/Frontend/CompilerInvocation.cpp
+++ b/flang/lib/Frontend/CompilerInvocation.cpp
@@ -402,6 +402,21 @@ 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());
+
+  llvm::Triple targetTriple{llvm::Triple(opts.triple)};
+  if (const llvm::opt::Arg *A =
+          args.getLastArg(clang::driver::options::OPT_moutline_atomics,
+                          clang::driver::options::OPT_mno_outline_atomics)) {
+    // Option -moutline-atomics supported for AArch64 target only.
+    if (!targetTriple.isAArch64()) {
+      if (A->getOption().matches(
+              clang::driver::options::OPT_moutline_atomics)) {
+        opts.featuresAsWritten.push_back("+outline-atomics");
+      } else {
+        opts.featuresAsWritten.push_back("-outline-atomics");
+      }
+    }
+  }
 }
 
 // Tweak the frontend configuration based on the frontend action
diff --git a/flang/test/Driver/aarch64-outline-atomics.f90 b/flang/test/Driver/aarch64-outline-atomics.f90
new file mode 100644
index 0000000000000..f469d50290ef0
--- /dev/null
+++ b/flang/test/Driver/aarch64-outline-atomics.f90
@@ -0,0 +1,11 @@
+! Test that flang-new forwards the -moutline-atomics and -mno-outline-atomics.
+! RUN: %flang -moutline-atomics --target=aarch64-none-none -### %s -o %t 2>&1  | FileCheck %s
+! CHECK: "-target-feature" "+outline-atomics"
+
+! RUN: %flang -mno-outline-atomics --target=aarch64-none-none -### %s -o %t 2>&1  | FileCheck %s --check-prefix=CHECK-NOOUTLINE
+! CHECK-NOOUTLINE: "-target-feature" "-outline-atomics"
+
+! RUN: %flang -mno-outline-atomics --target=x86-none-none -### %s -o %t 2>&1  | FileCheck %s --check-prefix=CHECK-ERRMSG
+! CHECK-ERRMSG: warning: 'x86' does not support '-mno-outline-atomics'
+
+
diff --git a/flang/test/Driver/driver-help-hidden.f90 b/flang/test/Driver/driver-help-hidden.f90
index 426b0e5a1c367..13698b65ae461 100644
--- a/flang/test/Driver/driver-help-hidden.f90
+++ b/flang/test/Driver/driver-help-hidden.f90
@@ -122,7 +122,9 @@
 ! CHECK-NEXT: -mllvm=<arg>            Alias for -mllvm
 ! CHECK-NEXT: -mllvm <value>          Additional arguments to forward to LLVM's option processing
 ! CHECK-NEXT: -mmlir <value>          Additional arguments to forward to MLIR's option processing
+! CHECK-NEXT: -mno-outline-atomics    Don't generate local calls to out-of-line atomic operations
 ! CHECK-NEXT: -module-dir <dir>       Put MODULE files in <dir>
+! CHECK-NEXT: -moutline-atomics       Generate local calls to out-of-line atomic operations
 ! CHECK-NEXT: -mrvv-vector-bits=<value>
 ! CHECK-NEXT:                         Specify the size in bits of an RVV vector register
 ! CHECK-NEXT: -msve-vector-bits=<value>
diff --git a/flang/test/Driver/driver-help.f90 b/flang/test/Driver/driver-help.f90
index 221da6439764b..73f5d6ee846d3 100644
--- a/flang/test/Driver/driver-help.f90
+++ b/flang/test/Driver/driver-help.f90
@@ -108,7 +108,9 @@
 ! HELP-NEXT: -mllvm=<arg>            Alias for -mllvm
 ! HELP-NEXT: -mllvm <value>          Additional arguments to forward to LLVM's option processing
 ! HELP-NEXT: -mmlir <value>          Additional arguments to forward to MLIR's option processing
+! HELP-NEXT: -mno-outline-atomics    Don't generate local calls to out-of-line atomic operations
 ! HELP-NEXT: -module-dir <dir>       Put MODULE files in <dir>
+! HELP-NEXT: -moutline-atomics       Generate local calls to out-of-line atomic operations
 ! HELP-NEXT: -mrvv-vector-bits=<value>
 ! HELP-NEXT:                         Specify the size in bits of an RVV vector register
 ! HELP-NEXT: -msve-vector-bits=<value>
@@ -246,8 +248,10 @@
 ! HELP-FC1-NEXT: -mframe-pointer=<value> Specify which frame pointers to retain.
 ! HELP-FC1-NEXT: -mllvm <value>          Additional arguments to forward to LLVM's option processing
 ! HELP-FC1-NEXT: -mmlir <value>          Additional arguments to forward to MLIR's option processing
+! HELP-FC1-NEXT: -mno-outline-atomics    Don't generate local calls to out-of-line atomic operations
 ! HELP-FC1-NEXT: -module-dir <dir>       Put MODULE files in <dir>
 ! HELP-FC1-NEXT: -module-suffix <suffix> Use <suffix> as the suffix for module files (the default value is `.mod`)
+! HELP-FC1-NEXT: -moutline-atomics       Generate local calls to out-of-line atomic operations
 ! HELP-FC1-NEXT: -mreassociate           Allow reassociation transformations for floating-point instructions
 ! HELP-FC1-NEXT: -mrelocation-model <value>
 ! HELP-FC1-NEXT:                         The relocation model to use

>From 7dcafdd78b95497f2ed3f84500570f1398bd9f73 Mon Sep 17 00:00:00 2001
From: Mats Petersson <mats.petersson at arm.com>
Date: Wed, 31 Jan 2024 10:20:16 +0000
Subject: [PATCH 2/2] Minor fix

---
 flang/lib/Frontend/CompilerInvocation.cpp | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/flang/lib/Frontend/CompilerInvocation.cpp b/flang/lib/Frontend/CompilerInvocation.cpp
index 74127327e798e..4d6894e0b5d03 100644
--- a/flang/lib/Frontend/CompilerInvocation.cpp
+++ b/flang/lib/Frontend/CompilerInvocation.cpp
@@ -408,7 +408,7 @@ static void parseTargetArgs(TargetOptions &opts, llvm::opt::ArgList &args) {
           args.getLastArg(clang::driver::options::OPT_moutline_atomics,
                           clang::driver::options::OPT_mno_outline_atomics)) {
     // Option -moutline-atomics supported for AArch64 target only.
-    if (!targetTriple.isAArch64()) {
+    if (targetTriple.isAArch64()) {
       if (A->getOption().matches(
               clang::driver::options::OPT_moutline_atomics)) {
         opts.featuresAsWritten.push_back("+outline-atomics");



More information about the flang-commits mailing list