Skip to content

Commit 3a92996

Browse files
ThomasHaashernanponcedeleon
authored andcommitted
More cleanup.
More efficient TBAA alias check.
1 parent 76d01bc commit 3a92996

File tree

2 files changed

+63
-48
lines changed

2 files changed

+63
-48
lines changed

dartagnan/src/main/java/com/dat3m/dartagnan/parsers/program/visitors/VisitorLlvm.java

Lines changed: 41 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -310,39 +310,34 @@ private List<Metadata> parseMetadataAttachment(List<MetadataAttachmentContext> m
310310
}
311311

312312
final List<Metadata> metadata = new ArrayList<>();
313-
//FIXME: This code only looks for DILocation metadata,
314-
// and it only extracts the information needed to construct SourceLocation metadata
315313
for (MetadataAttachmentContext metadataCtx: metadataAttachmentContexts) {
316314
MdNode mdNode = (MdNode) metadataCtx.accept(this);
317315
assert mdNode instanceof MdReference;
318316
mdNode = metadataSymbolTable.get(((MdReference) mdNode).mdName());
319317

320318
final String metaDatatype = metadataCtx.MetadataName().toString();
321-
322319
if (metaDatatype.equals("!tbaa")) {
323-
// We assume a tbaa access tag
324-
assert mdNode instanceof MdTuple;
325-
final MdTuple accessNode = (MdTuple) mdNode;
326-
final List<MdNode> children = accessNode.mdFields();
327-
328-
final TBAA.Type base = tbaaBuilder.getTBAATypeFromNode(children.get(0));
329-
final TBAA.Type access = tbaaBuilder.getTBAATypeFromNode(children.get(1));
330-
final int offset = ((MdGenericValue<IntLiteral>)children.get(2)).value().getValueAsInt();
331-
// final int isConstant = ((MdGenericValue<IntLiteral>)children.get(3)).value().getValueAsInt();
332-
333-
metadata.add(new TBAA.AccessTag(base, access, offset));
334-
}
335-
336-
if (mdNode instanceof SpecialMdTupleNode diLocationNode && diLocationNode.nodeType() == SpecialMdTupleNode.Type.DILocation) {
337-
SpecialMdTupleNode scope = diLocationNode;
338-
while (scope.getField("scope").isPresent()) {
339-
scope = (SpecialMdTupleNode) metadataSymbolTable.get(scope.<MdReference>getField("scope").orElseThrow().mdName());
320+
// TBAA
321+
metadata.add(tbaaBuilder.getTBAAAccessFromNode(mdNode));
322+
} else if (metaDatatype.equals("!dbg")) {
323+
//FIXME: This code only looks for DILocation metadata
324+
// and it only extracts the information needed to construct SourceLocation metadata
325+
if (mdNode instanceof SpecialMdTupleNode diLocationNode && diLocationNode.nodeType() == SpecialMdTupleNode.Type.DILocation) {
326+
SpecialMdTupleNode scope = diLocationNode;
327+
while (scope.getField("scope").isPresent()) {
328+
scope = (SpecialMdTupleNode) metadataSymbolTable.get(scope.<MdReference>getField("scope").orElseThrow().mdName());
329+
}
330+
assert scope.nodeType() == SpecialMdTupleNode.Type.DIFile;
331+
final String filename = scope.<MdGenericValue<String>>getField("filename").orElseThrow().value();
332+
final String directory = scope.<MdGenericValue<String>>getField("directory").orElseThrow().value();
333+
final int lineNumber = diLocationNode.<MdGenericValue<BigInteger>>getField("line").orElseThrow().value().intValue();
334+
metadata.add(new SourceLocation((directory + "/" + filename).intern(), lineNumber));
335+
}
336+
} else {
337+
if (logger.isDebugEnabled()) {
338+
final String optional = !metaDatatype.equals("!llvm.loop") ? " Potential for optimization?" : "";
339+
logger.debug("Skipped parsing of {} metadata.{}", metaDatatype, optional);
340340
}
341-
assert scope.nodeType() == SpecialMdTupleNode.Type.DIFile;
342-
final String filename = scope.<MdGenericValue<String>>getField("filename").orElseThrow().value();
343-
final String directory = scope.<MdGenericValue<String>>getField("directory").orElseThrow().value();
344-
final int lineNumber = diLocationNode.<MdGenericValue<BigInteger>>getField("line").orElseThrow().value().intValue();
345-
metadata.add(new SourceLocation((directory + "/" + filename).intern(), lineNumber));
346341
}
347342
}
348343

@@ -1487,6 +1482,27 @@ public <T extends MdNode> Optional<T> getField(String fieldName) {
14871482

14881483
private class TBAABuilder {
14891484
private final Map<MdNode, TBAA.Type> md2tbaaMap = new HashMap<>();
1485+
private final Map<MdNode, TBAA.AccessTag> md2accessMap = new HashMap<>();
1486+
1487+
private TBAA.AccessTag getTBAAAccessFromNode(MdNode node) {
1488+
if (node instanceof MdReference ref) {
1489+
node = metadataSymbolTable.get(ref.mdName());
1490+
}
1491+
assert node instanceof MdTuple;
1492+
final MdTuple accessNode = (MdTuple) node;
1493+
1494+
if (!md2accessMap.containsKey(accessNode)) {
1495+
// We assume a tbaa access tag
1496+
final List<MdNode> children = accessNode.mdFields();
1497+
final TBAA.Type base = tbaaBuilder.getTBAATypeFromNode(children.get(0));
1498+
final TBAA.Type access = tbaaBuilder.getTBAATypeFromNode(children.get(1));
1499+
final int offset = ((MdGenericValue<IntLiteral>) children.get(2)).value().getValueAsInt();
1500+
// final int isConstant = ((MdGenericValue<IntLiteral>)children.get(3)).value().getValueAsInt();
1501+
md2accessMap.put(node, new TBAA.AccessTag(base, access, offset));
1502+
}
1503+
1504+
return md2accessMap.get(node);
1505+
}
14901506

14911507
private TBAA.Type getTBAATypeFromNode(MdNode node) {
14921508
if (node instanceof MdReference ref) {

dartagnan/src/main/java/com/dat3m/dartagnan/program/analysis/alias/TBAA.java

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@
77
import java.util.List;
88
import java.util.stream.Collectors;
99

10+
/*
11+
Type-based alias analysis that relies on type annotations provided by metadata (e.g., from LLVM).
12+
This analysis does NOT(!) use the IR's internal type system.
13+
*/
1014
public final class TBAA implements AliasAnalysis {
1115

1216
private TBAA() {}
@@ -31,31 +35,26 @@ public boolean mayAlias(MemoryCoreEvent a, MemoryCoreEvent b) {
3135
// Helper methods
3236

3337
public static boolean canAlias(AccessTag x, AccessTag y) {
38+
if (x == y) {
39+
return true;
40+
}
3441
final TypeOffset xAccess = new TypeOffset(x.base(), x.offset());
3542
final TypeOffset yAccess = new TypeOffset(y.base(), y.offset());
3643

37-
TypeOffset xCheck = xAccess;
38-
while (xCheck != null) {
39-
if (xCheck.equals(yAccess)) {
40-
return true;
41-
}
42-
xCheck = getImmediateParent(xCheck);
43-
}
44-
45-
TypeOffset yCheck = yAccess;
46-
while (yCheck != null) {
47-
if (yCheck.equals(xAccess)) {
48-
return true;
49-
}
50-
yCheck = getImmediateParent(yCheck);
44+
final TypeOffset upper = x.offset() <= y.offset() ? xAccess : yAccess;
45+
TypeOffset lower = (upper == xAccess) ? yAccess : xAccess;
46+
while (lower.offset() > upper.offset()) {
47+
lower = getImmediateParent(lower);
48+
assert lower != null;
5149
}
5250

53-
return false;
51+
return lower.equals(upper);
5452
}
5553

5654
public static TypeOffset getImmediateParent(TypeOffset typeOffset) {
5755
if (typeOffset.type() instanceof ScalarType scalarType) {
58-
return scalarType.parent() == null ? null: new TypeOffset(scalarType.parent(), typeOffset.offset());
56+
final boolean isRoot = scalarType.parent() == null;
57+
return isRoot ? null : new TypeOffset(scalarType.parent(), typeOffset.offset());
5958
} else if (typeOffset.type() instanceof StructType structType) {
6059
for (int i = 0; i < structType.offsets().size(); i++) {
6160
final TypeOffset cur = structType.offsets().get(i);
@@ -92,17 +91,17 @@ public String toString() {
9291
}
9392

9493
public record ScalarType(String name, Type parent) implements Type {
94+
@Override
95+
public String getName() {
96+
return name;
97+
}
98+
9599
@Override
96100
public String toString() {
97101
if (parent == null) {
98102
return name;
99103
}
100-
return String.format("{ %s, %s }", name, parent.getName());
101-
}
102-
103-
@Override
104-
public String getName() {
105-
return name;
104+
return String.format("TBAA.Scalar{ %s, %s }", name, parent.getName());
106105
}
107106
}
108107

@@ -114,7 +113,7 @@ public String getName() {
114113

115114
@Override
116115
public String toString() {
117-
return String.format("{ %s, %s }", name,
116+
return String.format("TBAA.Struct{ %s, %s }", name,
118117
offsets.stream().map(Object::toString).collect(Collectors.joining(", ")));
119118
}
120119
}

0 commit comments

Comments
 (0)