This is an archive of the discontinued LLVM Phabricator instance.

[CodeGen][inlineasm] assume the flag output of inline asm is boolean value
ClosedPublic

Authored by ychen on Jul 16 2022, 10:02 PM.

Details

Summary

GCC inline asm document says that
"... the general rule is that the output variable must be a scalar
integer, and the value is boolean."

Commit e5c37958f901cc9bec50624dbee85d40143e4bca lowers flag output of
inline asm on X86 with setcc, hence it is guaranteed that the flag
is of boolean value. Clang does not support ARM inline asm flag output
yet so nothing need to be worried about ARM.

See "Flag Output" section at
https://gcc.gnu.org/onlinedocs/gcc/Extended-Asm.html#OutputOperands

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

Diff Detail

Event Timeline

ychen created this revision.Jul 16 2022, 10:02 PM
Herald added a project: Restricted Project. · View Herald TranscriptJul 16 2022, 10:02 PM
ychen requested review of this revision.Jul 16 2022, 10:02 PM
Herald added a project: Restricted Project. · View Herald TranscriptJul 16 2022, 10:02 PM
Herald added a subscriber: cfe-commits. · View Herald Transcript
nikic added a comment.Jul 17 2022, 7:51 AM

Hm, might it be better to teach LLVM about this (in some ConstantRange/KnownBits logic)? This doesn't really seem like something the frontend should know about and encode, as that means each frontend has to add this annotation by itself.

Hm, might it be better to teach LLVM about this (in some ConstantRange/KnownBits logic)? This doesn't really seem like something the frontend should know about and encode, as that means each frontend has to add this annotation by itself.

Agreed that teaching LLVM about his saves other frontends from doing the same thing. The information is inline asm semantic defined by GCC extension though, I think putting it in the frontend is a valid choice and makes the implementation easy.

I looked into where to put this in LLVM. It seems nontrivial: existing ConstantRanges are computed from binary-op/comparsion etc. Inferring range from an inline asm call is something passes (LazyValueInfo) need to learn, with some kinds of def-use chain walking. That said, I'm definitely new to ConstantRange analysis, it may well be that this is not that hard to implement. Could you please give some guidance?

void added a subscriber: void.Jul 19 2022, 3:21 PM
nikic accepted this revision.Jul 29 2022, 1:34 AM

LGTM. After some further consideration, implementing this properly in LLVM would probably take more effort than is worthwhile (especially as this is target-specific functionality, so we'd actually have to expose TTI queries for this, etc.)

clang/lib/CodeGen/CGStmt.cpp
2734

The canonical form of this is < 2 rather than <= 1.

clang/test/CodeGen/inline-asm-x86-flag-output.c
378

You might want to check that we're doing the right thing if there are multiple output constraints (via extractvalue).

This revision is now accepted and ready to land.Jul 29 2022, 1:34 AM
ychen updated this revision to Diff 448732.Jul 29 2022, 3:29 PM
  • use < 2
  • check multiple flag outputs
ychen updated this revision to Diff 448733.Jul 29 2022, 3:30 PM
  • update test
ychen marked 2 inline comments as done.Jul 29 2022, 3:33 PM

LGTM. After some further consideration, implementing this properly in LLVM would probably take more effort than is worthwhile (especially as this is target-specific functionality, so we'd actually have to expose TTI queries for this, etc.)

Agreed. Thanks for the review.

clang/test/CodeGen/inline-asm-x86-flag-output.c
378

That's a good idea. Done.

nickdesaulniers accepted this revision.Aug 1 2022, 2:39 PM

Thanks for the patch!