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

Translate _d_newclass to a template #14837

Merged
merged 1 commit into from
Jan 27, 2023

Conversation

teodutu
Copy link
Member

@teodutu teodutu commented Jan 18, 2023

This PR makes the following changes:

  • Add template _d_newclassT to druntime.src.core.lifetime.d
  • Replace lowering of new C() to _d_newclassT!C()
  • Add lowering member to NewExp. This field stores the above lowering to be used by e2ir.d
  • Keep the old _d_newclass hook because it's used by TypeInfo_Class.create()
  • Add dummy _d_newclassT hook to tests that redefine the object module
  • Remove new MinHeap!(TestType)() from fail308.d. Otherwise the errror was changed and printed the local path to druntime
  • Account for the GC.malloc() called by the template hook in the -profile=gc tests

@teodutu teodutu requested a review from ibuclaw as a code owner January 18, 2023 18:22
@dlang-bot
Copy link
Contributor

Thanks for your pull request, @teodutu!

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#14837"

compiler/src/dmd/expression.d Show resolved Hide resolved
@teodutu
Copy link
Member Author

teodutu commented Jan 18, 2023

fail_compilation/shared.d fails because of https://issues.dlang.org/show_bug.cgi?id=23639. The bug is currently being fixed by #14836.

@@ -3576,6 +3577,8 @@ extern (C++) final class NewExp : Expression
bool onstack; // allocate on stack
bool thrownew; // this NewExp is the expression of a ThrowStatement

Expression lowering; // lowered druntime hook: `_d_newclass`
Copy link
Member

Choose a reason for hiding this comment

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

Is this necessary? If yes keep it but keep an eye on adding unnecessary fields as dmd's classes are absolutely enormous

Copy link
Member Author

@teodutu teodutu Jan 18, 2023

Choose a reason for hiding this comment

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

I think it's necessary. I need an expression where to store the lowering, so I can then pass it to the backend in e2ir.d. This expression can either be a field in NewExp or the result set by visit(NewExp) (instead of the original expression) in expressionsemantic.d. The latter option is difficult because it requires me to initialise the context pointer in the semantic phase. I tried doing this in the past and it's ugly. I'd rather use the existing machinery that handles context pointers in e2ir.d instead. Furthermore, I received feedback from @ibuclaw in the past that eliding the original expressions might hinder some optimisations in GDC and LDC.

Copy link
Contributor

@RazvanN7 RazvanN7 left a comment

Choose a reason for hiding this comment

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

Why aren't we deleting the old hook?

@teodutu
Copy link
Member Author

teodutu commented Jan 20, 2023

Why aren't we deleting the old hook?

Because it's used by TypeInfo_Class.create(), which is publicly exported.

@RazvanN7
Copy link
Contributor

Can't we just call the template?

@teodutu
Copy link
Member Author

teodutu commented Jan 20, 2023

Can't we just call the template?

Maybe, but for this, we would need the actual type to be available at compile time. We can obtain it from TypeInfo_Class.name, but only at run time. LE: one can create a TypeInfo_Class object manually, in which case the type embedded within it is totally inaccessible at compile time.

@teodutu teodutu force-pushed the template-_d_newclass branch 2 times, most recently from 08a5136 to de58969 Compare January 20, 2023 13:31
@RazvanN7
Copy link
Contributor

@ibuclaw @maxhaton any objections against this addition?

@RazvanN7 RazvanN7 added the 72h no objection -> merge The PR will be merged if there are no objections raised. label Jan 20, 2023
const rtl = RTLSYM.NEWCLASS;
ex = el_bin(OPcall,TYnptr,el_var(getRtlsym(rtl)),el_ptr(csym));
toTraceGC(irs, ex, ne.loc);
ex = toElem(ne.lowering, irs);
Copy link
Member

Choose a reason for hiding this comment

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

Are previous lowerings going to be retroactively transplanted into a lowering field?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, that's the plan.

druntime/.gdb_history Outdated Show resolved Hide resolved
druntime/src/rt/sections.d Outdated Show resolved Hide resolved
@teodutu teodutu force-pushed the template-_d_newclass branch 3 times, most recently from e1f1ebc to 0af6853 Compare January 21, 2023 14:43
@teodutu teodutu requested review from dkorpel and ibuclaw and removed request for dkorpel January 21, 2023 19:43
@dkorpel dkorpel dismissed their stale review January 21, 2023 21:30

Points were addressed

@thewilsonator thewilsonator removed the 72h no objection -> merge The PR will be merged if there are no objections raised. label Jan 22, 2023
@RazvanN7
Copy link
Contributor

@thewilsonator why have you removed the 72h no objections->merge label. Is there anything that you are requesting?

@thewilsonator
Copy link
Contributor

#14837 (review)

@RazvanN7
Copy link
Contributor

It has been addressed.

@RazvanN7
Copy link
Contributor

@ibuclaw is this ready to go?

@RazvanN7
Copy link
Contributor

@thewilsonator @ibuclaw re-adding the 72h no objections -> merge label

@RazvanN7 RazvanN7 added the 72h no objection -> merge The PR will be merged if there are no objections raised. label Jan 24, 2023
druntime/src/core/lifetime.d Outdated Show resolved Hide resolved
This makes the following changes:
- Add template `_d_newclassT` to `druntime.src.core.lifetime.d`
- Replace lowering of `new C()` to `_d_newclassT!C()`
- Add `lowering` member to `NewExp`. This field stores the above
lowering to be used by e2ir.d
- Keep the old `_d_newclass` hook because it's used by
`TypeInfo_Class.create()`
- Add dummy `_d_newclassT` hook to tests that redefine the `object`
module
- Remove `new MinHeap!(TestType)()` from `fail308.d`. Otherwise the
errror was changed and printed the local path to druntime
- Move `err` to global scope in rt.sections.d to avoid the frontend
lowering
- Account for the `GC.malloc()` called by the template hook in the
`-profile=gc` tests

Signed-off-by: Teodor Dutu <teodor.dutu@gmail.com>
@RazvanN7 RazvanN7 merged commit 276ef21 into dlang:master Jan 27, 2023
@jsatellite
Copy link

This changes how COM classes are managed - the memory will now be allocated by the GC whereas currently _d_newclass uses malloc. Looks like the getLinkage trait replaces the test for ci.m_flags & TypeInfo_Class.ClassFlags.isCOMClass - but __traits(getLinkage, ...) doesn't return "Windows" even if the class is declared with extern(Windows) (is it supposed to?). COM classes should either use malloc, or GC memory and a root added.

@rainers
Copy link
Member

rainers commented Feb 2, 2023

This changes how COM classes are managed

Good catch. The linkage reported for a class is derived from its member classKind which is C++ for COM classes.

COM classes should either use malloc, or GC memory and a root added.

IMO GC memory is the right choice, but adding/removing roots should be done in AddRef/Release. See https://issues.dlang.org/show_bug.cgi?id=4092

@RazvanN7
Copy link
Contributor

RazvanN7 commented Feb 3, 2023

doesn't return "Windows" even if the class is declared with extern(Windows) (is it supposed to?)

It returns Windows only if compiled for 32-bit.

Scratch that, it returns D on my machine for both 32 and 64 bit builds.

@RazvanN7
Copy link
Contributor

RazvanN7 commented Feb 3, 2023

How are we supposed to test if a class is a COM class? See if it implements IUnknown? Is there an easier way?

@jsatellite
Copy link

Can't you use T.classinfo.m_flags & TypeInfo_Class.ClassFlags.isCOMClass like in the un-templated _d_newclass?

@ibuclaw
Copy link
Member

ibuclaw commented Feb 12, 2023

This PR introduced a regression https://issues.dlang.org/show_bug.cgi?id=23688

@@ -539,6 +539,8 @@ class NewExp final : public Expression
bool onstack; // allocate on stack
bool thrownew; // this NewExp is the expression of a ThrowStatement

Expression lowering; // lowered druntime hook: `_d_newclass`
Copy link
Contributor

Choose a reason for hiding this comment

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

Shouldn't have this been a pointer?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes. Fixed by #14872.

@ibuclaw
Copy link
Member

ibuclaw commented Jun 7, 2023

This PR caused a regression.

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=110113

I'm going to have to revert this PR if a proper fix doesn't come in a reasonable time-frame given the severity of it.

@maxhaton
Copy link
Member

maxhaton commented Jun 7, 2023 via email

@ibuclaw
Copy link
Member

ibuclaw commented Jun 7, 2023

Does it only trigger with dip1021?

All that dip1021 is doing is making the compiler do a few more GC allocations/freeing by executing this code.

https://github.com/dlang/dmd/blob/master/compiler/src/dmd/ob.d

I think more likely, the trigger is -lowmem. Turning on dip1021 just allows the mesns.

Upstream bug report. https://issues.dlang.org/show_bug.cgi?id=23978

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
72h no objection -> merge The PR will be merged if there are no objections raised.
Projects
No open projects
9 participants