Repository navigation
checkaction=context: Only generate template instances for codegen'ed functions - #13599
MoonlightSentinel wants to merge 3 commits into
Conversation
|
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 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 referencesYour 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 locallyIf you don't have a local development environment setup, you can use Digger to test this PR: dub run digger -- build "master + dmd#13599" |
b96d5a4 to
00a55ce
Compare
|
@MoonlightSentinel is this ready for review? |
| // 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(); |
There was a problem hiding this comment.
This is a rather crude heuristic. Cannot use needsCodegen here because it's too early in the semantic
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
-allinstwould take care of-checkaction=contextproblems. The price might be a bit high though, so disabling codegen culling of_d_assert_failinstantiations (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.
There was a problem hiding this comment.
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:
Lines 6297 to 6308 in b6985bb
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.
|
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.
62aa580 to
d07a44a
Compare
| { | ||
| assert(this.tempdecl); | ||
| auto mod = this.tempdecl.getModule(); | ||
| if (mod.hasName(Id.core, Id.internal, Id.dassert)) |
There was a problem hiding this comment.
Ah now I see what you meant. Yeah, the exact module some instance is appended to is defined by
Lines 7372 to 7388 in b6985bb
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).
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_failwould 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.