This is an archive of the discontinued LLVM Phabricator instance.

Pipe potentially invalid InstructionCost through CodeMetrics
ClosedPublic

Authored by reames on Jun 6 2022, 10:54 AM.

Details

Summary

Per the documentation in Support/InstructionCost.h, the purpose of an invalid cost is so that clients can change behavior on impossible to cost inputs. CodeMetrics was instead asserting that invalid costs never occurred.

On a target with an incomplete cost model - e.g. RISCV - this means that transformations would crash on (falsely) invalid constructs - e.g. scalable vectors. While we certainly should improve the cost model - and I plan to do so in the near future - we also shouldn't be crashing. This violates the explicitly stated purpose of an invalid InstructionCost.

I updated all of the "easy" consumers where bailouts were locally obvious. I plan to follow up with loop unroll in a following change.

Testing wise, I don't have a good answer. I'm about to update the RISC-V model which means that using riscv for tests won't work long term. I could in theory pick some architecture without scalable support (e.g. x86), but that seems somewhat weird. I'm tempted to just leave this untested in the patch. Thoughts?

Diff Detail

Event Timeline

reames created this revision.Jun 6 2022, 10:54 AM
Herald added a project: Restricted Project. · View Herald TranscriptJun 6 2022, 10:54 AM
reames requested review of this revision.Jun 6 2022, 10:54 AM

Thanks for the patch, @reames. I agree that invalid costs should never crash. Not just because it allows targets with incomplete models to work but there might always be something that's truly invalid for a target even with a complete model.

For testing, would we possibly be able to test scalable vectors on RISC-V without V enabled?

llvm/lib/Transforms/Scalar/LoopUnrollPass.cpp
678

gracefully

reames added a comment.Jun 7 2022, 7:56 AM

Thanks for the patch, @reames. I agree that invalid costs should never crash. Not just because it allows targets with incomplete models to work but there might always be something that's truly invalid for a target even with a complete model.

For testing, would we possibly be able to test scalable vectors on RISC-V without V enabled?

Good idea. I'll add some basic test coverage for the cost model printer itself with that approach. I don't plan on adding individual tests for each transform pass - mostly because figuring out to trigger each with a small test case seems quite tedious and not particularly worthwhile.

reames added a comment.Jun 7 2022, 8:20 AM

Thanks for the patch, @reames. I agree that invalid costs should never crash. Not just because it allows targets with incomplete models to work but there might always be something that's truly invalid for a target even with a complete model.

For testing, would we possibly be able to test scalable vectors on RISC-V without V enabled?

Good idea. I'll add some basic test coverage for the cost model printer itself with that approach. I don't plan on adding individual tests for each transform pass - mostly because figuring out to trigger each with a small test case seems quite tedious and not particularly worthwhile.

Actually, I wasn't thinking here. We already have a CostModel test for this, the CodeMetrics layer is one level up and we don't have a dedicated printer pass for that. Given that, the only options would be a) add a printer pass, or b) exercise through one of the transforms. The only transform I'd have even a clue how to approach is rotate, so I'll start there. If that turns out not to be straight forward, I'll come back and ask for permission to land without tests. :)

craig.topper added inline comments.Jun 7 2022, 8:56 AM
llvm/lib/Transforms/Scalar/DFAJumpThreading.cpp
834

Are we using "illegal" or "invalid""? In LoopRotationUtils.cpp you used "invalid"

reames added inline comments.Jun 7 2022, 10:01 AM
llvm/lib/Transforms/Scalar/DFAJumpThreading.cpp
834

Invalid is the right term. I'll fix the others. Thanks.

reames updated this revision to Diff 434860.Jun 7 2022, 10:11 AM

Address review comments.

nikic added inline comments.Jun 8 2022, 3:58 AM
llvm/lib/Transforms/Scalar/DFAJumpThreading.cpp
836

ConvergentInst here probably copy&paste mistake?

reames added inline comments.Jun 8 2022, 7:07 AM
llvm/lib/Transforms/Scalar/DFAJumpThreading.cpp
836

Yep, will fix.

reames added a comment.Jun 8 2022, 7:22 AM

Since we seem to be down to type fixes, any chance I can get an LGTM? Will of course fix any typos noted before landing.

craig.topper accepted this revision.Jun 9 2022, 12:37 PM

LGTM

llvm/include/llvm/Analysis/CodeMetrics.h
52

Is this field misnamed? It seems like that was already the case so I won't block the patch.

This revision is now accepted and ready to land.Jun 9 2022, 12:37 PM
This revision was landed with ongoing or failed builds.Jun 9 2022, 3:17 PM
This revision was automatically updated to reflect the committed changes.