Skip to content

Commit cc3ade6

Browse files
committed
LoongArch: Switch the default code model to medium
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]: lcpu-club/loongarch-packages#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.
1 parent 20ddfc9 commit cc3ade6

File tree

10 files changed

+27
-10
lines changed

10 files changed

+27
-10
lines changed

gcc/config.gcc

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5118,7 +5118,7 @@ case "${target}" in
51185118
;;
51195119

51205120
loongarch*-*)
5121-
supported_defaults="abi arch tune fpu simd multilib-default strict-align-lib tls"
5121+
supported_defaults="abi arch tune fpu simd multilib-default strict-align-lib tls cmodel"
51225122

51235123
# Local variables
51245124
unset \
@@ -5539,6 +5539,22 @@ case "${target}" in
55395539
# Remove the excessive appending comma.
55405540
loongarch_multilib_list_c=${loongarch_multilib_list_c%,}
55415541
loongarch_multilib_list_make=${loongarch_multilib_list_make%,}
5542+
5543+
# Handle --with-cmodel.
5544+
# Make sure --with-cmodel is valid. If it was not specified,
5545+
# use medium as the default value.
5546+
case "${with_cmodel}" in
5547+
"" | medium)
5548+
tm_defines="${tm_defines} TARGET_DEFAULT_CMODEL=CMODEL_MEDIUM"
5549+
;;
5550+
normal)
5551+
tm_defines="${tm_defines} TARGET_DEFAULT_CMODEL=CMODEL_NORMAL"
5552+
;;
5553+
*)
5554+
echo "invalid option for --with-cmodel: '${with_cmodel}', available values are 'medium' and 'normal'" 1>&2
5555+
exit 1
5556+
;;
5557+
esac
55425558
;;
55435559

55445560
nds32*-*-*)

gcc/config/loongarch/loongarch-opts.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -540,7 +540,7 @@ loongarch_config_target (struct loongarch_target *target,
540540

541541

542542
/* 5. Target code model */
543-
t.cmodel = constrained.cmodel ? target->cmodel : CMODEL_NORMAL;
543+
t.cmodel = constrained.cmodel ? target->cmodel : TARGET_DEFAULT_CMODEL;
544544

545545
switch (t.cmodel)
546546
{

gcc/doc/install.texi

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1615,7 +1615,7 @@ Use little endian by default. Provide a multilib for big endian.
16151615

16161616
@item --with-cmodel=@var{cmodel}
16171617
Specify what code model to use by default.
1618-
Currently only implemented for riscv*-*-*.
1618+
Currently only implemented for loongarch*-*-* and riscv*-*-*.
16191619

16201620
@item --enable-threads
16211621
Specify that the target

gcc/doc/invoke.texi

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28544,6 +28544,8 @@ be within 2GB addressing space.
2854428544

2854528545
@item medium
2854628546
The text segment and data segment must be within 2GB addressing space.
28547+
This is the default code model unless GCC has been configured with
28548+
@option{--with-cmodel=} specifying a different default code model.
2854728549

2854828550
@item large (Not implemented yet)
2854928551

@@ -28552,7 +28554,6 @@ This mode does not limit the size of the code segment and data segment.
2855228554
The @option{-mcmodel=extreme} option is incompatible with @option{-fplt}
2855328555
and/or @option{-mexplicit-relocs=none}.
2855428556
@end table
28555-
The default code model is @code{normal}.
2855628557

2855728558
@item -mexplicit-relocs=@var{style}
2855828559
Set when to use assembler relocation operators when dealing with symbolic

gcc/testsuite/gcc.target/loongarch/vect-frint-no-inexact.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* { dg-do compile } */
2-
/* { dg-options "-O2 -mabi=lp64d -mdouble-float -fno-math-errno -fno-fp-int-builtin-inexact -mlasx" } */
2+
/* { dg-options "-O2 -mabi=lp64d -mdouble-float -fno-math-errno -fno-fp-int-builtin-inexact -mlasx -mcmodel=normal" } */
33

44
#include "vect-frint.c"
55

gcc/testsuite/gcc.target/loongarch/vect-frint-scalar-no-inexact.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* { dg-do compile } */
2-
/* { dg-options "-O2 -mlsx -fno-fp-int-builtin-inexact" } */
2+
/* { dg-options "-O2 -mlsx -fno-fp-int-builtin-inexact -mcmodel=normal" } */
33

44
#include "vect-frint-scalar.c"
55

gcc/testsuite/gcc.target/loongarch/vect-frint-scalar.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* { dg-do compile } */
2-
/* { dg-options "-O2 -mlsx -ffp-int-builtin-inexact" } */
2+
/* { dg-options "-O2 -mlsx -ffp-int-builtin-inexact -mcmodel=normal" } */
33

44
#define test(func, suffix) \
55
__typeof__ (1.##suffix) \

gcc/testsuite/gcc.target/loongarch/vect-frint.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* { dg-do compile } */
2-
/* { dg-options "-O2 -mabi=lp64d -mdouble-float -fno-math-errno -ffp-int-builtin-inexact -mlasx" } */
2+
/* { dg-options "-O2 -mabi=lp64d -mdouble-float -fno-math-errno -ffp-int-builtin-inexact -mlasx -mcmodel=normal" } */
33

44
float out_x[8];
55
double out_y[4];

gcc/testsuite/gcc.target/loongarch/vect-ftint-no-inexact.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* { dg-do compile } */
2-
/* { dg-options "-O2 -mabi=lp64d -mdouble-float -fno-math-errno -fno-fp-int-builtin-inexact -mlasx" } */
2+
/* { dg-options "-O2 -mabi=lp64d -mdouble-float -fno-math-errno -fno-fp-int-builtin-inexact -mlasx -mcmodel=normal" } */
33

44
#include "vect-ftint.c"
55

gcc/testsuite/gcc.target/loongarch/vect-ftint.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* { dg-do compile } */
2-
/* { dg-options "-O2 -mabi=lp64d -mdouble-float -fno-math-errno -ffp-int-builtin-inexact -mlasx" } */
2+
/* { dg-options "-O2 -mabi=lp64d -mdouble-float -fno-math-errno -ffp-int-builtin-inexact -mlasx -mcmodel=normal" } */
33

44
int out_x[8];
55
long out_y[4];

0 commit comments

Comments
 (0)