Skip to content

Commit cd8a27e

Browse files
committed
[HLSL] Implementation of the frac intrinsic
This change implements the frontend for llvm#70099 Builtins.td - add the frac builtin CGBuiltin.cpp - add the builtin to DirectX intrinsic mapping hlsl_intrinsics.h - add the frac api SemaChecking.cpp - add type checks for builtin IntrinsicsDirectX.td - add the frac intrinsic The backend changes for this are going to be very simple: llvm@f309a0e They were not included because llvm/lib/Target/DirectX/DXIL.td is going through a major refactor.
1 parent f44c3fa commit cd8a27e

File tree

8 files changed

+171
-0
lines changed

8 files changed

+171
-0
lines changed

clang/include/clang/Basic/Builtins.td

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4536,6 +4536,12 @@ def HLSLDotProduct : LangBuiltin<"HLSL_LANG"> {
45364536
let Prototype = "void(...)";
45374537
}
45384538

4539+
def HLSLFrac : LangBuiltin<"HLSL_LANG"> {
4540+
let Spellings = ["__builtin_hlsl_elementwise_frac"];
4541+
let Attributes = [NoThrow, Const];
4542+
let Prototype = "void(...)";
4543+
}
4544+
45394545
// Builtins for XRay.
45404546
def XRayCustomEvent : Builtin {
45414547
let Spellings = ["__xray_customevent"];

clang/lib/CodeGen/CGBuiltin.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18007,6 +18007,14 @@ Value *CodeGenFunction::EmitHLSLBuiltinExpr(unsigned BuiltinID,
1800718007
/*ReturnType*/ T0->getScalarType(), Intrinsic::dx_dot,
1800818008
ArrayRef<Value *>{Op0, Op1}, nullptr, "dx.dot");
1800918009
} break;
18010+
case Builtin::BI__builtin_hlsl_elementwise_frac: {
18011+
Value *Op0 = EmitScalarExpr(E->getArg(0));
18012+
if (!E->getArg(0)->getType()->hasFloatingRepresentation())
18013+
llvm_unreachable("frac operand must have a float representation");
18014+
return Builder.CreateIntrinsic(
18015+
/*ReturnType*/ Op0->getType(), Intrinsic::dx_frac,
18016+
ArrayRef<Value *>{Op0}, nullptr, "dx.frac");
18017+
}
1801018018
}
1801118019
return nullptr;
1801218020
}

clang/lib/Headers/hlsl/hlsl_intrinsics.h

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,38 @@ double3 floor(double3);
317317
_HLSL_BUILTIN_ALIAS(__builtin_elementwise_floor)
318318
double4 floor(double4);
319319

320+
//===----------------------------------------------------------------------===//
321+
// frac builtins
322+
//===----------------------------------------------------------------------===//
323+
324+
/// \fn T frac(T x)
325+
/// \brief Returns the fractional (or decimal) part of x. \a x parameter.
326+
/// \param x The specified input value.
327+
///
328+
/// If \a the return value is greater than or equal to 0 and less than 1.
329+
330+
_HLSL_16BIT_AVAILABILITY(shadermodel, 6.2)
331+
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_elementwise_frac)
332+
half frac(half);
333+
_HLSL_16BIT_AVAILABILITY(shadermodel, 6.2)
334+
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_elementwise_frac)
335+
half2 frac(half2);
336+
_HLSL_16BIT_AVAILABILITY(shadermodel, 6.2)
337+
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_elementwise_frac)
338+
half3 frac(half3);
339+
_HLSL_16BIT_AVAILABILITY(shadermodel, 6.2)
340+
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_elementwise_frac)
341+
half4 frac(half4);
342+
343+
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_elementwise_frac)
344+
float frac(float);
345+
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_elementwise_frac)
346+
float2 frac(float2);
347+
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_elementwise_frac)
348+
float3 frac(float3);
349+
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_elementwise_frac)
350+
float4 frac(float4);
351+
320352
//===----------------------------------------------------------------------===//
321353
// log builtins
322354
//===----------------------------------------------------------------------===//

clang/lib/Sema/SemaChecking.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5247,6 +5247,21 @@ bool Sema::CheckHLSLBuiltinFunctionCall(unsigned BuiltinID, CallExpr *TheCall) {
52475247
return true;
52485248
break;
52495249
}
5250+
case Builtin::BI__builtin_hlsl_elementwise_frac: {
5251+
if (PrepareBuiltinElementwiseMathOneArgCall(TheCall))
5252+
return true;
5253+
QualType PassedType = TheCall->getArg(0)->getType();
5254+
if (!PassedType->hasFloatingRepresentation()) {
5255+
QualType ExpectedType = this->Context.FloatTy;
5256+
if (auto *VecTyA = PassedType->getAs<VectorType>())
5257+
ExpectedType = this->Context.getVectorType(
5258+
ExpectedType, VecTyA->getNumElements(), VecTyA->getVectorKind());
5259+
Diag(TheCall->getArg(0)->getBeginLoc(),
5260+
diag::err_typecheck_convert_incompatible)
5261+
<< PassedType << ExpectedType << 1 << 0 << 0;
5262+
return true;
5263+
}
5264+
}
52505265
}
52515266
return false;
52525267
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
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,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,NO_HALF
8+
9+
// NATIVE_HALF: define noundef half @
10+
// NATIVE_HALF: %dx.frac = call half @llvm.dx.frac.f16(
11+
// NATIVE_HALF: ret half %dx.frac
12+
// NO_HALF: define noundef float @"?test_frac_half@@YA$halff@$halff@@Z"(
13+
// NO_HALF: %dx.frac = call float @llvm.dx.frac.f32(
14+
// NO_HALF: ret float %dx.frac
15+
half test_frac_half ( half p0 ) {
16+
return frac ( p0 );
17+
}
18+
// NATIVE_HALF: define noundef <2 x half> @
19+
// NATIVE_HALF: %dx.frac = call <2 x half> @llvm.dx.frac.v2f16
20+
// NATIVE_HALF: ret <2 x half> %dx.frac
21+
// NO_HALF: define noundef <2 x float> @
22+
// NO_HALF: %dx.frac = call <2 x float> @llvm.dx.frac.v2f32(
23+
// NO_HALF: ret <2 x float> %dx.frac
24+
half2 test_frac_half2 ( half2 p0 ) {
25+
return frac ( p0 );
26+
}
27+
// NATIVE_HALF: define noundef <3 x half> @
28+
// NATIVE_HALF: %dx.frac = call <3 x half> @llvm.dx.frac.v3f16
29+
// NATIVE_HALF: ret <3 x half> %dx.frac
30+
// NO_HALF: define noundef <3 x float> @
31+
// NO_HALF: %dx.frac = call <3 x float> @llvm.dx.frac.v3f32(
32+
// NO_HALF: ret <3 x float> %dx.frac
33+
half3 test_frac_half3 ( half3 p0 ) {
34+
return frac ( p0 );
35+
}
36+
// NATIVE_HALF: define noundef <4 x half> @
37+
// NATIVE_HALF: %dx.frac = call <4 x half> @llvm.dx.frac.v4f16
38+
// NATIVE_HALF: ret <4 x half> %dx.frac
39+
// NO_HALF: define noundef <4 x float> @
40+
// NO_HALF: %dx.frac = call <4 x float> @llvm.dx.frac.v4f32(
41+
// NO_HALF: ret <4 x float> %dx.frac
42+
half4 test_frac_half4 ( half4 p0 ) {
43+
return frac ( p0 );
44+
}
45+
46+
// CHECK: define noundef float @
47+
// CHECK: %dx.frac = call float @llvm.dx.frac.f32(
48+
// CHECK: ret float %dx.frac
49+
float test_frac_float ( float p0 ) {
50+
return frac ( p0 );
51+
}
52+
// CHECK: define noundef <2 x float> @
53+
// CHECK: %dx.frac = call <2 x float> @llvm.dx.frac.v2f32
54+
// CHECK: ret <2 x float> %dx.frac
55+
float2 test_frac_float2 ( float2 p0 ) {
56+
return frac ( p0 );
57+
}
58+
// CHECK: define noundef <3 x float> @
59+
// CHECK: %dx.frac = call <3 x float> @llvm.dx.frac.v3f32
60+
// CHECK: ret <3 x float> %dx.frac
61+
float3 test_frac_float3 ( float3 p0 ) {
62+
return frac ( p0 );
63+
}
64+
// CHECK: define noundef <4 x float> @
65+
// CHECK: %dx.frac = call <4 x float> @llvm.dx.frac.v4f32
66+
// CHECK: ret <4 x float> %dx.frac
67+
float4 test_frac_float4 ( float4 p0 ) {
68+
return frac ( p0 );
69+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
2+
// RUN: %clang_cc1 -finclude-default-header -triple dxil-pc-shadermodel6.6-library %s -fnative-half-type -emit-llvm -disable-llvm-passes -verify -verify-ignore-unexpected
3+
4+
float test_too_few_arg () {
5+
return __builtin_hlsl_elementwise_frac ();
6+
// expected-error@-1 {{too few arguments to function call, expected 1, have 0}}
7+
}
8+
9+
float2 test_too_many_arg ( float2 p0) {
10+
return __builtin_hlsl_elementwise_frac ( p0, p0);
11+
// expected-error@-1 {{too many arguments to function call, expected 1, have 2}}
12+
}
13+
14+
float builtin_bool_to_float_type_promotion ( bool p1 ) {
15+
return __builtin_hlsl_elementwise_frac ( p1 );
16+
// expected-error@-1 {{1st argument must be a vector, integer or floating point type (was 'bool')}}
17+
}
18+
19+
float builtin_frac_int_to_float_promotion (int p1 ) {
20+
return __builtin_hlsl_elementwise_frac ( p1 );
21+
// expected-error@-1 {{passing 'int' to parameter of incompatible type 'float'}}
22+
}
23+
24+
float2 builtin_frac_int2_to_float2_promotion (int2 p1 ) {
25+
return __builtin_hlsl_elementwise_frac ( p1 );
26+
// expected-error@-1 {{passing 'int2' (aka 'vector<int, 2>') to parameter of incompatible type '__attribute__((__vector_size__(2 * sizeof(float)))) float' (vector of 2 'float' values)}}
27+
}

clang/test/SemaHLSL/OverloadResolutionBugs.hlsl

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,18 @@ int64_t test_builtin_dot_vec_int16_to_int64_promotion( int64_t2 p0, int16_t2 p1
6464
return dot( p0, p1 );
6565
}
6666

67+
float4 test_frac_int4 ( int4 p0 ) {
68+
return frac ( p0 );
69+
}
70+
71+
float test_frac_int ( int p0 ) {
72+
return frac ( p0 );
73+
}
74+
75+
float test_frac_bool( bool p0 ) {
76+
return frac ( p0 );
77+
}
78+
6779
// https://github.com/llvm/llvm-project/issues/81049
6880

6981
// RUN: %clang_cc1 -std=hlsl2021 -finclude-default-header -x hlsl -triple \

llvm/include/llvm/IR/IntrinsicsDirectX.td

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,6 @@ def int_dx_dot :
2424
Intrinsic<[LLVMVectorElementType<0>],
2525
[llvm_anyvector_ty, LLVMScalarOrSameVectorWidth<0, LLVMVectorElementType<0>>],
2626
[IntrNoMem, IntrWillReturn, Commutative] >;
27+
28+
def int_dx_frac : DefaultAttrsIntrinsic<[llvm_anyfloat_ty], [LLVMMatchType<0>]>;
2729
}

0 commit comments

Comments
 (0)