8000 Iterate inlining and simplification only when we actually inline by jwatzman · Pull Request #138 · laurentlb/shader-minifier · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Iterate inlining and simplification only when we actually inline #138

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 20 additions & 22 deletions src/rewriter.fs
Original file line number Diff line number Diff line change
Expand Up @@ -63,18 +63,18 @@ let private bool = function
| true -> Var (Ident "true") // Int (1, "")
| false -> Var (Ident "false") // Int (0, "")

let rec private simplifyExpr env = function
let rec private simplifyExpr didInline env = function
| FunCall(Op "-", [Int (i1, su)]) -> Int (-i1, su)
| FunCall(Op "-", [FunCall(Op "-", [e])]) -> e
| FunCall(Op "+", [e]) -> e

| FunCall(Op ",", [e1; FunCall(Op ",", [e2; e3])]) ->
FunCall(Op ",", [simplifyExpr env (FunCall(Op ",", [e1; e2])); e3])
FunCall(Op ",", [env.fExpr env (FunCall(Op ",", [e1; e2])); e3])

| FunCall(Op "-", [x; Float (f, s)]) when f < 0.M ->
FunCall(Op "+", [x; Float (-f, s)]) |> simplifyExpr env
FunCall(Op "+", [x; Float (-f, s)]) |> env.fExpr env
| FunCall(Op "-", [x; Int (i, s)]) when i < 0 ->
FunCall(Op "+", [x; Int (-i, s)]) |> simplifyExpr env
FunCall(Op "+", [x; Int (-i, s)]) |> env.fExpr env

// Boolean simplifications (let's ignore the suffix)
| FunCall(Op "<", [Int (i1, _); Int (i2, _)]) -> bool(i1 < i2)
Expand Down Expand Up @@ -134,7 +134,9 @@ let rec private simplifyExpr env = function

| Var s as e ->
match env.vars.TryFind s.Name with
| Some (_, {name = id; init = Some init}) when id.ToBeInlined -> init |> mapExpr env
| Some (_, {name = id; init = Some init}) when id.ToBeInlined ->
didInline := true
init |> mapExpr env
| _ -> e

// pi is acos(-1), pi/2 is acos(0)
Expand Down Expand Up @@ -194,7 +196,7 @@ let collectReferences stmtList =
// - the variable never appears in an lvalue position (is never written to
// after initalization)
// - the init value is trivial
let findInlinable foundInlinable block =
let findInlinable block =
// Variables that are defined in this scope.
// The boolean indicates if the variable initialization has dependencies.
let localDefs = Dictionary<string, (Ident * bool)>()
Expand Down Expand Up @@ -223,10 +225,10 @@ let findInlinable foundInlinable block =
let ident, hasInitDeps = def.Value
if not ident.ToBeInlined then
if options.aggroInlining && not hasInitDeps && not ident.IsLValue then
ident.Inline(); foundInlinable := true
ident.Inline()
match localReferences.TryGetValue(def.Key), allReferences.TryGetValue(def.Key) with
| (true, 1), (true, 1) when not hasInitDeps -> ident.Inline(); foundInlinable := true
| (false, _), (false, _) -> ident.Inline(); found 8000 Inlinable := true
| (true, 1), (true, 1) when not hasInitDeps -> ident.Inline()
| (false, _), (false, _) -> ident.Inline()
| _ -> ()

let private simplifyStmt = function
Expand Down Expand Up @@ -277,19 +279,15 @@ let reorderTopLevel t =
t

let rec iterateSimplifyAndInline li =
let foundInlinable =
if options.noInlining then
false
else
let foundInlinableRef = ref false
let mapExpr _ e = e
let mapStmt = function
| Block b as e -> findInlinable foundInlinableRef b; e
| e -> e
mapTopLevel (mapEnv mapExpr mapStmt) li |> ignore
!foundInlinableRef
let simplified = mapTopLevel (mapEnv simplifyExpr simplifyStmt) li
if foundInlinable then iterateSimplifyAndInline simplified else simplified
if not options.noInlining then
let mapExpr _ e = e
let mapStmt = function
| Block b as e -> findInlinable b; e
| e -> e
mapTopLevel (mapEnv mapExpr mapStmt) li |> ignore
let didInline = ref false
let simplified = mapTopLevel (mapEnv (simplifyExpr didInline) simplifyStmt) li
if !didInline then iterateSimplifyAndInline simplified else simplified

let inlineAllConsts li =
let mapInnerDecl = function
Expand Down
6 changes: 5 additions & 1 deletion tests/unit/inline.aggro.expected
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,8 @@ int unusedVars2()
int multiPass()
{
return 3;
}
}
float multiPass2()
{
return 9.;
}
4 changes: 4 additions & 0 deletions tests/unit/inline.expected
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,7 @@ int multiPass()
{
return 3;
}
float multiPass2()
{
return 9.;
}
6 changes: 6 additions & 0 deletions tests/unit/inline.frag
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,9 @@ int multiPass()
int three = two + 1;
return three;
}

float multiPass2() {
float i_a = 4.0;
float b = i_a + 5.0;
return b;
}
5 changes: 5 additions & 0 deletions tests/unit/inline.no.expected
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,8 @@ int multiPass()
int >
return three;
}
float multiPass2()
{
float b=9.;
return b;
}
0