[clang] [llvm] [AMDGPU] Fold `llvm.amdgcn.update.dpp` with a zero row or bank mask (PR #248230)
Alan Li via cfe-commits
cfe-commits at lists.llvm.org
Wed Dec 2 17:54:02 PST 2026
https://github.com/lialan updated https://github.com/llvm/llvm-project/pull/248230
>From cbdd71ff22b15664f8c9734ef0547b3c39259a7a Mon Sep 17 00:00:00 2001
From: Alan Li <me at alanli.org>
Date: Wed, 2 Dec 2026 13:18:15 -0800
Subject: [PATCH 1/3] [AMDGPU] Fold `llvm.amdgcn.update.dpp` with a zero row or
bank mask
An `llvm.amdgcn.update.dpp` with `row_mask == 0` or `bank_mask == 0`
writes no lanes, so its result is `old`. Fold it to `old` in
InstCombine.
- DPP writes a lane only if both its row and its bank are enabled;
every other lane keeps `old`, which is tied to the destination.
- With either mask zero, no lane is enabled, so the result is `old`
for any `src`, `dpp_ctrl`, `bound_ctrl` or `EXEC`.
---
.../AMDGPU/AMDGPUInstCombineIntrinsic.cpp | 4 ++
.../InstCombine/AMDGPU/amdgcn-intrinsics.ll | 40 +++++++++++++++++++
2 files changed, 44 insertions(+)
diff --git a/llvm/lib/Target/AMDGPU/AMDGPUInstCombineIntrinsic.cpp b/llvm/lib/Target/AMDGPU/AMDGPUInstCombineIntrinsic.cpp
index 243cc831c73a6..c3a86dc472bc7 100644
--- a/llvm/lib/Target/AMDGPU/AMDGPUInstCombineIntrinsic.cpp
+++ b/llvm/lib/Target/AMDGPU/AMDGPUInstCombineIntrinsic.cpp
@@ -1904,6 +1904,10 @@ GCNTTIImpl::instCombineIntrinsic(InstCombiner &IC, IntrinsicInst &II) const {
auto *BC = cast<ConstantInt>(II.getArgOperand(5));
auto *RM = cast<ConstantInt>(II.getArgOperand(3));
auto *BM = cast<ConstantInt>(II.getArgOperand(4));
+ // No lanes are updated if all rows or all banks are disabled.
+ if (RM->isZero() || BM->isZero())
+ return IC.replaceInstUsesWith(II, Old);
+
if (BC->isNullValue() || RM->getZExtValue() != 0xF ||
BM->getZExtValue() != 0xF || isa<PoisonValue>(Old))
break;
diff --git a/llvm/test/Transforms/InstCombine/AMDGPU/amdgcn-intrinsics.ll b/llvm/test/Transforms/InstCombine/AMDGPU/amdgcn-intrinsics.ll
index 640c1de6c6ad3..71b2077aa60ae 100644
--- a/llvm/test/Transforms/InstCombine/AMDGPU/amdgcn-intrinsics.ll
+++ b/llvm/test/Transforms/InstCombine/AMDGPU/amdgcn-intrinsics.ll
@@ -2272,6 +2272,46 @@ define amdgpu_kernel void @update_dpp_undef_old(ptr addrspace(1) %out, i32 %in1)
ret void
}
+define i32 @update_dpp_row_mask_zero(i32 %old, i32 %src) {
+; CHECK-LABEL: @update_dpp_row_mask_zero(
+; CHECK-NEXT: ret i32 [[OLD:%.*]]
+;
+ %r = call i32 @llvm.amdgcn.update.dpp.i32(i32 %old, i32 %src, i32 1, i32 0, i32 15, i1 false)
+ ret i32 %r
+}
+
+define i32 @update_dpp_bank_mask_zero(i32 %old, i32 %src) {
+; CHECK-LABEL: @update_dpp_bank_mask_zero(
+; CHECK-NEXT: ret i32 [[OLD:%.*]]
+;
+ %r = call i32 @llvm.amdgcn.update.dpp.i32(i32 %old, i32 %src, i32 1, i32 15, i32 0, i1 true)
+ ret i32 %r
+}
+
+define i64 @update_dpp_row_mask_zero_i64(i64 %old, i64 %src) {
+; CHECK-LABEL: @update_dpp_row_mask_zero_i64(
+; CHECK-NEXT: ret i64 [[OLD:%.*]]
+;
+ %r = call i64 @llvm.amdgcn.update.dpp.i64(i64 %old, i64 %src, i32 1, i32 0, i32 15, i1 false)
+ ret i64 %r
+}
+
+define i16 @update_dpp_bank_mask_zero_i16(i16 %old, i16 %src) {
+; CHECK-LABEL: @update_dpp_bank_mask_zero_i16(
+; CHECK-NEXT: ret i16 [[OLD:%.*]]
+;
+ %r = call i16 @llvm.amdgcn.update.dpp.i16(i16 %old, i16 %src, i32 1, i32 15, i32 0, i1 false)
+ ret i16 %r
+}
+
+define i64 @update_dpp_masks_zero_i64(i64 %old, i64 %src) {
+; CHECK-LABEL: @update_dpp_masks_zero_i64(
+; CHECK-NEXT: ret i64 [[OLD:%.*]]
+;
+ %r = call i64 @llvm.amdgcn.update.dpp.i64(i64 %old, i64 %src, i32 1, i32 0, i32 0, i1 false)
+ ret i64 %r
+}
+
; --------------------------------------------------------------------
; llvm.amdgcn.permlane16
>From 2575a3afd1ae7634483876a9461295289f610af0 Mon Sep 17 00:00:00 2001
From: Alan Li <me at alanli.org>
Date: Wed, 2 Dec 2026 14:27:14 -0800
Subject: [PATCH 2/3] [AMDGPU] Use nonzero DPP masks in `builtins-amdgcn-vi.cl`
OpenCL compiles at `-O2`, so InstCombine now folds the zero-mask
`update.dpp` calls these tests check.
---
.../test/CodeGenOpenCL/builtins-amdgcn-vi.cl | 20 +++++++++----------
1 file changed, 10 insertions(+), 10 deletions(-)
diff --git a/clang/test/CodeGenOpenCL/builtins-amdgcn-vi.cl b/clang/test/CodeGenOpenCL/builtins-amdgcn-vi.cl
index 44f7c8a0a0651..513b00aff256d 100644
--- a/clang/test/CodeGenOpenCL/builtins-amdgcn-vi.cl
+++ b/clang/test/CodeGenOpenCL/builtins-amdgcn-vi.cl
@@ -118,11 +118,11 @@ void test_s_dcache_wb()
}
// CHECK-LABEL: @test_mov_dpp_int
-// CHECK: {{.*}}call{{.*}} i32 @llvm.amdgcn.update.dpp.i32(i32 poison, i32 %src, i32 0, i32 0, i32 0, i1 false)
+// CHECK: {{.*}}call{{.*}} i32 @llvm.amdgcn.update.dpp.i32(i32 poison, i32 %src, i32 0, i32 15, i32 15, i1 false)
void test_mov_dpp_int(global int* out, int src)
{
if (__builtin_amdgcn_is_invocable(__builtin_amdgcn_mov_dpp))
- *out = __builtin_amdgcn_mov_dpp(src, 0, 0, 0, false);
+ *out = __builtin_amdgcn_mov_dpp(src, 0, 0xf, 0xf, false);
}
// CHECK-LABEL: @test_mov_dpp_long
@@ -183,11 +183,11 @@ void test_mov_dpp_half(half *x, global half *p) {
}
// CHECK-LABEL: @test_update_dpp_int
-// CHECK: {{.*}}call{{.*}} i32 @llvm.amdgcn.update.dpp.i32(i32 %arg1, i32 %arg2, i32 0, i32 0, i32 0, i1 false)
+// CHECK: {{.*}}call{{.*}} i32 @llvm.amdgcn.update.dpp.i32(i32 %arg1, i32 %arg2, i32 0, i32 15, i32 15, i1 false)
void test_update_dpp_int(global int* out, int arg1, int arg2)
{
if (__builtin_amdgcn_is_invocable(__builtin_amdgcn_update_dpp))
- *out = __builtin_amdgcn_update_dpp(arg1, arg2, 0, 0, 0, false);
+ *out = __builtin_amdgcn_update_dpp(arg1, arg2, 0, 0xf, 0xf, false);
}
// CHECK-LABEL: @test_update_dpp_long
@@ -248,29 +248,29 @@ void test_update_dpp_half(half *x, global half *p) {
}
// CHECK-LABEL: @test_update_dpp_int_uint
-// CHECK: {{.*}}call{{.*}} i32 @llvm.amdgcn.update.dpp.i32(i32 %arg1, i32 %arg2, i32 0, i32 0, i32 0, i1 false)
+// CHECK: {{.*}}call{{.*}} i32 @llvm.amdgcn.update.dpp.i32(i32 %arg1, i32 %arg2, i32 0, i32 15, i32 15, i1 false)
void test_update_dpp_int_uint(global int* out, int arg1, unsigned int arg2)
{
if (__builtin_amdgcn_is_invocable(__builtin_amdgcn_update_dpp))
- *out = __builtin_amdgcn_update_dpp(arg1, arg2, 0, 0, 0, false);
+ *out = __builtin_amdgcn_update_dpp(arg1, arg2, 0, 0xf, 0xf, false);
}
// CHECK-LABEL: @test_update_dpp_lit_int
-// CHECK: {{.*}}call{{.*}} i32 @llvm.amdgcn.update.dpp.i32(i32 5, i32 %arg1, i32 0, i32 0, i32 0, i1 false)
+// CHECK: {{.*}}call{{.*}} i32 @llvm.amdgcn.update.dpp.i32(i32 5, i32 %arg1, i32 0, i32 15, i32 15, i1 false)
void test_update_dpp_lit_int(global int* out, int arg1)
{
if (__builtin_amdgcn_is_invocable(__builtin_amdgcn_update_dpp))
- *out = __builtin_amdgcn_update_dpp(5, arg1, 0, 0, 0, false);
+ *out = __builtin_amdgcn_update_dpp(5, arg1, 0, 0xf, 0xf, false);
}
__constant int gi = 5;
// CHECK-LABEL: @test_update_dpp_const_int
-// CHECK: {{.*}}call{{.*}} i32 @llvm.amdgcn.update.dpp.i32(i32 5, i32 %arg1, i32 0, i32 0, i32 0, i1 false)
+// CHECK: {{.*}}call{{.*}} i32 @llvm.amdgcn.update.dpp.i32(i32 5, i32 %arg1, i32 0, i32 15, i32 15, i1 false)
void test_update_dpp_const_int(global int* out, int arg1)
{
if (__builtin_amdgcn_is_invocable(__builtin_amdgcn_update_dpp))
- *out = __builtin_amdgcn_update_dpp(gi, arg1, 0, 0, 0, false);
+ *out = __builtin_amdgcn_update_dpp(gi, arg1, 0, 0xf, 0xf, false);
}
// CHECK-LABEL: @test_ds_fadd
>From fb25eca5edf427583c3968247cbc81398c622549 Mon Sep 17 00:00:00 2001
From: Alan Li <me at alanli.org>
Date: Wed, 2 Dec 2026 17:49:05 -0800
Subject: [PATCH 3/3] [AMDGPU] Check only the low 4 bits of the `update.dpp`
masks
The encoding keeps the low 4 bits of `row_mask` and `bank_mask`.
---
.../AMDGPU/AMDGPUInstCombineIntrinsic.cpp | 10 +++++-----
.../InstCombine/AMDGPU/amdgcn-intrinsics.ll | 17 +++++++++++++++++
2 files changed, 22 insertions(+), 5 deletions(-)
diff --git a/llvm/lib/Target/AMDGPU/AMDGPUInstCombineIntrinsic.cpp b/llvm/lib/Target/AMDGPU/AMDGPUInstCombineIntrinsic.cpp
index c3a86dc472bc7..e039cbb0c1374 100644
--- a/llvm/lib/Target/AMDGPU/AMDGPUInstCombineIntrinsic.cpp
+++ b/llvm/lib/Target/AMDGPU/AMDGPUInstCombineIntrinsic.cpp
@@ -1902,14 +1902,14 @@ GCNTTIImpl::instCombineIntrinsic(InstCombiner &IC, IntrinsicInst &II) const {
Value *Old = II.getArgOperand(0);
auto *BC = cast<ConstantInt>(II.getArgOperand(5));
- auto *RM = cast<ConstantInt>(II.getArgOperand(3));
- auto *BM = cast<ConstantInt>(II.getArgOperand(4));
+ APInt RM = cast<ConstantInt>(II.getArgOperand(3))->getValue().trunc(4);
+ APInt BM = cast<ConstantInt>(II.getArgOperand(4))->getValue().trunc(4);
// No lanes are updated if all rows or all banks are disabled.
- if (RM->isZero() || BM->isZero())
+ if (RM.isZero() || BM.isZero())
return IC.replaceInstUsesWith(II, Old);
- if (BC->isNullValue() || RM->getZExtValue() != 0xF ||
- BM->getZExtValue() != 0xF || isa<PoisonValue>(Old))
+ if (BC->isNullValue() || !RM.isAllOnes() || !BM.isAllOnes() ||
+ isa<PoisonValue>(Old))
break;
// If bound_ctrl = 1, row mask = bank mask = 0xf we can omit old value.
diff --git a/llvm/test/Transforms/InstCombine/AMDGPU/amdgcn-intrinsics.ll b/llvm/test/Transforms/InstCombine/AMDGPU/amdgcn-intrinsics.ll
index 71b2077aa60ae..4a938eef2e44d 100644
--- a/llvm/test/Transforms/InstCombine/AMDGPU/amdgcn-intrinsics.ll
+++ b/llvm/test/Transforms/InstCombine/AMDGPU/amdgcn-intrinsics.ll
@@ -2312,6 +2312,23 @@ define i64 @update_dpp_masks_zero_i64(i64 %old, i64 %src) {
ret i64 %r
}
+define i32 @update_dpp_row_mask_zero_low_bits(i32 %old, i32 %src) {
+; CHECK-LABEL: @update_dpp_row_mask_zero_low_bits(
+; CHECK-NEXT: ret i32 [[OLD:%.*]]
+;
+ %r = call i32 @llvm.amdgcn.update.dpp.i32(i32 %old, i32 %src, i32 1, i32 16, i32 15, i1 false)
+ ret i32 %r
+}
+
+define i32 @update_dpp_drop_old_full_low_bits(i32 %old, i32 %src) {
+; CHECK-LABEL: @update_dpp_drop_old_full_low_bits(
+; CHECK-NEXT: [[R:%.*]] = call i32 @llvm.amdgcn.update.dpp.i32(i32 poison, i32 [[SRC:%.*]], i32 1, i32 31, i32 31, i1 true)
+; CHECK-NEXT: ret i32 [[R]]
+;
+ %r = call i32 @llvm.amdgcn.update.dpp.i32(i32 %old, i32 %src, i32 1, i32 31, i32 31, i1 true)
+ ret i32 %r
+}
+
; --------------------------------------------------------------------
; llvm.amdgcn.permlane16
More information about the cfe-commits
mailing list