Skip to content

[HLSL] Make bool in hlsl i32 #122977

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

Merged
merged 1 commit into from
Jan 15, 2025
Merged

[HLSL] Make bool in hlsl i32 #122977

merged 1 commit into from
Jan 15, 2025

Conversation

spall
Copy link
Contributor

@spall spall commented Jan 14, 2025

make a bool's memory representation i32 in hlsl
add new test
fix broken test
Closes #122932

@llvmbot llvmbot added clang Clang issues not falling into any other category clang:frontend Language frontend issues, e.g. anything involving "Sema" HLSL HLSL Language Support labels Jan 14, 2025
@llvmbot
Copy link
Member

llvmbot commented Jan 14, 2025

@llvm/pr-subscribers-clang

@llvm/pr-subscribers-hlsl

Author: Sarah Spall (spall)

Changes

make a bool's memory representation i32 in hlsl
add new test
fix broken test
Closes #122932


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

3 Files Affected:

  • (modified) clang/lib/Basic/TargetInfo.cpp (+1)
  • (added) clang/test/CodeGenHLSL/Bool.hlsl (+12)
  • (modified) clang/test/SemaHLSL/BuiltIns/asfloat-errors.hlsl (-9)
diff --git a/clang/lib/Basic/TargetInfo.cpp b/clang/lib/Basic/TargetInfo.cpp
index 86befb1cbc74fc..c0bf4e686cf03c 100644
--- a/clang/lib/Basic/TargetInfo.cpp
+++ b/clang/lib/Basic/TargetInfo.cpp
@@ -425,6 +425,7 @@ void TargetInfo::adjust(DiagnosticsEngine &Diags, LangOptions &Opts) {
   // HLSL explicitly defines the sizes and formats of some data types, and we
   // need to conform to those regardless of what architecture you are targeting.
   if (Opts.HLSL) {
+    BoolWidth = BoolAlign = 32;
     LongWidth = LongAlign = 64;
     if (!Opts.NativeHalfType) {
       HalfFormat = &llvm::APFloat::IEEEsingle();
diff --git a/clang/test/CodeGenHLSL/Bool.hlsl b/clang/test/CodeGenHLSL/Bool.hlsl
new file mode 100644
index 00000000000000..fb0f32b11241d4
--- /dev/null
+++ b/clang/test/CodeGenHLSL/Bool.hlsl
@@ -0,0 +1,12 @@
+// RUN: %clang_cc1 -finclude-default-header -triple dxil-pc-shadermodel6.3-library -emit-llvm -disable-llvm-passes -o - %s | FileCheck %s
+
+// CHECK-LABEL: define noundef i1 {{.*}}fn{{.*}}(i1 noundef %x)
+// CHECK: [[X:%.*]] = alloca i32, align 4
+// CHECK-NEXT: [[Y:%.*]] = zext i1 {{%.*}} to i32
+// CHECK-NEXT: store i32 [[Y]], ptr [[X]], align 4
+// CHECK-NEXT: [[Z:%.*]] = load i32, ptr [[X]], align 4
+// CHECK-NEXT: [[L:%.*]] = trunc i32 [[Z]] to i1
+// CHECK-NEXT: ret i1 [[L]]
+bool fn(bool x) {
+  return x;
+}
diff --git a/clang/test/SemaHLSL/BuiltIns/asfloat-errors.hlsl b/clang/test/SemaHLSL/BuiltIns/asfloat-errors.hlsl
index c2dd9e272e0937..f5f223943b4cdd 100644
--- a/clang/test/SemaHLSL/BuiltIns/asfloat-errors.hlsl
+++ b/clang/test/SemaHLSL/BuiltIns/asfloat-errors.hlsl
@@ -24,12 +24,3 @@ float test_float_half(half p1) {
     // 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'}}
-}



float test_float_half(bool p1) {
return asfloat(p1);
Copy link
Contributor

@bob80905 bob80905 Jan 14, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Presumably, now that p1 is 32 bits, it can be cast to an int for asfloat, so this doesn't emit any errors, right?

Copy link
Contributor Author

@spall spall Jan 14, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah no errors were being emitted, causing the old test to fail (due to lack of errors).

@spall spall merged commit 07a1847 into llvm:main Jan 15, 2025
12 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang:frontend Language frontend issues, e.g. anything involving "Sema" clang Clang issues not falling into any other category HLSL HLSL Language Support
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

[HLSL] sizeof(bool) should always be 4
5 participants