This is an archive of the discontinued LLVM Phabricator instance.

Do not merge traps in functions annotated optnone
ClosedPublic

Authored by hnrklssn on Nov 9 2022, 7:22 AM.

Details

Summary

This aligns the behaviour with that of disabling optimisations for the
translation unit entirely. Not merging the traps allows us to keep
separate debug information for each, improving the debugging experience
when finding the cause for a ubsan trap.

Diff Detail

Event Timeline

hnrklssn created this revision.Nov 9 2022, 7:22 AM
Herald added a project: Restricted Project. · View Herald TranscriptNov 9 2022, 7:22 AM

@hnrklssn Could you add a regression test?

clang/lib/CodeGen/CodeGenModule.cpp
5321

I'm a little worried about this ordering change here. Could this have some unintended consequences?

hnrklssn updated this revision to Diff 474504.Nov 10 2022, 4:26 AM

Added regression test

hnrklssn published this revision for review.Nov 10 2022, 4:44 AM
hnrklssn added inline comments.
clang/lib/CodeGen/CodeGenModule.cpp
5321

Yeah I was also a bit worried about that when making the change, since the functions are both quite broad and I'm not familiar with them from before. However it doesn't break any test cases, so I'm not sure what the consequences would be exactly.

For reference, also moving the call to setNonAliasAttributes so that it is still before the call to SetLLVMFunctionAttributesForDefinition breaks a ton of test cases so I'm somewhat hopeful that we have good test coverage for this type of change.

Herald added a project: Restricted Project. · View Herald TranscriptNov 10 2022, 4:44 AM
Herald added a subscriber: cfe-commits. · View Herald Transcript
hnrklssn marked an inline comment as not done.Nov 10 2022, 4:45 AM
fcloutier requested changes to this revision.Nov 10 2022, 8:51 AM
fcloutier added inline comments.
clang/lib/CodeGen/CodeGenModule.cpp
5321

Could you get it from CurCodeDecl->hasAttr<OptimizeNoneAttr>() in CGExpr instead? Then you wouldn't have to change this.

Caveat: am not sure that CurCodeDecl is always set. For instance, do you have a CurCodeDecl when you generate a C++ static initializer? On the upside, if it's NULL, you can just bail out.

This revision now requires changes to proceed.Nov 10 2022, 8:51 AM
hnrklssn updated this revision to Diff 474811.Nov 11 2022, 11:01 AM

No longer reorders annotations to occur before codegen. Instead EmitTrapCheck checks the function declaration for OptimizeNoneAttr.

hnrklssn marked 2 inline comments as done.Nov 11 2022, 11:04 AM

Made changes in line with what @fcloutier suggested.

clang/lib/CodeGen/CodeGenModule.cpp
5321

Indeed that approach works as well. There are some test cases that result in CurCodeDecl being NULL, like you suspected.

fcloutier accepted this revision.Nov 11 2022, 1:44 PM
This revision is now accepted and ready to land.Nov 11 2022, 1:44 PM

Other than minor issue in the test this LGTM

clang/test/CodeGen/ubsan-trap-debugloc.c
11

Could you add CHECK-LABEL: @foo and CHECK-LABEL: @bar to this test? This helps make it explicit which functions in the IR we are trying to match.

hnrklssn updated this revision to Diff 475104.Nov 14 2022, 4:33 AM
hnrklssn marked an inline comment as done.

Add explicit labels to FileCheck checks

hnrklssn marked an inline comment as done.Nov 14 2022, 4:34 AM