Skip to content

Commit

Permalink
Added new nondet_loop.c code + unit test.
Browse files Browse the repository at this point in the history
Moved IntrinsicsInlining to after Unrolling
Minor cleanup
  • Loading branch information
ThomasHaas committed Jul 13, 2023
1 parent 5cdf539 commit a56e373
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 13 deletions.
17 changes: 17 additions & 0 deletions benchmarks/c/miscellaneous/nondet_loop.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#include <stdint.h>
#include <assert.h>

int main()
{
int x = 0;
while (x != 5) {
if (__VERIFIER_nondet_int() == 0) {
x += 2;
} else {
x += 3;
}
}
assert (0);

return 0;
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,7 @@ public void run(Program program) {
}

private void run(Thread thread) {
for (Event event : List.copyOf(thread.getEvents())) {
if (!(event instanceof DirectFunctionCall call)) {
continue;
}
for (DirectFunctionCall call : thread.getEvents(DirectFunctionCall.class)) {
assert !call.getCallTarget().hasBody();

List<Event> replacement = switch (call.getCallTarget().getName()) {
Expand All @@ -59,8 +56,8 @@ private void run(Thread thread) {
String.format("Undefined function %s", call.getCallTarget().getName()));
};

call.insertAfter(replacement);
call.forceDelete();
replacement.forEach(e -> e.copyAllMetadataFrom(call));
call.replaceBy(replacement);
}
}

Expand All @@ -70,9 +67,10 @@ private List<Event> inlineNonDet(DirectFunctionCall call) {
assert call instanceof DirectValueFunctionCall;
Register register = ((DirectValueFunctionCall) call).getResultRegister();
String name = call.getCallTarget().getName();
String prefix = name.startsWith("__V") ? "__VERIFIER_nondet_" : "__SMACK_nondet_";
assert name.startsWith(prefix);
String suffix = call.getCallTarget().getName().substring(prefix.length());
final String separator = "nondet_";
int index = name.indexOf(separator);
assert index > -1;
String suffix = name.substring(index + separator.length());

// Nondeterministic booleans
if (suffix.equals("bool")) {
Expand Down Expand Up @@ -109,9 +107,9 @@ private List<Event> inlineNonDet(DirectFunctionCall call) {
throw new ParsingException(String.format("Non-integer result register %s.", register));
}
var expression = new INonDet(constantId++, type, signed);
call.getFunction().getProgram().addConstant(expression);
expression.setMin(min);
expression.setMax(max);
call.getFunction().getProgram().addConstant(expression);
return List.of(EventFactory.newLocal(register, expression));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,6 @@ private ProcessingManager(Configuration config) throws InvalidConfigurationExcep
StaticMemoryInitializer.newInstance(),
Inlining.fromConfig(config),
ThreadCreation.fromConfig(config),
IntrinsicsInlining.fromConfig(config),
UnreachableCodeElimination.fromConfig(config),
ComplexBlockSplitting.newInstance(),
BranchReordering.fromConfig(config),
Expand All @@ -91,6 +90,7 @@ private ProcessingManager(Configuration config) throws InvalidConfigurationExcep
SimpleSpinLoopDetection.fromConfig(config),
LoopUnrolling.fromConfig(config),
printAfterUnrolling ? DebugPrint.withHeader("After loop unrolling") : null,
IntrinsicsInlining.fromConfig(config),
dynamicPureLoopCutting ? DynamicPureLoopCutting.fromConfig(config) : null,
constantPropagation ? SparseConditionalConstantPropagation.fromConfig(config) : null,
dce ? DeadAssignmentElimination.fromConfig(config) : null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import java.util.stream.Collectors;

/*
This pass finds and marks simple loops that are totally side effect free (simple spin loops).
This pass finds and marks simple loops that are totally side-effect-free (simple spin loops).
It will also mark side-effect-full loops if they are annotated by a SpinStart event
(we assume the user guarantees the correctness of the annotation)
Expand All @@ -30,6 +30,9 @@ The pass is unable to detect complex types of loops that may spin endlessly (i.e
TODO: Instead of tagging labels as spinning and checking for that tag during loop unrolling
we could let this pass produce LoopBound-Annotations to guide the unrolling implicitly.
TODO 2: Intrinsic calls need to get special treatment as they might be side-effect-full
(for now, all our intrinsics are side-effect-free, so it works fine).
*/
public class SimpleSpinLoopDetection implements ProgramProcessor {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@ public static Iterable<Object[]> data() throws IOException {
{"thread_inlining", IMM, PASS, 1},
{"thread_inlining_complex", IMM, PASS, 1},
{"thread_inlining_complex_2", IMM, PASS, 1},
{"thread_local", IMM, PASS, 1}
{"thread_local", IMM, PASS, 1},
{"nondet_loop", IMM, FAIL, 3}
});
}

Expand Down

0 comments on commit a56e373

Please sign in to comment.