This is an archive of the discontinued LLVM Phabricator instance.

[clang] number labels in asm goto strings after tied inputs
ClosedPublic

Authored by nickdesaulniers on Dec 9 2021, 2:40 PM.

Details

Summary

I noticed that the following case would compile in Clang but not GCC:

void *x(void) {
  void *p = &&foo;
  asm goto ("# %0\n\t# %l1":"+r"(p):::foo);
  foo:;
  return p;
}

Changing the output template above from %l2 would compile in GCC but not
Clang.

This demonstrates that when using tied outputs (say via the "+r" output
constraint), the hidden inputs occur or are numbered BEFORE the labels,
at least with GCC.

In fact, GCC does denote this in its documentation:
https://gcc.gnu.org/onlinedocs/gcc-11.2.0/gcc/Extended-Asm.html#Goto-Labels

Output operand with constraint modifier ‘+’ is counted as two operands
because it is considered as one output and one input operand.

For the sake of compatibility, I think it's worthwhile to just make this
change.

It's better to use symbolic names for compatibility (especially now
between released version of Clang that support asm goto with outputs).
ie. %l1 from the above would be %l[foo]. The GCC docs also make this
recommendation.

Also, I cleaned up some cruft in GCCAsmStmt::getNamedOperand. AFAICT,
NumPlusOperands was no longer used, though I couldn't find which commit
didn't clean that up correctly.

Link: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=98096
Link: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=103640
Link: https://gcc.gnu.org/onlinedocs/gcc-11.2.0/gcc/Extended-Asm.html#Goto-Labels

Diff Detail

Event Timeline

nickdesaulniers requested review of this revision.Dec 9 2021, 2:40 PM
nickdesaulniers created this revision.
Herald added a project: Restricted Project. · View Herald TranscriptDec 9 2021, 2:40 PM
Herald added a subscriber: cfe-commits. · View Herald Transcript

It's rather sad that GCC made the quite-unintuitive decision to number the arguments in this way -- LONG AFTER clang had already implemented the other way...

It's rather sad that GCC made the quite-unintuitive decision to number the arguments in this way -- LONG AFTER clang had already implemented the other way...

I doubt it was intentional or tested or that it's too late to at least file a bug report.
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=103640

nickdesaulniers edited the summary of this revision. (Show Details)
  • note this semantic change in the release notes, add links to GCC docs
  • fix typo in release notes
xbolva00 added inline comments.
clang/docs/ReleaseNotes.rst
160

there is no general documentation? this is kinda bad to have it only in release notes.

clang/docs/ReleaseNotes.rst
160

Documenting this in clang/docs/LanguageExtensions.rst would be good.

nickdesaulniers marked an inline comment as done.Dec 14 2021, 4:53 PM
void accepted this revision.Dec 21 2021, 12:29 PM
void added inline comments.
clang/docs/LanguageExtensions.rst
1474

nit: s/ie./i.e./

This revision is now accepted and ready to land.Dec 21 2021, 12:29 PM
This revision was landed with ongoing or failed builds.Jan 11 2022, 12:10 PM
This revision was automatically updated to reflect the committed changes.
nickdesaulniers marked an inline comment as done.Jan 11 2022, 12:10 PM