Skip to content

Commit 0248d27

Browse files
authored
Reland [HIP] fix host min/max in header (#133590)
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. Since some HIP apps defined min/max functions by themselves, newly added min/max function are under the control of macro `__HIP_DEFINE_EXTENDED_HOST_MIN_MAX__`, which is 0 by default. In the future, this will change to 1 by default after most existing HIP apps adopt this change. 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 091051f commit 0248d27

File tree

1 file changed

+80
-6
lines changed

1 file changed

+80
-6
lines changed

clang/lib/Headers/__clang_hip_math.h

Lines changed: 80 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1311,15 +1311,89 @@ float min(float __x, float __y) { return __builtin_fminf(__x, __y); }
13111311
__DEVICE__
13121312
double min(double __x, double __y) { return __builtin_fmin(__x, __y); }
13131313

1314-
#if !defined(__HIPCC_RTC__) && !defined(__OPENMP_AMDGCN__)
1315-
__host__ inline static int min(int __arg1, int __arg2) {
1316-
return __arg1 < __arg2 ? __arg1 : __arg2;
1314+
// Define host min/max functions.
1315+
#if !defined(__HIPCC_RTC__) && !defined(__OPENMP_AMDGCN__) && \
1316+
!defined(__HIP_NO_HOST_MIN_MAX_IN_GLOBAL_NAMESPACE__)
1317+
1318+
// TODO: make this default to 1 after existing HIP apps adopting this change.
1319+
#ifndef __HIP_DEFINE_EXTENDED_HOST_MIN_MAX__
1320+
#define __HIP_DEFINE_EXTENDED_HOST_MIN_MAX__ 0
1321+
#endif
1322+
1323+
#ifndef __HIP_DEFINE_MIXED_HOST_MIN_MAX__
1324+
#define __HIP_DEFINE_MIXED_HOST_MIN_MAX__ 0
1325+
#endif
1326+
1327+
#pragma push_macro("DEFINE_MIN_MAX_FUNCTIONS")
1328+
#pragma push_macro("DEFINE_MIN_MAX_FUNCTIONS")
1329+
#define DEFINE_MIN_MAX_FUNCTIONS(ret_type, type1, type2) \
1330+
inline ret_type min(const type1 __a, const type2 __b) { \
1331+
return (__a < __b) ? __a : __b; \
1332+
} \
1333+
inline ret_type max(const type1 __a, const type2 __b) { \
1334+
return (__a > __b) ? __a : __b; \
1335+
}
1336+
1337+
// Define min and max functions for same type comparisons
1338+
DEFINE_MIN_MAX_FUNCTIONS(int, int, int)
1339+
1340+
#if __HIP_DEFINE_EXTENDED_HOST_MIN_MAX__
1341+
DEFINE_MIN_MAX_FUNCTIONS(unsigned int, unsigned int, unsigned int)
1342+
DEFINE_MIN_MAX_FUNCTIONS(long, long, long)
1343+
DEFINE_MIN_MAX_FUNCTIONS(unsigned long, unsigned long, unsigned long)
1344+
DEFINE_MIN_MAX_FUNCTIONS(long long, long long, long long)
1345+
DEFINE_MIN_MAX_FUNCTIONS(unsigned long long, unsigned long long,
1346+
unsigned long long)
1347+
#endif // if __HIP_DEFINE_EXTENDED_HOST_MIN_MAX__
1348+
1349+
// The host min/max functions below accept mixed signed/unsigned integer
1350+
// parameters and perform unsigned comparisons, which may produce unexpected
1351+
// results if a signed integer was passed unintentionally. To avoid this
1352+
// happening silently, these overloaded functions are not defined by default.
1353+
// However, for compatibility with CUDA, they will be defined if users define
1354+
// __HIP_DEFINE_MIXED_HOST_MIN_MAX__.
1355+
#if __HIP_DEFINE_MIXED_HOST_MIN_MAX__
1356+
DEFINE_MIN_MAX_FUNCTIONS(unsigned int, int, unsigned int)
1357+
DEFINE_MIN_MAX_FUNCTIONS(unsigned int, unsigned int, int)
1358+
DEFINE_MIN_MAX_FUNCTIONS(unsigned long, long, unsigned long)
1359+
DEFINE_MIN_MAX_FUNCTIONS(unsigned long, unsigned long, long)
1360+
DEFINE_MIN_MAX_FUNCTIONS(unsigned long long, long long, unsigned long long)
1361+
DEFINE_MIN_MAX_FUNCTIONS(unsigned long long, unsigned long long, long long)
1362+
#endif // if __HIP_DEFINE_MIXED_HOST_MIN_MAX__
1363+
1364+
// Floating-point comparisons using built-in functions
1365+
#if __HIP_DEFINE_EXTENDED_HOST_MIN_MAX__
1366+
inline float min(float const __a, float const __b) {
1367+
return __builtin_fminf(__a, __b);
1368+
}
1369+
inline double min(double const __a, double const __b) {
1370+
return __builtin_fmin(__a, __b);
1371+
}
1372+
inline double min(float const __a, double const __b) {
1373+
return __builtin_fmin(__a, __b);
1374+
}
1375+
inline double min(double const __a, float const __b) {
1376+
return __builtin_fmin(__a, __b);
13171377
}
13181378

1319-
__host__ inline static int max(int __arg1, int __arg2) {
1320-
return __arg1 > __arg2 ? __arg1 : __arg2;
1379+
inline float max(float const __a, float const __b) {
1380+
return __builtin_fmaxf(__a, __b);
1381+
}
1382+
inline double max(double const __a, double const __b) {
1383+
return __builtin_fmax(__a, __b);
13211384
}
1322-
#endif // !defined(__HIPCC_RTC__) && !defined(__OPENMP_AMDGCN__)
1385+
inline double max(float const __a, double const __b) {
1386+
return __builtin_fmax(__a, __b);
1387+
}
1388+
inline double max(double const __a, float const __b) {
1389+
return __builtin_fmax(__a, __b);
1390+
}
1391+
#endif // if __HIP_DEFINE_EXTENDED_HOST_MIN_MAX__
1392+
1393+
#pragma pop_macro("DEFINE_MIN_MAX_FUNCTIONS")
1394+
1395+
#endif // !defined(__HIPCC_RTC__) && !defined(__OPENMP_AMDGCN__) &&
1396+
// !defined(__HIP_NO_HOST_MIN_MAX_IN_GLOBAL_NAMESPACE__)
13231397
#endif
13241398

13251399
#pragma pop_macro("__DEVICE__")

0 commit comments

Comments
 (0)