Skip to content

Commit 734f4cc

Browse files
committed
fix bug and restore some tests
1 parent 147c818 commit 734f4cc

File tree

2 files changed

+138
-2
lines changed

2 files changed

+138
-2
lines changed

src/analysis/monotone-analyzer-impl.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ inline void MonotoneCFGAnalyzer<L, TxFn>::evaluate() {
4444
for (auto& dep : txfn.getDependents(cfg[i])) {
4545
// If the input state for the dependent block changes, we need to
4646
// re-analyze it.
47-
if (states[i].makeLeastUpperBound(state)) {
47+
if (states[dep.getIndex()].makeLeastUpperBound(state)) {
4848
worklist.push(dep.getIndex());
4949
}
5050
}

test/gtest/cfg.cpp

Lines changed: 137 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,6 @@ TEST_F(CFGTest, CallBlock) {
133133
EXPECT_EQ(ss.str(), cfgText);
134134
}
135135

136-
137136
TEST_F(CFGTest, FinitePowersetLatticeFunctioning) {
138137

139138
std::vector<std::string> initialSet = {"a", "b", "c", "d", "e", "f"};
@@ -275,6 +274,143 @@ TEST_F(CFGTest, LinearReachingDefinitions) {
275274
EXPECT_EQ(expectedResult, getSetses);
276275
}
277276

277+
TEST_F(CFGTest, ReachingDefinitionsIf) {
278+
auto moduleText = R"wasm(
279+
(module
280+
(func $bar
281+
(local $a (i32))
282+
(local $b (i32))
283+
(local.set $a
284+
(i32.const 1)
285+
)
286+
(if
287+
(i32.eq
288+
(local.get $a)
289+
(i32.const 2)
290+
)
291+
(local.set $b
292+
(i32.const 3)
293+
)
294+
(local.set $a
295+
(i32.const 4)
296+
)
297+
)
298+
(drop
299+
(local.get $b)
300+
)
301+
(drop
302+
(local.get $a)
303+
)
304+
)
305+
)
306+
)wasm";
307+
308+
Module wasm;
309+
parseWast(wasm, moduleText);
310+
311+
Function* func = wasm.getFunction("bar");
312+
CFG cfg = CFG::fromFunction(func);
313+
314+
LocalGraph::GetSetses getSetses;
315+
LocalGraph::Locations locations;
316+
ReachingDefinitionsTransferFunction transferFunction(
317+
func, getSetses, locations);
318+
319+
MonotoneCFGAnalyzer<FinitePowersetLattice<LocalSet*>,
320+
ReachingDefinitionsTransferFunction>
321+
analyzer(transferFunction.lattice, transferFunction, cfg);
322+
analyzer.evaluateFunctionEntry(func);
323+
analyzer.evaluateAndCollectResults();
324+
325+
FindAll<LocalSet> foundSets(func->body);
326+
FindAll<LocalGet> foundGets(func->body);
327+
328+
LocalGet* getA1 = foundGets.list[0];
329+
LocalGet* getB = foundGets.list[1];
330+
LocalGet* getA2 = foundGets.list[2];
331+
LocalSet* setA1 = foundSets.list[0];
332+
LocalSet* setB = foundSets.list[1];
333+
LocalSet* setA2 = foundSets.list[2];
334+
335+
LocalGraph::GetSetses expectedResult;
336+
expectedResult[getA1].insert(setA1);
337+
expectedResult[getB].insert(nullptr);
338+
expectedResult[getB].insert(setB);
339+
expectedResult[getA2].insert(setA1);
340+
expectedResult[getA2].insert(setA2);
341+
342+
EXPECT_EQ(expectedResult, getSetses);
343+
}
344+
345+
TEST_F(CFGTest, ReachingDefinitionsLoop) {
346+
auto moduleText = R"wasm(
347+
(module
348+
(func $bar (param $a (i32)) (param $b (i32))
349+
(loop $loop
350+
(drop
351+
(local.get $a)
352+
)
353+
(local.set $a
354+
(i32.add
355+
(i32.const 1)
356+
(local.get $a)
357+
)
358+
)
359+
(br_if $loop
360+
(i32.le_u
361+
(local.get $a)
362+
(i32.const 7)
363+
)
364+
)
365+
)
366+
(local.set $b
367+
(i32.sub
368+
(local.get $b)
369+
(local.get $a)
370+
)
371+
)
372+
)
373+
)
374+
)wasm";
375+
376+
Module wasm;
377+
parseWast(wasm, moduleText);
378+
379+
Function* func = wasm.getFunction("bar");
380+
CFG cfg = CFG::fromFunction(func);
381+
382+
LocalGraph::GetSetses getSetses;
383+
LocalGraph::Locations locations;
384+
ReachingDefinitionsTransferFunction transferFunction(
385+
func, getSetses, locations);
386+
387+
MonotoneCFGAnalyzer<FinitePowersetLattice<LocalSet*>,
388+
ReachingDefinitionsTransferFunction>
389+
analyzer(transferFunction.lattice, transferFunction, cfg);
390+
analyzer.evaluateFunctionEntry(func);
391+
analyzer.evaluateAndCollectResults();
392+
393+
FindAll<LocalSet> foundSets(func->body);
394+
FindAll<LocalGet> foundGets(func->body);
395+
396+
LocalGet* getA1 = foundGets.list[0];
397+
LocalGet* getA2 = foundGets.list[1];
398+
LocalGet* getA3 = foundGets.list[2];
399+
LocalGet* getB = foundGets.list[3];
400+
LocalGet* getA4 = foundGets.list[4];
401+
LocalSet* setA = foundSets.list[0];
402+
403+
LocalGraph::GetSetses expectedResult;
404+
expectedResult[getA1].insert(nullptr);
405+
expectedResult[getA1].insert(setA);
406+
expectedResult[getA2].insert(nullptr);
407+
expectedResult[getA2].insert(setA);
408+
expectedResult[getA3].insert(setA);
409+
expectedResult[getB].insert(nullptr);
410+
expectedResult[getA4].insert(setA);
411+
412+
EXPECT_EQ(expectedResult, getSetses);
413+
}
278414

279415
TEST_F(CFGTest, StackLatticeFunctioning) {
280416
FiniteIntPowersetLattice contentLattice(4);

0 commit comments

Comments
 (0)