This is an archive of the discontinued LLVM Phabricator instance.

[clang] Fix time profile in "isIntegerConstantExpr"
ClosedPublic

Authored by Izaron on Oct 23 2022, 4:04 AM.

Details

Summary

The time profiler in Expr::isIntegerConstantExpr used to
call Loc->printToString, it was inconsistent with other time
profiles in the file and caused segfaults if Loc was nullptr.

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

Diff Detail

Event Timeline

Izaron created this revision.Oct 23 2022, 4:04 AM
Herald added a project: Restricted Project. · View Herald TranscriptOct 23 2022, 4:04 AM
Izaron requested review of this revision.Oct 23 2022, 4:04 AM
Herald added a project: Restricted Project. · View Herald TranscriptOct 23 2022, 4:04 AM
Herald added a subscriber: cfe-commits. · View Herald Transcript
Izaron edited the summary of this revision. (Show Details)Oct 23 2022, 4:05 AM
jloser accepted this revision.Oct 23 2022, 3:25 PM

LGTM. Thanks for the fix.

I did leave an open question or two though, but it is non-blocking.

clang/lib/AST/ExprConstant.cpp
15908

Question This looks like the right fix for this call site. Are the other two uses of llvm::TimeTraceScope as a local variable subject to similar problems? I don't think so since we don't try to get the location explicitly, but want to confirm.

clang/unittests/Support/TimeProfilerTest.cpp
209

Question Is adding the ability to plumb the standards mode just useful for this bug fix in the sense of reducing the trace graph output of the AST?

This revision is now accepted and ready to land.Oct 23 2022, 3:25 PM
dyung accepted this revision.Oct 23 2022, 3:49 PM

I'm not really familiar with this code, so I'll leave others to review the actual change, but I can confirm that when I built a compiler with this change and ran it against the failing tests they all now pass. So in that regard, LGTM.

Izaron marked 2 inline comments as done.Oct 23 2022, 4:17 PM
Izaron added inline comments.
clang/lib/AST/ExprConstant.cpp
15908

That's a good question! We use custom llvm::TimeTraceScope in three places:

isPotentialConstantExpr
EvaluateAsInitializer
EvaluateWithSubstitution

The problem with isIntegerConstantExpr (which I fixed in this patch) was that the Loc was nullptr (and there were no tests that would catch it).

The three functions with custom time traces use either const VarDecl *VD or const FunctionDecl *FD. These variables are surely not nullptr because the methods bravely use them (VD->doSmth()/FD->doSmth()).

Also our unit test covers isPotentialConstantExpr and EvaluateAsInitializer (you can see them in ASSERT_TRUE).
So I think there is no obvious problems that I can think of =)

clang/unittests/Support/TimeProfilerTest.cpp
209

This is useful for bug fix, because some ExprConstant.cpp methods are called only for C code (not for C++ code). C and C++ have a somewhat different constant evaluations.

The segfault in Expr::isIntegerConstantExpr was only discoverable when compiling C code, because there is a explicit condition for calling this method only for C code:
https://github.com/llvm/llvm-project/blob/08d1c43c7023a2e955c43fbf4c3f1635f91521e0/clang/lib/Sema/SemaExpr.cpp#L17318

Izaron marked 2 inline comments as done.Oct 23 2022, 4:17 PM
This revision was automatically updated to reflect the committed changes.
jloser added inline comments.Oct 23 2022, 6:28 PM
clang/lib/AST/ExprConstant.cpp
15908

That was my takeaway as well — no obvious lingering bugs. :)

clang/unittests/Support/TimeProfilerTest.cpp
209

Makes sense — thanks for pointing out the relevant code block in Sema!