public Type lookupType(String name) { ScopeEntry entry = lookupScopeEntry(name); if (entry == null) { return null; } return entry.getType(); }
public MergeSet(ScopeEntry entry) { this.elementType = ((BasicType) entry.getType().getWithoutQualifiers()).getElementType(); this.entries = new ArrayList<>(); entries.add(entry); }
private int getWidth(ScopeEntry entry) { return ((BasicType) entry.getType().getWithoutQualifiers()).getNumElements(); }
public List<Integer> getIndices(String name) { int startIndex = 0; for (ScopeEntry entry : entries) { BasicType type = (BasicType) entry.getType().getWithoutQualifiers(); if (entry.getVariableDeclInfo().getName().equals(name)) { List<Integer> result = new ArrayList<>(); for (int i = 0; i < type.getNumElements(); i++) { result.add(startIndex + i); } return result; } startIndex += type.getNumElements(); } throw new RuntimeException("Should be unreachable."); }
public boolean canAccept(ScopeEntry entry) { BasicType basicType = (BasicType) entry.getType().getWithoutQualifiers(); if (!(basicType.getElementType().equals(elementType))) { return false; } if (entries.stream() .anyMatch(x -> x.getVariablesDeclaration() == entry.getVariablesDeclaration())) { return false; } return currentWidth() + basicType.getNumElements() <= MAX_WIDTH; }
private boolean isCandidateForMerging(ScopeEntry entry) { if (!entry.hasVariableDeclInfo()) { return false; } if (shadingLanguageVersion.initializersOfConstMustBeConst()) { if (entry.getType() instanceof QualifiedType && ((QualifiedType) entry.getType()).hasQualifier(TypeQualifier.CONST)) { // Do not merge const variables if the shading language requires the initializers of // const variables to be const. The issue is that if v is const and used in // some future initialiser, and we merge v into m, as m.x say, then m.x will appear in the // future initializer, which is illegal if m is not const itself. // With some additional analysis we could make this restriction only apply to consts // that are actually used in future initialisers. return false; } } Type unqualifiedType = entry.getType().getWithoutQualifiers(); if (!(unqualifiedType instanceof BasicType)) { return false; } BasicType basicType = (BasicType) unqualifiedType; if (!(basicType.isScalar() || basicType.isVector())) { return false; } if (basicType.isBoolean()) { return false; } return true; }
@Override public void visitVariableIdentifierExpr(VariableIdentifierExpr variableIdentifierExpr) { super.visitVariableIdentifierExpr(variableIdentifierExpr); final String name = variableIdentifierExpr.getName(); final ScopeEntry se = currentScope.lookupScopeEntry(name); if (se == null) { return; } if (se.getType() == null) { return; } if (!se.getType().hasQualifier(TypeQualifier.UNIFORM)) { return; } if (se.getType().getWithoutQualifiers() instanceof BasicType && uniformsInfo.containsKey(name)) { final BasicType basicType = (BasicType) se.getType().getWithoutQualifiers(); addOpportunity(new SimplifyExprReductionOpportunity( parentMap.getParent(variableIdentifierExpr), PruneUniforms.getBasicTypeLiteralExpr(basicType, uniformsInfo.getArgs(name)), variableIdentifierExpr, getVistitationDepth())); } }
@Override public void visitMemberLookupExpr(MemberLookupExpr memberLookupExpr) { super.visitMemberLookupExpr(memberLookupExpr); if (!(memberLookupExpr.getStructure() instanceof VariableIdentifierExpr)) { return; } VariableIdentifierExpr structVariable = ((VariableIdentifierExpr) memberLookupExpr .getStructure()); ScopeEntry se = currentScope.lookupScopeEntry(structVariable.getName()); if (se == null) { return; } if (se.getType().getWithoutQualifiers().equals(declaration.getVariablesDeclaration() .getBaseType().getWithoutQualifiers())) { // We've found the variable reference, but now we might have a chain, like: // s._f0._f2._f1._f0.v // We need to find the member expression that has .v and replace that with v MemberLookupExpr current = memberLookupExpr; while (current.getMember().startsWith(Constants.STRUCTIFICATION_FIELD_PREFIX)) { current = (MemberLookupExpr) parentMap.getParent(current); } parentMap.getParent(current).replaceChild( current, new VariableIdentifierExpr(originalVariableInfo.getName())); } } }.visit(tu);