This is an archive of the discontinued LLVM Phabricator instance.

[AMDGPU] Fix negative offset values interpretation in getMemOperandsWithOffset for DS
ClosedPublic

Authored by JanekvO on Apr 24 2023, 10:26 AM.

Details

Summary

The offset values may result in an erroneous scheduling of a load before write for a memory location if the offset values are represented as negative values in MIR, despite actually being unsigned values. This representation in MIR happens as SelectionDAG::getConstant could go through APInt to represent the encoding which assumes the MSB of the encoding as a sign-bit, regardless of whether it is supposed to be a signed value. The 8-bit negative (interpreted) value gets cast to an unsigned 32 bit value in getMemOperandsWithOffset used for comparisons in areMemAccessesTriviallyDisjoint eventually leading to an erroneous schedule in the machine scheduler.

Diff Detail

Event Timeline

JanekvO created this revision.Apr 24 2023, 10:26 AM
Herald added a project: Restricted Project. · View Herald TranscriptApr 24 2023, 10:26 AM
JanekvO requested review of this revision.Apr 24 2023, 10:26 AM
Herald added a project: Restricted Project. · View Herald TranscriptApr 24 2023, 10:26 AM
arsenm added inline comments.Apr 24 2023, 11:54 AM
llvm/lib/Target/AMDGPU/SIInstrInfo.cpp
332–333

Don't need to involve APInt? Just use int64_t like every imm operand really is

JanekvO added inline comments.Apr 25 2023, 7:08 AM
llvm/lib/Target/AMDGPU/SIInstrInfo.cpp
332–333

The added tests will fail in that case, Offset0Op->getImm() returns a negative value while the operand is supposed to be unsigned so we need to force unsigned type for the correct number of bits. I could change to uint8_t since it seems that all cases of this instruction will have 8 bit operands for the offsets.

foad added inline comments.Apr 25 2023, 7:17 AM
llvm/lib/Target/AMDGPU/SIInstrInfo.cpp
332–333
JanekvO updated this revision to Diff 516805.Apr 25 2023, 7:56 AM
  • Change method to force unsigned 8-bit value
JanekvO marked 2 inline comments as done.Apr 25 2023, 7:57 AM
arsenm added inline comments.Apr 25 2023, 6:58 PM
llvm/test/CodeGen/AMDGPU/triv-disjoint-mem-access-neg-offset.mir
79–124

Can use -simplify-mir (which really should be the default)

150

I don't think you really need the IR references (and then you could drop the whole IR section)

JanekvO updated this revision to Diff 517132.Apr 26 2023, 5:17 AM
JanekvO marked 2 inline comments as done.
  • Remove superfluous sections in mir test
arsenm accepted this revision.Apr 26 2023, 5:22 AM
arsenm added inline comments.
llvm/test/CodeGen/AMDGPU/triv-disjoint-mem-access-neg-offset.mir
2

It should be able to figure out it's mir without -x

Can you run just the scheduler instead of -start-after?

This revision is now accepted and ready to land.Apr 26 2023, 5:22 AM
foad accepted this revision.Apr 26 2023, 5:24 AM

LGTM but please try to cut down the test case some more. Usually you only need name:, tracksRegLiveness: and body:.

JanekvO updated this revision to Diff 517138.Apr 26 2023, 6:06 AM
JanekvO marked an inline comment as done.
  • Further simplify mir test
This revision was landed with ongoing or failed builds.Apr 26 2023, 6:10 AM
This revision was automatically updated to reflect the committed changes.