Skip to content

Commit

Permalink
Augmentable operators optimization: * is commutative when an operand …
Browse files Browse the repository at this point in the history
…is scalar (#377)

I hoped to make it handle all cases (`*` is commutative when at least
one operand is scalar or both are vector) but that would require
determining the type of an expression.

For now, when an operand is a variable declared as a scalar type the
optimization is possible for `*`.
  • Loading branch information
therontarigo authored May 6, 2024
1 parent cccfcd7 commit ccd10bc
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 7 deletions.
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

0 comments on commit ccd10bc

Please sign in to comment.