Skip to content

Commit 676313c

Browse files
chqrliebvdberg
authored andcommitted
Statistics: allow for finer grained analysis
* add `BucketWidth` to specify stats size granularity * don't round size up in ast node factories * sizes are rounded up in `alloc` and `Stat.add` * compute slack amount (79k for the c2c compiler < 2%)
1 parent 8674670 commit 676313c

File tree

4 files changed

+37
-17
lines changed

4 files changed

+37
-17
lines changed

ast/member_expr.c2

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,6 @@ public type MemberExpr struct @(opaque) {
7171
public fn MemberExpr* MemberExpr.create(ast_context.Context* c, Expr* base, const Ref* refs, u32 refcount) {
7272
u32 size = sizeof(MemberExpr) + refcount * sizeof(MemberRef) + (refcount - 1) * sizeof(u32);
7373
if (base) size += sizeof(MemberRef);
74-
size = (size + 7) & ~0x7; // round to 8-byte (temp)
7574
MemberExpr* e = c.alloc(size);
7675
e.base.init(ExprKind.Member, refs[0].loc, 0, 0, 0, ValType.NValue);
7776
e.base.base.memberExprBits.num_refs = refcount;
@@ -99,9 +98,7 @@ fn Expr* MemberExpr.instantiate(MemberExpr* e, Instantiator* inst) {
9998
u32 refcount = e.base.base.memberExprBits.num_refs;
10099
Expr* base = e.getExprBase();
101100
u32 size = sizeof(MemberExpr) + refcount * sizeof(MemberRef) + (refcount - 1) * sizeof(u32);
102-
103101
if (base) size += sizeof(MemberRef);
104-
size = (size + 7) & ~0x7; // round to 8-byte (temp)
105102
MemberExpr* e2 = inst.c.alloc(size);
106103

107104
string.memcpy(e2, e, size);

ast/statistics.c2

Lines changed: 36 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ module ast;
1919
import string;
2020
import stdio local;
2121

22-
const u32 NCat = 1;
22+
const u32 NCat = 1; // 32 for fine grained analysis
23+
const u32 BucketWidth = 8; // 4 or less for slack analysis
2324

2425
type Stat struct {
2526
u32[NCat] count;
@@ -32,19 +33,22 @@ type Stats struct {
3233
Stat[elemsof(StmtKind)] stmts;
3334
Stat[elemsof(DeclKind)] decls;
3435
Stat[3] others; // 0=ArrayValue, 1=StaticAssert, 2=SwitchCase
35-
//Stat arrayValues;
36-
//Stat staticAsserts;
3736
}
3837

3938
fn void Stats.reset(Stats* s) {
4039
string.memset(s, 0, sizeof(Stats));
4140
}
4241

4342
fn void Stat.add(Stat* ss, u32 size) {
44-
u32 cat = (size + 7) >> 3;
45-
if (cat >= NCat) cat = NCat - 1;
46-
ss.count[cat]++;
47-
ss.size[cat] += size;
43+
if (NCat > 1) {
44+
u32 cat = (size + BucketWidth - 1) / BucketWidth;
45+
if (cat >= NCat) cat = NCat - 1;
46+
ss.count[cat]++;
47+
ss.size[cat] += (size + 7) & ~0x7;
48+
} else {
49+
ss.count[0]++;
50+
ss.size[0] += (size + 7) & ~0x7;
51+
}
4852
}
4953

5054
fn void Stats.addType(TypeKind kind, u32 size) {
@@ -77,6 +81,17 @@ fn void Stats.addSwitchCase(u32 size) {
7781
globals.stats.others[2].add(size);
7882
}
7983

84+
fn void Stat.slack(const Stat* ss, u32* countp, u32* totalp) {
85+
if (BucketWidth & 0x7) {
86+
for (u32 cat = 0; cat < NCat; cat++) {
87+
if (u32 frac = (cat * BucketWidth) & 0x7) {
88+
*countp += ss.count[cat];
89+
*totalp += ss.count[cat] * (8 - frac);
90+
}
91+
}
92+
}
93+
}
94+
8095
fn void Stat.dump(const Stat* ss, const char* name, u32* countp, u32* totalp) {
8196
u32 count = 0;
8297
u32 size = 0;
@@ -90,10 +105,10 @@ fn void Stat.dump(const Stat* ss, const char* name, u32* countp, u32* totalp) {
90105
if (!ss.count[cat]) continue;
91106
if (count == ss.count[cat]) {
92107
// no repeat if single category
93-
if (NCat > 1) printf(" %d", cat * 8);
108+
if (NCat > 1) printf(" %d", cat * BucketWidth);
94109
break;
95110
}
96-
printf(" %d:%d/%d", cat * 8, ss.count[cat], ss.size[cat]);
111+
printf(" %d:%d/%d", cat * BucketWidth, ss.count[cat], ss.size[cat]);
97112
}
98113
printf("\n");
99114
*countp += count;
@@ -151,7 +166,18 @@ fn void Stats.dump(const Stats* s) {
151166
u32 totalCount = typesCount + exprCount + stmtCount + declCount + otherCount;
152167
u32 totalSize = typesTotal + exprTotal + stmtTotal + declTotal + otherTotal;
153168
printf(" %20s %6d %7d\n", "objects", totalCount, totalSize);
154-
169+
if (NCat > 1 && BucketWidth < 8) {
170+
u32 slackCount = 0;
171+
u32 slackTotal = 0;
172+
u32 nStats = sizeof(Stats) / sizeof(Stat);
173+
Stat* sp = (Stat*)s;
174+
for (u32 i = 0; i < nStats; i++) {
175+
sp[i].slack(&slackCount, &slackTotal);
176+
}
177+
if (slackCount) {
178+
printf(" %20s %6d %7d\n", "slack", slackCount, slackTotal);
179+
}
180+
}
155181
printf("---------------------------------------\n");
156182
}
157183

ast/struct_type_decl.c2

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,6 @@ public fn StructTypeDecl* StructTypeDecl.create(ast_context.Context* c,
115115
u32 size = sizeof(StructTypeDecl) + num_members * sizeof(Decl*);
116116
size += sizeof(StructLayout);
117117
size += num_members * sizeof(StructMemberLayout); // to store StructMemberLayouts
118-
size = (size + 7) & ~0x7; // round to 8-byte (temp)
119118
StructTypeDecl* d = c.alloc(size);
120119
StructType* stype = StructType.create(c, d);
121120
QualType qt = QualType.create(stype.asType());

ast/var_decl.c2

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,6 @@ public fn VarDecl* VarDecl.create(ast_context.Context* c,
103103
// Note: for incremental arrays, the initValue will be set later
104104
if (initValue || ref.isIncrArray() || has_embed) size += sizeof(VarDeclInit);
105105
assert (kind != VarDeclKind.StructMember);
106-
size = (size + 7) & ~0x7; // round to 8-byte (temp)
107106

108107
VarDecl* d = c.alloc(size);
109108
d.base.init(DeclKind.Variable, name, loc, is_public, QualType_Invalid, ast_idx);
@@ -131,7 +130,6 @@ public fn VarDecl* VarDecl.createStructMember(ast_context.Context* c,
131130
{
132131
u32 size = sizeof(VarDecl) + ref.getExtraSize();
133132
if (bitfield) size += sizeof(BitFieldInfo); // see above
134-
size = (size + 7) & ~0x7; // round to 8-byte (temp)
135133

136134
VarDecl* d = c.alloc(size);
137135
d.base.init(DeclKind.Variable, name, loc, is_public, QualType_Invalid, ast_idx);
@@ -160,7 +158,7 @@ fn VarDecl* VarDecl.instantiate(const VarDecl* vd, Instantiator* inst)
160158
if (vd.base.varDeclBits.has_init) extra += sizeof(VarDeclInit);
161159
if (vd.base.varDeclBits.is_bitfield) extra += sizeof(BitFieldInfo);
162160
u32 size = sizeof(VarDecl) + extra;
163-
size = (size + 7) & ~0x7; // round to 8-byte (temp)
161+
164162
VarDecl* vd2 = inst.c.alloc(size);
165163
vd2.base = vd.base;
166164
vd2.typeRef.instantiate(&vd.typeRef, inst);

0 commit comments

Comments
 (0)