Skip to content

[HLSL] Desugar type when converting from a ConstantArrayType to an ArrayParameterType #126561

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 2 commits into from
Feb 10, 2025

Conversation

spall
Copy link
Contributor

@spall spall commented Feb 10, 2025

Desugar type when converting from a ConstantArrayType to an ArrayParameterType in getArrayParameterType
Closes #125743

@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 Feb 10, 2025
@llvmbot
Copy link
Member

llvmbot commented Feb 10, 2025

@llvm/pr-subscribers-clang

@llvm/pr-subscribers-hlsl

Author: Sarah Spall (spall)

Changes

Desugar type when converting from a ConstantArrayType to an ArrayParameterType in getArrayParameterType
Closes #125743


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

2 Files Affected:

  • (modified) clang/lib/AST/ASTContext.cpp (+1-1)
  • (added) clang/test/AST/HLSL/TypdefArrayParam.hlsl (+57)
diff --git a/clang/lib/AST/ASTContext.cpp b/clang/lib/AST/ASTContext.cpp
index de617860b70040b..e3b44bdbe3dc52f 100644
--- a/clang/lib/AST/ASTContext.cpp
+++ b/clang/lib/AST/ASTContext.cpp
@@ -3902,7 +3902,7 @@ 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);
+  const auto *ATy = cast<ConstantArrayType>(Ty.getDesugaredType(*this));
   llvm::FoldingSetNodeID ID;
   ATy->Profile(ID, *this, ATy->getElementType(), ATy->getZExtSize(),
                ATy->getSizeExpr(), ATy->getSizeModifier(),
diff --git a/clang/test/AST/HLSL/TypdefArrayParam.hlsl b/clang/test/AST/HLSL/TypdefArrayParam.hlsl
new file mode 100644
index 000000000000000..c6ae168f8406456
--- /dev/null
+++ b/clang/test/AST/HLSL/TypdefArrayParam.hlsl
@@ -0,0 +1,57 @@
+// RUN: %clang_cc1 -triple dxil-pc-shadermodel6.0-library -finclude-default-header -x hlsl -ast-dump %s | FileCheck %s
+
+typedef uint4 uint32_t4;
+typedef uint32_t4 uint32_t8[2];
+
+// CHECK-LABEL: FunctionDecl {{.*}} used Accumulate 'uint32_t (uint32_t4[2])'
+// CHECK-NEXT: ParmVarDecl {{.*}} used V 'uint32_t4[2]'
+uint32_t Accumulate(uint32_t8 V) {
+  uint32_t4 SumVec = V[0] + V[1];
+  return SumVec.x + SumVec.y + SumVec.z + SumVec.w;
+}
+
+// CHECK-LABEL: FunctionDecl {{.*}} used InOutAccu 'void (inout uint32_t4[2])'
+// CHECK-NEXT: ParmVarDecl {{.*}} used V 'uint32_t4[2]'
+// CHECK-NEXT: HLSLParamModifierAttr {{.*}} inout
+void InOutAccu(inout uint32_t8 V) {
+  uint32_t4 SumVec = V[0] + V[1];
+  V[0] = SumVec;
+}
+
+// CHECK-LABEL: call1
+// CHECK: CallExpr {{.*}} 'void'
+// CHECK-NEXT: ImplicitCastExpr {{.*}} 'void (*)(inout uint32_t4[2])' <FunctionToPointerDecay>
+// CHECK-NEXT: DeclRefExpr {{.*}} 'void (inout uint32_t4[2])' lvalue Function {{.*}} 'InOutAccu' 'void (inout uint32_t4[2])'
+// CHECK-NEXT: HLSLOutArgExpr {{.*}} 'uint32_t4[2]' lvalue inout
+// CHECK-NEXT: OpaqueValueExpr {{.*}} 'uint32_t8':'uint32_t4[2]' lvalue
+// CHECK-NEXT: DeclRefExpr {{.*}} 'uint32_t8':'uint32_t4[2]' lvalue Var {{.*}} 'B' 'uint32_t8':'uint32_t4[2]'
+// CHECK-NEXT: OpaqueValueExpr {{.*}} 'uint32_t4[2]' lvalue
+// CHECK-NEXT: ImplicitCastExpr {{.*}} 'uint32_t4[2]' <HLSLArrayRValue>
+// CHECK-NEXT: OpaqueValueExpr {{.*}} 'uint32_t8':'uint32_t4[2]' lvalue
+// CHECK-NEXT: DeclRefExpr {{.*}} 'uint32_t8':'uint32_t4[2]' lvalue Var {{.*}} 'B' 'uint32_t8':'uint32_t4[2]'
+// CHECK-NEXT: BinaryOperator {{.*}} 'uint32_t8':'uint32_t4[2]' lvalue '='
+// CHECK-NEXT: OpaqueValueExpr {{.*}} 'uint32_t8':'uint32_t4[2]' lvalue
+// CHECK-NEXT: DeclRefExpr {{.*}} 'uint32_t8':'uint32_t4[2]' lvalue Var {{.*}} 'B' 'uint32_t8':'uint32_t4[2]'
+// CHECK-NEXT: ImplicitCastExpr {{.*}} 'uint32_t4[2]' <HLSLArrayRValue>
+// CHECK-NEXT: OpaqueValueExpr {{.*}} 'uint32_t4[2]' lvalue
+// CHECK-NEXT: ImplicitCastExpr {{.*}} 'uint32_t4[2]' <HLSLArrayRValue>
+// CHECK-NEXT: OpaqueValueExpr {{.*}} 'uint32_t8':'uint32_t4[2]' lvalue
+// CHECK-NEXT: DeclRefExpr {{.*}} 'uint32_t8':'uint32_t4[2]' lvalue Var {{.*}} 'B' 'uint32_t8':'uint32_t4[2]'
+void call1() {
+  uint32_t4 A = {1,2,3,4};
+  uint32_t8 B = {A,A};
+  InOutAccu(B);
+}
+
+// CHECK-LABEL: call2
+// CHECK: VarDecl {{.*}} D 'uint32_t':'unsigned int' cinit
+// CHECK-NEXT: CallExpr {{.*}} 'uint32_t':'unsigned int'
+// CHECK-NEXT: ImplicitCastExpr {{.*}} 'uint32_t (*)(uint32_t4[2])' <FunctionToPointerDecay>
+// CHECK-NEXT: DeclRefExpr {{.*}} 'uint32_t (uint32_t4[2])' lvalue Function {{.*}} 'Accumulate' 'uint32_t (uint32_t4[2])'
+// CHECK-NEXT: ImplicitCastExpr {{.*}} 'uint4[2]' <HLSLArrayRValue>
+// CHECK-NEXT: DeclRefExpr {{.*}} 'uint4[2]' lvalue Var {{.*}} 'C' 'uint4[2]'
+void call2() {
+  uint4 A = {1,2,3,4};
+  uint4 C[2] = {A,A};
+  uint32_t D = Accumulate(C);
+}

Copy link
Contributor

@damyanp damyanp left a comment

Choose a reason for hiding this comment

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

LGTM from a code perspective. If you're confident that it's the right thing to do then yay!

Copy link
Member

@hekota hekota left a comment

Choose a reason for hiding this comment

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

LGTM!

@spall spall merged commit db1dd87 into llvm:main Feb 10, 2025
12 checks passed
Icohedron pushed a commit to Icohedron/llvm-project that referenced this pull request Feb 11, 2025
…rayParameterType (llvm#126561)

Desugar type when converting from a ConstantArrayType to an
ArrayParameterType in getArrayParameterType
Closes llvm#125743
joaosaffran pushed a commit to joaosaffran/llvm-project that referenced this pull request Feb 14, 2025
…rayParameterType (llvm#126561)

Desugar type when converting from a ConstantArrayType to an
ArrayParameterType in getArrayParameterType
Closes llvm#125743
sivan-shani pushed a commit to sivan-shani/llvm-project that referenced this pull request Feb 24, 2025
…rayParameterType (llvm#126561)

Desugar type when converting from a ConstantArrayType to an
ArrayParameterType in getArrayParameterType
Closes llvm#125743
@damyanp damyanp moved this to Closed in HLSL Support Apr 25, 2025
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
Status: Closed
Development

Successfully merging this pull request may close these issues.

[HLSL] Crash using array parameters
5 participants