Bug 97309 - Improve documentation of -fallow-store-data-races
Summary: Improve documentation of -fallow-store-data-races
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: other (show other bugs)
Version: 11.0
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords: documentation
Depends on:
Blocks:
 
Reported: 2020-10-06 18:54 UTC by qinzhao
Modified: 2022-05-14 17:15 UTC (History)
3 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description qinzhao 2020-10-06 18:54:03 UTC
As of GCC 10, the former --param allow-store-data-races is now 
-fallow-store-data-races.  The default, in both cases, is not to allow them.   
  
For releases prior to GCC 10, the --param is documented as 

           allow-store-data-races 
               Allow optimizers to introduce new data races on stores.  Set 
to 1 to allow, otherwise to 0. 

The description for GCC 10 is simply: 

       -fallow-store-data-races 
           Allow the compiler to introduce new data races on stores. 
           Enabled at level -Ofast. 

There are three problems with this description. 
  
(1) The explanation is sparse - basically it just repeats the name of the 
switch.   
  
(2) It provides no context to explain the circumstances under which it may or 
may not be safe to use. 
  
(3) Because of the lack of clarity regarding safety, it may be questionable 
as to whether a SPEC CPU user is allowed to use -Ofast 
(http://www.spec.org/cpu2017/Docs/runrules.html#safe) 

Suggested improvement: 

Allow the compiler to perform optimizations that may introduce new data races 
on stores, without proving that the variable cannot be concurrently accessed 
by other threads. Does not affect optimization of local data. It is safe to 
use this option if it is known that global data will not be accessed by 
multiple threads. 

Examples of optimizations enabled by -fallow-store-data-races include 
hoisting or if-conversions that may cause a value that was already in memory 
to be re-written with that same value. Such re-writing is safe in a single 
threaded context but may be unsafe in a multi-threaded context. Note that on 
some processors, if-conversions may be  required in order to enable 
vectorization.
Comment 1 qinzhao 2020-10-06 20:39:46 UTC
proposed patch:

Subject: [PATCH] PR97309--improve documentation of -fallow-store-data-races

---
 gcc/doc/invoke.texi | 13 ++++++++++++-
 1 file changed, 12 insertions(+), 1 deletion(-)

diff --git a/gcc/doc/invoke.texi b/gcc/doc/invoke.texi
index 7c81d7f41bd..926ee1ff28e 100644
--- a/gcc/doc/invoke.texi
+++ b/gcc/doc/invoke.texi
@@ -11621,7 +11621,18 @@ Do not remove unused C++ allocations in dead code elimination.

 @item -fallow-store-data-races
 @opindex fallow-store-data-races
-Allow the compiler to introduce new data races on stores.
+Allow the compiler to perform optimizations that may introduce new data races
+on stores, without proving that the variable cannot be concurrently accessed
+by other threads.  Does not affect optimization of local data.  It is safe to
+use this option if it is known that global data will not be accessed by
+multiple threads.
+
+Examples of optimizations enabled by @option{-fallow-store-data-races} include
+hoisting or if-conversions that may cause a value that was already in memory
+to be re-written with that same value.  Such re-writing is safe in a single
+threaded context but may be unsafe in a multi-threaded context.  Note that on
+some processors, if-conversions may be required in order to enable
+vectorization.

 Enabled at level @option{-Ofast}.

--
2.11.0
Comment 2 CVS Commits 2020-10-08 15:01:22 UTC
The master branch has been updated by Qing Zhao <qinzhao@gcc.gnu.org>:

https://gcc.gnu.org/g:baf4750feaa6a5fa502ae7bc0b90f31640af6f47

commit r11-3732-gbaf4750feaa6a5fa502ae7bc0b90f31640af6f47
Author: qing zhao <qinzhao@gcc.gnu.org>
Date:   Thu Oct 8 17:01:07 2020 +0200

    Improve documentation of -fallow-store-data-races
    
    2020-10-08  John Henning  <john.henning@oracle.com>
    
    gcc/
    
            PR other/97309
            * doc/invoke.texi: Improve documentation of
            -fallow-store-data-races.
Comment 3 qinzhao 2020-10-08 15:03:49 UTC
fixed.
Comment 4 Jonathan Wakely 2022-05-12 08:55:57 UTC
I think this is still unclear.

What does "new data races" mean? Can it introduce races in code that had none previously? Or only add new ones to code that already has them?

Does this make -Ofast incompatible with multithreaded programs?

Does this only apply to non-atomic loads and stores?

If all accesses to a variable use atomic ops, does that mean it's immune from the unsafe optimizations enabled by this flag? If no, that makes -Ofast unusable in MT code. If yes, good, but why is the flag needed? If there are non-atomic accesses to a variable, we can already assume it's not concurrently accessed, because any such potentially concurrent conflicting action would already be a data race and UB. If we already have data races with UB, can't we just introduce more? Is this flag saying "allow the compiler to make existing UB even worse"? If not, what is it saying?
Comment 5 Jonathan Wakely 2022-05-12 08:58:22 UTC
(In reply to Jonathan Wakely from comment #4)
> If all accesses to a variable use atomic ops, does that mean it's immune
> from the unsafe optimizations enabled by this flag? If no, that makes -Ofast
> unusable in MT code. If yes, good, but why is the flag needed? If there are
> non-atomic accesses to a variable, we can already assume it's not
> concurrently accessed, because any such potentially concurrent conflicting
> action would already be a data race and UB. If we already have data races
> with UB, can't we just introduce more? Is this flag saying "allow the
> compiler to make existing UB even worse"? If not, what is it saying?

Or maybe this flag is relevant for languages that don't use the C and C++ memory models, where the rules are different?