Skip to content

Commit 2ec582b

Browse files
committed
address merge conflicts
1 parent e60ebbd commit 2ec582b

File tree

9 files changed

+191
-14
lines changed

9 files changed

+191
-14
lines changed

clang/include/clang/Basic/Builtins.td

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4535,9 +4535,14 @@ def HLSLDotProduct : LangBuiltin<"HLSL_LANG"> {
45354535
let Attributes = [NoThrow, Const];
45364536
let Prototype = "void(...)";
45374537
}
4538+
def HLSLFrac : LangBuiltin<"HLSL_LANG"> {
4539+
let Spellings = ["__builtin_hlsl_elementwise_frac"];
4540+
let Attributes = [NoThrow, Const];
4541+
let Prototype = "void(...)";
4542+
}
45384543

45394544
def HLSLLerp : LangBuiltin<"HLSL_LANG"> {
4540-
let Spellings = ["__builtin_hlsl_lerp"];
4545+
let Spellings = ["__builtin_hlsl_elementwise_frac"];
45414546
let Attributes = [NoThrow, Const];
45424547
let Prototype = "void(...)";
45434548
}

clang/include/clang/Basic/DiagnosticSemaKinds.td

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10270,7 +10270,7 @@ def err_vec_builtin_non_vector_all : Error<
1027010270
"all arguments to %0 must be vectors">;
1027110271
def err_vec_builtin_incompatible_vector_all : Error<
1027210272
"all arguments to %0 must have vectors of the same type">;
10273-
10273+
1027410274
def err_vec_builtin_non_vector : Error<
1027510275
"first two arguments to %0 must be vectors">;
1027610276
def err_vec_builtin_incompatible_vector : Error<

clang/lib/CodeGen/CGBuiltin.cpp

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18020,14 +18020,7 @@ Value *CodeGenFunction::EmitHLSLBuiltinExpr(unsigned BuiltinID,
1802018020
V = Builder.CreateFMul(S, V);
1802118021
return Builder.CreateFAdd(X, V, "dx.lerp");
1802218022
}
18023-
// DXC does this via casting to float should we do the same thing?
18024-
if (Xty->isIntegerTy()) {
18025-
auto V = Builder.CreateSub(Y, X);
18026-
V = Builder.CreateMul(S, V);
18027-
return Builder.CreateAdd(X, V, "dx.lerp");
18028-
}
18029-
// Bools should have been promoted
18030-
llvm_unreachable("Scalar Lerp is only supported on ints and floats.");
18023+
llvm_unreachable("Scalar Lerp is only supported on floats.");
1803118024
}
1803218025
// A VectorSplat should have happened
1803318026
assert(Xty->isVectorTy() && Yty->isVectorTy() && Sty->isVectorTy() &&
@@ -18041,12 +18034,24 @@ Value *CodeGenFunction::EmitHLSLBuiltinExpr(unsigned BuiltinID,
1804118034
E->getArg(2)->getType()->getAs<VectorType>();
1804218035
// A HLSLVectorTruncation should have happend
1804318036
assert(XVecTy->getNumElements() == YVecTy->getNumElements() &&
18044-
SVecTy->getNumElements() &&
18037+
XVecTy->getNumElements() == SVecTy->getNumElements() &&
1804518038
"Lerp requires vectors to be of the same size.");
18039+
assert(XVecTy->getElementType()->isRealFloatingType() &&
18040+
XVecTy->getElementType() == YVecTy->getElementType() &&
18041+
XVecTy->getElementType() == SVecTy->getElementType() &&
18042+
"Lerp requires float vectors to be of the same type.");
1804618043
return Builder.CreateIntrinsic(
1804718044
/*ReturnType*/ Xty, Intrinsic::dx_lerp, ArrayRef<Value *>{X, Y, S},
1804818045
nullptr, "dx.lerp");
1804918046
}
18047+
case Builtin::BI__builtin_hlsl_elementwise_frac: {
18048+
Value *Op0 = EmitScalarExpr(E->getArg(0));
18049+
if (!E->getArg(0)->getType()->hasFloatingRepresentation())
18050+
llvm_unreachable("frac operand must have a float representation");
18051+
return Builder.CreateIntrinsic(
18052+
/*ReturnType*/ Op0->getType(), Intrinsic::dx_frac,
18053+
ArrayRef<Value *>{Op0}, nullptr, "dx.frac");
18054+
}
1805018055
}
1805118056
return nullptr;
1805218057
}

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
// lerp builtins
322354
//===----------------------------------------------------------------------===//

clang/lib/Sema/SemaChecking.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5246,6 +5246,14 @@ bool CheckVectorElementCallArgs(Sema *S, CallExpr *TheCall) {
52465246
return true;
52475247
}
52485248

5249+
bool checkAllArgsAreFloatRepresentation(CallExpr *TheCall) {
5250+
for (unsigned i = 0; i < TheCall->getNumArgs(); ++i) {
5251+
if (!TheCall->getArg(0)->getType()->hasFloatingRepresentation())
5252+
return true;
5253+
}
5254+
return false;
5255+
}
5256+
52495257
// Note: returning true in this case results in CheckBuiltinFunctionCall
52505258
// returning an ExprError
52515259
bool Sema::CheckHLSLBuiltinFunctionCall(unsigned BuiltinID, CallExpr *TheCall) {
@@ -5259,13 +5267,30 @@ bool Sema::CheckHLSLBuiltinFunctionCall(unsigned BuiltinID, CallExpr *TheCall) {
52595267
return true;
52605268
break;
52615269
}
5270+
case Builtin::BI__builtin_hlsl_elementwise_frac: {
5271+
if (PrepareBuiltinElementwiseMathOneArgCall(TheCall))
5272+
return true;
5273+
QualType PassedType = TheCall->getArg(0)->getType();
5274+
if (!PassedType->hasFloatingRepresentation()) {
5275+
QualType ExpectedType = this->Context.FloatTy;
5276+
if (auto *VecTyA = PassedType->getAs<VectorType>())
5277+
ExpectedType = this->Context.getVectorType(
5278+
ExpectedType, VecTyA->getNumElements(), VecTyA->getVectorKind());
5279+
Diag(TheCall->getArg(0)->getBeginLoc(),
5280+
diag::err_typecheck_convert_incompatible)
5281+
<< PassedType << ExpectedType << 1 << 0 << 0;
5282+
return true;
5283+
}
5284+
}
52625285
case Builtin::BI__builtin_hlsl_lerp: {
52635286
if (checkArgCount(*this, TheCall, 3))
52645287
return true;
52655288
if (CheckVectorElementCallArgs(this, TheCall))
52665289
return true;
52675290
if (SemaBuiltinElementwiseTernaryMath(TheCall))
52685291
return true;
5292+
if (checkAllArgsAreFloatRepresentation(TheCall))
5293+
return true;
52695294
break;
52705295
}
52715296
}
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: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,10 @@ def int_dx_dot :
2525
[llvm_anyvector_ty, LLVMScalarOrSameVectorWidth<0, LLVMVectorElementType<0>>],
2626
[IntrNoMem, IntrWillReturn, Commutative] >;
2727

28+
def int_dx_frac : DefaultAttrsIntrinsic<[llvm_anyfloat_ty], [LLVMMatchType<0>]>;
29+
2830
def int_dx_lerp :
29-
Intrinsic<[LLVMMatchType<0>],
30-
[llvm_anyvector_ty, LLVMMatchType<0>, LLVMMatchType<0>],
31-
[IntrNoMem, IntrWillReturn, Commutative] >;
31+
Intrinsic<[LLVMScalarOrSameVectorWidth<0, LLVMVectorElementType<0>>],
32+
[llvm_anyvector_ty, LLVMScalarOrSameVectorWidth<0, LLVMVectorElementType<0>>,LLVMScalarOrSameVectorWidth<0, LLVMVectorElementType<0>>],
33+
[IntrNoMem, IntrWillReturn] >;
3234
}

0 commit comments

Comments
 (0)