public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [PATCH, C++, PR58282] Handle noexcept on transactions with -fno-exceptions
@ 2013-09-03 10:03 Tom de Vries
  2013-09-04 17:21 ` Jason Merrill
  0 siblings, 1 reply; 5+ messages in thread
From: Tom de Vries @ 2013-09-03 10:03 UTC (permalink / raw)
  To: Jason Merrill; +Cc: gcc-patches, Torvald Riegel

[-- Attachment #1: Type: text/plain, Size: 2002 bytes --]

Jason,

Consider this testcase reduced from g++.dg/tm/noexcept-1.C:
...
struct TrueFalse
{
  static constexpr bool v() { return true; }
};

int global;

template<typename T> int foo()
{
  __transaction_atomic noexcept(T::v()) { global += 1; }
  return __transaction_atomic noexcept(T::v()) (global + 2);
}

int f1()
{
  return foo<TrueFalse>();
}
...

It fails with a SIGSEGV when run with -fno-exceptions:
...
$ g++ noexcept-1.C -fno-exceptions -fgnu-tm -std=c++0x -S
noexcept-1.C: In function ‘int foo() [with T = TrueFalse]’:
noexcept-1.C:10:43: internal compiler error: Segmentation fault
   __transaction_atomic noexcept(T::v()) { global += 1; }
                                           ^
...

The noexcept(t::v()) operator evaluates to true, so the noexcept specifier on
the transaction is set to true. Then cp/semantics.c:build_transaction_expr
builds a MUST_NOT_THROW_EXPR to safe-guard the property at runtime:
...
  if (noex)
    {
      expr = build_must_not_throw_expr (expr, noex);
      SET_EXPR_LOCATION (expr, loc);
      TREE_SIDE_EFFECTS (expr) = 1;
    }
...

During gimplification of the MUST_NOT_THROW_EXPR in
cp/cp-gimplify.c:gimplify_must_not_throw_expr, we're trying to translate into a
try/catch:
...
  gimplify_and_add (body, &try_);
  mnt = gimple_build_eh_must_not_throw (terminate_node);
  gimple_seq_add_stmt_without_update (&catch_, mnt);
  mnt = gimple_build_try (try_, catch_, GIMPLE_TRY_CATCH);
...

However, terminate_node is NULL at that point because of -fno-exceptions, so in
gimple.c:gimple_build_eh_must_not_throw, this line causes a SIGSEGV because decl
== terminate_node == NULL:
...
  gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
...

This patch fixes the SIGSEGV by not generating a MUST_NOT_THROW_EXPR with
-fno-exceptions.

Bootstrapped and reg-tested on x86_64.

OK for trunk?

Thanks,
- Tom

2013-09-02  Tom de Vries  <tom@codesourcery.com>

	PR c++/58282
	* semantics.c (finish_transaction_stmt, build_transaction_expr): Handle
	flag_exceptions.

[-- Attachment #2: noexcept.patch --]
[-- Type: text/x-patch, Size: 761 bytes --]

diff --git a/gcc/cp/semantics.c b/gcc/cp/semantics.c
index ee3503c..c8b328c 100644
--- a/gcc/cp/semantics.c
+++ b/gcc/cp/semantics.c
@@ -5189,7 +5189,7 @@ finish_transaction_stmt (tree stmt, tree compound_stmt, int flags, tree noex)
 
   /* noexcept specifications are not allowed for function transactions.  */
   gcc_assert (!(noex && compound_stmt));
-  if (noex)
+  if (noex && flag_exceptions)
     {
       tree body = build_must_not_throw_expr (TRANSACTION_EXPR_BODY (stmt),
 					     noex);
@@ -5210,7 +5210,7 @@ tree
 build_transaction_expr (location_t loc, tree expr, int flags, tree noex)
 {
   tree ret;
-  if (noex)
+  if (noex && flag_exceptions)
     {
       expr = build_must_not_throw_expr (expr, noex);
       SET_EXPR_LOCATION (expr, loc);

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

* Re: [PATCH, C++, PR58282] Handle noexcept on transactions with -fno-exceptions
  2013-09-03 10:03 [PATCH, C++, PR58282] Handle noexcept on transactions with -fno-exceptions Tom de Vries
@ 2013-09-04 17:21 ` Jason Merrill
  2013-09-06  9:44   ` Tom de Vries
  0 siblings, 1 reply; 5+ messages in thread
From: Jason Merrill @ 2013-09-04 17:21 UTC (permalink / raw)
  To: Tom de Vries; +Cc: gcc-patches, Torvald Riegel

On 09/03/2013 06:03 AM, Tom de Vries wrote:
> 	* semantics.c (finish_transaction_stmt, build_transaction_expr): Handle
> 	flag_exceptions.

I'd rather handle this at a lower level, by making 
build_must_not_throw_expr return its argument if -fno-exceptions.

Jason

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

* Re: [PATCH, C++, PR58282] Handle noexcept on transactions with -fno-exceptions
  2013-09-04 17:21 ` Jason Merrill
@ 2013-09-06  9:44   ` Tom de Vries
  2013-09-07 17:27     ` Jason Merrill
  0 siblings, 1 reply; 5+ messages in thread
From: Tom de Vries @ 2013-09-06  9:44 UTC (permalink / raw)
  To: Jason Merrill; +Cc: gcc-patches, Torvald Riegel

[-- Attachment #1: Type: text/plain, Size: 637 bytes --]

On 04/09/13 19:21, Jason Merrill wrote:
> On 09/03/2013 06:03 AM, Tom de Vries wrote:
>> 	* semantics.c (finish_transaction_stmt, build_transaction_expr): Handle
>> 	flag_exceptions.
> 
> I'd rather handle this at a lower level, by making 
> build_must_not_throw_expr return its argument if -fno-exceptions.
> 

Jason,

This patch implements that way of fixing. In addition, it adds a test-case.

Bootstrapped and reg-tested on x86_64.

OK for trunk?

Thanks,
- Tom


2013-09-02  Tom de Vries  <tom@codesourcery.com>

	PR c++/58282
	* except.c (build_must_not_throw_expr): Handle
	flag_exceptions.

	* g++.dg/tm/noexcept-6.C: New test.


[-- Attachment #2: noexcept.patch --]
[-- Type: text/x-patch, Size: 1154 bytes --]

diff --git a/gcc/cp/except.c b/gcc/cp/except.c
index fbebcba..c76d944 100644
--- a/gcc/cp/except.c
+++ b/gcc/cp/except.c
@@ -374,6 +374,9 @@ build_must_not_throw_expr (tree body, tree cond)
 {
   tree type = body ? TREE_TYPE (body) : void_type_node;
 
+  if (!flag_exceptions)
+    return body;
+
   if (cond && !value_dependent_expression_p (cond))
     {
       cond = cxx_constant_value (cond);
diff --git a/gcc/testsuite/g++.dg/tm/noexcept-6.C b/gcc/testsuite/g++.dg/tm/noexcept-6.C
new file mode 100644
index 0000000..4391159
--- /dev/null
+++ b/gcc/testsuite/g++.dg/tm/noexcept-6.C
@@ -0,0 +1,23 @@
+// { dg-do compile }
+// { dg-options "-fno-exceptions -fgnu-tm -O -std=c++0x -fdump-tree-tmlower" }
+
+struct TrueFalse
+{
+  static constexpr bool v() { return true; }
+};
+
+int global;
+
+template<typename T> int foo()
+{
+  return __transaction_atomic noexcept(T::v()) (global + 1);
+}
+
+int f1()
+{
+  return foo<TrueFalse>();
+}
+
+/* { dg-final { scan-tree-dump-times "eh_must_not_throw" 0 "tmlower" } } */
+/* { dg-final { scan-tree-dump-times "__transaction_atomic" 1 "tmlower" } } */
+/* { dg-final { cleanup-tree-dump "tmlower" } } */

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

* Re: [PATCH, C++, PR58282] Handle noexcept on transactions with -fno-exceptions
  2013-09-06  9:44   ` Tom de Vries
@ 2013-09-07 17:27     ` Jason Merrill
  2013-10-25 14:30       ` Tom de Vries
  0 siblings, 1 reply; 5+ messages in thread
From: Jason Merrill @ 2013-09-07 17:27 UTC (permalink / raw)
  To: Tom de Vries; +Cc: gcc-patches, Torvald Riegel

OK.

Jason

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

* Re: [PATCH, C++, PR58282] Handle noexcept on transactions with -fno-exceptions
  2013-09-07 17:27     ` Jason Merrill
@ 2013-10-25 14:30       ` Tom de Vries
  0 siblings, 0 replies; 5+ messages in thread
From: Tom de Vries @ 2013-10-25 14:30 UTC (permalink / raw)
  To: Jason Merrill; +Cc: gcc-patches, Torvald Riegel

On 07/09/13 18:54, Jason Merrill wrote:
> OK.
> 

I've reproduced the same problem with the 4.7 and 4.8 branch, and checked that
applying the patch fixes the problem.

Committed to 4.7 and 4.8 branch as well.

Thanks,
- Tom

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

end of thread, other threads:[~2013-10-25 14:25 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2013-09-03 10:03 [PATCH, C++, PR58282] Handle noexcept on transactions with -fno-exceptions Tom de Vries
2013-09-04 17:21 ` Jason Merrill
2013-09-06  9:44   ` Tom de Vries
2013-09-07 17:27     ` Jason Merrill
2013-10-25 14:30       ` Tom de Vries

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).