Skip to content

Facade Improvements - #2595

Open
RobertLeahy wants to merge 3 commits into
NVIDIA:mainfrom
RobertLeahy:facade_improvements_20261202
Open

RobertLeahy wants to merge 3 commits into
NVIDIA:mainfrom
RobertLeahy:facade_improvements_20261202

Conversation

@RobertLeahy

Copy link
Copy Markdown
Contributor

No description provided.

@copy-pr-bot

copy-pr-bot Bot commented Dec 2, 2026

Copy link
Copy Markdown

This pull request requires additional validation before any workflows can run on NVIDIA's runners.

Pull request vetters can view their responsibilities here.

Contributors can view more details about this message here.

@ericniebler ericniebler left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i am ok with the first two commits, but i am not happy with the change in "Create Facade Tokens With __facade::__token". i know you want it for your tail recursion work, but this change is a significant complication to code that is already complicated. and tail recursion isn't standard, so nobody benefits from this change.

for the purpose of your proposal, i ask you to move that change out of this PR and maintain it in your own fork.

@RobertLeahy

Copy link
Copy Markdown
Contributor Author

i am ok with the first two commits, but i am not happy with the change in "Create Facade Tokens With __facade::__token". i know you want it for your tail recursion work, but this change is a significant complication to code that is already complicated. and tail recursion isn't standard, so nobody benefits from this change.

I had imagined the broader benefit being auto return types being "correct" at runtime, but I'm happy to skip it if that's not a desirable behavior.

The facade's walk constructs an algorithm's state with an archetype receiver,
and expects the operations in it to be placeholders (__facade::__state_op_t).
starts_on and continues_on (their schedule operations), the counting scopes'
join (its schedule operation), and test_facade.cpp's t_schedule connected
real operations instead.

The above-mentioned now connect their operation through a member that
returns the placeholder in a walk.
Algorithms that send stored values applied a lambda that calls __send to the
tuple that holds them. __send_tuple(rcvr, tag, tuple) does that, and returns
__send's token.
The member functions of __facade::__nested_receiver (the receiver of a
child or nested operation that an algorithm keeps in its state) were not
constexpr, so an operation that completes through one could not be
evaluated in a constant expression.

They weren't constexpr because they access the algorithm's state, which
is incomplete while the type of the operation it holds is computed, and
Clang instantiates a constexpr function as soon as a body it is
instantiating calls it. This is the problem that 8ee3a91
("stdexec::__apply: Do Not Deduce Return Type") describes, and as there
the problem is not this receiver, whose return types are declared, but
whatever builds a body while the state is incomplete. Making the
receiver constexpr found these, each fixed here:

- STDEXEC::__subscribe deduced its return type (decltype(auto)), so
  computing the type of a subscription built its body, and so the body
  of the sender's __static_subscribe, and so on down to the receiver's
  get_env. It now declares it, as STDEXEC::connect does.

- The bulk sender of task_scheduler deduced the return type of connect.
  It now declares it.

- exec::create's state computes the type of what the function returns
  when it is called with the context, which holds a receiver that
  points back at the state. Calling a function whose return type is
  deduced builds its body, and so (in Clang) the body of the
  receiver's set_value. The receiver now points at a base of the state
  holding only the downstream receiver, which is all it uses, and which
  is complete then.

- The receiver proxies of the parallel scheduler and task_scheduler,
  and iterate's operation, had constexpr virtual functions that
  complete the receiver they hold. Clang instantiates those with the
  class. They are no longer constexpr. (They are reached through
  virtual calls into a scheduler backend, or a type-erased sequence,
  which can't be constant evaluated anyway. The interfaces still
  declare them constexpr.)

- Two test senders (in test_let_value.cpp and test_starts_on.cpp)
  deduced the return type of connect. They now declare it.

The comment on __nested_receiver now describes these rules.

Even so, no operation which completes through __nested_receiver could
be evaluated in a constant expression, because algorithms keep their
nested operations (and the values they forward) in __variant and
__optional (i.e. __manual_lifetime), and both kept their objects in
byte arrays, which can't be accessed as objects of another type in
constant evaluation (not even in C++26, which allows placement new, as
the types aren't similar). Also STDEXEC::schedule wasn't constexpr. So
this also:

- Replaces the byte array of __manual_lifetime with a union of one
  member, which it constructs in constant evaluation with
  std::construct_at from an emplacer (STDEXEC::__emplace_from), as
  __variant::__emplace_from already did, since placement new isn't
  constexpr before C++26 and std::construct_at neither brace-initializes
  nor initializes an immovable type from a prvalue. Outside constant
  evaluation it still uses placement new.

- Replaces the byte array of __variant with a recursive union of its
  alternatives. Constructing an alternative first makes the nested
  unions enclosing it active (a no-op outside constant evaluation), and
  alternatives are reached through the union rather than by casting
  pointers.

- Makes __optional::__emplace_from and STDEXEC::schedule constexpr.

- Tests that let_value, let_stopped, finally, starts_on, and continues_on
  connect, start, and complete in constant expressions. (Without the
  changes to __nested_receiver these fail, because its get_env isn't
  constexpr.)

The tests are disabled for GCC before 15, which, in constant evaluation,
copies an object which a member initializer initializes from a
function's prvalue, rather than initializing it in place, leaving the
pointers an operation state keeps into itself dangling (__opstate
initializes its children that way, so this can't be avoided here), and
for compilers which haven't been tried (Clang before 18, Apple Clang,
clang-cl, MSVC, NVCC, and other EDG front ends).
@RobertLeahy
RobertLeahy force-pushed the facade_improvements_20261202 branch from 582f3b0 to 729937e Compare December 5, 2026 03:23
// such a function for a type (as exec::create asks for the result of its
// function) can point this receiver at a part of the state that is
// complete by then. (See 8ee3a918b, "stdexec::__apply: Do Not Deduce
// Return Type".)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

after this change, i see no difference between __receiver and __nested_receiver. __nested_receiver can just be deleted.

{}

constexpr void set_value() noexcept final
void set_value() noexcept final

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why are we removing constexpr here and below?

std::construct_at(std::addressof(__u.__tail_));
return __var::__union_activate<_Ny - 1>(__u.__tail_);
}
}

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

have you measured compile times after this change?

@ericniebler

Copy link
Copy Markdown
Collaborator

/ok to test 729937e

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants