Skip to content
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

Augmentable operators optimization: * is commutative when an operand is scalar #377

Merged
merged 2 commits into from
May 6, 2024
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
4 changes: 4 additions & 0 deletions src/ast.fs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,10 @@ and Type = {
not (Set.intersect (set this.typeQ) (set ["out"; "inout"])).IsEmpty
member this.IsExternal =
List.exists (fun s -> Set.contains s Builtin.externalQualifiers) this.typeQ
member this.isScalar =
match this.name with
| TypeName n -> Builtin.builtinScalarTypes.Contains n
| _ -> false
override t.ToString() =
let name = match t.name with
| TypeName n -> n
Expand Down
12 changes: 9 additions & 3 deletions src/builtin.fs
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,24 @@ let keywords = System.Collections.Generic.HashSet<_>([
"const"; "uniform"; "buffer"; "shared"; "attribute"; "varying"
])

let builtinTypes = set([
yield! [ "void"; "bool"; "int"; "uint"; "float"; "double" ]
let builtinScalarTypes = set [
"bool"; "int"; "uint"; "float"; "double"
]
let builtinVectorTypes = set([
for p in [""; "d"; "b"; "i"; "u"] do
for n in ["2"; "3"; "4"] do
yield p+"vec"+n
])
let builtinMatrixTypes = set([
for p in [""; "d"] do
for n in ["2"; "3"; "4"] do
yield p+"mat"+n
for c in ["2"; "3"; "4"] do
for r in ["2"; "3"; "4"] do
yield p+"mat"+c+"x"+r
])
])

let builtinTypes = set [ "void" ] + builtinScalarTypes + builtinVectorTypes + builtinMatrixTypes;

let implicitConversions = // (from, to)
[
Expand Down
9 changes: 7 additions & 2 deletions src/rewriter.fs
Original file line number Diff line number Diff line change
Expand Up @@ -218,8 +218,13 @@ module private RewriterImpl =

// x=...+x -> x+=...
// Works only if the operator is commutative. * is not commutative with vectors and matrices.
| FunCall(Op "=", [Var x; FunCall(Op ("+"|"&"|"^"|"|" as op), [e; Var y])])
when x.Name = y.Name ->
| FunCall(Op "=", [Var x; FunCall(Op ("+"|"*"|"&"|"^"|"|" as op), [e; Var y])])
when x.Name = y.Name
&& match x.VarDecl with
// * is commutative when at least one operand is scalar
| Some d -> op <> "*" || d.ty.isScalar
| _ -> false
->
FunCall(Op (op + "="), [Var x; e])

// Unsafe when x contains NaN or Inf values.
Expand Down
2 changes: 1 addition & 1 deletion tests/real/audio-flight-v2.frag.expected
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ vec2 marcher(vec3 ro,vec3 rd)
}
vec3 normal(vec3 p,float t)
{
t=MINDIST*t;
t*=MINDIST;
vec2 h=vec2(1,-1)*.5773;
return normalize(h.xyy*map(p+h.xyy*t,0.).x+h.yyx*map(p+h.yyx*t,0.).x+h.yxy*map(p+h.yxy*t,0.).x+h.xxx*map(p+h.xxx*t,0.).x);
}
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/inline.no.expected
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ float result;
void main()
{
float x=.5;
x=.6*x*x;
x*=.6*x;
result=x;
}
int arithmetic()
Expand Down