Skip to content

Commit df420ee

Browse files
authored
Implements isnan() HLSL intrinsic for DXIL and SPIR-V targets. (#157733)
Addresses #99132.
1 parent 68a253d commit df420ee

File tree

17 files changed

+311
-1
lines changed

17 files changed

+311
-1
lines changed

clang/include/clang/Basic/Builtins.td

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

5104+
def HLSLIsnan : LangBuiltin<"HLSL_LANG"> {
5105+
let Spellings = ["__builtin_hlsl_elementwise_isnan"];
5106+
let Attributes = [NoThrow, Const];
5107+
let Prototype = "void(...)";
5108+
}
5109+
51045110
def HLSLLerp : LangBuiltin<"HLSL_LANG"> {
51055111
let Spellings = ["__builtin_hlsl_lerp"];
51065112
let Attributes = [NoThrow, Const, CustomTypeChecking];

clang/lib/CodeGen/CGHLSLBuiltins.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -547,6 +547,21 @@ Value *CodeGenFunction::EmitHLSLBuiltinExpr(unsigned BuiltinID,
547547
retType, CGM.getHLSLRuntime().getIsInfIntrinsic(),
548548
ArrayRef<Value *>{Op0}, nullptr, "hlsl.isinf");
549549
}
550+
case Builtin::BI__builtin_hlsl_elementwise_isnan: {
551+
Value *Op0 = EmitScalarExpr(E->getArg(0));
552+
llvm::Type *Xty = Op0->getType();
553+
llvm::Type *retType = llvm::Type::getInt1Ty(this->getLLVMContext());
554+
if (Xty->isVectorTy()) {
555+
auto *XVecTy = E->getArg(0)->getType()->castAs<VectorType>();
556+
retType = llvm::VectorType::get(
557+
retType, ElementCount::getFixed(XVecTy->getNumElements()));
558+
}
559+
if (!E->getArg(0)->getType()->hasFloatingRepresentation())
560+
llvm_unreachable("isnan operand must have a float representation");
561+
return Builder.CreateIntrinsic(
562+
retType, CGM.getHLSLRuntime().getIsNaNIntrinsic(),
563+
ArrayRef<Value *>{Op0}, nullptr, "hlsl.isnan");
564+
}
550565
case Builtin::BI__builtin_hlsl_mad: {
551566
Value *M = EmitScalarExpr(E->getArg(0));
552567
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
@@ -95,6 +95,7 @@ class CGHLSLRuntime {
9595
GENERATE_HLSL_INTRINSIC_FUNCTION(FlattenedThreadIdInGroup,
9696
flattened_thread_id_in_group)
9797
GENERATE_HLSL_INTRINSIC_FUNCTION(IsInf, isinf)
98+
GENERATE_HLSL_INTRINSIC_FUNCTION(IsNaN, isnan)
9899
GENERATE_HLSL_INTRINSIC_FUNCTION(Lerp, lerp)
99100
GENERATE_HLSL_INTRINSIC_FUNCTION(Normalize, normalize)
100101
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
@@ -352,6 +352,15 @@ constexpr bool3 isinf(double3 V) { return isinf((float3)V); }
352352
_DXC_DEPRECATED_64BIT_FN(fn)
353353
constexpr bool4 isinf(double4 V) { return isinf((float4)V); }
354354

355+
//===----------------------------------------------------------------------===//
356+
// isnan builtins overloads
357+
//===----------------------------------------------------------------------===//
358+
359+
constexpr bool isnan(double V) { return isnan((float)V); }
360+
constexpr bool2 isnan(double2 V) { return isnan((float2)V); }
361+
constexpr bool3 isnan(double3 V) { return isnan((float3)V); }
362+
constexpr bool4 isnan(double4 V) { return isnan((float4)V); }
363+
355364
//===----------------------------------------------------------------------===//
356365
// lerp builtins overloads
357366
//===----------------------------------------------------------------------===//

clang/lib/Sema/SemaHLSL.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3090,7 +3090,8 @@ bool SemaHLSL::CheckBuiltinFunctionCall(unsigned BuiltinID, CallExpr *TheCall) {
30903090
return true;
30913091
break;
30923092
}
3093-
case Builtin::BI__builtin_hlsl_elementwise_isinf: {
3093+
case Builtin::BI__builtin_hlsl_elementwise_isinf:
3094+
case Builtin::BI__builtin_hlsl_elementwise_isnan: {
30943095
if (SemaRef.checkArgCount(TheCall, 1))
30953096
return true;
30963097
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+
}

llvm/include/llvm/IR/IntrinsicsDirectX.td

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,8 @@ def int_dx_degrees : DefaultAttrsIntrinsic<[LLVMMatchType<0>], [llvm_anyfloat_ty
134134

135135
def int_dx_isinf : DefaultAttrsIntrinsic<[LLVMScalarOrSameVectorWidth<0, llvm_i1_ty>],
136136
[llvm_anyfloat_ty], [IntrNoMem]>;
137+
def int_dx_isnan : DefaultAttrsIntrinsic<[LLVMScalarOrSameVectorWidth<0, llvm_i1_ty>],
138+
[llvm_anyfloat_ty], [IntrNoMem]>;
137139

138140
def int_dx_lerp : DefaultAttrsIntrinsic<[LLVMMatchType<0>], [llvm_anyfloat_ty, LLVMMatchType<0>,LLVMMatchType<0>],
139141
[IntrNoMem]>;

0 commit comments

Comments
 (0)