[llvm] [OpenMP] Fix debug locations in GPU reductions (PR #228622)
Amadeu Moya via llvm-commits
llvm-commits at lists.llvm.org
Fri Oct 2 17:02:46 PDT 2026
https://github.com/amad00r created https://github.com/llvm/llvm-project/pull/228622
GPU reduction codegen temporarily switches the IRBuilder to AllocaIP to create reduction storage. In Clang, AllocaIP points before the unlocated allocapt marker, which clears the current debug location. Restoring the insertion point does not restore the debug location, so the following inlinable runtime calls lack !dbg.
Use InsertPointGuard for temporary alloca insertion regions, preserving both the caller insertion point and debug location.
Remove redundant saveIP/restoreIP pairs around helper emitters that already use InsertPointGuard internally to preserve the builder state.
Add an OpenMPIRBuilder unit test that models Clang's allocapt insertion point and verifies the expected runtime call has a valid debug location.
Assisted by gpt-5.6.
reproducer:
```c
// clang -fopenmp --offload-arch=native -g %s
void foo(void) {
int sum = 0;
#pragma omp target teams distribute parallel for reduction(+:sum)
for (int i = 0; i < 2; i++)
sum++;
}
```
backtrace:
```
inlinable function call in a function with debug info must have a !dbg location
%48 = call i32 @__kmpc_gpu_xteam_reduce_nowait(ptr addrspacecast (ptr addrspace(1) @anon.1ed65f77c194573ad05de30b8156463e.6 to ptr), ptr %18, ptr @_omp_reduction_shuffle_and_reduce_func.1, ptr @_omp_reduction_inter_warp_copy_func.2, ptr @_omp_reduction_list_to_global_copy_func, ptr @_omp_reduction_global_to_list_copy_func, ptr @_omp_reduction_global_to_list_reduce_func)
inlinable function call in a function with debug info must have a !dbg location
%64 = call i32 @__kmpc_nvptx_parallel_reduce_nowait_v2(ptr addrspacecast (ptr addrspace(1) @anon.1ed65f77c194573ad05de30b8156463e.6 to ptr), i64 4, ptr %21, ptr @_omp_reduction_shuffle_and_reduce_func, ptr @_omp_reduction_inter_warp_copy_func)
LLVM ERROR: Broken module found, compilation aborted!
PLEASE submit a bug report to https://github.com/llvm/llvm-project/issues/ and include the crash backtrace and instructions to reproduce the bug.
Stack dump:
0. Program arguments: /usr/WS1/moyasarda1/gpu-kernel-versioning/install-tioga/bin/ld.lld --no-undefined -shared -plugin-opt=mcpu=gfx90a -L/usr/WS1/moyasarda1/gpu-kernel-versioning/install-tioga/bin/../lib/amdgpu-amd-amdhsa -L/usr/WS1/moyasarda1/gpu-kernel-versioning/install-tioga/lib/clang/24/lib/amdgpu-amd-amdhsa --no-undefined /var/tmp/moyasarda1/reproducer-6d0c90-amdgpu9.0a-amd-amdhsa-gfx90a-cdbedf.o -lompdevice -lc -lm -lclang_rt.builtins -o /var/tmp/moyasarda1/a.out.amdgpu9.0a.gfx90a-ef38a4.img
1. Running pass "verify" on module "ld-temp.o"
#0 0x000000000051b0bb llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) /usr/workspace/moyasarda1/gpu-kernel-versioning/llvm-project/llvm/lib/Support/Unix/Signals.inc:931:13
#1 0x0000000000518834 llvm::sys::RunSignalHandlers() /usr/workspace/moyasarda1/gpu-kernel-versioning/llvm-project/llvm/lib/Support/Signals.cpp:109:18
#2 0x000000000051bdd0 SignalHandler(int, siginfo_t*, void*) /usr/workspace/moyasarda1/gpu-kernel-versioning/llvm-project/llvm/lib/Support/Unix/Signals.inc:474:38
#3 0x0000155555116990 __restore_rt (/lib64/libpthread.so.0+0x12990)
#4 0x000015555381c5af raise (/lib64/libc.so.6+0x4e5af)
#5 0x00001555537efee5 abort (/lib64/libc.so.6+0x21ee5)
#6 0x00000000004bf78f llvm::report_fatal_error(llvm::Twine const&, bool) /usr/workspace/moyasarda1/gpu-kernel-versioning/llvm-project/llvm/lib/Support/ErrorHandling.cpp:137:5
#7 0x00000000004bf5b6 (/usr/WS1/moyasarda1/gpu-kernel-versioning/install-tioga/bin/ld.lld+0x4bf5b6)
#8 0x0000000004a63141 (/usr/WS1/moyasarda1/gpu-kernel-versioning/install-tioga/bin/ld.lld+0x4a63141)
#9 0x000000000272c08d llvm::detail::PassModel<llvm::Module, llvm::VerifierPass, llvm::AnalysisManager<llvm::Module>>::runImpl(llvm::detail::PassConcept<llvm::Module, llvm::AnalysisManager<llvm::Module>>&, llvm::Module&, llvm::AnalysisManager<llvm::Module>&) /usr/workspace/moyasarda1/gpu-kernel-versioning/llvm-project/llvm/include/llvm/IR/PassManagerInternal.h:128:5
#10 0x0000000004a2bb1b llvm::PassManager<llvm::Module, llvm::AnalysisManager<llvm::Module>>::run(llvm::Module&, llvm::AnalysisManager<llvm::Module>&) /usr/workspace/moyasarda1/gpu-kernel-versioning/llvm-project/llvm/include/llvm/IR/PassManagerImpl.h:80:8
#11 0x0000000002726990 llvm::SmallPtrSetImplBase::~SmallPtrSetImplBase() /usr/workspace/moyasarda1/gpu-kernel-versioning/llvm-project/llvm/include/llvm/ADT/SmallPtrSet.h:91:9
#12 0x0000000002726990 llvm::PreservedAnalyses::~PreservedAnalyses() /usr/workspace/moyasarda1/gpu-kernel-versioning/llvm-project/llvm/include/llvm/IR/Analysis.h:112:7
#13 0x0000000002726990 runNewPMPasses(llvm::lto::Config const&, llvm::Module&, llvm::TargetMachine*, unsigned int, bool, llvm::ModuleSummaryIndex*, llvm::ModuleSummaryIndex const*, llvm::DenseSet<llvm::StringRef, llvm::DenseMapInfo<llvm::StringRef, void>> const&) /usr/workspace/moyasarda1/gpu-kernel-versioning/llvm-project/llvm/lib/LTO/LTOBackend.cpp:388:3
#14 0x0000000002726990 llvm::lto::opt(llvm::lto::Config const&, llvm::TargetMachine*, unsigned int, llvm::Module&, bool, llvm::ModuleSummaryIndex*, llvm::ModuleSummaryIndex const*, std::vector<unsigned char, std::allocator<unsigned char>> const&, llvm::ArrayRef<llvm::StringRef>) /usr/workspace/moyasarda1/gpu-kernel-versioning/llvm-project/llvm/lib/LTO/LTOBackend.cpp:431:5
#15 0x000000000272705d llvm::lto::backend(llvm::lto::Config const&, std::function<llvm::Expected<std::unique_ptr<llvm::CachedFileStream, std::default_delete<llvm::CachedFileStream>>> (unsigned int, llvm::Twine const&)>, unsigned int, llvm::Module&, llvm::ModuleSummaryIndex&, llvm::ArrayRef<llvm::StringRef>) /usr/workspace/moyasarda1/gpu-kernel-versioning/llvm-project/llvm/lib/LTO/LTOBackend.cpp:608:10
#16 0x000000000270de64 llvm::lto::LTO::runRegularLTO(std::function<llvm::Expected<std::unique_ptr<llvm::CachedFileStream, std::default_delete<llvm::CachedFileStream>>> (unsigned int, llvm::Twine const&)>) /usr/workspace/moyasarda1/gpu-kernel-versioning/llvm-project/llvm/lib/LTO/LTO.cpp:1502:21
#17 0x000000000270d1ec std::_Function_base::~_Function_base() /opt/rh/gcc-toolset-15/root/usr/lib/gcc/x86_64-redhat-linux/15/../../../../include/c++/15/bits/std_function.h:245:11
#18 0x000000000270d1ec llvm::lto::LTO::run(std::function<llvm::Expected<std::unique_ptr<llvm::CachedFileStream, std::default_delete<llvm::CachedFileStream>>> (unsigned int, llvm::Twine const&)>, llvm::FileCache) /usr/workspace/moyasarda1/gpu-kernel-versioning/llvm-project/llvm/lib/LTO/LTO.cpp:1373:18
#19 0x00000000006e4ef6 lld::elf::BitcodeCompiler::compile() /usr/workspace/moyasarda1/gpu-kernel-versioning/llvm-project/lld/ELF/LTO.cpp:348:5
#20 0x000000000066b471 void lld::elf::LinkerDriver::compileBitcodeFiles<llvm::object::ELFType<(llvm::endianness)1, true>>(bool) /usr/workspace/moyasarda1/gpu-kernel-versioning/llvm-project/lld/ELF/Driver.cpp:0:25
#21 0x000000000064ddc5 void lld::elf::LinkerDriver::link<llvm::object::ELFType<(llvm::endianness)1, true>>(llvm::opt::InputArgList&) /usr/workspace/moyasarda1/gpu-kernel-versioning/llvm-project/lld/ELF/Driver.cpp:3406:18
#22 0x0000000000635e92 lld::elf::LinkerDriver::linkerMain(llvm::ArrayRef<char const*>) /usr/workspace/moyasarda1/gpu-kernel-versioning/llvm-project/lld/ELF/Driver.cpp:0:5
#23 0x0000000000632420 lld::elf::link(llvm::ArrayRef<char const*>, llvm::raw_ostream&, llvm::raw_ostream&, bool, bool) /usr/workspace/moyasarda1/gpu-kernel-versioning/llvm-project/lld/ELF/Driver.cpp:139:24
#24 0x000000000055a6f7 lld::unsafeLldMain(llvm::ArrayRef<char const*>, llvm::raw_ostream&, llvm::raw_ostream&, llvm::ArrayRef<lld::DriverDef>, bool) /usr/workspace/moyasarda1/gpu-kernel-versioning/llvm-project/lld/Common/DriverDispatcher.cpp:163:11
#25 0x000000000049ea03 lld_main(int, char**, llvm::ToolContext const&) /usr/workspace/moyasarda1/gpu-kernel-versioning/llvm-project/lld/tools/lld/lld.cpp:115:1
#26 0x000000000049ee4a main /usr/workspace/moyasarda1/gpu-kernel-versioning/build-tioga/tools/lld/tools/lld/lld-driver.cpp:17:10
#27 0x0000155553808865 __libc_start_main (/lib64/libc.so.6+0x3a865)
#28 0x000000000049e62e _start (/usr/WS1/moyasarda1/gpu-kernel-versioning/install-tioga/bin/ld.lld+0x49e62e)
```
>From c77fca3e06f1b46578123e0d486a9ac095132288 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Amadeu=20Moya=20Sard=C3=A0?= <moyasarda1 at llnl.gov>
Date: Fri, 2 Oct 2026 16:33:25 -0700
Subject: [PATCH] [OpenMP] Fix debug locations in GPU reductions
GPU reduction codegen temporarily switches the IRBuilder to AllocaIP to
create reduction storage. In Clang, AllocaIP points before the
unlocated allocapt marker, which clears the current debug location.
Restoring the insertion point does not restore the debug location, so
the following inlinable runtime calls lack !dbg.
Use InsertPointGuard for temporary alloca insertion regions, preserving
both the caller insertion point and debug location.
Remove redundant saveIP/restoreIP pairs around helper emitters that
already use InsertPointGuard internally to preserve the builder state.
Add an OpenMPIRBuilder unit test that models Clang's allocapt insertion
point and verifies the expected runtime call has a valid debug
location.
---
llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp | 60 +++++++++----------
.../Frontend/OpenMPIRBuilderTest.cpp | 60 +++++++++++++++++++
2 files changed, 89 insertions(+), 31 deletions(-)
diff --git a/llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp b/llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp
index 2c60bf3d67a9c..0a70b6337f1f4 100644
--- a/llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp
+++ b/llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp
@@ -4809,6 +4809,7 @@ OpenMPIRBuilder::InsertPointOrErrorTy OpenMPIRBuilder::createReductionsGPU(
if (!updateToLocation(Loc))
return InsertPointTy();
Builder.restoreIP(CodeGenIP);
+ Builder.SetCurrentDebugLocation(Loc.DL);
checkReductionInfos(ReductionInfos, /*IsGPU*/ true);
LLVMContext &Ctx = M.getContext();
@@ -4839,14 +4840,12 @@ OpenMPIRBuilder::InsertPointOrErrorTy OpenMPIRBuilder::createReductionsGPU(
AttrBldr.removeAttribute(Attribute::OptimizeNone);
FuncAttrs = FuncAttrs.addFnAttributes(Ctx, AttrBldr);
- CodeGenIP = Builder.saveIP();
Expected<Function *> ReductionResult = createReductionFunction(
Builder.GetInsertBlock()->getParent()->getName(), ReductionInfos, IsByRef,
ReductionGenCBKind, FuncAttrs);
if (!ReductionResult)
return ReductionResult.takeError();
Function *ReductionFunc = *ReductionResult;
- Builder.restoreIP(CodeGenIP);
// Set the grid value in the config needed for lowering later on
if (GridValue.has_value())
@@ -4867,13 +4866,14 @@ OpenMPIRBuilder::InsertPointOrErrorTy OpenMPIRBuilder::createReductionsGPU(
Type *FuncPtrTy =
Builder.getPtrTy(M.getDataLayout().getProgramAddressSpace());
Type *RedArrayTy = ArrayType::get(PtrTy, Size);
- CodeGenIP = Builder.saveIP();
- Builder.restoreIP(AllocaIP);
- Value *ReductionListAlloca =
- Builder.CreateAlloca(RedArrayTy, nullptr, ".omp.reduction.red_list");
- Value *ReductionList = Builder.CreatePointerBitCastOrAddrSpaceCast(
- ReductionListAlloca, PtrTy, ReductionListAlloca->getName() + ".ascast");
- Builder.restoreIP(CodeGenIP);
+ Value *ReductionList = [&] {
+ IRBuilder<>::InsertPointGuard IPG(Builder);
+ Builder.restoreIP(AllocaIP);
+ Value *ReductionListAlloca =
+ Builder.CreateAlloca(RedArrayTy, nullptr, ".omp.reduction.red_list");
+ return Builder.CreatePointerBitCastOrAddrSpaceCast(
+ ReductionListAlloca, PtrTy, ReductionListAlloca->getName() + ".ascast");
+ }();
Type *IndexTy = Builder.getIndexTy(
M.getDataLayout(), M.getDataLayout().getDefaultGlobalsAddressSpace());
for (auto En : enumerate(ReductionInfos)) {
@@ -4891,7 +4891,6 @@ OpenMPIRBuilder::InsertPointOrErrorTy OpenMPIRBuilder::createReductionsGPU(
Builder.CreatePointerBitCastOrAddrSpaceCast(PrivateVar, PtrTy);
Builder.CreateStore(CastElem, ElemPtr);
}
- CodeGenIP = Builder.saveIP();
Expected<Function *> SarFunc = emitShuffleAndReduceFunction(
ReductionInfos, ReductionFunc, FuncAttrs, IsByRef);
@@ -4903,7 +4902,6 @@ OpenMPIRBuilder::InsertPointOrErrorTy OpenMPIRBuilder::createReductionsGPU(
if (!CopyResult)
return CopyResult.takeError();
Function *WcFunc = *CopyResult;
- Builder.restoreIP(CodeGenIP);
Value *RL = Builder.CreatePointerBitCastOrAddrSpaceCast(ReductionList, PtrTy);
@@ -4960,7 +4958,6 @@ OpenMPIRBuilder::InsertPointOrErrorTy OpenMPIRBuilder::createReductionsGPU(
RuntimeFunction::OMPRTL___kmpc_is_team_main_thread);
Res = createRuntimeFunctionCall(IsMainThreadFn, {});
} else {
- CodeGenIP = Builder.saveIP();
StructType *ReductionsBufferTy = StructType::create(
Ctx, ReductionTypeArgs, "struct._globalized_locals_ty");
@@ -4979,8 +4976,6 @@ OpenMPIRBuilder::InsertPointOrErrorTy OpenMPIRBuilder::createReductionsGPU(
if (!GtLRFunc)
return GtLRFunc.takeError();
- Builder.restoreIP(CodeGenIP);
-
// The runtime's cross-team final aggregate uses the storage pointed at by
// its reduce-list argument as per-thread scratch. When the surrounding
// kernel is already in SPMD execution mode, clang emitted each reduction
@@ -5001,23 +4996,26 @@ OpenMPIRBuilder::InsertPointOrErrorTy OpenMPIRBuilder::createReductionsGPU(
Value *RuntimeRL = RL;
if (!IsSPMD) {
- CodeGenIP = Builder.saveIP();
- Builder.restoreIP(AllocaIP);
- // Allocate thread-local buffer for the reduction variables.
- Value *PerThreadScratchAlloca = Builder.CreateAlloca(
- ReductionsBufferTy, /*ArraySize=*/nullptr, ".omp.reduction.scratch");
- Value *PerThreadScratch = Builder.CreatePointerBitCastOrAddrSpaceCast(
- PerThreadScratchAlloca, PtrTy,
- PerThreadScratchAlloca->getName() + ".ascast");
- // Allocate thread-local buffer for the pointers to the reduction
- // variables.
- Value *PerThreadRedListAlloca =
- Builder.CreateAlloca(RedArrayTy, /*ArraySize=*/nullptr,
- ".omp.reduction.per_thread_red_list");
- RuntimeRL = Builder.CreatePointerBitCastOrAddrSpaceCast(
- PerThreadRedListAlloca, PtrTy,
- PerThreadRedListAlloca->getName() + ".ascast");
- Builder.restoreIP(CodeGenIP);
+ Value *PerThreadScratch = [&] {
+ IRBuilder<>::InsertPointGuard IPG(Builder);
+ Builder.restoreIP(AllocaIP);
+ // Allocate thread-local buffer for the reduction variables.
+ Value *PerThreadScratchAlloca =
+ Builder.CreateAlloca(ReductionsBufferTy, /*ArraySize=*/nullptr,
+ ".omp.reduction.scratch");
+ Value *PerThreadScratch = Builder.CreatePointerBitCastOrAddrSpaceCast(
+ PerThreadScratchAlloca, PtrTy,
+ PerThreadScratchAlloca->getName() + ".ascast");
+ // Allocate thread-local buffer for the pointers to the reduction
+ // variables.
+ Value *PerThreadRedListAlloca =
+ Builder.CreateAlloca(RedArrayTy, /*ArraySize=*/nullptr,
+ ".omp.reduction.per_thread_red_list");
+ RuntimeRL = Builder.CreatePointerBitCastOrAddrSpaceCast(
+ PerThreadRedListAlloca, PtrTy,
+ PerThreadRedListAlloca->getName() + ".ascast");
+ return PerThreadScratch;
+ }();
// Iterate over the reduction variables and copy the team-local value to
// the thread-local buffer.
diff --git a/llvm/unittests/Frontend/OpenMPIRBuilderTest.cpp b/llvm/unittests/Frontend/OpenMPIRBuilderTest.cpp
index 2010533b7e6db..e75754ffd05ea 100644
--- a/llvm/unittests/Frontend/OpenMPIRBuilderTest.cpp
+++ b/llvm/unittests/Frontend/OpenMPIRBuilderTest.cpp
@@ -5457,6 +5457,66 @@ TEST_F(OpenMPIRBuilderTest, CreateReductions) {
EXPECT_TRUE(findGEPZeroOne(ReductionFn->getArg(1), FirstRHS, SecondRHS));
}
+TEST_F(OpenMPIRBuilderTest, GPUTeamsReductionRuntimeCallHasDebugLoc) {
+ using InsertPointTy = OpenMPIRBuilder::InsertPointTy;
+ OpenMPIRBuilder OMPBuilder(*M);
+ OMPBuilder.Config.IsTargetDevice = true;
+ OMPBuilder.Config.setIsGPU(true);
+ OMPBuilder.initialize();
+
+ IRBuilder<> Builder(BB);
+ Instruction *AllocaInsertPt =
+ new BitCastInst(PoisonValue::get(Builder.getInt32Ty()),
+ Builder.getInt32Ty(), "allocapt", BB);
+ Builder.SetInsertPoint(BB);
+ Builder.SetCurrentDebugLocation(DL);
+ InsertPointTy AllocaIP(AllocaInsertPt->getIterator());
+ Value *Original;
+ Value *Private;
+ {
+ IRBuilder<>::InsertPointGuard Guard(Builder);
+ Builder.restoreIP(AllocaIP);
+ Original = Builder.CreateAlloca(Builder.getFloatTy(), nullptr, "original");
+ Private = Builder.CreateAlloca(Builder.getFloatTy(), nullptr, "private");
+ }
+
+ auto ReductionGen = [](InsertPointTy IP, unsigned, Value **LHSPtr,
+ Value **RHSPtr, Function *) {
+ IRBuilder<> Builder(IP.getNodeParent(), IP);
+ *LHSPtr = Builder.CreateAlloca(Builder.getFloatTy());
+ *RHSPtr = Builder.CreateAlloca(Builder.getFloatTy());
+ Builder.CreateLoad(Builder.getFloatTy(), *LHSPtr);
+ Builder.CreateLoad(Builder.getFloatTy(), *RHSPtr);
+ return Builder.saveIP();
+ };
+ OpenMPIRBuilder::ReductionInfo ReductionInfos[] = {
+ {Builder.getFloatTy(), Original, Private,
+ /*EvaluationKind=*/OpenMPIRBuilder::EvalKind::Scalar,
+ /*ReductionGen=*/nullptr, ReductionGen,
+ /*AtomicReductionGen=*/nullptr, /*DataPtrPtrGen=*/nullptr}};
+ bool IsByRef[] = {false};
+
+ OpenMPIRBuilder::LocationDescription Loc({Builder.saveIP(), DL});
+ ASSERT_EXPECTED_INIT(
+ InsertPointTy, AfterIP,
+ OMPBuilder.createReductionsGPU(
+ Loc, AllocaIP, Builder.saveIP(), ReductionInfos, IsByRef,
+ /*IsNoWait=*/false, /*IsTeamsReduction=*/true, /*IsSPMD=*/true,
+ OpenMPIRBuilder::ReductionGenCBKind::Clang, omp::NVPTXGridValues));
+ Builder.restoreIP(AfterIP);
+ Builder.CreateRetVoid();
+
+ SmallVector<CallInst *> Calls;
+ findCalls(F, RuntimeFunction::OMPRTL___kmpc_gpu_xteam_reduce_nowait,
+ OMPBuilder, Calls);
+ ASSERT_EQ(Calls.size(), 1u);
+ ASSERT_TRUE(Calls.front()->getDebugLoc());
+ EXPECT_EQ(Calls.front()->getDebugLoc()->getScope()->getSubprogram(),
+ F->getSubprogram());
+
+ EXPECT_FALSE(verifyModule(*M));
+}
+
static void createScan(llvm::Value *scanVar, llvm::Type *scanType,
OpenMPIRBuilder &OMPBuilder, IRBuilder<> &Builder,
OpenMPIRBuilder::LocationDescription Loc,
More information about the llvm-commits
mailing list