1
+ /*
2
+ * Copyright 2018 The GraphicsFuzz Project Authors
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License");
5
+ * you may not use this file except in compliance with the License.
6
+ * You may obtain a copy of the License at
7
+ *
8
+ * https://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS IS" BASIS,
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ * See the License for the specific language governing permissions and
14
+ * limitations under the License.
15
+ */
16
+
17
+ package com .graphicsfuzz .reducer .reductionopportunities ;
18
+
19
+ import com .graphicsfuzz .common .ast .TranslationUnit ;
20
+ import com .graphicsfuzz .common .glslversion .ShadingLanguageVersion ;
21
+ import com .graphicsfuzz .common .transformreduce .ShaderJob ;
22
+ import com .graphicsfuzz .common .util .CompareAsts ;
23
+ import com .graphicsfuzz .common .util .IdGenerator ;
24
+ import com .graphicsfuzz .common .util .ParseHelper ;
25
+ import com .graphicsfuzz .common .util .RandomWrapper ;
26
+ import java .util .List ;
27
+ import org .junit .Test ;
28
+
29
+ import static org .junit .Assert .*;
30
+
31
+ public class InlineFunctionReductionOpportunitiesTest {
32
+
33
+ @ Test
34
+ public void testBasicInline () throws Exception {
35
+
36
+ final String program = "int inlineme(int x) { int y = 2; return y + x; }" +
37
+ "void main() { int z = inlineme(5); }" ;
38
+
39
+ final String expected = "int inlineme(int x) { int y = 2; return y + x; }" +
40
+ "void main() { " +
41
+ " int inlineme_inline_return_value_0;" +
42
+ " {" +
43
+ " int x = 5;" +
44
+ " int y = 2;" +
45
+ " inlineme_inline_return_value_0 = y + x;" +
46
+ " }" +
47
+ " int z = inlineme_inline_return_value_0;" +
48
+ "}" ;
49
+
50
+ final TranslationUnit tu = ParseHelper .parse (program );
51
+ final ShaderJob shaderJob = MakeShaderJobFromFragmentShader .make (tu );
52
+
53
+ final List <InlineFunctionReductionOpportunity > ops =
54
+ InlineFunctionReductionOpportunities .findOpportunities (shaderJob ,
55
+ new ReducerContext (true ,
56
+ ShadingLanguageVersion .ESSL_100 , new RandomWrapper (0 ), new IdGenerator (), false ));
57
+
58
+ assertEquals (1 , ops .size ());
59
+ ops .get (0 ).applyReduction ();
60
+ CompareAsts .assertEqualAsts (expected , tu );
61
+ }
62
+
63
+ @ Test
64
+ public void testTooLargeToInline () throws Exception {
65
+
66
+ final StringBuilder program = new StringBuilder ();
67
+ program .append ("int donotinlineme(int x) { int y = 2;" );
68
+ for (int i = 0 ; i < 100 ; i ++) {
69
+ program .append (" y = y + x;" );
70
+ }
71
+ program .append ("}" );
72
+ program .append ("void main() { int z = donotinlineme(5); }" );
73
+
74
+ final TranslationUnit tu = ParseHelper .parse (program .toString ());
75
+ final ShaderJob shaderJob = MakeShaderJobFromFragmentShader .make (tu );
76
+
77
+ final List <InlineFunctionReductionOpportunity > ops =
78
+ InlineFunctionReductionOpportunities .findOpportunities (shaderJob ,
79
+ new ReducerContext (true ,
80
+ ShadingLanguageVersion .ESSL_100 , new RandomWrapper (0 ), new IdGenerator (), false ));
81
+
82
+ assertEquals (0 , ops .size ());
83
+ }
84
+
85
+
86
+ }
0 commit comments