for (Declaration decl : tu.getTopLevelDeclarations()) { if (decl instanceof VariablesDeclaration && ((VariablesDeclaration) decl).getBaseType().hasQualifier(TypeQualifier.UNIFORM)) { final VariablesDeclaration variablesDeclaration = (VariablesDeclaration) decl; for (int i = 0; i < variablesDeclaration.getNumDecls(); i++) { final VariableDeclInfo declInfo = variablesDeclaration.getDeclInfo(i); if (!declInfo.getName().equals(uniformName)) { continue; found = true; assert !declInfo.hasInitializer(); final BasicType withoutQualifiers = (BasicType) variablesDeclaration.getBaseType() .getWithoutQualifiers(); declInfo.setInitializer(makeInitializer( withoutQualifiers, declInfo.getArrayInfo(), uniformsInfo.getArgs(uniformName))); final VariablesDeclaration newVariablesDeclaration = new VariablesDeclaration( withoutQualifiers, declInfo); variablesDeclaration.removeDeclInfo(i); if (variablesDeclaration.getNumDecls() > 0) {
@Override void applyReductionImpl() { if (!variablesDeclaration.getDeclInfos().contains(variableDeclInfo)) { // The declaration must have been removed by another reduction opportunity return; } for (int i = 0; i < variablesDeclaration.getNumDecls(); i++) { if (variablesDeclaration.getDeclInfo(i) == variableDeclInfo) { variablesDeclaration.removeDeclInfo(i); return; } } throw new RuntimeException("Should be unreachable."); }
@Override public void visitVariablesDeclaration(VariablesDeclaration variablesDeclaration) { visitChildFromParent(variablesDeclaration.getBaseType(), variablesDeclaration); List<VariableDeclInfo> children = new ArrayList<>(); children.addAll(variablesDeclaration.getDeclInfos()); for (VariableDeclInfo vd : children) { visitChildFromParent(vd, variablesDeclaration); } }
private void sweep(TranslationUnit tu) { final List<Declaration> oldTopLevelDecls = new ArrayList<>(); oldTopLevelDecls.addAll(tu.getTopLevelDeclarations()); for (Declaration decl : oldTopLevelDecls) { if (!(decl instanceof VariablesDeclaration)) { continue; } final VariablesDeclaration variablesDeclaration = (VariablesDeclaration) decl; int index = 0; while (index < variablesDeclaration.getNumDecls()) { if (unusedGlobals.contains(variablesDeclaration.getDeclInfo(index))) { variablesDeclaration.removeDeclInfo(index); } else { index++; } } if (variablesDeclaration.getNumDecls() == 0 && !isUsedStructType(variablesDeclaration.getBaseType())) { tu.removeTopLevelDeclaration(variablesDeclaration); } } }
@Override public void visitDeclarationStmt(DeclarationStmt declarationStmt) { super.visitDeclarationStmt(declarationStmt); // Currently we only structify solo declarations if (declarationStmt.getVariablesDeclaration().getNumDecls() != 1) { return; } // We don't currently structify arrays if (declarationStmt.getVariablesDeclaration().getDeclInfo(0).hasArrayInfo()) { return; } final Type baseType = declarationStmt.getVariablesDeclaration().getBaseType(); if (hasQualifiers(baseType)) { return; } // TODO: For simplicity, at present we do not structify non-basic types. The issue is that // if a struct S is to be structified, we need to declare the structs that enclose S *after* // S is declared, which is a bit fiddly (currently they all go at the top of the translation // unit). if (!(baseType.getWithoutQualifiers() instanceof BasicType)) { return; } opportunities.add(new StructificationOpportunity(declarationStmt, currentBlock(), tu, shadingLanguageVersion)); }
static boolean isStructifiedDeclaration(DeclarationStmt declarationStmt) { return declarationStmt.getVariablesDeclaration().getNumDecls() == 1 && declarationStmt.getVariablesDeclaration().getDeclInfo(0).getName() .startsWith(Constants.STRUCTIFICATION_STRUCT_PREFIX); }
@Override public VariablesDeclaration clone() { return new VariablesDeclaration(baseType.clone(), declInfos.stream().map(x -> x.clone()).collect(Collectors.toList())); }
List<VariableDeclInfo> declInfo = ((VariablesDeclaration) d).getDeclInfos().stream() .filter(vd -> !recipientGlobalNames.contains(vd.getName())) .collect(Collectors.toList()); .add(new VariablesDeclaration(((VariablesDeclaration) d).getBaseType(), declInfo));
private StructifiedVariableInfo findOriginalVariableInfo() { return findOriginalVariableInfo( (StructNameType) declaration.getVariablesDeclaration().getBaseType() .getWithoutQualifiers(), Optional.ofNullable((ScalarInitializer) declaration.getVariablesDeclaration() .getDeclInfo(0).getInitializer())) .get(); }
@Override public boolean preconditionHolds() { return variablesDeclaration.getDeclInfos().contains(variableDeclInfo); }
@Override public void visitVariablesDeclaration(VariablesDeclaration variablesDeclaration) { assert enclosingVariablesDeclarationType == null; enclosingVariablesDeclarationType = variablesDeclaration.getBaseType(); super.visitVariablesDeclaration(variablesDeclaration); enclosingVariablesDeclarationType = null; }
@Override public void applyReductionImpl() { if (originalNumVariables != declaration.getVariablesDeclaration().getNumDecls()) { // Something else changed how many declarations there are, so bail out. return; } final StructifiedVariableInfo originalVariableInfo = findOriginalVariableInfo(); // First, replace all occurrences in the translation unit deStructify(originalVariableInfo); // Now change the declaration declaration.getVariablesDeclaration().getDeclInfo(0).setName(originalVariableInfo.getName()); declaration.getVariablesDeclaration().setBaseType(originalVariableInfo.getType()); declaration.getVariablesDeclaration().getDeclInfo(0).setInitializer(originalVariableInfo .getInitializer().orElse(null)); }
private static void replaceLoopCounterInInitializer(Stmt init, String newLoopCounter) { ((DeclarationStmt) init).getVariablesDeclaration().getDeclInfo(0).setName(newLoopCounter); }
assert !((VariablesDeclaration) decl).getBaseType().hasQualifier(TypeQualifier.UNIFORM); newTopLevelDeclarations.add(new VariablesDeclaration( new QualifiedType(interfaceBlock.getMemberTypes().get(0).getWithoutQualifiers(), Arrays.asList(TypeQualifier.UNIFORM)),
public DestructifyReductionOpportunity(DeclarationStmt declaration, TranslationUnit tu, BlockStmt block, VisitationDepth depth) { super(depth); assert declaration.getVariablesDeclaration().getNumDecls() == 1; this.declaration = declaration; this.tu = tu; this.block = block; this.originalNumVariables = declaration.getVariablesDeclaration().getNumDecls(); }
private void structifyDeclaration(String enclosingStructVariableName, StructDefinitionType enclosingStructType) { declToTransform.getVariablesDeclaration() .setBaseType(enclosingStructType.getStructNameType()); final VariableDeclInfo declInfo = declToTransform.getVariablesDeclaration().getDeclInfo(0); declInfo.setName(enclosingStructVariableName); if (declInfo.hasInitializer()) { declInfo.setInitializer( new ScalarInitializer( makeInitializationExpr(enclosingStructType, ((ScalarInitializer) declInfo.getInitializer()).getExpr()) ) ); } }
&& ((VariablesDeclaration) decl).getBaseType().hasQualifier(TypeQualifier.UNIFORM)) { final VariablesDeclaration variablesDeclaration = (VariablesDeclaration) decl; if (variablesDeclaration.getNumDecls() == 0) { final QualifiedType qualifiedType = (QualifiedType) variablesDeclaration.getBaseType(); assert variablesDeclaration.getNumDecls() == 1; assert !variablesDeclaration.getDeclInfo(0).hasArrayInfo(); final String uniformName = variablesDeclaration.getDeclInfo(0).getName(); assert uniformsInfo.containsKey(uniformName); if (!uniformsInfo.hasBinding(uniformName)) {
public StructificationOpportunity(DeclarationStmt declToTransform, BlockStmt block, TranslationUnit tu, ShadingLanguageVersion shadingLanguageVersion) { assert declToTransform.getVariablesDeclaration().getNumDecls() == 1 : "Only solo declarations can be structified"; assert !declToTransform.getVariablesDeclaration().getDeclInfo(0).hasArrayInfo() : "Structification of arrays not supported"; this.declToTransform = declToTransform; this.block = block; this.tu = tu; this.shadingLanguageVersion = shadingLanguageVersion; }
@Override public void visitVariablesDeclaration(VariablesDeclaration variablesDeclaration) { visit(variablesDeclaration.getBaseType()); List<VariableDeclInfo> children = new ArrayList<>(); children.addAll(variablesDeclaration.getDeclInfos()); for (VariableDeclInfo declInfo : children) { visitVariableDeclInfo(declInfo); currentScope.add(declInfo.getName(), declInfo.getArrayInfo() == null ? variablesDeclaration.getBaseType() : new ArrayType(variablesDeclaration.getBaseType(), declInfo.getArrayInfo()), Optional.empty(), declInfo, variablesDeclaration); visitVariableDeclInfoAfterAddedToScope(declInfo); } }
private void addComponentVariableIfNotPresent() { if (getAllVariableDeclInfosInBlock() .stream() .anyMatch(item -> item.getName().equals(getComponentName()))) { return; } block.insertStmt(0, new DeclarationStmt(new VariablesDeclaration( getComponentType(), new VariableDeclInfo(getComponentName(), null, null)))); }