[clang] [HLSL] Make it possible to assign an array from a cbuffer (PR #134174)
Sarah Spall via cfe-commits
cfe-commits at lists.llvm.org
Wed Apr 2 16:24:30 PDT 2025
https://github.com/spall created https://github.com/llvm/llvm-project/pull/134174
Update Sema Checking to always do an HLSL Array RValue cast in the case we are dealing with hlsl constant array types
Instead of comparing canonical types, compare canonical unqualified types
Add a test to show it is possible to assign an array from a cbuffer.
Closes #133767
>From 11ecff90d8d51bce41bc8063afb31bd27e3981cc Mon Sep 17 00:00:00 2001
From: Sarah Spall <sarahspall at microsoft.com>
Date: Wed, 2 Apr 2025 12:04:18 -0700
Subject: [PATCH] compare unqualified canonical types and add an hlsl array
rvalue cast
---
clang/lib/Sema/SemaExprCXX.cpp | 13 +++----------
clang/lib/Sema/SemaOverload.cpp | 10 ++++------
clang/test/CodeGenHLSL/ArrayAssignable.hlsl | 20 ++++++++++++++++++++
3 files changed, 27 insertions(+), 16 deletions(-)
diff --git a/clang/lib/Sema/SemaExprCXX.cpp b/clang/lib/Sema/SemaExprCXX.cpp
index fa492bc124abd..17e9311a631f7 100644
--- a/clang/lib/Sema/SemaExprCXX.cpp
+++ b/clang/lib/Sema/SemaExprCXX.cpp
@@ -4418,19 +4418,12 @@ Sema::PerformImplicitConversion(Expr *From, QualType ToType,
case ICK_HLSL_Array_RValue:
if (ToType->isArrayParameterType()) {
FromType = Context.getArrayParameterType(FromType);
- From = ImpCastExprToType(From, FromType, CK_HLSLArrayRValue, VK_PRValue,
- /*BasePath=*/nullptr, CCK)
- .get();
- } else { // FromType must be ArrayParameterType
- assert(FromType->isArrayParameterType() &&
- "FromType must be ArrayParameterType in ICK_HLSL_Array_RValue \
- if it is not ToType");
+ } else if (FromType->isArrayParameterType()) {
const ArrayParameterType *APT = cast<ArrayParameterType>(FromType);
FromType = APT->getConstantArrayType(Context);
- From = ImpCastExprToType(From, FromType, CK_HLSLArrayRValue, VK_PRValue,
- /*BasePath=*/nullptr, CCK)
- .get();
}
+ From = ImpCastExprToType(From, FromType, CK_HLSLArrayRValue, VK_PRValue,
+ /*BasePath=*/nullptr, CCK).get();
break;
case ICK_Function_To_Pointer:
diff --git a/clang/lib/Sema/SemaOverload.cpp b/clang/lib/Sema/SemaOverload.cpp
index 1802f8f4e1f91..950114046fe5e 100644
--- a/clang/lib/Sema/SemaOverload.cpp
+++ b/clang/lib/Sema/SemaOverload.cpp
@@ -2268,17 +2268,15 @@ static bool IsStandardConversion(Sema &S, Expr* From, QualType ToType,
// handling here.
if (ToType->isArrayParameterType()) {
FromType = S.Context.getArrayParameterType(FromType);
- SCS.First = ICK_HLSL_Array_RValue;
} else if (FromType->isArrayParameterType()) {
const ArrayParameterType *APT = cast<ArrayParameterType>(FromType);
FromType = APT->getConstantArrayType(S.Context);
- SCS.First = ICK_HLSL_Array_RValue;
- } else {
- SCS.First = ICK_Identity;
}
- if (S.Context.getCanonicalType(FromType) !=
- S.Context.getCanonicalType(ToType))
+ SCS.First = ICK_HLSL_Array_RValue;
+
+ if (FromType.getCanonicalType().getUnqualifiedType() !=
+ ToType.getCanonicalType().getUnqualifiedType())
return false;
SCS.setAllToTypes(ToType);
diff --git a/clang/test/CodeGenHLSL/ArrayAssignable.hlsl b/clang/test/CodeGenHLSL/ArrayAssignable.hlsl
index e2ff2de68ed99..ce806e1492d80 100644
--- a/clang/test/CodeGenHLSL/ArrayAssignable.hlsl
+++ b/clang/test/CodeGenHLSL/ArrayAssignable.hlsl
@@ -1,5 +1,13 @@
// RUN: %clang_cc1 -triple dxil-pc-shadermodel6.3-library -emit-llvm -disable-llvm-passes -o - %s | FileCheck %s --enable-var-scope
+
+//CHECK-DAG: [[CBLayout:%.*]] = type <{ [2 x float] }>
+//CHECK-DAG: @CBArrays.cb = global target("dx.CBuffer", target("dx.Layout", [[CBLayout]], 20, 0)) poison
+//CHECK-DAG: @c1 = external addrspace(2) global [2 x float], align 4
+cbuffer CBArrays {
+ float c1[2];
+}
+
// CHECK-LABEL: define void {{.*}}arr_assign1
// CHECK: [[Arr:%.*]] = alloca [2 x i32], align 4
// CHECK-NEXT: [[Arr2:%.*]] = alloca [2 x i32], align 4
@@ -116,3 +124,15 @@ void arr_assign7() {
int Arr2[2][2] = {{0, 0}, {1, 1}};
(Arr = Arr2)[0] = {6, 6};
}
+
+// Verify you can assign from a cbuffer array
+
+// CHECK-LABEL: define void {{.*}}arr_assign8
+// CHECK: [[C:%.*]] = alloca [2 x float], align 4
+// CHECK-NEXT: call void @llvm.memcpy.p0.p0.i32(ptr align 4 [[C]], ptr align 4 {{.*}}, i32 8, i1 false)
+// CHECK-NEXT: call void @llvm.memcpy.p0.p2.i32(ptr align 4 [[C]], ptr addrspace(2) align 4 @c1, i32 8, i1 false)
+// CHECK-NEXT: ret void
+void arr_assign8() {
+ float C[2] = {1.0, 2.0};
+ C = c1;
+}
\ No newline at end of file
More information about the cfe-commits
mailing list