Skip to content

Commit 92bba68

Browse files
authored
[Offload] Fix handling of 'bare' mode when environment missing (#136794)
Summary: We treated the missing kernel environment as a unique mode, but it was kind of this random bool that was doing the same thing and it explicitly expects the kernel environment to be zero. It broke after the previous change since it used to default to SPMD and didn't handle zero in any of the other cases despite being used. This fixes that and queries for it without needing to consume an error.
1 parent 5b0cd17 commit 92bba68

File tree

6 files changed

+25
-16
lines changed

6 files changed

+25
-16
lines changed

llvm/include/llvm/Frontend/OpenMP/OMPDeviceConstants.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ namespace llvm {
1919
namespace omp {
2020

2121
enum OMPTgtExecModeFlags : unsigned char {
22+
OMP_TGT_EXEC_MODE_BARE = 0,
2223
OMP_TGT_EXEC_MODE_GENERIC = 1 << 0,
2324
OMP_TGT_EXEC_MODE_SPMD = 1 << 1,
2425
OMP_TGT_EXEC_MODE_GENERIC_SPMD =

offload/DeviceRTL/src/Kernel.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ using namespace ompx;
2626
// These flags are copied from "llvm/Frontend/OpenMP/OMPDeviceConstants.h" and
2727
// must be kept in-sync.
2828
enum OMPTgtExecModeFlags : unsigned char {
29+
OMP_TGT_EXEC_MODE_BARE = 0,
2930
OMP_TGT_EXEC_MODE_GENERIC = 1 << 0,
3031
OMP_TGT_EXEC_MODE_SPMD = 1 << 1,
3132
OMP_TGT_EXEC_MODE_GENERIC_SPMD =

offload/plugins-nextgen/common/include/PluginInterface.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,7 @@ struct GenericKernelTy {
297297
/// Indicate whether an execution mode is valid.
298298
static bool isValidExecutionMode(OMPTgtExecModeFlags ExecutionMode) {
299299
switch (ExecutionMode) {
300+
case OMP_TGT_EXEC_MODE_BARE:
300301
case OMP_TGT_EXEC_MODE_SPMD:
301302
case OMP_TGT_EXEC_MODE_GENERIC:
302303
case OMP_TGT_EXEC_MODE_GENERIC_SPMD:
@@ -309,6 +310,8 @@ struct GenericKernelTy {
309310
/// Get the execution mode name of the kernel.
310311
const char *getExecutionModeName() const {
311312
switch (KernelEnvironment.Configuration.ExecMode) {
313+
case OMP_TGT_EXEC_MODE_BARE:
314+
return "BARE";
312315
case OMP_TGT_EXEC_MODE_SPMD:
313316
return "SPMD";
314317
case OMP_TGT_EXEC_MODE_GENERIC:
@@ -364,6 +367,9 @@ struct GenericKernelTy {
364367
bool isSPMDMode() const {
365368
return KernelEnvironment.Configuration.ExecMode == OMP_TGT_EXEC_MODE_SPMD;
366369
}
370+
bool isBareMode() const {
371+
return KernelEnvironment.Configuration.ExecMode == OMP_TGT_EXEC_MODE_BARE;
372+
}
367373

368374
/// The kernel name.
369375
const char *Name;

offload/plugins-nextgen/common/src/PluginInterface.cpp

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -440,18 +440,19 @@ Error GenericKernelTy::init(GenericDeviceTy &GenericDevice,
440440
ImagePtr = &Image;
441441

442442
// Retrieve kernel environment object for the kernel.
443-
GlobalTy KernelEnv(std::string(Name) + "_kernel_environment",
444-
sizeof(KernelEnvironment), &KernelEnvironment);
443+
std::string EnvironmentName = std::string(Name) + "_kernel_environment";
445444
GenericGlobalHandlerTy &GHandler = GenericDevice.Plugin.getGlobalHandler();
446-
if (auto Err =
447-
GHandler.readGlobalFromImage(GenericDevice, *ImagePtr, KernelEnv)) {
448-
[[maybe_unused]] std::string ErrStr = toString(std::move(Err));
449-
DP("Failed to read kernel environment for '%s': %s\n"
450-
"Using default SPMD (2) execution mode\n",
451-
Name, ErrStr.data());
452-
assert(KernelEnvironment.Configuration.ReductionDataSize == 0 &&
453-
"Default initialization failed.");
454-
IsBareKernel = true;
445+
if (GHandler.isSymbolInImage(GenericDevice, Image, EnvironmentName)) {
446+
GlobalTy KernelEnv(EnvironmentName, sizeof(KernelEnvironment),
447+
&KernelEnvironment);
448+
if (auto Err =
449+
GHandler.readGlobalFromImage(GenericDevice, *ImagePtr, KernelEnv))
450+
return Err;
451+
} else {
452+
KernelEnvironment = KernelEnvironmentTy{};
453+
DP("Failed to read kernel environment for '%s' Using default Bare (0) "
454+
"execution mode\n",
455+
Name);
455456
}
456457

457458
// Max = Config.Max > 0 ? min(Config.Max, Device.Max) : Device.Max;
@@ -573,7 +574,7 @@ Error GenericKernelTy::launch(GenericDeviceTy &GenericDevice, void **ArgPtrs,
573574
KernelArgs.ThreadLimit[2]};
574575
uint32_t NumBlocks[3] = {KernelArgs.NumTeams[0], KernelArgs.NumTeams[1],
575576
KernelArgs.NumTeams[2]};
576-
if (!IsBareKernel) {
577+
if (!isBareMode()) {
577578
NumThreads[0] = getNumThreads(GenericDevice, NumThreads);
578579
NumBlocks[0] = getNumBlocks(GenericDevice, NumBlocks, KernelArgs.Tripcount,
579580
NumThreads[0], KernelArgs.ThreadLimit[0] > 0);
@@ -627,7 +628,7 @@ KernelLaunchParamsTy GenericKernelTy::prepareArgs(
627628

628629
uint32_t GenericKernelTy::getNumThreads(GenericDeviceTy &GenericDevice,
629630
uint32_t ThreadLimitClause[3]) const {
630-
assert(!IsBareKernel && "bare kernel should not call this function");
631+
assert(!isBareMode() && "bare kernel should not call this function");
631632

632633
assert(ThreadLimitClause[1] == 1 && ThreadLimitClause[2] == 1 &&
633634
"Multi dimensional launch not supported yet.");
@@ -645,7 +646,7 @@ uint32_t GenericKernelTy::getNumBlocks(GenericDeviceTy &GenericDevice,
645646
uint64_t LoopTripCount,
646647
uint32_t &NumThreads,
647648
bool IsNumThreadsFromUser) const {
648-
assert(!IsBareKernel && "bare kernel should not call this function");
649+
assert(!isBareMode() && "bare kernel should not call this function");
649650

650651
assert(NumTeamsClause[1] == 1 && NumTeamsClause[2] == 1 &&
651652
"Multi dimensional launch not supported yet.");

offload/test/offloading/ompx_bare.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ int main(int argc, char *argv[]) {
1515
const int N = num_blocks * block_size;
1616
int *data = (int *)malloc(N * sizeof(int));
1717

18-
// CHECK: "PluginInterface" device 0 info: Launching kernel __omp_offloading_{{.*}} with [64,1,1] blocks and [64,1,1] threads in SPMD mode
18+
// CHECK: "PluginInterface" device 0 info: Launching kernel __omp_offloading_{{.*}} with [64,1,1] blocks and [64,1,1] threads in BARE mode
1919

2020
#pragma omp target teams ompx_bare num_teams(num_blocks) thread_limit(block_size) map(from: data[0:N])
2121
{

offload/test/offloading/ompx_bare_multi_dim.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
#include <cassert>
88
#include <vector>
99

10-
// CHECK: "PluginInterface" device 0 info: Launching kernel __omp_offloading_{{.*}} with [2,4,6] blocks and [32,4,2] threads in SPMD mode
10+
// CHECK: "PluginInterface" device 0 info: Launching kernel __omp_offloading_{{.*}} with [2,4,6] blocks and [32,4,2] threads in BARE mode
1111

1212
int main(int argc, char *argv[]) {
1313
int bs[3] = {32u, 4u, 2u};

0 commit comments

Comments
 (0)