This is the mail archive of the gcc-patches@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[RFC] Add -fmap-abort-to-trap


Hi,

In gcc we map __builtin_trap to abort if there's no trap insn:
...
Built-in Function: void __builtin_trap (void)

This function causes the program to exit abnormally. GCC implements this function by using a target-dependent mechanism (such as intentionally executing an illegal instruction) or by calling abort. The mechanism used may vary from release to release so you should not rely on any particular implementation.
...

This patch adds an option -fmap-abort-to-trap that does the opposite: it maps a call to either abort or __builtin_abort to __builtin_trap.

[ The patch is incomplete: it lacks documentation of:
- the option fmap-abort-to-trap, and
- the effective target trap_insn
]

The patch is tested by:
- running the test-cases in the patch on x86_64
- running the libgomp/testsuite/libgomp.oacc-c-c++-common/abort-*
  testcases on x86_64 with nvptx accelerator

I'm using this patch atm for the purpose of debugging libgomp openacc testcases on nvptx. There are many test-cases where abort is the only function called in offloaded code, and this option changes the offload functions from non-leaf into leaf.

I'm not sure if there's a general usefulness to this patch, but I'm posting it here in case anybody finds it useful.

Thanks,
- Tom
Add -fmap-abort-to-trap

2017-06-21  Tom de Vries  <tom@codesourcery.com>

	* builtins.c (expand_builtin): Map abort and __builtin_abort to
	__builtin_trap for -fmap-abort-to-trap.
	* common.opt (fmap-abort-to-trap): New option.
	* config/nvptx/nvptx.c (nvptx_option_override): Set fmap-abort-to-trap
	by default.

	* lib/target-supports.exp (check_effective_target_trap_insn): New proc.
	* gcc.dg/fmap-abort-to-trap.inc: New include file.
	* gcc.dg/fmap-abort-to-trap.c: New test.
	* gcc.dg/fmap-abort-to-trap-2.c: New test.
	* gcc.dg/fmap-abort-to-trap-3.c: New test.
	* gcc.dg/fmap-abort-to-trap-4.c: New test.

---
 gcc/builtins.c                              |  6 ++++++
 gcc/common.opt                              |  4 ++++
 gcc/config/nvptx/nvptx.c                    |  3 +++
 gcc/testsuite/gcc.dg/fmap-abort-to-trap-2.c |  7 +++++++
 gcc/testsuite/gcc.dg/fmap-abort-to-trap-3.c |  6 ++++++
 gcc/testsuite/gcc.dg/fmap-abort-to-trap-4.c |  6 ++++++
 gcc/testsuite/gcc.dg/fmap-abort-to-trap.c   |  7 +++++++
 gcc/testsuite/gcc.dg/fmap-abort-to-trap.inc | 13 +++++++++++++
 gcc/testsuite/lib/target-supports.exp       |  8 ++++++++
 9 files changed, 60 insertions(+)

diff --git a/gcc/builtins.c b/gcc/builtins.c
index ce657bf..8db29fc 100644
--- a/gcc/builtins.c
+++ b/gcc/builtins.c
@@ -6513,6 +6513,8 @@ expand_builtin (tree exp, rtx target, rtx subtarget, machine_mode mode,
      set of builtins.  */
   if (!optimize
       && !called_as_built_in (fndecl)
+      && (fcode != BUILT_IN_ABORT
+	  || !flag_map_abort_to_trap)
       && fcode != BUILT_IN_FORK
       && fcode != BUILT_IN_EXECL
       && fcode != BUILT_IN_EXECV
@@ -6996,6 +6998,10 @@ expand_builtin (tree exp, rtx target, rtx subtarget, machine_mode mode,
 	}
       break;
 
+    case BUILT_IN_ABORT:
+      if (!flag_map_abort_to_trap)
+	break;
+      /* FALLTHRU.  */
     case BUILT_IN_TRAP:
       expand_builtin_trap ();
       return const0_rtx;
diff --git a/gcc/common.opt b/gcc/common.opt
index 4f9c3dc..77143fa 100644
--- a/gcc/common.opt
+++ b/gcc/common.opt
@@ -1793,6 +1793,10 @@ flto-report-wpa
 Common Report Var(flag_lto_report_wpa) Init(0)
 Report various link-time optimization statistics for WPA only.
 
+fmap-abort-to-trap
+Common Report Var(flag_map_abort_to_trap) Init(0)
+Map abort and __builtin_abort to __builtin_trap
+
 fmath-errno
 Common Report Var(flag_errno_math) Init(1) Optimization SetByCombined
 Set errno after built-in math functions.
diff --git a/gcc/config/nvptx/nvptx.c b/gcc/config/nvptx/nvptx.c
index daeec27..4db23b3 100644
--- a/gcc/config/nvptx/nvptx.c
+++ b/gcc/config/nvptx/nvptx.c
@@ -181,6 +181,9 @@ nvptx_option_override (void)
   /* Assumes that it will see only hard registers.  */
   flag_var_tracking = 0;
 
+  if (!global_options_set.x_flag_map_abort_to_trap)
+    flag_map_abort_to_trap = 1;
+
   if (nvptx_optimize < 0)
     nvptx_optimize = optimize > 0;
 
diff --git a/gcc/testsuite/gcc.dg/fmap-abort-to-trap-2.c b/gcc/testsuite/gcc.dg/fmap-abort-to-trap-2.c
new file mode 100644
index 0000000..d678d5c
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/fmap-abort-to-trap-2.c
@@ -0,0 +1,7 @@
+/* { dg-do compile } */
+/* { dg-options "-O2 -fmap-abort-to-trap -fdump-rtl-expand" } */
+
+#include "fmap-abort-to-trap.inc"
+
+/* { dg-final { scan-rtl-dump-not "call_insn" "expand" { target trap_insn } } } */
+/* { dg-final { scan-rtl-dump-times "call_insn" 2 "expand" { target { ! trap_insn } } } } */
diff --git a/gcc/testsuite/gcc.dg/fmap-abort-to-trap-3.c b/gcc/testsuite/gcc.dg/fmap-abort-to-trap-3.c
new file mode 100644
index 0000000..22e8889
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/fmap-abort-to-trap-3.c
@@ -0,0 +1,6 @@
+/* { dg-do compile } */
+/* { dg-options "-O0 -fno-map-abort-to-trap -fdump-rtl-expand" } */
+
+#include "fmap-abort-to-trap.inc"
+
+/* { dg-final { scan-rtl-dump-times "call_insn" 2 "expand" } } */
diff --git a/gcc/testsuite/gcc.dg/fmap-abort-to-trap-4.c b/gcc/testsuite/gcc.dg/fmap-abort-to-trap-4.c
new file mode 100644
index 0000000..2e2fd30
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/fmap-abort-to-trap-4.c
@@ -0,0 +1,6 @@
+/* { dg-do compile } */
+/* { dg-options "-O2 -fno-map-abort-to-trap -fdump-rtl-expand" } */
+
+#include "fmap-abort-to-trap.inc"
+
+/* { dg-final { scan-rtl-dump-times "call_insn" 2 "expand" } } */
diff --git a/gcc/testsuite/gcc.dg/fmap-abort-to-trap.c b/gcc/testsuite/gcc.dg/fmap-abort-to-trap.c
new file mode 100644
index 0000000..cfe4aad
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/fmap-abort-to-trap.c
@@ -0,0 +1,7 @@
+/* { dg-do compile } */
+/* { dg-options "-O0 -fmap-abort-to-trap -fdump-rtl-expand" } */
+
+#include "fmap-abort-to-trap.inc"
+
+/* { dg-final { scan-rtl-dump-not "call_insn" "expand" { target trap_insn } } } */
+/* { dg-final { scan-rtl-dump-times "call_insn" 2 "expand" { target { ! trap_insn } } } } */
diff --git a/gcc/testsuite/gcc.dg/fmap-abort-to-trap.inc b/gcc/testsuite/gcc.dg/fmap-abort-to-trap.inc
new file mode 100644
index 0000000..837dd02
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/fmap-abort-to-trap.inc
@@ -0,0 +1,13 @@
+void
+f (void)
+{
+  __builtin_abort ();
+}
+
+extern void abort (void);
+
+void
+g (void)
+{
+  abort ();
+}
diff --git a/gcc/testsuite/lib/target-supports.exp b/gcc/testsuite/lib/target-supports.exp
index 502986e..2a98363 100644
--- a/gcc/testsuite/lib/target-supports.exp
+++ b/gcc/testsuite/lib/target-supports.exp
@@ -861,6 +861,14 @@ proc check_effective_target_tls_emulated {} {
     }]
 }
 
+# Return 1 if __builtin_trap generates a trap insn.
+
+proc check_effective_target_trap_insn {} {
+    return [check_no_messages_and_pattern trap_insn "!abort" assembly {
+	void f (void) { __builtin_trap (); }
+    }]
+}
+
 # Return 1 if TLS executables can run correctly, 0 otherwise.
 
 proc check_effective_target_tls_runtime {} {


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]