[llvm-dev] Proposal: writing calls that can nevertheless be removed

Joerg Sonnenberger via llvm-dev llvm-dev at lists.llvm.org
Mon Nov 8 07:43:03 PST 2021


On Mon, Nov 08, 2021 at 05:03:19AM +0000, Max Kazantsev via llvm-dev wrote:
> > I understand your attribute now as a way to express: "assume nobody will depend on the fact that there would have been a state change when you determine if a call is needed".
> Does that somewhat match with your initial idea?
> 
> Yes, it does.
> 
> > So `readonly` on the call sites would give you the right effect but if we propagate it, or the inliner would make use of it, you would have a problem, right?
> 
> The only problem here is, according to langref:
> > If a readonly function writes memory visible to the program, or has other side-effects, the behavior is undefined. If a function writes to a readonly pointer argument, the behavior is undefined.
> 
> Formally, this memory forming the state is visible to this program. At
> least it is visible to this very function. So we cannot use readonly
> here because it is UB. But otherwise yes, all optimizations effects
> from the proposed attribute would be the same as if it was readonly
> function.

Allocating memory and storing it in a function-local (static) variable
is not considered a side-effect.

Joerg


More information about the llvm-dev mailing list