]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
LoongArch: Make the code generation of the trap pattern configurable
authorXi Ruoyao <xry111@xry111.site>
Sun, 26 Oct 2025 05:20:20 +0000 (13:20 +0800)
committerLulu Cheng <chenglulu@loongson.cn>
Wed, 29 Oct 2025 02:53:26 +0000 (10:53 +0800)
In some applications (notably the Linux kernel), "break 0" is used as a
trap that a handler may be able to recover.  But in GCC the "trap"
pattern is meant to make the program rightfully die instead.

As [1] describes, sometimes it's vital to distinguish between the two
cases.  The kernel developers prefer "break 1" here, but in the
user-space it's better to trigger a SIGILL instead of SIGTRAP as the
latter is more likely used as a application-defined trap.

To support both cases, make the code generation configurable with a new
option.

[1]:https://lore.kernel.org/20250923061722.24457-1-yangtiezhu@loongson.cn

gcc/

* config/loongarch/genopts/loongarch.opt.in (-mbreak-code=):
New.
* config/loongarch/loongarch.opt: Regenerate.
* config/loongarch/loongarch.md (trap): Separate to a
define_insn and a define_expand which takes la_break_code.
* doc/invoke.texi (-mbreak-code=): Document.
* config/loongarch/loongarch.opt.urls: Regenerate.

gcc/testsuite

* gcc.target/loongarch/trap-default.c: New test.
* gcc.target/loongarch/trap-1.c: New test.

gcc/config/loongarch/genopts/loongarch.opt.in
gcc/config/loongarch/loongarch.md
gcc/config/loongarch/loongarch.opt
gcc/config/loongarch/loongarch.opt.urls
gcc/doc/invoke.texi
gcc/testsuite/gcc.target/loongarch/trap-1.c [new file with mode: 0644]
gcc/testsuite/gcc.target/loongarch/trap-default.c [new file with mode: 0644]

index 39c1545e5408b6394497ecacf29c31269367ec89..f0c089a928ec7f2b9d8abe343a98605128a22451 100644 (file)
@@ -205,6 +205,10 @@ mmax-inline-memcpy-size=
 Target Joined RejectNegative UInteger Var(la_max_inline_memcpy_size) Init(1024) Save
 -mmax-inline-memcpy-size=SIZE  Set the max size of memcpy to inline, default is 1024.
 
+mbreak-code=
+Target Joined UInteger Var(la_break_code) Init(-1) Save
+-mbreak-code=CODE      Use 'break CODE' for traps supposed to be unrecoverable, or an 'amswap.w' instruction leading to INE if CODE is out of range.
+
 Enum
 Name(explicit_relocs) Type(int)
 The code model option names for -mexplicit-relocs:
index be9a2351dd6e0a4d55a5713a6fa2e1033e1e4d6b..625f30ca26588f6a5aed998341e68b2d7bc1eedd 100644 (file)
 ;;  ....................
 ;;
 
-(define_insn "trap"
-  [(trap_if (const_int 1) (const_int 0))]
+(define_insn "*trap"
+  [(trap_if (const_int 1) (match_operand 0 "const_int_operand"))]
   ""
 {
-  return "break\t0";
+  return (const_uimm15_operand (operands[0], VOIDmode)
+         ? "break\t%0"
+         : "amswap.w\t$r0,$r1,$r0");
 }
   [(set_attr "type" "trap")])
 
+(define_expand "trap"
+  [(trap_if (const_int 1) (match_dup 0))]
+  ""
+{
+  operands[0] = GEN_INT (la_break_code);
+})
 
 \f
 ;;
index fbe61c0bf7c26bf03b2dddf971cdbb5a62254e63..628eabe8d591a433fe92e6749a73ba791ee9b02d 100644 (file)
@@ -213,6 +213,10 @@ mmax-inline-memcpy-size=
 Target Joined RejectNegative UInteger Var(la_max_inline_memcpy_size) Init(1024) Save
 -mmax-inline-memcpy-size=SIZE  Set the max size of memcpy to inline, default is 1024.
 
+mbreak-code=
+Target Joined UInteger Var(la_break_code) Init(-1) Save
+-mbreak-code=CODE      Use 'break CODE' for traps supposed to be unrecoverable, or an 'amswap.w' instruction leading to INE if CODE is out of range.
+
 Enum
 Name(explicit_relocs) Type(int)
 The code model option names for -mexplicit-relocs:
index 606a211f32233ecc29ea055c54cd1f53fe8476a0..c93f04683e1c8b7d55b66cc844ef65fa37213181 100644 (file)
@@ -48,6 +48,9 @@ UrlSuffix(gcc/LoongArch-Options.html#index-mstrict-align-1)
 mmax-inline-memcpy-size=
 UrlSuffix(gcc/LoongArch-Options.html#index-mmax-inline-memcpy-size)
 
+mbreak-code=
+UrlSuffix(gcc/LoongArch-Options.html#index-mbreak-code)
+
 mexplicit-relocs=
 UrlSuffix(gcc/LoongArch-Options.html#index-mexplicit-relocs-1)
 
index b40fc892fa0d9f60cf2fcb1d78f46239eca9d232..32b9c48f155c96abe3503eab9dbc82b99c0b6c78 100644 (file)
@@ -1097,7 +1097,7 @@ Objective-C and Objective-C++ Dialects}.
 -mfpu=@var{fpu-type} -msimd=@var{simd-type}
 -msoft-float -msingle-float -mdouble-float -mlsx -mno-lsx -mlasx -mno-lasx
 -mbranch-cost=@var{n} -maddr-reg-reg-cost=@var{n}  -mcheck-zero-division
--mno-check-zero-division
+-mno-check-zero-division -mbreak-code=@var{code}
 -mcond-move-int  -mno-cond-move-int
 -mcond-move-float  -mno-cond-move-float
 -memcpy  -mno-memcpy -mstrict-align -mno-strict-align -G @var{num}
@@ -28457,6 +28457,17 @@ Trap (do not trap) on integer division by zero.  The default is
 @option{-mcheck-zero-division} for @option{-O0} or @option{-Og}, and
 @option{-mno-check-zero-division} for other optimization levels.
 
+@opindex mbreak-code
+@item -mbreak-code=@var{code}
+Emit a @code{break} @var{code} instruction for irrecoverable traps
+from @code{__builtin_trap} or inserted by the compiler (for example
+an erroneous path isolated with
+@option{-fisolate-erroneous-paths-dereference}), or an
+@code{amswap.w $r0, $r1, $r0} instruction which will cause the hardware
+to trigger an Instruction Not-defined Exception if @var{code} is negative
+or greater than 32767.  The default is -1, meaning to use the
+@code{amswap.w} instruction.
+
 @opindex mcond-move-int
 @item -mcond-move-int
 @itemx -mno-cond-move-int
diff --git a/gcc/testsuite/gcc.target/loongarch/trap-1.c b/gcc/testsuite/gcc.target/loongarch/trap-1.c
new file mode 100644 (file)
index 0000000..8936f60
--- /dev/null
@@ -0,0 +1,9 @@
+/* { dg-do compile } */
+/* { dg-options "-O2 -w -fisolate-erroneous-paths-dereference -mbreak-code=1" } */
+/* { dg-final { scan-assembler "break\\t1" } } */
+
+int
+bug (void)
+{
+  return *(int *)0;
+}
diff --git a/gcc/testsuite/gcc.target/loongarch/trap-default.c b/gcc/testsuite/gcc.target/loongarch/trap-default.c
new file mode 100644 (file)
index 0000000..32948d4
--- /dev/null
@@ -0,0 +1,9 @@
+/* { dg-do compile } */
+/* { dg-options "-O2 -w -fisolate-erroneous-paths-dereference" } */
+/* { dg-final { scan-assembler "amswap\\.w\\t\\\$r0,\\\$r1,\\\$r0" } } */
+
+int
+bug (void)
+{
+  return *(int *)0;
+}