use of com.dat3m.dartagnan.program.event.core.Store in project Dat3M by hernanponcedeleon.
the class CoSymmetryBreaking method computeMustEdges.
/*
Computes co must-edges that are implied by this symmetry breaking
May return an empty list.
*/
public List<Tuple> computeMustEdges() {
List<Tuple> mustEdges = new ArrayList<>();
for (Info info : infoMap.values()) {
if (!info.hasMustEdges) {
continue;
}
Store w = info.writes.get(0);
final int numThreads = info.threads.size();
for (int i = 0; i < numThreads; i++) {
for (int j = i + 1; j < numThreads; j++) {
mustEdges.add(new Tuple(symm.map(w, info.threads.get(i)), symm.map(w, info.threads.get(j))));
}
}
}
return mustEdges;
}
use of com.dat3m.dartagnan.program.event.core.Store in project Dat3M by hernanponcedeleon.
the class CoSymmetryBreaking method encode.
public BooleanFormula encode(EquivalenceClass<Thread> symmClass, SolverContext ctx) {
BooleanFormulaManager bmgr = ctx.getFormulaManager().getBooleanFormulaManager();
BooleanFormula enc = bmgr.makeTrue();
if (symmClass.getEquivalence() != symm) {
return enc;
}
Info info = infoMap.get(symmClass);
if (info == null) {
logger.warn("Cannot encode co-symmetry because no information has been computed. " + "Make sure that <initialize> gets called before encoding.");
return enc;
}
List<Thread> symmThreads = info.threads;
// ============= Construct rows =============
Thread t1 = symmThreads.get(0);
List<Thread> otherThreads = symmThreads.subList(1, symmThreads.size());
List<Tuple> r1Tuples = new ArrayList<>();
for (Store w : info.writes) {
for (Thread t2 : otherThreads) {
r1Tuples.add(new Tuple(w, symm.map(w, t2)));
}
}
// Starting row
List<BooleanFormula> r1 = new ArrayList<>(r1Tuples.size() + 1);
if (info.hasMustEdges) {
r1.add(info.writes.get(0).exec());
}
r1.addAll(Lists.transform(r1Tuples, t -> co.getSMTVar(t, ctx)));
// Construct symmetric rows
Thread rep = symmClass.getRepresentative();
for (int i = 1; i < symmThreads.size(); i++) {
Thread t2 = symmThreads.get(i);
Function<Event, Event> p = symm.createTransposition(t1, t2);
List<Tuple> r2Tuples = r1Tuples.stream().map(t -> t.permute(p)).collect(Collectors.toList());
List<BooleanFormula> r2 = new ArrayList<>(r2Tuples.size() + 1);
if (info.hasMustEdges) {
r2.add(symm.map(info.writes.get(0), t2).exec());
}
r2.addAll(Lists.transform(r2Tuples, t -> co.getSMTVar(t, ctx)));
final String id = "_" + rep.getId() + "_" + i;
// NOTE: We want to have r1 >= r2 but lexLeader encodes r1 <= r2, so we swap r1 and r2.
enc = bmgr.and(enc, SymmetryEncoder.encodeLexLeader(id, r2, r1, ctx));
t1 = t2;
r1Tuples = r2Tuples;
r1 = r2;
}
return enc;
}
use of com.dat3m.dartagnan.program.event.core.Store in project Dat3M by hernanponcedeleon.
the class WitnessBuilder method build.
public WitnessGraph build() {
for (Thread t : task.getProgram().getThreads()) {
for (Event e : t.getEntry().getSuccessors()) {
eventThreadMap.put(e, t.getId() - 1);
}
}
WitnessGraph graph = new WitnessGraph();
graph.addAttribute(UNROLLBOUND.toString(), valueOf(task.getProgram().getUnrollingBound()));
graph.addAttribute(WITNESSTYPE.toString(), type + "_witness");
graph.addAttribute(SOURCECODELANG.toString(), "C");
graph.addAttribute(PRODUCER.toString(), "Dartagnan");
graph.addAttribute(SPECIFICATION.toString(), "CHECK( init(main()), LTL(G ! call(reach_error())))");
graph.addAttribute(PROGRAMFILE.toString(), originalProgramFilePath);
graph.addAttribute(PROGRAMHASH.toString(), getFileSHA256(new File(originalProgramFilePath)));
graph.addAttribute(ARCHITECTURE.toString(), "32bit");
DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
df.setTimeZone(TimeZone.getTimeZone("UTC"));
// "If the timestamp is in UTC time, it ends with a 'Z'."
// https://github.com/sosy-lab/sv-witnesses/blob/main/README-GraphML.md
graph.addAttribute(CREATIONTIME.toString(), df.format(new Date()) + "Z");
Node v0 = new Node("N0");
v0.addAttribute("entry", "true");
Node v1 = new Node("N1");
Node v2 = new Node("N2");
Edge edge = new Edge(v0, v1);
edge.addAttribute(CREATETHREAD.toString(), "0");
graph.addEdge(edge);
edge = new Edge(v1, v2);
edge.addAttribute(THREADID.toString(), "0");
edge.addAttribute(ENTERFUNCTION.toString(), "main");
graph.addEdge(edge);
int nextNode = 2;
int threads = 1;
if (type.equals("correctness")) {
return graph;
}
try (Model model = prover.getModel()) {
List<Event> execution = reOrderBasedOnAtomicity(task.getProgram(), getSCExecutionOrder(model));
for (int i = 0; i < execution.size(); i++) {
Event e = execution.get(i);
if (i + 1 < execution.size()) {
Event next = execution.get(i + 1);
if (e.getCLine() == next.getCLine() && e.getThread() == next.getThread()) {
continue;
}
}
edge = new Edge(new Node("N" + nextNode), new Node("N" + (nextNode + 1)));
edge.addAttribute(THREADID.toString(), valueOf(eventThreadMap.get(e)));
edge.addAttribute(STARTLINE.toString(), valueOf(e.getCLine()));
// CLines and thus won't create an edge (as expected)
if (e.hasFilter(WRITE) && e.hasFilter(PTHREAD)) {
edge.addAttribute(CREATETHREAD.toString(), valueOf(threads));
threads++;
}
if (e instanceof Load) {
RegWriter l = (RegWriter) e;
edge.addAttribute(EVENTID.toString(), valueOf(e.getUId()));
edge.addAttribute(LOADEDVALUE.toString(), l.getWrittenValue(e, model, ctx).toString());
}
if (e instanceof Store) {
Store s = (Store) e;
edge.addAttribute(EVENTID.toString(), valueOf(e.getUId()));
edge.addAttribute(STOREDVALUE.toString(), s.getMemValue().getIntValue(s, model, ctx).toString());
}
graph.addEdge(edge);
nextNode++;
if (e.hasFilter(Tag.ASSERTION)) {
break;
}
}
} catch (SolverException ignore) {
// The if above guarantees that if we reach this try, a Model exists
}
graph.getNode("N" + nextNode).addAttribute("violation", "true");
return graph;
}
use of com.dat3m.dartagnan.program.event.core.Store in project Dat3M by hernanponcedeleon.
the class WitnessGraph method encode.
public BooleanFormula encode(Program program, SolverContext ctx) {
BooleanFormulaManager bmgr = ctx.getFormulaManager().getBooleanFormulaManager();
IntegerFormulaManager imgr = ctx.getFormulaManager().getIntegerFormulaManager();
BooleanFormula enc = bmgr.makeTrue();
List<Event> previous = new ArrayList<>();
for (Edge edge : edges.stream().filter(Edge::hasCline).collect(Collectors.toList())) {
List<Event> events = program.getCache().getEvents(FilterBasic.get(MEMORY)).stream().filter(e -> e.getCLine() == edge.getCline()).collect(Collectors.toList());
if (!previous.isEmpty() && !events.isEmpty()) {
enc = bmgr.and(enc, bmgr.or(Lists.cartesianProduct(previous, events).stream().map(p -> edge("hb", p.get(0), p.get(1), ctx)).toArray(BooleanFormula[]::new)));
}
if (!events.isEmpty()) {
previous = events;
}
if (edge.hasAttributed(EVENTID.toString()) && edge.hasAttributed(LOADEDVALUE.toString())) {
int id = Integer.parseInt(edge.getAttributed(EVENTID.toString()));
if (program.getCache().getEvents(FilterBasic.get(READ)).stream().anyMatch(e -> e.getUId() == id)) {
Load load = (Load) program.getCache().getEvents(FilterBasic.get(READ)).stream().filter(e -> e.getUId() == id).findFirst().get();
BigInteger value = new BigInteger(edge.getAttributed(LOADEDVALUE.toString()));
enc = bmgr.and(enc, generalEqual(load.getResultRegisterExpr(), imgr.makeNumber(value), ctx));
}
}
if (edge.hasAttributed(EVENTID.toString()) && edge.hasAttributed(STOREDVALUE.toString())) {
int id = Integer.parseInt(edge.getAttributed(EVENTID.toString()));
if (program.getCache().getEvents(FilterBasic.get(WRITE)).stream().anyMatch(e -> e.getUId() == id)) {
Store store = (Store) program.getCache().getEvents(FilterBasic.get(WRITE)).stream().filter(e -> e.getUId() == id).findFirst().get();
BigInteger value = new BigInteger(edge.getAttributed(STOREDVALUE.toString()));
enc = bmgr.and(enc, generalEqual(store.getMemValueExpr(), imgr.makeNumber(value), ctx));
}
}
}
return enc;
}
use of com.dat3m.dartagnan.program.event.core.Store in project Dat3M by hernanponcedeleon.
the class RelRf method atomicBlockOptimization.
private void atomicBlockOptimization() {
// TODO: This function can not only reduce rf-edges
// but we could also figure out implied coherences:
// Assume w1 and w2 are aliasing in the same block and w1 is before w2,
// then if w1 is co-before some external w3, then so is w2, i.e.
// co(w1, w3) => co(w2, w3), but we also have co(w2, w3) => co(w1, w3)
// so co(w1, w3) <=> co(w2, w3).
// This information is not expressible in terms of min/must sets, but
// we could still encode it.
int sizeBefore = maxTupleSet.size();
// Atomics blocks: BeginAtomic -> EndAtomic
ExecutionAnalysis exec = analysisContext.get(ExecutionAnalysis.class);
AliasAnalysis alias = analysisContext.get(AliasAnalysis.class);
FilterAbstract filter = FilterIntersection.get(FilterBasic.get(RMW), FilterBasic.get(SVCOMP.SVCOMPATOMIC));
for (Event end : task.getProgram().getCache().getEvents(filter)) {
// Collect memEvents of the atomic block
List<Store> writes = new ArrayList<>();
List<Load> reads = new ArrayList<>();
EndAtomic endAtomic = (EndAtomic) end;
for (Event b : endAtomic.getBlock()) {
if (b instanceof Load) {
reads.add((Load) b);
} else if (b instanceof Store) {
writes.add((Store) b);
}
}
for (Load r : reads) {
// If there is any write w inside the atomic block that is guaranteed to
// execute before the read and that aliases with it,
// then the read won't be able to read any external writes
boolean hasImpliedWrites = writes.stream().anyMatch(w -> w.getCId() < r.getCId() && exec.isImplied(r, w) && alias.mustAlias(r, w));
if (hasImpliedWrites) {
maxTupleSet.removeIf(t -> t.getSecond() == r && t.isCrossThread());
}
}
}
logger.info("Atomic block optimization eliminated " + (sizeBefore - maxTupleSet.size()) + " reads");
}
Aggregations