Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
88 changes: 83 additions & 5 deletions llvm/lib/Target/AMDGPU/AMDGPUISelDAGToDAG.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -328,9 +328,6 @@ bool AMDGPUDAGToDAGISel::matchLoadD16FromBuildVector(SDNode *N) const {
}

void AMDGPUDAGToDAGISel::PreprocessISelDAG() {
if (!Subtarget->d16PreservesUnusedBits())
return;

SelectionDAG::allnodes_iterator Position = CurDAG->allnodes_end();

bool MadeChange = false;
Expand All @@ -341,8 +338,23 @@ void AMDGPUDAGToDAGISel::PreprocessISelDAG() {

switch (N->getOpcode()) {
case ISD::BUILD_VECTOR:
// TODO: Match load d16 from shl (extload:i16), 16
MadeChange |= matchLoadD16FromBuildVector(N);
// D16 optimization requires subtarget support
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a weird place to put the optimization. This doesn't have the special generalized RMW property that the d16 loads do.

Buffers don't just come out of nowhere, you could do this on the IR.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we were doing this on the MIR, I'd note that we do [vgpr + add] separation in ISelLowering/InstructionLegalizer.

So if anything I'd expect this to be with the other instruction-selection machinery.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't that for the legacy intrinsics? The new intrinsics have the explicit soffset and voffset operands

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No

Even with the new ones, if I have /*voffset=*/%x + 16 in the IR, ISel will pattern-match that + 16 part into offset:16 on the instruction

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So I claim that voffset => soffset should either go there or be an IR pass after uniformity analysis like you suggested.

(Making it an IR pass also means you can do s_buffer_load promotion there)

Copy link
Author

@PrasoonMishra PrasoonMishra Sep 29, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@arsenm Thank you for the clarification. I wasn't aware of the RMW property distinction that justifies the D16 optimization in PreprocessISelDAG.
As suggested an IR pass after uniformity analysis will be better. I can also enable the s_buffer_load promotion in the same pass as mentioned by @krzysz00. I'll rework this as an IR transformation.

if (Subtarget->d16PreservesUnusedBits()) {
// TODO: Match load d16 from shl (extload:i16), 16
MadeChange |= matchLoadD16FromBuildVector(N);
}
break;
case AMDGPUISD::BUFFER_LOAD:
case AMDGPUISD::BUFFER_LOAD_UBYTE:
case AMDGPUISD::BUFFER_LOAD_USHORT:
case AMDGPUISD::BUFFER_LOAD_BYTE:
case AMDGPUISD::BUFFER_LOAD_SHORT:
MadeChange |= sinkUniformAddendIntoSOffset(N, false);
break;
case AMDGPUISD::BUFFER_STORE:
case AMDGPUISD::BUFFER_STORE_BYTE:
case AMDGPUISD::BUFFER_STORE_SHORT:
MadeChange |= sinkUniformAddendIntoSOffset(N, true);
break;
default:
break;
Expand All @@ -356,6 +368,72 @@ void AMDGPUDAGToDAGISel::PreprocessISelDAG() {
}
}

/// Sink uniform addends in buffer address calculations into SOffset.
///
/// Transforms buffer loads/stores with voffset = add(uniform, divergent)
/// into voffset = divergent, soffset = uniform for better address coalescing.
/// Only applies when the result will use OFFEN addressing mode.
bool AMDGPUDAGToDAGISel::sinkUniformAddendIntoSOffset(SDNode *N, bool IsStore) {

// Buffer operand layout:
// Load: (chain, rsrc, vindex, voffset, soffset, offset, cachepolicy, idxen)
// Store: (chain, vdata, rsrc, vindex, voffset, soffset, offset, cachepolicy, idxen)
const unsigned VIndexIdx = IsStore ? 3 : 2;
const unsigned VOffsetIdx = IsStore ? 4 : 3;
const unsigned SOffsetIdx = IsStore ? 5 : 4;
const unsigned IdxEnIdx = IsStore ? 8 : 7;

if (N->getNumOperands() <= IdxEnIdx)
return false;

SDValue VIndex = N->getOperand(VIndexIdx);
SDValue VOffset = N->getOperand(VOffsetIdx);
SDValue SOffset = N->getOperand(SOffsetIdx);
SDValue IdxEn = N->getOperand(IdxEnIdx);

// Only optimize OFFEN mode: vindex=0, idxen=0 guarantees this
if (!isNullConstant(VIndex) || !isNullConstant(IdxEn))
return false;

// Only optimize when soffset is currently zero
// TODO: Handle non-zero soffset by combining with uniform addend
if (!isNullConstant(SOffset))
return false;

// voffset must be ADD of uniform and divergent values
if (VOffset.getOpcode() != ISD::ADD)
return false;

// Identify uniform and divergent addends
auto IsUniform = [](SDValue V) {
return isa<ConstantSDNode>(V) || !V.getNode()->isDivergent();
};

SDValue LHS = VOffset.getOperand(0);
SDValue RHS = VOffset.getOperand(1);
bool LHSUniform = IsUniform(LHS);
bool RHSUniform = IsUniform(RHS);

// Need exactly one uniform and one divergent operand
if (LHSUniform == RHSUniform)
return false;

SDValue UniformAddend = LHSUniform ? LHS : RHS;
SDValue DivergentAddend = LHSUniform ? RHS : LHS;

// Perform the transformation: sink uniform part into soffset
// SIFixSGPRCopies will handle any SGPR register class fixups if needed.
SmallVector<SDValue, 8> NewOps(N->op_values());
NewOps[VOffsetIdx] = DivergentAddend;
NewOps[SOffsetIdx] = UniformAddend;

LLVM_DEBUG(dbgs() << "Sinking uniform addend into SOffset for buffer "
<< (IsStore ? "store" : "load") << '\n');

CurDAG->UpdateNodeOperands(N, NewOps);
return true;
}

bool AMDGPUDAGToDAGISel::isInlineImmediate(const SDNode *N) const {
if (N->isUndef())
return true;
Expand Down
1 change: 1 addition & 0 deletions llvm/lib/Target/AMDGPU/AMDGPUISelDAGToDAG.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ class AMDGPUDAGToDAGISel : public SelectionDAGISel {

bool runOnMachineFunction(MachineFunction &MF) override;
bool matchLoadD16FromBuildVector(SDNode *N) const;
bool sinkUniformAddendIntoSOffset(SDNode *N, bool IsStore);
void PreprocessISelDAG() override;
void Select(SDNode *N) override;
void PostprocessISelDAG() override;
Expand Down
Loading
Loading