public Type lookupType(String name) { ScopeEntry entry = lookupScopeEntry(name); if (entry == null) { return null; } return entry.getType(); }
private boolean isScopeEntryForVectorVariable(ScopeEntry scopeEntry) { return scopeEntry != null && scopeEntry.hasVariableDeclInfo() && scopeEntry.getVariableDeclInfo() == vectorVariableDeclInfo; }
private void getReductionOpportunitiesForUnusedGlobals() { for (String name : currentScope.keys()) { ScopeEntry entry = currentScope.lookupScopeEntry(name); assert entry.hasVariableDeclInfo(); assert referencedScopeEntries.peek() != null; if (!referencedScopeEntries.peek().contains(entry)) { addOpportunity(new VariableDeclReductionOpportunity(entry.getVariableDeclInfo(), entry.getVariablesDeclaration(), getVistitationDepth())); } } }
private String applyVariableNameMapping(String name) { ScopeEntry scopeEntry = currentScope.lookupScopeEntry(name); if (scopeEntry == null) { return name; } if (scopeEntry.hasVariableDeclInfo()) { return varDeclMapping.getOrDefault(scopeEntry.getVariableDeclInfo(), name); } assert scopeEntry.hasParameterDecl(); return paramDeclMapping.getOrDefault(scopeEntry.getParameterDecl(), name); }
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; }
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."); }
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 String toString() { return entries.stream().map(item -> item.getVariableDeclInfo().getName()) .collect(Collectors.toList()).toString(); }
@Override public void visitVariableIdentifierExpr(VariableIdentifierExpr variableIdentifierExpr) { super.visitVariableIdentifierExpr(variableIdentifierExpr); final ScopeEntry scopeEntry = currentScope.lookupScopeEntry(variableIdentifierExpr.getName()); if (scopeEntry != null && scopeEntry.hasParameterDecl()) { unusedParametersForCurrentFunction.remove(scopeEntry.getParameterDecl()); } }
private boolean nonConst(VariableIdentifierExpr variableIdentifierExpr) { final ScopeEntry se = currentScope.lookupScopeEntry(variableIdentifierExpr.getName()); return se != null && se.hasVariablesDeclaration() && !se.getVariablesDeclaration().getBaseType() .hasQualifier(TypeQualifier.CONST); }
@Override public void visitVariableIdentifierExpr(VariableIdentifierExpr variableIdentifierExpr) { super.visitVariableIdentifierExpr(variableIdentifierExpr); ScopeEntry se = currentScope.lookupScopeEntry(variableIdentifierExpr.getName()); if (se == null) { // We are traversing a block in isolation, so we won't have a scope entry for any variable // declared outside the block. return; } if (se.getVariablesDeclaration() == declToTransform.getVariablesDeclaration()) { parentMap.getParent(variableIdentifierExpr).replaceChild( variableIdentifierExpr, structifiedExpr.clone()); } } }.visit(block);
public void add(String name, Type type, Optional<ParameterDecl> parameterDecl, VariableDeclInfo declInfo, VariablesDeclaration variablesDecl) { checkNameTypeAndParam(name, type, parameterDecl); variableMapping.put(name, new ScopeEntry(type, parameterDecl, declInfo, variablesDecl)); }
@Override public void visitVariablesDeclaration(VariablesDeclaration variablesDeclaration) { assert !inDeclarationOfTargetVariable; for (VariableDeclInfo vdi : variablesDeclaration.getDeclInfos()) { if (currentComponent.getVariableDeclInfo() == vdi) { inDeclarationOfTargetVariable = true; break; } } super.visitVariablesDeclaration(variablesDeclaration); inDeclarationOfTargetVariable = false; }
public void add(String name, Type type, Optional<ParameterDecl> parameterDecl) { checkNameTypeAndParam(name, type, parameterDecl); variableMapping.put(name, new ScopeEntry(type, parameterDecl)); }
private boolean isCurrentComponentVariable(String name) { ScopeEntry entry = currentScope.lookupScopeEntry(name); return entry != null && entry.hasVariableDeclInfo() && currentComponent.getVariableDeclInfo() == entry.getVariableDeclInfo(); }
for (String name : currentScope.keys()) { ScopeEntry entry = currentScope.lookupScopeEntry(name); if (entry.hasVariableDeclInfo() && !referencedScopeEntries.peek().contains(entry)) { if (allowedToReduceLocalDecl(entry.getVariableDeclInfo())) { addOpportunity( new VariableDeclReductionOpportunity( entry.getVariableDeclInfo(), entry.getVariablesDeclaration(), getVistitationDepth())); if (!entry.hasVariableDeclInfo()) { continue; String name = entry.getVariableDeclInfo().getName(); assert currentScope.lookupScopeEntry(name) == entry || currentScope.getParent().lookupScopeEntry(name) == entry;
public MergeSet(ScopeEntry entry) { this.elementType = ((BasicType) entry.getType().getWithoutQualifiers()).getElementType(); this.entries = new ArrayList<>(); entries.add(entry); }
public String getMergedName() { final List<MergedVariablesComponentData> mergedVariablesComponentDataList = new ArrayList<>(); int currentOffset = 0; for (ScopeEntry entry : entries) { mergedVariablesComponentDataList.add( new MergedVariablesComponentData(currentOffset, getWidth(entry), entry.getVariableDeclInfo().getName())); currentOffset += getWidth(entry); } return getMergedName(mergedVariablesComponentDataList); }
@Override public void visitVariableIdentifierExpr(VariableIdentifierExpr variableIdentifierExpr) { super.visitVariableIdentifierExpr(variableIdentifierExpr); final ScopeEntry scopeEntry = currentScope.lookupScopeEntry(variableIdentifierExpr.getName()); if (scopeEntry != null && scopeEntry.hasVariableDeclInfo()) { // If this is a global, mark it as used. unusedGlobals.remove(scopeEntry.getVariableDeclInfo()); } }
private int getWidth(ScopeEntry entry) { return ((BasicType) entry.getType().getWithoutQualifiers()).getNumElements(); }