Proposal: writing calls that can nevertheless be removed

Hello everyone,

I have a proposal to introduce a function attribute to solve a dead code elimination problem for singleton-get-like functions. Imagine the following code:

MyObj *Singleton = nullptr;

MyObj *getUnique() {

if (Singleton)

return Singleton;

Singleton =

}

Here getUnique is the only accessor of Singleton variable, which cannot be otherwise read or written.

This function is formally writing memory, but it has 2 important properties:

  • This function is idempotent, meaning that its result only depends on parameters (which is empty in this case);

  • If the result of the call is not used, it can safely be deleted without causing any problems.

In other words, if we have code like

for (int I = 0; I < 1000; I++) {

getUnique();

}

We can consider this loop dead and just delete it. Note that currently, because getUnique may write external memory, we cannot do so (even if we inline it and cleanse the remaining code, we’ll still have a store to Singleton).

In Java world, motivation for this is Object identity hash code. It requires to return the same value for the same object every time, it may write memory (usually it computes hash on 1st invocation basing on current address and stores it to the object). But if the hash code result is not used, it’s completely fine to skip this invocation.

In current function attributes, I could not find anything that could express “this is a mem-writing idempotent function which is OK to delete if trivially dead”. Optimization prospects for this are great: we can simply DCE them.

Do you think it’s work having such attribute?

Best regards,

Max

I assume you cannot make `Singleton` a "non-exposed" symbol, correct?
At the end of the day it is that global visibility/linkage that is problematic not necessarily the function.

~ Johannes

Right, but how is it a problem? Whoever sets this attribute wields all responsibility for its correctness, so it's used when appropriate.

--Max

Right, but how is it a problem? Whoever sets this attribute wields all responsibility for its correctness, so it's used when appropriate.

I'd avoid attributes that have "remote effects". Thus, any attribute
should go on the global, not the function. If you can annotate the
global with something that says "I will only be directly accessed by
the `getUnique` function (e.g., `only_accessed_directly_by(@getUnique)`)"
we should be able to optimize this properly. If we annotate the function
however, we would somehow iterate over users and then argue the users
attribute (which doesn't exist yet) has a remote effect on this global.
Does that make some sense?

~ Johannes

Doesn't speculatable + readonly cover this already?

Joerg

I think it's an orthogonal proposition. What I propose is, "it does change some external state, but we don't care what is this state and how it is changed, as long as idempotency and "can remove" properties stand". The state may compose of dozens of variables in theory, but it doesn't matter. You may think of this field as of a static field of some class, for example. Or set of fields. Or static hash map which may has its own (complex) state.

I don't think we should be discussing globals here at all. Singleton was just an example why we might need it. In our motivating case, the mutated state is internal state of a parameter object (specifically, its stored hash). My proposal wasn't about globals at all, there might not be any global at all involved in the process.

I don't think we should be discussing globals here at all. Singleton was just an example why we might need it. In our motivating case, the mutated state is internal state of a parameter object (specifically, its stored hash). My proposal wasn't about globals at all, there might not be any global at all involved in the process.

Let's step back then:

The function you shown, assuming that is actually an accurate model of the kind of functions you want to tag, has a write effect based on *some* state.
The function cannot be removed, even if the result is unused, because it may change *some* state.
The only way to remove all calls to the function (right now) is to either (A) show it never changes the state, (B) show nobody will observe the state change.

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?

Doesn't that basically mean: assume call sites are readonly but the callee is not (and do not propagate this from call sites to callee)?
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?

I guess if we reorganize the effect attributes (read/write/argmem/inacc/...) we could introduce a "call_only" version to express this.
So it would look like `call getUnique() effects(globalsonly, readonly | call_only)`, or something like that.

WDYT?

~ Johannes

As I said, we have that already in `speculatable`?

Joerg

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