@Test public void checkMutateSpecialCase() throws Exception { final String reference = "#version 300 es\n" + "void main() {\n" + " int t;\n" + " {\n" + " }\n" + " gl_FragColor = vec4(float(t));\n" + "}\n"; final IRandom generator = new RandomWrapper(0); for (int i = 0; i < 10; i++) { final DonateLiveCodeTransformation donateLiveCode = new DonateLiveCodeTransformation(item -> true, testFolder.getRoot(), GenerationParams.normal(ShaderKind.FRAGMENT, true), false); final TranslationUnit referenceTu = ParseHelper.parse(reference); BlockInjectionPoint blockInjectionPoint = new ScopeTrackingVisitor() { BlockInjectionPoint blockInjectionPoint; @Override public void visitBlockStmt(BlockStmt stmt) { super.visitBlockStmt(stmt); if (stmt.getNumStmts() == 0) { blockInjectionPoint = new BlockInjectionPoint(stmt, null, getEnclosingFunction(), false, false, getCurrentScope()); } } BlockInjectionPoint getBlockInjectionPoint(TranslationUnit tu) { visit(tu); return blockInjectionPoint; } }.getBlockInjectionPoint(referenceTu); final DeclarationStmt declarationStmt = new DeclarationStmt( new VariablesDeclaration( BasicType.INT, new VariableDeclInfo("a", null, new Initializer( new BinaryExpr( new BinaryExpr( new BinaryExpr( new BinaryExpr( new VariableIdentifierExpr("x1"), new VariableIdentifierExpr("x2"), BinOp.ADD ), new VariableIdentifierExpr("x3"), BinOp.ADD ), new VariableIdentifierExpr("x4"), BinOp.ADD ), new VariableIdentifierExpr("x5"), BinOp.ADD ) ) ))); final Map<String, Type> freeVariables = new HashMap<>(); freeVariables.put("x1", BasicType.INT); freeVariables.put("x2", BasicType.INT); freeVariables.put("x3", BasicType.INT); freeVariables.put("x4", BasicType.INT); freeVariables.put("x5", BasicType.INT); Stmt toDonate = donateLiveCode.prepareStatementToDonate(blockInjectionPoint, new DonationContext( declarationStmt, freeVariables, new ArrayList<>(), new FunctionDefinition(new FunctionPrototype("foo", VoidType.VOID, new ArrayList<>()), null)), TransformationProbabilities.onlyLiveCodeAlwaysSubstitute(), generator, referenceTu.getShadingLanguageVersion()); blockInjectionPoint.inject(toDonate); final IdentityTransformation identityTransformation = new IdentityTransformation(); identityTransformation.apply(referenceTu, TransformationProbabilities.onlyMutateExpressions(), generator, GenerationParams.large(ShaderKind.FRAGMENT, true) ); } }
@Override Stmt prepareStatementToDonate(IInjectionPoint injectionPoint, DonationContext donationContext, TransformationProbabilities probabilities, IRandom generator, ShadingLanguageVersion shadingLanguageVersion) { List<Stmt> donatedStmts = new ArrayList<>(); for (Map.Entry<String, Type> vars : donationContext.getFreeVariables().entrySet()) { final Type type = vars.getValue(); final Type typeWithRestrictedQualifiers = dropQualifiersThatCannotBeUsedForLocalVariable(type); final Initializer initializer; if (isLoopLimiter(vars.getKey(), typeWithRestrictedQualifiers.getWithoutQualifiers())) { initializer = new Initializer(new IntConstantExpr("0")); } else { initializer = getInitializer(injectionPoint, donationContext, typeWithRestrictedQualifiers, true, generator, shadingLanguageVersion); } final ImmutablePair<Type, ArrayInfo> baseTypeAndArrayInfo = Typer.getBaseTypeArrayInfo(typeWithRestrictedQualifiers); donatedStmts.add(new DeclarationStmt( new VariablesDeclaration(baseTypeAndArrayInfo.left, new VariableDeclInfo(vars.getKey(), baseTypeAndArrayInfo.right, initializer)))); } donatedStmts.add(donationContext.getDonorFragment()); BlockStmt donatedStmt = new BlockStmt(donatedStmts, true); new RemoveImmediateBreakStatements(donatedStmt); new RemoveImmediateContinueStatements(donatedStmt); new RemoveImmediateCaseLabels(donatedStmt); new RemoveReturnStatements(donatedStmt); new RemoveDiscardStatements(donatedStmt); return donatedStmt; }
DonateLiveCodeTransformation extends DonateCodeTransformation { @Override Stmt prepareStatementToDonate(IInjectionPoint injectionPoint, DonationContext donationContext, TransformationProbabilities probabilities, IRandom generator, ShadingLanguageVersion shadingLanguageVersion) { List<Stmt> donatedStmts = new ArrayList<>(); for (Map.Entry<String, Type> vars : donationContext.getFreeVariables().entrySet()) { final Type type = vars.getValue(); final Type typeWithRestrictedQualifiers = dropQualifiersThatCannotBeUsedForLocalVariable(type); final Initializer initializer; if (isLoopLimiter(vars.getKey(), typeWithRestrictedQualifiers.getWithoutQualifiers())) { initializer = new Initializer(new IntConstantExpr("0")); } else { initializer = getInitializer(injectionPoint, donationContext, typeWithRestrictedQualifiers, true, generator, shadingLanguageVersion); } final ImmutablePair<Type, ArrayInfo> baseTypeAndArrayInfo = Typer.getBaseTypeArrayInfo(typeWithRestrictedQualifiers); donatedStmts.add(new DeclarationStmt( new VariablesDeclaration(baseTypeAndArrayInfo.left, new VariableDeclInfo(vars.getKey(), baseTypeAndArrayInfo.right, initializer)))); } donatedStmts.add(donationContext.getDonorFragment()); BlockStmt donatedStmt = new BlockStmt(donatedStmts, true); new RemoveImmediateBreakStatements(donatedStmt); new RemoveImmediateContinueStatements(donatedStmt); new RemoveImmediateCaseLabels(donatedStmt); new RemoveReturnStatements(donatedStmt); new RemoveDiscardStatements(donatedStmt); return donatedStmt; } }
DonateLiveCodeTransformation extends DonateCodeTransformation { @Override Stmt prepareStatementToDonate(IInjectionPoint injectionPoint, DonationContext donationContext, TransformationProbabilities probabilities, IRandom generator, ShadingLanguageVersion shadingLanguageVersion) { List<Stmt> donatedStmts = new ArrayList<>(); for (Map.Entry<String, Type> vars : donationContext.getFreeVariables().entrySet()) { final Type type = vars.getValue(); final Type typeWithRestrictedQualifiers = dropQualifiersThatCannotBeUsedForLocalVariable(type); final Initializer initializer; if (isLoopLimiter(vars.getKey(), typeWithRestrictedQualifiers.getWithoutQualifiers())) { initializer = new Initializer(new IntConstantExpr("0")); } else { initializer = getInitializer(injectionPoint, donationContext, typeWithRestrictedQualifiers, true, generator, shadingLanguageVersion); } final ImmutablePair<Type, ArrayInfo> baseTypeAndArrayInfo = Typer.getBaseTypeArrayInfo(typeWithRestrictedQualifiers); donatedStmts.add(new DeclarationStmt( new VariablesDeclaration(baseTypeAndArrayInfo.left, new VariableDeclInfo(vars.getKey(), baseTypeAndArrayInfo.right, initializer)))); } donatedStmts.add(donationContext.getDonorFragment()); BlockStmt donatedStmt = new BlockStmt(donatedStmts, true); new RemoveImmediateBreakStatements(donatedStmt); new RemoveImmediateContinueStatements(donatedStmt); new RemoveImmediateCaseLabels(donatedStmt); new RemoveReturnStatements(donatedStmt); new RemoveDiscardStatements(donatedStmt); return donatedStmt; } DonateLiveCodeTransformation(Function<IRandom, Boolean> probabilityOfDonation,
                                      File donorsDirectory,
                                      GenerationParams generationParams,
                                      boolean allowLongLoops); }
DonateLiveCodeTransformation extends DonateCodeTransformation { @Override Stmt prepareStatementToDonate(IInjectionPoint injectionPoint, DonationContext donationContext, TransformationProbabilities probabilities, IRandom generator, ShadingLanguageVersion shadingLanguageVersion) { List<Stmt> donatedStmts = new ArrayList<>(); for (Map.Entry<String, Type> vars : donationContext.getFreeVariables().entrySet()) { final Type type = vars.getValue(); final Type typeWithRestrictedQualifiers = dropQualifiersThatCannotBeUsedForLocalVariable(type); final Initializer initializer; if (isLoopLimiter(vars.getKey(), typeWithRestrictedQualifiers.getWithoutQualifiers())) { initializer = new Initializer(new IntConstantExpr("0")); } else { initializer = getInitializer(injectionPoint, donationContext, typeWithRestrictedQualifiers, true, generator, shadingLanguageVersion); } final ImmutablePair<Type, ArrayInfo> baseTypeAndArrayInfo = Typer.getBaseTypeArrayInfo(typeWithRestrictedQualifiers); donatedStmts.add(new DeclarationStmt( new VariablesDeclaration(baseTypeAndArrayInfo.left, new VariableDeclInfo(vars.getKey(), baseTypeAndArrayInfo.right, initializer)))); } donatedStmts.add(donationContext.getDonorFragment()); BlockStmt donatedStmt = new BlockStmt(donatedStmts, true); new RemoveImmediateBreakStatements(donatedStmt); new RemoveImmediateContinueStatements(donatedStmt); new RemoveImmediateCaseLabels(donatedStmt); new RemoveReturnStatements(donatedStmt); new RemoveDiscardStatements(donatedStmt); return donatedStmt; } DonateLiveCodeTransformation(Function<IRandom, Boolean> probabilityOfDonation,
                                      File donorsDirectory,
                                      GenerationParams generationParams,
                                      boolean allowLongLoops); @Override String getName(); }
DonateLiveCodeTransformation extends DonateCodeTransformation { @Override Stmt prepareStatementToDonate(IInjectionPoint injectionPoint, DonationContext donationContext, TransformationProbabilities probabilities, IRandom generator, ShadingLanguageVersion shadingLanguageVersion) { List<Stmt> donatedStmts = new ArrayList<>(); for (Map.Entry<String, Type> vars : donationContext.getFreeVariables().entrySet()) { final Type type = vars.getValue(); final Type typeWithRestrictedQualifiers = dropQualifiersThatCannotBeUsedForLocalVariable(type); final Initializer initializer; if (isLoopLimiter(vars.getKey(), typeWithRestrictedQualifiers.getWithoutQualifiers())) { initializer = new Initializer(new IntConstantExpr("0")); } else { initializer = getInitializer(injectionPoint, donationContext, typeWithRestrictedQualifiers, true, generator, shadingLanguageVersion); } final ImmutablePair<Type, ArrayInfo> baseTypeAndArrayInfo = Typer.getBaseTypeArrayInfo(typeWithRestrictedQualifiers); donatedStmts.add(new DeclarationStmt( new VariablesDeclaration(baseTypeAndArrayInfo.left, new VariableDeclInfo(vars.getKey(), baseTypeAndArrayInfo.right, initializer)))); } donatedStmts.add(donationContext.getDonorFragment()); BlockStmt donatedStmt = new BlockStmt(donatedStmts, true); new RemoveImmediateBreakStatements(donatedStmt); new RemoveImmediateContinueStatements(donatedStmt); new RemoveImmediateCaseLabels(donatedStmt); new RemoveReturnStatements(donatedStmt); new RemoveDiscardStatements(donatedStmt); return donatedStmt; } DonateLiveCodeTransformation(Function<IRandom, Boolean> probabilityOfDonation,
                                      File donorsDirectory,
                                      GenerationParams generationParams,
                                      boolean allowLongLoops); @Override String getName(); static final String NAME; }