Boost logo

Boost :

From: Niall Douglas (s_sourceforge_at_[hidden])
Date: 2022-02-18 21:10:23


On 18/02/2022 20:36, Peter Dimov via Boost wrote:
>> Niall Douglas wrote:
>>> Surely laundering the address each time is rather bad for optimisation
>>> and codegen?
>>>
>>> I mean, effectively launder invokes "escaped" during escape analysis
 right?
>>
>> I honestly have no idea.
>>
>> However, looking at https://godbolt.org/z/oKj5c5x7v, it doesn't seem so.

Your examples aren't right - launder is for telling the compiler that an
escaped value must be assumed to have changed, even if the compiler is
allowed to assume it has not. I modified your example thusly:
https://godbolt.org/z/oosdK6GMo

That's not clear, so I made a clearer example of when launder is
actually needed: https://godbolt.org/z/na78G77jY.

You can see for test1 that even though ext() is called and it could
modify the value returned by foo() because foo() is extern, the compiler
is allowed by the standard to assume that the value won't change,
becuase it is const and const values have special rules about immutability.

In test2 we explicitly tell the compiler it can't assume immutability
using launder, and it correctly reloads the const value after the ext()
call.

I think clang's implementation of launder is bugged because it doesn't
inhibit compile time constant folding. I vaguely remember submitting
that as a bug yonks back, and Richard Smith telling me yes it was a
known issue and a timely fix would be unlikely for various reasons.

MSVC gets launder right, same as GCC.

Niall


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk