This is an archive of the discontinued LLVM Phabricator instance.

[clang] Reset track of immediate function context when entering new function
ClosedPublic

Authored by Fznamznon on Apr 4 2023, 6:44 AM.

Details

Summary

Due to not resetting that, clang still thinks that it is in immediate
function context even if it already entered non-consteval function.
This caused consteval functions reaching codegen in some cases.

Fixes https://github.com/llvm/llvm-project/issues/61142

Diff Detail

Event Timeline

Fznamznon created this revision.Apr 4 2023, 6:44 AM
Herald added a project: Restricted Project. · View Herald TranscriptApr 4 2023, 6:44 AM
Fznamznon requested review of this revision.Apr 4 2023, 6:44 AM
Herald added a project: Restricted Project. · View Herald TranscriptApr 4 2023, 6:44 AM
Herald added a subscriber: cfe-commits. · View Herald Transcript

Thanks, for working on this
Congrats on finding the underlying issue, it must not have been easy!

clang/lib/Sema/SemaDecl.cpp
15177

I think it might be worth adding a comment there to explain why this is necessary.
I think i convinced myself this make sense and is almost certainly the correct fix but it would, I think, benefit from some more explanation for future reference.

clang/test/CodeGenCXX/cxx20-consteval-crash.cpp
138

Just to make sure, did you check that this fixes the other examples in the issue? I was afraid i reduced too much

Fznamznon edited the summary of this revision. (Show Details)Apr 4 2023, 7:24 AM
Fznamznon updated this revision to Diff 510807.Apr 4 2023, 7:52 AM

Add a comment, rebase, fix format

clang/lib/Sema/SemaDecl.cpp
15177

Sure, done. Hope it turned out helpful.

clang/test/CodeGenCXX/cxx20-consteval-crash.cpp
138

Yes, it helps to fix them. I checked even the original big one.

cor3ntin accepted this revision.Apr 4 2023, 8:17 AM

I reworked the comment a bit otherwise LGTM.

clang/lib/Sema/SemaDecl.cpp
15178–15185
This revision is now accepted and ready to land.Apr 4 2023, 8:17 AM
aaron.ballman added inline comments.Apr 4 2023, 9:07 AM
clang/docs/ReleaseNotes.rst
273–277
clang/test/CodeGenCXX/cxx20-consteval-crash.cpp
120

Would it make sense to add some CHECK lines to check the actual codegen is sensible?

shafik added inline comments.Apr 4 2023, 9:19 AM
clang/lib/Sema/SemaDecl.cpp
15186

I am a bit concerned that we are updating InImmediateFunctionContext outside of PushExpressionEvaluationContext. So we are now spreading the logic outside of where it was originally contained.

I am wondering if instead inside of PushExpressionEvaluationContext we could set InImmediateFunctionContext like so:

ExprEvalContexts.back().InImmediateFunctionContext =
      ExprEvalContexts[ExprEvalContexts.size() - 2]
          .isImmediateFunctionContext() || NewContext == ImmediateFunctionContext;

or something along those lines.

cor3ntin added inline comments.Apr 4 2023, 9:29 AM
clang/lib/Sema/SemaDecl.cpp
15186

Here isImmediateFunctionContext() will be true, we want it to be false

If we wanted to do somelink like what you are suggesting, we'd need something like a new EnteringFunctionDefinitionContext boolean parameter to PushExpressionEvaluationContext. so that
InImmediateFunctionContext is only set to the outer context value when EnteringFunctionDefinitionContextwould be false

Given that we should only get in that situation from ActOnStartOfFunctionDef, I'm not sure that would be a better design

Fznamznon updated this revision to Diff 510843.Apr 4 2023, 9:48 AM

Fix grammar, add checks to the test

Fznamznon marked 2 inline comments as done.Apr 4 2023, 9:51 AM
Fznamznon added inline comments.
clang/lib/Sema/SemaDecl.cpp
15186

Thanks, @cor3ntin .
This is the exact reason why I implemented the fix this way.

clang/test/CodeGenCXX/cxx20-consteval-crash.cpp
120

Well, the assertion was due to consteval function reaching codegen, so I added checks that it doesn't appear in the IR in any way.

aaron.ballman accepted this revision.Apr 4 2023, 12:52 PM

LGTM, thank you for the fix!