InlineInitializerReductionOpportunities extends ReductionOpportunitiesBase<SimplifyExprReductionOpportunity> { static List<SimplifyExprReductionOpportunity> findOpportunities( ShaderJob shaderJob, ReducerContext context) { return shaderJob.getShaders() .stream() .map(item -> findOpportunitiesForShader(item, context)) .reduce(Arrays.asList(), ListConcat::concatenate); } private  InlineInitializerReductionOpportunities(
        TranslationUnit tu,
        ReducerContext context); @Override void visitVariableIdentifierExpr(VariableIdentifierExpr variableIdentifierExpr);  }
@Test public void testGlobalScope() throws Exception { final String program = "const int x = 2; void main() { int y = x + 3; }"; final String expected = "const int x = 2; void main() { int y = (2) + 3; }"; final TranslationUnit tu = ParseHelper.parse(program); final List<SimplifyExprReductionOpportunity> ops = InlineInitializerReductionOpportunities .findOpportunities(MakeShaderJobFromFragmentShader.make(tu), new ReducerContext(true, ShadingLanguageVersion.ESSL_100, new RandomWrapper(0), new IdGenerator())); assertEquals(1, ops.size()); ops.get(0).applyReduction(); CompareAsts.assertEqualAsts(expected, tu); assertTrue(InlineInitializerReductionOpportunities .findOpportunities(MakeShaderJobFromFragmentShader.make(tu), new ReducerContext(false, ShadingLanguageVersion.ESSL_100, new RandomWrapper(0), new IdGenerator())).isEmpty()); }
@Test public void testDoNotInlineLargeInitializer() throws Exception { final String largeProgram = make1Plus1Plus1Program(100); final String smallProgram = make1Plus1Plus1Program(5); final String smallProgramInlined = make1Plus1Plus1ProgramInlined(5); final TranslationUnit largeProgramTu = ParseHelper.parse(largeProgram); final TranslationUnit smallProgramTu = ParseHelper.parse(smallProgram); List<SimplifyExprReductionOpportunity> ops = InlineInitializerReductionOpportunities .findOpportunities(MakeShaderJobFromFragmentShader.make(largeProgramTu), new ReducerContext(true, ShadingLanguageVersion.ESSL_100, new RandomWrapper(0), new IdGenerator())); assertEquals(0, ops.size()); ops = InlineInitializerReductionOpportunities .findOpportunities(MakeShaderJobFromFragmentShader.make(smallProgramTu), new ReducerContext(true, ShadingLanguageVersion.ESSL_100, new RandomWrapper(0), new IdGenerator())); assertEquals(3, ops.size()); ops.get(0).applyReduction(); ops.get(1).applyReduction(); ops.get(2).applyReduction(); CompareAsts.assertEqualAsts(smallProgramInlined, smallProgramTu); }
@Test public void testDoNotInlineWhenPreservingSemantics() throws Exception { final String program = "void main() { int i = 4 + 2; i += i + i + i; i -= i * i; }"; final TranslationUnit tu = ParseHelper.parse(program); List<SimplifyExprReductionOpportunity> ops = InlineInitializerReductionOpportunities .findOpportunities(MakeShaderJobFromFragmentShader.make(tu), new ReducerContext(false, ShadingLanguageVersion.ESSL_100, new RandomWrapper(0), new IdGenerator())); assertTrue(ops.isEmpty()); }
@Test public void testDoNotInlineWhenPreservingSemantics2() throws Exception { final String program = "void main() { int i = 4 + 2; i += i + i + i; i -= i * i; }"; final TranslationUnit tu = ParseHelper.parse(program); List<SimplifyExprReductionOpportunity> ops = InlineInitializerReductionOpportunities .findOpportunities(MakeShaderJobFromFragmentShader.make(tu), new ReducerContext(false, ShadingLanguageVersion.ESSL_100, new RandomWrapper(0), new IdGenerator())); assertTrue(ops.isEmpty()); }
@Test public void testDoNotInlineWhenPreservingSemantics3() throws Exception { final String program = "void main() { int x = 2; int y = x; x = 3; y; }"; final TranslationUnit tu = ParseHelper.parse(program); List<SimplifyExprReductionOpportunity> ops = InlineInitializerReductionOpportunities .findOpportunities(MakeShaderJobFromFragmentShader.make(tu), new ReducerContext(false, ShadingLanguageVersion.ESSL_100, new RandomWrapper(0), new IdGenerator())); assertTrue(ops.isEmpty()); }
@Test public void testNoInliningWithNameShadowing1() throws Exception { final String program = "void main() { int x; { int x = x; x; } }"; final TranslationUnit tu = ParseHelper.parse(program); List<SimplifyExprReductionOpportunity> ops = InlineInitializerReductionOpportunities .findOpportunities(MakeShaderJobFromFragmentShader.make(tu), new ReducerContext(true, ShadingLanguageVersion.ESSL_100, new RandomWrapper(0), new IdGenerator())); assertEquals(0, ops.size()); }
@Test public void testNoInliningWithNameShadowing2() throws Exception { final String program = "void main() { int a; int x = a; { float a; int k; k = x; } }"; final TranslationUnit tu = ParseHelper.parse(program); List<SimplifyExprReductionOpportunity> ops = InlineInitializerReductionOpportunities .findOpportunities(MakeShaderJobFromFragmentShader.make(tu), new ReducerContext(true, ShadingLanguageVersion.ESSL_100, new RandomWrapper(0), new IdGenerator())); assertEquals(0, ops.size()); }
@Test public void testInliningInDeadCodeWithPreserveSemantics() throws Exception { final String program = "void main() {\n" + " if(" + Constants.GLF_DEAD + "(" + Constants.GLF_FALSE + "(false, false))) {\n" + " int do_inline_me = 5 + 2;\n" + " do_inline_me * do_inline_me;\n" + " }\n" + "}\n"; final String expected = "void main() {\n" + " if(" + Constants.GLF_DEAD + "(" + Constants.GLF_FALSE + "(false, false))) {\n" + " int do_inline_me = 5 + 2;\n" + " (5 + 2) * (5 + 2);\n" + " }\n" + "}\n"; final TranslationUnit tu = ParseHelper.parse(program); final List<SimplifyExprReductionOpportunity> ops = InlineInitializerReductionOpportunities .findOpportunities(MakeShaderJobFromFragmentShader.make(tu), new ReducerContext(false, ShadingLanguageVersion.ESSL_100, new RandomWrapper(0), new IdGenerator())); assertEquals(2, ops.size()); ops.get(0).applyReduction(); ops.get(1).applyReduction(); CompareAsts.assertEqualAsts(expected, tu); }
@Test public void testInliningInLiveCodeWithPreserveSemantics() throws Exception { final String variableName = Constants.LIVE_PREFIX + "do_inline_me"; final String program = "void main() {\n" + " {\n" + " int " + variableName + " = 5 + 2;\n" + " " + variableName + " * " + variableName + ";\n" + " }\n" + "}\n"; final String expected = "void main() {\n" + " {\n" + " int " + variableName + " = 5 + 2;\n" + " (5 + 2) * (5 + 2);\n" + " }\n" + "}\n"; final TranslationUnit tu = ParseHelper.parse(program); final List<SimplifyExprReductionOpportunity> ops = InlineInitializerReductionOpportunities .findOpportunities(MakeShaderJobFromFragmentShader.make(tu), new ReducerContext(false, ShadingLanguageVersion.ESSL_100, new RandomWrapper(0), new IdGenerator())); assertEquals(2, ops.size()); ops.get(0).applyReduction(); ops.get(1).applyReduction(); CompareAsts.assertEqualAsts(expected, tu); }