[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:15:10 PDT 2026
https://github.com/danbrown-amd updated https://github.com/llvm/llvm-project/pull/221105
>From 44a9d32303295dd3399553c47ff5bcbb64f36d62 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/CodeGen/CGExpr.cpp | 5 +++--
clang/lib/Frontend/CompilerInvocation.cpp | 6 ++++++
clang/test/CodeGenHLSL/strict-bool.hlsl | 2 +-
3 files changed, 10 insertions(+), 3 deletions(-)
diff --git a/clang/lib/CodeGen/CGExpr.cpp b/clang/lib/CodeGen/CGExpr.cpp
index 25e775f8328b9..afbb1f99b0bf0 100644
--- a/clang/lib/CodeGen/CGExpr.cpp
+++ b/clang/lib/CodeGen/CGExpr.cpp
@@ -5676,9 +5676,10 @@ EmitExtVectorElementExpr(const ExtVectorElementExpr *E) {
Address VecMem = CreateMemTemp(E->getBase()->getType());
// need to zero extend an hlsl boolean vector to store it back to memory
QualType Ty = E->getBase()->getType();
- llvm::Type *LTy = convertTypeForLoadStore(Ty, Vec->getType());
- if (LTy->getScalarSizeInBits() > Vec->getType()->getScalarSizeInBits())
+ if (Ty->isExtVectorBoolType()) {
+ llvm::Type *LTy = convertTypeForLoadStore(Ty, Vec->getType());
Vec = Builder.CreateZExt(Vec, LTy);
+ }
Builder.CreateStore(Vec, VecMem);
Base = MakeAddrLValue(VecMem, Ty, AlignmentSource::Decl);
}
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