[clang] [HLSL] Implement `asint` casting using `bit_cast` (PR #110027)

via cfe-commits cfe-commits at lists.llvm.org
Wed Sep 25 15:25:43 PDT 2024


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-hlsl

Author: Finn Plummer (inbelic)

<details>
<summary>Changes</summary>

Using clang's `__builtin_bit_cast`, implement the hlsl intrinsic `asint`.

Follows implementation details of `asuint/asfloat`.

Fixes #<!-- -->99091

---
Full diff: https://github.com/llvm/llvm-project/pull/110027.diff


3 Files Affected:

- (modified) clang/lib/Headers/hlsl/hlsl_intrinsics.h (+16) 
- (added) clang/test/CodeGenHLSL/builtins/asint.hlsl (+41) 
- (added) clang/test/SemaHLSL/BuiltIns/asint-errors.hlsl (+25) 


``````````diff
diff --git a/clang/lib/Headers/hlsl/hlsl_intrinsics.h b/clang/lib/Headers/hlsl/hlsl_intrinsics.h
index b139f9eb7d999b..9d426de4cca2a2 100644
--- a/clang/lib/Headers/hlsl/hlsl_intrinsics.h
+++ b/clang/lib/Headers/hlsl/hlsl_intrinsics.h
@@ -378,6 +378,22 @@ template <typename T> constexpr float asfloat(T F) {
   return __detail::bit_cast<float, T>(F);
 }
 
+//===----------------------------------------------------------------------===//
+// asint builtins
+//===----------------------------------------------------------------------===//
+
+/// \fn int asint(T Val)
+/// \brief Interprets the bit pattern of x as an integer.
+/// \param Val The input value.
+
+template <typename T, int N> constexpr vector<int, N> asint(vector<T, N> V) {
+  return __detail::bit_cast<int, T, N>(V);
+}
+
+template <typename T> constexpr int asint(T F) {
+  return __detail::bit_cast<int, T>(F);
+}
+
 //===----------------------------------------------------------------------===//
 // asin builtins
 //===----------------------------------------------------------------------===//
diff --git a/clang/test/CodeGenHLSL/builtins/asint.hlsl b/clang/test/CodeGenHLSL/builtins/asint.hlsl
new file mode 100644
index 00000000000000..e1d80df5015c94
--- /dev/null
+++ b/clang/test/CodeGenHLSL/builtins/asint.hlsl
@@ -0,0 +1,41 @@
+// 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_int{{.*}}(i32 {{.*}} [[VAL:%.*]]){{.*}}
+// CHECK-NOT: bitcast
+// CHECK: ret i32 [[VAL]]
+int test_int(int p0) {
+  return asint(p0);
+}
+
+// CHECK: define {{.*}}test_uint{{.*}}(i32 {{.*}} [[VAL:%.*]]){{.*}}
+// CHECK-NOT: bitcast
+// CHECK: ret i32 [[VAL]]
+int test_uint(uint p0) {
+  return asint(p0);
+}
+
+// CHECK: define {{.*}}test_float{{.*}}(float {{.*}} [[VAL:%.*]]){{.*}}
+// CHECK: bitcast float [[VAL]] to i32
+int test_float(float p0) {
+  return asint(p0);
+}
+
+// CHECK: define {{.*}}test_vector_int{{.*}}(<4 x i32> {{.*}} [[VAL:%.*]]){{.*}}
+// CHECK-NOT: bitcast
+// CHECK: ret <4 x i32> [[VAL]]
+int4 test_vector_int(int4 p0) {
+  return asint(p0);
+}
+
+// CHECK: define {{.*}}test_vector_uint{{.*}}(<4 x i32> {{.*}} [[VAL:%.*]]){{.*}}
+// CHECK-NOT: bitcast
+// CHECK: ret <4 x i32> [[VAL]]
+int4 test_vector_uint(uint4 p0) {
+  return asint(p0);
+}
+
+// CHECK: define {{.*}}test_vector_float{{.*}}(<4 x float> {{.*}} [[VAL:%.*]]){{.*}}
+// CHECK: bitcast <4 x float> [[VAL]] to <4 x i32>
+int4 test_vector_float(float4 p0) {
+  return asint(p0);
+}
diff --git a/clang/test/SemaHLSL/BuiltIns/asint-errors.hlsl b/clang/test/SemaHLSL/BuiltIns/asint-errors.hlsl
new file mode 100644
index 00000000000000..815a0c35cb04cb
--- /dev/null
+++ b/clang/test/SemaHLSL/BuiltIns/asint-errors.hlsl
@@ -0,0 +1,25 @@
+// RUN: %clang_cc1 -finclude-default-header -triple dxil-pc-shadermodel6.6-library %s -fnative-half-type -verify
+
+
+int4 test_asint_too_many_arg(float p0, float p1) {
+  return asint(p0, p1);
+  // expected-error at -1 {{no matching function for call to 'asint'}}
+  // expected-note at hlsl/hlsl_intrinsics.h:* {{candidate function template not viable: requires single argument 'V', but 2 arguments were provided}}
+  // expected-note at hlsl/hlsl_intrinsics.h:* {{candidate function template not viable: requires single argument 'F', but 2 arguments were provided}}
+}
+
+int test_asint_double(double p1) {
+    return asint(p1);
+    // expected-error at hlsl/hlsl_intrinsics.h:* {{no matching function for call to 'bit_cast'}}
+    // expected-note at -2 {{in instantiation of function template specialization 'hlsl::asint<double>'}}
+    // expected-note at hlsl/hlsl_detail.h:* {{candidate template ignored: could not match 'vector<double, N>' against 'double'}}
+    // expected-note at hlsl/hlsl_detail.h:* {{candidate template ignored: substitution failure [with U = int, T = double]: no type named 'Type'}}
+}
+
+int test_asint_half(half p1) {
+    return asint(p1);
+    // expected-error at hlsl/hlsl_intrinsics.h:* {{no matching function for call to 'bit_cast'}}
+    // expected-note at -2 {{in instantiation of function template specialization 'hlsl::asint<half>'}}
+    // expected-note at hlsl/hlsl_detail.h:* {{candidate template ignored: could not match 'vector<half, N>' against 'half'}}
+    // expected-note at hlsl/hlsl_detail.h:* {{candidate template ignored: substitution failure [with U = int, T = half]: no type named 'Type'}}
+}

``````````

</details>


https://github.com/llvm/llvm-project/pull/110027


More information about the cfe-commits mailing list