Skip to content

Commit c2c8189

Browse files
authored
Refactor _d_arrayliteralTX (#21573)
1 parent 02fa48b commit c2c8189

9 files changed

Lines changed: 133 additions & 56 deletions

File tree

compiler/src/dmd/backend/drtlsym.d

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,6 @@ Symbol* getRtlsym(RTLSYM i) @trusted
104104
case RTLSYM.MEMSETSIMD: symbolz(ps,FL.func,FREGSAVED,"_memsetSIMD",0, t); break;
105105
case RTLSYM.MEMSETN: symbolz(ps,FL.func,FREGSAVED,"_memsetn", 0, t); break;
106106
case RTLSYM.NEWTHROW: symbolz(ps,FL.func,FREGSAVED,"_d_newThrowable", 0, t); break;
107-
case RTLSYM.ARRAYLITERALTX: symbolz(ps,FL.func,FREGSAVED,"_d_arrayliteralTX", 0, t); break;
108107
case RTLSYM.ASSOCARRAYLITERALTX: symbolz(ps,FL.func,FREGSAVED,"_d_assocarrayliteralTX", 0, t); break;
109108
case RTLSYM.CALLFINALIZER: symbolz(ps,FL.func,FREGSAVED,"_d_callfinalizer", 0, t); break;
110109
case RTLSYM.CALLINTERFACEFINALIZER: symbolz(ps,FL.func,FREGSAVED,"_d_callinterfacefinalizer", 0, t); break;
@@ -158,7 +157,6 @@ Symbol* getRtlsym(RTLSYM i) @trusted
158157

159158
case RTLSYM.TRACECALLFINALIZER: symbolz(ps,FL.func,FREGSAVED,"_d_callfinalizerTrace", 0, t); break;
160159
case RTLSYM.TRACECALLINTERFACEFINALIZER: symbolz(ps,FL.func,FREGSAVED,"_d_callinterfacefinalizerTrace", 0, t); break;
161-
case RTLSYM.TRACEARRAYLITERALTX: symbolz(ps,FL.func,FREGSAVED,"_d_arrayliteralTXTrace", 0, t); break;
162160
case RTLSYM.TRACEASSOCARRAYLITERALTX: symbolz(ps,FL.func,FREGSAVED,"_d_assocarrayliteralTXTrace", 0, t); break;
163161
case RTLSYM.TRACEARRAYAPPENDCD: symbolz(ps,FL.func,FREGSAVED,"_d_arrayappendcdTrace", 0, t); break;
164162
case RTLSYM.TRACEARRAYAPPENDWD: symbolz(ps,FL.func,FREGSAVED,"_d_arrayappendwdTrace", 0, t); break;

compiler/src/dmd/backend/rtlsym.d

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@ enum RTLSYM
4949
MEMSETN,
5050
NEWTHROW,
5151

52-
ARRAYLITERALTX,
5352
ASSOCARRAYLITERALTX,
5453
CALLFINALIZER,
5554
CALLINTERFACEFINALIZER,
@@ -102,7 +101,6 @@ enum RTLSYM
102101

103102
TRACECALLFINALIZER,
104103
TRACECALLINTERFACEFINALIZER,
105-
TRACEARRAYLITERALTX,
106104
TRACEASSOCARRAYLITERALTX,
107105
TRACEARRAYAPPENDCD,
108106
TRACEARRAYAPPENDWD,

compiler/src/dmd/dtemplate.d

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4124,10 +4124,9 @@ extern (C++) class TemplateInstance : ScopeDsymbol
41244124
* To bypass this edge case, we always do codegen for `_d_arrayliteralTX` template instances,
41254125
* even if an instance already exists in non-root module.
41264126
*/
4127-
if (this.inst && this.inst.name == Id._d_arrayliteralTX)
4128-
{
4129-
return true;
4130-
}
4127+
if (auto inst = this.inst)
4128+
if (inst.name == Id._d_arrayliteralTX || inst.name == Id._d_arrayliteralTXTrace)
4129+
return true;
41314130

41324131
if (global.params.allInst)
41334132
{

compiler/src/dmd/e2ir.d

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -349,7 +349,6 @@ void toTraceGC(ref IRState irs, elem* e, Loc loc)
349349
[ RTLSYM.CALLFINALIZER, RTLSYM.TRACECALLFINALIZER ],
350350
[ RTLSYM.CALLINTERFACEFINALIZER, RTLSYM.TRACECALLINTERFACEFINALIZER ],
351351

352-
[ RTLSYM.ARRAYLITERALTX, RTLSYM.TRACEARRAYLITERALTX ],
353352
[ RTLSYM.ASSOCARRAYLITERALTX, RTLSYM.TRACEASSOCARRAYLITERALTX ],
354353

355354

compiler/src/dmd/nogc.d

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ public:
164164
lowering = new DotIdExp(e.loc, lowering, Id.object);
165165
auto tiargs = new Objects();
166166
// Remove `inout`, `const`, `immutable` and `shared` to reduce template instances
167-
auto t = e.type.unqualify(MODFlags.wild | MODFlags.const_ | MODFlags.immutable_ | MODFlags.shared_);
167+
auto t = e.type.nextOf().unqualify(MODFlags.wild | MODFlags.const_ | MODFlags.immutable_ | MODFlags.shared_);
168168
tiargs.push(t);
169169
lowering = new DotTemplateInstanceExp(e.loc, lowering, hook, tiargs);
170170

compiler/test/compilable/extra-files/vcg-ast.d.cg

Lines changed: 84 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -163,15 +163,96 @@ values!(__c_wchar_t)
163163
}
164164

165165
}
166-
_d_arrayliteralTX!(__c_wchar_t[])
166+
_d_arrayliteralTX!(__c_wchar_t)
167167
{
168168
pure nothrow @trusted void* _d_arrayliteralTX(ulong length)
169169
{
170-
return _d_arrayliteralTX(typeid(__c_wchar_t[]), length);
170+
const const(ulong) allocsize = length * 4LU;
171+
if (allocsize == 0LU)
172+
return null;
173+
else
174+
{
175+
import core.memory : GC;
176+
import core.internal.traits : hasIndirections, hasElaborateDestructor;
177+
alias BlkAttr = BlkAttr;
178+
uint attrs = 8u;
179+
attrs |= BlkAttr.NO_SCAN;
180+
return malloc(allocsize, attrs, typeid(__c_wchar_t));
181+
}
171182
}
172183

173184
}
174-
static __gshared TypeInfo_Array _D28TypeInfo_AE3vcg11__c_wchar_t6__initZ;
185+
hasIndirections!(__c_wchar_t)
186+
{
187+
enum bool hasIndirections = false;
188+
189+
}
190+
OriginalType!(__c_wchar_t)
191+
{
192+
alias OriginalType = dchar;
193+
194+
}
195+
ModifyTypePreservingTQ!(Impl, __c_wchar_t)
196+
{
197+
alias ModifyTypePreservingTQ = dchar;
198+
199+
}
200+
Modifier!(__c_wchar_t)
201+
{
202+
alias Impl = dchar;
203+
204+
}
205+
OriginalType!dchar
206+
{
207+
alias OriginalType = dchar;
208+
209+
}
210+
ModifyTypePreservingTQ!(Impl, dchar)
211+
{
212+
alias ModifyTypePreservingTQ = dchar;
213+
214+
}
215+
Modifier!dchar
216+
{
217+
alias Impl = dchar;
218+
219+
}
220+
hasIndirections!dchar
221+
{
222+
enum bool hasIndirections = false;
223+
224+
}
225+
isFunctionPointer!dchar
226+
{
227+
enum bool isFunctionPointer = false;
228+
229+
}
230+
isPointer!dchar
231+
{
232+
enum bool isPointer = false;
233+
234+
}
235+
isAggregateType!dchar
236+
{
237+
enum bool isAggregateType = false;
238+
239+
}
240+
isDelegate!dchar
241+
{
242+
enum bool isDelegate = false;
243+
244+
}
245+
isDynamicArray!dchar
246+
{
247+
enum bool isDynamicArray = false;
248+
249+
}
250+
Unqual!(dchar, dchar)
251+
{
252+
alias Unqual = dchar;
253+
254+
}
255+
static __gshared TypeInfo_Enum _D27TypeInfo_E3vcg11__c_wchar_t6__initZ;
175256
RTInfo!(_R)
176257
{
177258
enum immutable(void)* RTInfo = null;

druntime/src/core/internal/array/construction.d

Lines changed: 45 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -589,11 +589,53 @@ version (D_ProfileGC)
589589
}
590590
}
591591

592-
extern (C) void* _d_arrayliteralTX(const TypeInfo ti, size_t length) @trusted pure nothrow;
593592

593+
/**
594+
Allocate an array literal
595+
596+
Rely on the caller to do the initialization of the array.
597+
598+
---
599+
int[] getArr()
600+
{
601+
return [10, 20];
602+
// auto res = cast(int*) _d_arrayliteralTX(typeid(int[]), 2);
603+
// res[0] = 10;
604+
// res[1] = 20;
605+
// return res[0..2];
606+
}
607+
---
608+
609+
Params:
610+
T = unqualified type of array elements
611+
length = `.length` of array literal
612+
613+
Returns: pointer to allocated array
614+
*/
594615
void* _d_arrayliteralTX(T)(size_t length) @trusted pure nothrow
595616
{
596-
return _d_arrayliteralTX(typeid(T), length);
617+
const allocsize = length * T.sizeof;
618+
619+
if (allocsize == 0)
620+
return null;
621+
else
622+
{
623+
import core.memory : GC;
624+
import core.internal.traits : hasIndirections, hasElaborateDestructor;
625+
alias BlkAttr = GC.BlkAttr;
626+
627+
/* Same as in core.internal.array.utils.__typeAttrs!T,
628+
* but don't use a nested template function call here to avoid
629+
* possible linking errors.
630+
*/
631+
uint attrs = BlkAttr.APPENDABLE;
632+
static if (!hasIndirections!T)
633+
attrs |= BlkAttr.NO_SCAN;
634+
static if (is(T == struct) && hasElaborateDestructor!T)
635+
attrs |= BlkAttr.FINALIZE;
636+
637+
return GC.malloc(allocsize, attrs, typeid(T));
638+
}
597639
}
598640

599641
version (D_ProfileGC)
@@ -602,7 +644,7 @@ void* _d_arrayliteralTXTrace(T)(size_t length, string file = __FILE__, int line
602644
version (D_TypeInfo)
603645
{
604646
import core.internal.array.utils : TraceHook, gcStatsPure, accumulatePure;
605-
mixin(TraceHook!(T.stringof, "_d_arrayliteralTX"));
647+
mixin(TraceHook!((T[]).stringof, "_d_arrayliteralTX"));
606648

607649
return _d_arrayliteralTX!T(length);
608650
}

druntime/src/rt/lifetime.d

Lines changed: 0 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -695,45 +695,6 @@ extern (C) void[] _d_arrayappendwd(ref byte[] x, dchar c) @weak
695695
return x;
696696
}
697697

698-
/**
699-
Allocate an array literal
700-
701-
Rely on the caller to do the initialization of the array.
702-
703-
---
704-
int[] getArr()
705-
{
706-
return [10, 20];
707-
// auto res = cast(int*) _d_arrayliteralTX(typeid(int[]), 2);
708-
// res[0] = 10;
709-
// res[1] = 20;
710-
// return res[0..2];
711-
}
712-
---
713-
714-
Params:
715-
ti = `TypeInfo` of resulting array type
716-
length = `.length` of array literal
717-
718-
Returns: pointer to allocated array
719-
*/
720-
extern (C)
721-
void* _d_arrayliteralTX(const TypeInfo ti, size_t length) @weak
722-
{
723-
auto tinext = unqualify(ti.next);
724-
auto sizeelem = tinext.tsize; // array element size
725-
void* result;
726-
727-
debug(PRINTF) printf("_d_arrayliteralTX(sizeelem = %zd, length = %zd)\n", sizeelem, length);
728-
if (length == 0 || sizeelem == 0)
729-
return null;
730-
else
731-
{
732-
auto allocsize = length * sizeelem;
733-
return GC.malloc(allocsize, __typeAttrs(tinext) | BlkAttr.APPENDABLE, tinext);
734-
}
735-
}
736-
737698

738699
unittest
739700
{

druntime/src/rt/tracegc.d

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ extern (C) void _d_callinterfacefinalizer(void *p);
2020
extern (C) void _d_delclass(Object* p);
2121
extern (C) void _d_delinterface(void** p);
2222
extern (C) void _d_delmemory(void* *p);
23-
extern (C) void* _d_arrayliteralTX(const TypeInfo ti, size_t length);
2423
extern (C) void* _d_assocarrayliteralTX(const TypeInfo_AssociativeArray ti,
2524
void[] keys, void[] vals);
2625
extern (C) void[] _d_arrayappendcd(ref byte[] x, dchar c);

0 commit comments

Comments
 (0)