[clang] [HLSL] Use icmp-ne instead of truncation on boolean load (PR #221105)

Dan Brown via cfe-commits cfe-commits at lists.llvm.org
Tue Sep 29 18:03:39 PDT 2026


https://github.com/danbrown-amd updated https://github.com/llvm/llvm-project/pull/221105

>From b10a9e04b70dc840f1aa94c8d2a3df46bf4086a3 Mon Sep 17 00:00:00 2001
From: danbrown-amd <danbrown at amd.com>
Date: Thu, 3 Sep 2026 18:46:23 -0600
Subject: [PATCH] Use icmp-ne instead of truncation on boolean load

Fixes #140824

Assisted-by: Claude Sonnet 4
---
 clang/lib/Frontend/CompilerInvocation.cpp | 6 ++++++
 clang/test/CodeGenHLSL/strict-bool.hlsl   | 2 +-
 2 files changed, 7 insertions(+), 1 deletion(-)

diff --git a/clang/lib/Frontend/CompilerInvocation.cpp b/clang/lib/Frontend/CompilerInvocation.cpp
index 649a1a18b4ebe..642220fa98ed2 100644
--- a/clang/lib/Frontend/CompilerInvocation.cpp
+++ b/clang/lib/Frontend/CompilerInvocation.cpp
@@ -653,6 +653,12 @@ static bool FixupInvocation(CompilerInvocation &Invocation,
       Warnings.insert(Warnings.begin(), "vector-conversion");
     if (!llvm::is_contained(Warnings, "matrix-conversion"))
       Warnings.insert(Warnings.begin(), "matrix-conversion");
+
+    // HLSL booleans in memory are i32; any non-zero value is true.
+    // Use icmp-ne rather than truncation to avoid InstCombine rewriting
+    // trunc i32 to i1 into an illegal wide <N x i1> bitcast (issue #140824).
+    if (!Args.hasArg(OPT_load_bool_from_mem))
+      CodeGenOpts.setLoadBoolFromMem(CodeGenOptions::BoolFromMem::NonZero);
   }
 
   // When these options are used, the compiler is allowed to apply
diff --git a/clang/test/CodeGenHLSL/strict-bool.hlsl b/clang/test/CodeGenHLSL/strict-bool.hlsl
index 31867065e77a3..30bdc02231fa5 100644
--- a/clang/test/CodeGenHLSL/strict-bool.hlsl
+++ b/clang/test/CodeGenHLSL/strict-bool.hlsl
@@ -1,6 +1,6 @@
 // RUN: %clang_cc1 -triple dxil-pc-shadermodel6.3-library -O1 -load-bool-from-mem=truncate -emit-llvm -o - %s | FileCheck %s -check-prefixes=CHECK-TRUNCATE
 // RUN: %clang_cc1 -triple dxil-pc-shadermodel6.3-library -O1 -load-bool-from-mem=nonzero -emit-llvm -o - %s | FileCheck %s -check-prefixes=CHECK-NONZERO
-// RUN: %clang_cc1 -triple dxil-pc-shadermodel6.3-library -O0 -emit-llvm -o - %s | FileCheck %s -check-prefixes=CHECK-NONZERO
+// RUN: %clang_cc1 -triple dxil-pc-shadermodel6.3-library -O1 -emit-llvm -o - %s | FileCheck %s -check-prefixes=CHECK-NONZERO
 
 typedef bool bool8_t __attribute__((ext_vector_type(8)));
 extern bool8_t vec;



More information about the cfe-commits mailing list