October 28, 2021

On Thursday, 28 October 2021 at 14:49:41 UTC, Paul Backus wrote:

>

According to the current language spec, the entire program has undefined behavior whether or not the result contains pointers. Walter has proposed to change this 1, but the proposal has not been accepted.

Some other change has been made to the spec after that PR was made. It currently reads:

>
  1. Implementation Defined: If a void initialized variable's value is used before it is set, its value is implementation defined.
void bad()
{
    int x = void;
    writeln(x);  // print implementation defined value
}
>
  1. Undefined Behavior: If a void initialized variable's value is used before it is set, and the value is a reference, pointer or an instance of a struct with an invariant, the behavior is undefined.
void muchWorse()
{
    char[] p = void;
    writeln(p);  // may result in apocalypse
}
October 28, 2021

On Thursday, 28 October 2021 at 15:18:54 UTC, Dukc wrote:

>

I think that, in addition to bugs, there is one hurdle in letting the compiler to fully optimise based on pure: how do we implement a function that manually frees an array?

That's exactly what the linked issue (22277) is about, so if you have any more insights on that, please post them there.

October 28, 2021

On Thursday, 28 October 2021 at 15:40:21 UTC, Dennis wrote:

>

On Thursday, 28 October 2021 at 15:18:54 UTC, Dukc wrote:

>

I think that, in addition to bugs, there is one hurdle in letting the compiler to fully optimise based on pure: how do we implement a function that manually frees an array?

That's exactly what the linked issue (22277) is about, so if you have any more insights on that, please post them there.

No that's slightly different. The linked issue deals with strongly pure functions. I'm dreaming about letting the compiler to optimise based on weak pure too - not currently allowed if I read the spec right, but could be without this issue (I think?).

October 28, 2021

On Thursday, 28 October 2021 at 15:18:54 UTC, Dukc wrote:

>

On Thursday, 28 October 2021 at 10:53:25 UTC, Dennis wrote:

>

On Wednesday, 27 October 2021 at 19:53:52 UTC, H. S. Teoh wrote:

>

[...]

The function needs to be nothrow and compiled with -O -release, because dmd needs to account for exceptions / errors, and even then the optimization shouldn't really be done. LDC doesn't do anything with pure for optimizations, but since it has cross module inlining, it doesn't need it.

I think that, in addition to bugs, there is one hurdle in letting the compiler to fully optimise based on pure: how do we implement a function that manually frees an array? If the signature is

pure nothrow @nogc void free(int[])

Not at all. free cannot, by its semantic, be pure (same for malloc). Trying to make free pure is a silly challenge.

October 28, 2021

On Thursday, 28 October 2021 at 16:36:30 UTC, Dukc wrote:

>

On Thursday, 28 October 2021 at 15:40:21 UTC, Dennis wrote:

>

On Thursday, 28 October 2021 at 15:18:54 UTC, Dukc wrote:

>

I think that, in addition to bugs, there is one hurdle in letting the compiler to fully optimise based on pure: how do we implement a function that manually frees an array?

That's exactly what the linked issue (22277) is about, so if you have any more insights on that, please post them there.

No that's slightly different. The linked issue deals with strongly pure functions. I'm dreaming about letting the compiler to optimise based on weak pure too - not currently allowed if I read the spec right, but could be without this issue (I think?).

Is this worth caring about? Do the backend a that actually matter not already perform this analysis as part of their IPA?

October 28, 2021

On Thursday, 28 October 2021 at 19:46:32 UTC, Patrick Schluter wrote:

>

Not at all. free cannot, by its semantic, be pure (same for malloc). Trying to make free pure is a silly challenge.

https://dlang.org/phobos/core_memory.html#.pureFree

October 28, 2021

On Thursday, 28 October 2021 at 21:48:09 UTC, max haughton wrote:

> >

No that's slightly different. The linked issue deals with strongly pure functions. I'm dreaming about letting the compiler to optimise based on weak pure too - not currently allowed if I read the spec right, but could be without this issue (I think?).

Is this worth caring about?

You decide. Without that, the weak pure can still be used inside strongly pure functions, but is otherwise useless for optimisation.

>

Do the backend a that actually matter not already perform this analysis as part of their IPA?

If the function body is available and not too complicated, probably. But with pure it's possible for a compiler to optimise based on the signature alone.

I don't personally care that much about having a super-optimising compiler, but I still wish that our attributes provide as much info as possible for any analysis program.

October 28, 2021
On Thu, Oct 28, 2021 at 10:48:10PM +0000, Dukc via Digitalmars-d wrote:
> On Thursday, 28 October 2021 at 19:46:32 UTC, Patrick Schluter wrote:
> > Not at all. free cannot, by its semantic, be pure (same for malloc). Trying to make free pure is a silly challenge.
> 
> https://dlang.org/phobos/core_memory.html#.pureFree

Such a function cannot be strongly pure; at best it can only be weakly pure.  IMO it's a bug that a function that takes an immutable pointer and frees it can be strongly pure. That breaks the "no visible change to the outside world" principle of strong purity and invalidates optimizations based on strong purity.

A weakly pure function rightly cannot be elided in an expression that calls it multiple times, because weak purity is not strong enough to guarantee no side-effects.


T

-- 
For every argument for something, there is always an equal and opposite argument against it. Debates don't give answers, only wounded or inflated egos.
October 28, 2021

On Thursday, 28 October 2021 at 23:05:04 UTC, Dukc wrote:

>

On Thursday, 28 October 2021 at 21:48:09 UTC, max haughton wrote:

> >

No that's slightly different. The linked issue deals with strongly pure functions. I'm dreaming about letting the compiler to optimise based on weak pure too - not currently allowed if I read the spec right, but could be without this issue (I think?).

Is this worth caring about?

You decide. Without that, the weak pure can still be used inside strongly pure functions, but is otherwise useless for optimisation.

>

Do the backend a that actually matter not already perform this analysis as part of their IPA?

If the function body is available and not too complicated, probably. But with pure it's possible for a compiler to optimise based on the signature alone.

I don't personally care that much about having a super-optimising compiler, but I still wish that our attributes provide as much info as possible for any analysis program.

I still think this analysis is going to be done anyway by either GCC or LLVM. Also, is elision even valid in the general case, what if the function is weak. It can't be elided until link-time in some rare but not impossible circumstances.

October 29, 2021

On Thursday, 28 October 2021 at 22:48:10 UTC, Dukc wrote:

>

On Thursday, 28 October 2021 at 19:46:32 UTC, Patrick Schluter wrote:

>

Not at all. free cannot, by its semantic, be pure (same for malloc). Trying to make free pure is a silly challenge.

https://dlang.org/phobos/core_memory.html#.pureFree

I don't understand it. It does not make any sense. pure functions are function that do not depend on global state for their result. Weak purity allows for some exception like for a print function which has global effects but these effects have no feedback and can ne ignored. This is not the case with allocation/free, which are, by defintion, dependend on a global state (even if only thread local).
Each call to malloc, by definition must return another value and/or can return a same value with other parameter. The result does not depend on ANYTHING in its scope.
pure allocation/free is a recipe for disaster.
My understanding of purity at l east.