[PATCH] D138532: [SPARC][clang] Enable frame pointer optimization by default

2022-11-22 Thread Koakuma via Phabricator via cfe-commits
koakuma created this revision.
koakuma added reviewers: arsenm, MaskRay, brad, dcederman.
Herald added subscribers: StephenFan, fedor.sergeev, jyknight.
Herald added a project: All.
koakuma requested review of this revision.
Herald added subscribers: cfe-commits, wdng.
Herald added a project: clang.

Enable frame pointer optimization by default to match it with other targets.
This brings a small reduction in generated binary sizes.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D138532

Files:
  clang/lib/Driver/ToolChains/Clang.cpp
  clang/test/Driver/frame-pointer-elim.c


Index: clang/test/Driver/frame-pointer-elim.c
===
--- clang/test/Driver/frame-pointer-elim.c
+++ clang/test/Driver/frame-pointer-elim.c
@@ -112,6 +112,20 @@
 // RUN: %clang -### -target powerpc64 -S -O1 %s 2>&1 | \
 // RUN:   FileCheck --check-prefix=KEEP-NONE %s
 
+// SPARC targets omit the frame pointer when optimizations are enabled.
+// RUN: %clang -### -target sparc -S %s 2>&1 | \
+// RUN:   FileCheck --check-prefix=KEEP-ALL %s
+// RUN: %clang -### -target sparc -S -O1 %s 2>&1 | \
+// RUN:   FileCheck --check-prefix=KEEP-NONE %s
+// RUN: %clang -### -target sparcel -S %s 2>&1 | \
+// RUN:   FileCheck --check-prefix=KEEP-ALL %s
+// RUN: %clang -### -target sparcel -S -O1 %s 2>&1 | \
+// RUN:   FileCheck --check-prefix=KEEP-NONE %s
+// RUN: %clang -### -target sparc64 -S %s 2>&1 | \
+// RUN:   FileCheck --check-prefix=KEEP-ALL %s
+// RUN: %clang -### -target sparc64 -S -O1 %s 2>&1 | \
+// RUN:   FileCheck --check-prefix=KEEP-NONE %s
+
 // For AAarch32 (A32, T32) linux targets, default omit frame pointer when
 // optimizations are enabled.
 // RUN: %clang -### -target arm-linux-gnueabihf- -marm -S %s 2>&1 | \
Index: clang/lib/Driver/ToolChains/Clang.cpp
===
--- clang/lib/Driver/ToolChains/Clang.cpp
+++ clang/lib/Driver/ToolChains/Clang.cpp
@@ -538,6 +538,9 @@
   case llvm::Triple::ppc64le:
   case llvm::Triple::riscv32:
   case llvm::Triple::riscv64:
+  case llvm::Triple::sparc:
+  case llvm::Triple::sparcel:
+  case llvm::Triple::sparcv9:
   case llvm::Triple::amdgcn:
   case llvm::Triple::r600:
   case llvm::Triple::csky:


Index: clang/test/Driver/frame-pointer-elim.c
===
--- clang/test/Driver/frame-pointer-elim.c
+++ clang/test/Driver/frame-pointer-elim.c
@@ -112,6 +112,20 @@
 // RUN: %clang -### -target powerpc64 -S -O1 %s 2>&1 | \
 // RUN:   FileCheck --check-prefix=KEEP-NONE %s
 
+// SPARC targets omit the frame pointer when optimizations are enabled.
+// RUN: %clang -### -target sparc -S %s 2>&1 | \
+// RUN:   FileCheck --check-prefix=KEEP-ALL %s
+// RUN: %clang -### -target sparc -S -O1 %s 2>&1 | \
+// RUN:   FileCheck --check-prefix=KEEP-NONE %s
+// RUN: %clang -### -target sparcel -S %s 2>&1 | \
+// RUN:   FileCheck --check-prefix=KEEP-ALL %s
+// RUN: %clang -### -target sparcel -S -O1 %s 2>&1 | \
+// RUN:   FileCheck --check-prefix=KEEP-NONE %s
+// RUN: %clang -### -target sparc64 -S %s 2>&1 | \
+// RUN:   FileCheck --check-prefix=KEEP-ALL %s
+// RUN: %clang -### -target sparc64 -S -O1 %s 2>&1 | \
+// RUN:   FileCheck --check-prefix=KEEP-NONE %s
+
 // For AAarch32 (A32, T32) linux targets, default omit frame pointer when
 // optimizations are enabled.
 // RUN: %clang -### -target arm-linux-gnueabihf- -marm -S %s 2>&1 | \
Index: clang/lib/Driver/ToolChains/Clang.cpp
===
--- clang/lib/Driver/ToolChains/Clang.cpp
+++ clang/lib/Driver/ToolChains/Clang.cpp
@@ -538,6 +538,9 @@
   case llvm::Triple::ppc64le:
   case llvm::Triple::riscv32:
   case llvm::Triple::riscv64:
+  case llvm::Triple::sparc:
+  case llvm::Triple::sparcel:
+  case llvm::Triple::sparcv9:
   case llvm::Triple::amdgcn:
   case llvm::Triple::r600:
   case llvm::Triple::csky:
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D138532: [SPARC][clang] Enable frame pointer optimization by default

2022-11-22 Thread Koakuma via Phabricator via cfe-commits
koakuma added a comment.

In D138532#3945422 , @MaskRay wrote:

> I think we usually follow GCC instead of other targets.

GCC enables it by default too, for example see https://godbolt.org/z/brPMKnM17
(That link also has comparison with clang with 
`-fomit-frame-pointer`/`-fno-omit-frame-pointer`)

> I am unfamiliar with sparc. Can you show an example where 
> `-fomit-frame-pointer`/`-fno-omit-frame-pointer` demonstrate a codegen 
> difference? I tried some simple examples and cannot reproduce a difference.

Unfortunately on SPARC, due to the register window mechanics this optimization 
can only be done on leaf functions.
As for the concrete effects, it skips the `save` and `restore` instructions for 
register window management, which, among other things, does the 
saving/restoring of frame pointers, and changes the return instruction to 
`retl` (aka `jmp %o7+8`).


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D138532/new/

https://reviews.llvm.org/D138532

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D139768: [SPARC][clang] Add SPARC target feature flags

2022-12-09 Thread Koakuma via Phabricator via cfe-commits
koakuma created this revision.
koakuma added reviewers: jrtc27, brad, ro, dcederman.
Herald added subscribers: fedor.sergeev, jyknight.
Herald added a project: All.
koakuma requested review of this revision.
Herald added subscribers: cfe-commits, MaskRay.
Herald added a project: clang.

This adds some SPARC feature flags to clang, for those that we have in common 
with GCC:

- `-m[no-]fpu`
- `-m[no-]fsmuld`
- `-m[no-]popc`
- `-m[no-]vis`
- `-m[no-]vis2`
- `-m[no-]vis3`
- `-m[hard/soft]-quad-float`

All have the same meanings as GCC's options 
(https://gcc.gnu.org/onlinedocs/gcc/SPARC-Options.html).

This fixes, among other things, the -mno-fpu part of bug #40792 
(https://github.com/llvm/llvm-project/issues/40792).


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D139768

Files:
  clang/include/clang/Driver/Options.td
  clang/lib/Driver/ToolChains/Arch/Sparc.cpp
  clang/test/Driver/sparc-target-features.c

Index: clang/test/Driver/sparc-target-features.c
===
--- /dev/null
+++ clang/test/Driver/sparc-target-features.c
@@ -0,0 +1,34 @@
+// RUN: %clang --target=sparc -mfpu %s -### 2>&1 | FileCheck -check-prefix=FPU %s
+// RUN: %clang --target=sparc -mno-fpu %s -### 2>&1 | FileCheck -check-prefix=NO-FPU %s
+// FPU: "-mfloat-abi" "hard"
+// NO-FPU: "-mfloat-abi" "soft"
+
+// RUN: %clang --target=sparc -mfsmuld %s -### 2>&1 | FileCheck -check-prefix=FSMULD %s
+// RUN: %clang --target=sparc -mno-fsmuld %s -### 2>&1 | FileCheck -check-prefix=NO-FSMULD %s
+// FSMULD: "-target-feature" "+fsmuld"
+// NO-FSMULD: "-target-feature" "-fsmuld"
+
+// RUN: %clang --target=sparc -mpopc %s -### 2>&1 | FileCheck -check-prefix=POPC %s
+// RUN: %clang --target=sparc -mno-popc %s -### 2>&1 | FileCheck -check-prefix=NO-POPC %s
+// POPC: "-target-feature" "+popc"
+// NO-POPC: "-target-feature" "-popc"
+
+// RUN: %clang --target=sparc -mvis %s -### 2>&1 | FileCheck -check-prefix=VIS %s
+// RUN: %clang --target=sparc -mno-vis %s -### 2>&1 | FileCheck -check-prefix=NO-VIS %s
+// VIS: "-target-feature" "+vis"
+// NO-VIS: "-target-feature" "-vis"
+
+// RUN: %clang --target=sparc -mvis2 %s -### 2>&1 | FileCheck -check-prefix=VIS2 %s
+// RUN: %clang --target=sparc -mno-vis2 %s -### 2>&1 | FileCheck -check-prefix=NO-VIS2 %s
+// VIS2: "-target-feature" "+vis2"
+// NO-VIS2: "-target-feature" "-vis2"
+
+// RUN: %clang --target=sparc -mvis3 %s -### 2>&1 | FileCheck -check-prefix=VIS3 %s
+// RUN: %clang --target=sparc -mno-vis3 %s -### 2>&1 | FileCheck -check-prefix=NO-VIS3 %s
+// VIS3: "-target-feature" "+vis3"
+// NO-VIS3: "-target-feature" "-vis3"
+
+// RUN: %clang --target=sparc -mhard-quad-float %s -### 2>&1 | FileCheck -check-prefix=HARD-QUAD-FLOAT %s
+// RUN: %clang --target=sparc -msoft-quad-float %s -### 2>&1 | FileCheck -check-prefix=SOFT-QUAD-FLOAT %s
+// HARD-QUAD-FLOAT: "-target-feature" "+hard-quad-float"
+// SOFT-QUAD-FLOAT: "-target-feature" "-hard-quad-float"
Index: clang/lib/Driver/ToolChains/Arch/Sparc.cpp
===
--- clang/lib/Driver/ToolChains/Arch/Sparc.cpp
+++ clang/lib/Driver/ToolChains/Arch/Sparc.cpp
@@ -82,12 +82,14 @@
 sparc::FloatABI sparc::getSparcFloatABI(const Driver &D,
 const ArgList &Args) {
   sparc::FloatABI ABI = sparc::FloatABI::Invalid;
-  if (Arg *A = Args.getLastArg(clang::driver::options::OPT_msoft_float,
-   options::OPT_mhard_float,
+  if (Arg *A = Args.getLastArg(options::OPT_msoft_float, options::OPT_mno_fpu,
+   options::OPT_mhard_float, options::OPT_mfpu,
options::OPT_mfloat_abi_EQ)) {
-if (A->getOption().matches(clang::driver::options::OPT_msoft_float))
+if (A->getOption().matches(options::OPT_msoft_float) ||
+A->getOption().matches(options::OPT_mno_fpu))
   ABI = sparc::FloatABI::Soft;
-else if (A->getOption().matches(options::OPT_mhard_float))
+else if (A->getOption().matches(options::OPT_mhard_float) ||
+ A->getOption().matches(options::OPT_mfpu))
   ABI = sparc::FloatABI::Hard;
 else {
   ABI = llvm::StringSwitch(A->getValue())
@@ -143,4 +145,47 @@
   sparc::FloatABI FloatABI = sparc::getSparcFloatABI(D, Args);
   if (FloatABI == sparc::FloatABI::Soft)
 Features.push_back("+soft-float");
+
+  if (Arg *A = Args.getLastArg(options::OPT_mfsmuld, options::OPT_mno_fsmuld)) {
+if (A->getOption().matches(options::OPT_mfsmuld))
+  Features.push_back("+fsmuld");
+else
+  Features.push_back("-fsmuld");
+  }
+
+  if (Arg *A = Args.getLastArg(options::OPT_mpopc, options::OPT_mno_popc)) {
+if (A->getOption().matches(options::OPT_mpopc))
+  Features.push_back("+popc");
+else
+  Features.push_back("-popc");
+  }
+
+  if (Arg *A = Args.getLastArg(options::OPT_mvis, options::OPT_mno_vis)) {
+if (A->getOption().matches(options::OPT_mvis))
+  Features.p