Skip to content

Commit 55783bd

Browse files
authored
[HIP] fix host min/max in header (#82956)
CUDA defines min/max functions for host in global namespace. HIP header needs to define them too to be compatible. Currently only min/max(int, int) is defined. This causes wrong result for arguments that are out of range for int. This patch defines host min/max functions to be compatible with CUDA. Also allows users to define `__HIP_NO_HOST_MIN_MAX_IN_GLOBAL_NAMESPACE__` to disable host max/min in global namespace. min/max functions with mixed signed/unsigned integer parameters are not defined unless `__HIP_DEFINE_MIXED_HOST_MIN_MAX__` is defined. Fixes: SWDEV-446564
1 parent 822142f commit 55783bd

File tree

1 file changed

+66
-6
lines changed

1 file changed

+66
-6
lines changed

clang/lib/Headers/__clang_hip_math.h

Lines changed: 66 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1306,15 +1306,75 @@ float min(float __x, float __y) { return __builtin_fminf(__x, __y); }
13061306
__DEVICE__
13071307
double min(double __x, double __y) { return __builtin_fmin(__x, __y); }
13081308

1309-
#if !defined(__HIPCC_RTC__) && !defined(__OPENMP_AMDGCN__)
1310-
__host__ inline static int min(int __arg1, int __arg2) {
1311-
return __arg1 < __arg2 ? __arg1 : __arg2;
1309+
// Define host min/max functions.
1310+
#if !defined(__HIPCC_RTC__) && !defined(__OPENMP_AMDGCN__) && \
1311+
!defined(__HIP_NO_HOST_MIN_MAX_IN_GLOBAL_NAMESPACE__)
1312+
1313+
#pragma push_macro("DEFINE_MIN_MAX_FUNCTIONS")
1314+
#pragma push_macro("DEFINE_MIN_MAX_FUNCTIONS")
1315+
#define DEFINE_MIN_MAX_FUNCTIONS(ret_type, type1, type2) \
1316+
inline ret_type min(const type1 __a, const type2 __b) { \
1317+
return (__a < __b) ? __a : __b; \
1318+
} \
1319+
inline ret_type max(const type1 __a, const type2 __b) { \
1320+
return (__a > __b) ? __a : __b; \
1321+
}
1322+
1323+
// Define min and max functions for same type comparisons
1324+
DEFINE_MIN_MAX_FUNCTIONS(int, int, int)
1325+
DEFINE_MIN_MAX_FUNCTIONS(unsigned int, unsigned int, unsigned int)
1326+
DEFINE_MIN_MAX_FUNCTIONS(long, long, long)
1327+
DEFINE_MIN_MAX_FUNCTIONS(unsigned long, unsigned long, unsigned long)
1328+
DEFINE_MIN_MAX_FUNCTIONS(long long, long long, long long)
1329+
DEFINE_MIN_MAX_FUNCTIONS(unsigned long long, unsigned long long,
1330+
unsigned long long)
1331+
1332+
// The host min/max functions below accept mixed signed/unsigned integer
1333+
// parameters and perform unsigned comparisons, which may produce unexpected
1334+
// results if a signed integer was passed unintentionally. To avoid this
1335+
// happening silently, these overloaded functions are not defined by default.
1336+
// However, for compatibility with CUDA, they will be defined if users define
1337+
// __HIP_DEFINE_MIXED_HOST_MIN_MAX__.
1338+
#ifdef __HIP_DEFINE_MIXED_HOST_MIN_MAX__
1339+
DEFINE_MIN_MAX_FUNCTIONS(unsigned int, int, unsigned int)
1340+
DEFINE_MIN_MAX_FUNCTIONS(unsigned int, unsigned int, int)
1341+
DEFINE_MIN_MAX_FUNCTIONS(unsigned long, long, unsigned long)
1342+
DEFINE_MIN_MAX_FUNCTIONS(unsigned long, unsigned long, long)
1343+
DEFINE_MIN_MAX_FUNCTIONS(unsigned long long, long long, unsigned long long)
1344+
DEFINE_MIN_MAX_FUNCTIONS(unsigned long long, unsigned long long, long long)
1345+
#endif // ifdef __HIP_DEFINE_MIXED_HOST_MIN_MAX__
1346+
1347+
// Floating-point comparisons using built-in functions
1348+
inline float min(float const __a, float const __b) {
1349+
return __builtin_fminf(__a, __b);
1350+
}
1351+
inline double min(double const __a, double const __b) {
1352+
return __builtin_fmin(__a, __b);
1353+
}
1354+
inline double min(float const __a, double const __b) {
1355+
return __builtin_fmin(__a, __b);
1356+
}
1357+
inline double min(double const __a, float const __b) {
1358+
return __builtin_fmin(__a, __b);
13121359
}
13131360

1314-
__host__ inline static int max(int __arg1, int __arg2) {
1315-
return __arg1 > __arg2 ? __arg1 : __arg2;
1361+
inline float max(float const __a, float const __b) {
1362+
return __builtin_fmaxf(__a, __b);
1363+
}
1364+
inline double max(double const __a, double const __b) {
1365+
return __builtin_fmax(__a, __b);
1366+
}
1367+
inline double max(float const __a, double const __b) {
1368+
return __builtin_fmax(__a, __b);
13161369
}
1317-
#endif // !defined(__HIPCC_RTC__) && !defined(__OPENMP_AMDGCN__)
1370+
inline double max(double const __a, float const __b) {
1371+
return __builtin_fmax(__a, __b);
1372+
}
1373+
1374+
#pragma pop_macro("DEFINE_MIN_MAX_FUNCTIONS")
1375+
1376+
#endif // !defined(__HIPCC_RTC__) && !defined(__OPENMP_AMDGCN__) &&
1377+
// !defined(__HIP_NO_HOST_MIN_MAX_IN_GLOBAL_NAMESPACE__)
13181378
#endif
13191379

13201380
#pragma pop_macro("__DEVICE__")

0 commit comments

Comments
 (0)