Skip to content

Commit

Permalink
Fix complexity analysis
Browse files Browse the repository at this point in the history
The complexity analysis was ignoring intrinsics operations that can
contain functor calls. It had an impact on the reordering of conditions
that would move a functor call too early when it is within an intrinsic
operation.

fix #2373
  • Loading branch information
quentin committed Jan 10, 2023
1 parent 4256bab commit 5b42c1e
Show file tree
Hide file tree
Showing 8 changed files with 83 additions and 3 deletions.
16 changes: 13 additions & 3 deletions src/ram/analysis/Complexity.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ int ComplexityAnalysis::getComplexity(const Node* node) const {
}

int visit_(type_identity<UserDefinedOperator>, const UserDefinedOperator&) override {
return 10;
return std::numeric_limits<int>::max();
}

// emptiness check
Expand All @@ -71,8 +71,17 @@ int ComplexityAnalysis::getComplexity(const Node* node) const {
return (ra->lookup(emptiness.getRelation()).getArity() > 0) ? 1 : 0;
}

int visit_(type_identity<AbstractOperator>, const AbstractOperator& op) override {
int exprComplexity = 0;
for (auto* expr : op.getArguments()) {
exprComplexity += dispatch(*expr);
}
return exprComplexity;
}

// default rule
int visit_(type_identity<Node>, const Node&) override {
int visit_(type_identity<Node>, const Node& node) override {
(void)node;
return 0;
}

Expand All @@ -81,7 +90,8 @@ int ComplexityAnalysis::getComplexity(const Node* node) const {
};

assert((isA<Expression>(node) || isA<Condition>(node)) && "not an expression/condition/operation");
return ValueComplexityVisitor(ra).dispatch(*node);
const int complexity = ValueComplexityVisitor(ra).dispatch(*node);
return complexity;
}

} // namespace souffle::ram::analysis
2 changes: 2 additions & 0 deletions tests/semantic/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ if (NOT ${CMAKE_SYSTEM_NAME} MATCHES "Linux")
endif()

add_subdirectory(functor_fact)
add_subdirectory(issue2373)


function(positive_test NAME)
Expand Down Expand Up @@ -258,3 +259,4 @@ positive_test(underscore5)
negative_test(mutually_dependent_aggregate)
positive_test(independent_aggregate)
positive_test(independent_aggregate2)
souffle_positive_functor_test(issue2373 CATEGORY semantic)
35 changes: 35 additions & 0 deletions tests/semantic/issue2373/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# Souffle - A Datalog Compiler
# Copyright (c) 2022 The Souffle Developers. All rights reserved
# Licensed under the Universal Permissive License v 1.0 as shown at:
# - https://opensource.org/licenses/UPL
# - <souffle root>/licenses/SOUFFLE-UPL.txt

add_library(issue2373 SHARED functors.cpp)
target_include_directories(issue2373 PRIVATE "${CMAKE_SOURCE_DIR}/src/include")

target_compile_features(issue2373
PUBLIC cxx_std_17)

set_target_properties(issue2373 PROPERTIES CXX_EXTENSIONS OFF)
set_target_properties(issue2373 PROPERTIES OUTPUT_NAME "functors")
set_target_properties(issue2373 PROPERTIES WINDOWS_EXPORT_ALL_SYMBOLS ON)

if (NOT MSVC)
target_compile_options(issue2373
PUBLIC "-Wall;-Wextra;-Werror;-fwrapv")
else ()
target_compile_options(issue2373 PUBLIC /W3 /WX)
endif()

if (WIN32)
# Prefix all shared libraries with 'lib'.
set(CMAKE_SHARED_LIBRARY_PREFIX "lib")

# Prefix all static libraries with 'lib'.
set(CMAKE_STATIC_LIBRARY_PREFIX "lib")
endif ()

if (SOUFFLE_DOMAIN_64BIT)
target_compile_definitions(issue2373
PUBLIC RAM_DOMAIN_SIZE=64)
endif()
1 change: 1 addition & 0 deletions tests/semantic/issue2373/P.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
1 $A(1000)
16 changes: 16 additions & 0 deletions tests/semantic/issue2373/functors.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// functors.cpp
#include "souffle/RecordTable.h"
#include "souffle/SymbolTable.h"

extern "C" {

souffle::RamDomain id(souffle::SymbolTable*, souffle::RecordTable*, souffle::RamDomain x) {
return x;
}

souffle::RamDomain blubb(
souffle::SymbolTable* symbolTable, souffle::RecordTable*, souffle::RamDomain, souffle::RamDomain y) {
symbolTable->decode(y);
return 1;
}
}
16 changes: 16 additions & 0 deletions tests/semantic/issue2373/issue2373.dl
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
//mini-rules.dl
.type T = A {N: number} | B {S : symbol}

.decl P(N: number, T: T)

.functor blubb(symbol, symbol): number stateful
.functor id(symbol): symbol stateful

.output P

P(1, $A(1000)).

P(0, $B(?_p)) :-
(1-@blubb("60",?_p)) != 0,
(?_p != @id("40")),
P(1, $B(?_p)).
Empty file.
Empty file.

0 comments on commit 5b42c1e

Please sign in to comment.