[llvm] [llvm][OpenMP][SPIRV] Fix assertion for GPU reductions (PR #194879)

Alex Duran via llvm-commits llvm-commits at lists.llvm.org
Wed Apr 29 08:07:37 PDT 2026


https://github.com/adurang created https://github.com/llvm/llvm-project/pull/194879

Currenty compiling a `target reduction` results in the following assert for spirv64-intel target: 

> Assertion `New->getType() == getType() && "replaceUses of value with new value of different type!"' failed.

This patch fixes it by adding an addrespace cast where necessary to make the types of the expressions match.

Assisted-by: claude-sonnet-4-5

>From 4560134032728638746f3bdd8fc33332b8e5019b Mon Sep 17 00:00:00 2001
From: "Duran, Alex" <alejandro.duran at intel.com>
Date: Wed, 29 Apr 2026 07:59:29 -0700
Subject: [PATCH] [llvm][OpenMP][SPIRV] Fix assertion for GPU reductions

Currenty compiling a target reduction results in the following assert for spirv64-intel target:
Assertion `New->getType() == getType() && "replaceUses of value with new value of different type!"' failed.

This patch fixes it by adding an addrespace cast where necessary to make the types of the expressions match.

Assisted-by: claude-sonnet-4-5
---
 llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp | 16 +++++++++++++---
 1 file changed, 13 insertions(+), 3 deletions(-)

diff --git a/llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp b/llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp
index 5a4f12d91d540..6a1832a34cd9c 100644
--- a/llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp
+++ b/llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp
@@ -4731,12 +4731,22 @@ OpenMPIRBuilder::InsertPointOrErrorTy OpenMPIRBuilder::createReductionsGPU(
                                              &LHSPtr, &RHSPtr, CurFunc));
 
       // Fix the CallBack code genereated to use the correct Values for the LHS
-      // and RHS
-      LHSPtr->replaceUsesWithIf(RedValue, [ReductionFunc](const Use &U) {
+      // and RHS. Cast to match types before replacing (necessary to handle SPIRV address
+      // spaces).
+      Value *CastRedValue = RedValue;
+      if (LHSPtr->getType() != RedValue->getType())
+        CastRedValue = Builder.CreatePointerBitCastOrAddrSpaceCast(
+            RedValue, LHSPtr->getType());
+      Value *CastRHS = RHS;
+      if (RHSPtr->getType() != RHS->getType())
+        CastRHS =
+            Builder.CreatePointerBitCastOrAddrSpaceCast(RHS, RHSPtr->getType());
+
+      LHSPtr->replaceUsesWithIf(CastRedValue, [ReductionFunc](const Use &U) {
         return cast<Instruction>(U.getUser())->getParent()->getParent() ==
                ReductionFunc;
       });
-      RHSPtr->replaceUsesWithIf(RHS, [ReductionFunc](const Use &U) {
+      RHSPtr->replaceUsesWithIf(CastRHS, [ReductionFunc](const Use &U) {
         return cast<Instruction>(U.getUser())->getParent()->getParent() ==
                ReductionFunc;
       });



More information about the llvm-commits mailing list