[llvm] [AMDGPU] Fix miscompile trimming sparse DMask on image stores (PR #213586)

Arseniy Obolenskiy via llvm-commits llvm-commits at lists.llvm.org
Sun Aug 2 21:07:34 PDT 2026


https://github.com/aobolensk created https://github.com/llvm/llvm-project/pull/213586

Dropping a channel from a sparse DMask removes that write entirely

It does not zero-fill like a contiguous trim does

Only simplify stores when DMask is a contiguous prefix

>From 4ad2b051fc6c2094104727a0415e0e3a8a5e746b Mon Sep 17 00:00:00 2001
From: Arseniy Obolenskiy <arseniy.obolenskiy at amd.com>
Date: Mon, 3 Aug 2026 06:06:26 +0200
Subject: [PATCH] [AMDGPU] Fix miscompile trimming sparse DMask on image stores

Dropping a channel from a sparse DMask removes that write entirely

It does not zero-fill like a contiguous trim does

Only simplify stores when DMask is a contiguous prefix
---
 .../AMDGPU/AMDGPUInstCombineIntrinsic.cpp     |  3 +++
 .../amdgcn-simplify-image-buffer-stores.ll    | 23 +++++++++++++++++++
 2 files changed, 26 insertions(+)

diff --git a/llvm/lib/Target/AMDGPU/AMDGPUInstCombineIntrinsic.cpp b/llvm/lib/Target/AMDGPU/AMDGPUInstCombineIntrinsic.cpp
index cbda99ae7d33c..7e88334f70993 100644
--- a/llvm/lib/Target/AMDGPU/AMDGPUInstCombineIntrinsic.cpp
+++ b/llvm/lib/Target/AMDGPU/AMDGPUInstCombineIntrinsic.cpp
@@ -2395,6 +2395,9 @@ static Value *simplifyAMDGCNMemoryIntrinsicDemanded(InstCombiner &IC,
     if (DMaskVal == 0)
       return nullptr;
 
+    if (!IsLoad && !isMask_32(DMaskVal))
+      return nullptr;
+
     // Mask off values that are undefined because the dmask doesn't cover them
     DemandedElts &= (1 << llvm::popcount(DMaskVal)) - 1;
 
diff --git a/llvm/test/Transforms/InstCombine/AMDGPU/amdgcn-simplify-image-buffer-stores.ll b/llvm/test/Transforms/InstCombine/AMDGPU/amdgcn-simplify-image-buffer-stores.ll
index ce36325731b81..7304ee30fb505 100644
--- a/llvm/test/Transforms/InstCombine/AMDGPU/amdgcn-simplify-image-buffer-stores.ll
+++ b/llvm/test/Transforms/InstCombine/AMDGPU/amdgcn-simplify-image-buffer-stores.ll
@@ -72,6 +72,28 @@ define amdgpu_ps void @image_store_mip_1d_store_insert_zeros_at_end(<8 x i32> in
   ret void
 }
 
+define amdgpu_ps void @image_store_1d_store_sparse_dmask_zero_at_end(<8 x i32> inreg %rsrc, float %vdata1, i32 %s) #0 {
+; GCN-LABEL: @image_store_1d_store_sparse_dmask_zero_at_end(
+; GCN-NEXT:    [[NEWVDATA2:%.*]] = insertelement <2 x float> <float poison, float 0.000000e+00>, float [[VDATA1:%.*]], i64 0
+; GCN-NEXT:    call void @llvm.amdgcn.image.store.1d.v2f32.i32.v8i32(<2 x float> [[NEWVDATA2]], i32 10, i32 [[S:%.*]], <8 x i32> [[RSRC:%.*]], i32 0, i32 0)
+; GCN-NEXT:    ret void
+;
+; GFX12-LABEL: @image_store_1d_store_sparse_dmask_zero_at_end(
+; GFX12-NEXT:    [[NEWVDATA2:%.*]] = insertelement <2 x float> <float poison, float 0.000000e+00>, float [[VDATA1:%.*]], i64 0
+; GFX12-NEXT:    call void @llvm.amdgcn.image.store.1d.v2f32.i32.v8i32(<2 x float> [[NEWVDATA2]], i32 10, i32 [[S:%.*]], <8 x i32> [[RSRC:%.*]], i32 0, i32 0)
+; GFX12-NEXT:    ret void
+;
+; GFXUNKNOWN-LABEL: @image_store_1d_store_sparse_dmask_zero_at_end(
+; GFXUNKNOWN-NEXT:    [[NEWVDATA2:%.*]] = insertelement <2 x float> <float poison, float 0.000000e+00>, float [[VDATA1:%.*]], i64 0
+; GFXUNKNOWN-NEXT:    call void @llvm.amdgcn.image.store.1d.v2f32.i32.v8i32(<2 x float> [[NEWVDATA2]], i32 10, i32 [[S:%.*]], <8 x i32> [[RSRC:%.*]], i32 0, i32 0)
+; GFXUNKNOWN-NEXT:    ret void
+;
+  %newvdata1 = insertelement <2 x float> undef, float %vdata1, i32 0
+  %newvdata2 = insertelement <2 x float> %newvdata1, float 0.0, i32 1
+  call void @llvm.amdgcn.image.store.1d.v2f32.i32.v8i32(<2 x float> %newvdata2, i32 10, i32 %s, <8 x i32> %rsrc, i32 0, i32 0)
+  ret void
+}
+
 define amdgpu_ps void @struct_buffer_store_format_insert_zeros(<4 x i32> inreg %a, float %vdata1, i32 %b) {
 ; GCN-LABEL: @struct_buffer_store_format_insert_zeros(
 ; GCN-NEXT:    [[TMP1:%.*]] = insertelement <3 x float> <float poison, float 0.000000e+00, float poison>, float [[VDATA1:%.*]], i64 0
@@ -280,6 +302,7 @@ declare void @llvm.amdgcn.raw.buffer.store.format.v4f32(<4 x float>, <4 x i32>,
 declare void @llvm.amdgcn.struct.buffer.store.format.v4f32(<4 x float>, <4 x i32>, i32, i32, i32, i32) #2
 declare void @llvm.amdgcn.struct.tbuffer.store.v4f32(<4 x float>, <4 x i32>, i32, i32, i32, i32, i32) #0
 declare void @llvm.amdgcn.raw.tbuffer.store.v4f32(<4 x float>, <4 x i32>, i32, i32, i32, i32) #0
+declare void @llvm.amdgcn.image.store.1d.v2f32.i32.v8i32(<2 x float>, i32, i32, <8 x i32>, i32, i32) #0
 declare void @llvm.amdgcn.image.store.1d.v4f32.i32.v8i32(<4 x float>, i32, i32, <8 x i32>, i32, i32) #0
 declare void @llvm.amdgcn.image.store.2d.v4f32.i32.v8i32(<4 x float>, i32, i32, i32, <8 x i32>, i32, i32) #0
 declare void @llvm.amdgcn.image.store.3d.v4f32.i32.v8i32(<4 x float>, i32, i32, i32, i32, <8 x i32>, i32, i32) #0



More information about the llvm-commits mailing list