-
Notifications
You must be signed in to change notification settings - Fork 13.6k
[HLSL] Implement the dst HLSL Function #133828
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 6 commits
3a45246
4cde5a5
0b82759
464aee1
cd53a27
bd44eaa
b31595b
5b4157a
93f8f19
6073386
312817d
1e3a68c
b164fc1
6ca58ec
57517c3
60eddc7
30ab473
7f0a5c9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -174,6 +174,31 @@ const inline float distance(__detail::HLSL_FIXED_VECTOR<float, N> X, | |
__detail::HLSL_FIXED_VECTOR<float, N> Y) { | ||
return __detail::distance_vec_impl(X, Y); | ||
} | ||
//===----------------------------------------------------------------------===// | ||
metkarpoonam marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// dst builtins | ||
//===----------------------------------------------------------------------===// | ||
|
||
/// \fn fvector dst( fvector, fvector) | ||
farzonl marked this conversation as resolved.
Show resolved
Hide resolved
|
||
/// \brief Returns the length of a vector | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This description sounds off to me. Is this quoted from official documentation anywhere? To me the "length" of a https://learn.microsoft.com/en-us/windows/win32/direct3dhlsl/dst says |
||
/// \param src0 [in] The first vector contain {_, d*d, d*d, _} | ||
/// \param src1 [in] The second vector contain {_, 1/d, _, 1/d} | ||
/// | ||
/// Return the computed distance vector contain {1, d, d*d, 1/d} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The combination of squared distance (src0) and reciprocal distance (src1) enables the dst function to efficiently compute a distance vector.
https://stackoverflow.com/questions/8525803/what-is-the-hlsl-dst-instruction-for There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think the agorithm correctness is the problem. Its confusing because no one knows what d is. your inputs are Src0 and Src1. You need to put your documentation in those terms. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yep, that was the point I was making as well. Think about the comment header from the perspective of a random user who is being shown the comment as an editor popup. It needs to be useful in that context |
||
|
||
_HLSL_16BIT_AVAILABILITY(shadermodel, 6.2) | ||
const inline vector<half, 4> dst(vector<half, 4> src0, vector<half, 4> src1) { | ||
return __detail::dst_impl(src0, src1); | ||
} | ||
|
||
const inline vector<float, 4> dst(vector<float, 4> src0, | ||
farzonl marked this conversation as resolved.
Show resolved
Hide resolved
|
||
vector<float, 4> src1) { | ||
return __detail::dst_impl(src0, src1); | ||
} | ||
|
||
const inline vector<double, 4> dst(vector<double, 4> src0, | ||
farzonl marked this conversation as resolved.
Show resolved
Hide resolved
|
||
vector<double, 4> src1) { | ||
return __detail::dst_impl(src0, src1); | ||
} | ||
|
||
//===----------------------------------------------------------------------===// | ||
// fmod builtins | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
// RUN: %clang_cc1 -finclude-default-header -x hlsl -triple dxil-pc-shadermodel6.2-library %s -fnative-half-type -emit-llvm -O1 -o - | FileCheck %s | ||
farzonl marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
|
||
// CHECK-LABEL: define noundef nofpclass(nan inf) <4 x float> @_Z12dstWithFloatDv4_fS_( | ||
// CHECK-SAME: <4 x float> noundef nofpclass(nan inf) [[P:%.*]], <4 x float> noundef nofpclass(nan inf) [[Q:%.*]]) local_unnamed_addr #[[ATTR0:[0-9]+]] { | ||
// CHECK: [[VECEXT:%.*]] = extractelement <4 x float> [[P]], i64 1 | ||
// CHECK-NEXT: [[VECEXT1:%.*]] = extractelement <4 x float> [[Q]], i64 1 | ||
// CHECK-NEXT: [[MULRES:%.*]] = fmul reassoc nnan ninf nsz arcp afn float [[VECEXT1]], [[VECEXT]] | ||
// CHECK-NEXT: [[VECINIT:%.*]] = insertelement <4 x float> <float 1.000000e+00, float poison, float poison, float poison>, float [[MULRES]], i64 1 | ||
// CHECK-NEXT: [[VECINIT3:%.*]] = shufflevector <4 x float> [[VECINIT]], <4 x float> [[P]], <4 x i32> <i32 0, i32 1, i32 6, i32 poison> | ||
// CHECK-NEXT: [[VECINIT5:%.*]] = shufflevector <4 x float> [[VECINIT3]], <4 x float> [[Q]], <4 x i32> <i32 0, i32 1, i32 2, i32 7> | ||
// CHECK-NEXT: ret <4 x float> [[VECINIT5]] | ||
|
||
float4 dstWithFloat(float4 p1, float4 p2) | ||
{ | ||
return dst(p1, p2); | ||
} | ||
|
||
// CHECK-LABEL: define noundef nofpclass(nan inf) <4 x half> @_Z11dstwithHalfDv4_DhS_( | ||
// CHECK-SAME: <4 x half> noundef nofpclass(nan inf) [[P:%.*]], <4 x half> noundef nofpclass(nan inf) [[Q:%.*]]) local_unnamed_addr #[[ATTR0]] { | ||
// CHECK: [[VECEXT:%.*]] = extractelement <4 x half> [[P]], i64 1 | ||
// CHECK-NEXT: [[VECEXT1:%.*]] = extractelement <4 x half> [[Q]], i64 1 | ||
// CHECK-NEXT: [[MULRES:%.*]] = fmul reassoc nnan ninf nsz arcp afn half [[VECEXT1]], [[VECEXT]] | ||
// CHECK-NEXT: [[VECINIT:%.*]] = insertelement <4 x half> <half 0xH3C00, half poison, half poison, half poison>, half [[MULRES]], i64 1 | ||
// CHECK-NEXT: [[VECINIT3:%.*]] = shufflevector <4 x half> [[VECINIT]], <4 x half> [[P]], <4 x i32> <i32 0, i32 1, i32 6, i32 poison> | ||
// CHECK-NEXT: [[VECINIT5:%.*]] = shufflevector <4 x half> [[VECINIT3]], <4 x half> [[Q]], <4 x i32> <i32 0, i32 1, i32 2, i32 7> | ||
// CHECK-NEXT: ret <4 x half> [[VECINIT5]] | ||
half4 dstwithHalf(half4 p1, half4 p2) | ||
{ | ||
return dst(p1, p2); | ||
} | ||
|
||
// CHECK-LABEL: define noundef nofpclass(nan inf) <4 x double> @_Z13dstWithDoubleDv4_dS_( | ||
// CHECK-SAME: <4 x double> noundef nofpclass(nan inf) [[P:%.*]], <4 x double> noundef nofpclass(nan inf) [[Q:%.*]]) local_unnamed_addr #[[ATTR0]] { | ||
// CHECK: [[VECEXT:%.*]] = extractelement <4 x double> [[P]], i64 1 | ||
// CHECK-NEXT: [[VECEXT1:%.*]] = extractelement <4 x double> [[Q]], i64 1 | ||
// CHECK-NEXT: [[MULRES:%.*]] = fmul reassoc nnan ninf nsz arcp afn double [[VECEXT1]], [[VECEXT]] | ||
// CHECK-NEXT: [[VECINIT:%.*]] = insertelement <4 x double> <double 1.000000e+00, double poison, double poison, double poison>, double [[MULRES]], i64 1 | ||
// CHECK-NEXT: [[VECINIT3:%.*]] = shufflevector <4 x double> [[VECINIT]], <4 x double> [[P]], <4 x i32> <i32 0, i32 1, i32 6, i32 poison> | ||
// CHECK-NEXT: [[VECINIT5:%.*]] = shufflevector <4 x double> [[VECINIT3]], <4 x double> [[Q]], <4 x i32> <i32 0, i32 1, i32 2, i32 7> | ||
// CHECK-NEXT: ret <4 x double> [[VECINIT5]] | ||
double4 dstWithDouble(double4 p1, double4 p2) | ||
{ | ||
return dst(p1, p2); | ||
} | ||
farzonl marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
// CHECK-LABEL: define noundef nofpclass(nan inf) <4 x float> @_Z9testfloatff( | ||
// CHECK-SAME: float noundef nofpclass(nan inf) [[P:%.*]], float noundef nofpclass(nan inf) [[Q:%.*]]) local_unnamed_addr #[[ATTR0]] { | ||
// CHECK: [[MULRES:%.*]] = fmul reassoc nnan ninf nsz arcp afn float [[Q]], [[P]] | ||
// CHECK-NEXT: [[VECINIT:%.*]] = insertelement <4 x float> <float 1.000000e+00, float poison, float poison, float poison>, float [[MULRES]], i64 1 | ||
// CHECK-NEXT: [[VECINIT3:%.*]] = insertelement <4 x float> [[VECINIT]], float [[P]], i64 2 | ||
// CHECK-NEXT: [[VECINIT5:%.*]] = insertelement <4 x float> [[VECINIT3]], float [[Q]], i64 3 | ||
// CHECK-NEXT: ret <4 x float> [[VECINIT5]] | ||
float4 testfloat(float a, float b) | ||
{ | ||
return dst(a, b); | ||
} | ||
|
||
// CHECK-LABEL: define noundef nofpclass(nan inf) <4 x float> @_Z10testfloat4fDv4_f( | ||
// CHECK-SAME: float noundef nofpclass(nan inf) [[P:%.*]], <4 x float> noundef nofpclass(nan inf) [[Q:%.*]]) local_unnamed_addr #[[ATTR0]] { | ||
// CHECK: [[VECEXT1:%.*]] = extractelement <4 x float> [[Q:%.*]], i64 1 | ||
// CHECK-NEXT: [[MULRES:%.*]] = fmul reassoc nnan ninf nsz arcp afn float [[VECEXT1]], [[P]] | ||
// CHECK-NEXT: [[VECINIT:%.*]] = insertelement <4 x float> <float 1.000000e+00, float poison, float poison, float poison>, float [[MULRES]], i64 1 | ||
// CHECK-NEXT: [[VECINIT3:%.*]] = insertelement <4 x float> [[VECINIT]], float %a, i64 2 | ||
// CHECK-NEXT: [[VECINIT5:%.*]] = shufflevector <4 x float> [[VECINIT3]], <4 x float> [[Q]], <4 x i32> <i32 0, i32 1, i32 2, i32 7> | ||
// CHECK-NEXT: ret <4 x float> [[VECINIT5]] | ||
float4 testfloat4(float a, float4 b) | ||
{ | ||
return dst(a, b); | ||
} | ||
|
||
// CHECK-LABEL: define noundef nofpclass(nan inf) <4 x half> @_Z21testRetTypeShriinkingDv4_fS_( | ||
// CHECK-SAME: <4 x float> noundef nofpclass(nan inf) [[P:%.*]], <4 x float> noundef nofpclass(nan inf) [[Q:%.*]]) local_unnamed_addr #[[ATTR0]] { | ||
// CHECK: [[VECEXT:%.*]] = extractelement <4 x float> [[P]], i64 1 | ||
// CHECK-NEXT: [[VECEXT1:%.*]] = extractelement <4 x float> [[Q]], i64 1 | ||
// CHECK-NEXT: [[MULRES:%.*]] = fmul reassoc nnan ninf nsz arcp afn float [[VECEXT1]], [[VECEXT]] | ||
// CHECK-NEXT: [[VECINIT:%.*]] = insertelement <4 x float> <float 1.000000e+00, float poison, float poison, float poison>, float [[MULRES]], i64 1 | ||
// CHECK-NEXT: [[VECINIT3:%.*]] = shufflevector <4 x float> [[VECINIT]], <4 x float> %a, <4 x i32> <i32 0, i32 1, i32 6, i32 poison> | ||
// CHECK-NEXT: [[VECINIT5:%.*]] = shufflevector <4 x float> %vecinit3.i, <4 x float> %b, <4 x i32> <i32 0, i32 1, i32 2, i32 7> | ||
// CHECK-NEXT: [[CONV:%.*]] = fptrunc reassoc nnan ninf nsz arcp afn <4 x float> [[VECINIT5]] to <4 x half> | ||
// CHECK-NEXT: ret <4 x half> [[CONV]] | ||
half4 testRetTypeShriinking(float4 a, float4 b) | ||
farzonl marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
return dst(a, b); | ||
} |
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There are no code changes to Sema in this PR, so this test is only testing existing functionality that is well tested within clang. We should remove it so as to not increase testing time without adding meaningful test coverage. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I have removed the dst-error.hlsl file as suggested. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
// RUN: %clang_cc1 -finclude-default-header -triple dxil-pc-shadermodel6.6-library %s -fnative-half-type -emit-llvm-only -disable-llvm-passes -verify | ||
|
||
float4 test_too_many_arg(float4 p0) | ||
{ | ||
V-FEXrt marked this conversation as resolved.
Show resolved
Hide resolved
|
||
dst(p0, p0, p0); | ||
// expected-error@-1 {{no matching function for call to 'dst'}} | ||
// expected-note@hlsl/hlsl_intrinsics.h:* {{candidate function not viable: requires 2 arguments, but 3 were provided}} | ||
// expected-note@hlsl/hlsl_intrinsics.h:* {{candidate function not viable: requires 2 arguments, but 3 were provided}} | ||
// expected-note@hlsl/hlsl_intrinsics.h:* {{candidate function not viable: requires 2 arguments, but 3 were provided}} | ||
} | ||
|
||
float4 test_no_second_arg(float4 p0) | ||
{ | ||
return dst(p0); | ||
// expected-error@-1 {{no matching function for call to 'dst'}} | ||
// expected-note@hlsl/hlsl_intrinsics.h:* {{candidate function not viable: requires 2 arguments, but 1 was provided}} | ||
// expected-note@hlsl/hlsl_intrinsics.h:* {{candidate function not viable: requires 2 arguments, but 1 was provided}} | ||
// expected-note@hlsl/hlsl_intrinsics.h:* {{candidate function not viable: requires 2 arguments, but 1 was provided}} | ||
} | ||
|
||
float4 test_no_args() | ||
{ | ||
return dst(); | ||
// expected-error@-1 {{no matching function for call to 'dst'}} | ||
// expected-note@hlsl/hlsl_intrinsics.h:* {{candidate function not viable: requires 2 arguments, but 0 were provided}} | ||
// expected-note@hlsl/hlsl_intrinsics.h:* {{candidate function not viable: requires 2 arguments, but 0 were provided}} | ||
// expected-note@hlsl/hlsl_intrinsics.h:* {{candidate function not viable: requires 2 arguments, but 0 were provided}} | ||
} | ||
|
||
float4 test_3_components(float3 p0, float3 p1) | ||
{ | ||
return dst(p0, p1); | ||
// expected-error@-1 {{no matching function for call to 'dst'}} | ||
// expected-note@hlsl/hlsl_intrinsics.h:* {{candidate function not viable: no known conversion from 'vector<[...], 3>' to 'vector<[...], 4>' for 1st argument}} | ||
// expected-note@hlsl/hlsl_intrinsics.h:* {{candidate function not viable: no known conversion from 'vector<float, 3>' to 'vector<half, 4>' for 1st argument}} | ||
// expected-note@hlsl/hlsl_intrinsics.h:* {{candidate function not viable: no known conversion from 'vector<float, 3>' to 'vector<double, 4>' for 1st argument}} | ||
} | ||
farzonl marked this conversation as resolved.
Show resolved
Hide resolved
farzonl marked this conversation as resolved.
Show resolved
Hide resolved
farzonl marked this conversation as resolved.
Show resolved
Hide resolved
farzonl marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
float4 test_float2(double4 p0, float4 p1) | ||
farzonl marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
return dst(p0, p1); | ||
// expected-error@-1 {{call to 'dst' is ambiguous}} | ||
// expected-note@hlsl/hlsl_intrinsics.h:* {{candidate function}} | ||
// expected-note@hlsl/hlsl_intrinsics.h:* {{candidate function}} | ||
} | ||
|
||
float2 test_float2(float4 p0, float4 p1) | ||
farzonl marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
return dst(p0, p1); | ||
// expected-warning@-1 {{implicit conversion truncates vector: 'vector<float, 4>' (vector of 4 'float' values) to 'vector<float, 2>' (vector of 2 'float' values)}} | ||
} |
Uh oh!
There was an error while loading. Please reload this page.