Skip to content

checkaction=context: Only generate template instances for codegen'ed functions - #13599

Draft
MoonlightSentinel wants to merge 3 commits into
dlang:masterfrom
MoonlightSentinel:context/non-root-modules
Draft

MoonlightSentinel wants to merge 3 commits into
dlang:masterfrom
MoonlightSentinel:context/non-root-modules

Conversation

@MoonlightSentinel

Copy link
Copy Markdown
Contributor

Dependencies might be compiled with different flags (-checkaction=D, -release, -check=assert=off) and hence not provide the required template instances. So generating a call to _d_assert_fail would cause undefined symbols because the template instance was assumed to emitted into the dependencies object file.

Omitting the message within functions that don't get codegen'd ensures that the template instances are codegen'd as well - as not other module references them. But this also comes with two drawbacks, those messages won't be available during CTFE and might be codegen' for multiple for object files.

@dlang-bot

Copy link
Copy Markdown

Thanks for your pull request and interest in making D better, @MoonlightSentinel! 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#13599"

@MoonlightSentinel
MoonlightSentinel force-pushed the context/non-root-modules branch 3 times, most recently from b96d5a4 to 00a55ce Compare February 8, 2022 02:07
@RazvanN7

Copy link
Copy Markdown
Contributor

@MoonlightSentinel is this ready for review?

Comment thread src/dmd/expressionsem.d Outdated
// seperately compiled modules may use other -checkaction configurations, i.e.
// -checkaction=[halt|C|D] / -release / -check=assert=off
// => Missing template instances expected from their object file
&& ((ti = sc.func.isInstantiated()) !is null ? ti.toParentLocal() : sc.func).getModule().isRoot();

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a rather crude heuristic. Cannot use needsCodegen here because it's too early in the semantic

@kinke kinke Feb 11, 2022 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IIUC, the idea is good - don't use -checkaction=context for non-root modules and templates instantiated in non-root, so that the compiler won't cull underlying _d_assert_fail instantiations later on during codegen.

But AFAIK, this won't work - the compiler relies on instantiations with equivalent params to behave identically, regardless of where they are instantiated. E.g., suppose some non-root module speculatively instantiates some Template!int with a nested assert(x == 1). The compiler analyzes it, skipping _d_assert_fail. Later, the compiler sees another Template!int instantiation from a root module - and will reuse the already analyzed first instantiation. So even if it does get codegen'd, it won't call _d_assert_fail.

@kinke kinke Feb 11, 2022 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think a working -allinst would take care of -checkaction=context problems. The price might be a bit high though, so disabling codegen culling of _d_assert_fail instantiations (incl. nested instantiations) would be another hacky option.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But AFAIK, this won't work - the compiler relies on instantiations with equivalent params to behave identically, regardless of where they are instantiated.

Right, this won't work (== produce unexpected results) for multiple instantiations in root and non-root modules.

I think a working -allinst would take care of -checkaction=context problems. The price might be a bit high though, so disabling codegen culling of _d_assert_fail instantiations (incl. nested instantiations) would be another hacky option.

Thought about that too, but -checkaction=context should work without additionally passing -allinst - it incurs unnecessary overhead for larger projects (and not to mention the possibility of additional linker errors).

I've added another commit that tries to emit all instances from template declarations found in core.internal.dassert (i.e. a local -allinst). The patch doesn't work yet because several helper templates (e.g. miniFormat) aren't consistently added to the members of core.internal.dassert.

@kinke kinke Feb 12, 2022 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The patch doesn't work yet because several helper templates (e.g. miniFormat) aren't consistently added to the members of core.internal.dassert.

No template instances are ever appended to some parent instance's members - they are all appended to one module each, no hierarchy. The only connection to the parent instance AFAIK is the tinst parent, which is checked in needsCodegen:

dmd/src/dmd/dtemplate.d

Lines 6297 to 6308 in b6985bb

// If the ancestor isn't speculative,
// 1. do codegen if the ancestor needs it
// 2. elide codegen if the ancestor doesn't need it (non-root instantiation of ancestor incl. subtree)
if (tinst)
{
const needsCodegen = tinst.needsCodegen(); // sets tinst.minst
if (tinst.minst) // not speculative
{
minst = tinst.minst; // cache result
return needsCodegen;
}
}

So returning true in needsCodegen for any _d_assert_fail instantiation with -checkaction=context should probably suffice, for its children too. Edit: Well maybe not any, but skipping the 'oh, I have a guaranteed non-root instance => do cull' part, and so possibly using the -allinst branch for the _d_assert_fail + -checkaction=context combo.

@MoonlightSentinel

Copy link
Copy Markdown
Contributor Author

Yes. But i would prefer some input from @kinke

Dependencies might be compiled with different flags (`-checkaction=D`,
`-release`, `-check=assert=off`) and hence not provide the required
template instances. So generating a call to `_d_assert_fail` would
cause undefined symbols because the template instance was assumed to
emitted into the dependencies object file.

Omitting the message within functions that don't get codegen'd ensures
that the template instances are codegen'd as well - as not other
module references them. But this also comes with two drawbacks, those
messages won't be available during CTFE and might be codegen' for multiple
for object files.
@MoonlightSentinel
MoonlightSentinel force-pushed the context/non-root-modules branch 2 times, most recently from 62aa580 to d07a44a Compare February 11, 2022 23:20
Comment thread src/dmd/dtemplate.d
{
assert(this.tempdecl);
auto mod = this.tempdecl.getModule();
if (mod.hasName(Id.core, Id.internal, Id.dassert))

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah now I see what you meant. Yeah, the exact module some instance is appended to is defined by

dmd/src/dmd/dtemplate.d

Lines 7372 to 7388 in b6985bb

Dsymbol enc = getStrictEnclosing(this);
// insert target is made stable by using the module
// where tempdecl is declared.
mi = (enc ? enc : tempdecl).getModule();
if (!mi.isRoot())
{
if (mi.importedFrom)
{
mi = mi.importedFrom;
assert(mi.isRoot());
}
else
{
// This can happen when using the frontend as a library.
// Append it to the non-root module.
}
}

and so AFAICT not really predictable and highly depending on the command-line (importedFrom).

I'd check the identifier of this.tempdecl instead, and then checking its declaring module - as codegen'ing it should lead to all children instances getting codegen too. As mentioned earlier, I'd try the allinst branch in that case.

Note that this needsCodegen() function is likely performance-sensitive and called many times (e.g., when traversing the tinst parents of children); setting up minst has been enough as cache for now (tinst/tnext chains are disconnected after the 1st invocation).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants