This is an archive of the discontinued LLVM Phabricator instance.

Avoid 8 and 16bit switch conditions on x86
ClosedPublic

Authored by MatzeB on May 3 2022, 6:16 PM.

Details

Summary

This adds a TargetLoweringBase::getSwitchConditionType callback to give targets a chance to control the type used in CodeGenPrepare::optimizeSwitchInst.

Implement callback for X86 to avoid i8 and i16 types where possible as they often incur extra zero-extensions.

This is NFC for non-X86 targets.

Diff Detail

Event Timeline

MatzeB created this revision.May 3 2022, 6:16 PM
Herald added a project: Restricted Project. · View Herald TranscriptMay 3 2022, 6:16 PM
MatzeB requested review of this revision.May 3 2022, 6:16 PM
Herald added a project: Restricted Project. · View Herald TranscriptMay 3 2022, 6:16 PM
MatzeB edited the summary of this revision. (Show Details)May 3 2022, 6:17 PM
MatzeB updated this revision to Diff 426886.May 3 2022, 6:26 PM

Add a comment.

craig.topper added inline comments.May 4 2022, 12:17 PM
llvm/lib/CodeGen/TargetLoweringBase.cpp
1614

If we just gave targets control over RegType here would that be enough?

MatzeB added inline comments.May 4 2022, 12:46 PM
llvm/lib/CodeGen/TargetLoweringBase.cpp
1614

The callback is used by CodeGenPrepare though which deals with llvm IR and rather has Type*s than MVTs...

craig.topper added inline comments.May 4 2022, 12:51 PM
llvm/lib/CodeGen/TargetLoweringBase.cpp
1614

Ok could we return ExtType from the target and use ExtType to calculate RegWidth for if on 1617?

Realy, I'm wondering why we had to move all of the code into TargetLowering and duplicate Argument attributes checking in X86. Or is there some subtle difference that prevents us from sharing the Argument attribute handling.

MatzeB added inline comments.May 4 2022, 1:10 PM
llvm/lib/CodeGen/TargetLoweringBase.cpp
1614

If you prefer we can share the logic starting at line 1632 (deciding between ZExt/SExt and the argument handling) simplifying the X86 callback with the drawback that targets can no longer opt-out of that logic (admittedly I don't know why they would want to opt-out, so I don't care too deeply).

MatzeB added inline comments.May 4 2022, 1:20 PM
llvm/lib/CodeGen/TargetLoweringBase.cpp
1614

Hmm we need an MVT if we want to have the isSExtCheaperThanZExt in the shared code as there's no equivalent callback for Type*...

craig.topper added inline comments.May 4 2022, 1:28 PM
llvm/lib/CodeGen/TargetLoweringBase.cpp
1607

I think we would typically pass ExtOp by reference so you can't pass null.

1614

I guess it might be better to give targets the control. There was a patch proposing to ignore the attributes on X86. D122963.

MatzeB updated this revision to Diff 427125.May 4 2022, 1:51 PM
MatzeB retitled this revision from Prefer 32bit and 64bit switch conditions for x86 to Avoid 8 and 16bit switch conditions on x86.
MatzeB edited the summary of this revision. (Show Details)

Address review comments: Keep more code shared.

MatzeB marked an inline comment as done.May 4 2022, 1:55 PM
MatzeB added inline comments.
llvm/lib/CodeGen/TargetLoweringBase.cpp
1607

I think we would typically pass ExtOp by reference so you can't pass null.

No longer relevant as the parameter is gone now, but FWIW:

I prefer pointers for "out parameters" because that makes it clearer when readining the calling code that there is an "out parameter".

i.e. foobar(&baz) has a clear signal that baz is probably an "out parameter", for foobar(baz) it's easy to miss this when baz happens to be a writable reference.

I find that more valuable than having an assurance that nobody passes nullptr...

MatzeB marked an inline comment as done.May 4 2022, 1:56 PM
MatzeB added inline comments.
llvm/lib/CodeGen/TargetLoweringBase.cpp
1614

Oh, I just switched it to a more minimal version of this patch. Should I go back to the old version?

MatzeB added inline comments.May 4 2022, 2:00 PM
llvm/lib/CodeGen/TargetLoweringBase.cpp
1614

There was a patch proposing to ignore the attributes on X86

FWIW: While working on this I did originally not replicate the logic in the X86 callback and the result was worse at least for the llvm unit-tests.

I think ultimately we had a global instruction selection and wouldn't need to rely on IR->IR transformations upfront... (because I think for optimal results you want to control the overflow-check independently of the value used for the jump-table address calculations; it also depends about what sorts of extensions and truncations the target gets for free; etc.).

But yeah I think for the LLVM of today this patch here at least improves some cases.

craig.topper accepted this revision.May 4 2022, 2:11 PM

LGTM with what I think is a typo in the X86 override fixed.

llvm/lib/CodeGen/TargetLoweringBase.cpp
1607

Yeah that is an unfortunate annoyance of the syntax. I was basing on what I usually see done in code. Our coding standards probably say nothing about this.

1614

We can keep the minimal patch. If the x86 patch were to go through and ignore the attribute, it would only be for i8/i16 types. Extending those to i32 isn't free or more costly no matter which we choose.

llvm/lib/Target/X86/X86ISelLowering.cpp
33724

the -> they?

This revision is now accepted and ready to land.May 4 2022, 2:11 PM
spatel accepted this revision.May 5 2022, 5:30 AM
spatel added inline comments.
llvm/include/llvm/CodeGen/TargetLowering.h
1226

I'd put the "preferred" in the name itself to make the logic clear:

getPreferredSwitchCondType(...)
llvm/lib/Target/X86/X86ISelLowering.cpp
33726

Nit: can remove braces for one-line 'if'

MatzeB updated this revision to Diff 427378.May 5 2022, 10:07 AM
MatzeB marked 6 inline comments as done.

address review feedback

MatzeB marked 2 inline comments as done.May 5 2022, 10:08 AM
This revision was landed with ongoing or failed builds.May 10 2022, 10:01 AM
This revision was automatically updated to reflect the committed changes.