Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

POC: automatically move scope array literals to the stack #16897

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

dkorpel
Copy link
Contributor

@dkorpel dkorpel commented Sep 27, 2024

See #16888 (comment)

Just a proof of concept, I don't think this is worth merging at this stage. Maybe under -preview=dip1000 only.

@dlang-bot
Copy link
Contributor

Thanks for your pull request and interest in making D better, @dkorpel! We are looking forward to reviewing it, and you should be hearing from a maintainer soon.
Please verify that your PR follows this checklist:

  • My PR is fully covered with tests (you can see the coverage diff by visiting the details link of the codecov check)
  • My PR is as minimal as possible (smaller, focused PRs are easier to review than big ones)
  • I have provided a detailed rationale explaining my changes
  • New or modified functions have Ddoc comments (with Params: and Returns:)

Please see CONTRIBUTING.md for more information.


If you have addressed all reviews or aren't sure how to proceed, don't hesitate to ping us with a simple comment.

Bugzilla references

Your PR doesn't reference any Bugzilla issue.

If your PR contains non-trivial changes, please reference a Bugzilla issue or create a manual changelog.

Testing this PR locally

If you don't have a local development environment setup, you can use Digger to test this PR:

dub run digger -- build "master + dmd#16897"

@Geod24
Copy link
Member

Geod24 commented Sep 27, 2024

I assume you will make scope foo = [...] @nogc ?

@dkorpel
Copy link
Contributor Author

dkorpel commented Sep 27, 2024

I assume you will make scope foo = [...] @nogc ?

If this is actually worth pursuing, yes. It's non-trivial because currently, @nogc is eagerly checked on each expression, while in this case, an array literal expression can only be checked at the end of the function.

@ryuukk
Copy link
Contributor

ryuukk commented Sep 28, 2024

This doesn't work for me, however this does:

else if (auto ale = ex.isArrayLiteralExp())
{
// or an array literal assigned to a `scope` variable
if (sc.useDIP1000 == FeatureState.enabled
&& !dsym.type.nextOf().needsDestruction())
ale.onstack = true;
}
}

                        else if (auto ale = ex.isArrayLiteralExp())
                        {
                            // or an array literal assigned to a `scope` variable
-                            if (sc.useDIP1000 == FeatureState.enabled
+                            if ((sc.useDIP1000 == FeatureState.enabled || global.params.betterC)
                                && !dsym.type.nextOf().needsDestruction())
                                ale.onstack = true;
                        }

@ryuukk
Copy link
Contributor

ryuukk commented Sep 28, 2024

My patch works with your tests too, please use it instead, more efficient, and let's merge

@thewilsonator thewilsonator added Needs Changelog A changelog entry needs to be added to /changelog Needs Spec PR A PR updating the language specification needs to be submitted to dlang.org labels Sep 28, 2024
@dkorpel
Copy link
Contributor Author

dkorpel commented Sep 28, 2024

That patch could introduce dangling pointers in @safe code. -preview=dip1000 is required to verify the stack-allocated array literal doesn't escape.

-betterC simply disables language features that require druntime, it does not negate @safe or change language semantics in other ways, so checking for global.params.betterC likely isn't the right answer here.

@ryuukk
Copy link
Contributor

ryuukk commented Sep 28, 2024

From what i see, that DIP is 8 years old

I'm not gonna rely on a rotten DIP and i don't use attributes

new patch:

                        else if (auto ale = ex.isArrayLiteralExp())
                        {
                            // or an array literal assigned to a `scope` variable
-                            if (sc.useDIP1000 == FeatureState.enabled
+                            if ((sc.useDIP1000 == FeatureState.enabled || (global.params.betterC && !sc.func.isSafe()))
                                && !dsym.type.nextOf().needsDestruction())
                                ale.onstack = true;
                        }

Let's merge

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Needs Changelog A changelog entry needs to be added to /changelog Needs Spec PR A PR updating the language specification needs to be submitted to dlang.org
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants