Skip to content

Commit

Permalink
POC: automatically move scope array literals to the stack
Browse files Browse the repository at this point in the history
  • Loading branch information
Dennis Korpel committed Sep 27, 2024
1 parent 1d0b93b commit 62bc416
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
17 changes: 17 additions & 0 deletions compiler/src/dmd/escape.d
Original file line number Diff line number Diff line change
Expand Up @@ -2056,6 +2056,23 @@ void finishScopeParamInference(FuncDeclaration funcdecl, ref TypeFunction f)
f.isScopeQual = funcdecl.vthis.isScope();
f.isScopeInferred = !!(funcdecl.vthis.storage_class & STC.scopeinferred);
}

import dmd.foreachvar;
void dgVar(VarDeclaration v)
{
if (v.maybeScope && v._init)
if (auto ei = v._init.isExpInitializer())
if (auto ce = ei.exp.isConstructExp())
if (auto ale = ce.e2.isArrayLiteralExp())
ale.onstack = true;
}

void dgExp(Expression e)
{
foreachVar(e, &dgVar);
}

foreachExpAndVar(funcdecl.fbody, &dgExp, &dgVar);
}

/************************************************
Expand Down
13 changes: 13 additions & 0 deletions compiler/test/runnable/betterc.d
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ extern (C) void main()
testRuntimeLowerings();
test18457();
test20737();
testScopeInferenceArrayLiteral();
}

/*******************************************/
Expand Down Expand Up @@ -221,3 +222,15 @@ void test22427()
char[] p;
auto a = cast(int[])p;
}

/*******************************************/
// Test that local variable can be inferred scope, moving
// array literal from GC to stack, allowing usage in betterC

auto testScopeInferenceArrayLiteral()
{
auto x = [10, 20, 30] ~ [40];
x[0] = 50;
assert(x[0] + x[1] == 70);
return x[0];
}

0 comments on commit 62bc416

Please sign in to comment.