Skip to content

Commit 08533a3

Browse files
authored
[Offload][NFC] Reorganize utils:: and make Device/Host/Shared clearer (#100280)
We had three `utils::` namespaces, all with different "meaning" (host, device, hsa_utils). We should, when we can, keep "include/Shared" accessible from host and device, thus RefCountTy has been moved to a separate header. `hsa_utils` was introduced to make `utils::` less overloaded. And common functionality was de-duplicated, e.g., `utils::advance` and `utils::advanceVoidPtr` -> `utils:advancePtr`. Type punning now checks for the size of the result to make sure it matches the source type. No functional change was intended.
1 parent e6dece9 commit 08533a3

36 files changed

+308
-280
lines changed

offload/DeviceRTL/CMakeLists.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,8 @@ set(include_files
8080
${include_directory}/Profiling.h
8181
${include_directory}/State.h
8282
${include_directory}/Synchronization.h
83-
${include_directory}/Types.h
84-
${include_directory}/Utils.h
83+
${include_directory}/DeviceTypes.h
84+
${include_directory}/DeviceUtils.h
8585
${include_directory}/Workshare.h
8686
)
8787

@@ -99,7 +99,7 @@ set(src_files
9999
${source_directory}/State.cpp
100100
${source_directory}/Synchronization.cpp
101101
${source_directory}/Tasking.cpp
102-
${source_directory}/Utils.cpp
102+
${source_directory}/DeviceUtils.cpp
103103
${source_directory}/Workshare.cpp
104104
)
105105

offload/DeviceRTL/include/Allocator.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
#ifndef OMPTARGET_ALLOCATOR_H
1313
#define OMPTARGET_ALLOCATOR_H
1414

15-
#include "Types.h"
15+
#include "DeviceTypes.h"
1616

1717
// Forward declaration.
1818
struct KernelEnvironmentTy;

offload/DeviceRTL/include/Configuration.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
#include "Shared/Environment.h"
1717

18-
#include "Types.h"
18+
#include "DeviceTypes.h"
1919

2020
namespace ompx {
2121
namespace config {

offload/DeviceRTL/include/Types.h renamed to offload/DeviceRTL/include/DeviceTypes.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//===---------- Types.h - OpenMP types ---------------------------- C++ -*-===//
1+
//===---------- DeviceTypes.h - OpenMP types ---------------------- C++ -*-===//
22
//
33
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
44
// See https://llvm.org/LICENSE.txt for license information.
@@ -115,9 +115,9 @@ enum kmp_sched_t {
115115
#define SCHEDULE_WITHOUT_MODIFIERS(s) \
116116
(enum kmp_sched_t)( \
117117
(s) & ~(kmp_sched_modifier_nonmonotonic | kmp_sched_modifier_monotonic))
118-
#define SCHEDULE_HAS_MONOTONIC(s) (((s)&kmp_sched_modifier_monotonic) != 0)
118+
#define SCHEDULE_HAS_MONOTONIC(s) (((s) & kmp_sched_modifier_monotonic) != 0)
119119
#define SCHEDULE_HAS_NONMONOTONIC(s) \
120-
(((s)&kmp_sched_modifier_nonmonotonic) != 0)
120+
(((s) & kmp_sched_modifier_nonmonotonic) != 0)
121121
#define SCHEDULE_HAS_NO_MODIFIERS(s) \
122122
(((s) & (kmp_sched_modifier_nonmonotonic | kmp_sched_modifier_monotonic)) == \
123123
0)
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
//===--- DeviceUtils.h - OpenMP device runtime utility functions -- C++ -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
//
9+
//
10+
//===----------------------------------------------------------------------===//
11+
12+
#ifndef OMPTARGET_DEVICERTL_DEVICE_UTILS_H
13+
#define OMPTARGET_DEVICERTL_DEVICE_UTILS_H
14+
15+
#include "DeviceTypes.h"
16+
#include "Shared/Utils.h"
17+
18+
#pragma omp begin declare target device_type(nohost)
19+
20+
namespace utils {
21+
22+
/// Return the value \p Var from thread Id \p SrcLane in the warp if the thread
23+
/// is identified by \p Mask.
24+
int32_t shuffle(uint64_t Mask, int32_t Var, int32_t SrcLane, int32_t Width);
25+
26+
int32_t shuffleDown(uint64_t Mask, int32_t Var, uint32_t Delta, int32_t Width);
27+
28+
int64_t shuffleDown(uint64_t Mask, int64_t Var, uint32_t Delta, int32_t Width);
29+
30+
uint64_t ballotSync(uint64_t Mask, int32_t Pred);
31+
32+
/// Return \p LowBits and \p HighBits packed into a single 64 bit value.
33+
uint64_t pack(uint32_t LowBits, uint32_t HighBits);
34+
35+
/// Unpack \p Val into \p LowBits and \p HighBits.
36+
void unpack(uint64_t Val, uint32_t &LowBits, uint32_t &HighBits);
37+
38+
/// Return true iff \p Ptr is pointing into shared (local) memory (AS(3)).
39+
bool isSharedMemPtr(void *Ptr);
40+
41+
/// Return true iff \p Ptr is pointing into (thread) local memory (AS(5)).
42+
bool isThreadLocalMemPtr(void *Ptr);
43+
44+
/// A pointer variable that has by design an `undef` value. Use with care.
45+
[[clang::loader_uninitialized]] static void *const UndefPtr;
46+
47+
#define OMP_LIKELY(EXPR) __builtin_expect((bool)(EXPR), true)
48+
#define OMP_UNLIKELY(EXPR) __builtin_expect((bool)(EXPR), false)
49+
50+
} // namespace utils
51+
52+
#pragma omp end declare target
53+
54+
#endif

offload/DeviceRTL/include/Interface.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
#include "Shared/Environment.h"
1616

17-
#include "Types.h"
17+
#include "DeviceTypes.h"
1818

1919
/// External API
2020
///

offload/DeviceRTL/include/LibC.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
#ifndef OMPTARGET_LIBC_H
1313
#define OMPTARGET_LIBC_H
1414

15-
#include "Types.h"
15+
#include "DeviceTypes.h"
1616

1717
extern "C" {
1818

offload/DeviceRTL/include/Mapping.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
#ifndef OMPTARGET_MAPPING_H
1313
#define OMPTARGET_MAPPING_H
1414

15-
#include "Types.h"
15+
#include "DeviceTypes.h"
1616

1717
namespace ompx {
1818

offload/DeviceRTL/include/State.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@
1515
#include "Shared/Environment.h"
1616

1717
#include "Debug.h"
18+
#include "DeviceTypes.h"
19+
#include "DeviceUtils.h"
1820
#include "Mapping.h"
19-
#include "Types.h"
20-
#include "Utils.h"
2121

2222
// Forward declaration.
2323
struct KernelEnvironmentTy;

offload/DeviceRTL/include/Synchronization.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
#ifndef OMPTARGET_DEVICERTL_SYNCHRONIZATION_H
1313
#define OMPTARGET_DEVICERTL_SYNCHRONIZATION_H
1414

15-
#include "Types.h"
15+
#include "DeviceTypes.h"
1616

1717
namespace ompx {
1818

offload/DeviceRTL/include/Utils.h

Lines changed: 0 additions & 100 deletions
This file was deleted.

offload/DeviceRTL/src/Allocator.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@
1212

1313
#include "Allocator.h"
1414
#include "Configuration.h"
15+
#include "DeviceTypes.h"
16+
#include "DeviceUtils.h"
1517
#include "Mapping.h"
1618
#include "Synchronization.h"
17-
#include "Types.h"
18-
#include "Utils.h"
1919

2020
using namespace ompx;
2121

offload/DeviceRTL/src/Configuration.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212
//===----------------------------------------------------------------------===//
1313

1414
#include "Configuration.h"
15+
#include "DeviceTypes.h"
1516
#include "State.h"
16-
#include "Types.h"
1717

1818
using namespace ompx;
1919

offload/DeviceRTL/src/Debug.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@
1414

1515
#include "Configuration.h"
1616
#include "Debug.h"
17+
#include "DeviceTypes.h"
1718
#include "Interface.h"
1819
#include "Mapping.h"
1920
#include "State.h"
20-
#include "Types.h"
2121

2222
using namespace ompx;
2323

offload/DeviceRTL/src/Utils.cpp renamed to offload/DeviceRTL/src/DeviceUtils.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
//
1010
//===----------------------------------------------------------------------===//
1111

12-
#include "Utils.h"
12+
#include "DeviceUtils.h"
1313

1414
#include "Debug.h"
1515
#include "Interface.h"
@@ -33,7 +33,7 @@ uint64_t Pack(uint32_t LowBits, uint32_t HighBits) {
3333
return (((uint64_t)HighBits) << 32) | (uint64_t)LowBits;
3434
}
3535

36-
int32_t shuffle(uint64_t Mask, int32_t Var, int32_t SrcLane);
36+
int32_t shuffle(uint64_t Mask, int32_t Var, int32_t SrcLane, int32_t Width);
3737
int32_t shuffleDown(uint64_t Mask, int32_t Var, uint32_t LaneDelta,
3838
int32_t Width);
3939

@@ -44,8 +44,7 @@ uint64_t ballotSync(uint64_t Mask, int32_t Pred);
4444
///{
4545
#pragma omp begin declare variant match(device = {arch(amdgcn)})
4646

47-
int32_t shuffle(uint64_t Mask, int32_t Var, int32_t SrcLane) {
48-
int Width = mapping::getWarpSize();
47+
int32_t shuffle(uint64_t Mask, int32_t Var, int32_t SrcLane, int32_t Width) {
4948
int Self = mapping::getThreadIdInWarp();
5049
int Index = SrcLane + (Self & ~(Width - 1));
5150
return __builtin_amdgcn_ds_bpermute(Index << 2, Var);
@@ -77,8 +76,8 @@ bool isSharedMemPtr(const void *Ptr) {
7776
device = {arch(nvptx, nvptx64)}, \
7877
implementation = {extension(match_any)})
7978

80-
int32_t shuffle(uint64_t Mask, int32_t Var, int32_t SrcLane) {
81-
return __nvvm_shfl_sync_idx_i32(Mask, Var, SrcLane, 0x1f);
79+
int32_t shuffle(uint64_t Mask, int32_t Var, int32_t SrcLane, int32_t Width) {
80+
return __nvvm_shfl_sync_idx_i32(Mask, Var, SrcLane, Width - 1);
8281
}
8382

8483
int32_t shuffleDown(uint64_t Mask, int32_t Var, uint32_t Delta, int32_t Width) {
@@ -104,8 +103,9 @@ void utils::unpack(uint64_t Val, uint32_t &LowBits, uint32_t &HighBits) {
104103
impl::Unpack(Val, &LowBits, &HighBits);
105104
}
106105

107-
int32_t utils::shuffle(uint64_t Mask, int32_t Var, int32_t SrcLane) {
108-
return impl::shuffle(Mask, Var, SrcLane);
106+
int32_t utils::shuffle(uint64_t Mask, int32_t Var, int32_t SrcLane,
107+
int32_t Width) {
108+
return impl::shuffle(Mask, Var, SrcLane, Width);
109109
}
110110

111111
int32_t utils::shuffleDown(uint64_t Mask, int32_t Var, uint32_t Delta,

offload/DeviceRTL/src/Kernel.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@
1414

1515
#include "Allocator.h"
1616
#include "Debug.h"
17+
#include "DeviceTypes.h"
1718
#include "Interface.h"
1819
#include "Mapping.h"
1920
#include "State.h"
2021
#include "Synchronization.h"
21-
#include "Types.h"
2222
#include "Workshare.h"
2323

2424
#include "llvm/Frontend/OpenMP/OMPDeviceConstants.h"

offload/DeviceRTL/src/Mapping.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@
1010
//===----------------------------------------------------------------------===//
1111

1212
#include "Mapping.h"
13+
#include "DeviceTypes.h"
14+
#include "DeviceUtils.h"
1315
#include "Interface.h"
1416
#include "State.h"
15-
#include "Types.h"
16-
#include "Utils.h"
1717

1818
#pragma omp begin declare target device_type(nohost)
1919

offload/DeviceRTL/src/Misc.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
#include "Allocator.h"
1313
#include "Configuration.h"
14-
#include "Types.h"
14+
#include "DeviceTypes.h"
1515

1616
#include "Debug.h"
1717

offload/DeviceRTL/src/Parallelism.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,12 @@
3333
//===----------------------------------------------------------------------===//
3434

3535
#include "Debug.h"
36+
#include "DeviceTypes.h"
37+
#include "DeviceUtils.h"
3638
#include "Interface.h"
3739
#include "Mapping.h"
3840
#include "State.h"
3941
#include "Synchronization.h"
40-
#include "Types.h"
41-
#include "Utils.h"
4242

4343
using namespace ompx;
4444

0 commit comments

Comments
 (0)