diff options
author | Richard Sandiford <richard.sandiford@arm.com> | 2025-01-23 13:57:01 +0000 |
---|---|---|
committer | Richard Sandiford <richard.sandiford@arm.com> | 2025-01-23 13:57:01 +0000 |
commit | 1886dfb27a296b31de46b44beae0f1db6c1584b6 (patch) | |
tree | c4e71b4dcba2f0f4eff4df3d13506a0dddb07428 /gcc/config | |
parent | ce6fc67da7f600b63985abeb39ba85440cbad549 (diff) | |
download | gcc-1886dfb27a296b31de46b44beae0f1db6c1584b6.zip gcc-1886dfb27a296b31de46b44beae0f1db6c1584b6.tar.gz gcc-1886dfb27a296b31de46b44beae0f1db6c1584b6.tar.bz2 |
aarch64: Avoid redundant writes to FPMR
GCC 15 is the first release to support FP8 intrinsics.
The underlying instructions depend on the value of a new register,
FPMR. Unlike FPCR, FPMR is a normal call-clobbered/caller-save
register rather than a global register. So:
- The FP8 intrinsics take a final uint64_t argument that
specifies what value FPMR should have.
- If an FP8 operation is split across multiple functions,
it is likely that those functions would have a similar argument.
If the object code has the structure:
for (...)
fp8_kernel (..., fpmr_value);
then fp8_kernel would set FPMR to fpmr_value each time it is
called, even though FPMR will already have that value for at
least the second and subsequent calls (and possibly the first).
The working assumption for the ABI has been that writes to
registers like FPMR can in general be more expensive than
reads and so it would be better to use a conditional write like:
mrs tmp, fpmr
cmp tmp, <value>
beq 1f
msr fpmr, <value>
1:
instead of writing the same value to FPMR repeatedly.
This patch implements that. It also adds a tuning flag that suppresses
the behaviour, both to make testing easier and to support any future
cores that (for example) are able to rename FPMR.
Hopefully this really is the last part of the FP8 enablement.
gcc/
* config/aarch64/aarch64-tuning-flags.def
(AARCH64_EXTRA_TUNE_CHEAP_FPMR_WRITE): New tuning flag.
* config/aarch64/aarch64.h (TARGET_CHEAP_FPMR_WRITE): New macro.
* config/aarch64/aarch64.md: Split moves into FPMR into a test
and branch around.
(aarch64_write_fpmr): New pattern.
gcc/testsuite/
* g++.target/aarch64/sve2/acle/aarch64-sve2-acle-asm.exp: Add
cheap_fpmr_write by default.
* gcc.target/aarch64/sve2/acle/aarch64-sve2-acle-asm.exp: Likewise.
* gcc.target/aarch64/acle/fp8.c: Add cheap_fpmr_write.
* gcc.target/aarch64/acle/fpmr-2.c: Likewise.
* gcc.target/aarch64/simd/vcvt_fpm.c: Likewise.
* gcc.target/aarch64/simd/vdot2_fpm.c: Likewise.
* gcc.target/aarch64/simd/vdot4_fpm.c: Likewise.
* gcc.target/aarch64/simd/vmla_fpm.c: Likewise.
* gcc.target/aarch64/acle/fpmr-6.c: New test.
Diffstat (limited to 'gcc/config')
-rw-r--r-- | gcc/config/aarch64/aarch64-tuning-flags.def | 15 | ||||
-rw-r--r-- | gcc/config/aarch64/aarch64.h | 5 | ||||
-rw-r--r-- | gcc/config/aarch64/aarch64.md | 39 |
3 files changed, 59 insertions, 0 deletions
diff --git a/gcc/config/aarch64/aarch64-tuning-flags.def b/gcc/config/aarch64/aarch64-tuning-flags.def index 60967aa..f2c916e 100644 --- a/gcc/config/aarch64/aarch64-tuning-flags.def +++ b/gcc/config/aarch64/aarch64-tuning-flags.def @@ -48,6 +48,21 @@ AARCH64_EXTRA_TUNING_OPTION ("fully_pipelined_fma", FULLY_PIPELINED_FMA) rather than re-use an input predicate register. */ AARCH64_EXTRA_TUNING_OPTION ("avoid_pred_rmw", AVOID_PRED_RMW) +/* Whether writes to the FPMR are cheap enough that: + + msr fpmr, <value> + + is better than: + + mrs tmp, fpmr + cmp tmp, <value> + beq 1f + msr fpmr, <value> + 1: + + even when the branch is predictably taken. */ +AARCH64_EXTRA_TUNING_OPTION ("cheap_fpmr_write", CHEAP_FPMR_WRITE) + /* Baseline tuning settings suitable for all modern cores. */ #define AARCH64_EXTRA_TUNE_BASE (AARCH64_EXTRA_TUNE_CHEAP_SHIFT_EXTEND \ | AARCH64_EXTRA_TUNE_FULLY_PIPELINED_FMA) diff --git a/gcc/config/aarch64/aarch64.h b/gcc/config/aarch64/aarch64.h index 218868a..5cbf442 100644 --- a/gcc/config/aarch64/aarch64.h +++ b/gcc/config/aarch64/aarch64.h @@ -486,6 +486,11 @@ constexpr auto AARCH64_FL_DEFAULT_ISA_MODE ATTRIBUTE_UNUSED /* fp8 instructions are enabled through +fp8. */ #define TARGET_FP8 AARCH64_HAVE_ISA (FP8) +/* See the comment above the tuning flag for details. */ +#define TARGET_CHEAP_FPMR_WRITE \ + (bool (aarch64_tune_params.extra_tuning_flags \ + & AARCH64_EXTRA_TUNE_CHEAP_FPMR_WRITE)) + /* Combinatorial tests. */ #define TARGET_SVE2_OR_SME2 \ diff --git a/gcc/config/aarch64/aarch64.md b/gcc/config/aarch64/aarch64.md index 776c4c4c..071058d 100644 --- a/gcc/config/aarch64/aarch64.md +++ b/gcc/config/aarch64/aarch64.md @@ -356,6 +356,7 @@ UNSPEC_UPDATE_FFRT UNSPEC_RDFFR UNSPEC_WRFFR + UNSPEC_WRITE_FPMR UNSPEC_SYSREG_RDI UNSPEC_SYSREG_RTI UNSPEC_SYSREG_WDI @@ -1883,6 +1884,44 @@ } ) +;; The preferred way of writing to the FPMR is to test whether it already +;; has the desired value and branch around the write if so. This reduces +;; the number of redundant FPMR writes caused by ABI boundaries, such as in: +;; +;; for (...) +;; fp8_kernel (..., fpmr_value); +;; +;; Without this optimization, fp8_kernel would set FPMR to fpmr_value each +;; time that it is called. +;; +;; We do this as a split so that hardreg_pre can optimize the moves first. +(define_split + [(set (reg:DI FPM_REGNUM) + (match_operand:DI 0 "aarch64_reg_or_zero"))] + "TARGET_FP8 && !TARGET_CHEAP_FPMR_WRITE && can_create_pseudo_p ()" + [(const_int 0)] + { + auto label = gen_label_rtx (); + rtx current = copy_to_reg (gen_rtx_REG (DImode, FPM_REGNUM)); + rtx cond = gen_rtx_EQ (VOIDmode, current, operands[0]); + emit_jump_insn (gen_cbranchdi4 (cond, current, operands[0], label)); + emit_insn (gen_aarch64_write_fpmr (operands[0])); + emit_label (label); + DONE; + } +) + +;; A write to the FPMR that is already protected by a conditional branch. +;; Since this instruction is introduced late, it shouldn't matter too much +;; that we're using an unspec for a move. +(define_insn "aarch64_write_fpmr" + [(set (reg:DI FPM_REGNUM) + (unspec:DI [(match_operand:DI 0 "aarch64_reg_or_zero" "rZ")] + UNSPEC_WRITE_FPMR))] + "TARGET_FP8" + "msr\tfpmr, %x0" +) + (define_expand "aarch64_cpymemdi" [(parallel [(set (match_operand 2) (const_int 0)) |