InlineUniformReductionOpportunities extends SimplifyExprReductionOpportunities { public static List<SimplifyExprReductionOpportunity> findOpportunities( ShaderJob shaderJob, ReducerContext context) { return shaderJob.getShaders() .stream() .map(item -> findOpportunitiesForShader(item, context, shaderJob.getPipelineInfo())) .reduce(Arrays.asList(), ListConcat::concatenate); } private  InlineUniformReductionOpportunities(TranslationUnit tu,
                                              ReducerContext context,
                                              PipelineInfo pipelineInfo); static List<SimplifyExprReductionOpportunity> findOpportunities(
      ShaderJob shaderJob,
      ReducerContext context); @Override void visitVariableIdentifierExpr(VariableIdentifierExpr variableIdentifierExpr);  }
@Test public void testDoNotInlineWhenPreservingSemantics() throws Exception { final String shader = "#version 310 es\n" + "uniform int u;\n" + "void main() {\n" + " u;\n" + "}\n"; final TranslationUnit tu = ParseHelper.parse(shader); final PipelineInfo pipelineInfo = new PipelineInfo(); pipelineInfo.addUniform("u", BasicType.INT, Optional.empty(), Collections.singletonList(2)); final List<SimplifyExprReductionOpportunity> opportunities = InlineUniformReductionOpportunities.findOpportunities(new GlslShaderJob(Optional.empty(), pipelineInfo, tu), new ReducerContext(false, ShadingLanguageVersion.ESSL_310, new RandomWrapper(0), new IdGenerator())); assertTrue(opportunities.isEmpty()); }
@Test public void testDoInlineWhenPreservingSemanticsDeadCode() throws Exception { final String shader = "#version 310 es\n" + "uniform int u;\n" + "void main() {\n" + " u;\n" + " if(" + Constants.GLF_DEAD + "(" + Constants.GLF_FALSE + "(false, false))) {\n" + " u;\n" + " }\n" + "}\n"; final String expected = "#version 310 es\n" + "uniform int u;\n" + "void main() {\n" + " u;\n" + " if(" + Constants.GLF_DEAD + "(" + Constants.GLF_FALSE + "(false, false))) {\n" + " 2;\n" + " }\n" + "}\n"; final TranslationUnit tu = ParseHelper.parse(shader); final PipelineInfo pipelineInfo = new PipelineInfo(); pipelineInfo.addUniform("u", BasicType.INT, Optional.empty(), Collections.singletonList(2)); final List<SimplifyExprReductionOpportunity> opportunities = InlineUniformReductionOpportunities.findOpportunities(new GlslShaderJob(Optional.empty(), pipelineInfo, tu), new ReducerContext(false, ShadingLanguageVersion.ESSL_310, new RandomWrapper(0), new IdGenerator())); assertEquals(1, opportunities.size()); opportunities.get(0).applyReduction(); CompareAsts.assertEqualAsts(expected, tu); }
@Test public void testDoInlineWhenPreservingSemanticsDeadFunction() throws Exception { final String shader = "#version 310 es\n" + "uniform int u;\n" + "void " + Constants.GLF_DEAD + "_foo() {\n" + " u;\n" + "}\n" + "void main() {\n" + " u;\n" + "}\n"; final String expected = "#version 310 es\n" + "uniform int u;\n" + "void " + Constants.GLF_DEAD + "_foo() {\n" + " 2;\n" + "}\n" + "void main() {\n" + " u;\n" + "}\n"; final TranslationUnit tu = ParseHelper.parse(shader); final PipelineInfo pipelineInfo = new PipelineInfo(); pipelineInfo.addUniform("u", BasicType.INT, Optional.empty(), Collections.singletonList(2)); final List<SimplifyExprReductionOpportunity> opportunities = InlineUniformReductionOpportunities.findOpportunities(new GlslShaderJob(Optional.empty(), pipelineInfo, tu), new ReducerContext(false, ShadingLanguageVersion.ESSL_310, new RandomWrapper(0), new IdGenerator())); assertEquals(1, opportunities.size()); opportunities.get(0).applyReduction(); CompareAsts.assertEqualAsts(expected, tu); }
@Test public void testDoInlineWhenPreservingSemanticsUnreachableSwitchCase() throws Exception { final String shader = "#version 310 es\n" + "uniform int u;\n" + "void main() {\n" + " switch(" + Constants.GLF_SWITCH + "(0)) {\n" + " case 2:\n" + " u;\n" + " case 3:\n" + " u;\n" + " case 0:\n" + " u;\n" + " case 5:\n" + " u;\n" + " break;\n" + " case 6:\n" + " u;\n" + " default:\n" + " u;\n" + " }\n" + "}\n"; final String expected = "#version 310 es\n" + "uniform int u;\n" + "void main() {\n" + " switch(" + Constants.GLF_SWITCH + "(0)) {\n" + " case 2:\n" + " 2;\n" + " case 3:\n" + " 2;\n" + " case 0:\n" + " u;\n" + " case 5:\n" + " u;\n" + " break;\n" + " case 6:\n" + " 2;\n" + " default:\n" + " 2;\n" + " }\n" + "}\n"; final TranslationUnit tu = ParseHelper.parse(shader); final PipelineInfo pipelineInfo = new PipelineInfo(); pipelineInfo.addUniform("u", BasicType.INT, Optional.empty(), Collections.singletonList(2)); final List<SimplifyExprReductionOpportunity> opportunities = InlineUniformReductionOpportunities.findOpportunities(new GlslShaderJob(Optional.empty(), pipelineInfo, tu), new ReducerContext(false, ShadingLanguageVersion.ESSL_310, new RandomWrapper(0), new IdGenerator())); assertEquals(4, opportunities.size()); for (SimplifyExprReductionOpportunity op : opportunities) { op.applyReduction(); } CompareAsts.assertEqualAsts(expected, tu); }
@Test public void testDoInlineWhenPreservingSemanticsLiveCode() throws Exception { final String shader = "#version 310 es\n" + "uniform int u;\n" + "uniform int " + Constants.LIVE_PREFIX + "v;\n" + "void main() {\n" + " u;\n" + " " + Constants.LIVE_PREFIX + "v;\n" + "}\n"; final String expected = "#version 310 es\n" + "uniform int u;\n" + "uniform int " + Constants.LIVE_PREFIX + "v;\n" + "void main() {\n" + " u;\n" + " 3;\n" + "}\n"; final TranslationUnit tu = ParseHelper.parse(shader); final PipelineInfo pipelineInfo = new PipelineInfo(); pipelineInfo.addUniform("u", BasicType.INT, Optional.empty(), Collections.singletonList(2)); pipelineInfo.addUniform(Constants.LIVE_PREFIX + "v", BasicType.INT, Optional.empty(), Collections.singletonList(3)); final List<SimplifyExprReductionOpportunity> opportunities = InlineUniformReductionOpportunities.findOpportunities(new GlslShaderJob(Optional.empty(), pipelineInfo, tu), new ReducerContext(false, ShadingLanguageVersion.ESSL_310, new RandomWrapper(0), new IdGenerator())); assertEquals(1, opportunities.size()); opportunities.get(0).applyReduction(); CompareAsts.assertEqualAsts(expected, tu); }