internal/shader: optimization: remove unnecessary local variables

Updates #2034
Updates #2188
This commit is contained in:
Hajime Hoshi 2022-07-10 18:58:39 +09:00
parent b6a340a96f
commit 4afabe34ae
8 changed files with 25 additions and 73 deletions

View File

@ -534,32 +534,12 @@ func (cs *compileState) parseExpr(block *block, expr ast.Expr, markLocalVariable
return nil, nil, nil, false
}
idx := block.totalLocalVariableNum()
block.vars = append(block.vars, variable{
typ: t,
})
// Calling the function should be done eariler to treat out-params correctly.
stmts = append(stmts, shaderir.Stmt{
Type: shaderir.Assign,
Exprs: []shaderir.Expr{
{
Type: shaderir.LocalVariable,
Index: idx,
},
{
Type: shaderir.Call,
Exprs: append([]shaderir.Expr{callee}, args...),
},
},
})
// The actual expression here is just a local variable that includes the result of the
// function call.
return []shaderir.Expr{
{
Type: shaderir.LocalVariable,
Index: idx,
Type: shaderir.Call,
Exprs: append([]shaderir.Expr{callee}, args...),
},
}, []shaderir.Type{t}, stmts, true
}

View File

@ -2,9 +2,7 @@ vec2 F0(in vec2 l0);
vec2 F1(in vec2 l0);
vec2 F0(in vec2 l0) {
vec2 l1 = vec2(0);
l1 = F1(l0);
return l1;
return F1(l0);
}
vec2 F1(in vec2 l0) {

View File

@ -12,13 +12,9 @@ float F1(in float l0) {
}
int F2(void) {
int l0 = 0;
l0 = F0(1);
return l0;
return F0(1);
}
float F3(void) {
float l0 = float(0);
l0 = F1(1.0);
return l0;
return F1(1.0);
}

View File

@ -4,13 +4,9 @@ vec2 F1(void);
vec2 F0(void) {
vec2 l0 = vec2(0);
vec2 l1 = vec2(0);
vec2 l2 = vec2(0);
vec2 l3 = vec2(0);
l0 = F1();
l1 = (1.0) * (l0);
l2 = F1();
l3 = (l2) * (1.0);
return l1;
l0 = (1.0) * (F1());
l1 = (F1()) * (1.0);
return l0;
}
vec2 F1(void) {

View File

@ -11,10 +11,8 @@ int F1(void) {
l0 = 0;
for (int l1 = 0; l1 < 10; l1++) {
int l2 = 0;
int l3 = 0;
l2 = F0(l1);
l3 = l2;
l0 = (l0) + (l3);
l0 = (l0) + (l2);
}
l2 = 0;
l0 = (l0) + (l2);

View File

@ -14,16 +14,12 @@ void main(void) {
l0 = 0;
for (int l1 = 0; l1 < 10; l1++) {
int l2 = 0;
int l3 = 0;
l2 = F0(l1);
l3 = l2;
l0 = (l0) + (l3);
for (int l4 = 0; l4 < 10; l4++) {
int l5 = 0;
int l6 = 0;
l5 = F0(l4);
l6 = l5;
l0 = (l0) + (l6);
l0 = (l0) + (l2);
for (int l3 = 0; l3 < 10; l3++) {
int l4 = 0;
l4 = F0(l3);
l0 = (l0) + (l4);
}
}
l2 = 0;

View File

@ -15,16 +15,12 @@ void main(void) {
l0 = 0;
for (int l1 = 0; l1 < 10; l1++) {
int l2 = 0;
int l3 = 0;
l2 = F0(l1);
l3 = l2;
l0 = (l0) + (l3);
for (int l4 = 0; l4 < 10; l4++) {
int l5 = 0;
int l6 = 0;
l5 = F0(l4);
l6 = l5;
l0 = (l0) + (l6);
l0 = (l0) + (l2);
for (int l3 = 0; l3 < 10; l3++) {
int l4 = 0;
l4 = F0(l3);
l0 = (l0) + (l4);
}
}
l2 = 0;

View File

@ -8,25 +8,17 @@ void F5(void);
vec2 F0(void) {
float l0 = float(0);
float l1 = float(0);
float l2 = float(0);
float l3 = float(0);
l0 = F2();
l1 = (l0) * (1.0);
l2 = F2();
l3 = (1.0) * (l2);
return vec2(l1, l3);
l0 = (F2()) * (1.0);
l1 = (1.0) * (F2());
return vec2(l0, l1);
}
vec2 F1(void) {
float l0 = float(0);
float l1 = float(0);
float l2 = float(0);
float l3 = float(0);
l0 = F2();
l1 = (l0) * (1.0);
l2 = F2();
l3 = (1.0) * (l2);
return vec2(l1, l3);
l0 = (F2()) * (1.0);
l1 = (1.0) * (F2());
return vec2(l0, l1);
}
float F2(void) {