Skip to content

Commit 46e4f0c

Browse files
vmustyaigcbot
authored andcommitted
Support builtins for min and max operations in CMCL
.
1 parent e707d37 commit 46e4f0c

File tree

4 files changed

+95
-25
lines changed

4 files changed

+95
-25
lines changed

IGC/VectorCompiler/CMCL/lib/Headers/cm-cl/detail/builtins.h

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,11 @@ template <typename T> uint32_t __cm_cl_cbit(T src);
118118
template <typename T, int width>
119119
vector_impl<uint32_t, width> __cm_cl_cbit(vector_impl<T, width> src);
120120

121+
template <typename T> T __cm_cl_smin(T src0, T src1);
122+
template <typename T> T __cm_cl_smax(T src0, T src1);
123+
template <typename T> T __cm_cl_umin(T src0, T src1);
124+
template <typename T> T __cm_cl_umax(T src0, T src1);
125+
121126
template <typename T> T __cm_cl_fma(T src0, T src1, T src2);
122127

123128
uint32_t __cm_cl_bfrev(uint32_t src);
@@ -394,6 +399,30 @@ template <typename T> T absolute(T src) {
394399
return src;
395400
}
396401

402+
template <typename T> T min(T src0, T src1) {
403+
static_assert(cl::is_arithmetic<T>::value,
404+
"Min only supports arithmetic types");
405+
if constexpr (cl::is_floating_point<T>::value)
406+
return __cm_cl_minnum(src0, src1);
407+
408+
if constexpr (cl::is_signed<T>::value)
409+
return __cm_cl_smin(src0, src1);
410+
411+
return __cm_cl_umin(src0, src1);
412+
}
413+
414+
template <typename T> T max(T src0, T src1) {
415+
static_assert(cl::is_arithmetic<T>::value,
416+
"Max only supports arithmetic types");
417+
if constexpr (cl::is_floating_point<T>::value)
418+
return __cm_cl_maxnum(src0, src1);
419+
420+
if constexpr (cl::is_signed<T>::value)
421+
return __cm_cl_smax(src0, src1);
422+
423+
return __cm_cl_umax(src0, src1);
424+
}
425+
397426
template <typename T> T ceil(T src) {
398427
static_assert(cl::is_floating_point<T>::value,
399428
"Ceil function expects floating poing type.");
@@ -418,18 +447,6 @@ template <typename T> T roundne(T src) {
418447
return __cm_cl_roundne(src);
419448
}
420449

421-
template <typename T> T min_float(T src0, T src1) {
422-
static_assert(cl::is_floating_point<T>::value,
423-
"illegal type provided in min_float");
424-
return __cm_cl_minnum(src0, src1);
425-
}
426-
427-
template <typename T> T max_float(T src0, T src1) {
428-
static_assert(cl::is_floating_point<T>::value,
429-
"illegal type provided in max_float");
430-
return __cm_cl_maxnum(src0, src1);
431-
}
432-
433450
template <bool use_fast, typename T> T sqrt(T src) {
434451
static_assert(cl::is_floating_point<T>::value,
435452
"illegal type provided in sqrt");

IGC/VectorCompiler/CMCL/lib/Headers/cm-cl/math.h

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -271,28 +271,20 @@ vector<T, width> square_root(vector<T, width> src, cm::tag::fast_t) {
271271

272272
template <typename T, int width>
273273
vector<T, width> minimum(vector<T, width> src0, vector<T, width> src1) {
274-
static_assert(cl::is_floating_point<T>::value,
275-
"only floating point types supported yet for minimum");
276-
return detail::min_float(src0.cl_vector(), src1.cl_vector());
274+
return detail::min(src0.cl_vector(), src1.cl_vector());
277275
}
278276

279277
template <typename T, int width>
280278
vector<T, width> maximum(vector<T, width> src0, vector<T, width> src1) {
281-
static_assert(cl::is_floating_point<T>::value,
282-
"only floating point types supported yet for maximum");
283-
return detail::max_float(src0.cl_vector(), src1.cl_vector());
279+
return detail::max(src0.cl_vector(), src1.cl_vector());
284280
}
285281

286282
template <typename T> T minimum(T src0, T src1) {
287-
static_assert(cl::is_floating_point<T>::value,
288-
"only floating point types supported yet for minimum");
289-
return detail::min_float(src0, src1);
283+
return detail::min(src0, src1);
290284
}
291285

292286
template <typename T> T maximum(T src0, T src1) {
293-
static_assert(cl::is_floating_point<T>::value,
294-
"only floating point types supported yet for maximum");
295-
return detail::max_float(src0, src1);
287+
return detail::max(src0, src1);
296288
}
297289

298290
/*====================== log_base_2 =====================*/

IGC/VectorCompiler/CMCL/lib/Headers/opencl_type_traits.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,8 @@ template <typename T> struct pointer_traits<__constant T *> {
101101
template <typename T>
102102
struct is_floating_point
103103
: integral_constant<
104-
bool, is_same<float, typename remove_cv<T>::type>::value ||
104+
bool, is_same<half, typename remove_cv<T>::type>::value ||
105+
is_same<float, typename remove_cv<T>::type>::value ||
105106
is_same<double, typename remove_cv<T>::type>::value ||
106107
is_same<long double, typename remove_cv<T>::type>::value> {
107108
};

IGC/VectorCompiler/CMCL/lib/Support/TranslationDescription.json

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -699,6 +699,66 @@
699699
]
700700
}
701701
},
702+
"SMin": {
703+
"Name": "smin",
704+
"Operands": [
705+
{"Name": "Source0", "Kind": "Input"},
706+
{"Name": "Source1", "Kind": "Input"}
707+
],
708+
"TranslateInto": {
709+
"LLVM-Intrinsic": "smin",
710+
"ReturnType": {"GetBuiltinReturnType": []},
711+
"Operands": [
712+
{"GetBuiltinOperand": ["Source0"]},
713+
{"GetBuiltinOperand": ["Source1"]}
714+
]
715+
}
716+
},
717+
"SMax": {
718+
"Name": "smax",
719+
"Operands": [
720+
{"Name": "Source0", "Kind": "Input"},
721+
{"Name": "Source1", "Kind": "Input"}
722+
],
723+
"TranslateInto": {
724+
"LLVM-Intrinsic": "smax",
725+
"ReturnType": {"GetBuiltinReturnType": []},
726+
"Operands": [
727+
{"GetBuiltinOperand": ["Source0"]},
728+
{"GetBuiltinOperand": ["Source1"]}
729+
]
730+
}
731+
},
732+
"UMin": {
733+
"Name": "umin",
734+
"Operands": [
735+
{"Name": "Source0", "Kind": "Input"},
736+
{"Name": "Source1", "Kind": "Input"}
737+
],
738+
"TranslateInto": {
739+
"LLVM-Intrinsic": "umin",
740+
"ReturnType": {"GetBuiltinReturnType": []},
741+
"Operands": [
742+
{"GetBuiltinOperand": ["Source0"]},
743+
{"GetBuiltinOperand": ["Source1"]}
744+
]
745+
}
746+
},
747+
"UMax": {
748+
"Name": "umax",
749+
"Operands": [
750+
{"Name": "Source0", "Kind": "Input"},
751+
{"Name": "Source1", "Kind": "Input"}
752+
],
753+
"TranslateInto": {
754+
"LLVM-Intrinsic": "umax",
755+
"ReturnType": {"GetBuiltinReturnType": []},
756+
"Operands": [
757+
{"GetBuiltinOperand": ["Source0"]},
758+
{"GetBuiltinOperand": ["Source1"]}
759+
]
760+
}
761+
},
702762
"Log2": {
703763
"Name": "log2",
704764
"Operands": [

0 commit comments

Comments
 (0)