LoongArch: Allow -mcmodel=extreme and model attribute with -mexplicit-relocs=auto

Message ID 20231207122014.19826-2-xry111@xry111.site
State Unresolved
Headers
Series LoongArch: Allow -mcmodel=extreme and model attribute with -mexplicit-relocs=auto |

Checks

Context Check Description
snail/gcc-patch-check warning Git am fail log

Commit Message

Xi Ruoyao Dec. 7, 2023, 12:20 p.m. UTC
  There seems no real reason to require -mexplicit-relocs=always for
-mcmodel=extreme or model attribute.  As the linker does not know how to
relax a 3-operand la.local or la.global pseudo instruction, just emit
explicit relocs for SYMBOL_PCREL64, and under TARGET_CMODEL_EXTREME also
SYMBOL_GOT_DISP.

gcc/ChangeLog:

	* config/loongarch/loongarch.cc (loongarch_explicit_relocs_p):
	Return true for SYMBOL_PCREL64.  Return true for SYMBOL_GOT_DISP
	if TARGET_CMODEL_EXTREME.
	(loongarch_split_symbol): Check for la_opt_explicit_relocs !=
	EXPLICIT_RELOCS_NONE instead of TARGET_EXPLICIT_RELOCS.
	(loongarch_print_operand_reloc): Likewise.
	(loongarch_option_override_internal): Likewise.
	(loongarch_handle_model_attribute): Likewise.

gcc/testsuite/ChangeLog:

	* gcc.target/loongarch/attr-model-3.c: New test.
	* gcc.target/loongarch/attr-model-4.c: New test.
	* gcc.target/loongarch/func-call-extreme-3.c: New test.
	* gcc.target/loongarch/func-call-extreme-4.c: New test.
---

Bootstrapped and regtested on loongarch64-linux-gnu.  Not sure if it's a
good idea to commit it now as we are in stage 3, maybe it's better to
defer this into GCC 15.  Any idea?

 gcc/config/loongarch/loongarch.cc             | 25 ++++++++++++-------
 .../gcc.target/loongarch/attr-model-3.c       |  6 +++++
 .../gcc.target/loongarch/attr-model-4.c       |  6 +++++
 .../loongarch/func-call-extreme-3.c           |  7 ++++++
 .../loongarch/func-call-extreme-4.c           |  7 ++++++
 5 files changed, 42 insertions(+), 9 deletions(-)
 create mode 100644 gcc/testsuite/gcc.target/loongarch/attr-model-3.c
 create mode 100644 gcc/testsuite/gcc.target/loongarch/attr-model-4.c
 create mode 100644 gcc/testsuite/gcc.target/loongarch/func-call-extreme-3.c
 create mode 100644 gcc/testsuite/gcc.target/loongarch/func-call-extreme-4.c
  

Comments

chenglulu Dec. 8, 2023, 2:04 a.m. UTC | #1
在 2023/12/7 下午8:20, Xi Ruoyao 写道:
> There seems no real reason to require -mexplicit-relocs=always for
> -mcmodel=extreme or model attribute.  As the linker does not know how to
> relax a 3-operand la.local or la.global pseudo instruction, just emit
> explicit relocs for SYMBOL_PCREL64, and under TARGET_CMODEL_EXTREME also
> SYMBOL_GOT_DISP.

I think there is no problem, HeJinyang is testing and if there is no 
problem with the

test play, you can merge in.


Thanks!
  
Jinyang He Dec. 8, 2023, 3:43 a.m. UTC | #2
On 2023-12-08 10:04, chenglulu wrote:

>
> 在 2023/12/7 下午8:20, Xi Ruoyao 写道:
>> There seems no real reason to require -mexplicit-relocs=always for
>> -mcmodel=extreme or model attribute.  As the linker does not know how to
>> relax a 3-operand la.local or la.global pseudo instruction, just emit
>> explicit relocs for SYMBOL_PCREL64, and under TARGET_CMODEL_EXTREME also
>> SYMBOL_GOT_DISP.
>
> I think there is no problem, HeJinyang is testing and if there is no 
> problem with the
>
> test play, you can merge in.
>
>
> Thanks!


Thanks! Now I can compile linux kernel with `-mexplicit-relocs=auto`,
and instructions is as expected. (kernel relax some pcalau12i+addi.d
pair to pcaddi and module still access percpu var by 64bits-la.pcrel)
  

Patch

diff --git a/gcc/config/loongarch/loongarch.cc b/gcc/config/loongarch/loongarch.cc
index 3545e66a10e..5274b513807 100644
--- a/gcc/config/loongarch/loongarch.cc
+++ b/gcc/config/loongarch/loongarch.cc
@@ -1958,9 +1958,16 @@  loongarch_explicit_relocs_p (enum loongarch_symbol_type type)
       case SYMBOL_TLS_LE:
       case SYMBOL_TLSGD:
       case SYMBOL_TLSLDM:
-	/* The linker don't know how to relax TLS accesses.  */
+      case SYMBOL_PCREL64:
+	/* The linker don't know how to relax TLS accesses or 64-bit
+	   pc-relative accesses.  */
 	return true;
       case SYMBOL_GOT_DISP:
+	/* The linker don't know how to relax GOT accesses in extreme
+	   code model.  */
+	if (TARGET_CMODEL_EXTREME)
+	  return true;
+
 	/* If we are performing LTO for a final link, and we have the
 	   linker plugin so we know the resolution of the symbols, then
 	   all GOT references are binding to external symbols or
@@ -3124,7 +3131,7 @@  loongarch_split_symbol (rtx temp, rtx addr, machine_mode mode, rtx *low_out)
 
   if (loongarch_symbol_extreme_p (symbol_type) && can_create_pseudo_p ())
     {
-      gcc_assert (TARGET_EXPLICIT_RELOCS);
+      gcc_assert (la_opt_explicit_relocs != EXPLICIT_RELOCS_NONE);
 
       temp1 = gen_reg_rtx (Pmode);
       emit_move_insn (temp1, gen_rtx_LO_SUM (Pmode, gen_rtx_REG (Pmode, 0),
@@ -5891,7 +5898,7 @@  loongarch_print_operand_reloc (FILE *file, rtx op, bool hi64_part,
     loongarch_classify_symbolic_expression (op);
 
   if (loongarch_symbol_extreme_p (symbol_type))
-    gcc_assert (TARGET_EXPLICIT_RELOCS);
+    gcc_assert (la_opt_explicit_relocs != EXPLICIT_RELOCS_NONE);
 
   switch (symbol_type)
     {
@@ -7493,9 +7500,9 @@  loongarch_option_override_internal (struct gcc_options *opts,
   switch (la_target.cmodel)
     {
       case CMODEL_EXTREME:
-	if (!TARGET_EXPLICIT_RELOCS)
-	  error ("code model %qs needs %s",
-		 "extreme", "-mexplicit-relocs=always");
+	if (la_opt_explicit_relocs == EXPLICIT_RELOCS_NONE)
+	  error ("code model %qs is not compatible with %s",
+		 "extreme", "-mexplicit-relocs=none");
 
 	if (opts->x_flag_plt)
 	  {
@@ -7796,11 +7803,11 @@  loongarch_handle_model_attribute (tree *node, tree name, tree arg, int,
 	  *no_add_attrs = true;
 	  return NULL_TREE;
 	}
-      if (!TARGET_EXPLICIT_RELOCS)
+      if (la_opt_explicit_relocs == EXPLICIT_RELOCS_NONE)
 	{
 	  error_at (DECL_SOURCE_LOCATION (decl),
-		    "%qE attribute requires %s", name,
-		    "-mexplicit-relocs=always");
+		    "%qE attribute is not compatible with %s", name,
+		    "-mexplicit-relocs=none");
 	  *no_add_attrs = true;
 	  return NULL_TREE;
 	}
diff --git a/gcc/testsuite/gcc.target/loongarch/attr-model-3.c b/gcc/testsuite/gcc.target/loongarch/attr-model-3.c
new file mode 100644
index 00000000000..5622d508678
--- /dev/null
+++ b/gcc/testsuite/gcc.target/loongarch/attr-model-3.c
@@ -0,0 +1,6 @@ 
+/* { dg-do compile } */
+/* { dg-options "-mexplicit-relocs=auto -mcmodel=normal -O2" } */
+/* { dg-final { scan-assembler-times "%pc64_hi12" 2 } } */
+
+#define ATTR_MODEL_TEST
+#include "attr-model-test.c"
diff --git a/gcc/testsuite/gcc.target/loongarch/attr-model-4.c b/gcc/testsuite/gcc.target/loongarch/attr-model-4.c
new file mode 100644
index 00000000000..482724bb974
--- /dev/null
+++ b/gcc/testsuite/gcc.target/loongarch/attr-model-4.c
@@ -0,0 +1,6 @@ 
+/* { dg-do compile } */
+/* { dg-options "-mexplicit-relocs=auto -mcmodel=extreme -O2" } */
+/* { dg-final { scan-assembler-times "%pc64_hi12" 3 } } */
+
+#define ATTR_MODEL_TEST
+#include "attr-model-test.c"
diff --git a/gcc/testsuite/gcc.target/loongarch/func-call-extreme-3.c b/gcc/testsuite/gcc.target/loongarch/func-call-extreme-3.c
new file mode 100644
index 00000000000..a4da44b4a3d
--- /dev/null
+++ b/gcc/testsuite/gcc.target/loongarch/func-call-extreme-3.c
@@ -0,0 +1,7 @@ 
+/* { dg-do compile } */
+/* { dg-options "-mabi=lp64d -O0 -fno-pic -fno-plt -mexplicit-relocs=auto -mcmodel=extreme" } */
+/* { dg-final { scan-assembler "test:.*pcalau12i.*%got_pc_hi20.*\n\taddi\.d.*%got_pc_lo12.*\n\tlu32i\.d.*%got64_pc_lo20.*\n\tlu52i\.d.*%got64_pc_hi12.*\n\tldx\.d" } } */
+/* { dg-final { scan-assembler "test1:.*pcalau12i.*%pc_hi20.*\n\taddi\.d.*%pc_lo12.*\n\tlu32i\.d.*%pc64_lo20.*\n\tlu52i\.d.*pc64_hi12.*\n\tadd\.d" } } */
+/* { dg-final { scan-assembler "test2:.*pcalau12i.*%pc_hi20.*\n\taddi\.d.*%pc_lo12.*\n\tlu32i\.d.*%pc64_lo20.*\n\tlu52i\.d.*pc64_hi12.*\n\tadd\.d" } } */
+
+#include "func-call-extreme-1.c"
diff --git a/gcc/testsuite/gcc.target/loongarch/func-call-extreme-4.c b/gcc/testsuite/gcc.target/loongarch/func-call-extreme-4.c
new file mode 100644
index 00000000000..16b00f4c5f2
--- /dev/null
+++ b/gcc/testsuite/gcc.target/loongarch/func-call-extreme-4.c
@@ -0,0 +1,7 @@ 
+/* { dg-do compile } */
+/* { dg-options "-mabi=lp64d -O0 -fpic -fno-plt -mexplicit-relocs=auto -mcmodel=extreme" } */
+/* { dg-final { scan-assembler "test:.*pcalau12i.*%got_pc_hi20.*\n\taddi\.d.*%got_pc_lo12.*\n\tlu32i\.d.*%got64_pc_lo20.*\n\tlu52i\.d.*%got64_pc_hi12.*\n\tldx\.d" } } */
+/* { dg-final { scan-assembler "test1:.*pcalau12i.*%got_pc_hi20.*\n\taddi\.d.*%got_pc_lo12.*\n\tlu32i\.d.*%got64_pc_lo20.*\n\tlu52i\.d.*%got64_pc_hi12.*\n\tldx\.d" } } */
+/* { dg-final { scan-assembler "test2:.*pcalau12i.*%pc_hi20.*\n\taddi\.d.*%pc_lo12.*\n\tlu32i\.d.*%pc64_lo20.*\n\tlu52i\.d.*pc64_hi12.*\n\tadd\.d" } } */
+
+#include "func-call-extreme-1.c"