[llvm] [mlir] [OpenMP] Fix truncation/extension bug when calling __kmpc_push_num_teams (PR #173067)
Michael Klemm via llvm-commits
llvm-commits at lists.llvm.org
Fri Dec 19 10:02:08 PST 2025
https://github.com/mjklemm created https://github.com/llvm/llvm-project/pull/173067
This PR fixes a bug when the lower and upper bound for the number of teams was not an `int32`, but a different type. In this case, an internal compiler would trigger due to a mismatching call to `__kmpc_push_num_teams`.
>From 42e1d6da07acb19f70b1b1920800806da4ae0606 Mon Sep 17 00:00:00 2001
From: Michael Klemm <michael.klemm at amd.com>
Date: Thu, 18 Dec 2025 21:10:02 +0100
Subject: [PATCH] [OpenMP] Fix truncation/extension bug when calling
__kmpc_push_num_teams_51
---
llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp | 13 ++++++++++++-
.../Dialect/OpenMP/OpenMPToLLVMIRTranslation.cpp | 13 +++++++++----
2 files changed, 21 insertions(+), 5 deletions(-)
diff --git a/llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp b/llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp
index 716f8582dd7b2..548b2df53ad48 100644
--- a/llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp
+++ b/llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp
@@ -10286,10 +10286,21 @@ OpenMPIRBuilder::createTeams(const LocationDescription &Loc,
if (ThreadLimit == nullptr)
ThreadLimit = Builder.getInt32(0);
+ // The __kmpc_fork_teams function expects int32 as the arguments. So,
+ // truncate or sign extend the passed values to match the int32 parameters.
+ Value *NumTeamsLowerInt32 =
+ Builder.CreateSExtOrTrunc(NumTeamsLower, Builder.getInt32Ty());
+ Value *NumTeamsUpperInt32 =
+ Builder.CreateSExtOrTrunc(NumTeamsUpper, Builder.getInt32Ty());
+ Value *ThreadLimitInt32 =
+ Builder.CreateSExtOrTrunc(ThreadLimit, Builder.getInt32Ty());
+
Value *ThreadNum = getOrCreateThreadID(Ident);
+
createRuntimeFunctionCall(
getOrCreateRuntimeFunctionPtr(OMPRTL___kmpc_push_num_teams_51),
- {Ident, ThreadNum, NumTeamsLower, NumTeamsUpper, ThreadLimit});
+ {Ident, ThreadNum, NumTeamsLowerInt32, NumTeamsUpperInt32,
+ ThreadLimitInt32});
}
// Generate the body of teams.
InsertPointTy AllocaIP(AllocaBB, AllocaBB->begin());
diff --git a/mlir/lib/Target/LLVMIR/Dialect/OpenMP/OpenMPToLLVMIRTranslation.cpp b/mlir/lib/Target/LLVMIR/Dialect/OpenMP/OpenMPToLLVMIRTranslation.cpp
index 03d67a52853f6..6b06b4405b3a7 100644
--- a/mlir/lib/Target/LLVMIR/Dialect/OpenMP/OpenMPToLLVMIRTranslation.cpp
+++ b/mlir/lib/Target/LLVMIR/Dialect/OpenMP/OpenMPToLLVMIRTranslation.cpp
@@ -5889,15 +5889,20 @@ initTargetRuntimeAttrs(llvm::IRBuilderBase &builder,
attrs.TargetThreadLimit.front() =
moduleTranslation.lookupValue(targetThreadLimit);
+ // The __kmpc_fork_teams function expects int32 as the arguments. So,
+ // truncate or sign extend lower and upper num_teams bounds as well as
+ // thread_limit to match int32 ABI requirements for the OpenMP runtime.
if (numTeamsLower)
- attrs.MinTeams = moduleTranslation.lookupValue(numTeamsLower);
+ attrs.MinTeams = builder.CreateSExtOrTrunc(
+ moduleTranslation.lookupValue(numTeamsLower), builder.getInt32Ty());
if (numTeamsUpper)
- attrs.MaxTeams.front() = moduleTranslation.lookupValue(numTeamsUpper);
+ attrs.MaxTeams.front() = builder.CreateSExtOrTrunc(
+ moduleTranslation.lookupValue(numTeamsUpper), builder.getInt32Ty());
if (teamsThreadLimit)
- attrs.TeamsThreadLimit.front() =
- moduleTranslation.lookupValue(teamsThreadLimit);
+ attrs.TeamsThreadLimit.front() = builder.CreateSExtOrTrunc(
+ moduleTranslation.lookupValue(teamsThreadLimit), builder.getInt32Ty());
if (numThreads)
attrs.MaxThreads = moduleTranslation.lookupValue(numThreads);
More information about the llvm-commits
mailing list