Skip to content

Commit 3b58c79

Browse files
committed
Implements isnan() HLSL intrinsic for DXIL and SPIR-V targets.
Addresses #99132.
1 parent 7fbfd1c commit 3b58c79

File tree

14 files changed

+288
-1
lines changed

14 files changed

+288
-1
lines changed

clang/include/clang/Basic/Builtins.td

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5083,6 +5083,12 @@ def HLSLIsinf : LangBuiltin<"HLSL_LANG"> {
50835083
let Prototype = "void(...)";
50845084
}
50855085

5086+
def HLSLIsnan : LangBuiltin<"HLSL_LANG"> {
5087+
let Spellings = ["__builtin_hlsl_elementwise_isnan"];
5088+
let Attributes = [NoThrow, Const];
5089+
let Prototype = "int(...)";
5090+
}
5091+
50865092
def HLSLLerp : LangBuiltin<"HLSL_LANG"> {
50875093
let Spellings = ["__builtin_hlsl_lerp"];
50885094
let Attributes = [NoThrow, Const, CustomTypeChecking];

clang/lib/CodeGen/CGHLSLBuiltins.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -540,6 +540,21 @@ Value *CodeGenFunction::EmitHLSLBuiltinExpr(unsigned BuiltinID,
540540
retType, CGM.getHLSLRuntime().getIsInfIntrinsic(),
541541
ArrayRef<Value *>{Op0}, nullptr, "hlsl.isinf");
542542
}
543+
case Builtin::BI__builtin_hlsl_elementwise_isnan: {
544+
Value *Op0 = EmitScalarExpr(E->getArg(0));
545+
llvm::Type *Xty = Op0->getType();
546+
llvm::Type *retType = llvm::Type::getInt1Ty(this->getLLVMContext());
547+
if (Xty->isVectorTy()) {
548+
auto *XVecTy = E->getArg(0)->getType()->castAs<VectorType>();
549+
retType = llvm::VectorType::get(
550+
retType, ElementCount::getFixed(XVecTy->getNumElements()));
551+
}
552+
if (!E->getArg(0)->getType()->hasFloatingRepresentation())
553+
llvm_unreachable("isnan operand must have a float representation");
554+
return Builder.CreateIntrinsic(
555+
retType, CGM.getHLSLRuntime().getIsNaNIntrinsic(),
556+
ArrayRef<Value *>{Op0}, nullptr, "hlsl.isnan");
557+
}
543558
case Builtin::BI__builtin_hlsl_mad: {
544559
Value *M = EmitScalarExpr(E->getArg(0));
545560
Value *A = EmitScalarExpr(E->getArg(1));

clang/lib/CodeGen/CGHLSLRuntime.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ class CGHLSLRuntime {
9292
GENERATE_HLSL_INTRINSIC_FUNCTION(FlattenedThreadIdInGroup,
9393
flattened_thread_id_in_group)
9494
GENERATE_HLSL_INTRINSIC_FUNCTION(IsInf, isinf)
95+
GENERATE_HLSL_INTRINSIC_FUNCTION(IsNaN, isnan)
9596
GENERATE_HLSL_INTRINSIC_FUNCTION(Lerp, lerp)
9697
GENERATE_HLSL_INTRINSIC_FUNCTION(Normalize, normalize)
9798
GENERATE_HLSL_INTRINSIC_FUNCTION(Rsqrt, rsqrt)

clang/lib/Headers/hlsl/hlsl_alias_intrinsics.h

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1292,6 +1292,39 @@ bool3 isinf(float3);
12921292
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_elementwise_isinf)
12931293
bool4 isinf(float4);
12941294

1295+
//===----------------------------------------------------------------------===//
1296+
// isnan builtins
1297+
//===----------------------------------------------------------------------===//
1298+
1299+
/// \fn T isnan(T x)
1300+
/// \brief Determines if the specified value \a x is Not a Number.
1301+
/// \param x The specified input value.
1302+
///
1303+
/// Returns a value of the same size as the input, with a value set
1304+
/// to True if the x parameter is NaN or QNaN. Otherwise, False.
1305+
1306+
_HLSL_16BIT_AVAILABILITY(shadermodel, 6.2)
1307+
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_elementwise_isnan)
1308+
bool isnan(half);
1309+
_HLSL_16BIT_AVAILABILITY(shadermodel, 6.2)
1310+
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_elementwise_isnan)
1311+
bool2 isnan(half2);
1312+
_HLSL_16BIT_AVAILABILITY(shadermodel, 6.2)
1313+
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_elementwise_isnan)
1314+
bool3 isnan(half3);
1315+
_HLSL_16BIT_AVAILABILITY(shadermodel, 6.2)
1316+
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_elementwise_isnan)
1317+
bool4 isnan(half4);
1318+
1319+
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_elementwise_isnan)
1320+
bool isnan(float);
1321+
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_elementwise_isnan)
1322+
bool2 isnan(float2);
1323+
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_elementwise_isnan)
1324+
bool3 isnan(float3);
1325+
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_elementwise_isnan)
1326+
bool4 isnan(float4);
1327+
12951328
//===----------------------------------------------------------------------===//
12961329
// lerp builtins
12971330
//===----------------------------------------------------------------------===//

clang/lib/Headers/hlsl/hlsl_compat_overloads.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,15 @@ constexpr bool2 isinf(double2 V) { return isinf((float2)V); }
273273
constexpr bool3 isinf(double3 V) { return isinf((float3)V); }
274274
constexpr bool4 isinf(double4 V) { return isinf((float4)V); }
275275

276+
//===----------------------------------------------------------------------===//
277+
// isnan builtins overloads
278+
//===----------------------------------------------------------------------===//
279+
280+
constexpr bool isnan(double V) { return isnan((float)V); }
281+
constexpr bool2 isnan(double2 V) { return isnan((float2)V); }
282+
constexpr bool3 isnan(double3 V) { return isnan((float3)V); }
283+
constexpr bool4 isnan(double4 V) { return isnan((float4)V); }
284+
276285
//===----------------------------------------------------------------------===//
277286
// lerp builtins overloads
278287
//===----------------------------------------------------------------------===//

clang/lib/Headers/hlsl/hlsl_intrinsics.h

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,43 @@ fmod(__detail::HLSL_FIXED_VECTOR<float, N> X,
303303
return __detail::fmod_vec_impl(X, Y);
304304
}
305305

306+
//===----------------------------------------------------------------------===//
307+
// isnan builtins
308+
//===----------------------------------------------------------------------===//
309+
310+
/// \fn bool isnan(T x)
311+
/// \brief Returns whether x is NaN or QNaN.
312+
/// \param x [in] A number or vector of numbers.
313+
///
314+
/// Return whether (each element of) x is NaN or QNaN.
315+
316+
template <typename T>
317+
_HLSL_16BIT_AVAILABILITY(shadermodel, 6.2)
318+
const inline __detail::enable_if_t<
319+
__detail::is_arithmetic<T>::Value && __detail::is_same<half, T>::value, T>
320+
isnan(T X) {
321+
return __builtin_elementwise_isnan(X);
322+
}
323+
template <typename T>
324+
const inline __detail::enable_if_t<
325+
__detail::is_arithmetic<T>::Value && __detail::is_same<float, T>::value, T>
326+
isnan(T X) {
327+
return __builtin_elementwise_isnan(X);
328+
}
329+
330+
template <int N>
331+
_HLSL_16BIT_AVAILABILITY(shadermodel, 6.2)
332+
const inline __detail::HLSL_FIXED_VECTOR<half, N>
333+
isnan(__detail::HLSL_FIXED_VECTOR<half, N> X) {
334+
return __builtin_elementwise_isnan(X);
335+
}
336+
337+
template <int N>
338+
const inline __detail::HLSL_FIXED_VECTOR<float, N>
339+
isnan(__detail::HLSL_FIXED_VECTOR<float, N> X) {
340+
return __builtin_elementwise_isnan(X);
341+
}
342+
306343
//===----------------------------------------------------------------------===//
307344
// ldexp builtins
308345
//===----------------------------------------------------------------------===//

clang/lib/Sema/SemaHLSL.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2990,7 +2990,8 @@ bool SemaHLSL::CheckBuiltinFunctionCall(unsigned BuiltinID, CallExpr *TheCall) {
29902990
return true;
29912991
break;
29922992
}
2993-
case Builtin::BI__builtin_hlsl_elementwise_isinf: {
2993+
case Builtin::BI__builtin_hlsl_elementwise_isinf:
2994+
case Builtin::BI__builtin_hlsl_elementwise_isnan: {
29942995
if (SemaRef.checkArgCount(TheCall, 1))
29952996
return true;
29962997
if (CheckAllArgTypesAreCorrect(&SemaRef, TheCall,
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// RUN: %clang_cc1 -std=hlsl202x -finclude-default-header -x hlsl -triple \
2+
// RUN: dxil-pc-shadermodel6.3-library %s -emit-llvm -disable-llvm-passes \
3+
// RUN: -o - | FileCheck %s
4+
5+
// CHECK: define hidden noundef i1 @
6+
// CHECK: %hlsl.isnan = call i1 @llvm.dx.isnan.f32(
7+
// CHECK: ret i1 %hlsl.isnan
8+
bool test_isnan_double(double p0) { return isnan(p0); }
9+
// CHECK: define hidden noundef <2 x i1> @
10+
// CHECK: %hlsl.isnan = call <2 x i1> @llvm.dx.isnan.v2f32
11+
// CHECK: ret <2 x i1> %hlsl.isnan
12+
bool2 test_isnan_double2(double2 p0) { return isnan(p0); }
13+
// CHECK: define hidden noundef <3 x i1> @
14+
// CHECK: %hlsl.isnan = call <3 x i1> @llvm.dx.isnan.v3f32
15+
// CHECK: ret <3 x i1> %hlsl.isnan
16+
bool3 test_isnan_double3(double3 p0) { return isnan(p0); }
17+
// CHECK: define hidden noundef <4 x i1> @
18+
// CHECK: %hlsl.isnan = call <4 x i1> @llvm.dx.isnan.v4f32
19+
// CHECK: ret <4 x i1> %hlsl.isnan
20+
bool4 test_isnan_double4(double4 p0) { return isnan(p0); }
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
// RUN: %clang_cc1 -finclude-default-header -x hlsl -triple \
2+
// RUN: dxil-pc-shadermodel6.3-library %s -fnative-half-type \
3+
// RUN: -emit-llvm -disable-llvm-passes -o - | FileCheck %s \
4+
// RUN: --check-prefixes=CHECK,DXCHECK,NATIVE_HALF
5+
// RUN: %clang_cc1 -finclude-default-header -x hlsl -triple \
6+
// RUN: dxil-pc-shadermodel6.3-library %s -emit-llvm -disable-llvm-passes \
7+
// RUN: -o - | FileCheck %s --check-prefixes=CHECK,DXCHECK,NO_HALF
8+
9+
// RUN: %clang_cc1 -finclude-default-header -x hlsl -triple \
10+
// RUN: spirv-unknown-vulkan-compute %s -fnative-half-type \
11+
// RUN: -emit-llvm -disable-llvm-passes -o - | FileCheck %s \
12+
// RUN: --check-prefixes=CHECK,SPVCHECK,NATIVE_HALF
13+
// RUN: %clang_cc1 -finclude-default-header -x hlsl -triple \
14+
// RUN: spirv-unknown-vulkan-compute %s -emit-llvm -disable-llvm-passes \
15+
// RUN: -o - | FileCheck %s --check-prefixes=CHECK,SPVCHECK,NO_HALF
16+
17+
// DXCHECK: define hidden [[FN_TYPE:]]noundef i1 @
18+
// SPVCHECK: define hidden [[FN_TYPE:spir_func ]]noundef i1 @
19+
// DXCHECK: %hlsl.isnan = call i1 @llvm.[[ICF:dx]].isnan.f32(
20+
// SPVCHECK: %hlsl.isnan = call i1 @llvm.[[ICF:spv]].isnan.f32(
21+
// CHECK: ret i1 %hlsl.isnan
22+
bool test_isnan_float(float p0) { return isnan(p0); }
23+
24+
// CHECK: define hidden [[FN_TYPE]]noundef i1 @
25+
// NATIVE_HALF: %hlsl.isnan = call i1 @llvm.[[ICF]].isnan.f16(
26+
// NO_HALF: %hlsl.isnan = call i1 @llvm.[[ICF]].isnan.f32(
27+
// CHECK: ret i1 %hlsl.isnan
28+
bool test_isnan_half(half p0) { return isnan(p0); }
29+
30+
// CHECK: define hidden [[FN_TYPE]]noundef <2 x i1> @
31+
// NATIVE_HALF: %hlsl.isnan = call <2 x i1> @llvm.[[ICF]].isnan.v2f16
32+
// NO_HALF: %hlsl.isnan = call <2 x i1> @llvm.[[ICF]].isnan.v2f32(
33+
// CHECK: ret <2 x i1> %hlsl.isnan
34+
bool2 test_isnan_half2(half2 p0) { return isnan(p0); }
35+
36+
// NATIVE_HALF: define hidden [[FN_TYPE]]noundef <3 x i1> @
37+
// NATIVE_HALF: %hlsl.isnan = call <3 x i1> @llvm.[[ICF]].isnan.v3f16
38+
// NO_HALF: %hlsl.isnan = call <3 x i1> @llvm.[[ICF]].isnan.v3f32(
39+
// CHECK: ret <3 x i1> %hlsl.isnan
40+
bool3 test_isnan_half3(half3 p0) { return isnan(p0); }
41+
42+
// NATIVE_HALF: define hidden [[FN_TYPE]]noundef <4 x i1> @
43+
// NATIVE_HALF: %hlsl.isnan = call <4 x i1> @llvm.[[ICF]].isnan.v4f16
44+
// NO_HALF: %hlsl.isnan = call <4 x i1> @llvm.[[ICF]].isnan.v4f32(
45+
// CHECK: ret <4 x i1> %hlsl.isnan
46+
bool4 test_isnan_half4(half4 p0) { return isnan(p0); }
47+
48+
49+
// CHECK: define hidden [[FN_TYPE]]noundef <2 x i1> @
50+
// CHECK: %hlsl.isnan = call <2 x i1> @llvm.[[ICF]].isnan.v2f32
51+
// CHECK: ret <2 x i1> %hlsl.isnan
52+
bool2 test_isnan_float2(float2 p0) { return isnan(p0); }
53+
54+
// CHECK: define hidden [[FN_TYPE]]noundef <3 x i1> @
55+
// CHECK: %hlsl.isnan = call <3 x i1> @llvm.[[ICF]].isnan.v3f32
56+
// CHECK: ret <3 x i1> %hlsl.isnan
57+
bool3 test_isnan_float3(float3 p0) { return isnan(p0); }
58+
59+
// CHECK: define hidden [[FN_TYPE]]noundef <4 x i1> @
60+
// CHECK: %hlsl.isnan = call <4 x i1> @llvm.[[ICF]].isnan.v4f32
61+
// CHECK: ret <4 x i1> %hlsl.isnan
62+
bool4 test_isnan_float4(float4 p0) { return isnan(p0); }
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
2+
// RUN: %clang_cc1 -finclude-default-header -triple dxil-pc-shadermodel6.6-library %s -fnative-half-type -emit-llvm-only -disable-llvm-passes -verify
3+
4+
bool test_too_few_arg() {
5+
return __builtin_hlsl_elementwise_isnan();
6+
// expected-error@-1 {{too few arguments to function call, expected 1, have 0}}
7+
}
8+
9+
bool2 test_too_many_arg(float2 p0) {
10+
return __builtin_hlsl_elementwise_isnan(p0, p0);
11+
// expected-error@-1 {{too many arguments to function call, expected 1, have 2}}
12+
}
13+
14+
bool builtin_bool_to_float_type_promotion(bool p1) {
15+
return __builtin_hlsl_elementwise_isnan(p1);
16+
// expected-error@-1 {{1st argument must be a scalar or vector of 16 or 32 bit floating-point types (was 'bool')}}
17+
}
18+
19+
bool builtin_isnan_int_to_float_promotion(int p1) {
20+
return __builtin_hlsl_elementwise_isnan(p1);
21+
// expected-error@-1 {{1st argument must be a scalar or vector of 16 or 32 bit floating-point types (was 'int')}}
22+
}
23+
24+
bool2 builtin_isnan_int2_to_float2_promotion(int2 p1) {
25+
return __builtin_hlsl_elementwise_isnan(p1);
26+
// expected-error@-1 {{1st argument must be a scalar or vector of 16 or 32 bit floating-point types (was 'int2' (aka 'vector<int, 2>'))}}
27+
}
28+
29+
// builtins are variadic functions and so are subject to DefaultVariadicArgumentPromotion
30+
half builtin_isnan_half_scalar (half p0) {
31+
return __builtin_hlsl_elementwise_isnan (p0);
32+
// expected-error@-1 {{1st argument must be a scalar or vector of 16 or 32 bit floating-point types (was 'double')}}
33+
}
34+
35+
float builtin_isnan_float_scalar ( float p0) {
36+
return __builtin_hlsl_elementwise_isnan (p0);
37+
// expected-error@-1 {{1st argument must be a scalar or vector of 16 or 32 bit floating-point types (was 'double')}}
38+
}

0 commit comments

Comments
 (0)