This is an archive of the discontinued LLVM Phabricator instance.

[AMDGPU] Do not crash on agpr_hi16 in AMDGPUResourceUsageAnalysis
ClosedPublic

Authored by Pierre-vh on Apr 15 2023, 11:30 AM.

Diff Detail

Event Timeline

foad created this revision.Apr 15 2023, 11:30 AM
Herald added a project: Restricted Project. · View Herald TranscriptApr 15 2023, 11:30 AM
foad requested review of this revision.Apr 15 2023, 11:30 AM
Herald added a project: Restricted Project. · View Herald TranscriptApr 15 2023, 11:30 AM
foad added inline comments.Apr 15 2023, 11:35 AM
llvm/test/CodeGen/AMDGPU/resource-usage-agpr-hi16.ll
43

Note that $agpr0_hi16 is defined but there is no AGPR_HI16 register class.

These mentions of AGPR hi16 regs were introduced by BranchFolderPass.

There's a test failure, otherwise LGTM so I'll approve when it passes

llvm/test/CodeGen/AMDGPU/resource-usage-agpr-hi16.ll
43

small nit: as we don't care about most of the output, perhaps just check for that line only instead of generating the whole test?
I would also add a comment to the test to explain that we're checking that agpr_16 doesn't crash resource usage analysis because it doesn't have an associated RC.

foad updated this revision to Diff 514123.Apr 17 2023, 12:41 AM

Ungenerate test checks.

Pierre-vh accepted this revision.Apr 17 2023, 1:00 AM

LGTM if the test passes

This revision is now accepted and ready to land.Apr 17 2023, 1:00 AM
foad added a comment.Apr 17 2023, 2:22 AM

An alternative would be to fix/optimize the branch folder. Currently BranchFolder::mergeCommonTails adds IMPLICIT_DEFs based on the regs computed by LivePhysRegs:computeLiveIns before calling addLiveIns, which means it lacks the "Skip the register if we are about to add one of its super registers" optimization from addLiveIns and adds redundant IMPLICIT_DEFs of subregs. But I haven't found a neat way to implement this yet.

foad updated this revision to Diff 514145.Apr 17 2023, 2:27 AM

Add a TODO.

An alternative would be to fix/optimize the branch folder. Currently BranchFolder::mergeCommonTails adds IMPLICIT_DEFs based on the regs computed by LivePhysRegs:computeLiveIns before calling addLiveIns, which means it lacks the "Skip the register if we are about to add one of its super registers" optimization from addLiveIns and adds redundant IMPLICIT_DEFs of subregs. But I haven't found a neat way to implement this yet.

This sounds like it definitely needs fixing, but I feel like this is a separate issue. Resource usage analysis shouldn't crash on valid code, though i guess the definition of "valid" is quite loose here - agpr_hi16 isn't addressable I think so it's not exactly valid, but it doesn't crash at emission so I would say there's no reason for resource usage analysis to crash.

Pierre-vh commandeered this revision.Apr 17 2023, 4:56 AM
Pierre-vh edited reviewers, added: foad; removed: Pierre-vh.
This revision now requires review to proceed.Apr 17 2023, 4:56 AM
Pierre-vh planned changes to this revision.Apr 17 2023, 4:56 AM
Pierre-vh updated this revision to Diff 514168.Apr 17 2023, 5:19 AM

Take over patch

Pierre-vh retitled this revision from [AMDGPU] Allow AGPR hi16 regs in AMDGPUResourceUsageAnalysis to [AMDGPU] Do not crash on unknown register in AMDGPUResourceUsageAnalysis.Apr 17 2023, 5:19 AM
Pierre-vh edited reviewers, added: arsenm, rampitec; removed: kparzysz, MatzeB.
foad added a comment.Apr 17 2023, 5:22 AM

LGTM but you should probably get an independent reviewer.

JonChesterfield added inline comments.
llvm/lib/Target/AMDGPU/AMDGPUResourceUsageAnalysis.cpp
486

The structure above the assert definitely looks error prone, but on a glance it seems plausible that we should be adding the missing cases for agpr_lo16 and keeping the assert

foad added inline comments.Apr 17 2023, 6:14 AM
llvm/lib/Target/AMDGPU/AMDGPUResourceUsageAnalysis.cpp
486

We can't easily add AMDGPU::AGPR_HI16RegClass.contains(Reg) above because there is no AGPR_HI16 register class. I guess we could define one, but that seems like an invasive change just to avoid an assertion failure here.

Pierre-vh added inline comments.Apr 17 2023, 6:32 AM
llvm/lib/Target/AMDGPU/AMDGPUResourceUsageAnalysis.cpp
486

I'm also not a fan of asserting on non ill-formed MIR. I think this assert is intended to catch missing/uncovered RCs but I don't think this is the right way to do it as lit tests aren't guaranteed to catch it.

If it's really important to check that this pass doesn't miss any register (class), IMO the logic should be moved into a helper w/ a unit test that iterates over all RCs instead so it catches missing cases much earlier.

rampitec added inline comments.Apr 17 2023, 10:20 AM
llvm/lib/Target/AMDGPU/AMDGPUResourceUsageAnalysis.cpp
486

You have TargetRegisterInfo here, so you may keep the assert and complement it with the check || !getPhysRegBaseClass(Reg). I.e. the check that the register does not belong to any RC.

Pierre-vh marked 3 inline comments as done.

Add the assert back in

Pierre-vh retitled this revision from [AMDGPU] Do not crash on unknown register in AMDGPUResourceUsageAnalysis to [AMDGPU] Do not crash on agpr_hi16 in AMDGPUResourceUsageAnalysis.Apr 18 2023, 12:19 AM
arsenm accepted this revision.Apr 18 2023, 4:27 AM
This revision is now accepted and ready to land.Apr 18 2023, 4:27 AM