public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [PATCH v1 0/4] Fix a series of problems caused by
@ 2024-02-21  3:04 Lulu Cheng
  2024-02-21  3:04 ` [PATCH v1 1/4] LoongArch: Delete macro definition ASM_OUTPUT_ALIGN_WITH_NOP Lulu Cheng
                   ` (4 more replies)
  0 siblings, 5 replies; 8+ messages in thread
From: Lulu Cheng @ 2024-02-21  3:04 UTC (permalink / raw)
  To: gcc-patches; +Cc: xry111, i, xuchenghua, chenglulu

Because binutils2.42 corrects the implementation of
".align [abs-expr,[abs-expr[,abs-expr]]]".
The macro ASM_OUTPUT_ALIGN_WITH_NOP in GCC uses this assembler directive,
and an error occurs. See link below for detailed description.
https://gcc.gnu.org/pipermail/gcc-patches/2024-February/645067.html

In order to solve the above problems, do the following operations:

1. Delete macro definition ASM_OUTPUT_ALIGN_WITH_NOP. (cherry pick r14-4674)
2. Check whether binutils supports the relax function. (cherry pick r14-4160)
3. Disable relaxation if the assembler don't support
  conditional branch relaxation. (cherry pick r14-5434)

PR112299 is also fixed here.

Lulu Cheng (2):
  LoongArch: Delete macro definition ASM_OUTPUT_ALIGN_WITH_NOP.
  LoongArch: Check whether binutils supports the relax function. If
    supported, explicit relocs are turned off by default.

Xi Ruoyao (2):
  LoongArch: Disable relaxation if the assembler don't support
    conditional branch relaxation [PR112330]
  LoongArch: Define HAVE_AS_TLS to 0 if it's undefined [PR112299]

 gcc/config.in                                 | 12 ++++
 gcc/config/loongarch/genopts/loongarch.opt.in | 11 +++-
 gcc/config/loongarch/gnu-user.h               |  3 +-
 gcc/config/loongarch/loongarch-opts.h         | 12 ++++
 gcc/config/loongarch/loongarch.h              | 22 +++++--
 gcc/config/loongarch/loongarch.opt            | 11 +++-
 gcc/configure                                 | 66 +++++++++++++++++++
 gcc/configure.ac                              | 14 ++++
 gcc/doc/invoke.texi                           | 24 ++++++-
 9 files changed, 165 insertions(+), 10 deletions(-)

-- 
2.39.3


^ permalink raw reply	[flat|nested] 8+ messages in thread

* [PATCH v1 1/4] LoongArch: Delete macro definition ASM_OUTPUT_ALIGN_WITH_NOP.
  2024-02-21  3:04 [PATCH v1 0/4] Fix a series of problems caused by Lulu Cheng
@ 2024-02-21  3:04 ` Lulu Cheng
  2024-02-21  3:04 ` [PATCH v1 2/4] LoongArch: Check whether binutils supports the relax function. If supported, explicit relocs are turned off by default Lulu Cheng
                   ` (3 subsequent siblings)
  4 siblings, 0 replies; 8+ messages in thread
From: Lulu Cheng @ 2024-02-21  3:04 UTC (permalink / raw)
  To: gcc-patches; +Cc: xry111, i, xuchenghua, chenglulu

There are two reasons for removing this macro definition:
1. The default in the assembler is to use the nop instruction for filling.
2. For assembly directives: .align [abs-expr[, abs-expr[, abs-expr]]]
   The third expression it is the maximum number of bytes that should be
   skipped by this alignment directive.
   Therefore, it will affect the display of the specified alignment rules
   and affect the operating efficiency.

This modification relies on binutils commit 1fb3cdd87ec61715a5684925fb6d6a6cf53bb97c.
(Since the assembler will add nop based on the .align information when doing relax,
it will cause the conditional branch to go out of bounds during the assembly process.
This submission of binutils solves this problem.)

gcc/ChangeLog:

	* config/loongarch/loongarch.h (ASM_OUTPUT_ALIGN_WITH_NOP):
	Delete.

Co-authored-by: Chenghua Xu <xuchenghua@loongson.cn>

(cherry picked from commit b20c7ee066cb7d952fa193972e8bc6362c6e4063)
---
 gcc/config/loongarch/loongarch.h | 5 -----
 1 file changed, 5 deletions(-)

diff --git a/gcc/config/loongarch/loongarch.h b/gcc/config/loongarch/loongarch.h
index f0db67f8c7b..cc719d0c796 100644
--- a/gcc/config/loongarch/loongarch.h
+++ b/gcc/config/loongarch/loongarch.h
@@ -982,11 +982,6 @@ typedef struct {
 
 #define ASM_OUTPUT_ALIGN(STREAM, LOG) fprintf (STREAM, "\t.align\t%d\n", (LOG))
 
-/* "nop" instruction 54525952 (andi $r0,$r0,0) is
-   used for padding.  */
-#define ASM_OUTPUT_ALIGN_WITH_NOP(STREAM, LOG) \
-  fprintf (STREAM, "\t.align\t%d,54525952,4\n", (LOG))
-
 /* This is how to output an assembler line to advance the location
    counter by SIZE bytes.  */
 
-- 
2.39.3


^ permalink raw reply	[flat|nested] 8+ messages in thread

* [PATCH v1 2/4] LoongArch: Check whether binutils supports the relax function. If supported, explicit relocs are turned off by default.
  2024-02-21  3:04 [PATCH v1 0/4] Fix a series of problems caused by Lulu Cheng
  2024-02-21  3:04 ` [PATCH v1 1/4] LoongArch: Delete macro definition ASM_OUTPUT_ALIGN_WITH_NOP Lulu Cheng
@ 2024-02-21  3:04 ` Lulu Cheng
  2024-02-21  3:04 ` [PATCH v1 3/4] LoongArch: Disable relaxation if the assembler don't support conditional branch relaxation [PR112330] Lulu Cheng
                   ` (2 subsequent siblings)
  4 siblings, 0 replies; 8+ messages in thread
From: Lulu Cheng @ 2024-02-21  3:04 UTC (permalink / raw)
  To: gcc-patches; +Cc: xry111, i, xuchenghua, chenglulu

gcc/ChangeLog:

	* config.in: Regenerate.
	* config/loongarch/genopts/loongarch.opt.in: Add compilation option
	mrelax. And set the initial value of explicit-relocs according to the
	detection status.
	* config/loongarch/gnu-user.h: When compiling with -mno-relax, pass the
	--no-relax option to the linker.
	* config/loongarch/loongarch-driver.h (ASM_SPEC): When compiling with
	-mno-relax, pass the -mno-relax option to the assembler.
	* config/loongarch/loongarch-opts.h (HAVE_AS_MRELAX_OPTION): Define macro.
	* config/loongarch/loongarch.opt: Regenerate.
	* configure: Regenerate.
	* configure.ac: Add detection of support for binutils relax function.

(cherry picked from commint 9bab65a77049edcc7afc59532173206ee816e726)
---
 gcc/config.in                                 |  6 ++++
 gcc/config/loongarch/genopts/loongarch.opt.in |  7 ++++-
 gcc/config/loongarch/gnu-user.h               |  3 +-
 gcc/config/loongarch/loongarch-opts.h         |  4 +++
 gcc/config/loongarch/loongarch.opt            |  7 ++++-
 gcc/configure                                 | 31 +++++++++++++++++++
 gcc/configure.ac                              |  4 +++
 7 files changed, 59 insertions(+), 3 deletions(-)

diff --git a/gcc/config.in b/gcc/config.in
index ef35af16f2f..36a74dd5974 100644
--- a/gcc/config.in
+++ b/gcc/config.in
@@ -636,6 +636,12 @@
 #endif
 
 
+/* Define if your assembler supports -mrelax option. */
+#ifndef USED_FOR_TARGET
+#undef HAVE_AS_MRELAX_OPTION
+#endif
+
+
 /* Define if your assembler supports .mspabi_attribute. */
 #ifndef USED_FOR_TARGET
 #undef HAVE_AS_MSPABI_ATTRIBUTE
diff --git a/gcc/config/loongarch/genopts/loongarch.opt.in b/gcc/config/loongarch/genopts/loongarch.opt.in
index 4b9b4ac273e..e7c32e61a50 100644
--- a/gcc/config/loongarch/genopts/loongarch.opt.in
+++ b/gcc/config/loongarch/genopts/loongarch.opt.in
@@ -155,7 +155,7 @@ Target Joined RejectNegative UInteger Var(loongarch_max_inline_memcpy_size) Init
 -mmax-inline-memcpy-size=SIZE	Set the max size of memcpy to inline, default is 1024.
 
 mexplicit-relocs
-Target Var(TARGET_EXPLICIT_RELOCS) Init(HAVE_AS_EXPLICIT_RELOCS)
+Target Var(TARGET_EXPLICIT_RELOCS) Init(HAVE_AS_EXPLICIT_RELOCS & !HAVE_AS_MRELAX_OPTION)
 Use %reloc() assembly operators.
 
 ; The code model option names for -mcmodel.
@@ -188,3 +188,8 @@ Specify the code model.
 mdirect-extern-access
 Target Var(TARGET_DIRECT_EXTERN_ACCESS) Init(0)
 Avoid using the GOT to access external symbols.
+
+mrelax
+Target Var(loongarch_mrelax) Init(HAVE_AS_MRELAX_OPTION)
+Take advantage of linker relaxations to reduce the number of instructions
+required to materialize symbol addresses.
diff --git a/gcc/config/loongarch/gnu-user.h b/gcc/config/loongarch/gnu-user.h
index 5f1bd60ada3..e9f4bcef1d4 100644
--- a/gcc/config/loongarch/gnu-user.h
+++ b/gcc/config/loongarch/gnu-user.h
@@ -48,7 +48,8 @@ along with GCC; see the file COPYING3.  If not see
   "%{!shared: %{static} " \
   "%{!static: %{!static-pie: %{rdynamic:-export-dynamic} " \
   "-dynamic-linker " GNU_USER_DYNAMIC_LINKER "}} " \
-  "%{static-pie: -static -pie --no-dynamic-linker -z text}}"
+  "%{static-pie: -static -pie --no-dynamic-linker -z text}}" \
+  "%{mno-relax: --no-relax}"
 
 
 /* Similar to standard Linux, but adding -ffast-math support.  */
diff --git a/gcc/config/loongarch/loongarch-opts.h b/gcc/config/loongarch/loongarch-opts.h
index b1ff54426e4..7ea02f4978c 100644
--- a/gcc/config/loongarch/loongarch-opts.h
+++ b/gcc/config/loongarch/loongarch-opts.h
@@ -92,4 +92,8 @@ loongarch_config_target (struct loongarch_target *target,
 #define HAVE_AS_EXPLICIT_RELOCS 0
 #endif
 
+#ifndef HAVE_AS_MRELAX_OPTION
+#define HAVE_AS_MRELAX_OPTION 0
+#endif
+
 #endif /* LOONGARCH_OPTS_H */
diff --git a/gcc/config/loongarch/loongarch.opt b/gcc/config/loongarch/loongarch.opt
index 68018ade73f..e37ed9015de 100644
--- a/gcc/config/loongarch/loongarch.opt
+++ b/gcc/config/loongarch/loongarch.opt
@@ -162,7 +162,7 @@ Target Joined RejectNegative UInteger Var(loongarch_max_inline_memcpy_size) Init
 -mmax-inline-memcpy-size=SIZE	Set the max size of memcpy to inline, default is 1024.
 
 mexplicit-relocs
-Target Var(TARGET_EXPLICIT_RELOCS) Init(HAVE_AS_EXPLICIT_RELOCS)
+Target Var(TARGET_EXPLICIT_RELOCS) Init(HAVE_AS_EXPLICIT_RELOCS & !HAVE_AS_MRELAX_OPTION)
 Use %reloc() assembly operators.
 
 ; The code model option names for -mcmodel.
@@ -195,3 +195,8 @@ Specify the code model.
 mdirect-extern-access
 Target Var(TARGET_DIRECT_EXTERN_ACCESS) Init(0)
 Avoid using the GOT to access external symbols.
+
+mrelax
+Target Var(loongarch_mrelax) Init(HAVE_AS_MRELAX_OPTION)
+Take advantage of linker relaxations to reduce the number of instructions
+required to materialize symbol addresses.
diff --git a/gcc/configure b/gcc/configure
index dec2eca1a45..760bea9d4a0 100755
--- a/gcc/configure
+++ b/gcc/configure
@@ -29075,6 +29075,37 @@ if test $gcc_cv_as_loongarch_eh_frame_pcrel_encoding_support = yes; then
 
 $as_echo "#define HAVE_AS_EH_FRAME_PCREL_ENCODING_SUPPORT 1" >>confdefs.h
 
+fi
+
+    { $as_echo "$as_me:${as_lineno-$LINENO}: checking assembler for -mrelax option" >&5
+$as_echo_n "checking assembler for -mrelax option... " >&6; }
+if ${gcc_cv_as_loongarch_relax+:} false; then :
+  $as_echo_n "(cached) " >&6
+else
+  gcc_cv_as_loongarch_relax=no
+  if test x$gcc_cv_as != x; then
+    $as_echo '.text' > conftest.s
+    if { ac_try='$gcc_cv_as $gcc_cv_as_flags -mrelax -o conftest.o conftest.s >&5'
+  { { eval echo "\"\$as_me\":${as_lineno-$LINENO}: \"$ac_try\""; } >&5
+  (eval $ac_try) 2>&5
+  ac_status=$?
+  $as_echo "$as_me:${as_lineno-$LINENO}: \$? = $ac_status" >&5
+  test $ac_status = 0; }; }
+    then
+	gcc_cv_as_loongarch_relax=yes
+    else
+      echo "configure: failed program was" >&5
+      cat conftest.s >&5
+    fi
+    rm -f conftest.o conftest.s
+  fi
+fi
+{ $as_echo "$as_me:${as_lineno-$LINENO}: result: $gcc_cv_as_loongarch_relax" >&5
+$as_echo "$gcc_cv_as_loongarch_relax" >&6; }
+if test $gcc_cv_as_loongarch_relax = yes; then
+
+$as_echo "#define HAVE_AS_MRELAX_OPTION 1" >>confdefs.h
+
 fi
 
     ;;
diff --git a/gcc/configure.ac b/gcc/configure.ac
index c0d067ec271..4f3c4ebb5a5 100644
--- a/gcc/configure.ac
+++ b/gcc/configure.ac
@@ -5366,6 +5366,10 @@ x:
        .cfi_endproc],,
       [AC_DEFINE(HAVE_AS_EH_FRAME_PCREL_ENCODING_SUPPORT, 1,
 	  [Define if your assembler supports eh_frame pcrel encoding.])])
+    gcc_GAS_CHECK_FEATURE([-mrelax option], gcc_cv_as_loongarch_relax,
+      [-mrelax], [.text],,
+      [AC_DEFINE(HAVE_AS_MRELAX_OPTION, 1,
+		[Define if your assembler supports -mrelax option.])])
     ;;
     s390*-*-*)
     gcc_GAS_CHECK_FEATURE([.gnu_attribute support],
-- 
2.39.3


^ permalink raw reply	[flat|nested] 8+ messages in thread

* [PATCH v1 3/4] LoongArch: Disable relaxation if the assembler don't support conditional branch relaxation [PR112330]
  2024-02-21  3:04 [PATCH v1 0/4] Fix a series of problems caused by Lulu Cheng
  2024-02-21  3:04 ` [PATCH v1 1/4] LoongArch: Delete macro definition ASM_OUTPUT_ALIGN_WITH_NOP Lulu Cheng
  2024-02-21  3:04 ` [PATCH v1 2/4] LoongArch: Check whether binutils supports the relax function. If supported, explicit relocs are turned off by default Lulu Cheng
@ 2024-02-21  3:04 ` Lulu Cheng
  2024-02-21  3:04 ` [PATCH v1 4/4] LoongArch: Define HAVE_AS_TLS to 0 if it's undefined [PR112299] Lulu Cheng
  2024-02-22  3:25 ` Re:[pushed] [PATCH v1 0/4] Fix a series of problems caused by chenglulu
  4 siblings, 0 replies; 8+ messages in thread
From: Lulu Cheng @ 2024-02-21  3:04 UTC (permalink / raw)
  To: gcc-patches; +Cc: xry111, i, xuchenghua, chenglulu

From: Xi Ruoyao <xry111@xry111.site>

As the commit message of r14-4674 has indicated, if the assembler does
not support conditional branch relaxation, a relocation overflow may
happen on conditional branches when relaxation is enabled because the
number of NOP instructions inserted by the assembler will be more than
the number estimated by GCC.

To work around this issue, disable relaxation by default if the
assembler is detected incapable to perform conditional branch relaxation
at GCC build time.  We also need to pass -mno-relax to the assembler to
really disable relaxation.  But, if the assembler does not support
-mrelax option at all, we should not pass -mno-relax to the assembler or
it will immediately error out.  Also handle this with the build time
assembler capability probing, and add a pair of options
-m[no-]pass-mrelax-to-as to allow using a different assembler from the
build-time one.

With this change, if GCC is built with GAS 2.41, relaxation will be
disabled by default.  So the default value of -mexplicit-relocs= is also
changed to 'always' if -mno-relax is specified or implied by the
build-time default, because using assembler macros for symbol addresses
produces no benefit when relaxation is disabled.

gcc/ChangeLog:

	PR target/112330
	* config/loongarch/genopts/loongarch.opt.in: Add
	-m[no]-pass-relax-to-as.  Change the default of -m[no]-relax to
	account conditional branch relaxation support status.
	* config/loongarch/loongarch.opt: Regenerate.
	* configure.ac (gcc_cv_as_loongarch_cond_branch_relax): Check if
	the assembler supports conditional branch relaxation.
	* configure: Regenerate.
	* config.in: Regenerate.  Note that there are some unrelated
	changes introduced by r14-5424 (which does not contain a
	config.in regeneration).
	* config/loongarch/loongarch-opts.h
	(HAVE_AS_COND_BRANCH_RELAXATION): Define to 0 if not defined.
	* config/loongarch/loongarch-driver.h (ASM_MRELAX_DEFAULT):
	Define.
	(ASM_MRELAX_SPEC): Define.
	(ASM_SPEC): Use ASM_MRELAX_SPEC instead of "%{mno-relax}".
	* config/loongarch/loongarch.cc: Take the setting of
	-m[no-]relax into account when determining the default of
	-mexplicit-relocs=.
	* doc/invoke.texi: Document -m[no-]relax and
	-m[no-]pass-mrelax-to-as for LoongArch.  Update the default
	value of -mexplicit-relocs=.

(cherry picked from commit fe23a2ff1f5072559552be0e41ab55bf72f5c79f)
---
 gcc/config.in                                 |  6 ++++
 gcc/config/loongarch/genopts/loongarch.opt.in |  6 +++-
 gcc/config/loongarch/loongarch-opts.h         |  4 +++
 gcc/config/loongarch/loongarch.h              | 17 ++++++++-
 gcc/config/loongarch/loongarch.opt            |  6 +++-
 gcc/configure                                 | 35 +++++++++++++++++++
 gcc/configure.ac                              | 10 ++++++
 gcc/doc/invoke.texi                           | 24 ++++++++++++-
 8 files changed, 104 insertions(+), 4 deletions(-)

diff --git a/gcc/config.in b/gcc/config.in
index 36a74dd5974..83c98ae1457 100644
--- a/gcc/config.in
+++ b/gcc/config.in
@@ -373,6 +373,12 @@
 #endif
 
 
+/* Define if your assembler supports conditional branch relaxation. */
+#ifndef USED_FOR_TARGET
+#undef HAVE_AS_COND_BRANCH_RELAXATION
+#endif
+
+
 /* Define if your assembler supports the --debug-prefix-map option. */
 #ifndef USED_FOR_TARGET
 #undef HAVE_AS_DEBUG_PREFIX_MAP
diff --git a/gcc/config/loongarch/genopts/loongarch.opt.in b/gcc/config/loongarch/genopts/loongarch.opt.in
index e7c32e61a50..da6fedd153e 100644
--- a/gcc/config/loongarch/genopts/loongarch.opt.in
+++ b/gcc/config/loongarch/genopts/loongarch.opt.in
@@ -190,6 +190,10 @@ Target Var(TARGET_DIRECT_EXTERN_ACCESS) Init(0)
 Avoid using the GOT to access external symbols.
 
 mrelax
-Target Var(loongarch_mrelax) Init(HAVE_AS_MRELAX_OPTION)
+Target Var(loongarch_mrelax) Init(HAVE_AS_MRELAX_OPTION && HAVE_AS_COND_BRANCH_RELAXATION)
 Take advantage of linker relaxations to reduce the number of instructions
 required to materialize symbol addresses.
+
+mpass-mrelax-to-as
+Target Var(loongarch_pass_mrelax_to_as) Init(HAVE_AS_MRELAX_OPTION)
+Pass -mrelax or -mno-relax option to the assembler.
diff --git a/gcc/config/loongarch/loongarch-opts.h b/gcc/config/loongarch/loongarch-opts.h
index 7ea02f4978c..edd41c82b17 100644
--- a/gcc/config/loongarch/loongarch-opts.h
+++ b/gcc/config/loongarch/loongarch-opts.h
@@ -96,4 +96,8 @@ loongarch_config_target (struct loongarch_target *target,
 #define HAVE_AS_MRELAX_OPTION 0
 #endif
 
+#ifndef HAVE_AS_COND_BRANCH_RELAXATION
+#define HAVE_AS_COND_BRANCH_RELAXATION 0
+#endif
+
 #endif /* LOONGARCH_OPTS_H */
diff --git a/gcc/config/loongarch/loongarch.h b/gcc/config/loongarch/loongarch.h
index cc719d0c796..d072522e3cf 100644
--- a/gcc/config/loongarch/loongarch.h
+++ b/gcc/config/loongarch/loongarch.h
@@ -69,8 +69,23 @@ along with GCC; see the file COPYING3.  If not see
 #define SUBTARGET_ASM_SPEC ""
 #endif
 
+#if HAVE_AS_MRELAX_OPTION && HAVE_AS_COND_BRANCH_RELAXATION
+#define ASM_MRELAX_DEFAULT "%{!mrelax:%{!mno-relax:-mrelax}}"
+#else
+#define ASM_MRELAX_DEFAULT "%{!mrelax:%{!mno-relax:-mno-relax}}"
+#endif
+
+#if HAVE_AS_MRELAX_OPTION
+#define ASM_MRELAX_SPEC \
+  "%{!mno-pass-mrelax-to-as:%{mrelax} %{mno-relax} " ASM_MRELAX_DEFAULT "}"
+#else
+#define ASM_MRELAX_SPEC \
+  "%{mpass-mrelax-to-as:%{mrelax} %{mno-relax} " ASM_MRELAX_DEFAULT "}"
+#endif
+
 #undef ASM_SPEC
-#define ASM_SPEC "%{mabi=*} %{subtarget_asm_spec}"
+#define ASM_SPEC \
+  "%{mabi=*} " ASM_MRELAX_SPEC " %(subtarget_asm_spec)"
 
 /* Extra switches sometimes passed to the linker.  */
 
diff --git a/gcc/config/loongarch/loongarch.opt b/gcc/config/loongarch/loongarch.opt
index e37ed9015de..59b1e06d3f2 100644
--- a/gcc/config/loongarch/loongarch.opt
+++ b/gcc/config/loongarch/loongarch.opt
@@ -197,6 +197,10 @@ Target Var(TARGET_DIRECT_EXTERN_ACCESS) Init(0)
 Avoid using the GOT to access external symbols.
 
 mrelax
-Target Var(loongarch_mrelax) Init(HAVE_AS_MRELAX_OPTION)
+Target Var(loongarch_mrelax) Init(HAVE_AS_MRELAX_OPTION && HAVE_AS_COND_BRANCH_RELAXATION)
 Take advantage of linker relaxations to reduce the number of instructions
 required to materialize symbol addresses.
+
+mpass-mrelax-to-as
+Target Var(loongarch_pass_mrelax_to_as) Init(HAVE_AS_MRELAX_OPTION)
+Pass -mrelax or -mno-relax option to the assembler.
diff --git a/gcc/configure b/gcc/configure
index 760bea9d4a0..8a92601ef40 100755
--- a/gcc/configure
+++ b/gcc/configure
@@ -29106,6 +29106,41 @@ if test $gcc_cv_as_loongarch_relax = yes; then
 
 $as_echo "#define HAVE_AS_MRELAX_OPTION 1" >>confdefs.h
 
+fi
+
+    { $as_echo "$as_me:${as_lineno-$LINENO}: checking assembler for conditional branch relaxation support" >&5
+$as_echo_n "checking assembler for conditional branch relaxation support... " >&6; }
+if ${gcc_cv_as_loongarch_cond_branch_relax+:} false; then :
+  $as_echo_n "(cached) " >&6
+else
+  gcc_cv_as_loongarch_cond_branch_relax=no
+  if test x$gcc_cv_as != x; then
+    $as_echo 'a:
+       .rept 32769
+       nop
+       .endr
+       beq $a0,$a1,a' > conftest.s
+    if { ac_try='$gcc_cv_as $gcc_cv_as_flags --fatal-warnings -o conftest.o conftest.s >&5'
+  { { eval echo "\"\$as_me\":${as_lineno-$LINENO}: \"$ac_try\""; } >&5
+  (eval $ac_try) 2>&5
+  ac_status=$?
+  $as_echo "$as_me:${as_lineno-$LINENO}: \$? = $ac_status" >&5
+  test $ac_status = 0; }; }
+    then
+	gcc_cv_as_loongarch_cond_branch_relax=yes
+    else
+      echo "configure: failed program was" >&5
+      cat conftest.s >&5
+    fi
+    rm -f conftest.o conftest.s
+  fi
+fi
+{ $as_echo "$as_me:${as_lineno-$LINENO}: result: $gcc_cv_as_loongarch_cond_branch_relax" >&5
+$as_echo "$gcc_cv_as_loongarch_cond_branch_relax" >&6; }
+if test $gcc_cv_as_loongarch_cond_branch_relax = yes; then
+
+$as_echo "#define HAVE_AS_COND_BRANCH_RELAXATION 1" >>confdefs.h
+
 fi
 
     ;;
diff --git a/gcc/configure.ac b/gcc/configure.ac
index 4f3c4ebb5a5..f2e61a32d7e 100644
--- a/gcc/configure.ac
+++ b/gcc/configure.ac
@@ -5370,6 +5370,16 @@ x:
       [-mrelax], [.text],,
       [AC_DEFINE(HAVE_AS_MRELAX_OPTION, 1,
 		[Define if your assembler supports -mrelax option.])])
+    gcc_GAS_CHECK_FEATURE([conditional branch relaxation support],
+      gcc_cv_as_loongarch_cond_branch_relax,
+      [--fatal-warnings],
+      [a:
+       .rept 32769
+       nop
+       .endr
+       beq $a0,$a1,a],,
+      [AC_DEFINE(HAVE_AS_COND_BRANCH_RELAXATION, 1,
+		[Define if your assembler supports conditional branch relaxation.])])
     ;;
     s390*-*-*)
     gcc_GAS_CHECK_FEATURE([.gnu_attribute support],
diff --git a/gcc/doc/invoke.texi b/gcc/doc/invoke.texi
index 49941bf0370..99657fb44d8 100644
--- a/gcc/doc/invoke.texi
+++ b/gcc/doc/invoke.texi
@@ -1030,7 +1030,7 @@ Objective-C and Objective-C++ Dialects}.
 -mmax-inline-memcpy-size=@var{n}
 -mexplicit-relocs -mno-explicit-relocs
 -mdirect-extern-access -mno-direct-extern-access
--mcmodel=@var{code-model}}
+-mcmodel=@var{code-model} -mrelax -mpass-mrelax-to-as}
 
 @emph{M32R/D Options}
 @gccoptlist{-m32r2  -m32rx  -m32r
@@ -25850,6 +25850,28 @@ environments where no dynamic link is performed, like firmwares, OS
 kernels, executables linked with @option{-static} or @option{-static-pie}.
 @option{-mdirect-extern-access} is not compatible with @option{-fPIC} or
 @option{-fpic}.
+
+@item -mrelax
+@itemx -mno-relax
+Take (do not take) advantage of linker relaxations.  If
+@option{-mpass-mrelax-to-as} is enabled, this option is also passed to
+the assembler.  The default is determined during GCC build-time by
+detecting corresponding assembler support:
+@option{-mrelax} if the assembler supports both the @option{-mrelax}
+option and the conditional branch relaxation (it's required or the
+@code{.align} directives and conditional branch instructions in the
+assembly code outputted by GCC may be rejected by the assembler because
+of a relocation overflow), @option{-mno-relax} otherwise.
+
+@item -mpass-mrelax-to-as
+@itemx -mno-pass-mrelax-to-as
+Pass (do not pass) the @option{-mrelax} or @option{-mno-relax} option
+to the assembler.  The default is determined during GCC build-time by
+detecting corresponding assembler support:
+@option{-mpass-mrelax-to-as} if the assembler supports the
+@option{-mrelax} option, @option{-mno-pass-mrelax-to-as} otherwise.
+This option is mostly useful for debugging, or interoperation with
+assemblers different from the build-time one.
 @end table
 
 @node M32C Options
-- 
2.39.3


^ permalink raw reply	[flat|nested] 8+ messages in thread

* [PATCH v1 4/4] LoongArch: Define HAVE_AS_TLS to 0 if it's undefined [PR112299]
  2024-02-21  3:04 [PATCH v1 0/4] Fix a series of problems caused by Lulu Cheng
                   ` (2 preceding siblings ...)
  2024-02-21  3:04 ` [PATCH v1 3/4] LoongArch: Disable relaxation if the assembler don't support conditional branch relaxation [PR112330] Lulu Cheng
@ 2024-02-21  3:04 ` Lulu Cheng
  2024-02-22  3:25 ` Re:[pushed] [PATCH v1 0/4] Fix a series of problems caused by chenglulu
  4 siblings, 0 replies; 8+ messages in thread
From: Lulu Cheng @ 2024-02-21  3:04 UTC (permalink / raw)
  To: gcc-patches; +Cc: xry111, i, xuchenghua, chenglulu

From: Xi Ruoyao <xry111@xry111.site>

Now loongarch.md uses HAVE_AS_TLS, we need this to fix the failure
building a cross compiler if the cross assembler is not installed yet.

gcc/ChangeLog:

	PR target/112299
	* config/loongarch/loongarch-opts.h (HAVE_AS_TLS): Define to 0
	if not defined yet.

(cherry picked from commit 6bf2cebe2bf49919c78814cb447d3aa6e3550d89)
---
 gcc/config/loongarch/loongarch-opts.h | 4 ++++
 1 file changed, 4 insertions(+)

diff --git a/gcc/config/loongarch/loongarch-opts.h b/gcc/config/loongarch/loongarch-opts.h
index edd41c82b17..02184e2991a 100644
--- a/gcc/config/loongarch/loongarch-opts.h
+++ b/gcc/config/loongarch/loongarch-opts.h
@@ -100,4 +100,8 @@ loongarch_config_target (struct loongarch_target *target,
 #define HAVE_AS_COND_BRANCH_RELAXATION 0
 #endif
 
+#ifndef HAVE_AS_TLS
+#define HAVE_AS_TLS 0
+#endif
+
 #endif /* LOONGARCH_OPTS_H */
-- 
2.39.3


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re:[pushed] [PATCH v1 0/4] Fix a series of problems caused by
  2024-02-21  3:04 [PATCH v1 0/4] Fix a series of problems caused by Lulu Cheng
                   ` (3 preceding siblings ...)
  2024-02-21  3:04 ` [PATCH v1 4/4] LoongArch: Define HAVE_AS_TLS to 0 if it's undefined [PR112299] Lulu Cheng
@ 2024-02-22  3:25 ` chenglulu
  4 siblings, 0 replies; 8+ messages in thread
From: chenglulu @ 2024-02-22  3:25 UTC (permalink / raw)
  To: gcc-patches; +Cc: xry111, i, xuchenghua

Pushed to r13-8349...r13-8352.

在 2024/2/21 上午11:04, Lulu Cheng 写道:
> Because binutils2.42 corrects the implementation of
> ".align [abs-expr,[abs-expr[,abs-expr]]]".
> The macro ASM_OUTPUT_ALIGN_WITH_NOP in GCC uses this assembler directive,
> and an error occurs. See link below for detailed description.
> https://gcc.gnu.org/pipermail/gcc-patches/2024-February/645067.html
>
> In order to solve the above problems, do the following operations:
>
> 1. Delete macro definition ASM_OUTPUT_ALIGN_WITH_NOP. (cherry pick r14-4674)
> 2. Check whether binutils supports the relax function. (cherry pick r14-4160)
> 3. Disable relaxation if the assembler don't support
>    conditional branch relaxation. (cherry pick r14-5434)
>
> PR112299 is also fixed here.
>
> Lulu Cheng (2):
>    LoongArch: Delete macro definition ASM_OUTPUT_ALIGN_WITH_NOP.
>    LoongArch: Check whether binutils supports the relax function. If
>      supported, explicit relocs are turned off by default.
>
> Xi Ruoyao (2):
>    LoongArch: Disable relaxation if the assembler don't support
>      conditional branch relaxation [PR112330]
>    LoongArch: Define HAVE_AS_TLS to 0 if it's undefined [PR112299]
>
>   gcc/config.in                                 | 12 ++++
>   gcc/config/loongarch/genopts/loongarch.opt.in | 11 +++-
>   gcc/config/loongarch/gnu-user.h               |  3 +-
>   gcc/config/loongarch/loongarch-opts.h         | 12 ++++
>   gcc/config/loongarch/loongarch.h              | 22 +++++--
>   gcc/config/loongarch/loongarch.opt            | 11 +++-
>   gcc/configure                                 | 66 +++++++++++++++++++
>   gcc/configure.ac                              | 14 ++++
>   gcc/doc/invoke.texi                           | 24 ++++++-
>   9 files changed, 165 insertions(+), 10 deletions(-)
>


^ permalink raw reply	[flat|nested] 8+ messages in thread

* [PATCH v1 1/4] LoongArch: Delete macro definition ASM_OUTPUT_ALIGN_WITH_NOP.
  2024-02-21  3:10 [PATCH v1 0/4] Fix a series of problems caused by ASM_OUTPUT_ALIGN_WITH_NOP (release/gcc-12) Lulu Cheng
@ 2024-02-21  3:10 ` Lulu Cheng
  0 siblings, 0 replies; 8+ messages in thread
From: Lulu Cheng @ 2024-02-21  3:10 UTC (permalink / raw)
  To: gcc-patches; +Cc: xry111, i, xuchenghua, chenglulu

There are two reasons for removing this macro definition:
1. The default in the assembler is to use the nop instruction for filling.
2. For assembly directives: .align [abs-expr[, abs-expr[, abs-expr]]]
   The third expression it is the maximum number of bytes that should be
   skipped by this alignment directive.
   Therefore, it will affect the display of the specified alignment rules
   and affect the operating efficiency.

This modification relies on binutils commit 1fb3cdd87ec61715a5684925fb6d6a6cf53bb97c.
(Since the assembler will add nop based on the .align information when doing relax,
it will cause the conditional branch to go out of bounds during the assembly process.
This submission of binutils solves this problem.)

gcc/ChangeLog:

	* config/loongarch/loongarch.h (ASM_OUTPUT_ALIGN_WITH_NOP):
	Delete.

Co-authored-by: Chenghua Xu <xuchenghua@loongson.cn>

(cherry picked from commit b20c7ee066cb7d952fa193972e8bc6362c6e4063)
---
 gcc/config/loongarch/loongarch.h | 5 -----
 1 file changed, 5 deletions(-)

diff --git a/gcc/config/loongarch/loongarch.h b/gcc/config/loongarch/loongarch.h
index f34a7a604cc..8d08b84c8eb 100644
--- a/gcc/config/loongarch/loongarch.h
+++ b/gcc/config/loongarch/loongarch.h
@@ -978,11 +978,6 @@ typedef struct {
 
 #define ASM_OUTPUT_ALIGN(STREAM, LOG) fprintf (STREAM, "\t.align\t%d\n", (LOG))
 
-/* "nop" instruction 54525952 (andi $r0,$r0,0) is
-   used for padding.  */
-#define ASM_OUTPUT_ALIGN_WITH_NOP(STREAM, LOG) \
-  fprintf (STREAM, "\t.align\t%d,54525952,4\n", (LOG))
-
 /* This is how to output an assembler line to advance the location
    counter by SIZE bytes.  */
 
-- 
2.39.3


^ permalink raw reply	[flat|nested] 8+ messages in thread

* [PATCH v1 1/4] LoongArch: Delete macro definition ASM_OUTPUT_ALIGN_WITH_NOP.
  2024-02-21  3:08 Lulu Cheng
@ 2024-02-21  3:08 ` Lulu Cheng
  0 siblings, 0 replies; 8+ messages in thread
From: Lulu Cheng @ 2024-02-21  3:08 UTC (permalink / raw)
  To: gcc-patches; +Cc: xry111, i, xuchenghua, chenglulu

There are two reasons for removing this macro definition:
1. The default in the assembler is to use the nop instruction for filling.
2. For assembly directives: .align [abs-expr[, abs-expr[, abs-expr]]]
   The third expression it is the maximum number of bytes that should be
   skipped by this alignment directive.
   Therefore, it will affect the display of the specified alignment rules
   and affect the operating efficiency.

This modification relies on binutils commit 1fb3cdd87ec61715a5684925fb6d6a6cf53bb97c.
(Since the assembler will add nop based on the .align information when doing relax,
it will cause the conditional branch to go out of bounds during the assembly process.
This submission of binutils solves this problem.)

gcc/ChangeLog:

	* config/loongarch/loongarch.h (ASM_OUTPUT_ALIGN_WITH_NOP):
	Delete.

Co-authored-by: Chenghua Xu <xuchenghua@loongson.cn>

(cherry picked from commit b20c7ee066cb7d952fa193972e8bc6362c6e4063)
---
 gcc/config/loongarch/loongarch.h | 5 -----
 1 file changed, 5 deletions(-)

diff --git a/gcc/config/loongarch/loongarch.h b/gcc/config/loongarch/loongarch.h
index f34a7a604cc..8d08b84c8eb 100644
--- a/gcc/config/loongarch/loongarch.h
+++ b/gcc/config/loongarch/loongarch.h
@@ -978,11 +978,6 @@ typedef struct {
 
 #define ASM_OUTPUT_ALIGN(STREAM, LOG) fprintf (STREAM, "\t.align\t%d\n", (LOG))
 
-/* "nop" instruction 54525952 (andi $r0,$r0,0) is
-   used for padding.  */
-#define ASM_OUTPUT_ALIGN_WITH_NOP(STREAM, LOG) \
-  fprintf (STREAM, "\t.align\t%d,54525952,4\n", (LOG))
-
 /* This is how to output an assembler line to advance the location
    counter by SIZE bytes.  */
 
-- 
2.39.3


^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2024-02-22  3:26 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-02-21  3:04 [PATCH v1 0/4] Fix a series of problems caused by Lulu Cheng
2024-02-21  3:04 ` [PATCH v1 1/4] LoongArch: Delete macro definition ASM_OUTPUT_ALIGN_WITH_NOP Lulu Cheng
2024-02-21  3:04 ` [PATCH v1 2/4] LoongArch: Check whether binutils supports the relax function. If supported, explicit relocs are turned off by default Lulu Cheng
2024-02-21  3:04 ` [PATCH v1 3/4] LoongArch: Disable relaxation if the assembler don't support conditional branch relaxation [PR112330] Lulu Cheng
2024-02-21  3:04 ` [PATCH v1 4/4] LoongArch: Define HAVE_AS_TLS to 0 if it's undefined [PR112299] Lulu Cheng
2024-02-22  3:25 ` Re:[pushed] [PATCH v1 0/4] Fix a series of problems caused by chenglulu
2024-02-21  3:08 Lulu Cheng
2024-02-21  3:08 ` [PATCH v1 1/4] LoongArch: Delete macro definition ASM_OUTPUT_ALIGN_WITH_NOP Lulu Cheng
2024-02-21  3:10 [PATCH v1 0/4] Fix a series of problems caused by ASM_OUTPUT_ALIGN_WITH_NOP (release/gcc-12) Lulu Cheng
2024-02-21  3:10 ` [PATCH v1 1/4] LoongArch: Delete macro definition ASM_OUTPUT_ALIGN_WITH_NOP Lulu Cheng

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).