1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
|
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Diagnostics.Contracts;
using Microsoft.Boogie;
namespace GPUVerify
{
class KernelDualiser
{
private GPUVerifier verifier;
public KernelDualiser(GPUVerifier verifier)
{
this.verifier = verifier;
}
private string procName = null;
internal void DualiseProcedure(Microsoft.Boogie.Procedure proc)
{
procName = proc.Name;
proc.Requires = DualiseRequires(proc.Requires);
proc.Ensures = DualiseEnsures(proc.Ensures);
proc.InParams = DualiseVariableSequence(proc.InParams);
proc.OutParams = DualiseVariableSequence(proc.OutParams);
procName = null;
}
private RequiresSeq DualiseRequires(RequiresSeq requiresSeq)
{
RequiresSeq newRequires = new RequiresSeq();
foreach (Requires r in requiresSeq)
{
newRequires.Add(new Requires(r.Free, new VariableDualiser(1, verifier.uniformityAnalyser, procName).
VisitExpr(r.Condition.Clone() as Expr)));
if (!ContainsAsymmetricExpression(r.Condition)
&& !verifier.uniformityAnalyser.IsUniform(procName, r.Condition))
{
newRequires.Add(new Requires(r.Free, new VariableDualiser(2, verifier.uniformityAnalyser, procName).
VisitExpr(r.Condition.Clone() as Expr)));
}
}
return newRequires;
}
private EnsuresSeq DualiseEnsures(EnsuresSeq ensuresSeq)
{
EnsuresSeq newEnsures = new EnsuresSeq();
foreach (Ensures e in ensuresSeq)
{
newEnsures.Add(new Ensures(e.Free, new VariableDualiser(1, verifier.uniformityAnalyser, procName).
VisitExpr(e.Condition.Clone() as Expr)));
if (!ContainsAsymmetricExpression(e.Condition)
&& !verifier.uniformityAnalyser.IsUniform(procName, e.Condition))
{
newEnsures.Add(new Ensures(e.Free, new VariableDualiser(2, verifier.uniformityAnalyser, procName).
VisitExpr(e.Condition.Clone() as Expr)));
}
}
return newEnsures;
}
private StmtList MakeDual(StmtList stmtList)
{
Contract.Requires(stmtList != null);
StmtList result = new StmtList(new List<BigBlock>(), stmtList.EndCurly);
foreach (BigBlock bodyBlock in stmtList.BigBlocks)
{
result.BigBlocks.Add(MakeDual(bodyBlock));
}
return result;
}
private void MakeDual(CmdSeq cs, Cmd c)
{
if (c is CallCmd)
{
CallCmd Call = c as CallCmd;
List<Expr> uniformNewIns = new List<Expr>();
List<Expr> nonUniformNewIns = new List<Expr>();
for (int i = 0; i < Call.Ins.Count; i++)
{
if (verifier.uniformityAnalyser.knowsOf(Call.callee) && verifier.uniformityAnalyser.IsUniform(Call.callee, verifier.uniformityAnalyser.GetInParameter(Call.callee, i)))
{
uniformNewIns.Add(Call.Ins[i]);
}
else
{
nonUniformNewIns.Add(new VariableDualiser(1, verifier.uniformityAnalyser, procName).VisitExpr(Call.Ins[i]));
}
}
for (int i = 0; i < Call.Ins.Count; i++)
{
if (!(verifier.uniformityAnalyser.knowsOf(Call.callee) && verifier.uniformityAnalyser.IsUniform(Call.callee, verifier.uniformityAnalyser.GetInParameter(Call.callee, i))))
{
nonUniformNewIns.Add(new VariableDualiser(2, verifier.uniformityAnalyser, procName).VisitExpr(Call.Ins[i]));
}
}
List<Expr> newIns = uniformNewIns;
newIns.AddRange(nonUniformNewIns);
List<IdentifierExpr> uniformNewOuts = new List<IdentifierExpr>();
List<IdentifierExpr> nonUniformNewOuts = new List<IdentifierExpr>();
for (int i = 0; i < Call.Outs.Count; i++)
{
if (verifier.uniformityAnalyser.knowsOf(Call.callee) && verifier.uniformityAnalyser.IsUniform(Call.callee, verifier.uniformityAnalyser.GetOutParameter(Call.callee, i)))
{
uniformNewOuts.Add(Call.Outs[i]);
}
else
{
nonUniformNewOuts.Add(new VariableDualiser(1, verifier.uniformityAnalyser, procName).VisitIdentifierExpr(Call.Outs[i].Clone() as IdentifierExpr) as IdentifierExpr);
}
}
for (int i = 0; i < Call.Outs.Count; i++)
{
if (!(verifier.uniformityAnalyser.knowsOf(Call.callee) && verifier.uniformityAnalyser.IsUniform(Call.callee, verifier.uniformityAnalyser.GetOutParameter(Call.callee, i))))
{
nonUniformNewOuts.Add(new VariableDualiser(2, verifier.uniformityAnalyser, procName).VisitIdentifierExpr(Call.Outs[i].Clone() as IdentifierExpr) as IdentifierExpr);
}
}
List<IdentifierExpr> newOuts = uniformNewOuts;
newOuts.AddRange(nonUniformNewOuts);
CallCmd NewCallCmd = new CallCmd(Call.tok, Call.callee, newIns, newOuts);
NewCallCmd.Proc = Call.Proc;
cs.Add(NewCallCmd);
}
else if (c is AssignCmd)
{
AssignCmd assign = c as AssignCmd;
if (assign.Lhss.All(lhs =>
lhs is SimpleAssignLhs &&
verifier.uniformityAnalyser.IsUniform(procName, (lhs as SimpleAssignLhs).AssignedVariable.Name)))
{
cs.Add(assign);
}
else
{
List<AssignLhs> newLhss = assign.Lhss.SelectMany(lhs => new AssignLhs[] {
new VariableDualiser(1, verifier.uniformityAnalyser, procName).Visit(lhs.Clone() as AssignLhs) as AssignLhs,
new VariableDualiser(2, verifier.uniformityAnalyser, procName).Visit(lhs.Clone() as AssignLhs) as AssignLhs
}).ToList();
List<Expr> newRhss = assign.Rhss.SelectMany(rhs => new Expr[] {
new VariableDualiser(1, verifier.uniformityAnalyser, procName).VisitExpr(rhs.Clone() as Expr),
new VariableDualiser(2, verifier.uniformityAnalyser, procName).VisitExpr(rhs.Clone() as Expr)
}).ToList();
AssignCmd newAssign = new AssignCmd(assign.tok, newLhss, newRhss);
cs.Add(newAssign);
}
}
else if (c is HavocCmd)
{
HavocCmd havoc = c as HavocCmd;
Debug.Assert(havoc.Vars.Length == 1);
HavocCmd newHavoc;
newHavoc = new HavocCmd(havoc.tok, new IdentifierExprSeq(new IdentifierExpr[] {
(IdentifierExpr)(new VariableDualiser(1, verifier.uniformityAnalyser, procName).VisitIdentifierExpr(havoc.Vars[0].Clone() as IdentifierExpr)),
(IdentifierExpr)(new VariableDualiser(2, verifier.uniformityAnalyser, procName).VisitIdentifierExpr(havoc.Vars[0].Clone() as IdentifierExpr))
}));
cs.Add(newHavoc);
}
else if (c is AssertCmd)
{
AssertCmd ass = c as AssertCmd;
cs.Add(new AssertCmd(c.tok, new VariableDualiser(1, verifier.uniformityAnalyser, procName).VisitExpr(ass.Expr.Clone() as Expr), ass.Attributes));
if (!ContainsAsymmetricExpression(ass.Expr))
{
cs.Add(new AssertCmd(c.tok, new VariableDualiser(2, verifier.uniformityAnalyser, procName).VisitExpr(ass.Expr.Clone() as Expr), ass.Attributes));
}
}
else if (c is AssumeCmd)
{
AssumeCmd ass = c as AssumeCmd;
if (QKeyValue.FindBoolAttribute(ass.Attributes, "backedge"))
{
cs.Add(new AssumeCmd(c.tok, Expr.Or(new VariableDualiser(1, verifier.uniformityAnalyser, procName).VisitExpr(ass.Expr.Clone() as Expr),
new VariableDualiser(2, verifier.uniformityAnalyser, procName).VisitExpr(ass.Expr.Clone() as Expr))));
}
else
{
cs.Add(new AssumeCmd(c.tok, new VariableDualiser(1, verifier.uniformityAnalyser, procName).VisitExpr(ass.Expr.Clone() as Expr)));
if (!ContainsAsymmetricExpression(ass.Expr))
{
cs.Add(new AssumeCmd(c.tok, new VariableDualiser(2, verifier.uniformityAnalyser, procName).VisitExpr(ass.Expr.Clone() as Expr)));
}
}
}
else
{
Debug.Assert(false);
}
}
private BigBlock MakeDual(BigBlock bb)
{
// Not sure what to do about the transfer command
BigBlock result = new BigBlock(bb.tok, bb.LabelName, new CmdSeq(), null, bb.tc);
foreach (Cmd c in bb.simpleCmds)
{
MakeDual(result.simpleCmds, c);
}
if (bb.ec is WhileCmd)
{
Expr NewGuard;
if (verifier.uniformityAnalyser.IsUniform(procName, (bb.ec as WhileCmd).Guard))
{
NewGuard = (bb.ec as WhileCmd).Guard;
}
else
{
NewGuard = Expr.Or(Dualise((bb.ec as WhileCmd).Guard, 1),
Dualise((bb.ec as WhileCmd).Guard, 2)
);
}
result.ec = new WhileCmd(bb.ec.tok,
NewGuard,
MakeDualInvariants((bb.ec as WhileCmd).Invariants), MakeDual((bb.ec as WhileCmd).Body));
}
else if (bb.ec is IfCmd)
{
Debug.Assert(verifier.uniformityAnalyser.IsUniform(procName, (bb.ec as IfCmd).Guard));
result.ec = new IfCmd(bb.ec.tok,
(bb.ec as IfCmd).Guard,
MakeDual((bb.ec as IfCmd).thn),
null,
(bb.ec as IfCmd).elseBlock == null ? null : MakeDual((bb.ec as IfCmd).elseBlock));
}
else if (bb.ec is BreakCmd)
{
result.ec = bb.ec;
}
else
{
Debug.Assert(bb.ec == null);
}
return result;
}
private Block MakeDual(Block b)
{
var newCmds = new CmdSeq();
foreach (Cmd c in b.Cmds)
{
MakeDual(newCmds, c);
}
b.Cmds = newCmds;
return b;
}
private List<PredicateCmd> MakeDualInvariants(List<PredicateCmd> originalInvariants)
{
List<PredicateCmd> result = new List<PredicateCmd>();
foreach (PredicateCmd p in originalInvariants)
{
{
PredicateCmd newP = new AssertCmd(p.tok,
Dualise(p.Expr, 1));
newP.Attributes = p.Attributes;
result.Add(newP);
}
if (!ContainsAsymmetricExpression(p.Expr)
&& !verifier.uniformityAnalyser.IsUniform(procName, p.Expr))
{
PredicateCmd newP = new AssertCmd(p.tok, Dualise(p.Expr, 2));
newP.Attributes = p.Attributes;
result.Add(newP);
}
}
return result;
}
private void MakeDualLocalVariables(Implementation impl)
{
VariableSeq NewLocalVars = new VariableSeq();
foreach (LocalVariable v in impl.LocVars)
{
if (verifier.uniformityAnalyser.IsUniform(procName, v.Name))
{
NewLocalVars.Add (v);
}
else
{
NewLocalVars.Add(
new VariableDualiser(1, verifier.uniformityAnalyser, procName).VisitVariable(v.Clone() as Variable));
NewLocalVars.Add(
new VariableDualiser(2, verifier.uniformityAnalyser, procName).VisitVariable(v.Clone() as Variable));
}
}
impl.LocVars = NewLocalVars;
}
private bool ContainsAsymmetricExpression(Expr expr)
{
AsymmetricExpressionFinder finder = new AsymmetricExpressionFinder();
finder.VisitExpr(expr);
return finder.foundAsymmetricExpr();
}
private VariableSeq DualiseVariableSequence(VariableSeq seq)
{
VariableSeq uniform = new VariableSeq();
VariableSeq nonuniform = new VariableSeq();
foreach (Variable v in seq)
{
if (verifier.uniformityAnalyser.IsUniform(procName, v.Name))
{
uniform.Add(v);
}
else
{
nonuniform.Add(new VariableDualiser(1, verifier.uniformityAnalyser, procName).VisitVariable((Variable)v.Clone()));
}
}
foreach (Variable v in seq)
{
if (!verifier.uniformityAnalyser.IsUniform(procName, v.Name))
{
nonuniform.Add(new VariableDualiser(2, verifier.uniformityAnalyser, procName).VisitVariable((Variable)v.Clone()));
}
}
VariableSeq result = uniform;
result.AddRange(nonuniform);
return result;
}
internal void DualiseImplementation(Implementation impl, bool unstructured)
{
procName = impl.Name;
impl.InParams = DualiseVariableSequence(impl.InParams);
impl.OutParams = DualiseVariableSequence(impl.OutParams);
MakeDualLocalVariables(impl);
if (unstructured)
impl.Blocks = new List<Block>(impl.Blocks.Select(MakeDual));
else
impl.StructuredStmts = MakeDual(impl.StructuredStmts);
procName = null;
}
private Expr Dualise(Expr expr, int thread)
{
return new VariableDualiser(thread, verifier.uniformityAnalyser, procName).VisitExpr(expr.Clone() as Expr);
}
}
}
|