This is the mail archive of the
mailing list for the GCC project.
Re: Optimization of conditional access to globals: thread-unsafe?
From: skaller <firstname.lastname@example.org>
Date: Mon, 29 Oct 2007 14:21:10 +1100
> On Sun, 2007-10-28 at 18:37 -0700, David Miller wrote:
> > Even basic correct single-threaded UNIX programs are broken by these
> > speculative stores. If I use a conditional test to protect access to
> > memory mmap()'d with a read-only attribute, GCC's optimization will
> > cause write-protection exceptions.
> That is the programmers fault, they should have accessed the
> variable using a const. Failing to do so gives the compiler
> permission to write speculatively.
I do not agree with you.
It is perfectly legal to use read-only protection to implement
things like efficient garbage collection scans.
It's not even write exceptions, what about the pointer being
valid at all?
Memory accesses really are special. You can only execute them when
the program would have allowed them to occur, otherwise you risk
Do you really think that:
the_pointer_is_valid = func(potentially_bad_pointer);
should generate any memory accesses when 'the_pointer_is_valid'
evaluates to false?
And yet this is just another form of our original "threading" example:
It shows that memory accesses are a fundamental issue.
Only if you can prove that the program would access said memory with
said kind of access (read or write) can you legally speculate.
Happily it seems that for the cases where it helps code generation
substantially, this precondition is true.