[clang] [HLSL] Bug fix crash using Array Parameters when De-sugaring is the same as canonicalizing (PR #127670)

Sarah Spall via cfe-commits cfe-commits at lists.llvm.org
Tue Feb 18 09:12:44 PST 2025


https://github.com/spall created https://github.com/llvm/llvm-project/pull/127670

Fixes this crash: https://hlsl.godbolt.org/z/9aP74s4bP
Which happens because the de-sugared type is the same as the canonicalized type. 
Check if the de-sugared type is canonical before getting the ArrayParameterType of the canonical type.
Add AST test to ensure crash doesn't happen.

>From 975e1e3fa8dbf68fdc57af1353aea5aff32f5d62 Mon Sep 17 00:00:00 2001
From: Sarah Spall <sarahspall at microsoft.com>
Date: Tue, 18 Feb 2025 08:29:25 -0800
Subject: [PATCH] fix bug + test

---
 clang/lib/AST/ASTContext.cpp              |  5 +++--
 clang/test/AST/HLSL/TypdefArrayParam.hlsl | 11 +++++++++++
 2 files changed, 14 insertions(+), 2 deletions(-)

diff --git a/clang/lib/AST/ASTContext.cpp b/clang/lib/AST/ASTContext.cpp
index b1b9d56ccca9f..5aaf4ce5c9155 100644
--- a/clang/lib/AST/ASTContext.cpp
+++ b/clang/lib/AST/ASTContext.cpp
@@ -3902,7 +3902,8 @@ QualType ASTContext::getArrayParameterType(QualType Ty) const {
   if (Ty->isArrayParameterType())
     return Ty;
   assert(Ty->isConstantArrayType() && "Ty must be an array type.");
-  const auto *ATy = cast<ConstantArrayType>(Ty.getDesugaredType(*this));
+  QualType DTy = Ty.getDesugaredType(*this);
+  const auto *ATy = cast<ConstantArrayType>(DTy);
   llvm::FoldingSetNodeID ID;
   ATy->Profile(ID, *this, ATy->getElementType(), ATy->getZExtSize(),
                ATy->getSizeExpr(), ATy->getSizeModifier(),
@@ -3914,7 +3915,7 @@ QualType ASTContext::getArrayParameterType(QualType Ty) const {
     return QualType(AT, 0);
 
   QualType Canonical;
-  if (!Ty.isCanonical()) {
+  if (!DTy.isCanonical()) {
     Canonical = getArrayParameterType(getCanonicalType(Ty));
 
     // Get the new insert position for the node we care about.
diff --git a/clang/test/AST/HLSL/TypdefArrayParam.hlsl b/clang/test/AST/HLSL/TypdefArrayParam.hlsl
index c6ae168f84064..37f7a66de23a1 100644
--- a/clang/test/AST/HLSL/TypdefArrayParam.hlsl
+++ b/clang/test/AST/HLSL/TypdefArrayParam.hlsl
@@ -55,3 +55,14 @@ void call2() {
   uint4 C[2] = {A,A};
   uint32_t D = Accumulate(C);
 }
+
+typedef int Foo[2];
+
+// CHECK-LABEL: call3
+// CHECK: ArraySubscriptExpr {{.*}} 'int' lvalue
+// CHECK-NEXT: ImplicitCastExpr {{.*}} 'int *' <ArrayToPointerDecay>
+// CHECK-NEXT: DeclRefExpr {{.*}} 'int[2]' lvalue ParmVar {{.*}} 'F' 'int[2]'
+// CHECK-NEXT: IntegerLiteral {{.*}} 'int' 0
+int call3(Foo F) {
+  return F[0];
+}



More information about the cfe-commits mailing list