This is an archive of the discontinued LLVM Phabricator instance.

[coroutines] Experimenting with compile-time size check
Needs ReviewPublic

Authored by GorNishanov on Nov 13 2018, 3:45 PM.

Details

Reviewers
modocache
iains
Summary

! Not intended to be merged into the trunk at the moment !

Adding a coro_size_check(IntConstant) intrinsic.

Presense of this intrinsic in the body of the coroutine inhibits Halo optimization of both into this coroutine and this coroutine into others.

The intent is that this intrinsics will be emitted into the Coroutine IR when a constract is specified on operator new for the coroutine indicating the maximum allowed size. To be used when coroutine state is stored in a fixed size buffer.

At the moment this uses report_fatal_error, in further iteration diagnistic will be improve. Currently it emits two kind of errors:

error in backend: In function '_Z1fv' with coroutine frame size 24 front end size estimate is not a constant.

or

In function '_Z1fv' coroutine frame size 24 exceeds the frontend estimate of 15.

Intrinsic can be extended to include more precise location information so that it can point precisely back to file name + line number.

Here is a background for the intended use by the frontend:

The coroutine machinery requests memory via overloaded operator new on the coroutine promise.

Let’s say we put a contract there:

template <size_t MaxSize>
void *operator new(size_t sz,

                 Storage<MaxSize> put_it_here)[[expects:sz <= MaxSize]] {
return &put_it_here;

}

Note that in “[[expects: sz <= MaxSize]]” contract sz is a compile time constant, but, it is a constant that is known at translation time, but not necessarily during semantic analysis time.

Normally, it is QoI if contract checking is done at compile time as opposed to a runtime.

I am wondering if having some wording that mandates compile checking of some contracts we will have desired portable guaranteed way of given a hard compile time error if precondition on operator new is not satisfied.

Here is one way to give the guarantee: (Somewhere in the coroutine wording when describing how memory for the coroutine state is acquired).

“If operator new has a precondition contract AND that contract condition would be a constant expression if “sz” argument were a constant time expression, then, the contract will be checked even in the Contract checking “Off” mode and violation will make the program ill-formed.”

Diff Detail

Event Timeline

GorNishanov created this revision.Nov 13 2018, 3:45 PM