]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
LoongArch: Switch the default code model to medium
authorXi Ruoyao <xry111@xry111.site>
Tue, 4 Nov 2025 13:03:18 +0000 (21:03 +0800)
committerXi Ruoyao <xry111@xry111.site>
Thu, 6 Nov 2025 09:02:43 +0000 (17:02 +0800)
It has turned out the normal code model isn't enough for some large
LoongArch link units in practice.  Quoting WANG Rui's comment [1]:

We’ve actually been considering pushing for a change to the default
code model for LoongArch compilers (including GCC) for a while now.
In fact, this was one of the topics discussed in yesterday’s internal
compiler tool-chain meeting. The reason we haven’t moved forward with
it yet is that the medium code model generates a R_LARCH_CALL36
relocation, which had some issues with earlier versions of the linker.
We need to assess the impact on users before proceeding with the change.

In GCC we have build-time probe for linker call36 support and if the
linker does not support it, we fall back to pcalau12i + jirl or
la.{local,global} + jirl for the medium code model.  I also had some
concern about a potential performance regression caused by the
conservative nature of the relaxation process, but when I tested this
patch it turned out the relaxation is powerful enough to eliminate all
the pcaddu18i instructions in cc1plus and libstdc++.so.

The Loong Arch Linux project has been using -mcmodel=medium in their
{C,CXX}FLAGS building packages for a while [2] and they've not reported
any issues with that.

The Linux kernel developers has already anticipated the change and
explicitly specified -mcmodel=normal for a while [3].

Thus to me it's safe to make GCC 16 the first release with the medium
code model as the default now.  If someone must keep the normal code
model as the default for any reason, it's possible to configure GCC
using --with-cmodel=normal.

[1]: https://discourse.llvm.org/t/rfc-changing-the-default-code-model-for-loongarch/85317/3
[2]: https://github.com/lcpu-club/loongarch-packages/pull/340
[3]: https://git.kernel.org/torvalds/c/e67e0eb6a98b

gcc/

* config.gcc: Support --with-cmodel={medium,normal} and make
medium the default for LoongArch, define TARGET_DEFAULT_CMODEL
as the selected value.
* config/loongarch/loongarch-opts.cc: Use TARGET_DEFAULT_CMODEL
instead of hard coding CMODEL_NORMAL.
* doc/install.texi: Document that --with-cmodel= is supported
for LoongArch.
* doc/invoke.texi: Update the document about default code model
on LoongArch.

gcc/testsuite/

* gcc.target/loongarch/vect-frint-no-inexact.c (dg-options): Add
-mcmodel=normal.
* gcc.target/loongarch/vect-frint-scalar-no-inexact.c: Likewise.
* gcc.target/loongarch/vect-frint-scalar.c: Likewise.
* gcc.target/loongarch/vect-frint.c: Likewise.
* gcc.target/loongarch/vect-ftint-no-inexact.c: Likewise.
* gcc.target/loongarch/vect-ftint.c: Likewise.

gcc/config.gcc
gcc/config/loongarch/loongarch-opts.cc
gcc/doc/install.texi
gcc/doc/invoke.texi
gcc/testsuite/gcc.target/loongarch/vect-frint-no-inexact.c
gcc/testsuite/gcc.target/loongarch/vect-frint-scalar-no-inexact.c
gcc/testsuite/gcc.target/loongarch/vect-frint-scalar.c
gcc/testsuite/gcc.target/loongarch/vect-frint.c
gcc/testsuite/gcc.target/loongarch/vect-ftint-no-inexact.c
gcc/testsuite/gcc.target/loongarch/vect-ftint.c

index c678b801f705cd029f1cc3f11d81e7e662d06f0f..b0fa43b5eba73a2f6b4eac609f09a2449c2edd2f 100644 (file)
@@ -5118,7 +5118,7 @@ case "${target}" in
                ;;
 
        loongarch*-*)
-               supported_defaults="abi arch tune fpu simd multilib-default strict-align-lib tls"
+               supported_defaults="abi arch tune fpu simd multilib-default strict-align-lib tls cmodel"
 
                # Local variables
                unset \
@@ -5539,6 +5539,22 @@ case "${target}" in
                # Remove the excessive appending comma.
                loongarch_multilib_list_c=${loongarch_multilib_list_c%,}
                loongarch_multilib_list_make=${loongarch_multilib_list_make%,}
+
+               # Handle --with-cmodel.
+               # Make sure --with-cmodel is valid.  If it was not specified,
+               # use medium as the default value.
+               case "${with_cmodel}" in
+               "" | medium)
+                       tm_defines="${tm_defines} TARGET_DEFAULT_CMODEL=CMODEL_MEDIUM"
+                       ;;
+               normal)
+                       tm_defines="${tm_defines} TARGET_DEFAULT_CMODEL=CMODEL_NORMAL"
+                       ;;
+               *)
+                       echo "invalid option for --with-cmodel: '${with_cmodel}', available values are 'medium' and 'normal'" 1>&2
+                       exit 1
+                       ;;
+               esac
                ;;
 
        nds32*-*-*)
index 6e72084b9beb1af0f553e9187b687ded2beb1978..cacfe370345fc478cfc05ddec718747af64b1eca 100644 (file)
@@ -540,7 +540,7 @@ fallback:
 
 
   /* 5.  Target code model */
-  t.cmodel = constrained.cmodel ? target->cmodel : CMODEL_NORMAL;
+  t.cmodel = constrained.cmodel ? target->cmodel : TARGET_DEFAULT_CMODEL;
 
   switch (t.cmodel)
     {
index 50cefa1c4d7f07c4b04a47595c1adefd5a9ec162..d1068dc25ad44da04b301f4eb28aa8b2050c7f90 100644 (file)
@@ -1615,7 +1615,7 @@ Use little endian by default.  Provide a multilib for big endian.
 
 @item --with-cmodel=@var{cmodel}
 Specify what code model to use by default.
-Currently only implemented for riscv*-*-*.
+Currently only implemented for loongarch*-*-* and riscv*-*-*.
 
 @item --enable-threads
 Specify that the target
index e4a595251e632742bf56faaeacbaf1b1cffef6db..07a21fd7fc993589b9e14acfcd9789e860e0528b 100644 (file)
@@ -28544,6 +28544,8 @@ be within 2GB addressing space.
 
 @item medium
 The text segment and data segment must be within 2GB addressing space.
+This is the default code model unless GCC has been configured with
+@option{--with-cmodel=} specifying a different default code model.
 
 @item large (Not implemented yet)
 
@@ -28552,7 +28554,6 @@ This mode does not limit the size of the code segment and data segment.
 The @option{-mcmodel=extreme} option is incompatible with @option{-fplt}
 and/or @option{-mexplicit-relocs=none}.
 @end table
-The default code model is @code{normal}.
 
 @item -mexplicit-relocs=@var{style}
 Set when to use assembler relocation operators when dealing with symbolic
index 7bbaf1fba5a85a9e3e3a1ff78f425ae19482361e..e20eaea205a5be4efa79a590b022f831759a39bf 100644 (file)
@@ -1,5 +1,5 @@
 /* { dg-do compile } */
-/* { dg-options "-O2 -mabi=lp64d -mdouble-float -fno-math-errno -fno-fp-int-builtin-inexact -mlasx" } */
+/* { dg-options "-O2 -mabi=lp64d -mdouble-float -fno-math-errno -fno-fp-int-builtin-inexact -mlasx -mcmodel=normal" } */
 
 #include "vect-frint.c"
 
index 002e3b92df723e5268c98dda1b116da2999798d5..d5f0933537d0663fc6df57718c50da22ff6b538a 100644 (file)
@@ -1,5 +1,5 @@
 /* { dg-do compile } */
-/* { dg-options "-O2 -mlsx -fno-fp-int-builtin-inexact" } */
+/* { dg-options "-O2 -mlsx -fno-fp-int-builtin-inexact -mcmodel=normal" } */
 
 #include "vect-frint-scalar.c"
 
index dbcb9065ad483dbd7b4a58b2beab28e0fd55f7bc..171ba98f00b716e65f01b2b994f6e3a1988b063c 100644 (file)
@@ -1,5 +1,5 @@
 /* { dg-do compile } */
-/* { dg-options "-O2 -mlsx -ffp-int-builtin-inexact" } */
+/* { dg-options "-O2 -mlsx -ffp-int-builtin-inexact -mcmodel=normal" } */
 
 #define test(func, suffix) \
 __typeof__ (1.##suffix) \
index 6bf211e7e986095f84e8ae5b7971ef2580e5b9a6..bda041bdf9106e7d9ec8b983115bae39cbecf1a6 100644 (file)
@@ -1,5 +1,5 @@
 /* { dg-do compile } */
-/* { dg-options "-O2 -mabi=lp64d -mdouble-float -fno-math-errno -ffp-int-builtin-inexact -mlasx" } */
+/* { dg-options "-O2 -mabi=lp64d -mdouble-float -fno-math-errno -ffp-int-builtin-inexact -mlasx -mcmodel=normal" } */
 
 float out_x[8];
 double out_y[4];
index 61918beef5c6d1003ce951250c24999aed6393ea..3fa97531d598e5b2208843c4232904b6a680fe90 100644 (file)
@@ -1,5 +1,5 @@
 /* { dg-do compile } */
-/* { dg-options "-O2 -mabi=lp64d -mdouble-float -fno-math-errno -fno-fp-int-builtin-inexact -mlasx" } */
+/* { dg-options "-O2 -mabi=lp64d -mdouble-float -fno-math-errno -fno-fp-int-builtin-inexact -mlasx -mcmodel=normal" } */
 
 #include "vect-ftint.c"
 
index c4962ed17745418cc3150e4ae3273a6d9690d804..96da3cd7b576e7d1711c1027e29759c1700e5652 100644 (file)
@@ -1,5 +1,5 @@
 /* { dg-do compile } */
-/* { dg-options "-O2 -mabi=lp64d -mdouble-float -fno-math-errno -ffp-int-builtin-inexact -mlasx" } */
+/* { dg-options "-O2 -mabi=lp64d -mdouble-float -fno-math-errno -ffp-int-builtin-inexact -mlasx -mcmodel=normal" } */
 
 int out_x[8];
 long out_y[4];