Translate _d_arrayappend{T,cTX} to templates#13495
Conversation
|
Thanks for your pull request and interest in making D better, @teodutu! 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#13495" |
src/dmd/e2ir.d
Outdated
| if (auto ve = ce.econd.isVarExp) | ||
| if (ve.var.ident == Id.ctfe) | ||
| { | ||
| elem *e = toElem(ce.e2, irs); | ||
| if (irs.params.cov && ce.e2.loc.linnum) | ||
| e = el_combine(incUsageElem(irs, ce.e2.loc), e); | ||
| if (tybasic(e.Ety) == TYstruct) | ||
| e.ET = Type_toCtype(ce.e2.type); | ||
| elem_setLoc(e, ce.loc); | ||
| result = e; | ||
| return; | ||
| } |
There was a problem hiding this comment.
This doesn't look relevant to the change, and the backend should already optimize this case.
There was a problem hiding this comment.
src/dmd/s2ir.d
Outdated
| * This is necessary for expressions such as `a ~= b`, where `b` is not | ||
| * an array. In expressionsem.d, they are lowered to: | ||
| * `__ctfe ? a ~= b : _d_arrayappendcTX(a, 1), a[$ - 1] = b, a;` | ||
| * If CTFE hasn't already interpreted `a ~= b`, then this branch must be | ||
| * removed, as e2ir.d no longer supports the runtime hook to | ||
| * `_d_arrayappendcTX`. |
There was a problem hiding this comment.
Can't you just remove it during semantic?
There was a problem hiding this comment.
I'm not sure I understand what you mean. I create the lowering during semantic3 and the lowering is needed for CTFE. I don't see where in semantic i should remove it.
There was a problem hiding this comment.
Sounds like something has gone wrong to me. Lowering should only occur after CTFE has had a chance to reduce the code, not before.
There was a problem hiding this comment.
The problem is that when the compiler analyzes the body of a function it cannot know if the function is going to be interpreted or not in the future. Since we cannot interpret _d_arrayappend this trick with __ctfe is used.
There was a problem hiding this comment.
Performing the lowering in e2ir.d, where it previously was, has the following disadvantages:
- Maintainability decreases as LDC and GDC will have to implement the lowering themselves.
- Readability also decreases, because the code in e2ir.d is less expressive than that in expressionsem.d.
While it is true that lowering the CatAssign as early as expressionsem.d makes the logic more complex, requiring that e2ir.d and s2ir.d omit the true branch of the __ctfe cond, I believe this approach is preferable because it elegantly solves the problems above, while also clearly separating the expression intended for CTFE from those meant for code generation.
There was a problem hiding this comment.
I believe this approach is preferable because it elegantly solves the problems above, while also clearly separating the expression intended for CTFE from those meant for code generation.
No it doesn't, you're adding semantic logic to the code generator. The code generator should at best be just a trivial translation from one representation to another.
What I see is that you've added some new assert(0) branches in the code generator. This means that the front-end should never leak these AST constructs to the back-end, and yet, that is exactly what the lowering does, so the code generator is becomes even more complex than it needs to be by suddenly having a requirement to filter AST nodes that should have not have reached it in the first place.
There was a problem hiding this comment.
Hmm, maybe we can make the hook interpret-able. That would get rid of the filtering code in e2ir.d and would still enable us to perform the lowering in the frontend. Is that an acceptable solution @ibuclaw ?
I don't think that is a correct lowering. |
First, thanks for pointing out to me that I had forgotten the __ctfe ? a ~= b : _d_arrayappendcTX(a, 1), a[a.length - 1] = b, a;I've also updated the PR's description. Now to answer your question, the lowering creates a [1] Line 10292 in 92d4630 |
|
Could you then reword the description please? Because |
|
Done @radcapricorn. Is the description clearer now? I'd prefer to keep the code itself as it is, because that's more or less what you get if you print |
src/dmd/dinterpret.d
Outdated
| else if (fd.ident == Id._d_arrayappendT) | ||
| assert(0, "CTFE cannot interpret _d_arrayappendT!"); | ||
| else if (fd.ident == Id._d_arrayappendcTX) | ||
| assert(0, "CTFE cannot interpret _d_arrayappendcTX!"); |
There was a problem hiding this comment.
Having another look over, if I understand this function right, you should instead implement these two paths because CTFE can interpret arrayappend operations. Then there's no need for the (__ctfe) ? a : b set-up you've done in semantic, just generate the lowering to template directly.
There was a problem hiding this comment.
If the lowering reaches CTFE, this error [1] is triggered because of the mixin in the hook's body [2]. That's why those asserts are here and that's why I use __ctfe ? ....
[1]
Lines 4967 to 4972 in b14861d
[2] https://github.com/dlang/druntime/blob/cc101972b1f6d3047ca7b54cbf4aee11ac499b75/src/core/internal/array/appending.d#L42
92d4630 to
aa50741
Compare
|
In order to pass the tests, this PR requires an updated implementation of |
aa50741 to
ce4be68
Compare
6f0bf28 to
98439b5
Compare
dkorpel
left a comment
There was a problem hiding this comment.
I've put a few nits, but my bigger issue is with the size. The added code is substantially more complex than the removed code. Especially seeing how the hook functions get rewritten back to CatAssignExp for CTFE looks clumsy. I feel we can do better than this, but unfortunately I can't give an insightful direction without delving into the project myself, so at this moment I'm abstaining.
I'm curious about the following things:
- How many more hooks are there to be translated to templates?
- Do those require PRs of similar complexity?
- Are there 'clean up' PRs planned after this?
Maybe this was all planned out somewhere, but I can't find the document. (I'm looking at dlang/project-ideas#25)
|
@dkorpel There really isn't anything too complex happening here. The extra lines that you are seeing are added because:
In addition, some code from previous commits is obsolete and therefore will be deleted so the size will get smaller. The idea here is that we might add a bit of complexity but, in general, it will worth it. |
@teodutu can provide the exact count, but I thing there are still somewhere between 10-15 hooks that need to be translated.
No, we expect that this one is the most complex one. However, we have no way of knowing until we look at a given hook.
What do you mean? Anyone is welcome to improve the code is possible. |
Can't you interpret the hook? There's an explicit check, but it asserts: else if (fd.ident == Id._d_arrayappendcTX)
assert(0, "CTFE cannot interpret _d_arrayappendcTX!"); |
|
No, the hook ends up calling C code. |
|
You need a rebase to latest master to get rid of the FreeBSD failure. Also, mind the other nits in my review. |
Signed-off-by: Dan Printzell <xwildn00bx@gmail.com>
Signed-off-by: Dan Printzell <xwildn00bx@gmail.com>
Signed-off-by: Dan Printzell <xwildn00bx@gmail.com>
Signed-off-by: Dan Printzell <xwildn00bx@gmail.com>
Signed-off-by: Dan Printzell <xwildn00bx@gmail.com>
Signed-off-by: Dan Printzell <xwildn00bx@gmail.com>
1296991 to
3323ec3
Compare
dkorpel
left a comment
There was a problem hiding this comment.
Are there no existing tests for global.params.tracegc?
|
@dkorpel There are tests for |
Lower `a ~= b` to: - `_d_arrayappendT(a, b)` when `b` is an array - `_d_arrayappendcTX(a, 1), a[a.length - 1] = b, a` when `b` is an element Signed-off-by: Teodor Dutu <teodor.dutu@gmail.com>
3323ec3 to
9f7cea7
Compare
Good, so the codecov warnings are false positives. |
* Lower array appending to _d_arrayappend{T,cTX}
Signed-off-by: Dan Printzell <xwildn00bx@gmail.com>
* Don't allow _d_arrayappend{T,cTX} to be called in CTFE
Signed-off-by: Dan Printzell <xwildn00bx@gmail.com>
* Implement check for _d_arrayappend{T,cTX} in nogc.d
Signed-off-by: Dan Printzell <xwildn00bx@gmail.com>
* e2ir.d: If symbol is already added to symtab, don't call symbol_add
Signed-off-by: Dan Printzell <xwildn00bx@gmail.com>
* e2ir.d: Only compile `e2` of CondExp if `econd` is `__ctfe`
Signed-off-by: Dan Printzell <xwildn00bx@gmail.com>
* Remove backend code for _d_arrayappend{T,cTX}
Signed-off-by: Dan Printzell <xwildn00bx@gmail.com>
* Change lowering of `CatAssignExp`
Lower `a ~= b` to:
- `_d_arrayappendT(a, b)` when `b` is an array
- `_d_arrayappendcTX(a, 1), a[a.length - 1] = b, a` when `b` is an
element
Signed-off-by: Teodor Dutu <teodor.dutu@gmail.com>
Co-authored-by: Dan Printzell <xwildn00bx@gmail.com>
* Lower array appending to _d_arrayappend{T,cTX}
Signed-off-by: Dan Printzell <xwildn00bx@gmail.com>
* Don't allow _d_arrayappend{T,cTX} to be called in CTFE
Signed-off-by: Dan Printzell <xwildn00bx@gmail.com>
* Implement check for _d_arrayappend{T,cTX} in nogc.d
Signed-off-by: Dan Printzell <xwildn00bx@gmail.com>
* e2ir.d: If symbol is already added to symtab, don't call symbol_add
Signed-off-by: Dan Printzell <xwildn00bx@gmail.com>
* e2ir.d: Only compile `e2` of CondExp if `econd` is `__ctfe`
Signed-off-by: Dan Printzell <xwildn00bx@gmail.com>
* Remove backend code for _d_arrayappend{T,cTX}
Signed-off-by: Dan Printzell <xwildn00bx@gmail.com>
* Change lowering of `CatAssignExp`
Lower `a ~= b` to:
- `_d_arrayappendT(a, b)` when `b` is an array
- `_d_arrayappendcTX(a, 1), a[a.length - 1] = b, a` when `b` is an
element
Signed-off-by: Teodor Dutu <teodor.dutu@gmail.com>
Co-authored-by: Dan Printzell <xwildn00bx@gmail.com>
These hooks have been converted to templates and their lowerings changed to use those templates: - dlang/dmd#13116 - dlang/dmd#13495 - dlang/dmd#13398 Signed-off-by: Teodor Dutu <teodor.dutu@gmail.com>
These hooks have been converted to templates and their lowerings changed to use those templates: - dlang/dmd#13116 - dlang/dmd#13495 - dlang/dmd#13398 Signed-off-by: Teodor Dutu <teodor.dutu@gmail.com>
These hooks have been converted to templates and their lowerings changed to use those templates: - dlang/dmd#13116 - dlang/dmd#13495 - dlang/dmd#13398 Signed-off-by: Teodor Dutu <teodor.dutu@gmail.com>
These hooks have been converted to templates and their lowerings changed to use those templates: - dlang/dmd#13116 - dlang/dmd#13495 - dlang/dmd#13398 Signed-off-by: Teodor Dutu <teodor.dutu@gmail.com>
|
This pull request introduced a regression: |
…RRAYAPPENDCTX, TRACEARRAYAPPENDT and TRACEARRAYAPPENDCTX
* Remove RTLSYM for Translation PR #15819: Removed NEWARRAYMITX, NEWARRAYMITX, TRACENEWARRAYMTX and TRACENEWARRAYMITX * Remove RTLSYM for Translation PR #15299: Removed NEWARRAYT, NEWARRAYIT, TRACENEWARRAYT and TRACENEWARRAYIT * Remove RTLSYM for Translation PR #14837: Removed NEWCLASS, TRACENEWCLASS * Remove RTLSYM for Translation PR #14664: Removed NEWITEMT, NEWITEMIT, TRACENEWITEMT and TRACENEWITEMIT * Remove RTLSYM for Translation PR #14550: Removed ARRAYCATNTX, ARRAYCATT, TRACEARRAYCATNTX and TRACEARRAYCATT * Remove RTLSYM for Translation PR #14382: Removed ARRAYSETASSIGN * Remove RTLSYM for Translation PR #14310: Removed ARRAYASSIGN * Remove RTLSYM for Translation PR #13495: Removed ARRAYAPPENDT, ARRAYAPPENDCTX, TRACEARRAYAPPENDT and TRACEARRAYAPPENDCTX
This PR lowers expressions such as
a ~= bto:when
bis a single element, orwhen
bis an array. These lowerings are now moved to the compiler's frontend, as opposed to e2ir.d.This revives #9982.