Anyways, I've fixed the problem by setting TREE_SIDE_EFFECTS if the
transaction body has side effects.  Perhaps we should do this for
build_transaction_expr() as well?

I would think managing the transaction boundaries would always have
side-effects, no?  If that's correct, I'd set the flag unconditionally
in begin_transaction_stmt and build_transaction_expr.

Yeah, any transaction would have to go through the run-time at least once, so that is definitely a side-effect.

OK?
+       PR c++/56419
+       * semantics.c (begin_transaction_stmt): Set TREE_SIDE_EFFECTS.
+       (build_transaction_expr): Same.

diff --git a/gcc/cp/semantics.c b/gcc/cp/semantics.c
index 458ed26..589c821 100644
--- a/gcc/cp/semantics.c
+++ b/gcc/cp/semantics.c
@@ -5108,6 +5108,7 @@ begin_transaction_stmt (location_t loc, tree *pcompound, 
int flags)
                         "transactional memory support enabled")));
 
   TRANSACTION_EXPR_BODY (r) = push_stmt_list ();
+  TREE_SIDE_EFFECTS (r) = 1;
   return r;
 }
 
@@ -5157,6 +5158,7 @@ build_transaction_expr (location_t loc, tree expr, int 
flags, tree noex)
   ret = build1 (TRANSACTION_EXPR, TREE_TYPE (expr), expr);
   if (flags & TM_STMT_ATTR_RELAXED)
        TRANSACTION_EXPR_RELAXED (ret) = 1;
+  TREE_SIDE_EFFECTS (ret) = 1;
   SET_EXPR_LOCATION (ret, loc);
   return ret;
 }
diff --git a/gcc/testsuite/g++.dg/tm/pr56419.C 
b/gcc/testsuite/g++.dg/tm/pr56419.C
new file mode 100644
index 0000000..c9a33a8
--- /dev/null
+++ b/gcc/testsuite/g++.dg/tm/pr56419.C
@@ -0,0 +1,13 @@
+// { dg-do compile }
+// { dg-options "-fgnu-tm" }
+
+int x = 0;
+int inc_func(int i) {
+     for (int j = 0; j < i; ++j)
+     {
+         __transaction_atomic { x+=1; }
+     }
+     return 0;
+}
+
+// { dg-final { scan-assembler "ITM_commitTransaction" } }

Reply via email to