Skip to content

Commit a6c359a

Browse files
committed
[HLSL] Overloads for lerp with a scalar weight
This adds overloads for the `lerp` function that accept a scalar for the weight parameter by splatting it into the appropriate vector. Fixes #137827
1 parent d851490 commit a6c359a

File tree

4 files changed

+40
-14
lines changed

4 files changed

+40
-14
lines changed

clang/lib/Headers/hlsl/hlsl_compat_overloads.h

+6
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,12 @@ constexpr bool4 isinf(double4 V) { return isinf((float4)V); }
277277
// lerp builtins overloads
278278
//===----------------------------------------------------------------------===//
279279

280+
template <typename T, uint N>
281+
constexpr __detail::enable_if_t<(N > 1 && N <= 4), vector<T, N>>
282+
lerp(vector<T, N> x, vector<T, N> y, T s) {
283+
return lerp(x, y, (vector<T, N>)s);
284+
}
285+
280286
_DXC_COMPAT_TERNARY_DOUBLE_OVERLOADS(lerp)
281287
_DXC_COMPAT_TERNARY_INTEGER_OVERLOADS(lerp)
282288

clang/lib/Sema/SemaHLSL.cpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -2587,7 +2587,8 @@ bool SemaHLSL::CheckBuiltinFunctionCall(unsigned BuiltinID, CallExpr *TheCall) {
25872587
case Builtin::BI__builtin_hlsl_lerp: {
25882588
if (SemaRef.checkArgCount(TheCall, 3))
25892589
return true;
2590-
if (CheckVectorElementCallArgs(&SemaRef, TheCall))
2590+
if (CheckAnyScalarOrVector(&SemaRef, TheCall, 0) ||
2591+
CheckAllArgsHaveSameType(&SemaRef, TheCall))
25912592
return true;
25922593
if (SemaRef.BuiltinElementwiseTernaryMath(TheCall))
25932594
return true;

clang/test/CodeGenHLSL/builtins/lerp-overloads.hlsl

+21-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1-
// RUN: %clang_cc1 -std=hlsl202x -finclude-default-header -x hlsl -triple dxil-pc-shadermodel6.3-library %s -emit-llvm -o - | FileCheck %s --check-prefixes=CHECK -DFNATTRS="noundef nofpclass(nan inf)" -DTARGET=dx
2-
// RUN: %clang_cc1 -std=hlsl202x -finclude-default-header -x hlsl -triple spirv-unknown-vulkan-compute %s -emit-llvm -o - | FileCheck %s --check-prefixes=CHECK -DFNATTRS="spir_func noundef nofpclass(nan inf)" -DTARGET=spv
1+
// RUN: %clang_cc1 -std=hlsl202x -finclude-default-header -x hlsl -triple dxil-pc-shadermodel6.3-library %s -fnative-half-type -emit-llvm -o - | FileCheck %s --check-prefixes=CHECK,NATIVE_HALF -DFNATTRS="noundef nofpclass(nan inf)" -DTARGET=dx
2+
// RUN: %clang_cc1 -std=hlsl202x -finclude-default-header -x hlsl -triple dxil-pc-shadermodel6.3-library %s -emit-llvm -o - | FileCheck %s --check-prefixes=CHECK,NO_HALF -DFNATTRS="noundef nofpclass(nan inf)" -DTARGET=dx
3+
// RUN: %clang_cc1 -std=hlsl202x -finclude-default-header -x hlsl -triple spirv-unknown-vulkan-compute %s -fnative-half-type -emit-llvm -o - | FileCheck %s --check-prefixes=CHECK,NATIVE_HALF -DFNATTRS="spir_func noundef nofpclass(nan inf)" -DTARGET=spv
4+
// RUN: %clang_cc1 -std=hlsl202x -finclude-default-header -x hlsl -triple spirv-unknown-vulkan-compute %s -emit-llvm -o - | FileCheck %s --check-prefixes=CHECK,NO_HALF -DFNATTRS="spir_func noundef nofpclass(nan inf)" -DTARGET=spv
35

46
// CHECK: define [[FNATTRS]] float @_Z16test_lerp_doubled(
57
// CHECK: [[CONV0:%.*]] = fptrunc {{.*}} double %{{.*}} to float
@@ -160,3 +162,20 @@ float3 test_lerp_uint64_t3(uint64_t3 p0) { return lerp(p0, p0, p0); }
160162
// CHECK: [[LERP:%.*]] = call {{.*}} <4 x float> @llvm.[[TARGET]].lerp.v4f32(<4 x float> [[CONV0]], <4 x float> [[CONV1]], <4 x float> [[CONV2]])
161163
// CHECK: ret <4 x float> [[LERP]]
162164
float4 test_lerp_uint64_t4(uint64_t4 p0) { return lerp(p0, p0, p0); }
165+
166+
// NATIVE_HALF: define [[FNATTRS]] <3 x [[TY:half]]> @_Z21test_lerp_half_scalarDv3_DhS_Dh{{.*}}(
167+
// NO_HALF: define [[FNATTRS]] <3 x [[TY:float]]> @_Z21test_lerp_half_scalarDv3_DhS_Dh(
168+
// CHECK: [[SPLATINSERT:%.*]] = insertelement <3 x [[TY]]> poison, [[TY]] %{{.*}}, i64 0
169+
// CHECK: [[SPLAT:%.*]] = shufflevector <3 x [[TY]]> [[SPLATINSERT]], <3 x [[TY]]> poison, <3 x i32> zeroinitializer
170+
// CHECK: [[LERP:%.*]] = call {{.*}} <3 x [[TY]]> @llvm.[[TARGET]].lerp.{{.*}}(<3 x [[TY]]> {{.*}}, <3 x [[TY]]> {{.*}}, <3 x [[TY]]> [[SPLAT]])
171+
// CHECK: ret <3 x [[TY]]> [[LERP]]
172+
half3 test_lerp_half_scalar(half3 x, half3 y, half s) { return lerp(x, y, s); }
173+
174+
// CHECK: define [[FNATTRS]] <3 x float> @_Z22test_lerp_float_scalarDv3_fS_f(
175+
// CHECK: [[SPLATINSERT:%.*]] = insertelement <3 x float> poison, float %{{.*}}, i64 0
176+
// CHECK: [[SPLAT:%.*]] = shufflevector <3 x float> [[SPLATINSERT]], <3 x float> poison, <3 x i32> zeroinitializer
177+
// CHECK: [[LERP:%.*]] = call {{.*}} <3 x float> @llvm.[[TARGET]].lerp.v3f32(<3 x float> {{.*}}, <3 x float> {{.*}}, <3 x float> [[SPLAT]])
178+
// CHECK: ret <3 x float> [[LERP]]
179+
float3 test_lerp_float_scalar(float3 x, float3 y, float s) {
180+
return lerp(x, y, s);
181+
}

clang/test/SemaHLSL/BuiltIns/lerp-errors.hlsl

+11-11
Original file line numberDiff line numberDiff line change
@@ -62,42 +62,42 @@ float2 test_lerp_element_type_mismatch(half2 p0, float2 p1) {
6262

6363
float2 test_builtin_lerp_float2_splat(float p0, float2 p1) {
6464
return __builtin_hlsl_lerp(p0, p1, p1);
65-
// expected-error@-1 {{all arguments to '__builtin_hlsl_lerp' must be vectors}}
65+
// expected-error@-1 {{all arguments to '__builtin_hlsl_lerp' must have the same type}}
6666
}
6767

6868
float2 test_builtin_lerp_float2_splat2(double p0, double2 p1) {
6969
return __builtin_hlsl_lerp(p1, p0, p1);
70-
// expected-error@-1 {{all arguments to '__builtin_hlsl_lerp' must be vectors}}
70+
// expected-error@-1 {{all arguments to '__builtin_hlsl_lerp' must have the same type}}
7171
}
7272

7373
float2 test_builtin_lerp_float2_splat3(double p0, double2 p1) {
7474
return __builtin_hlsl_lerp(p1, p1, p0);
75-
// expected-error@-1 {{all arguments to '__builtin_hlsl_lerp' must be vectors}}
75+
// expected-error@-1 {{all arguments to '__builtin_hlsl_lerp' must have the same type}}
7676
}
7777

7878
float3 test_builtin_lerp_float3_splat(float p0, float3 p1) {
7979
return __builtin_hlsl_lerp(p0, p1, p1);
80-
// expected-error@-1 {{all arguments to '__builtin_hlsl_lerp' must be vectors}}
80+
// expected-error@-1 {{all arguments to '__builtin_hlsl_lerp' must have the same type}}
8181
}
8282

8383
float4 test_builtin_lerp_float4_splat(float p0, float4 p1) {
8484
return __builtin_hlsl_lerp(p0, p1, p1);
85-
// expected-error@-1 {{all arguments to '__builtin_hlsl_lerp' must be vectors}}
85+
// expected-error@-1 {{all arguments to '__builtin_hlsl_lerp' must have the same type}}
8686
}
8787

8888
float2 test_lerp_float2_int_splat(float2 p0, int p1) {
8989
return __builtin_hlsl_lerp(p0, p1, p1);
90-
// expected-error@-1 {{all arguments to '__builtin_hlsl_lerp' must be vectors}}
90+
// expected-error@-1 {{all arguments to '__builtin_hlsl_lerp' must have the same type}}
9191
}
9292

9393
float3 test_lerp_float3_int_splat(float3 p0, int p1) {
9494
return __builtin_hlsl_lerp(p0, p1, p1);
95-
// expected-error@-1 {{all arguments to '__builtin_hlsl_lerp' must be vectors}}
95+
// expected-error@-1 {{all arguments to '__builtin_hlsl_lerp' must have the same type}}
9696
}
9797

9898
float2 test_builtin_lerp_int_vect_to_float_vec_promotion(int2 p0, float p1) {
9999
return __builtin_hlsl_lerp(p0, p1, p1);
100-
// expected-error@-1 {{all arguments to '__builtin_hlsl_lerp' must be vectors}}
100+
// expected-error@-1 {{all arguments to '__builtin_hlsl_lerp' must have the same type}}
101101
}
102102

103103
float test_builtin_lerp_bool_type_promotion(bool p0) {
@@ -107,17 +107,17 @@ float test_builtin_lerp_bool_type_promotion(bool p0) {
107107

108108
float builtin_bool_to_float_type_promotion(float p0, bool p1) {
109109
return __builtin_hlsl_lerp(p0, p0, p1);
110-
// expected-error@-1 {{3rd argument must be a scalar or vector of floating-point types (was 'bool')}}
110+
// expected-error@-1 {{all arguments to '__builtin_hlsl_lerp' must have the same type}}
111111
}
112112

113113
float builtin_bool_to_float_type_promotion2(bool p0, float p1) {
114114
return __builtin_hlsl_lerp(p1, p0, p1);
115-
// expected-error@-1 {{2nd argument must be a scalar or vector of floating-point types (was 'bool')}}
115+
// expected-error@-1 {{all arguments to '__builtin_hlsl_lerp' must have the same type}}
116116
}
117117

118118
float builtin_lerp_int_to_float_promotion(float p0, int p1) {
119119
return __builtin_hlsl_lerp(p0, p0, p1);
120-
// expected-error@-1 {{3rd argument must be a scalar or vector of floating-point types (was 'int')}}
120+
// expected-error@-1 {{all arguments to '__builtin_hlsl_lerp' must have the same type}}
121121
}
122122

123123
float4 test_lerp_int4(int4 p0, int4 p1, int4 p2) {

0 commit comments

Comments
 (0)