Skip to content

Commit ef30ca4

Browse files
authored
Refactor -profile=gc _d_...Trace hooks (#22859)
1 parent a84051c commit ef30ca4

File tree

16 files changed

+256
-345
lines changed

16 files changed

+256
-345
lines changed

compiler/src/dmd/dinterpret.d

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1021,7 +1021,7 @@ Expression interpretStatement(UnionExp* pue, Statement s, InterState* istate)
10211021
return;
10221022

10231023
/**
1024-
* Interpret `return a ~= b` (i.e. `return _d_arrayappendT{,Trace}(a, b)`) as:
1024+
* Interpret `return a ~= b` (i.e. `return _d_arrayappendT(a, b)`) as:
10251025
* a ~= b;
10261026
* return a;
10271027
* This is needed because `a ~= b` has to be interpreted as an lvalue, in order to avoid
@@ -7587,10 +7587,10 @@ private void removeHookTraceImpl(ref CallExp ce, ref FuncDeclaration fd)
75877587
assert(hook.isDsymbol(), "Expected _d_HookTraceImpl's second template parameter to be an alias to the hook!");
75887588
fd = (cast(Dsymbol)hook).isFuncDeclaration;
75897589

7590-
// Remove the first three trace parameters
7590+
// Remove the last three trace parameters
75917591
auto arguments = new Expressions();
75927592
arguments.reserve(ce.arguments.length - 3);
7593-
arguments.pushSlice((*ce.arguments)[3 .. $]);
7593+
arguments.pushSlice((*ce.arguments)[0 .. $ - 3]);
75947594

75957595
ce = ctfeEmplaceExp!CallExp(ce.loc, ctfeEmplaceExp!VarExp(ce.loc, fd, false), arguments);
75967596

compiler/src/dmd/expressionsem.d

Lines changed: 19 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5203,7 +5203,7 @@ Expression lowerArrayLiteral(ArrayLiteralExp ale, Scope* sc)
52035203
{
52045204
const dim = ale.elements ? ale.elements.length : 0;
52055205

5206-
Identifier hook = global.params.tracegc ? Id._d_arrayliteralTXTrace : Id._d_arrayliteralTX;
5206+
Identifier hook = Id._d_arrayliteralTX;
52075207
if (!verifyHookExist(ale.loc, *sc, hook, "creating array literals"))
52085208
return null;
52095209

@@ -6425,7 +6425,7 @@ private extern (C++) final class ExpressionSemanticVisitor : Visitor
64256425
if (!global.params.useGC || !sc.needsCodegen())
64266426
return;
64276427

6428-
auto hook = global.params.tracegc ? Id._d_newitemTTrace : Id._d_newitemT;
6428+
auto hook = Id._d_newitemT;
64296429
if (!verifyHookExist(ne.loc, *sc, hook, "new struct"))
64306430
return;
64316431

@@ -6844,10 +6844,8 @@ private extern (C++) final class ExpressionSemanticVisitor : Visitor
68446844
!exp.placement &&
68456845
!exp.onstack && !exp.type.isScopeClass()) // these won't use the GC
68466846
{
6847-
/* replace `new T(arguments)` with `core.lifetime._d_newclassT!T(arguments)`
6848-
* or `_d_newclassTTrace`
6849-
*/
6850-
auto hook = global.params.tracegc ? Id._d_newclassTTrace : Id._d_newclassT;
6847+
/* replace `new T(arguments)` with `core.lifetime._d_newclassT!T(arguments)` */
6848+
auto hook = Id._d_newclassT;
68516849
if (!verifyHookExist(exp.loc, *sc, hook, "new class"))
68526850
return setError();
68536851

@@ -7032,7 +7030,7 @@ private extern (C++) final class ExpressionSemanticVisitor : Visitor
70327030
}
70337031
else if (nargs == 1)
70347032
{
7035-
auto hook = global.params.tracegc ? Id._d_newarrayTTrace : Id._d_newarrayT;
7033+
auto hook = Id._d_newarrayT;
70367034
if (!verifyHookExist(exp.loc, *sc, hook, "new array"))
70377035
goto LskipNewArrayLowering;
70387036

@@ -7058,7 +7056,7 @@ private extern (C++) final class ExpressionSemanticVisitor : Visitor
70587056
}
70597057
else
70607058
{
7061-
auto hook = global.params.tracegc ? Id._d_newarraymTXTrace : Id._d_newarraymTX;
7059+
auto hook = Id._d_newarraymTX;
70627060
if (!verifyHookExist(exp.loc, *sc, hook, "new multi-dimensional array"))
70637061
goto LskipNewArrayLowering;
70647062

@@ -12803,7 +12801,7 @@ private extern (C++) final class ExpressionSemanticVisitor : Visitor
1280312801
checkDefCtor(ale.loc, tn);
1280412802

1280512803
// Choose correct GC hook
12806-
Identifier hook = global.params.tracegc ? Id._d_arraysetlengthTTrace : Id._d_arraysetlengthT;
12804+
Identifier hook = Id._d_arraysetlengthT;
1280712805

1280812806
// Verify the correct hook exists
1280912807
if (!verifyHookExist(exp.loc, *sc, hook, "resizing arrays"))
@@ -13532,12 +13530,12 @@ private extern (C++) final class ExpressionSemanticVisitor : Visitor
1353213530

1353313531
if (exp.op == EXP.concatenateAssign)
1353413532
{
13535-
Identifier hook = global.params.tracegc ? Id._d_arrayappendTTrace : Id._d_arrayappendT;
13533+
Identifier hook = Id._d_arrayappendT;
1353613534

1353713535
if (!verifyHookExist(exp.loc, *sc, hook, "appending array to arrays", Id.object))
1353813536
return setError();
1353913537

13540-
// Lower to object._d_arrayappendT{,Trace}({file, line, funcname}, e1, e2)
13538+
// Lower to object._d_arrayappendT(e1, e2)
1354113539
Expression id = new IdentifierExp(exp.loc, Id.empty);
1354213540
id = new DotIdExp(exp.loc, id, Id.object);
1354313541
id = new DotIdExp(exp.loc, id, hook);
@@ -13565,11 +13563,11 @@ private extern (C++) final class ExpressionSemanticVisitor : Visitor
1356513563
return;
1356613564
}
1356713565

13568-
Identifier hook = global.params.tracegc ? Id._d_arrayappendcTXTrace : Id._d_arrayappendcTX;
13566+
Identifier hook = Id._d_arrayappendcTX;
1356913567
if (!verifyHookExist(exp.loc, *sc, hook, "appending element to arrays", Id.object))
1357013568
return setError();
1357113569

13572-
// Lower to object._d_arrayappendcTX{,Trace}(e1, 1), e1[$-1]=e2
13570+
// Lower to object._d_arrayappendcTX(e1, 1), e1[$-1]=e2
1357313571
Expression id = new IdentifierExp(exp.loc, Id.empty);
1357413572
id = new DotIdExp(exp.loc, id, Id.object);
1357513573
id = new DotIdExp(exp.loc, id, hook);
@@ -13880,9 +13878,7 @@ private extern (C++) final class ExpressionSemanticVisitor : Visitor
1388013878
(exp.e2.isStringExp() && (exp.e1.isIntegerExp() || exp.e1.isStringExp())))
1388113879
return exp;
1388213880

13883-
bool useTraceGCHook = global.params.tracegc && sc.needsCodegen();
13884-
13885-
Identifier hook = useTraceGCHook ? Id._d_arraycatnTXTrace : Id._d_arraycatnTX;
13881+
Identifier hook = Id._d_arraycatnTX;
1388613882
if (!verifyHookExist(exp.loc, *sc, hook, "concatenating arrays"))
1388713883
{
1388813884
setError();
@@ -13903,15 +13899,16 @@ private extern (C++) final class ExpressionSemanticVisitor : Visitor
1390313899
{
1390413900
Expression lowering = ce.lowering;
1390513901

13906-
/* Skip `file`, `line`, and `funcname` if the hook of the parent
13907-
* `CatExp` is `_d_arraycatnTXTrace`.
13908-
*/
1390913902
if (auto callExp = isRuntimeHook(lowering, hook))
1391013903
{
13911-
if (hook == Id._d_arraycatnTX)
13912-
arguments.pushSlice((*callExp.arguments)[]);
13913-
else
13904+
/* Under -profile=gc, the profiling wrapper appends `file`, `line`,
13905+
* and `funcname` default arguments. Strip them when flattening
13906+
* nested concatenations so we don't accumulate stale locations.
13907+
*/
13908+
if (global.params.tracegc)
1391413909
arguments.pushSlice((*callExp.arguments)[0 .. $ - 3]);
13910+
else
13911+
arguments.pushSlice((*callExp.arguments)[]);
1391513912
}
1391613913
}
1391713914
else

compiler/src/dmd/frontend.h

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8691,17 +8691,11 @@ struct Id final
86918691
static Identifier* _d_delThrowable;
86928692
static Identifier* _d_newThrowable;
86938693
static Identifier* _d_newclassT;
8694-
static Identifier* _d_newclassTTrace;
86958694
static Identifier* _d_newitemT;
8696-
static Identifier* _d_newitemTTrace;
86978695
static Identifier* _d_newarrayT;
8698-
static Identifier* _d_newarrayTTrace;
86998696
static Identifier* _d_newarrayU;
8700-
static Identifier* _d_newarrayUTrace;
87018697
static Identifier* _d_newarraymTX;
8702-
static Identifier* _d_newarraymTXTrace;
87038698
static Identifier* _d_arrayliteralTX;
8704-
static Identifier* _d_arrayliteralTXTrace;
87058699
static Identifier* _d_assert_fail;
87068700
static Identifier* _d_arrayctor;
87078701
static Identifier* _d_arraysetctor;
@@ -8732,13 +8726,9 @@ struct Id final
87328726
static Identifier* rt_init;
87338727
static Identifier* _d_HookTraceImpl;
87348728
static Identifier* _d_arraysetlengthT;
8735-
static Identifier* _d_arraysetlengthTTrace;
87368729
static Identifier* _d_arrayappendT;
8737-
static Identifier* _d_arrayappendTTrace;
87388730
static Identifier* _d_arrayappendcTX;
8739-
static Identifier* _d_arrayappendcTXTrace;
87408731
static Identifier* _d_arraycatnTX;
8741-
static Identifier* _d_arraycatnTXTrace;
87428732
static Identifier* _d_assocarrayliteralTX;
87438733
static Identifier* stdc;
87448734
static Identifier* stdarg;

compiler/src/dmd/id.d

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -280,17 +280,11 @@ immutable Msgtable[] msgtable =
280280
{ "_d_delThrowable" },
281281
{ "_d_newThrowable" },
282282
{ "_d_newclassT" },
283-
{ "_d_newclassTTrace" },
284283
{ "_d_newitemT" },
285-
{ "_d_newitemTTrace" },
286284
{ "_d_newarrayT" },
287-
{ "_d_newarrayTTrace" },
288285
{ "_d_newarrayU" },
289-
{ "_d_newarrayUTrace" },
290286
{ "_d_newarraymTX" },
291-
{ "_d_newarraymTXTrace" },
292287
{ "_d_arrayliteralTX" },
293-
{ "_d_arrayliteralTXTrace" },
294288
{ "_d_assert_fail" },
295289
{ "_d_arrayctor" },
296290
{ "_d_arraysetctor" },
@@ -334,13 +328,9 @@ immutable Msgtable[] msgtable =
334328
{ "__ArrayCast"},
335329
{ "_d_HookTraceImpl" },
336330
{ "_d_arraysetlengthT"},
337-
{ "_d_arraysetlengthTTrace"},
338331
{ "_d_arrayappendT" },
339-
{ "_d_arrayappendTTrace" },
340332
{ "_d_arrayappendcTX" },
341-
{ "_d_arrayappendcTXTrace" },
342333
{ "_d_arraycatnTX" },
343-
{ "_d_arraycatnTXTrace" },
344334
{ "_d_assocarrayliteralTX" },
345335

346336
// varargs implementation

compiler/src/dmd/templatesem.d

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2189,15 +2189,14 @@ bool findMixinTempDecl(TemplateMixin tm, Scope* sc)
21892189
private bool isDRuntimeHook(Identifier id)
21902190
{
21912191
return id == Id._d_HookTraceImpl ||
2192-
id == Id._d_newclassT || id == Id._d_newclassTTrace ||
2193-
id == Id._d_arraycatnTX || id == Id._d_arraycatnTXTrace ||
2192+
id == Id._d_newclassT ||
2193+
id == Id._d_arraycatnTX ||
21942194
id == Id._d_newThrowable || id == Id._d_delThrowable ||
21952195
id == Id._d_arrayassign_l || id == Id._d_arrayassign_r ||
21962196
id == Id._d_arraysetassign || id == Id._d_arraysetctor ||
21972197
id == Id._d_arrayctor ||
21982198
id == Id._d_arraysetlengthT ||
2199-
id == Id._d_arraysetlengthTTrace ||
2200-
id == Id._d_arrayappendT || id == Id._d_arrayappendTTrace ||
2199+
id == Id._d_arrayappendT ||
22012200
id == Id._d_arrayappendcTX;
22022201
}
22032202

druntime/mak/COPY

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ COPY=\
4141
$(IMPDIR)\core\internal\newaa.d \
4242
$(IMPDIR)\core\internal\parseoptions.d \
4343
$(IMPDIR)\core\internal\postblit.d \
44+
$(IMPDIR)\core\internal\profile_gc.d \
4445
$(IMPDIR)\core\internal\qsort.d \
4546
$(IMPDIR)\core\internal\spinlock.d \
4647
$(IMPDIR)\core\internal\string.d \

druntime/mak/SRCS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ SRCS=\
3939
src\core\internal\newaa.d \
4040
src\core\internal\parseoptions.d \
4141
src\core\internal\postblit.d \
42+
src\core\internal\profile_gc.d \
4243
src\core\internal\qsort.d \
4344
src\core\internal\spinlock.d \
4445
src\core\internal\string.d \

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

Lines changed: 0 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -111,26 +111,6 @@ private ref Tarr _d_arrayappendcTX_(Tarr : T[], T)(return ref scope Tarr px, siz
111111
assert(0, "Cannot append to array if compiling without support for runtime type information!");
112112
}
113113

114-
version (D_ProfileGC)
115-
{
116-
/**
117-
* TraceGC wrapper around _d_arrayappendcTX.
118-
*/
119-
ref Tarr _d_arrayappendcTXTrace(Tarr : T[], T)(return ref scope Tarr px, size_t n,
120-
string file = __FILE__, int line = __LINE__, string funcname = __FUNCTION__) @trusted
121-
{
122-
version (D_TypeInfo)
123-
{
124-
import core.internal.array.utils: TraceHook, gcStatsPure, accumulatePure;
125-
mixin(TraceHook!("Tarr", "_d_arrayappendcTX"));
126-
127-
return _d_arrayappendcTX(px, n);
128-
}
129-
else
130-
static assert(0, "Cannot append to array if compiling without support for runtime type information!");
131-
}
132-
}
133-
134114
/// Implementation of `_d_arrayappendT`
135115
ref Tarr _d_arrayappendT(Tarr : T[], T)(return ref scope Tarr x, scope Tarr y) @trusted
136116
{
@@ -197,25 +177,6 @@ ref Tarr _d_arrayappendT(Tarr : T[], T)(return ref scope Tarr x, scope Tarr y) @
197177
return x;
198178
}
199179

200-
version (D_ProfileGC)
201-
{
202-
/**
203-
* TraceGC wrapper around $(REF _d_arrayappendT, core,internal,array,appending).
204-
*/
205-
ref Tarr _d_arrayappendTTrace(Tarr : T[], T)(return ref scope Tarr x, scope Tarr y, string file = __FILE__, int line = __LINE__, string funcname = __FUNCTION__) @trusted
206-
{
207-
version (D_TypeInfo)
208-
{
209-
import core.internal.array.utils: TraceHook, gcStatsPure, accumulatePure;
210-
mixin(TraceHook!("Tarr", "_d_arrayappendT"));
211-
212-
return _d_arrayappendT(x, y);
213-
}
214-
else
215-
static assert(0, "Cannot append to array if compiling without support for runtime type information!");
216-
}
217-
}
218-
219180
@safe unittest
220181
{
221182
double[] arr1;

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

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -368,24 +368,6 @@ private size_t _d_arraysetlengthT_(Tarr : T[], T)(return ref scope Tarr arr, siz
368368
return newlength;
369369
}
370370

371-
version (D_ProfileGC)
372-
{
373-
enum errorMessage = "Cannot resize arrays";
374-
import core.internal.array.utils : _d_HookTraceImpl;
375-
376-
// Function wrapper around the hook, so it’s callable
377-
size_t _d_arraysetlengthTTrace(Tarr : T[], T)(
378-
return ref scope Tarr arr,
379-
size_t newlength,
380-
string file = __FILE__,
381-
int line = __LINE__,
382-
string func = __FUNCTION__
383-
) @trusted
384-
{
385-
alias Hook = _d_HookTraceImpl!(Tarr, _d_arraysetlengthT!Tarr, errorMessage);
386-
return Hook(arr, newlength, file, line, func);
387-
}
388-
}
389371

390372
// @safe unittest remains intact
391373
@safe unittest

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

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -163,23 +163,3 @@ Tret _d_arraycatnTX(Tret: Tret_El[], Tret_El, Tarr...)(auto ref Tarr froms) @tru
163163
assert(counter == 4);
164164
assert(didThrow);
165165
}
166-
167-
version (D_ProfileGC)
168-
{
169-
/**
170-
* TraceGC wrapper around $(REF _d_arraycatnTX, core,internal,array,concatenation).
171-
*/
172-
Tret _d_arraycatnTXTrace(Tret, Tarr...)(scope auto ref Tarr froms, string file = __FILE__, int line = __LINE__, string funcname = __FUNCTION__) @trusted
173-
{
174-
version (D_TypeInfo)
175-
{
176-
import core.internal.array.utils: TraceHook, gcStatsPure, accumulatePure;
177-
mixin(TraceHook!("Tarr", "_d_arraycatnTX"));
178-
179-
import core.lifetime: forward;
180-
return _d_arraycatnTX!Tret(forward!froms);
181-
}
182-
else
183-
assert(0, "Cannot concatenate arrays if compiling without support for runtime type information!");
184-
}
185-
}

0 commit comments

Comments
 (0)