Can consteval functions from different translation units interfere?
Asked Answered
E

3

0

I am trying to dig into implications of a function being inline and stumbled upon this issue. Consider this small program (demo):

/* ---------- main.cpp ---------- */
void other();

constexpr int get()
{
    return 3;
}

int main() 
{
    std::cout << get() << std::endl;
    other();
}

/* ---------- other.cpp ---------- */
constexpr int get()
{
    return 4;
}

void other()
{
    std::cout << get() << std::endl;
}

When compiled without optimizations, the program yields the following output:

3
3

Which might be not what we want, but at least I can explain it.

  1. The compiler is not required to compute results of constexpr functions on compile time, so it decided to postpone it to runtime.
  2. constexpr on functions implies inline
  3. Our get() functions happened to have different implementations
  4. We did not declare get() functions static
  5. The linker must chose only one implementation of the get() function

And it so happened that the linker chose get() from main.cpp, which returned 3.

Now to the part I don't get. I simply changed get() functions from constexpr to consteval. Now the compiler is required to compute the value during compile time, i.e. before the link time (right?). I'd expect get() functions not to be present in object files at all.

But when I run it (demo), I have exactly the same output! How can this be?.. I mean yes, I understand that this is undefined behavior, but this is not the point. How come that the values that should have been computed on compile time interfered with other translation unit?

UPD: I am aware that this feature is listed as unimplemented in clang, but the question is applicable anyway. Is a conformant compiler allowed to exhibit such a behavior?

Ebarta answered 18/10, 2019 at 14:35 Comment(1)
UB means anything can happen to any part of your program. Get rid of any UB first, before trying to reason about anything.Khasi
T
7

A program with two definitions of the same inline function is an ill-formed program, no diagnostics required.

The standard places no requirements on the runtime or compile-time behavior of an ill-formed program.

Now, there is no "compile time" in C++ as you are imagining it. While almost every C++ implementation compiles files, links them, builds a binary, then runs it, the C++ standard tip-toes around this fact.

It talks about translation units, and what happens when you put them together into a program, and what that program's runtime behaviour is.

...

In practice, your compiler could be building a map from symbol to some internal structure. It is compiling your first file, and then in the second file it is still accessing that map. A new definition of the same inline function? Just skip it.

Second, your code must produce a compile time constant expression. But a compile time constant expression is not an observable property in the context where you used it, and there are no side effects to doing it at link or even run time! And under as-if there is nothing preventing that.

consteval is saying "if I run this and the rules that permit it being a constant expression are violated, I should error rather than fall back on non-constant expression". This is similar to "it must be run at compile time", but it is not the same.

To determine which of these is happening, try this:

template<auto x>
constexpr std::integral_constant< decltype(x), x > constant = {};

now replace your print lines with:

std::cout << constant<get()> << std::endl;

this makes putting off the evaluation to run/link time as impractical.

That will distinguish between the "compiler is being clever and caching get" from "compiler is evaluating it later at link time", because determining which ostream& << to call requires instantiating the type of constant<get()>, which in turn requires evaluating get().

Compilers tend not to defer overload resolution to link time.

Truthfunction answered 18/10, 2019 at 14:47 Comment(4)
This is a great explanation, but I'm still not convinced, sorry. See gcc.godbolt.org/z/rb_X2W, where consteval f() is clearly called during runtime. Are you trying to say this is a correct behavior? I think this is a bug, similar to this one: https://mcmap.net/q/282803/-why-does-a-consteval-function-allow-undefined-behavior/261217Ebarta
Please take a look: https://mcmap.net/q/282774/-is-compiler-allowed-to-call-an-immediate-consteval-function-during-runtime/261217Ebarta
@Mikhail, Clang lists immediate functions as not implemented, as you saw in your bug report. The result of using an unimplemented feature is not particularly compelling.Sit
@Sit that's why I am asking the question in the first place, because I do not have an implementation I can rely on.Ebarta
F
7

The requirement for a consteval function is that every call to it must produce a constant expression.

Once the compiler satisfies itself that a call does produce a constant expression, there's no requirement that it must not codegen the function and call it at run time. Of course, for some consteval functions (like those envisioned for reflection) it had better not do that (at least if it doesn't want to put all its internal data structures into the object file), but that's not a general requirement.

Undefined behavior is undefined.

Forespeak answered 18/10, 2019 at 14:46 Comment(1)
T
7

A program with two definitions of the same inline function is an ill-formed program, no diagnostics required.

The standard places no requirements on the runtime or compile-time behavior of an ill-formed program.

Now, there is no "compile time" in C++ as you are imagining it. While almost every C++ implementation compiles files, links them, builds a binary, then runs it, the C++ standard tip-toes around this fact.

It talks about translation units, and what happens when you put them together into a program, and what that program's runtime behaviour is.

...

In practice, your compiler could be building a map from symbol to some internal structure. It is compiling your first file, and then in the second file it is still accessing that map. A new definition of the same inline function? Just skip it.

Second, your code must produce a compile time constant expression. But a compile time constant expression is not an observable property in the context where you used it, and there are no side effects to doing it at link or even run time! And under as-if there is nothing preventing that.

consteval is saying "if I run this and the rules that permit it being a constant expression are violated, I should error rather than fall back on non-constant expression". This is similar to "it must be run at compile time", but it is not the same.

To determine which of these is happening, try this:

template<auto x>
constexpr std::integral_constant< decltype(x), x > constant = {};

now replace your print lines with:

std::cout << constant<get()> << std::endl;

this makes putting off the evaluation to run/link time as impractical.

That will distinguish between the "compiler is being clever and caching get" from "compiler is evaluating it later at link time", because determining which ostream& << to call requires instantiating the type of constant<get()>, which in turn requires evaluating get().

Compilers tend not to defer overload resolution to link time.

Truthfunction answered 18/10, 2019 at 14:47 Comment(4)
This is a great explanation, but I'm still not convinced, sorry. See gcc.godbolt.org/z/rb_X2W, where consteval f() is clearly called during runtime. Are you trying to say this is a correct behavior? I think this is a bug, similar to this one: https://mcmap.net/q/282803/-why-does-a-consteval-function-allow-undefined-behavior/261217Ebarta
Please take a look: https://mcmap.net/q/282774/-is-compiler-allowed-to-call-an-immediate-consteval-function-during-runtime/261217Ebarta
@Mikhail, Clang lists immediate functions as not implemented, as you saw in your bug report. The result of using an unimplemented feature is not particularly compelling.Sit
@Sit that's why I am asking the question in the first place, because I do not have an implementation I can rely on.Ebarta
B
1

The answer to this is that it's still an ODR violation, no matter if the function is a constexpr or consteval. Maybe with a particular compiler and a particular code you may get the answer you're expecting, but it's still ill formed, no diagnostic required.

What you could do is to define them in anonymous namespaces:

/* ---------- main.cpp ---------- */
void other();

namespace {
    constexpr int get()
    {
        return 3;
    }
}

int main() 
{
    std::cout << get() << std::endl;
    other();
}

/* ---------- other.cpp ---------- */
namespace {
    constexpr int get()
    {
        return 4;
    }
}

void other()
{
    std::cout << get() << std::endl;
}

But even better, simply use modules:

/* ---------- main.cpp ---------- */
import other;

constexpr int get()
{
    return 3;
}

int main() 
{
    std::cout << get() << std::endl; // print 3
    other();
}

/* ---------- other.cpp ---------- */
export module other;

constexpr int get() // okay, module linkage
{
    return 4;
}

export void other()
{
    std::cout << get() << std::endl; // print 4
}
Bibliography answered 18/10, 2019 at 14:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.