https://github.com/kmpeng updated https://github.com/llvm/llvm-project/pull/134171
>From 719bb94279f64f134c826faa22898e4e549bb23c Mon Sep 17 00:00:00 2001 From: kmpeng <kaitlinp...@microsoft.com> Date: Thu, 27 Mar 2025 14:39:27 -0700 Subject: [PATCH 1/8] finished lit implementation, added codegen and sema tests --- .../lib/Headers/hlsl/hlsl_intrinsic_helpers.h | 12 +++++ clang/lib/Headers/hlsl/hlsl_intrinsics.h | 31 ++++++++++++ clang/test/CodeGenHLSL/builtins/lit.hlsl | 36 +++++++++++++ clang/test/SemaHLSL/BuiltIns/lit-errors.hlsl | 50 +++++++++++++++++++ 4 files changed, 129 insertions(+) create mode 100644 clang/test/CodeGenHLSL/builtins/lit.hlsl create mode 100644 clang/test/SemaHLSL/BuiltIns/lit-errors.hlsl diff --git a/clang/lib/Headers/hlsl/hlsl_intrinsic_helpers.h b/clang/lib/Headers/hlsl/hlsl_intrinsic_helpers.h index 8cdd63d7e07bb..619d4c59f8074 100644 --- a/clang/lib/Headers/hlsl/hlsl_intrinsic_helpers.h +++ b/clang/lib/Headers/hlsl/hlsl_intrinsic_helpers.h @@ -101,6 +101,18 @@ constexpr vector<T, N> smoothstep_vec_impl(vector<T, N> Min, vector<T, N> Max, #endif } +template <typename T> +constexpr vector<T, 4> lit_impl(T N_dot_l, T N_dot_h, T M) { + bool Cond1 = N_dot_l < 0; + T ClampedP1 = select<T>(Cond1, 0, N_dot_l); + vector<T, 4> Result = {1, ClampedP1, 0, 1}; + bool CombinedCond = or (Cond1, (N_dot_h < 0)); + T LogP2 = log(N_dot_h); + T Exp = exp(LogP2 * M); + Result[2] = select<T>(CombinedCond, 0, Exp); + return Result; +} + } // namespace __detail } // namespace hlsl diff --git a/clang/lib/Headers/hlsl/hlsl_intrinsics.h b/clang/lib/Headers/hlsl/hlsl_intrinsics.h index fd799b8d874ae..5b6692d2a4281 100644 --- a/clang/lib/Headers/hlsl/hlsl_intrinsics.h +++ b/clang/lib/Headers/hlsl/hlsl_intrinsics.h @@ -253,6 +253,37 @@ const inline float length(__detail::HLSL_FIXED_VECTOR<float, N> X) { return __detail::length_vec_impl(X); } +//===----------------------------------------------------------------------===// +// lit builtins +//===----------------------------------------------------------------------===// + +/// \fn vector<T, 4> lit(T x, T y) +/// \brief Returns a lighting coefficient vector. +/// \param N_dot_l The dot product of the normalized surface normal and the +/// light vector. +/// \param N_dot_h The dot product of the half-angle vector and the surface +/// normal. +/// \param M A specular exponent. +/// +/// This function returns a lighting coefficient vector (ambient, diffuse, +/// specular, 1). + +template <typename T> +_HLSL_16BIT_AVAILABILITY(shadermodel, 6.2) +const inline __detail::enable_if_t<__detail::is_arithmetic<T>::Value && + __detail::is_same<half, T>::value, + vector<T, 4>> lit(T N_dot_l, T N_dot_h, T M) { + return __detail::lit_impl(N_dot_l, N_dot_h, M); +} + +template <typename T> +const inline __detail::enable_if_t<__detail::is_arithmetic<T>::Value && + __detail::is_same<float, T>::value, + vector<T, 4>> +lit(T N_dot_l, T N_dot_h, T M) { + return __detail::lit_impl(N_dot_l, N_dot_h, M); +} + //===----------------------------------------------------------------------===// // D3DCOLORtoUBYTE4 builtin //===----------------------------------------------------------------------===// diff --git a/clang/test/CodeGenHLSL/builtins/lit.hlsl b/clang/test/CodeGenHLSL/builtins/lit.hlsl new file mode 100644 index 0000000000000..3fb0a2c5b5d6f --- /dev/null +++ b/clang/test/CodeGenHLSL/builtins/lit.hlsl @@ -0,0 +1,36 @@ +// NOTE: Assertions have been autogenerated by utils/update_cc_test_checks.py UTC_ARGS: --version 5 +// RUN: %clang_cc1 -finclude-default-header -x hlsl -triple dxil-pc-shadermodel6.3-library %s -fnative-half-type -emit-llvm -O1 -o - | FileCheck %s + +// CHECK-LABEL: define noundef nofpclass(nan inf) <4 x half> @_Z13test_lit_halfDhDhDh( +// CHECK-SAME: half noundef nofpclass(nan inf) [[N_DOT_L:%.*]], half noundef nofpclass(nan inf) [[N_DOT_H:%.*]], half noundef nofpclass(nan inf) [[M:%.*]]) local_unnamed_addr #[[ATTR0:[0-9]+]] { +// CHECK-NEXT: [[ENTRY:.*:]] +// CHECK-NEXT: [[CMP_I:%.*]] = fcmp reassoc nnan ninf nsz arcp afn olt half [[N_DOT_L]], 0xH0000 +// CHECK-NEXT: [[HLSL_SELECT_I:%.*]] = tail call reassoc nnan ninf nsz arcp afn half @llvm.maxnum.f16(half [[N_DOT_L]], half 0xH0000) +// CHECK-NEXT: [[VECINIT2_I:%.*]] = insertelement <4 x half> <half 0xH3C00, half poison, half poison, half 0xH3C00>, half [[HLSL_SELECT_I]], i64 1 +// CHECK-NEXT: [[CMP4_I:%.*]] = fcmp reassoc nnan ninf nsz arcp afn olt half [[N_DOT_H]], 0xH0000 +// CHECK-NEXT: [[HLSL_OR_I:%.*]] = or i1 [[CMP_I]], [[CMP4_I]] +// CHECK-NEXT: [[ELT_LOG_I:%.*]] = tail call reassoc nnan ninf nsz arcp afn half @llvm.log.f16(half [[N_DOT_H]]) +// CHECK-NEXT: [[MUL_I:%.*]] = fmul reassoc nnan ninf nsz arcp afn half [[ELT_LOG_I]], [[M]] +// CHECK-NEXT: [[ELT_EXP_I:%.*]] = tail call reassoc nnan ninf nsz arcp afn half @llvm.exp.f16(half [[MUL_I]]) +// CHECK-NEXT: [[HLSL_SELECT7_I:%.*]] = select reassoc nnan ninf nsz arcp afn i1 [[HLSL_OR_I]], half 0xH0000, half [[ELT_EXP_I]] +// CHECK-NEXT: [[VECINS_I:%.*]] = insertelement <4 x half> [[VECINIT2_I]], half [[HLSL_SELECT7_I]], i64 2 +// CHECK-NEXT: ret <4 x half> [[VECINS_I]] +// +half4 test_lit_half(half N_dot_l, half N_dot_h, half M) { return lit(N_dot_l, N_dot_h, M); } + +// CHECK-LABEL: define noundef nofpclass(nan inf) <4 x float> @_Z14test_lit_floatfff( +// CHECK-SAME: float noundef nofpclass(nan inf) [[N_DOT_L:%.*]], float noundef nofpclass(nan inf) [[N_DOT_H:%.*]], float noundef nofpclass(nan inf) [[M:%.*]]) local_unnamed_addr #[[ATTR0]] { +// CHECK-NEXT: [[ENTRY:.*:]] +// CHECK-NEXT: [[CMP_I:%.*]] = fcmp reassoc nnan ninf nsz arcp afn olt float [[N_DOT_L]], 0.000000e+00 +// CHECK-NEXT: [[HLSL_SELECT_I:%.*]] = tail call reassoc nnan ninf nsz arcp afn float @llvm.maxnum.f32(float [[N_DOT_L]], float 0.000000e+00) +// CHECK-NEXT: [[VECINIT2_I:%.*]] = insertelement <4 x float> <float 1.000000e+00, float poison, float poison, float 1.000000e+00>, float [[HLSL_SELECT_I]], i64 1 +// CHECK-NEXT: [[CMP4_I:%.*]] = fcmp reassoc nnan ninf nsz arcp afn olt float [[N_DOT_H]], 0.000000e+00 +// CHECK-NEXT: [[HLSL_OR_I:%.*]] = or i1 [[CMP_I]], [[CMP4_I]] +// CHECK-NEXT: [[ELT_LOG_I:%.*]] = tail call reassoc nnan ninf nsz arcp afn float @llvm.log.f32(float [[N_DOT_H]]) +// CHECK-NEXT: [[MUL_I:%.*]] = fmul reassoc nnan ninf nsz arcp afn float [[ELT_LOG_I]], [[M]] +// CHECK-NEXT: [[ELT_EXP_I:%.*]] = tail call reassoc nnan ninf nsz arcp afn float @llvm.exp.f32(float [[MUL_I]]) +// CHECK-NEXT: [[HLSL_SELECT7_I:%.*]] = select reassoc nnan ninf nsz arcp afn i1 [[HLSL_OR_I]], float 0.000000e+00, float [[ELT_EXP_I]] +// CHECK-NEXT: [[VECINS_I:%.*]] = insertelement <4 x float> [[VECINIT2_I]], float [[HLSL_SELECT7_I]], i64 2 +// CHECK-NEXT: ret <4 x float> [[VECINS_I]] +// +float4 test_lit_float(float N_dot_l, float N_dot_h, float M) { return lit(N_dot_l, N_dot_h, M); } diff --git a/clang/test/SemaHLSL/BuiltIns/lit-errors.hlsl b/clang/test/SemaHLSL/BuiltIns/lit-errors.hlsl new file mode 100644 index 0000000000000..33fb33ca3204d --- /dev/null +++ b/clang/test/SemaHLSL/BuiltIns/lit-errors.hlsl @@ -0,0 +1,50 @@ +// RUN: %clang_cc1 -finclude-default-header -x hlsl -triple dxil-pc-shadermodel6.6-library %s -fnative-half-type -emit-llvm-only -disable-llvm-passes -verify + +float4 test_no_second_arg(float p0) { + return lit(p0); + // expected-error@-1 {{no matching function for call to 'lit'}} + // expected-note@hlsl/hlsl_intrinsics.h:* {{candidate function template not viable: requires 3 arguments, but 1 was provided}} + // expected-note@hlsl/hlsl_intrinsics.h:* {{candidate function template not viable: requires 3 arguments, but 1 was provided}} +} + +float4 test_no_third_arg(float p0) { + return lit(p0, p0); + // expected-error@-1 {{no matching function for call to 'lit'}} + // expected-note@hlsl/hlsl_intrinsics.h:* {{candidate function template not viable: requires 3 arguments, but 2 were provided}} + // expected-note@hlsl/hlsl_intrinsics.h:* {{candidate function template not viable: requires 3 arguments, but 2 were provided}} +} + +float4 test_too_many_arg(float p0) { + return lit(p0, p0, p0, p0); + // expected-error@-1 {{no matching function for call to 'lit'}} + // expected-note@hlsl/hlsl_intrinsics.h:* {{candidate function template not viable: requires 3 arguments, but 4 were provided}} + // expected-note@hlsl/hlsl_intrinsics.h:* {{candidate function template not viable: requires 3 arguments, but 4 were provided}} +} + +float4 test_double_inputs(double p0, double p1, double p2) { + return lit(p0, p1, p2); + // expected-error@-1 {{no matching function for call to 'lit'}} + // expected-note@hlsl/hlsl_intrinsics.h:* {{candidate template ignored: substitution failure [with T = double]: no type named 'Type' in 'hlsl::__detail::enable_if<false, vector<double, 4>>'}} + // expected-note@hlsl/hlsl_intrinsics.h:* {{candidate template ignored: substitution failure [with T = double]: no type named 'Type' in 'hlsl::__detail::enable_if<false, vector<double, 4>>'}} +} + +float4 test_int_inputs(int p0, int p1, int p2) { + return lit(p0, p1, p2); + // expected-error@-1 {{no matching function for call to 'lit'}} + // expected-note@hlsl/hlsl_intrinsics.h:* {{candidate template ignored: substitution failure [with T = int]: no type named 'Type' in 'hlsl::__detail::enable_if<false, vector<int, 4>>'}} + // expected-note@hlsl/hlsl_intrinsics.h:* {{candidate template ignored: substitution failure [with T = int]: no type named 'Type' in 'hlsl::__detail::enable_if<false, vector<int, 4>>'}} +} + +float4 test_vec_inputs(float2 p0, float2 p1, float2 p2) { + return lit(p0, p1, p2); + // expected-error@-1 {{no matching function for call to 'lit'}} + // expected-note@hlsl/hlsl_intrinsics.h:* {{candidate template ignored: substitution failure [with T = float2]: invalid vector element type 'vector<float, 2>' (vector of 2 'float' values)}} + // expected-note@hlsl/hlsl_intrinsics.h:* {{candidate template ignored: substitution failure [with T = float2]: invalid vector element type 'vector<float, 2>' (vector of 2 'float' values)}} +} + +float4 test_vec1_inputs(float1 p0, float1 p1, float1 p2) { + return lit(p0, p1, p2); + // expected-error@-1 {{no matching function for call to 'lit'}} + // expected-note@hlsl/hlsl_intrinsics.h:* {{candidate template ignored: substitution failure [with T = float1]: invalid vector element type 'vector<float, 1>' (vector of 1 'float' value)}} + // expected-note@hlsl/hlsl_intrinsics.h:* {{candidate template ignored: substitution failure [with T = float1]: invalid vector element type 'vector<float, 1>' (vector of 1 'float' value)}} +} \ No newline at end of file >From f133aecdfe1f09006159e5c73c220ea457c03ee6 Mon Sep 17 00:00:00 2001 From: kmpeng <kaitlinp...@microsoft.com> Date: Mon, 31 Mar 2025 12:39:39 -0700 Subject: [PATCH 2/8] rename variables --- .../lib/Headers/hlsl/hlsl_intrinsic_helpers.h | 16 ++++++------- clang/lib/Headers/hlsl/hlsl_intrinsics.h | 16 ++++++------- clang/test/CodeGenHLSL/builtins/lit.hlsl | 24 +++++++++---------- 3 files changed, 27 insertions(+), 29 deletions(-) diff --git a/clang/lib/Headers/hlsl/hlsl_intrinsic_helpers.h b/clang/lib/Headers/hlsl/hlsl_intrinsic_helpers.h index 619d4c59f8074..3a41635a89aa4 100644 --- a/clang/lib/Headers/hlsl/hlsl_intrinsic_helpers.h +++ b/clang/lib/Headers/hlsl/hlsl_intrinsic_helpers.h @@ -101,15 +101,13 @@ constexpr vector<T, N> smoothstep_vec_impl(vector<T, N> Min, vector<T, N> Max, #endif } -template <typename T> -constexpr vector<T, 4> lit_impl(T N_dot_l, T N_dot_h, T M) { - bool Cond1 = N_dot_l < 0; - T ClampedP1 = select<T>(Cond1, 0, N_dot_l); - vector<T, 4> Result = {1, ClampedP1, 0, 1}; - bool CombinedCond = or (Cond1, (N_dot_h < 0)); - T LogP2 = log(N_dot_h); - T Exp = exp(LogP2 * M); - Result[2] = select<T>(CombinedCond, 0, Exp); +template <typename T> constexpr vector<T, 4> lit_impl(T NDotL, T NDotH, T M) { + bool DiffuseCond = NDotL < 0; + T Diffuse = select<T>(DiffuseCond, 0, NDotL); + vector<T, 4> Result = {1, Diffuse, 0, 1}; + bool SpecularCond = or (DiffuseCond, (NDotH < 0)); + T SpecularExp = exp(log(NDotH) * M); + Result[2] = select<T>(SpecularCond, 0, SpecularExp); return Result; } diff --git a/clang/lib/Headers/hlsl/hlsl_intrinsics.h b/clang/lib/Headers/hlsl/hlsl_intrinsics.h index 5b6692d2a4281..5d23eb89b889b 100644 --- a/clang/lib/Headers/hlsl/hlsl_intrinsics.h +++ b/clang/lib/Headers/hlsl/hlsl_intrinsics.h @@ -257,11 +257,11 @@ const inline float length(__detail::HLSL_FIXED_VECTOR<float, N> X) { // lit builtins //===----------------------------------------------------------------------===// -/// \fn vector<T, 4> lit(T x, T y) +/// \fn vector<T, 4> lit(T NDotL, T NDotH, T M) /// \brief Returns a lighting coefficient vector. -/// \param N_dot_l The dot product of the normalized surface normal and the +/// \param NDotL The dot product of the normalized surface normal and the /// light vector. -/// \param N_dot_h The dot product of the half-angle vector and the surface +/// \param NDotH The dot product of the half-angle vector and the surface /// normal. /// \param M A specular exponent. /// @@ -271,17 +271,17 @@ const inline float length(__detail::HLSL_FIXED_VECTOR<float, N> X) { template <typename T> _HLSL_16BIT_AVAILABILITY(shadermodel, 6.2) const inline __detail::enable_if_t<__detail::is_arithmetic<T>::Value && - __detail::is_same<half, T>::value, - vector<T, 4>> lit(T N_dot_l, T N_dot_h, T M) { - return __detail::lit_impl(N_dot_l, N_dot_h, M); + __detail::is_same<half, T>::value, + vector<T, 4>> lit(T NDotL, T NDotH, T M) { + return __detail::lit_impl(NDotL, NDotH, M); } template <typename T> const inline __detail::enable_if_t<__detail::is_arithmetic<T>::Value && __detail::is_same<float, T>::value, vector<T, 4>> -lit(T N_dot_l, T N_dot_h, T M) { - return __detail::lit_impl(N_dot_l, N_dot_h, M); +lit(T NDotL, T NDotH, T M) { + return __detail::lit_impl(NDotL, NDotH, M); } //===----------------------------------------------------------------------===// diff --git a/clang/test/CodeGenHLSL/builtins/lit.hlsl b/clang/test/CodeGenHLSL/builtins/lit.hlsl index 3fb0a2c5b5d6f..484a85779a375 100644 --- a/clang/test/CodeGenHLSL/builtins/lit.hlsl +++ b/clang/test/CodeGenHLSL/builtins/lit.hlsl @@ -2,35 +2,35 @@ // RUN: %clang_cc1 -finclude-default-header -x hlsl -triple dxil-pc-shadermodel6.3-library %s -fnative-half-type -emit-llvm -O1 -o - | FileCheck %s // CHECK-LABEL: define noundef nofpclass(nan inf) <4 x half> @_Z13test_lit_halfDhDhDh( -// CHECK-SAME: half noundef nofpclass(nan inf) [[N_DOT_L:%.*]], half noundef nofpclass(nan inf) [[N_DOT_H:%.*]], half noundef nofpclass(nan inf) [[M:%.*]]) local_unnamed_addr #[[ATTR0:[0-9]+]] { +// CHECK-SAME: half noundef nofpclass(nan inf) [[NDOTL:%.*]], half noundef nofpclass(nan inf) [[NDOTH:%.*]], half noundef nofpclass(nan inf) [[M:%.*]]) local_unnamed_addr #[[ATTR0:[0-9]+]] { // CHECK-NEXT: [[ENTRY:.*:]] -// CHECK-NEXT: [[CMP_I:%.*]] = fcmp reassoc nnan ninf nsz arcp afn olt half [[N_DOT_L]], 0xH0000 -// CHECK-NEXT: [[HLSL_SELECT_I:%.*]] = tail call reassoc nnan ninf nsz arcp afn half @llvm.maxnum.f16(half [[N_DOT_L]], half 0xH0000) +// CHECK-NEXT: [[CMP_I:%.*]] = fcmp reassoc nnan ninf nsz arcp afn olt half [[NDOTL]], 0xH0000 +// CHECK-NEXT: [[HLSL_SELECT_I:%.*]] = tail call reassoc nnan ninf nsz arcp afn half @llvm.maxnum.f16(half [[NDOTL]], half 0xH0000) // CHECK-NEXT: [[VECINIT2_I:%.*]] = insertelement <4 x half> <half 0xH3C00, half poison, half poison, half 0xH3C00>, half [[HLSL_SELECT_I]], i64 1 -// CHECK-NEXT: [[CMP4_I:%.*]] = fcmp reassoc nnan ninf nsz arcp afn olt half [[N_DOT_H]], 0xH0000 +// CHECK-NEXT: [[CMP4_I:%.*]] = fcmp reassoc nnan ninf nsz arcp afn olt half [[NDOTH]], 0xH0000 // CHECK-NEXT: [[HLSL_OR_I:%.*]] = or i1 [[CMP_I]], [[CMP4_I]] -// CHECK-NEXT: [[ELT_LOG_I:%.*]] = tail call reassoc nnan ninf nsz arcp afn half @llvm.log.f16(half [[N_DOT_H]]) +// CHECK-NEXT: [[ELT_LOG_I:%.*]] = tail call reassoc nnan ninf nsz arcp afn half @llvm.log.f16(half [[NDOTH]]) // CHECK-NEXT: [[MUL_I:%.*]] = fmul reassoc nnan ninf nsz arcp afn half [[ELT_LOG_I]], [[M]] // CHECK-NEXT: [[ELT_EXP_I:%.*]] = tail call reassoc nnan ninf nsz arcp afn half @llvm.exp.f16(half [[MUL_I]]) // CHECK-NEXT: [[HLSL_SELECT7_I:%.*]] = select reassoc nnan ninf nsz arcp afn i1 [[HLSL_OR_I]], half 0xH0000, half [[ELT_EXP_I]] // CHECK-NEXT: [[VECINS_I:%.*]] = insertelement <4 x half> [[VECINIT2_I]], half [[HLSL_SELECT7_I]], i64 2 // CHECK-NEXT: ret <4 x half> [[VECINS_I]] // -half4 test_lit_half(half N_dot_l, half N_dot_h, half M) { return lit(N_dot_l, N_dot_h, M); } +half4 test_lit_half(half NDotL, half NDotH, half M) { return lit(NDotL, NDotH, M); } // CHECK-LABEL: define noundef nofpclass(nan inf) <4 x float> @_Z14test_lit_floatfff( -// CHECK-SAME: float noundef nofpclass(nan inf) [[N_DOT_L:%.*]], float noundef nofpclass(nan inf) [[N_DOT_H:%.*]], float noundef nofpclass(nan inf) [[M:%.*]]) local_unnamed_addr #[[ATTR0]] { +// CHECK-SAME: float noundef nofpclass(nan inf) [[NDOTL:%.*]], float noundef nofpclass(nan inf) [[NDOTH:%.*]], float noundef nofpclass(nan inf) [[M:%.*]]) local_unnamed_addr #[[ATTR0]] { // CHECK-NEXT: [[ENTRY:.*:]] -// CHECK-NEXT: [[CMP_I:%.*]] = fcmp reassoc nnan ninf nsz arcp afn olt float [[N_DOT_L]], 0.000000e+00 -// CHECK-NEXT: [[HLSL_SELECT_I:%.*]] = tail call reassoc nnan ninf nsz arcp afn float @llvm.maxnum.f32(float [[N_DOT_L]], float 0.000000e+00) +// CHECK-NEXT: [[CMP_I:%.*]] = fcmp reassoc nnan ninf nsz arcp afn olt float [[NDOTL]], 0.000000e+00 +// CHECK-NEXT: [[HLSL_SELECT_I:%.*]] = tail call reassoc nnan ninf nsz arcp afn float @llvm.maxnum.f32(float [[NDOTL]], float 0.000000e+00) // CHECK-NEXT: [[VECINIT2_I:%.*]] = insertelement <4 x float> <float 1.000000e+00, float poison, float poison, float 1.000000e+00>, float [[HLSL_SELECT_I]], i64 1 -// CHECK-NEXT: [[CMP4_I:%.*]] = fcmp reassoc nnan ninf nsz arcp afn olt float [[N_DOT_H]], 0.000000e+00 +// CHECK-NEXT: [[CMP4_I:%.*]] = fcmp reassoc nnan ninf nsz arcp afn olt float [[NDOTH]], 0.000000e+00 // CHECK-NEXT: [[HLSL_OR_I:%.*]] = or i1 [[CMP_I]], [[CMP4_I]] -// CHECK-NEXT: [[ELT_LOG_I:%.*]] = tail call reassoc nnan ninf nsz arcp afn float @llvm.log.f32(float [[N_DOT_H]]) +// CHECK-NEXT: [[ELT_LOG_I:%.*]] = tail call reassoc nnan ninf nsz arcp afn float @llvm.log.f32(float [[NDOTH]]) // CHECK-NEXT: [[MUL_I:%.*]] = fmul reassoc nnan ninf nsz arcp afn float [[ELT_LOG_I]], [[M]] // CHECK-NEXT: [[ELT_EXP_I:%.*]] = tail call reassoc nnan ninf nsz arcp afn float @llvm.exp.f32(float [[MUL_I]]) // CHECK-NEXT: [[HLSL_SELECT7_I:%.*]] = select reassoc nnan ninf nsz arcp afn i1 [[HLSL_OR_I]], float 0.000000e+00, float [[ELT_EXP_I]] // CHECK-NEXT: [[VECINS_I:%.*]] = insertelement <4 x float> [[VECINIT2_I]], float [[HLSL_SELECT7_I]], i64 2 // CHECK-NEXT: ret <4 x float> [[VECINS_I]] // -float4 test_lit_float(float N_dot_l, float N_dot_h, float M) { return lit(N_dot_l, N_dot_h, M); } +float4 test_lit_float(float NDotL, float NDotH, float M) { return lit(NDotL, NDotH, M); } >From 02306a62daf9eeef8ad8d6ce56e1f80e37592fb2 Mon Sep 17 00:00:00 2001 From: kmpeng <kaitlinp...@microsoft.com> Date: Tue, 1 Apr 2025 12:17:15 -0700 Subject: [PATCH 3/8] WIP accept double/int inputs and downcast to floats --- .../lib/Headers/hlsl/hlsl_compat_overloads.h | 9 ++++++++ .../CodeGenHLSL/builtins/lit-overloads.hlsl | 23 +++++++++++++++++++ clang/test/SemaHLSL/BuiltIns/lit-errors.hlsl | 14 ----------- 3 files changed, 32 insertions(+), 14 deletions(-) create mode 100644 clang/test/CodeGenHLSL/builtins/lit-overloads.hlsl diff --git a/clang/lib/Headers/hlsl/hlsl_compat_overloads.h b/clang/lib/Headers/hlsl/hlsl_compat_overloads.h index 47ae34adfe541..47c4a15b6fef9 100644 --- a/clang/lib/Headers/hlsl/hlsl_compat_overloads.h +++ b/clang/lib/Headers/hlsl/hlsl_compat_overloads.h @@ -280,6 +280,15 @@ constexpr bool4 isinf(double4 V) { return isinf((float4)V); } _DXC_COMPAT_TERNARY_DOUBLE_OVERLOADS(lerp) _DXC_COMPAT_TERNARY_INTEGER_OVERLOADS(lerp) +//===----------------------------------------------------------------------===// +// lit builtins overloads +//===----------------------------------------------------------------------===// +constexpr float4 lit(double V1, double V2, double V3) { return lit((float)V1, (float)V2, (float)V3); } +constexpr float4 lit(int V1, int V2, int V3) { return lit((float)V1, (float)V2, (float)V3); } +constexpr float4 lit(uint V1, uint V2, uint V3) { return lit((float)V1, (float)V2, (float)V3); } +constexpr float4 lit(int64_t V1, int64_t V2, int64_t V3) { return lit((float)V1, (float)V2, (float)V3); } +constexpr float4 lit(uint64_t V1, uint64_t V2, uint64_t V3) { return lit((float)V1, (float)V2, (float)V3); } + //===----------------------------------------------------------------------===// // log builtins overloads //===----------------------------------------------------------------------===// diff --git a/clang/test/CodeGenHLSL/builtins/lit-overloads.hlsl b/clang/test/CodeGenHLSL/builtins/lit-overloads.hlsl new file mode 100644 index 0000000000000..9d2bca2efd05c --- /dev/null +++ b/clang/test/CodeGenHLSL/builtins/lit-overloads.hlsl @@ -0,0 +1,23 @@ +// RUN: %clang_cc1 -std=hlsl202x -finclude-default-header -x hlsl -triple dxil-pc-shadermodel6.3-library %s \ +// RUN: -emit-llvm -disable-llvm-passes -o - | \ +// RUN: FileCheck %s --check-prefixes=CHECK + +// CHECK-LABEL: define noundef nofpclass(nan inf) float {{.*}}test_floor_double +// CHECK: call reassoc nnan ninf nsz arcp afn float @llvm.floor.f32( +float4 test_lit_double(double NDotL, double NDotH, double M) { return lit(NDotL, NDotH, M); } + +// CHECK-LABEL: define noundef nofpclass(nan inf) float {{.*}}test_floor_int +// CHECK: call reassoc nnan ninf nsz arcp afn float @llvm.floor.f32( +float4 test_lit_int(int NDotL, int NDotH, int M) { return lit(NDotL, NDotH, M); } + +// CHECK-LABEL: define noundef nofpclass(nan inf) float {{.*}}test_floor_uint +// CHECK: call reassoc nnan ninf nsz arcp afn float @llvm.floor.f32( +float4 test_lit_uint(uint NDotL, uint NDotH, uint M) { return lit(NDotL, NDotH, M); } + +// CHECK-LABEL: define noundef nofpclass(nan inf) float {{.*}}test_floor_int64_t +// CHECK: call reassoc nnan ninf nsz arcp afn float @llvm.floor.f32( +float4 test_lit_int64_t(int64_t NDotL, int64_t NDotH, int64_t M) { return lit(NDotL, NDotH, M); } + +// CHECK-LABEL: define noundef nofpclass(nan inf) float {{.*}}test_floor_uint64_t +// CHECK: call reassoc nnan ninf nsz arcp afn float @llvm.floor.f32( +float4 test_lit_uint64_t(uint64_t NDotL, uint64_t NDotH, uint64_t M) { return lit(NDotL, NDotH, M); } diff --git a/clang/test/SemaHLSL/BuiltIns/lit-errors.hlsl b/clang/test/SemaHLSL/BuiltIns/lit-errors.hlsl index 33fb33ca3204d..de93d1d0a64ad 100644 --- a/clang/test/SemaHLSL/BuiltIns/lit-errors.hlsl +++ b/clang/test/SemaHLSL/BuiltIns/lit-errors.hlsl @@ -21,20 +21,6 @@ float4 test_too_many_arg(float p0) { // expected-note@hlsl/hlsl_intrinsics.h:* {{candidate function template not viable: requires 3 arguments, but 4 were provided}} } -float4 test_double_inputs(double p0, double p1, double p2) { - return lit(p0, p1, p2); - // expected-error@-1 {{no matching function for call to 'lit'}} - // expected-note@hlsl/hlsl_intrinsics.h:* {{candidate template ignored: substitution failure [with T = double]: no type named 'Type' in 'hlsl::__detail::enable_if<false, vector<double, 4>>'}} - // expected-note@hlsl/hlsl_intrinsics.h:* {{candidate template ignored: substitution failure [with T = double]: no type named 'Type' in 'hlsl::__detail::enable_if<false, vector<double, 4>>'}} -} - -float4 test_int_inputs(int p0, int p1, int p2) { - return lit(p0, p1, p2); - // expected-error@-1 {{no matching function for call to 'lit'}} - // expected-note@hlsl/hlsl_intrinsics.h:* {{candidate template ignored: substitution failure [with T = int]: no type named 'Type' in 'hlsl::__detail::enable_if<false, vector<int, 4>>'}} - // expected-note@hlsl/hlsl_intrinsics.h:* {{candidate template ignored: substitution failure [with T = int]: no type named 'Type' in 'hlsl::__detail::enable_if<false, vector<int, 4>>'}} -} - float4 test_vec_inputs(float2 p0, float2 p1, float2 p2) { return lit(p0, p1, p2); // expected-error@-1 {{no matching function for call to 'lit'}} >From 4bd1ab874cec8d0ae6c6f856bb418a8d0b22e25b Mon Sep 17 00:00:00 2001 From: kmpeng <kaitlinp...@microsoft.com> Date: Tue, 1 Apr 2025 15:28:11 -0700 Subject: [PATCH 4/8] WIP double/int overloads --- .../lib/Headers/hlsl/hlsl_compat_overloads.h | 24 +++++++++--- .../CodeGenHLSL/builtins/lit-overloads.hlsl | 20 +++++----- clang/test/SemaHLSL/BuiltIns/lit-errors.hlsl | 39 +++++++++++++------ 3 files changed, 56 insertions(+), 27 deletions(-) diff --git a/clang/lib/Headers/hlsl/hlsl_compat_overloads.h b/clang/lib/Headers/hlsl/hlsl_compat_overloads.h index 47c4a15b6fef9..86e81123caffc 100644 --- a/clang/lib/Headers/hlsl/hlsl_compat_overloads.h +++ b/clang/lib/Headers/hlsl/hlsl_compat_overloads.h @@ -9,6 +9,8 @@ #ifndef _HLSL_COMPAT_OVERLOADS_H_ #define _HLSl_COMPAT_OVERLOADS_H_ +#include "hlsl/hlsl_intrinsic_helpers.h" + namespace hlsl { // Note: Functions in this file are sorted alphabetically, then grouped by base @@ -283,11 +285,23 @@ _DXC_COMPAT_TERNARY_INTEGER_OVERLOADS(lerp) //===----------------------------------------------------------------------===// // lit builtins overloads //===----------------------------------------------------------------------===// -constexpr float4 lit(double V1, double V2, double V3) { return lit((float)V1, (float)V2, (float)V3); } -constexpr float4 lit(int V1, int V2, int V3) { return lit((float)V1, (float)V2, (float)V3); } -constexpr float4 lit(uint V1, uint V2, uint V3) { return lit((float)V1, (float)V2, (float)V3); } -constexpr float4 lit(int64_t V1, int64_t V2, int64_t V3) { return lit((float)V1, (float)V2, (float)V3); } -constexpr float4 lit(uint64_t V1, uint64_t V2, uint64_t V3) { return lit((float)V1, (float)V2, (float)V3); } + +// Note: calling lit_impl because calling lit directly causes infinite recursion +constexpr float4 lit(double V1, double V2, double V3) { + return __detail::lit_impl((float)V1, (float)V2, (float)V3); +} +constexpr float4 lit(int V1, int V2, int V3) { + return __detail::lit_impl((float)V1, (float)V2, (float)V3); +} +constexpr float4 lit(uint V1, uint V2, uint V3) { + return __detail::lit_impl((float)V1, (float)V2, (float)V3); +} +constexpr float4 lit(int64_t V1, int64_t V2, int64_t V3) { + return __detail::lit_impl((float)V1, (float)V2, (float)V3); +} +constexpr float4 lit(uint64_t V1, uint64_t V2, uint64_t V3) { + return __detail::lit_impl((float)V1, (float)V2, (float)V3); +} //===----------------------------------------------------------------------===// // log builtins overloads diff --git a/clang/test/CodeGenHLSL/builtins/lit-overloads.hlsl b/clang/test/CodeGenHLSL/builtins/lit-overloads.hlsl index 9d2bca2efd05c..8a9b31bf6ae8b 100644 --- a/clang/test/CodeGenHLSL/builtins/lit-overloads.hlsl +++ b/clang/test/CodeGenHLSL/builtins/lit-overloads.hlsl @@ -2,22 +2,22 @@ // RUN: -emit-llvm -disable-llvm-passes -o - | \ // RUN: FileCheck %s --check-prefixes=CHECK -// CHECK-LABEL: define noundef nofpclass(nan inf) float {{.*}}test_floor_double -// CHECK: call reassoc nnan ninf nsz arcp afn float @llvm.floor.f32( +// CHECK-LABEL: define linkonce_odr noundef nofpclass(nan inf) <4 x float> @_ZN4hlsl3litEddd( +// CHECK: call reassoc nnan ninf nsz arcp afn noundef nofpclass(nan inf) <4 x float> @_ZN4hlsl8__detail8lit_implIfEEDv4_T_S2_S2_S2_( float4 test_lit_double(double NDotL, double NDotH, double M) { return lit(NDotL, NDotH, M); } -// CHECK-LABEL: define noundef nofpclass(nan inf) float {{.*}}test_floor_int -// CHECK: call reassoc nnan ninf nsz arcp afn float @llvm.floor.f32( +// CHECK-LABEL: define linkonce_odr noundef nofpclass(nan inf) <4 x float> @_ZN4hlsl3litEiii( +// CHECK: call reassoc nnan ninf nsz arcp afn noundef nofpclass(nan inf) <4 x float> @_ZN4hlsl8__detail8lit_implIfEEDv4_T_S2_S2_S2_( float4 test_lit_int(int NDotL, int NDotH, int M) { return lit(NDotL, NDotH, M); } -// CHECK-LABEL: define noundef nofpclass(nan inf) float {{.*}}test_floor_uint -// CHECK: call reassoc nnan ninf nsz arcp afn float @llvm.floor.f32( +// CHECK-LABEL: define linkonce_odr noundef nofpclass(nan inf) <4 x float> @_ZN4hlsl3litEjjj( +// CHECK: call reassoc nnan ninf nsz arcp afn noundef nofpclass(nan inf) <4 x float> @_ZN4hlsl8__detail8lit_implIfEEDv4_T_S2_S2_S2_( float4 test_lit_uint(uint NDotL, uint NDotH, uint M) { return lit(NDotL, NDotH, M); } -// CHECK-LABEL: define noundef nofpclass(nan inf) float {{.*}}test_floor_int64_t -// CHECK: call reassoc nnan ninf nsz arcp afn float @llvm.floor.f32( +// CHECK-LABEL: define linkonce_odr noundef nofpclass(nan inf) <4 x float> @_ZN4hlsl3litElll( +// CHECK: call reassoc nnan ninf nsz arcp afn noundef nofpclass(nan inf) <4 x float> @_ZN4hlsl8__detail8lit_implIfEEDv4_T_S2_S2_S2_( float4 test_lit_int64_t(int64_t NDotL, int64_t NDotH, int64_t M) { return lit(NDotL, NDotH, M); } -// CHECK-LABEL: define noundef nofpclass(nan inf) float {{.*}}test_floor_uint64_t -// CHECK: call reassoc nnan ninf nsz arcp afn float @llvm.floor.f32( +// CHECK-LABEL: define linkonce_odr noundef nofpclass(nan inf) <4 x float> @_ZN4hlsl3litEmmm( +// CHECK: call reassoc nnan ninf nsz arcp afn noundef nofpclass(nan inf) <4 x float> @_ZN4hlsl8__detail8lit_implIfEEDv4_T_S2_S2_S2_( float4 test_lit_uint64_t(uint64_t NDotL, uint64_t NDotH, uint64_t M) { return lit(NDotL, NDotH, M); } diff --git a/clang/test/SemaHLSL/BuiltIns/lit-errors.hlsl b/clang/test/SemaHLSL/BuiltIns/lit-errors.hlsl index de93d1d0a64ad..b14d3e840fa0e 100644 --- a/clang/test/SemaHLSL/BuiltIns/lit-errors.hlsl +++ b/clang/test/SemaHLSL/BuiltIns/lit-errors.hlsl @@ -3,6 +3,11 @@ float4 test_no_second_arg(float p0) { return lit(p0); // expected-error@-1 {{no matching function for call to 'lit'}} + // expected-note@hlsl/hlsl_compat_overloads.h:* {{candidate function not viable: requires 3 arguments, but 1 was provided}} + // expected-note@hlsl/hlsl_compat_overloads.h:* {{candidate function not viable: requires 3 arguments, but 1 was provided}} + // expected-note@hlsl/hlsl_compat_overloads.h:* {{candidate function not viable: requires 3 arguments, but 1 was provided}} + // expected-note@hlsl/hlsl_compat_overloads.h:* {{candidate function not viable: requires 3 arguments, but 1 was provided}} + // expected-note@hlsl/hlsl_compat_overloads.h:* {{candidate function not viable: requires 3 arguments, but 1 was provided}} // expected-note@hlsl/hlsl_intrinsics.h:* {{candidate function template not viable: requires 3 arguments, but 1 was provided}} // expected-note@hlsl/hlsl_intrinsics.h:* {{candidate function template not viable: requires 3 arguments, but 1 was provided}} } @@ -10,6 +15,11 @@ float4 test_no_second_arg(float p0) { float4 test_no_third_arg(float p0) { return lit(p0, p0); // expected-error@-1 {{no matching function for call to 'lit'}} + // expected-note@hlsl/hlsl_compat_overloads.h:* {{candidate function not viable: requires 3 arguments, but 2 were provided}} + // expected-note@hlsl/hlsl_compat_overloads.h:* {{candidate function not viable: requires 3 arguments, but 2 were provided}} + // expected-note@hlsl/hlsl_compat_overloads.h:* {{candidate function not viable: requires 3 arguments, but 2 were provided}} + // expected-note@hlsl/hlsl_compat_overloads.h:* {{candidate function not viable: requires 3 arguments, but 2 were provided}} + // expected-note@hlsl/hlsl_compat_overloads.h:* {{candidate function not viable: requires 3 arguments, but 2 were provided}} // expected-note@hlsl/hlsl_intrinsics.h:* {{candidate function template not viable: requires 3 arguments, but 2 were provided}} // expected-note@hlsl/hlsl_intrinsics.h:* {{candidate function template not viable: requires 3 arguments, but 2 were provided}} } @@ -17,20 +27,25 @@ float4 test_no_third_arg(float p0) { float4 test_too_many_arg(float p0) { return lit(p0, p0, p0, p0); // expected-error@-1 {{no matching function for call to 'lit'}} + // expected-note@hlsl/hlsl_compat_overloads.h:* {{candidate function not viable: requires 3 arguments, but 4 were provided}} + // expected-note@hlsl/hlsl_compat_overloads.h:* {{candidate function not viable: requires 3 arguments, but 4 were provided}} + // expected-note@hlsl/hlsl_compat_overloads.h:* {{candidate function not viable: requires 3 arguments, but 4 were provided}} + // expected-note@hlsl/hlsl_compat_overloads.h:* {{candidate function not viable: requires 3 arguments, but 4 were provided}} + // expected-note@hlsl/hlsl_compat_overloads.h:* {{candidate function not viable: requires 3 arguments, but 4 were provided}} // expected-note@hlsl/hlsl_intrinsics.h:* {{candidate function template not viable: requires 3 arguments, but 4 were provided}} // expected-note@hlsl/hlsl_intrinsics.h:* {{candidate function template not viable: requires 3 arguments, but 4 were provided}} } -float4 test_vec_inputs(float2 p0, float2 p1, float2 p2) { - return lit(p0, p1, p2); - // expected-error@-1 {{no matching function for call to 'lit'}} - // expected-note@hlsl/hlsl_intrinsics.h:* {{candidate template ignored: substitution failure [with T = float2]: invalid vector element type 'vector<float, 2>' (vector of 2 'float' values)}} - // expected-note@hlsl/hlsl_intrinsics.h:* {{candidate template ignored: substitution failure [with T = float2]: invalid vector element type 'vector<float, 2>' (vector of 2 'float' values)}} -} +// float4 test_vec_inputs(float2 p0, float2 p1, float2 p2) { +// return lit(p0, p1, p2); +// // expected-error@-1 {{no matching function for call to 'lit'}} +// // expected-note@hlsl/hlsl_intrinsics.h:* {{candidate template ignored: substitution failure [with T = float2]: invalid vector element type 'vector<float, 2>' (vector of 2 'float' values)}} +// // expected-note@hlsl/hlsl_intrinsics.h:* {{candidate template ignored: substitution failure [with T = float2]: invalid vector element type 'vector<float, 2>' (vector of 2 'float' values)}} +// } -float4 test_vec1_inputs(float1 p0, float1 p1, float1 p2) { - return lit(p0, p1, p2); - // expected-error@-1 {{no matching function for call to 'lit'}} - // expected-note@hlsl/hlsl_intrinsics.h:* {{candidate template ignored: substitution failure [with T = float1]: invalid vector element type 'vector<float, 1>' (vector of 1 'float' value)}} - // expected-note@hlsl/hlsl_intrinsics.h:* {{candidate template ignored: substitution failure [with T = float1]: invalid vector element type 'vector<float, 1>' (vector of 1 'float' value)}} -} \ No newline at end of file +// float4 test_vec1_inputs(float1 p0, float1 p1, float1 p2) { +// return lit(p0, p1, p2); +// // expected-error@-1 {{no matching function for call to 'lit'}} +// // expected-note@hlsl/hlsl_intrinsics.h:* {{candidate template ignored: substitution failure [with T = float1]: invalid vector element type 'vector<float, 1>' (vector of 1 'float' value)}} +// // expected-note@hlsl/hlsl_intrinsics.h:* {{candidate template ignored: substitution failure [with T = float1]: invalid vector element type 'vector<float, 1>' (vector of 1 'float' value)}} +// } >From 216353547d60732c0ac72104bd4e1426a3962be4 Mon Sep 17 00:00:00 2001 From: kmpeng <kaitlinp...@microsoft.com> Date: Wed, 2 Apr 2025 09:43:08 -0700 Subject: [PATCH 5/8] WIP fix double/int overloads --- clang/lib/Headers/hlsl.h | 2 +- .../lib/Headers/hlsl/hlsl_compat_overloads.h | 10 ++++---- .../CodeGenHLSL/builtins/lit-overloads.hlsl | 10 ++++---- clang/test/SemaHLSL/BuiltIns/lit-errors.hlsl | 24 +++++++++---------- 4 files changed, 23 insertions(+), 23 deletions(-) diff --git a/clang/lib/Headers/hlsl.h b/clang/lib/Headers/hlsl.h index b494b4d0f78bb..2bc1973f6eb2b 100644 --- a/clang/lib/Headers/hlsl.h +++ b/clang/lib/Headers/hlsl.h @@ -22,10 +22,10 @@ // HLSL standard library function declarations/definitions. #include "hlsl/hlsl_alias_intrinsics.h" +#include "hlsl/hlsl_intrinsics.h" #if __HLSL_VERSION <= __HLSL_202x #include "hlsl/hlsl_compat_overloads.h" #endif -#include "hlsl/hlsl_intrinsics.h" #if defined(__clang__) #pragma clang diagnostic pop diff --git a/clang/lib/Headers/hlsl/hlsl_compat_overloads.h b/clang/lib/Headers/hlsl/hlsl_compat_overloads.h index 86e81123caffc..f463a530c266c 100644 --- a/clang/lib/Headers/hlsl/hlsl_compat_overloads.h +++ b/clang/lib/Headers/hlsl/hlsl_compat_overloads.h @@ -288,19 +288,19 @@ _DXC_COMPAT_TERNARY_INTEGER_OVERLOADS(lerp) // Note: calling lit_impl because calling lit directly causes infinite recursion constexpr float4 lit(double V1, double V2, double V3) { - return __detail::lit_impl((float)V1, (float)V2, (float)V3); + return lit((float)V1, (float)V2, (float)V3); } constexpr float4 lit(int V1, int V2, int V3) { - return __detail::lit_impl((float)V1, (float)V2, (float)V3); + return lit((float)V1, (float)V2, (float)V3); } constexpr float4 lit(uint V1, uint V2, uint V3) { - return __detail::lit_impl((float)V1, (float)V2, (float)V3); + return lit((float)V1, (float)V2, (float)V3); } constexpr float4 lit(int64_t V1, int64_t V2, int64_t V3) { - return __detail::lit_impl((float)V1, (float)V2, (float)V3); + return lit((float)V1, (float)V2, (float)V3); } constexpr float4 lit(uint64_t V1, uint64_t V2, uint64_t V3) { - return __detail::lit_impl((float)V1, (float)V2, (float)V3); + return lit((float)V1, (float)V2, (float)V3); } //===----------------------------------------------------------------------===// diff --git a/clang/test/CodeGenHLSL/builtins/lit-overloads.hlsl b/clang/test/CodeGenHLSL/builtins/lit-overloads.hlsl index 8a9b31bf6ae8b..734f0452900e6 100644 --- a/clang/test/CodeGenHLSL/builtins/lit-overloads.hlsl +++ b/clang/test/CodeGenHLSL/builtins/lit-overloads.hlsl @@ -3,21 +3,21 @@ // RUN: FileCheck %s --check-prefixes=CHECK // CHECK-LABEL: define linkonce_odr noundef nofpclass(nan inf) <4 x float> @_ZN4hlsl3litEddd( -// CHECK: call reassoc nnan ninf nsz arcp afn noundef nofpclass(nan inf) <4 x float> @_ZN4hlsl8__detail8lit_implIfEEDv4_T_S2_S2_S2_( +// CHECK: call reassoc nnan ninf nsz arcp afn noundef nofpclass(nan inf) <4 x float> @_ZN4hlsl3litIfEEKNS_8__detail9enable_ifIXaasr8__detail13is_arithmeticIT_EE5Valuesr8__detail7is_sameIfS3_EE5valueEDv4_S3_E4TypeES3_S3_S3_( float4 test_lit_double(double NDotL, double NDotH, double M) { return lit(NDotL, NDotH, M); } // CHECK-LABEL: define linkonce_odr noundef nofpclass(nan inf) <4 x float> @_ZN4hlsl3litEiii( -// CHECK: call reassoc nnan ninf nsz arcp afn noundef nofpclass(nan inf) <4 x float> @_ZN4hlsl8__detail8lit_implIfEEDv4_T_S2_S2_S2_( +// CHECK: call reassoc nnan ninf nsz arcp afn noundef nofpclass(nan inf) <4 x float> @_ZN4hlsl3litIfEEKNS_8__detail9enable_ifIXaasr8__detail13is_arithmeticIT_EE5Valuesr8__detail7is_sameIfS3_EE5valueEDv4_S3_E4TypeES3_S3_S3_( float4 test_lit_int(int NDotL, int NDotH, int M) { return lit(NDotL, NDotH, M); } // CHECK-LABEL: define linkonce_odr noundef nofpclass(nan inf) <4 x float> @_ZN4hlsl3litEjjj( -// CHECK: call reassoc nnan ninf nsz arcp afn noundef nofpclass(nan inf) <4 x float> @_ZN4hlsl8__detail8lit_implIfEEDv4_T_S2_S2_S2_( +// CHECK: call reassoc nnan ninf nsz arcp afn noundef nofpclass(nan inf) <4 x float> @_ZN4hlsl3litIfEEKNS_8__detail9enable_ifIXaasr8__detail13is_arithmeticIT_EE5Valuesr8__detail7is_sameIfS3_EE5valueEDv4_S3_E4TypeES3_S3_S3_( float4 test_lit_uint(uint NDotL, uint NDotH, uint M) { return lit(NDotL, NDotH, M); } // CHECK-LABEL: define linkonce_odr noundef nofpclass(nan inf) <4 x float> @_ZN4hlsl3litElll( -// CHECK: call reassoc nnan ninf nsz arcp afn noundef nofpclass(nan inf) <4 x float> @_ZN4hlsl8__detail8lit_implIfEEDv4_T_S2_S2_S2_( +// CHECK: call reassoc nnan ninf nsz arcp afn noundef nofpclass(nan inf) <4 x float> @_ZN4hlsl3litIfEEKNS_8__detail9enable_ifIXaasr8__detail13is_arithmeticIT_EE5Valuesr8__detail7is_sameIfS3_EE5valueEDv4_S3_E4TypeES3_S3_S3_( float4 test_lit_int64_t(int64_t NDotL, int64_t NDotH, int64_t M) { return lit(NDotL, NDotH, M); } // CHECK-LABEL: define linkonce_odr noundef nofpclass(nan inf) <4 x float> @_ZN4hlsl3litEmmm( -// CHECK: call reassoc nnan ninf nsz arcp afn noundef nofpclass(nan inf) <4 x float> @_ZN4hlsl8__detail8lit_implIfEEDv4_T_S2_S2_S2_( +// CHECK: call reassoc nnan ninf nsz arcp afn noundef nofpclass(nan inf) <4 x float> @_ZN4hlsl3litIfEEKNS_8__detail9enable_ifIXaasr8__detail13is_arithmeticIT_EE5Valuesr8__detail7is_sameIfS3_EE5valueEDv4_S3_E4TypeES3_S3_S3_( float4 test_lit_uint64_t(uint64_t NDotL, uint64_t NDotH, uint64_t M) { return lit(NDotL, NDotH, M); } diff --git a/clang/test/SemaHLSL/BuiltIns/lit-errors.hlsl b/clang/test/SemaHLSL/BuiltIns/lit-errors.hlsl index b14d3e840fa0e..9ac6fa0157be8 100644 --- a/clang/test/SemaHLSL/BuiltIns/lit-errors.hlsl +++ b/clang/test/SemaHLSL/BuiltIns/lit-errors.hlsl @@ -36,16 +36,16 @@ float4 test_too_many_arg(float p0) { // expected-note@hlsl/hlsl_intrinsics.h:* {{candidate function template not viable: requires 3 arguments, but 4 were provided}} } -// float4 test_vec_inputs(float2 p0, float2 p1, float2 p2) { -// return lit(p0, p1, p2); -// // expected-error@-1 {{no matching function for call to 'lit'}} -// // expected-note@hlsl/hlsl_intrinsics.h:* {{candidate template ignored: substitution failure [with T = float2]: invalid vector element type 'vector<float, 2>' (vector of 2 'float' values)}} -// // expected-note@hlsl/hlsl_intrinsics.h:* {{candidate template ignored: substitution failure [with T = float2]: invalid vector element type 'vector<float, 2>' (vector of 2 'float' values)}} -// } +float4 test_vec_inputs(float2 p0, float2 p1, float2 p2) { + return lit(p0, p1, p2); + // expected-error@-1 {{no matching function for call to 'lit'}} + // expected-note@hlsl/hlsl_intrinsics.h:* {{candidate template ignored: substitution failure [with T = float2]: invalid vector element type 'vector<float, 2>' (vector of 2 'float' values)}} + // expected-note@hlsl/hlsl_intrinsics.h:* {{candidate template ignored: substitution failure [with T = float2]: invalid vector element type 'vector<float, 2>' (vector of 2 'float' values)}} +} -// float4 test_vec1_inputs(float1 p0, float1 p1, float1 p2) { -// return lit(p0, p1, p2); -// // expected-error@-1 {{no matching function for call to 'lit'}} -// // expected-note@hlsl/hlsl_intrinsics.h:* {{candidate template ignored: substitution failure [with T = float1]: invalid vector element type 'vector<float, 1>' (vector of 1 'float' value)}} -// // expected-note@hlsl/hlsl_intrinsics.h:* {{candidate template ignored: substitution failure [with T = float1]: invalid vector element type 'vector<float, 1>' (vector of 1 'float' value)}} -// } +float4 test_vec1_inputs(float1 p0, float1 p1, float1 p2) { + return lit(p0, p1, p2); + // expected-error@-1 {{no matching function for call to 'lit'}} + // expected-note@hlsl/hlsl_intrinsics.h:* {{candidate template ignored: substitution failure [with T = float1]: invalid vector element type 'vector<float, 1>' (vector of 1 'float' value)}} + // expected-note@hlsl/hlsl_intrinsics.h:* {{candidate template ignored: substitution failure [with T = float1]: invalid vector element type 'vector<float, 1>' (vector of 1 'float' value)}} +} >From 670b8cc954c1631e622ba41070c112835f32306e Mon Sep 17 00:00:00 2001 From: kmpeng <kaitlinp...@microsoft.com> Date: Wed, 2 Apr 2025 15:52:28 -0700 Subject: [PATCH 6/8] finished double/int overloads and overload tests --- .../lib/Headers/hlsl/hlsl_compat_overloads.h | 27 ++--- .../CodeGenHLSL/builtins/lit-overloads.hlsl | 102 +++++++++++++++--- clang/test/CodeGenHLSL/builtins/lit.hlsl | 4 +- .../SemaHLSL/BuiltIns/lit-errors-16bit.hlsl | 9 ++ clang/test/SemaHLSL/BuiltIns/lit-errors.hlsl | 20 +--- 5 files changed, 112 insertions(+), 50 deletions(-) create mode 100644 clang/test/SemaHLSL/BuiltIns/lit-errors-16bit.hlsl diff --git a/clang/lib/Headers/hlsl/hlsl_compat_overloads.h b/clang/lib/Headers/hlsl/hlsl_compat_overloads.h index f463a530c266c..cbf5364c0b29c 100644 --- a/clang/lib/Headers/hlsl/hlsl_compat_overloads.h +++ b/clang/lib/Headers/hlsl/hlsl_compat_overloads.h @@ -9,8 +9,6 @@ #ifndef _HLSL_COMPAT_OVERLOADS_H_ #define _HLSl_COMPAT_OVERLOADS_H_ -#include "hlsl/hlsl_intrinsic_helpers.h" - namespace hlsl { // Note: Functions in this file are sorted alphabetically, then grouped by base @@ -286,21 +284,16 @@ _DXC_COMPAT_TERNARY_INTEGER_OVERLOADS(lerp) // lit builtins overloads //===----------------------------------------------------------------------===// -// Note: calling lit_impl because calling lit directly causes infinite recursion -constexpr float4 lit(double V1, double V2, double V3) { - return lit((float)V1, (float)V2, (float)V3); -} -constexpr float4 lit(int V1, int V2, int V3) { - return lit((float)V1, (float)V2, (float)V3); -} -constexpr float4 lit(uint V1, uint V2, uint V3) { - return lit((float)V1, (float)V2, (float)V3); -} -constexpr float4 lit(int64_t V1, int64_t V2, int64_t V3) { - return lit((float)V1, (float)V2, (float)V3); -} -constexpr float4 lit(uint64_t V1, uint64_t V2, uint64_t V3) { - return lit((float)V1, (float)V2, (float)V3); +template <typename T> +constexpr __detail::enable_if_t<__detail::is_arithmetic<T>::Value && + (__detail::is_same<double, T>::value || + __detail::is_same<int, T>::value || + __detail::is_same<uint, T>::value || + __detail::is_same<int64_t, T>::value || + __detail::is_same<uint64_t, T>::value), + vector<T, 4>> +lit(T NDotL, T NDotH, T M) { + return lit((float)NDotL, (float)NDotH, (float)M); } //===----------------------------------------------------------------------===// diff --git a/clang/test/CodeGenHLSL/builtins/lit-overloads.hlsl b/clang/test/CodeGenHLSL/builtins/lit-overloads.hlsl index 734f0452900e6..676889ecf16b0 100644 --- a/clang/test/CodeGenHLSL/builtins/lit-overloads.hlsl +++ b/clang/test/CodeGenHLSL/builtins/lit-overloads.hlsl @@ -1,23 +1,93 @@ // RUN: %clang_cc1 -std=hlsl202x -finclude-default-header -x hlsl -triple dxil-pc-shadermodel6.3-library %s \ -// RUN: -emit-llvm -disable-llvm-passes -o - | \ +// RUN: -emit-llvm -o - | \ // RUN: FileCheck %s --check-prefixes=CHECK -// CHECK-LABEL: define linkonce_odr noundef nofpclass(nan inf) <4 x float> @_ZN4hlsl3litEddd( -// CHECK: call reassoc nnan ninf nsz arcp afn noundef nofpclass(nan inf) <4 x float> @_ZN4hlsl3litIfEEKNS_8__detail9enable_ifIXaasr8__detail13is_arithmeticIT_EE5Valuesr8__detail7is_sameIfS3_EE5valueEDv4_S3_E4TypeES3_S3_S3_( -float4 test_lit_double(double NDotL, double NDotH, double M) { return lit(NDotL, NDotH, M); } +// CHECK-LABEL: test_lit_double +// CHECK: %conv.i = fptrunc reassoc nnan ninf nsz arcp afn double %{{.*}} to float +// CHECK: %conv1.i = fptrunc reassoc nnan ninf nsz arcp afn double %{{.*}} to float +// CHECK: %conv2.i = fptrunc reassoc nnan ninf nsz arcp afn double %{{.*}} to float +// CHECK: %cmp.i = fcmp reassoc nnan ninf nsz arcp afn olt float %{{.*}}, 0.000000e+00 +// CHECK: %hlsl.select.i = select reassoc nnan ninf nsz arcp afn i1 %{{.*}}, float 0.000000e+00, float %{{.*}} +// CHECK: %vecinit.i = insertelement <4 x float> <float 1.000000e+00, float poison, float poison, float poison>, float %{{.*}}, i32 1 +// CHECK: %cmp4.i = fcmp reassoc nnan ninf nsz arcp afn olt float %{{.*}}, 0.000000e+00 +// CHECK: %hlsl.or.i = or i1 %{{.*}}, %cmp4.i +// CHECK: %elt.log.i = call reassoc nnan ninf nsz arcp afn float @llvm.log.f32(float %{{.*}}) +// CHECK: %mul.i = fmul reassoc nnan ninf nsz arcp afn float %elt.log.i, %{{.*}} +// CHECK: %elt.exp.i = call reassoc nnan ninf nsz arcp afn float @llvm.exp.f32(float %mul.i) +// CHECK: %hlsl.select7.i = select reassoc nnan ninf nsz arcp afn i1 %{{.*}}, float 0.000000e+00, float %{{.*}} +// CHECK: %vecins.i = insertelement <4 x float> %{{.*}}, float %hlsl.select7.i, i32 2 +// CHECK: %conv3.i = fpext reassoc nnan ninf nsz arcp afn <4 x float> %{{.*}} to <4 x double> +// CHECK: ret <4 x double> %conv3.i +double4 test_lit_double(double NDotL, double NDotH, double M) { return lit(NDotL, NDotH, M); } -// CHECK-LABEL: define linkonce_odr noundef nofpclass(nan inf) <4 x float> @_ZN4hlsl3litEiii( -// CHECK: call reassoc nnan ninf nsz arcp afn noundef nofpclass(nan inf) <4 x float> @_ZN4hlsl3litIfEEKNS_8__detail9enable_ifIXaasr8__detail13is_arithmeticIT_EE5Valuesr8__detail7is_sameIfS3_EE5valueEDv4_S3_E4TypeES3_S3_S3_( -float4 test_lit_int(int NDotL, int NDotH, int M) { return lit(NDotL, NDotH, M); } +// CHECK-LABEL: test_lit_int +// CHECK: %conv.i = sitofp i32 %{{.*}} to float +// CHECK: %conv1.i = sitofp i32 %{{.*}} to float +// CHECK: %conv2.i = sitofp i32 %{{.*}} to float +// CHECK: %cmp.i = fcmp reassoc nnan ninf nsz arcp afn olt float %{{.*}}, 0.000000e+00 +// CHECK: %hlsl.select.i = select reassoc nnan ninf nsz arcp afn i1 %{{.*}}, float 0.000000e+00, float %{{.*}} +// CHECK: %vecinit.i = insertelement <4 x float> <float 1.000000e+00, float poison, float poison, float poison>, float %{{.*}}, i32 1 +// CHECK: %cmp4.i = fcmp reassoc nnan ninf nsz arcp afn olt float %{{.*}}, 0.000000e+00 +// CHECK: %hlsl.or.i = or i1 %{{.*}}, %cmp4.i +// CHECK: %elt.log.i = call reassoc nnan ninf nsz arcp afn float @llvm.log.f32(float %{{.*}}) +// CHECK: %mul.i = fmul reassoc nnan ninf nsz arcp afn float %elt.log.i, %{{.*}} +// CHECK: %elt.exp.i = call reassoc nnan ninf nsz arcp afn float @llvm.exp.f32(float %mul.i) +// CHECK: %hlsl.select7.i = select reassoc nnan ninf nsz arcp afn i1 %{{.*}}, float 0.000000e+00, float %{{.*}} +// CHECK: %vecins.i = insertelement <4 x float> %{{.*}}, float %hlsl.select7.i, i32 2 +// CHECK: %conv3.i = fptosi <4 x float> %{{.*}} to <4 x i32> +// CHECK: ret <4 x i32> %conv3.i +int4 test_lit_int(int NDotL, int NDotH, int M) { return lit(NDotL, NDotH, M); } -// CHECK-LABEL: define linkonce_odr noundef nofpclass(nan inf) <4 x float> @_ZN4hlsl3litEjjj( -// CHECK: call reassoc nnan ninf nsz arcp afn noundef nofpclass(nan inf) <4 x float> @_ZN4hlsl3litIfEEKNS_8__detail9enable_ifIXaasr8__detail13is_arithmeticIT_EE5Valuesr8__detail7is_sameIfS3_EE5valueEDv4_S3_E4TypeES3_S3_S3_( -float4 test_lit_uint(uint NDotL, uint NDotH, uint M) { return lit(NDotL, NDotH, M); } +// CHECK-LABEL: test_lit_uint +// CHECK: %conv.i = uitofp i32 %{{.*}} to float +// CHECK: %conv1.i = uitofp i32 %{{.*}} to float +// CHECK: %conv2.i = uitofp i32 %{{.*}} to float +// CHECK: %cmp.i = fcmp reassoc nnan ninf nsz arcp afn olt float %{{.*}}, 0.000000e+00 +// CHECK: %hlsl.select.i = select reassoc nnan ninf nsz arcp afn i1 %{{.*}}, float 0.000000e+00, float %{{.*}} +// CHECK: %vecinit.i = insertelement <4 x float> <float 1.000000e+00, float poison, float poison, float poison>, float %{{.*}}, i32 1 +// CHECK: %cmp4.i = fcmp reassoc nnan ninf nsz arcp afn olt float %{{.*}}, 0.000000e+00 +// CHECK: %hlsl.or.i = or i1 %{{.*}}, %cmp4.i +// CHECK: %elt.log.i = call reassoc nnan ninf nsz arcp afn float @llvm.log.f32(float %{{.*}}) +// CHECK: %mul.i = fmul reassoc nnan ninf nsz arcp afn float %elt.log.i, %{{.*}} +// CHECK: %elt.exp.i = call reassoc nnan ninf nsz arcp afn float @llvm.exp.f32(float %mul.i) +// CHECK: %hlsl.select7.i = select reassoc nnan ninf nsz arcp afn i1 %{{.*}}, float 0.000000e+00, float %{{.*}} +// CHECK: %vecins.i = insertelement <4 x float> %{{.*}}, float %hlsl.select7.i, i32 2 +// CHECK: %conv3.i = fptoui <4 x float> %{{.*}} to <4 x i32> +// CHECK: ret <4 x i32> %conv3.i +uint4 test_lit_uint(uint NDotL, uint NDotH, uint M) { return lit(NDotL, NDotH, M); } -// CHECK-LABEL: define linkonce_odr noundef nofpclass(nan inf) <4 x float> @_ZN4hlsl3litElll( -// CHECK: call reassoc nnan ninf nsz arcp afn noundef nofpclass(nan inf) <4 x float> @_ZN4hlsl3litIfEEKNS_8__detail9enable_ifIXaasr8__detail13is_arithmeticIT_EE5Valuesr8__detail7is_sameIfS3_EE5valueEDv4_S3_E4TypeES3_S3_S3_( -float4 test_lit_int64_t(int64_t NDotL, int64_t NDotH, int64_t M) { return lit(NDotL, NDotH, M); } +// CHECK-LABEL: test_lit_int64_t +// CHECK: %conv.i = sitofp i64 %{{.*}} to float +// CHECK: %conv1.i = sitofp i64 %{{.*}} to float +// CHECK: %conv2.i = sitofp i64 %{{.*}} to float +// CHECK: %cmp.i = fcmp reassoc nnan ninf nsz arcp afn olt float %{{.*}}, 0.000000e+00 +// CHECK: %hlsl.select.i = select reassoc nnan ninf nsz arcp afn i1 %{{.*}}, float 0.000000e+00, float %{{.*}} +// CHECK: %vecinit.i = insertelement <4 x float> <float 1.000000e+00, float poison, float poison, float poison>, float %{{.*}}, i32 1 +// CHECK: %cmp4.i = fcmp reassoc nnan ninf nsz arcp afn olt float %{{.*}}, 0.000000e+00 +// CHECK: %hlsl.or.i = or i1 %{{.*}}, %cmp4.i +// CHECK: %elt.log.i = call reassoc nnan ninf nsz arcp afn float @llvm.log.f32(float %{{.*}}) +// CHECK: %mul.i = fmul reassoc nnan ninf nsz arcp afn float %elt.log.i, %{{.*}} +// CHECK: %elt.exp.i = call reassoc nnan ninf nsz arcp afn float @llvm.exp.f32(float %mul.i) +// CHECK: %hlsl.select7.i = select reassoc nnan ninf nsz arcp afn i1 %{{.*}}, float 0.000000e+00, float %{{.*}} +// CHECK: %vecins.i = insertelement <4 x float> %{{.*}}, float %hlsl.select7.i, i32 2 +// CHECK: %conv3.i = fptosi <4 x float> %{{.*}} to <4 x i64> +// CHECK: ret <4 x i64> %conv3.i +int64_t4 test_lit_int64_t(int64_t NDotL, int64_t NDotH, int64_t M) { return lit(NDotL, NDotH, M); } -// CHECK-LABEL: define linkonce_odr noundef nofpclass(nan inf) <4 x float> @_ZN4hlsl3litEmmm( -// CHECK: call reassoc nnan ninf nsz arcp afn noundef nofpclass(nan inf) <4 x float> @_ZN4hlsl3litIfEEKNS_8__detail9enable_ifIXaasr8__detail13is_arithmeticIT_EE5Valuesr8__detail7is_sameIfS3_EE5valueEDv4_S3_E4TypeES3_S3_S3_( -float4 test_lit_uint64_t(uint64_t NDotL, uint64_t NDotH, uint64_t M) { return lit(NDotL, NDotH, M); } +// CHECK-LABEL: test_lit_uint64_t +// CHECK: %conv.i = uitofp i64 %{{.*}} to float +// CHECK: %conv1.i = uitofp i64 %{{.*}} to float +// CHECK: %conv2.i = uitofp i64 %{{.*}} to float +// CHECK: %cmp.i = fcmp reassoc nnan ninf nsz arcp afn olt float %{{.*}}, 0.000000e+00 +// CHECK: %hlsl.select.i = select reassoc nnan ninf nsz arcp afn i1 %{{.*}}, float 0.000000e+00, float %{{.*}} +// CHECK: %vecinit.i = insertelement <4 x float> <float 1.000000e+00, float poison, float poison, float poison>, float %{{.*}}, i32 1 +// CHECK: %cmp4.i = fcmp reassoc nnan ninf nsz arcp afn olt float %{{.*}}, 0.000000e+00 +// CHECK: %hlsl.or.i = or i1 %{{.*}}, %cmp4.i +// CHECK: %elt.log.i = call reassoc nnan ninf nsz arcp afn float @llvm.log.f32(float %{{.*}}) +// CHECK: %mul.i = fmul reassoc nnan ninf nsz arcp afn float %elt.log.i, %{{.*}} +// CHECK: %elt.exp.i = call reassoc nnan ninf nsz arcp afn float @llvm.exp.f32(float %mul.i) +// CHECK: %hlsl.select7.i = select reassoc nnan ninf nsz arcp afn i1 %{{.*}}, float 0.000000e+00, float %{{.*}} +// CHECK: %vecins.i = insertelement <4 x float> %{{.*}}, float %hlsl.select7.i, i32 2 +// CHECK: %conv3.i = fptoui <4 x float> %{{.*}} to <4 x i64> +// CHECK: ret <4 x i64> %conv3.i +uint64_t4 test_lit_uint64_t(uint64_t NDotL, uint64_t NDotH, uint64_t M) { return lit(NDotL, NDotH, M); } diff --git a/clang/test/CodeGenHLSL/builtins/lit.hlsl b/clang/test/CodeGenHLSL/builtins/lit.hlsl index 484a85779a375..1737a460e020f 100644 --- a/clang/test/CodeGenHLSL/builtins/lit.hlsl +++ b/clang/test/CodeGenHLSL/builtins/lit.hlsl @@ -1,7 +1,7 @@ // NOTE: Assertions have been autogenerated by utils/update_cc_test_checks.py UTC_ARGS: --version 5 // RUN: %clang_cc1 -finclude-default-header -x hlsl -triple dxil-pc-shadermodel6.3-library %s -fnative-half-type -emit-llvm -O1 -o - | FileCheck %s -// CHECK-LABEL: define noundef nofpclass(nan inf) <4 x half> @_Z13test_lit_halfDhDhDh( +// CHECK-LABEL: test_lit_half // CHECK-SAME: half noundef nofpclass(nan inf) [[NDOTL:%.*]], half noundef nofpclass(nan inf) [[NDOTH:%.*]], half noundef nofpclass(nan inf) [[M:%.*]]) local_unnamed_addr #[[ATTR0:[0-9]+]] { // CHECK-NEXT: [[ENTRY:.*:]] // CHECK-NEXT: [[CMP_I:%.*]] = fcmp reassoc nnan ninf nsz arcp afn olt half [[NDOTL]], 0xH0000 @@ -18,7 +18,7 @@ // half4 test_lit_half(half NDotL, half NDotH, half M) { return lit(NDotL, NDotH, M); } -// CHECK-LABEL: define noundef nofpclass(nan inf) <4 x float> @_Z14test_lit_floatfff( +// CHECK-LABEL: test_lit_float // CHECK-SAME: float noundef nofpclass(nan inf) [[NDOTL:%.*]], float noundef nofpclass(nan inf) [[NDOTH:%.*]], float noundef nofpclass(nan inf) [[M:%.*]]) local_unnamed_addr #[[ATTR0]] { // CHECK-NEXT: [[ENTRY:.*:]] // CHECK-NEXT: [[CMP_I:%.*]] = fcmp reassoc nnan ninf nsz arcp afn olt float [[NDOTL]], 0.000000e+00 diff --git a/clang/test/SemaHLSL/BuiltIns/lit-errors-16bit.hlsl b/clang/test/SemaHLSL/BuiltIns/lit-errors-16bit.hlsl new file mode 100644 index 0000000000000..311bad9a0ef79 --- /dev/null +++ b/clang/test/SemaHLSL/BuiltIns/lit-errors-16bit.hlsl @@ -0,0 +1,9 @@ +// RUN: not %clang_dxc -enable-16bit-types -T cs_6_0 -HV 202x %s 2>&1 | FileCheck %s -DTEST_TYPE=half +// RUN: not %clang_dxc -enable-16bit-types -T cs_6_0 -HV 202x %s 2>&1 | FileCheck %s -DTEST_TYPE=int16_t +// RUN: not %clang_dxc -enable-16bit-types -T cs_6_0 -HV 202x %s 2>&1 | FileCheck %s -DTEST_TYPE=uint16_t + +// check we error on 16 bit type if shader model is too old +// CHECK: '-enable-16bit-types' option requires target HLSL Version >= 2018 and shader model >= 6.2, but HLSL Version is 'hlsl202x' and shader model is '6.0' +vector<TEST_TYPE,4> test_error(TEST_TYPE p0) { + return lit(p0, p0, p0); +} diff --git a/clang/test/SemaHLSL/BuiltIns/lit-errors.hlsl b/clang/test/SemaHLSL/BuiltIns/lit-errors.hlsl index 9ac6fa0157be8..799fb62a9560d 100644 --- a/clang/test/SemaHLSL/BuiltIns/lit-errors.hlsl +++ b/clang/test/SemaHLSL/BuiltIns/lit-errors.hlsl @@ -3,37 +3,25 @@ float4 test_no_second_arg(float p0) { return lit(p0); // expected-error@-1 {{no matching function for call to 'lit'}} - // expected-note@hlsl/hlsl_compat_overloads.h:* {{candidate function not viable: requires 3 arguments, but 1 was provided}} - // expected-note@hlsl/hlsl_compat_overloads.h:* {{candidate function not viable: requires 3 arguments, but 1 was provided}} - // expected-note@hlsl/hlsl_compat_overloads.h:* {{candidate function not viable: requires 3 arguments, but 1 was provided}} - // expected-note@hlsl/hlsl_compat_overloads.h:* {{candidate function not viable: requires 3 arguments, but 1 was provided}} - // expected-note@hlsl/hlsl_compat_overloads.h:* {{candidate function not viable: requires 3 arguments, but 1 was provided}} // expected-note@hlsl/hlsl_intrinsics.h:* {{candidate function template not viable: requires 3 arguments, but 1 was provided}} // expected-note@hlsl/hlsl_intrinsics.h:* {{candidate function template not viable: requires 3 arguments, but 1 was provided}} + // expected-note@hlsl/hlsl_compat_overloads.h:* {{candidate function template not viable: requires 3 arguments, but 1 was provided}} } float4 test_no_third_arg(float p0) { return lit(p0, p0); // expected-error@-1 {{no matching function for call to 'lit'}} - // expected-note@hlsl/hlsl_compat_overloads.h:* {{candidate function not viable: requires 3 arguments, but 2 were provided}} - // expected-note@hlsl/hlsl_compat_overloads.h:* {{candidate function not viable: requires 3 arguments, but 2 were provided}} - // expected-note@hlsl/hlsl_compat_overloads.h:* {{candidate function not viable: requires 3 arguments, but 2 were provided}} - // expected-note@hlsl/hlsl_compat_overloads.h:* {{candidate function not viable: requires 3 arguments, but 2 were provided}} - // expected-note@hlsl/hlsl_compat_overloads.h:* {{candidate function not viable: requires 3 arguments, but 2 were provided}} // expected-note@hlsl/hlsl_intrinsics.h:* {{candidate function template not viable: requires 3 arguments, but 2 were provided}} // expected-note@hlsl/hlsl_intrinsics.h:* {{candidate function template not viable: requires 3 arguments, but 2 were provided}} + // expected-note@hlsl/hlsl_compat_overloads.h:* {{candidate function template not viable: requires 3 arguments, but 2 were provided}} } float4 test_too_many_arg(float p0) { return lit(p0, p0, p0, p0); // expected-error@-1 {{no matching function for call to 'lit'}} - // expected-note@hlsl/hlsl_compat_overloads.h:* {{candidate function not viable: requires 3 arguments, but 4 were provided}} - // expected-note@hlsl/hlsl_compat_overloads.h:* {{candidate function not viable: requires 3 arguments, but 4 were provided}} - // expected-note@hlsl/hlsl_compat_overloads.h:* {{candidate function not viable: requires 3 arguments, but 4 were provided}} - // expected-note@hlsl/hlsl_compat_overloads.h:* {{candidate function not viable: requires 3 arguments, but 4 were provided}} - // expected-note@hlsl/hlsl_compat_overloads.h:* {{candidate function not viable: requires 3 arguments, but 4 were provided}} // expected-note@hlsl/hlsl_intrinsics.h:* {{candidate function template not viable: requires 3 arguments, but 4 were provided}} // expected-note@hlsl/hlsl_intrinsics.h:* {{candidate function template not viable: requires 3 arguments, but 4 were provided}} + // expected-note@hlsl/hlsl_compat_overloads.h:* {{candidate function template not viable: requires 3 arguments, but 4 were provided}} } float4 test_vec_inputs(float2 p0, float2 p1, float2 p2) { @@ -41,6 +29,7 @@ float4 test_vec_inputs(float2 p0, float2 p1, float2 p2) { // expected-error@-1 {{no matching function for call to 'lit'}} // expected-note@hlsl/hlsl_intrinsics.h:* {{candidate template ignored: substitution failure [with T = float2]: invalid vector element type 'vector<float, 2>' (vector of 2 'float' values)}} // expected-note@hlsl/hlsl_intrinsics.h:* {{candidate template ignored: substitution failure [with T = float2]: invalid vector element type 'vector<float, 2>' (vector of 2 'float' values)}} + // expected-note@hlsl/hlsl_compat_overloads.h:* {{candidate template ignored: substitution failure [with T = float2]: invalid vector element type 'vector<float, 2>' (vector of 2 'float' values)}} } float4 test_vec1_inputs(float1 p0, float1 p1, float1 p2) { @@ -48,4 +37,5 @@ float4 test_vec1_inputs(float1 p0, float1 p1, float1 p2) { // expected-error@-1 {{no matching function for call to 'lit'}} // expected-note@hlsl/hlsl_intrinsics.h:* {{candidate template ignored: substitution failure [with T = float1]: invalid vector element type 'vector<float, 1>' (vector of 1 'float' value)}} // expected-note@hlsl/hlsl_intrinsics.h:* {{candidate template ignored: substitution failure [with T = float1]: invalid vector element type 'vector<float, 1>' (vector of 1 'float' value)}} + // expected-note@hlsl/hlsl_compat_overloads.h:* {{candidate template ignored: substitution failure [with T = float1]: invalid vector element type 'vector<float, 1>' (vector of 1 'float' value)}} } >From 5718451969d92bb0b9a4db4ef3acbe16b5fe3709 Mon Sep 17 00:00:00 2001 From: kmpeng <kaitlinp...@microsoft.com> Date: Thu, 3 Apr 2025 15:09:38 -0700 Subject: [PATCH 7/8] address PR comments - formatting changes --- clang/lib/Headers/hlsl/hlsl_intrinsic_helpers.h | 2 +- clang/test/CodeGenHLSL/builtins/lit.hlsl | 2 -- 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/clang/lib/Headers/hlsl/hlsl_intrinsic_helpers.h b/clang/lib/Headers/hlsl/hlsl_intrinsic_helpers.h index 3a41635a89aa4..c3e5b105f6e86 100644 --- a/clang/lib/Headers/hlsl/hlsl_intrinsic_helpers.h +++ b/clang/lib/Headers/hlsl/hlsl_intrinsic_helpers.h @@ -105,7 +105,7 @@ template <typename T> constexpr vector<T, 4> lit_impl(T NDotL, T NDotH, T M) { bool DiffuseCond = NDotL < 0; T Diffuse = select<T>(DiffuseCond, 0, NDotL); vector<T, 4> Result = {1, Diffuse, 0, 1}; - bool SpecularCond = or (DiffuseCond, (NDotH < 0)); + bool SpecularCond = or(DiffuseCond, (NDotH < 0)); T SpecularExp = exp(log(NDotH) * M); Result[2] = select<T>(SpecularCond, 0, SpecularExp); return Result; diff --git a/clang/test/CodeGenHLSL/builtins/lit.hlsl b/clang/test/CodeGenHLSL/builtins/lit.hlsl index 1737a460e020f..114cbd0066844 100644 --- a/clang/test/CodeGenHLSL/builtins/lit.hlsl +++ b/clang/test/CodeGenHLSL/builtins/lit.hlsl @@ -15,7 +15,6 @@ // CHECK-NEXT: [[HLSL_SELECT7_I:%.*]] = select reassoc nnan ninf nsz arcp afn i1 [[HLSL_OR_I]], half 0xH0000, half [[ELT_EXP_I]] // CHECK-NEXT: [[VECINS_I:%.*]] = insertelement <4 x half> [[VECINIT2_I]], half [[HLSL_SELECT7_I]], i64 2 // CHECK-NEXT: ret <4 x half> [[VECINS_I]] -// half4 test_lit_half(half NDotL, half NDotH, half M) { return lit(NDotL, NDotH, M); } // CHECK-LABEL: test_lit_float @@ -32,5 +31,4 @@ half4 test_lit_half(half NDotL, half NDotH, half M) { return lit(NDotL, NDotH, M // CHECK-NEXT: [[HLSL_SELECT7_I:%.*]] = select reassoc nnan ninf nsz arcp afn i1 [[HLSL_OR_I]], float 0.000000e+00, float [[ELT_EXP_I]] // CHECK-NEXT: [[VECINS_I:%.*]] = insertelement <4 x float> [[VECINIT2_I]], float [[HLSL_SELECT7_I]], i64 2 // CHECK-NEXT: ret <4 x float> [[VECINS_I]] -// float4 test_lit_float(float NDotL, float NDotH, float M) { return lit(NDotL, NDotH, M); } >From 2460593e6a5f509f803e9978dc5b678acaee0ce8 Mon Sep 17 00:00:00 2001 From: kmpeng <kaitlinp...@microsoft.com> Date: Thu, 3 Apr 2025 15:28:14 -0700 Subject: [PATCH 8/8] address PR comments - turn clang-format off for `or` line in `lit_impl` --- clang/lib/Headers/hlsl/hlsl_intrinsic_helpers.h | 2 ++ 1 file changed, 2 insertions(+) diff --git a/clang/lib/Headers/hlsl/hlsl_intrinsic_helpers.h b/clang/lib/Headers/hlsl/hlsl_intrinsic_helpers.h index c3e5b105f6e86..32954e7bc1bdb 100644 --- a/clang/lib/Headers/hlsl/hlsl_intrinsic_helpers.h +++ b/clang/lib/Headers/hlsl/hlsl_intrinsic_helpers.h @@ -105,7 +105,9 @@ template <typename T> constexpr vector<T, 4> lit_impl(T NDotL, T NDotH, T M) { bool DiffuseCond = NDotL < 0; T Diffuse = select<T>(DiffuseCond, 0, NDotL); vector<T, 4> Result = {1, Diffuse, 0, 1}; + // clang-format off bool SpecularCond = or(DiffuseCond, (NDotH < 0)); + // clang-format on T SpecularExp = exp(log(NDotH) * M); Result[2] = select<T>(SpecularCond, 0, SpecularExp); return Result; _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits