-
Notifications
You must be signed in to change notification settings - Fork 12.6k
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
Implementing asfloat
using bit_cast
#108686
Merged
Merged
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
a405059
to
b52c5ec
Compare
@llvm/pr-subscribers-backend-x86 @llvm/pr-subscribers-clang Author: None (joaosaffran) ChangesThis PR is implementing Fixes: #70098 Full diff: https://github.com/llvm/llvm-project/pull/108686.diff 3 Files Affected:
diff --git a/clang/lib/Headers/hlsl/hlsl_intrinsics.h b/clang/lib/Headers/hlsl/hlsl_intrinsics.h
index b5c22f7c91b2d6..cb8e1119e9b4e8 100644
--- a/clang/lib/Headers/hlsl/hlsl_intrinsics.h
+++ b/clang/lib/Headers/hlsl/hlsl_intrinsics.h
@@ -361,6 +361,23 @@ bool any(double3);
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_any)
bool any(double4);
+//===----------------------------------------------------------------------===//
+// asfloat builtins
+//===----------------------------------------------------------------------===//
+
+/// \fn uint asfloat(T Val)
+/// \brief Interprets the bit pattern of x as an unsigned integer.
+/// \param Val The input value.
+
+template <typename T, int N>
+_HLSL_INLINE vector<float, N> asfloat(vector<T, N> V) {
+ return __detail::bit_cast<float, T, N>(V);
+}
+
+template <typename T> _HLSL_INLINE float asfloat(T F) {
+ return __detail::bit_cast<float, T>(F);
+}
+
//===----------------------------------------------------------------------===//
// asin builtins
//===----------------------------------------------------------------------===//
diff --git a/clang/test/CodeGenHLSL/builtins/asfloat.hlsl b/clang/test/CodeGenHLSL/builtins/asfloat.hlsl
new file mode 100644
index 00000000000000..59fc15fa60b1e5
--- /dev/null
+++ b/clang/test/CodeGenHLSL/builtins/asfloat.hlsl
@@ -0,0 +1,40 @@
+// 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: define {{.*}}test_uint{{.*}}(i32 {{.*}} [[VAL:%.*]]){{.*}}
+// CHECK: bitcast i32 [[VAL]] to float
+float test_uint(uint p0) {
+ return asfloat(p0);
+}
+
+// CHECK: define {{.*}}test_int{{.*}}(i32 {{.*}} [[VAL:%.*]]){{.*}}
+// CHECK: bitcast i32 [[VAL]] to float
+float test_int(int p0) {
+ return asfloat(p0);
+}
+
+// CHECK: define {{.*}}test_float{{.*}}(float {{.*}} [[VAL:%.*]]){{.*}}
+// CHECK-NOT: bitcast
+// CHECK: ret float [[VAL]]
+float test_float(float p0) {
+ return asfloat(p0);
+}
+
+// CHECK: define {{.*}}test_vector_uint{{.*}}(<4 x i32> {{.*}} [[VAL:%.*]]){{.*}}
+// CHECK: bitcast <4 x i32> [[VAL]] to <4 x float>
+
+float4 test_vector_uint(uint4 p0) {
+ return asfloat(p0);
+}
+
+// CHECK: define {{.*}}test_vector_int{{.*}}(<4 x i32> {{.*}} [[VAL:%.*]]){{.*}}
+// CHECK: bitcast <4 x i32> [[VAL]] to <4 x float>
+float4 test_vector_int(int4 p0) {
+ return asfloat(p0);
+}
+
+// CHECK: define {{.*}}test_vector_float{{.*}}(<4 x float> {{.*}} [[VAL:%.*]]){{.*}}
+// CHECK-NOT: bitcast
+// CHECK: ret <4 x float> [[VAL]]
+float4 test_vector_float(float4 p0) {
+ return asfloat(p0);
+}
diff --git a/clang/test/SemaHLSL/BuiltIns/asfloat-errors.hlsl b/clang/test/SemaHLSL/BuiltIns/asfloat-errors.hlsl
new file mode 100644
index 00000000000000..16273a513c41e6
--- /dev/null
+++ b/clang/test/SemaHLSL/BuiltIns/asfloat-errors.hlsl
@@ -0,0 +1,35 @@
+// RUN: %clang_cc1 -finclude-default-header -triple dxil-pc-shadermodel6.6-library %s -fnative-half-type -verify
+
+
+float4 test_float_too_many_arg(float p0, float p1) {
+ return asfloat(p0, p1);
+ // expected-error@-1 {{no matching function for call to 'asfloat'}}
+ // expected-note@hlsl/hlsl_intrinsics.h:* {{candidate function template not viable: requires single argument 'V', but 2 arguments were provided}}
+ // expected-note@hlsl/hlsl_intrinsics.h:* {{candidate function template not viable: requires single argument 'F', but 2 arguments were provided}}
+}
+
+
+float test_float_double(double p1) {
+ return asfloat(p1);
+ // expected-error@hlsl/hlsl_intrinsics.h:* {{no matching function for call to 'bit_cast'}}
+ // expected-note@-2 {{in instantiation of function template specialization 'hlsl::asfloat<double>'}}
+ // expected-note@hlsl/hlsl_detail.h:* {{candidate template ignored: could not match 'vector<double, N>' against 'double'}}
+ // expected-note@hlsl/hlsl_detail.h:* {{candidate template ignored: substitution failure [with U = float, T = double]: no type named 'Type'}}
+}
+
+float test_float_half(half p1) {
+ return asfloat(p1);
+ // expected-error@hlsl/hlsl_intrinsics.h:* {{no matching function for call to 'bit_cast'}}
+ // expected-note@-2 {{in instantiation of function template specialization 'hlsl::asfloat<half>'}}
+ // expected-note@hlsl/hlsl_detail.h:* {{candidate template ignored: could not match 'vector<half, N>' against 'half'}}
+ // expected-note@hlsl/hlsl_detail.h:* {{candidate template ignored: substitution failure [with U = float, T = half]: no type named 'Type'}}
+}
+
+
+float test_float_half(bool p1) {
+ return asfloat(p1);
+ // expected-error@hlsl/hlsl_intrinsics.h:* {{no matching function for call to 'bit_cast'}}
+ // expected-note@-2 {{in instantiation of function template specialization 'hlsl::asfloat<bool>'}}
+ // expected-note@hlsl/hlsl_detail.h:* {{candidate template ignored: could not match 'vector<bool, N>' against 'bool'}}
+ // expected-note@hlsl/hlsl_detail.h:* {{candidate template ignored: substitution failure [with U = float, T = bool]: no type named 'Type'}}
+}
\ No newline at end of file
|
llvm-beanz
reviewed
Sep 14, 2024
b52c5ec
to
32e71ea
Compare
python3kgae
approved these changes
Sep 16, 2024
farzonl
reviewed
Sep 16, 2024
farzonl
approved these changes
Sep 16, 2024
Can someone merge this, please :) |
32e71ea
to
8a030f5
Compare
4 tasks
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Labels
backend:X86
clang:headers
Headers provided by Clang, e.g. for intrinsics
clang
Clang issues not falling into any other category
HLSL
HLSL Language Support
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR is implementing
asfloat
for HLSL.Fixes: #70098