Skip to content

Commit e68c5b6

Browse files
authored
Fix least-free-vars SIPS metric (#2587)
1 parent ee5cda5 commit e68c5b6

3 files changed

Lines changed: 59 additions & 1 deletion

File tree

src/ast/tests/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,4 @@ souffle_add_binary_test(ast_utils_test ast)
1313
souffle_add_binary_test(type_system_test ast)
1414
souffle_add_binary_test(constraints_test ast)
1515
souffle_add_binary_test(ast_recursive_clauses_test ast)
16+
souffle_add_binary_test(sips_metric_test ast)

src/ast/tests/sips_metric_test.cpp

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
* Souffle - A Datalog Compiler
3+
* Copyright (c) 2026 The Souffle Developers. All rights reserved
4+
* Licensed under the Universal Permissive License v 1.0 as shown at:
5+
* - https://opensource.org/licenses/UPL
6+
* - <souffle root>/licenses/SOUFFLE-UPL.txt
7+
*/
8+
9+
/************************************************************************
10+
*
11+
* @file sips_metric_test.cpp
12+
*
13+
* Tests souffle's SIPS cost metrics.
14+
*
15+
***********************************************************************/
16+
17+
#include "tests/test.h"
18+
19+
#include "Global.h"
20+
#include "ast/Program.h"
21+
#include "ast/QualifiedName.h"
22+
#include "ast/TranslationUnit.h"
23+
#include "ast2ram/utility/SipsMetric.h"
24+
#include "parser/ParserDriver.h"
25+
#include "reports/DebugReport.h"
26+
#include "reports/ErrorReport.h"
27+
#include <string>
28+
#include <vector>
29+
30+
namespace souffle::ast {
31+
32+
namespace test {
33+
34+
TEST(SipsMetric, LeastFreeVarsCountsUnboundVariables) {
35+
Global glb;
36+
ErrorReport e;
37+
DebugReport d(glb);
38+
39+
Own<TranslationUnit> tu = ParserDriver::parseTranslationUnit(glb,
40+
R"(
41+
.decl seed(x:number)
42+
.decl low(z:number, w:number)
43+
.decl high(x:number, y:number)
44+
.decl result(y:number, z:number, w:number)
45+
46+
result(y, z, w) :- seed(x), low(z, w), high(x, y).
47+
)",
48+
e, d);
49+
50+
auto* clause = tu->getProgram().getClauses(QualifiedName::fromString("result"))[0];
51+
LeastFreeVarsSips sips(*tu);
52+
53+
EXPECT_EQ(std::vector<std::size_t>({0, 2, 1}), sips.getReordering(clause, {"seed", "low", "high"}));
54+
}
55+
56+
} // namespace test
57+
} // namespace souffle::ast

src/ast2ram/utility/SipsMetric.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -478,7 +478,7 @@ std::vector<double> LeastFreeVarsSips::evaluateCosts(const std::vector<Atom*> at
478478
// use a set to hold all free variables to avoid double-counting
479479
std::set<std::string> freeVars;
480480
visit(*atom, [&](const Variable& var) {
481-
if (bindingStore.isBound(var.getName())) {
481+
if (!bindingStore.isBound(var.getName())) {
482482
freeVars.insert(var.getName());
483483
}
484484
});

0 commit comments

Comments
 (0)