souffle-lang / souffle

Soufflé is a variant of Datalog for tool designers crafting analyses in Horn clauses. Soufflé synthesizes a native parallel C++ program from a logic specification.
http://souffle-lang.github.io/
Universal Permissive License v1.0
912 stars 200 forks source link

Registration may not run when using the C++ interface #2424

Open aDifferentJT opened 1 year ago

aDifferentJT commented 1 year ago

The C++ standard says that:

If no variable or function is odr-used from a given translation unit, the non-local variables defined in that translation unit may never be initialized (this models the behavior of an on-demand dynamic library). However, as long as anything from a translation unit is odr-used, all non-local variables whose initialization or destruction has side effects will be initialized even if they are not used in the program.

https://en.cppreference.com/w/cpp/language/initialization#Deferred_dynamic_initialization

Since the generated C++ files do not contain any variable or function that is odr-used this means that the compiler is free not to initialise the factory instance and so that factory doesn't get registered and souffle::ProgramFactory::newInstance fails to find the program.

quentin commented 1 year ago

Hi, do you have a working example that exhibits such issue? Any platform or compiler where newInstance fails to find the program?

aDifferentJT commented 1 year ago

Indeed, I ran into this issue when linking the generated C++ file with a Rust program. At first I thought it was a Rust issue but they pointed out that this behaviour is allowed by the C++ standard.

quentin commented 1 year ago

Can you reproduce with a 100% C++ test? Would you like to suggest a fix?

aDifferentJT commented 1 year ago

I am not aware of a C++ compiler whose default linker behaves this way, nor is there an easy fix.