This is an archive of the discontinued LLVM Phabricator instance.

[TableGen] Fix sort order of asm operand classes
ClosedPublic

Authored by olista01 on Jan 13 2016, 4:01 AM.

Details

Summary

This is a fix for https://llvm.org/bugs/show_bug.cgi?id=22796.

The previous implementation of ClassInfo::operator< allowed cycles of classes such that x < y < z < x, meaning that a list of them cannot be correctly sorted, and the sort order could differ with different standard libraries.

The original implementation sorted classes by ValueName if they were otherwise equal. This isn't strictly necessary, but some backends seem to accidentally rely on it. If I reverse this comparison I get 8 test failures spread across the AArch64, Mips and X86 backends, so I have left it in until those backends can be fixed.

There was one case in the X86 backend where the observable behaviour of the assembler is changed by this patch. This was because some of the memory asm operands were not marked as children of X86MemAsmOperand.

This bug caused my patch to add the ARMv8.2 FP16 AArch32 scalar instructions (http://reviews.llvm.org/rL255762) to fail on windows.

Diff Detail

Repository
rL LLVM

Event Timeline

olista01 updated this revision to Diff 44733.Jan 13 2016, 4:01 AM
olista01 retitled this revision from to [TableGen] Fix sort order of asm operand classes.
olista01 updated this object.
olista01 added reviewers: dblaikie, dexonsmith.
olista01 set the repository for this revision to rL LLVM.
olista01 added a subscriber: llvm-commits.
dexonsmith edited edge metadata.Jan 18 2016, 4:15 PM
dexonsmith added a subscriber: dexonsmith.

Thanks for working on this!

FWIW this fix looks right to me, but I'm not sure I've ever committed
to tablegen. Please track down someone that knows the code better to
chime in.

+ / operator< - Compare two classes. This does not produce a total ordering,
+
/ but does guarantee that subclasses are sorted before their parents, and
+ /// that the ordering is transitive.

Since you're changing the comment anyway, can you update the style
(remove the function name at the start of it)?

olista01 updated this revision to Diff 45233.Jan 19 2016, 1:36 AM
olista01 edited edge metadata.
olista01 removed rL LLVM as the repository for this revision.

Updated doxygen comment style.

If I reverse this comparison I get 8 test failures spread across the AArch64, Mips and X86 backends, so I have left it in until those backends can be fixed.

The Mips failure is that there's a choice of two accurate error messages and changing the order of these matchables changes which one is emitted. Of the two, the current one is preferable (since it slightly exaggerates its expected values to accommodate both possible matches) but ideally we would be able to emit multiple reasons for a failed match (e.g. 'expected either unsigned 5 bit immediate or values 32..63').

The ValueName is the only available tie breaker at the moment. Superclasses could be used to produce the right effect but it seems wrong to declare one a superclass of the other when the accepted values are distinct sets. At the moment, I'm thinking we should just make the priority explicit using something similar to the AddedComplexity field from instruction selection. Do you agree?

craig.topper added inline comments.Jan 20 2016, 10:11 PM
utils/TableGen/AsmMatcherEmitter.cpp
267 ↗(On Diff #45233)

Variable names should be capitalized by coding standards.

1596 ↗(On Diff #45233)

Can we use std::is_sorted here and the location below?

craig.topper added inline comments.Jan 20 2016, 10:20 PM
utils/TableGen/AsmMatcherEmitter.cpp
319 ↗(On Diff #45233)

Should this say "name" instead of "kind"?

olista01 added inline comments.Jan 21 2016, 1:46 AM
utils/TableGen/AsmMatcherEmitter.cpp
1596 ↗(On Diff #45233)

std::is_sorted just checks adjacent elements, and assumes that operator< is well-behaved, so wouldn't have caught this bug before it started causing (windows-only in my case) test failures.

olista01 updated this revision to Diff 45496.Jan 21 2016, 1:47 AM
craig.topper accepted this revision.Jan 23 2016, 11:34 AM
craig.topper edited edge metadata.

LGTM

This revision is now accepted and ready to land.Jan 23 2016, 11:34 AM
This revision was automatically updated to reflect the committed changes.