Skip to content

Commit

Permalink
Support of Showing Local Events Location in Spirv (#761)
Browse files Browse the repository at this point in the history
Signed-off-by: Tianrui Zheng <tianrui.zheng@huawei.com>
Co-authored-by: Tianrui Zheng <tianrui.zheng@huawei.com>
  • Loading branch information
CapZTr and Tianrui Zheng authored Nov 11, 2024
1 parent ce0ffaa commit 749c055
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,10 @@ public Event visitOpPhi(SpirvParser.OpPhiContext ctx) {
String labelId = pCtx.idRef(1).getText();
String expressionId = pCtx.idRef(0).getText();
cfBuilder.addPhiDefinition(labelId, register, expressionId);
cfBuilder.setPhiId(labelId, register, id);
}
builder.addExpression(id, register);
cfBuilder.setPhiLocation(id);
return null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ public class ControlFlowBuilder {
protected final Map<String, String> mergeLabelIds = new HashMap<>();
protected final Deque<String> blockStack = new ArrayDeque<>();
protected final Map<String, Map<Register, String>> phiDefinitions = new HashMap<>();
protected final Map<String, SourceLocation> phiDefinitionLocations = new HashMap<>();
protected final Map<String, Map<Register, String>> phiDefinitionIds = new HashMap<>();
protected final Map<String, Expression> expressions;
protected SourceLocation currentLocation;

Expand All @@ -38,6 +40,8 @@ public void build() {
phiDefinitions.forEach((blockId, def) ->
def.forEach((k, v) -> {
Event event = EventFactory.newLocal(k, expressions.get(v));
SourceLocation loc = getPhiLocation(blockId, k);
if (loc != null) { event.setMetadata(loc); }
lastBlockEvents.get(blockId).getPredecessor().insertAfter(event);
}));
mergeLabelIds.forEach((jumpLabelId, endLabelId) ->
Expand Down Expand Up @@ -89,6 +93,25 @@ public boolean hasCurrentLocation() {
return currentLocation != null;
}

public void setPhiLocation(String id) {
if (phiDefinitionLocations.containsKey(id)) {
throw new ParsingException("Already set source location for Phi definition %s", id);
}
if (hasCurrentLocation()) {
phiDefinitionLocations.put(id, currentLocation);
}
}

public void setPhiId(String blockId, Register register, String id) {
String phiId = phiDefinitionIds.computeIfAbsent(blockId, k -> new HashMap<>()).get(register);
if (phiId != null) {
throw new ParsingException(
"Already set id %s for the Phi definition in the block %s", phiId, blockId);
} else {
phiDefinitionIds.get(blockId).put(register, id);
}
}

private void validateBeforeBuild() {
if (!blockStack.isEmpty()) {
throw new ParsingException("Unclosed blocks %s", String.join(",", blockStack));
Expand Down Expand Up @@ -118,4 +141,12 @@ private Label createLabel(String id) {
}
return getOrCreateLabel(id);
}

private SourceLocation getPhiLocation(String blockId, Register register) {
String id = phiDefinitionIds.get(blockId).get(register);
if (id != null) {
return phiDefinitionLocations.get(id);
}
return null;
}
}

0 comments on commit 749c055

Please sign in to comment.