From ccb720aa26d28b038fe3303f3b1d2c92024b56ce Mon Sep 17 00:00:00 2001 From: Alex Voicu Date: Thu, 23 Apr 2026 16:15:32 +0100 Subject: [PATCH 1/3] HIPSTDPAR fixes. --- lib/SPIRV/SPIRVReader.cpp | 31 ++++++++++++++++++++++++++++--- 1 file changed, 28 insertions(+), 3 deletions(-) diff --git a/lib/SPIRV/SPIRVReader.cpp b/lib/SPIRV/SPIRVReader.cpp index 10328cfb4..fed3de073 100644 --- a/lib/SPIRV/SPIRVReader.cpp +++ b/lib/SPIRV/SPIRVReader.cpp @@ -260,6 +260,10 @@ SPIRVErrorLog &SPIRVToLLVM::getErrorLog() { return BM->getErrorLog(); } void SPIRVToLLVM::setCallingConv(CallInst *Call) { Function *F = Call->getCalledFunction(); assert(F && "Function pointers are not allowed in SPIRV"); + + if (M->getTargetTriple().isAMDGCN() && + F->getCallingConv() == CallingConv::AMDGPU_KERNEL) + return Call->setCallingConv(CallingConv::C); // This is a stub call. Call->setCallingConv(F->getCallingConv()); } @@ -2827,10 +2831,31 @@ Value *SPIRVToLLVM::transValueWithoutDecoration(SPIRVValue *BV, Function *F, case OpFunctionCall: { SPIRVFunctionCall *BC = static_cast(BV); std::vector Args = transValue(BC->getArgumentValues(), F, BB); - auto *Call = CallInst::Create(transFunction(BC->getFunction()), Args, - BC->getName(), BB); - setCallingConv(Call); + Function *Callee = transFunction(BC->getFunction()); + if (M->getTargetTriple().isAMDGCN() && isKernel(BC->getFunction())) { + // In HIPSTDPAR mode we sometimes get some host side calls that have not + // yet been pruned (this happens later on reverse translated AMDGPU LLVM + // IR); whilst these are essentially dead, we should generate valid IR + // nonetheless, and this might require inserting an AS cast. + // TODO: we should only do this for HIPSTDPAR modules; this is a temporary + // workaround. + std::transform( + Callee->arg_begin(), Callee->arg_end(), Args.begin(), Args.begin(), + [BB](auto &&Formal, auto &&Actual) { + if (!Formal.getType()->isPointerTy()) + return Actual; + + if (Formal.getType()->getPointerAddressSpace() == + Actual->getType()->getPointerAddressSpace()) + return Actual; + + return cast(CastInst::CreatePointerBitCastOrAddrSpaceCast( + Actual, Formal.getType(), "", BB)); + }); + } + auto *Call = CallInst::Create(Callee, Args, BC->getName(), BB); setAttrByCalledFunc(Call); + setCallingConv(Call); applyFPFastMathModeDecorations(BV, Call); return mapValue(BV, Call); } From b5730e3b353300aadf715b7fb79a702641ebcb78 Mon Sep 17 00:00:00 2001 From: Alex Voicu Date: Tue, 5 May 2026 23:34:25 +0100 Subject: [PATCH 2/3] Undo accidental removal. --- lib/SPIRV/SPIRVReader.cpp | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/lib/SPIRV/SPIRVReader.cpp b/lib/SPIRV/SPIRVReader.cpp index aadd91edb..6a3e1c3af 100644 --- a/lib/SPIRV/SPIRVReader.cpp +++ b/lib/SPIRV/SPIRVReader.cpp @@ -2853,8 +2853,9 @@ Value *SPIRVToLLVM::transValueWithoutDecoration(SPIRVValue *BV, Function *F, return cast(CastInst::CreatePointerBitCastOrAddrSpaceCast( Actual, Formal.getType(), "", BB)); }); - } else if (BC->getFunction()->getName() == "llvm.amdgcn.is.shared" || - BC->getFunction()->getName() == "llvm.amdgcn.is.private") { + } else if (Args.size() == 1 && + (BC->getFunction()->getName() == "llvm.amdgcn.is.shared" || + BC->getFunction()->getName() == "llvm.amdgcn.is.private")) { if (BC->getArgumentValues().front()->getType()->getPointerStorageClass() != StorageClassGeneric) { auto *PTy = PointerType::get( @@ -2866,8 +2867,8 @@ Value *SPIRVToLLVM::transValueWithoutDecoration(SPIRVValue *BV, Function *F, } } auto *Call = CallInst::Create(Callee, Args, BC->getName(), BB); - setAttrByCalledFunc(Call); setCallingConv(Call); + setAttrByCalledFunc(Call); applyFPFastMathModeDecorations(BV, Call); return mapValue(BV, Call); } From 372aa6eed9313d5c84a8a526b5a720aca5362ce8 Mon Sep 17 00:00:00 2001 From: Alex Voicu Date: Fri, 8 May 2026 13:34:31 +0100 Subject: [PATCH 3/3] Remove vestigial noise. --- lib/SPIRV/SPIRVWriter.cpp | 4 ---- 1 file changed, 4 deletions(-) diff --git a/lib/SPIRV/SPIRVWriter.cpp b/lib/SPIRV/SPIRVWriter.cpp index f7c7d786e..6006e99fc 100644 --- a/lib/SPIRV/SPIRVWriter.cpp +++ b/lib/SPIRV/SPIRVWriter.cpp @@ -856,10 +856,6 @@ SPIRVType *LLVMToSPIRVBase::transScavengedType(Value *V) { SPIRVEC_UnsupportedVarArgFunction); SPIRVType *RT = transType(FnTy->getReturnType()); - if (M->getTargetTriple().getVendor() == Triple::VendorType::AMD) - if (F->getReturnType()->isPtrOrPtrVectorTy()) - RT = transType(F->getReturnType()->getWithNewType( - PointerType::get(F->getContext(), SPIRAS_Constant))); std::vector PT; for (Argument &Arg : F->args()) {