This is an archive of the discontinued LLVM Phabricator instance.

Support unwinding from inline assembly
ClosedPublic

Authored by cynecx on Jan 30 2021, 11:20 AM.

Details

Reviewers
rnk
nikic
deadalnix
Amanieu
Group Reviewers
Restricted Project
Restricted Project
Commits
rG8ec9fd483949: Support unwinding from inline assembly
Summary

I haven't received any feedback about the approach I've taken yet (https://lists.llvm.org/pipermail/llvm-dev/2021-January/148175.html).
So I thought I'd try my luck here.

A splitted patchset can be found here: https://github.com/cynecx/llvm-project/commits/asmunwind-final

I've taken the following steps to add unwinding support from inline assembly:

  1. Add a new unwind "attribute" (like sideeffect) to the asm syntax:
invoke void asm sideeffect unwind "call thrower", "~{dirflag},~{fpsr},~{flags}"()
    to label %exit unwind label %uexit

2.) Add Bitcode writing/reading support + LLVM-IR parsing.

3.) Emit EHLabels around inline assembly lowering (SelectionDAGBuilder + GlobalISel) when InlineAsm::canThrow is enabled.

4.) Tweak InstCombineCalls/InlineFunction pass to not mark inline assembly "calls" as nounwind.

5.) Add clang support by introducing a new clobber: "unwind", which lower to the canThrow being enabled.

6.) Don't allow unwinding callbr.

Diff Detail

Event Timeline

cynecx created this revision.Jan 30 2021, 11:20 AM
cynecx requested review of this revision.Jan 30 2021, 11:20 AM

Tests are missing right now. But I'd like to get some feedback first whether this approach goes in the right direction.

cynecx edited the summary of this revision. (Show Details)Jan 31 2021, 10:03 AM
cynecx edited the summary of this revision. (Show Details)
cynecx edited the summary of this revision. (Show Details)
cynecx edited the summary of this revision. (Show Details)
cynecx updated this revision to Diff 321479.Feb 4 2021, 9:08 AM

clang-format changes

cynecx edited reviewers, added: rnk, nikic; removed: deadalnix.Feb 4 2021, 9:11 AM
rnk added a comment.Feb 4 2021, 2:22 PM

Basically, this seems totally reasonable and I think this is the right direction.

cynecx updated this revision to Diff 325323.Feb 21 2021, 10:37 AM
cynecx edited the summary of this revision. (Show Details)

Added tests and updated splitted patchset.

@rnk This is ready for a proper review :)

cynecx updated this revision to Diff 325328.Feb 21 2021, 11:44 AM

clang-format

cynecx added a comment.Mar 3 2021, 8:34 AM

Weekly ping :)

Weekly ping!

Weekly Ping!

The new operand to InlineAsm needs to be handled in llvm/lib/Transforms/Utils/ValueMapper.cpp otherwise you will end up with a bug similar to https://bugs.llvm.org/show_bug.cgi?id=45291.

Some tests with other unwinding models such as SJLJ and SEH would be nice.

clang/lib/CodeGen/CGStmt.cpp
2542

This should be a compiler error diagnostic in SemaAsmStmt.cpp rather than an assert.

llvm/lib/AsmParser/LLParser.cpp
5573–5575
llvm/lib/Bitcode/Reader/BitcodeReader.cpp
2865
llvm/lib/CodeGen/GlobalISel/IRTranslator.cpp
2436

Is this fast path actually useful? The frontend will almost never emit an invoke instruction for inline asm that can't unwind.

llvm/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp
8469

This code is likely to get out of sync with the one in visitInvokable. It would be nice to share the code in a single place, but if that is not practical then at least add a comment in visitInvokable to remind anyone modifying that code to apply the same changes here as well.

cynecx updated this revision to Diff 343853.May 8 2021, 10:48 AM
cynecx marked 4 inline comments as done.

Address review

llvm/lib/CodeGen/GlobalISel/IRTranslator.cpp
2436

This is just an optimization so that we don't emit unwind information since the inline asm doesn't "throw" and also matches the old/new SelectionDAG behavior. Either way I don't really mind removing this but I'd personally keep this for consistency.

Sorry about the delay. I've updated and rebased the patchset (which also includes tests for different exception models like seh/sjlj).

cynecx updated this revision to Diff 343924.May 9 2021, 11:34 AM

Fix assertion and clang-format patch.

cynecx updated this revision to Diff 343932.May 9 2021, 1:46 PM

actually run clang-format

Amanieu accepted this revision.May 10 2021, 3:35 PM

LGTM

This revision is now accepted and ready to land.May 10 2021, 3:35 PM

I would be great if someone with commit rights could push this through since I don't have commit rights :)

This revision was landed with ongoing or failed builds.May 13 2021, 11:15 AM
Closed by commit rG8ec9fd483949: Support unwinding from inline assembly (authored by cynecx, committed by Amanieu). · Explain Why
This revision was automatically updated to reflect the committed changes.

@Amanieu Thanks for the review and commit! :)

cynecx added a comment.EditedMay 13 2021, 11:45 AM

Hmm, there is a build-failure when buildbot is running extended tests (MachineVerifier): https://lab.llvm.org/buildbot/#/builders/16/builds/10825

Something unrelated might be wrong with the test because I can reproduce the MachineVerifier issue (liveins + non-entry/landingpad) on llvm-stable (11.1) and even without the inline-asm call (replaced by a normal invoke to the target).

Okay. This is a known issue https://bugs.llvm.org/show_bug.cgi?id=39439. I'll upload a new patch which includes`-verify-machineinstrs=0` as a temporary workaround. (See the sjlj-eh.ll test)