Skip to content

[AMDGPU] Support TFE D16 format buffer loads on gfx8/10/11/12 - #211465

Merged
aobolensk merged 11 commits into
llvm:mainfrom
aobolensk:llvm-amdgpu-tfe-d16-loads
Sep 13, 2026
Merged

aobolensk merged 11 commits into
llvm:mainfrom
aobolensk:llvm-amdgpu-tfe-d16-loads

Conversation

@aobolensk

@aobolensk aobolensk commented Jul 23, 2026 •

Copy link
Copy Markdown
Contributor

gfx90a has no hardware encoding for TFE D16 format buffer loads, so reject the combination there with a diagnostic. Other targets (gfx8/10/11/12) do have real encodings, so add codegen support for them instead of rejecting unconditionally.

@llvmorg-github-actions

Copy link
Copy Markdown

@llvm/pr-subscribers-backend-amdgpu

Author: Arseniy Obolenskiy (aobolensk)

Changes

D16 format buffer loads have no TFE hardware variant


Full diff: https://github.com/llvm/llvm-project/pull/211465.diff

3 Files Affected:

  • (modified) llvm/lib/Target/AMDGPU/AMDGPULegalizerInfo.cpp (+10)
  • (modified) llvm/lib/Target/AMDGPU/SIISelLowering.cpp (+18-6)
  • (modified) llvm/test/CodeGen/AMDGPU/llvm.amdgcn.struct.ptr.buffer.format.i8.xfail.ll (+23)
diff --git a/llvm/lib/Target/AMDGPU/AMDGPULegalizerInfo.cpp b/llvm/lib/Target/AMDGPU/AMDGPULegalizerInfo.cpp
index 87e85bef7c701..8db7fd4d5541d 100644
--- a/llvm/lib/Target/AMDGPU/AMDGPULegalizerInfo.cpp
+++ b/llvm/lib/Target/AMDGPU/AMDGPULegalizerInfo.cpp
@@ -6901,6 +6901,16 @@ bool AMDGPULegalizerInfo::legalizeBufferLoad(MachineInstr &MI,
     return true;
   }
 
+  if (IsFormat && !IsTyped && IsD16 && IsTFE) {
+    const Function &Fn = B.getMF().getFunction();
+    Fn.getContext().diagnose(DiagnosticInfoUnsupported(
+        Fn, "unsupported TFE D16 format buffer load", MI.getDebugLoc()));
+    B.buildUndef(Dst);
+    B.buildUndef(StatusDst);
+    MI.eraseFromParent();
+    return true;
+  }
+
   std::tie(VOffset, ImmOffset) = splitBufferOffsets(B, VOffset);
 
   unsigned Opc;
diff --git a/llvm/lib/Target/AMDGPU/SIISelLowering.cpp b/llvm/lib/Target/AMDGPU/SIISelLowering.cpp
index 8156de7a24b01..c14a06aa9bc97 100644
--- a/llvm/lib/Target/AMDGPU/SIISelLowering.cpp
+++ b/llvm/lib/Target/AMDGPU/SIISelLowering.cpp
@@ -7835,6 +7835,15 @@ SDValue SITargetLowering::lowerIntrinsicLoad(MemSDNode *M, bool IsFormat,
   assert(M->getNumValues() == 2 || M->getNumValues() == 3);
   bool IsTFE = M->getNumValues() == 3;
 
+  if (IsD16 && IsTFE) {
+    DAG.getContext()->diagnose(DiagnosticInfoUnsupported(
+        DAG.getMachineFunction().getFunction(),
+        "unsupported TFE D16 format buffer load", DL.getDebugLoc()));
+    return DAG.getMergeValues(
+        {DAG.getPOISON(LoadVT), DAG.getPOISON(MVT::i32), M->getOperand(0)},
+        DL);
+  }
+
   unsigned Opc = IsFormat ? (IsTFE ? AMDGPUISD::BUFFER_LOAD_FORMAT_TFE
                                    : AMDGPUISD::BUFFER_LOAD_FORMAT)
                  : IsTFE  ? AMDGPUISD::BUFFER_LOAD_TFE
@@ -7855,12 +7864,15 @@ SDValue SITargetLowering::lowerIntrinsicLoad(MemSDNode *M, bool IsFormat,
   }
 
   EVT CastVT = getEquivalentMemType(*DAG.getContext(), LoadVT);
-  SDVTList VTList = DAG.getVTList(CastVT, MVT::Other);
+  SDVTList VTList = IsTFE ? DAG.getVTList(CastVT, MVT::i32, MVT::Other)
+                          : DAG.getVTList(CastVT, MVT::Other);
   SDValue MemNode = getMemIntrinsicNode(Opc, DL, VTList, Ops, CastVT,
                                         M->getMemOperand(), DAG);
-  return DAG.getMergeValues(
-      {DAG.getNode(ISD::BITCAST, DL, LoadVT, MemNode), MemNode.getValue(1)},
-      DL);
+  SDValue Data = DAG.getNode(ISD::BITCAST, DL, LoadVT, MemNode);
+  if (IsTFE)
+    return DAG.getMergeValues(
+        {Data, MemNode.getValue(1), MemNode.getValue(2)}, DL);
+  return DAG.getMergeValues({Data, MemNode.getValue(1)}, DL);
 }
 
 static SDValue lowerICMPIntrinsic(const SITargetLowering &TLI, SDNode *N,
@@ -8407,8 +8419,8 @@ void SITargetLowering::ReplaceNodeResults(SDNode *N,
           Results.push_back(Res.getOperand(I));
         }
       } else {
-        Results.push_back(Res);
-        Results.push_back(Res.getValue(1));
+        for (unsigned I = 0; I < N->getNumValues(); ++I)
+          Results.push_back(Res.getValue(I));
       }
       return;
     }
diff --git a/llvm/test/CodeGen/AMDGPU/llvm.amdgcn.struct.ptr.buffer.format.i8.xfail.ll b/llvm/test/CodeGen/AMDGPU/llvm.amdgcn.struct.ptr.buffer.format.i8.xfail.ll
index f5f8471f4dc1f..df137c5df98ea 100644
--- a/llvm/test/CodeGen/AMDGPU/llvm.amdgcn.struct.ptr.buffer.format.i8.xfail.ll
+++ b/llvm/test/CodeGen/AMDGPU/llvm.amdgcn.struct.ptr.buffer.format.i8.xfail.ll
@@ -18,3 +18,26 @@ define amdgpu_ps void @store_i8(ptr addrspace(8) inreg %rsrc, i8 %data, i32 %ind
   call void @llvm.amdgcn.struct.ptr.buffer.store.format.i8(i8 %data, ptr addrspace(8) %rsrc, i32 %index, i32 0, i32 0, i32 0)
   ret void
 }
+
+; A D16 buffer.load.format combined with TFE has no corresponding real
+; instruction (no TFE variant of the D16 format load opcodes exists in
+; hardware), so it must be refused instead of miscounting SDNode results.
+; CHECK: error: {{.*}}unsupported TFE D16 format buffer load
+define amdgpu_kernel void @load_v3i16_tfe(ptr addrspace(8) inreg %rsrc, ptr addrspace(1) %out, ptr addrspace(1) %status) {
+  %r = call {<3 x i16>, i32} @llvm.amdgcn.struct.ptr.buffer.load.format.sl_v3i16i32s(ptr addrspace(8) %rsrc, i32 0, i32 0, i32 0, i32 0)
+  %data = extractvalue {<3 x i16>, i32} %r, 0
+  %st = extractvalue {<3 x i16>, i32} %r, 1
+  store <3 x i16> %data, ptr addrspace(1) %out
+  store i32 %st, ptr addrspace(1) %status
+  ret void
+}
+
+; CHECK: error: {{.*}}unsupported TFE D16 format buffer load
+define amdgpu_kernel void @load_f16_tfe(ptr addrspace(8) inreg %rsrc, ptr addrspace(1) %out, ptr addrspace(1) %status) {
+  %r = call {half, i32} @llvm.amdgcn.struct.ptr.buffer.load.format.sl_f16i32s(ptr addrspace(8) %rsrc, i32 0, i32 0, i32 0, i32 0)
+  %data = extractvalue {half, i32} %r, 0
+  %st = extractvalue {half, i32} %r, 1
+  store half %data, ptr addrspace(1) %out
+  store i32 %st, ptr addrspace(1) %status
+  ret void
+}

@github-actions

github-actions Bot commented Jul 23, 2026 •

Copy link
Copy Markdown

✅ With the latest revision this PR passed the C/C++ code formatter.

@aobolensk
aobolensk force-pushed the llvm-amdgpu-tfe-d16-loads branch from 0287c2e to 2b51427 Compare July 23, 2026 06:05
@carlobertolli

Copy link
Copy Markdown
Member

I see TFE+D16 supported for >=gfx10 (but I may be wrong). Should this check be target specific, if so?

@aobolensk
aobolensk requested a review from vangthao95 as a code owner July 30, 2026 09:40
@aobolensk

Copy link
Copy Markdown
Contributor Author

I see TFE+D16 supported for >=gfx10 (but I may be wrong). Should this check be target specific, if so?

You're right, it's a gfx90a-specific hardware limitation...

Narrowed the check to hasGFX90AInsts() and added codegen for D16+TFE format buffer loads on gfx8/gfx10/gfx11/gfx12, which do have real encodings

}

if (IsTFE) {
if (IsTFE && IsD16 && Ty.isVector()) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This is a lot more than introducing a new error?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Yes, renamed the PR

return true;
}

if (IsFormat && !IsTyped && IsD16 && IsTFE && ST.hasGFX90AInsts()) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This should not be a 90a insist check. I would also expect this to be caught by the target feature requirements on the intrinsic declaration

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

TargetFeatures will gate on the whole Intrinsic::ID, but we have some non-TFE loads that are supposed to be allowed

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

You can tag add a target tag for "has D16 TFE".

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

TargetFeatures will gate on the whole Intrinsic::ID, but we have some non-TFE loads that are supposed to be allowed

That seems to make the custom intrinsic feature check necessary. I remember there is a PR somewhere.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This should still be a !has-tfe-d16 check, not has90AInsts

@mgcarrasco

mgcarrasco commented Aug 4, 2026 •

Copy link
Copy Markdown
Contributor

Hi! This comment was generated using fuzz-fill, a new tool we're building to help improve LLVM test coverage by spotting lines introduced in a PR that don't yet appear to be exercised by the full test suite, including any tests added in this PR.

Looking at PR head e8d9ae05ac89, we found 6 lines added or modified here that we couldn't find a test for:

  1. B.buildBitcast(Dst, ValueI16);
  2. SDVTList VTList = IsTFE ? DAG.getVTList(CastVT, MVT::i32, MVT::Other)
  3. return DAG.getMergeValues({Data, MemNode.getValue(1), MemNode.getValue(2)},
  4. for (unsigned I = 0; I < N->getNumValues(); ++I)
    Results.push_back(Res.getValue(I));

If it makes sense, adding a regression test that validates these lines would be great. And if any of them are already tested and we missed it, we'd love to hear so we can improve the tool. Feel free to give us some feedback on whether this review was useful from your perspective. Thanks!

@aobolensk aobolensk changed the title [AMDGPU] Reject unsupported TFE D16 format buffer loads [AMDGPU] Support TFE D16 format buffer loads on gfx8/10/11/12 Aug 4, 2026
@aobolensk
aobolensk requested a review from arsenm August 5, 2026 16:20
SDValue ZeroIdx = DAG.getVectorIdxConstant(0, DL);
SDValue ValueDWords =
NumValueDWords == 1
? DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32, Load, ZeroIdx)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

There're buildels for this, no?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Extracted, done

@aobolensk

Copy link
Copy Markdown
Contributor Author

@krzysz00 @arsenm could you please take a look one more time?

@aobolensk

Copy link
Copy Markdown
Contributor Author

return true;
}

if (IsFormat && !IsTyped && IsD16 && IsTFE && ST.hasGFX90AInsts()) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This should be a has-TFE, not a not-90a

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

changed

bool hasPkMovB32() const { return HasGFX90AInsts; }

// Has a hardware encoding for TFE D16 format buffer loads.
bool hasBufferTFEFormatD16() const { return !HasGFX90AInsts; }

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Should move this to a feature but I suppose that can be separate

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Let's do it in follow up

Comment on lines +7003 to +7032
Register PackedI16;
if (Unpacked) {
SmallVector<Register, 4> Repack;
for (Register R : LoadElts)
Repack.push_back(B.buildTrunc(LLT::integer(16), R).getReg(0));
LLT RepackedTy = LLT::fixed_vector(NumValueDWords, LLT::integer(16));
PackedI16 = B.buildMergeLikeInstr(RepackedTy, Repack).getReg(0);
} else {
Register Merged;
if (NumValueDWords == 1) {
Merged = LoadElts[0];
} else {
LLT MergedTy = LLT::fixed_vector(NumValueDWords, I32);
Merged = B.buildMergeLikeInstr(MergedTy, LoadElts).getReg(0);
}
LLT PackedI16Ty = LLT::fixed_vector(NumValueDWords * 2, LLT::integer(16));
PackedI16 = B.buildBitcast(PackedI16Ty, Merged).getReg(0);
}

LLT PackedI16Ty = B.getMRI()->getType(PackedI16);
LLT DstIntTy = Ty.changeElementType(LLT::integer(16));
Register ValueI16 = PackedI16;
if (PackedI16Ty.getNumElements() != NumElts) {
ValueI16 = B.getMRI()->createGenericVirtualRegister(DstIntTy);
B.buildDeleteTrailingVectorElements(ValueI16, PackedI16);
}
if (EltTy.isFloat())
B.buildBitcast(Dst, ValueI16);
else
B.buildCopy(Dst, ValueI16);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This is pretty ugly, and we have similar code in all of these functions

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Simplified

@arsenm arsenm left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The globalisel legalizer stuff is still ugly, we need better APIs

@aobolensk
aobolensk merged commit 8497b7a into llvm:main Sep 13, 2026
12 checks passed
aobolensk added a commit to aobolensk/llvm-project that referenced this pull request Sep 15, 2026
…11465)

gfx90a has no hardware encoding for TFE D16 format buffer loads, so
reject the combination there with a diagnostic. Other targets
(gfx8/10/11/12) do have real encodings, so add codegen support for them
instead of rejecting unconditionally.
anilkund pushed a commit to anilkund/llvm-project that referenced this pull request Sep 16, 2026
…11465)

gfx90a has no hardware encoding for TFE D16 format buffer loads, so
reject the combination there with a diagnostic. Other targets
(gfx8/10/11/12) do have real encodings, so add codegen support for them
instead of rejecting unconditionally.
@llvm-ci

llvm-ci commented Sep 21, 2026

Copy link
Copy Markdown

LLVM Buildbot has detected a new failure on builder ppc64le-mlir-rhel-clang running on ppc64le-mlir-rhel-test while building llvm at step 6 "test-build-check-mlir-build-only-check-mlir".

Full details are available at: https://lab.llvm.org/buildbot/#/builders/129/builds/52603

Here is the relevant piece of the build log for the reference
Step 6 (test-build-check-mlir-build-only-check-mlir) failure: 1200 seconds without output running [b'ninja', b'check-mlir'], attempting to kill
...
PASS: MLIR :: Transforms/test-canonicalize-filter.mlir (4602 of 4613)
PASS: MLIR :: Transforms/test-canonicalize-merge-large-blocks.mlir (4603 of 4613)
PASS: MLIR :: mlir-tblgen/interfaces.mlir (4604 of 4613)
PASS: MLIR :: mlir-tblgen/llvm-intrinsics.td (4605 of 4613)
PASS: MLIR :: mlir-runner/sgemm-naive-codegen.mlir (4606 of 4613)
PASS: MLIR :: mlir-runner/simple.mlir (4607 of 4613)
PASS: MLIR :: Pass/pipeline-options-parsing.mlir (4608 of 4613)
PASS: MLIR :: Pass/pipeline-parsing.mlir (4609 of 4613)
PASS: MLIR :: mlir-reduce/opt-reduction/cse-test.mlir (4610 of 4613)
PASS: MLIR :: mlir-reduce/opt-reduction/dce-test.mlir (4611 of 4613)
command timed out: 1200 seconds without output running [b'ninja', b'check-mlir'], attempting to kill
process killed by signal 9
program finished with exit code -1
elapsedTime=2465.174013

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants