[clang] [HLSL] Implement array temporary support (PR #79382)

2024-04-02 Thread Haojian Wu via cfe-commits


@@ -0,0 +1,76 @@
+; ModuleID = 
'/Users/cbieneman/dev/llvm-project/clang/test/SemaHLSL/ArrayTemporary.hlsl'

hokein wrote:

IIUC, this file was added unintentionally, sent out 
https://github.com/llvm/llvm-project/pull/87346

https://github.com/llvm/llvm-project/pull/79382
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [HLSL] Implement array temporary support (PR #79382)

2024-04-02 Thread Haojian Wu via cfe-commits


@@ -0,0 +1,76 @@
+; ModuleID = 
'/Users/cbieneman/dev/llvm-project/clang/test/SemaHLSL/ArrayTemporary.hlsl'

hokein wrote:

Is this `.ll` file necessary? It doesn't look like a lit test file, no `RUN` 
commands etc.

https://github.com/llvm/llvm-project/pull/79382
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [HLSL] Implement array temporary support (PR #79382)

2024-04-01 Thread Chris B via cfe-commits

https://github.com/llvm-beanz closed 
https://github.com/llvm/llvm-project/pull/79382
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [HLSL] Implement array temporary support (PR #79382)

2024-04-01 Thread Chris B via cfe-commits

https://github.com/llvm-beanz updated 
https://github.com/llvm/llvm-project/pull/79382

>From bd74d7db681cd07fda56f26e79e047c6d1a41f6b Mon Sep 17 00:00:00 2001
From: Chris Bieneman 
Date: Thu, 29 Feb 2024 15:37:50 -0600
Subject: [PATCH 1/9] [HLSL] Pass arrays by value

HLSL constant sized array function parameters do not decay to pointers.
Instead constant sized array types are preserved as unique types for
overload resolution, template instantiation and name mangling.

This implements the change by adding a new `ArrayParameterType` which
represents a non-decaying `ConstantArrayType`. The new type behaves the
same as `ConstantArrayType` except that it does not decay to a pointer.

Values of `ConstantArrayType` in HLSL decay during overload resolution
via a new `HLSLArrayRValue` cast to `ArrayParameterType`.

`ArrayParamterType` values are passed indirectly by-value to functions
in IR generation resulting in callee generated memcpy instructions.
---
 clang/docs/HLSL/FunctionCalls.rst |  32 +++---
 clang/include/clang/AST/ASTContext.h  |   7 ++
 clang/include/clang/AST/OperationKinds.def|   3 +
 clang/include/clang/AST/RecursiveASTVisitor.h |  11 ++
 clang/include/clang/AST/Type.h|  44 +++-
 clang/include/clang/AST/TypeLoc.h |   5 +
 clang/include/clang/AST/TypeProperties.td |   7 ++
 clang/include/clang/Basic/TypeNodes.td|   1 +
 clang/include/clang/Sema/Overload.h   |   3 +
 .../clang/Serialization/TypeBitCodes.def  |   1 +
 clang/lib/AST/ASTContext.cpp  |  62 ++-
 clang/lib/AST/ASTImporter.cpp |   9 ++
 clang/lib/AST/ASTStructuralEquivalence.cpp|   1 +
 clang/lib/AST/Expr.cpp|   1 +
 clang/lib/AST/ExprConstant.cpp|   3 +
 clang/lib/AST/ItaniumMangle.cpp   |   5 +
 clang/lib/AST/MicrosoftMangle.cpp |   5 +
 clang/lib/AST/ODRHash.cpp |   4 +
 clang/lib/AST/Type.cpp|  11 ++
 clang/lib/AST/TypePrinter.cpp |  11 ++
 clang/lib/CodeGen/CGCall.cpp  |   7 ++
 clang/lib/CodeGen/CGDebugInfo.cpp |   1 +
 clang/lib/CodeGen/CGExpr.cpp  |   1 +
 clang/lib/CodeGen/CGExprAgg.cpp   |   4 +
 clang/lib/CodeGen/CGExprComplex.cpp   |   1 +
 clang/lib/CodeGen/CGExprConstant.cpp  |   1 +
 clang/lib/CodeGen/CGExprScalar.cpp|   3 +-
 clang/lib/CodeGen/CodeGenFunction.cpp |   6 +
 clang/lib/CodeGen/CodeGenTypes.cpp|   1 +
 clang/lib/CodeGen/ItaniumCXXABI.cpp   |   4 +
 clang/lib/Edit/RewriteObjCFoundationAPI.cpp   |   1 +
 clang/lib/Sema/Sema.cpp   |   1 +
 clang/lib/Sema/SemaExpr.cpp   |   5 +
 clang/lib/Sema/SemaExprCXX.cpp|   8 ++
 clang/lib/Sema/SemaInit.cpp   |   5 +-
 clang/lib/Sema/SemaLookup.cpp |   4 +
 clang/lib/Sema/SemaOverload.cpp   |  16 +++
 clang/lib/Sema/SemaTemplate.cpp   |   5 +
 clang/lib/Sema/SemaTemplateDeduction.cpp  |   3 +-
 clang/lib/Sema/SemaType.cpp   |   3 +
 clang/lib/Sema/TreeTransform.h|  17 +++
 clang/lib/Serialization/ASTReader.cpp |   4 +
 clang/lib/Serialization/ASTWriter.cpp |   4 +
 clang/lib/StaticAnalyzer/Core/ExprEngineC.cpp |   3 +-
 clang/test/CodeGenHLSL/ArrayTemporary.hlsl| 104 ++
 clang/test/SemaHLSL/ArrayParams.hlsl  |  29 +
 clang/test/SemaHLSL/ArrayTemporary.hlsl   |  92 
 clang/test/SemaHLSL/ArrayTemporary.ll |  76 +
 clang/tools/libclang/CIndex.cpp   |   1 +
 49 files changed, 609 insertions(+), 27 deletions(-)
 create mode 100644 clang/test/CodeGenHLSL/ArrayTemporary.hlsl
 create mode 100644 clang/test/SemaHLSL/ArrayParams.hlsl
 create mode 100644 clang/test/SemaHLSL/ArrayTemporary.hlsl
 create mode 100644 clang/test/SemaHLSL/ArrayTemporary.ll

diff --git a/clang/docs/HLSL/FunctionCalls.rst 
b/clang/docs/HLSL/FunctionCalls.rst
index 7317de2163f897..34f52004ec30f0 100644
--- a/clang/docs/HLSL/FunctionCalls.rst
+++ b/clang/docs/HLSL/FunctionCalls.rst
@@ -157,22 +157,24 @@ Clang Implementation
   of the changes in the prototype implementation are restoring Clang-3.7 code
   that was previously modified to its original state.
 
-The implementation in clang depends on two new AST nodes and minor extensions 
to
-Clang's existing support for Objective-C write-back arguments. The goal of this
-design is to capture the semantic details of HLSL function calls in the AST, 
and
-minimize the amount of magic that needs to occur during IR generation.
-
-The two new AST nodes are ``HLSLArrayTemporaryExpr`` and ``HLSLOutParamExpr``,
-which respectively represent the temporaries used for passing arrays by value
-and the temporaries created for function outputs.
+The implementation in clang adds a new 

[clang] [HLSL] Implement array temporary support (PR #79382)

2024-03-29 Thread Chris B via cfe-commits

https://github.com/llvm-beanz updated 
https://github.com/llvm/llvm-project/pull/79382

>From bd74d7db681cd07fda56f26e79e047c6d1a41f6b Mon Sep 17 00:00:00 2001
From: Chris Bieneman 
Date: Thu, 29 Feb 2024 15:37:50 -0600
Subject: [PATCH 1/8] [HLSL] Pass arrays by value

HLSL constant sized array function parameters do not decay to pointers.
Instead constant sized array types are preserved as unique types for
overload resolution, template instantiation and name mangling.

This implements the change by adding a new `ArrayParameterType` which
represents a non-decaying `ConstantArrayType`. The new type behaves the
same as `ConstantArrayType` except that it does not decay to a pointer.

Values of `ConstantArrayType` in HLSL decay during overload resolution
via a new `HLSLArrayRValue` cast to `ArrayParameterType`.

`ArrayParamterType` values are passed indirectly by-value to functions
in IR generation resulting in callee generated memcpy instructions.
---
 clang/docs/HLSL/FunctionCalls.rst |  32 +++---
 clang/include/clang/AST/ASTContext.h  |   7 ++
 clang/include/clang/AST/OperationKinds.def|   3 +
 clang/include/clang/AST/RecursiveASTVisitor.h |  11 ++
 clang/include/clang/AST/Type.h|  44 +++-
 clang/include/clang/AST/TypeLoc.h |   5 +
 clang/include/clang/AST/TypeProperties.td |   7 ++
 clang/include/clang/Basic/TypeNodes.td|   1 +
 clang/include/clang/Sema/Overload.h   |   3 +
 .../clang/Serialization/TypeBitCodes.def  |   1 +
 clang/lib/AST/ASTContext.cpp  |  62 ++-
 clang/lib/AST/ASTImporter.cpp |   9 ++
 clang/lib/AST/ASTStructuralEquivalence.cpp|   1 +
 clang/lib/AST/Expr.cpp|   1 +
 clang/lib/AST/ExprConstant.cpp|   3 +
 clang/lib/AST/ItaniumMangle.cpp   |   5 +
 clang/lib/AST/MicrosoftMangle.cpp |   5 +
 clang/lib/AST/ODRHash.cpp |   4 +
 clang/lib/AST/Type.cpp|  11 ++
 clang/lib/AST/TypePrinter.cpp |  11 ++
 clang/lib/CodeGen/CGCall.cpp  |   7 ++
 clang/lib/CodeGen/CGDebugInfo.cpp |   1 +
 clang/lib/CodeGen/CGExpr.cpp  |   1 +
 clang/lib/CodeGen/CGExprAgg.cpp   |   4 +
 clang/lib/CodeGen/CGExprComplex.cpp   |   1 +
 clang/lib/CodeGen/CGExprConstant.cpp  |   1 +
 clang/lib/CodeGen/CGExprScalar.cpp|   3 +-
 clang/lib/CodeGen/CodeGenFunction.cpp |   6 +
 clang/lib/CodeGen/CodeGenTypes.cpp|   1 +
 clang/lib/CodeGen/ItaniumCXXABI.cpp   |   4 +
 clang/lib/Edit/RewriteObjCFoundationAPI.cpp   |   1 +
 clang/lib/Sema/Sema.cpp   |   1 +
 clang/lib/Sema/SemaExpr.cpp   |   5 +
 clang/lib/Sema/SemaExprCXX.cpp|   8 ++
 clang/lib/Sema/SemaInit.cpp   |   5 +-
 clang/lib/Sema/SemaLookup.cpp |   4 +
 clang/lib/Sema/SemaOverload.cpp   |  16 +++
 clang/lib/Sema/SemaTemplate.cpp   |   5 +
 clang/lib/Sema/SemaTemplateDeduction.cpp  |   3 +-
 clang/lib/Sema/SemaType.cpp   |   3 +
 clang/lib/Sema/TreeTransform.h|  17 +++
 clang/lib/Serialization/ASTReader.cpp |   4 +
 clang/lib/Serialization/ASTWriter.cpp |   4 +
 clang/lib/StaticAnalyzer/Core/ExprEngineC.cpp |   3 +-
 clang/test/CodeGenHLSL/ArrayTemporary.hlsl| 104 ++
 clang/test/SemaHLSL/ArrayParams.hlsl  |  29 +
 clang/test/SemaHLSL/ArrayTemporary.hlsl   |  92 
 clang/test/SemaHLSL/ArrayTemporary.ll |  76 +
 clang/tools/libclang/CIndex.cpp   |   1 +
 49 files changed, 609 insertions(+), 27 deletions(-)
 create mode 100644 clang/test/CodeGenHLSL/ArrayTemporary.hlsl
 create mode 100644 clang/test/SemaHLSL/ArrayParams.hlsl
 create mode 100644 clang/test/SemaHLSL/ArrayTemporary.hlsl
 create mode 100644 clang/test/SemaHLSL/ArrayTemporary.ll

diff --git a/clang/docs/HLSL/FunctionCalls.rst 
b/clang/docs/HLSL/FunctionCalls.rst
index 7317de2163f897..34f52004ec30f0 100644
--- a/clang/docs/HLSL/FunctionCalls.rst
+++ b/clang/docs/HLSL/FunctionCalls.rst
@@ -157,22 +157,24 @@ Clang Implementation
   of the changes in the prototype implementation are restoring Clang-3.7 code
   that was previously modified to its original state.
 
-The implementation in clang depends on two new AST nodes and minor extensions 
to
-Clang's existing support for Objective-C write-back arguments. The goal of this
-design is to capture the semantic details of HLSL function calls in the AST, 
and
-minimize the amount of magic that needs to occur during IR generation.
-
-The two new AST nodes are ``HLSLArrayTemporaryExpr`` and ``HLSLOutParamExpr``,
-which respectively represent the temporaries used for passing arrays by value
-and the temporaries created for function outputs.
+The implementation in clang adds a new 

[clang] [HLSL] Implement array temporary support (PR #79382)

2024-03-29 Thread Cooper Partin via cfe-commits

https://github.com/coopp approved this pull request.

WOW.. This was pretty big.  I went through it twice and didn't see anything 
that jumped out.  Looks good to me.

https://github.com/llvm/llvm-project/pull/79382
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [HLSL] Implement array temporary support (PR #79382)

2024-03-29 Thread Eli Friedman via cfe-commits

https://github.com/efriedma-quic approved this pull request.

LGTM

https://github.com/llvm/llvm-project/pull/79382
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [HLSL] Implement array temporary support (PR #79382)

2024-03-29 Thread Chris B via cfe-commits

https://github.com/llvm-beanz updated 
https://github.com/llvm/llvm-project/pull/79382

>From bd74d7db681cd07fda56f26e79e047c6d1a41f6b Mon Sep 17 00:00:00 2001
From: Chris Bieneman 
Date: Thu, 29 Feb 2024 15:37:50 -0600
Subject: [PATCH 1/7] [HLSL] Pass arrays by value

HLSL constant sized array function parameters do not decay to pointers.
Instead constant sized array types are preserved as unique types for
overload resolution, template instantiation and name mangling.

This implements the change by adding a new `ArrayParameterType` which
represents a non-decaying `ConstantArrayType`. The new type behaves the
same as `ConstantArrayType` except that it does not decay to a pointer.

Values of `ConstantArrayType` in HLSL decay during overload resolution
via a new `HLSLArrayRValue` cast to `ArrayParameterType`.

`ArrayParamterType` values are passed indirectly by-value to functions
in IR generation resulting in callee generated memcpy instructions.
---
 clang/docs/HLSL/FunctionCalls.rst |  32 +++---
 clang/include/clang/AST/ASTContext.h  |   7 ++
 clang/include/clang/AST/OperationKinds.def|   3 +
 clang/include/clang/AST/RecursiveASTVisitor.h |  11 ++
 clang/include/clang/AST/Type.h|  44 +++-
 clang/include/clang/AST/TypeLoc.h |   5 +
 clang/include/clang/AST/TypeProperties.td |   7 ++
 clang/include/clang/Basic/TypeNodes.td|   1 +
 clang/include/clang/Sema/Overload.h   |   3 +
 .../clang/Serialization/TypeBitCodes.def  |   1 +
 clang/lib/AST/ASTContext.cpp  |  62 ++-
 clang/lib/AST/ASTImporter.cpp |   9 ++
 clang/lib/AST/ASTStructuralEquivalence.cpp|   1 +
 clang/lib/AST/Expr.cpp|   1 +
 clang/lib/AST/ExprConstant.cpp|   3 +
 clang/lib/AST/ItaniumMangle.cpp   |   5 +
 clang/lib/AST/MicrosoftMangle.cpp |   5 +
 clang/lib/AST/ODRHash.cpp |   4 +
 clang/lib/AST/Type.cpp|  11 ++
 clang/lib/AST/TypePrinter.cpp |  11 ++
 clang/lib/CodeGen/CGCall.cpp  |   7 ++
 clang/lib/CodeGen/CGDebugInfo.cpp |   1 +
 clang/lib/CodeGen/CGExpr.cpp  |   1 +
 clang/lib/CodeGen/CGExprAgg.cpp   |   4 +
 clang/lib/CodeGen/CGExprComplex.cpp   |   1 +
 clang/lib/CodeGen/CGExprConstant.cpp  |   1 +
 clang/lib/CodeGen/CGExprScalar.cpp|   3 +-
 clang/lib/CodeGen/CodeGenFunction.cpp |   6 +
 clang/lib/CodeGen/CodeGenTypes.cpp|   1 +
 clang/lib/CodeGen/ItaniumCXXABI.cpp   |   4 +
 clang/lib/Edit/RewriteObjCFoundationAPI.cpp   |   1 +
 clang/lib/Sema/Sema.cpp   |   1 +
 clang/lib/Sema/SemaExpr.cpp   |   5 +
 clang/lib/Sema/SemaExprCXX.cpp|   8 ++
 clang/lib/Sema/SemaInit.cpp   |   5 +-
 clang/lib/Sema/SemaLookup.cpp |   4 +
 clang/lib/Sema/SemaOverload.cpp   |  16 +++
 clang/lib/Sema/SemaTemplate.cpp   |   5 +
 clang/lib/Sema/SemaTemplateDeduction.cpp  |   3 +-
 clang/lib/Sema/SemaType.cpp   |   3 +
 clang/lib/Sema/TreeTransform.h|  17 +++
 clang/lib/Serialization/ASTReader.cpp |   4 +
 clang/lib/Serialization/ASTWriter.cpp |   4 +
 clang/lib/StaticAnalyzer/Core/ExprEngineC.cpp |   3 +-
 clang/test/CodeGenHLSL/ArrayTemporary.hlsl| 104 ++
 clang/test/SemaHLSL/ArrayParams.hlsl  |  29 +
 clang/test/SemaHLSL/ArrayTemporary.hlsl   |  92 
 clang/test/SemaHLSL/ArrayTemporary.ll |  76 +
 clang/tools/libclang/CIndex.cpp   |   1 +
 49 files changed, 609 insertions(+), 27 deletions(-)
 create mode 100644 clang/test/CodeGenHLSL/ArrayTemporary.hlsl
 create mode 100644 clang/test/SemaHLSL/ArrayParams.hlsl
 create mode 100644 clang/test/SemaHLSL/ArrayTemporary.hlsl
 create mode 100644 clang/test/SemaHLSL/ArrayTemporary.ll

diff --git a/clang/docs/HLSL/FunctionCalls.rst 
b/clang/docs/HLSL/FunctionCalls.rst
index 7317de2163f897..34f52004ec30f0 100644
--- a/clang/docs/HLSL/FunctionCalls.rst
+++ b/clang/docs/HLSL/FunctionCalls.rst
@@ -157,22 +157,24 @@ Clang Implementation
   of the changes in the prototype implementation are restoring Clang-3.7 code
   that was previously modified to its original state.
 
-The implementation in clang depends on two new AST nodes and minor extensions 
to
-Clang's existing support for Objective-C write-back arguments. The goal of this
-design is to capture the semantic details of HLSL function calls in the AST, 
and
-minimize the amount of magic that needs to occur during IR generation.
-
-The two new AST nodes are ``HLSLArrayTemporaryExpr`` and ``HLSLOutParamExpr``,
-which respectively represent the temporaries used for passing arrays by value
-and the temporaries created for function outputs.
+The implementation in clang adds a new 

[clang] [HLSL] Implement array temporary support (PR #79382)

2024-03-29 Thread Chris B via cfe-commits

https://github.com/llvm-beanz updated 
https://github.com/llvm/llvm-project/pull/79382

>From bd74d7db681cd07fda56f26e79e047c6d1a41f6b Mon Sep 17 00:00:00 2001
From: Chris Bieneman 
Date: Thu, 29 Feb 2024 15:37:50 -0600
Subject: [PATCH 1/6] [HLSL] Pass arrays by value

HLSL constant sized array function parameters do not decay to pointers.
Instead constant sized array types are preserved as unique types for
overload resolution, template instantiation and name mangling.

This implements the change by adding a new `ArrayParameterType` which
represents a non-decaying `ConstantArrayType`. The new type behaves the
same as `ConstantArrayType` except that it does not decay to a pointer.

Values of `ConstantArrayType` in HLSL decay during overload resolution
via a new `HLSLArrayRValue` cast to `ArrayParameterType`.

`ArrayParamterType` values are passed indirectly by-value to functions
in IR generation resulting in callee generated memcpy instructions.
---
 clang/docs/HLSL/FunctionCalls.rst |  32 +++---
 clang/include/clang/AST/ASTContext.h  |   7 ++
 clang/include/clang/AST/OperationKinds.def|   3 +
 clang/include/clang/AST/RecursiveASTVisitor.h |  11 ++
 clang/include/clang/AST/Type.h|  44 +++-
 clang/include/clang/AST/TypeLoc.h |   5 +
 clang/include/clang/AST/TypeProperties.td |   7 ++
 clang/include/clang/Basic/TypeNodes.td|   1 +
 clang/include/clang/Sema/Overload.h   |   3 +
 .../clang/Serialization/TypeBitCodes.def  |   1 +
 clang/lib/AST/ASTContext.cpp  |  62 ++-
 clang/lib/AST/ASTImporter.cpp |   9 ++
 clang/lib/AST/ASTStructuralEquivalence.cpp|   1 +
 clang/lib/AST/Expr.cpp|   1 +
 clang/lib/AST/ExprConstant.cpp|   3 +
 clang/lib/AST/ItaniumMangle.cpp   |   5 +
 clang/lib/AST/MicrosoftMangle.cpp |   5 +
 clang/lib/AST/ODRHash.cpp |   4 +
 clang/lib/AST/Type.cpp|  11 ++
 clang/lib/AST/TypePrinter.cpp |  11 ++
 clang/lib/CodeGen/CGCall.cpp  |   7 ++
 clang/lib/CodeGen/CGDebugInfo.cpp |   1 +
 clang/lib/CodeGen/CGExpr.cpp  |   1 +
 clang/lib/CodeGen/CGExprAgg.cpp   |   4 +
 clang/lib/CodeGen/CGExprComplex.cpp   |   1 +
 clang/lib/CodeGen/CGExprConstant.cpp  |   1 +
 clang/lib/CodeGen/CGExprScalar.cpp|   3 +-
 clang/lib/CodeGen/CodeGenFunction.cpp |   6 +
 clang/lib/CodeGen/CodeGenTypes.cpp|   1 +
 clang/lib/CodeGen/ItaniumCXXABI.cpp   |   4 +
 clang/lib/Edit/RewriteObjCFoundationAPI.cpp   |   1 +
 clang/lib/Sema/Sema.cpp   |   1 +
 clang/lib/Sema/SemaExpr.cpp   |   5 +
 clang/lib/Sema/SemaExprCXX.cpp|   8 ++
 clang/lib/Sema/SemaInit.cpp   |   5 +-
 clang/lib/Sema/SemaLookup.cpp |   4 +
 clang/lib/Sema/SemaOverload.cpp   |  16 +++
 clang/lib/Sema/SemaTemplate.cpp   |   5 +
 clang/lib/Sema/SemaTemplateDeduction.cpp  |   3 +-
 clang/lib/Sema/SemaType.cpp   |   3 +
 clang/lib/Sema/TreeTransform.h|  17 +++
 clang/lib/Serialization/ASTReader.cpp |   4 +
 clang/lib/Serialization/ASTWriter.cpp |   4 +
 clang/lib/StaticAnalyzer/Core/ExprEngineC.cpp |   3 +-
 clang/test/CodeGenHLSL/ArrayTemporary.hlsl| 104 ++
 clang/test/SemaHLSL/ArrayParams.hlsl  |  29 +
 clang/test/SemaHLSL/ArrayTemporary.hlsl   |  92 
 clang/test/SemaHLSL/ArrayTemporary.ll |  76 +
 clang/tools/libclang/CIndex.cpp   |   1 +
 49 files changed, 609 insertions(+), 27 deletions(-)
 create mode 100644 clang/test/CodeGenHLSL/ArrayTemporary.hlsl
 create mode 100644 clang/test/SemaHLSL/ArrayParams.hlsl
 create mode 100644 clang/test/SemaHLSL/ArrayTemporary.hlsl
 create mode 100644 clang/test/SemaHLSL/ArrayTemporary.ll

diff --git a/clang/docs/HLSL/FunctionCalls.rst 
b/clang/docs/HLSL/FunctionCalls.rst
index 7317de2163f897..34f52004ec30f0 100644
--- a/clang/docs/HLSL/FunctionCalls.rst
+++ b/clang/docs/HLSL/FunctionCalls.rst
@@ -157,22 +157,24 @@ Clang Implementation
   of the changes in the prototype implementation are restoring Clang-3.7 code
   that was previously modified to its original state.
 
-The implementation in clang depends on two new AST nodes and minor extensions 
to
-Clang's existing support for Objective-C write-back arguments. The goal of this
-design is to capture the semantic details of HLSL function calls in the AST, 
and
-minimize the amount of magic that needs to occur during IR generation.
-
-The two new AST nodes are ``HLSLArrayTemporaryExpr`` and ``HLSLOutParamExpr``,
-which respectively represent the temporaries used for passing arrays by value
-and the temporaries created for function outputs.
+The implementation in clang adds a new 

[clang] [HLSL] Implement array temporary support (PR #79382)

2024-03-29 Thread Eli Friedman via cfe-commits


@@ -10926,6 +10965,10 @@ QualType ASTContext::mergeTypes(QualType LHS, QualType 
RHS, bool OfBlockPointer,
 assert(LHS != RHS &&
"Equivalent pipe types should have already been handled!");
 return {};
+  case Type::ArrayParameter:
+assert(LHS != RHS &&
+   "Equivalent pipe types should have already been handled!");
+return LHS;

efriedma-quic wrote:

`return {};`?  Also, assertion text is wrong.

You could try to merge like we do for ConstantArray... but it's unlikely to 
matter, since HLSL is a C++ variant (and type merging is basically a C-only 
thing).

https://github.com/llvm/llvm-project/pull/79382
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [HLSL] Implement array temporary support (PR #79382)

2024-03-29 Thread Eli Friedman via cfe-commits


@@ -2331,6 +2332,11 @@ void CodeGenFunction::EmitVariablyModifiedType(QualType 
type) {
   type = cast(ty)->getPointeeType();
   break;
 
+case Type::ArrayParameter:

efriedma-quic wrote:

Can we just use the existing "case" for ConstantArray?

https://github.com/llvm/llvm-project/pull/79382
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [HLSL] Implement array temporary support (PR #79382)

2024-03-29 Thread Chris B via cfe-commits

https://github.com/llvm-beanz updated 
https://github.com/llvm/llvm-project/pull/79382

>From bd74d7db681cd07fda56f26e79e047c6d1a41f6b Mon Sep 17 00:00:00 2001
From: Chris Bieneman 
Date: Thu, 29 Feb 2024 15:37:50 -0600
Subject: [PATCH 1/5] [HLSL] Pass arrays by value

HLSL constant sized array function parameters do not decay to pointers.
Instead constant sized array types are preserved as unique types for
overload resolution, template instantiation and name mangling.

This implements the change by adding a new `ArrayParameterType` which
represents a non-decaying `ConstantArrayType`. The new type behaves the
same as `ConstantArrayType` except that it does not decay to a pointer.

Values of `ConstantArrayType` in HLSL decay during overload resolution
via a new `HLSLArrayRValue` cast to `ArrayParameterType`.

`ArrayParamterType` values are passed indirectly by-value to functions
in IR generation resulting in callee generated memcpy instructions.
---
 clang/docs/HLSL/FunctionCalls.rst |  32 +++---
 clang/include/clang/AST/ASTContext.h  |   7 ++
 clang/include/clang/AST/OperationKinds.def|   3 +
 clang/include/clang/AST/RecursiveASTVisitor.h |  11 ++
 clang/include/clang/AST/Type.h|  44 +++-
 clang/include/clang/AST/TypeLoc.h |   5 +
 clang/include/clang/AST/TypeProperties.td |   7 ++
 clang/include/clang/Basic/TypeNodes.td|   1 +
 clang/include/clang/Sema/Overload.h   |   3 +
 .../clang/Serialization/TypeBitCodes.def  |   1 +
 clang/lib/AST/ASTContext.cpp  |  62 ++-
 clang/lib/AST/ASTImporter.cpp |   9 ++
 clang/lib/AST/ASTStructuralEquivalence.cpp|   1 +
 clang/lib/AST/Expr.cpp|   1 +
 clang/lib/AST/ExprConstant.cpp|   3 +
 clang/lib/AST/ItaniumMangle.cpp   |   5 +
 clang/lib/AST/MicrosoftMangle.cpp |   5 +
 clang/lib/AST/ODRHash.cpp |   4 +
 clang/lib/AST/Type.cpp|  11 ++
 clang/lib/AST/TypePrinter.cpp |  11 ++
 clang/lib/CodeGen/CGCall.cpp  |   7 ++
 clang/lib/CodeGen/CGDebugInfo.cpp |   1 +
 clang/lib/CodeGen/CGExpr.cpp  |   1 +
 clang/lib/CodeGen/CGExprAgg.cpp   |   4 +
 clang/lib/CodeGen/CGExprComplex.cpp   |   1 +
 clang/lib/CodeGen/CGExprConstant.cpp  |   1 +
 clang/lib/CodeGen/CGExprScalar.cpp|   3 +-
 clang/lib/CodeGen/CodeGenFunction.cpp |   6 +
 clang/lib/CodeGen/CodeGenTypes.cpp|   1 +
 clang/lib/CodeGen/ItaniumCXXABI.cpp   |   4 +
 clang/lib/Edit/RewriteObjCFoundationAPI.cpp   |   1 +
 clang/lib/Sema/Sema.cpp   |   1 +
 clang/lib/Sema/SemaExpr.cpp   |   5 +
 clang/lib/Sema/SemaExprCXX.cpp|   8 ++
 clang/lib/Sema/SemaInit.cpp   |   5 +-
 clang/lib/Sema/SemaLookup.cpp |   4 +
 clang/lib/Sema/SemaOverload.cpp   |  16 +++
 clang/lib/Sema/SemaTemplate.cpp   |   5 +
 clang/lib/Sema/SemaTemplateDeduction.cpp  |   3 +-
 clang/lib/Sema/SemaType.cpp   |   3 +
 clang/lib/Sema/TreeTransform.h|  17 +++
 clang/lib/Serialization/ASTReader.cpp |   4 +
 clang/lib/Serialization/ASTWriter.cpp |   4 +
 clang/lib/StaticAnalyzer/Core/ExprEngineC.cpp |   3 +-
 clang/test/CodeGenHLSL/ArrayTemporary.hlsl| 104 ++
 clang/test/SemaHLSL/ArrayParams.hlsl  |  29 +
 clang/test/SemaHLSL/ArrayTemporary.hlsl   |  92 
 clang/test/SemaHLSL/ArrayTemporary.ll |  76 +
 clang/tools/libclang/CIndex.cpp   |   1 +
 49 files changed, 609 insertions(+), 27 deletions(-)
 create mode 100644 clang/test/CodeGenHLSL/ArrayTemporary.hlsl
 create mode 100644 clang/test/SemaHLSL/ArrayParams.hlsl
 create mode 100644 clang/test/SemaHLSL/ArrayTemporary.hlsl
 create mode 100644 clang/test/SemaHLSL/ArrayTemporary.ll

diff --git a/clang/docs/HLSL/FunctionCalls.rst 
b/clang/docs/HLSL/FunctionCalls.rst
index 7317de2163f897..34f52004ec30f0 100644
--- a/clang/docs/HLSL/FunctionCalls.rst
+++ b/clang/docs/HLSL/FunctionCalls.rst
@@ -157,22 +157,24 @@ Clang Implementation
   of the changes in the prototype implementation are restoring Clang-3.7 code
   that was previously modified to its original state.
 
-The implementation in clang depends on two new AST nodes and minor extensions 
to
-Clang's existing support for Objective-C write-back arguments. The goal of this
-design is to capture the semantic details of HLSL function calls in the AST, 
and
-minimize the amount of magic that needs to occur during IR generation.
-
-The two new AST nodes are ``HLSLArrayTemporaryExpr`` and ``HLSLOutParamExpr``,
-which respectively represent the temporaries used for passing arrays by value
-and the temporaries created for function outputs.
+The implementation in clang adds a new 

[clang] [HLSL] Implement array temporary support (PR #79382)

2024-03-13 Thread Chris B via cfe-commits


@@ -3173,41 +3174,46 @@ class ArrayType : public Type, public 
llvm::FoldingSetNode {
 return T->getTypeClass() == ConstantArray ||
T->getTypeClass() == VariableArray ||
T->getTypeClass() == IncompleteArray ||
-   T->getTypeClass() == DependentSizedArray;
+   T->getTypeClass() == DependentSizedArray ||
+   T->getTypeClass() == ArrayParameter;
   }
 };
 
 /// Represents the canonical version of C arrays with a specified constant 
size.
 /// For example, the canonical type for 'int A[4 + 4*100]' is a
 /// ConstantArrayType where the element type is 'int' and the size is 404.
-class ConstantArrayType final
-: public ArrayType,
-  private llvm::TrailingObjects {

llvm-beanz wrote:

My team is trying to be pedantic about tracking our work, so I've filed #85124 
to track implementing @zygoloid's suggestion. I'll try and get a PR up for that 
in the next day or two then update this PR on top of that change.

https://github.com/llvm/llvm-project/pull/79382
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [HLSL] Implement array temporary support (PR #79382)

2024-03-13 Thread Richard Smith via cfe-commits


@@ -3173,41 +3174,46 @@ class ArrayType : public Type, public 
llvm::FoldingSetNode {
 return T->getTypeClass() == ConstantArray ||
T->getTypeClass() == VariableArray ||
T->getTypeClass() == IncompleteArray ||
-   T->getTypeClass() == DependentSizedArray;
+   T->getTypeClass() == DependentSizedArray ||
+   T->getTypeClass() == ArrayParameter;
   }
 };
 
 /// Represents the canonical version of C arrays with a specified constant 
size.
 /// For example, the canonical type for 'int A[4 + 4*100]' is a
 /// ConstantArrayType where the element type is 'int' and the size is 404.
-class ConstantArrayType final
-: public ArrayType,
-  private llvm::TrailingObjects {

zygoloid wrote:

As a general principle we try to keep our AST nodes small. This change isn't 
going to make a big difference by itself, but abandoning that principle and 
making a bunch of changes like this would. I think it's worth putting in a 
small amount of effort here, but if it doesn't work out nicely then it's fine 
to remove the optimization. We can find other places to save space.

https://github.com/llvm/llvm-project/pull/79382
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [HLSL] Implement array temporary support (PR #79382)

2024-03-13 Thread Chris B via cfe-commits


@@ -4655,6 +4655,13 @@ void CodeGenFunction::EmitCallArg(CallArgList , 
const Expr *E,
 return emitWritebackArg(*this, args, CRE);
   }
 
+  // If an argument is an array paramter expression being passed through. Emit
+  // the argument to a temporary and pass the temporary as the call arg.
+  if (auto AT = dyn_cast(type)) {
+args.add(EmitAnyExprToTemp(E), type);

llvm-beanz wrote:

Ah, yea, I think I need to fix this a different way.

The case where this happens is:

```c++
void fn(float x[2]);

void call(float Arr[2]) {
  fn(Arr);
}
```

In the CallExpr to fn, the argument is an lvalue DeclRefExpr to the ParmVar.

I think the right way to do this is to insert an ImplicitCastExpr for 
CK_HLSLArrayRValue or CK_LValueToRValue.

https://github.com/llvm/llvm-project/pull/79382
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [HLSL] Implement array temporary support (PR #79382)

2024-03-13 Thread Chris B via cfe-commits


@@ -3173,41 +3174,46 @@ class ArrayType : public Type, public 
llvm::FoldingSetNode {
 return T->getTypeClass() == ConstantArray ||
T->getTypeClass() == VariableArray ||
T->getTypeClass() == IncompleteArray ||
-   T->getTypeClass() == DependentSizedArray;
+   T->getTypeClass() == DependentSizedArray ||
+   T->getTypeClass() == ArrayParameter;
   }
 };
 
 /// Represents the canonical version of C arrays with a specified constant 
size.
 /// For example, the canonical type for 'int A[4 + 4*100]' is a
 /// ConstantArrayType where the element type is 'int' and the size is 404.
-class ConstantArrayType final
-: public ArrayType,
-  private llvm::TrailingObjects {

llvm-beanz wrote:

@zygoloid do you think the optimization is important to preserve? I guessed it 
might be safe to eliminate because other array types store the `SizeExpr` 
pointer, but if it's important I can make a separate PR to change the 
implementation as you suggested and we can merge that change first.

https://github.com/llvm/llvm-project/pull/79382
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [HLSL] Implement array temporary support (PR #79382)

2024-03-12 Thread Richard Smith via cfe-commits


@@ -3173,41 +3174,46 @@ class ArrayType : public Type, public 
llvm::FoldingSetNode {
 return T->getTypeClass() == ConstantArray ||
T->getTypeClass() == VariableArray ||
T->getTypeClass() == IncompleteArray ||
-   T->getTypeClass() == DependentSizedArray;
+   T->getTypeClass() == DependentSizedArray ||
+   T->getTypeClass() == ArrayParameter;
   }
 };
 
 /// Represents the canonical version of C arrays with a specified constant 
size.
 /// For example, the canonical type for 'int A[4 + 4*100]' is a
 /// ConstantArrayType where the element type is 'int' and the size is 404.
-class ConstantArrayType final
-: public ArrayType,
-  private llvm::TrailingObjects {

zygoloid wrote:

The size expression is usually not part of a constant array type. Normally, all 
we want as part of the type node is the bound, and the size expression if 
needed can be found on the `ArrayTypeLoc`. The rare special case being handled 
here is that if the array bound is instantiation-dependent but not dependent, 
then we need to preserve the expression in the type (not just in the `TypeLoc`) 
because it affects (for example) whether two template declarations involving 
such a type are considered to declare the same template. Because this is a very 
rare case, and `ConstantArrayType`s can be relatively common (eg, as the types 
of string literals), it seemed preferable to not store the `Expr*` if it's not 
needed, but in absolute terms it's probably a minor difference in memory usage, 
so it's probably fine to remove this optimization.

If we want to keep the optimization, perhaps we could replace the `Size` and 
`SizeExpr` fields with a pointer union that can hold either a <= 63-bit array 
bound (the common case) or a pointer to a slab-allocated pair of `APInt` and 
`const Expr*` (in the case where the bound doesn't fit in 63 bits or the size 
expression is instantiation-dependent).

https://github.com/llvm/llvm-project/pull/79382
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [HLSL] Implement array temporary support (PR #79382)

2024-03-12 Thread Eli Friedman via cfe-commits


@@ -3173,41 +3174,46 @@ class ArrayType : public Type, public 
llvm::FoldingSetNode {
 return T->getTypeClass() == ConstantArray ||
T->getTypeClass() == VariableArray ||
T->getTypeClass() == IncompleteArray ||
-   T->getTypeClass() == DependentSizedArray;
+   T->getTypeClass() == DependentSizedArray ||
+   T->getTypeClass() == ArrayParameter;
   }
 };
 
 /// Represents the canonical version of C arrays with a specified constant 
size.
 /// For example, the canonical type for 'int A[4 + 4*100]' is a
 /// ConstantArrayType where the element type is 'int' and the size is 404.
-class ConstantArrayType final
-: public ArrayType,
-  private llvm::TrailingObjects {

efriedma-quic wrote:

It looks like the TrailingObjects optimization here was added in 772e266fb?  
Not sure what the context was; CC @zygoloid 

https://github.com/llvm/llvm-project/pull/79382
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [HLSL] Implement array temporary support (PR #79382)

2024-03-12 Thread Eli Friedman via cfe-commits


@@ -2231,7 +2231,8 @@ Value *ScalarExprEmitter::VisitCastExpr(CastExpr *CE) {
   case CK_UserDefinedConversion:
 return Visit(const_cast(E));
 
-  case CK_NoOp: {
+  case CK_NoOp:
+  case CK_HLSLArrayRValue: {

efriedma-quic wrote:

It shouldn't be possible to hit this case; a value of ArrayParameterType isn't 
a scalar.

https://github.com/llvm/llvm-project/pull/79382
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [HLSL] Implement array temporary support (PR #79382)

2024-03-12 Thread Eli Friedman via cfe-commits


@@ -500,6 +500,7 @@ ComplexPairTy ComplexExprEmitter::EmitCast(CastKind CK, 
Expr *Op,
   case CK_NoOp:
   case CK_LValueToRValue:
   case CK_UserDefinedConversion:
+  case CK_HLSLArrayRValue:

efriedma-quic wrote:

The result of an CK_HLSLArrayRValue can't be a complex type.

https://github.com/llvm/llvm-project/pull/79382
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [HLSL] Implement array temporary support (PR #79382)

2024-03-12 Thread Eli Friedman via cfe-commits


@@ -1442,6 +1445,7 @@ static bool castPreservesZero(const CastExpr *CE) {
   case CK_BitCast:
   case CK_ToUnion:
   case CK_ToVoid:
+  case CK_HLSLArrayRValue:

efriedma-quic wrote:

Probably should be false, like other lvalue-to-rvalue casts?

https://github.com/llvm/llvm-project/pull/79382
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [HLSL] Implement array temporary support (PR #79382)

2024-03-12 Thread Eli Friedman via cfe-commits

https://github.com/efriedma-quic commented:

This seems like the right direction.

https://github.com/llvm/llvm-project/pull/79382
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [HLSL] Implement array temporary support (PR #79382)

2024-03-12 Thread Eli Friedman via cfe-commits


@@ -10900,6 +10936,10 @@ QualType ASTContext::mergeTypes(QualType LHS, QualType 
RHS, bool OfBlockPointer,
 assert(LHS != RHS &&
"Equivalent pipe types should have already been handled!");
 return {};
+  case Type::ArrayParameter:
+if (RHS != LHS)

efriedma-quic wrote:

It's impossible for RHS and LHS to be equal here.

https://github.com/llvm/llvm-project/pull/79382
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [HLSL] Implement array temporary support (PR #79382)

2024-03-12 Thread Eli Friedman via cfe-commits


@@ -1126,6 +1126,7 @@ class ConstExprEmitter :
 case CK_NonAtomicToAtomic:
 case CK_NoOp:
 case CK_ConstructorConversion:
+case CK_HLSLArrayRValue:

efriedma-quic wrote:

We shouldn't accept CK_HLSLArrayRValue casts here (or if we do, we need to do 
something different with them).

https://github.com/llvm/llvm-project/pull/79382
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [HLSL] Implement array temporary support (PR #79382)

2024-03-12 Thread Eli Friedman via cfe-commits


@@ -4655,6 +4655,13 @@ void CodeGenFunction::EmitCallArg(CallArgList , 
const Expr *E,
 return emitWritebackArg(*this, args, CRE);
   }
 
+  // If an argument is an array paramter expression being passed through. Emit
+  // the argument to a temporary and pass the temporary as the call arg.
+  if (auto AT = dyn_cast(type)) {
+args.add(EmitAnyExprToTemp(E), type);

efriedma-quic wrote:

I don't think this code should be necessary?  E shouldn't be an lvalue or a 
record type, so we should just fall through to the same code at the end of the 
function, I think?

https://github.com/llvm/llvm-project/pull/79382
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [HLSL] Implement array temporary support (PR #79382)

2024-03-12 Thread Eli Friedman via cfe-commits

https://github.com/efriedma-quic edited 
https://github.com/llvm/llvm-project/pull/79382
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [HLSL] Implement array temporary support (PR #79382)

2024-03-12 Thread Chris B via cfe-commits

https://github.com/llvm-beanz updated 
https://github.com/llvm/llvm-project/pull/79382

>From 762646ef1d95efa968199156b52a60c3472a7611 Mon Sep 17 00:00:00 2001
From: Chris Bieneman 
Date: Thu, 29 Feb 2024 15:37:50 -0600
Subject: [PATCH 1/3] [HLSL] Pass arrays by value

HLSL constant sized array function parameters do not decay to pointers.
Instead constant sized array types are preserved as unique types for
overload resolution, template instantiation and name mangling.

This implements the change by adding a new `ArrayParameterType` which
represents a non-decaying `ConstantArrayType`. The new type behaves the
same as `ConstantArrayType` except that it does not decay to a pointer.

Values of `ConstantArrayType` in HLSL decay during overload resolution
via a new `HLSLArrayRValue` cast to `ArrayParameterType`.

`ArrayParamterType` values are passed indirectly by-value to functions
in IR generation resulting in callee generated memcpy instructions.
---
 clang/docs/HLSL/FunctionCalls.rst |  32 +++---
 clang/include/clang/AST/ASTContext.h  |   7 ++
 clang/include/clang/AST/OperationKinds.def|   3 +
 clang/include/clang/AST/RecursiveASTVisitor.h |  11 ++
 clang/include/clang/AST/Type.h|  57 +++---
 clang/include/clang/AST/TypeLoc.h |   5 +
 clang/include/clang/AST/TypeProperties.td |   7 ++
 clang/include/clang/Basic/TypeNodes.td|   1 +
 clang/include/clang/Sema/Overload.h   |   3 +
 .../clang/Serialization/TypeBitCodes.def  |   1 +
 clang/lib/AST/ASTContext.cpp  |  69 ++--
 clang/lib/AST/ASTImporter.cpp |   9 ++
 clang/lib/AST/ASTStructuralEquivalence.cpp|   1 +
 clang/lib/AST/Expr.cpp|   1 +
 clang/lib/AST/ExprConstant.cpp|   3 +
 clang/lib/AST/ItaniumMangle.cpp   |   5 +
 clang/lib/AST/MicrosoftMangle.cpp |   5 +
 clang/lib/AST/ODRHash.cpp |   4 +
 clang/lib/AST/Type.cpp|  13 ++-
 clang/lib/AST/TypePrinter.cpp |  11 ++
 clang/lib/CodeGen/CGCall.cpp  |   7 ++
 clang/lib/CodeGen/CGDebugInfo.cpp |   1 +
 clang/lib/CodeGen/CGExpr.cpp  |   1 +
 clang/lib/CodeGen/CGExprAgg.cpp   |   4 +
 clang/lib/CodeGen/CGExprComplex.cpp   |   1 +
 clang/lib/CodeGen/CGExprConstant.cpp  |   1 +
 clang/lib/CodeGen/CGExprScalar.cpp|   3 +-
 clang/lib/CodeGen/CodeGenFunction.cpp |   6 +
 clang/lib/CodeGen/CodeGenTypes.cpp|   1 +
 clang/lib/CodeGen/ItaniumCXXABI.cpp   |   4 +
 clang/lib/Edit/RewriteObjCFoundationAPI.cpp   |   1 +
 clang/lib/Sema/Sema.cpp   |   1 +
 clang/lib/Sema/SemaExpr.cpp   |   5 +
 clang/lib/Sema/SemaExprCXX.cpp|   8 ++
 clang/lib/Sema/SemaInit.cpp   |   5 +-
 clang/lib/Sema/SemaLookup.cpp |   4 +
 clang/lib/Sema/SemaOverload.cpp   |  16 +++
 clang/lib/Sema/SemaTemplate.cpp   |   5 +
 clang/lib/Sema/SemaTemplateDeduction.cpp  |   3 +-
 clang/lib/Sema/SemaType.cpp   |   3 +
 clang/lib/Sema/TreeTransform.h|  17 +++
 clang/lib/Serialization/ASTReader.cpp |   4 +
 clang/lib/Serialization/ASTWriter.cpp |   4 +
 clang/lib/StaticAnalyzer/Core/ExprEngineC.cpp |   3 +-
 clang/test/CodeGenHLSL/ArrayTemporary.hlsl| 104 ++
 clang/test/SemaHLSL/ArrayParams.hlsl  |  29 +
 clang/test/SemaHLSL/ArrayTemporary.hlsl   |  92 
 clang/test/SemaHLSL/ArrayTemporary.ll |  76 +
 clang/tools/libclang/CIndex.cpp   |   1 +
 49 files changed, 616 insertions(+), 42 deletions(-)
 create mode 100644 clang/test/CodeGenHLSL/ArrayTemporary.hlsl
 create mode 100644 clang/test/SemaHLSL/ArrayParams.hlsl
 create mode 100644 clang/test/SemaHLSL/ArrayTemporary.hlsl
 create mode 100644 clang/test/SemaHLSL/ArrayTemporary.ll

diff --git a/clang/docs/HLSL/FunctionCalls.rst 
b/clang/docs/HLSL/FunctionCalls.rst
index 7317de2163f897..34f52004ec30f0 100644
--- a/clang/docs/HLSL/FunctionCalls.rst
+++ b/clang/docs/HLSL/FunctionCalls.rst
@@ -157,22 +157,24 @@ Clang Implementation
   of the changes in the prototype implementation are restoring Clang-3.7 code
   that was previously modified to its original state.
 
-The implementation in clang depends on two new AST nodes and minor extensions 
to
-Clang's existing support for Objective-C write-back arguments. The goal of this
-design is to capture the semantic details of HLSL function calls in the AST, 
and
-minimize the amount of magic that needs to occur during IR generation.
-
-The two new AST nodes are ``HLSLArrayTemporaryExpr`` and ``HLSLOutParamExpr``,
-which respectively represent the temporaries used for passing arrays by value
-and the temporaries created for function outputs.
+The implementation in clang adds a 

[clang] [HLSL] Implement array temporary support (PR #79382)

2024-03-12 Thread Chris B via cfe-commits

https://github.com/llvm-beanz updated 
https://github.com/llvm/llvm-project/pull/79382

>From 762646ef1d95efa968199156b52a60c3472a7611 Mon Sep 17 00:00:00 2001
From: Chris Bieneman 
Date: Thu, 29 Feb 2024 15:37:50 -0600
Subject: [PATCH 1/2] [HLSL] Pass arrays by value

HLSL constant sized array function parameters do not decay to pointers.
Instead constant sized array types are preserved as unique types for
overload resolution, template instantiation and name mangling.

This implements the change by adding a new `ArrayParameterType` which
represents a non-decaying `ConstantArrayType`. The new type behaves the
same as `ConstantArrayType` except that it does not decay to a pointer.

Values of `ConstantArrayType` in HLSL decay during overload resolution
via a new `HLSLArrayRValue` cast to `ArrayParameterType`.

`ArrayParamterType` values are passed indirectly by-value to functions
in IR generation resulting in callee generated memcpy instructions.
---
 clang/docs/HLSL/FunctionCalls.rst |  32 +++---
 clang/include/clang/AST/ASTContext.h  |   7 ++
 clang/include/clang/AST/OperationKinds.def|   3 +
 clang/include/clang/AST/RecursiveASTVisitor.h |  11 ++
 clang/include/clang/AST/Type.h|  57 +++---
 clang/include/clang/AST/TypeLoc.h |   5 +
 clang/include/clang/AST/TypeProperties.td |   7 ++
 clang/include/clang/Basic/TypeNodes.td|   1 +
 clang/include/clang/Sema/Overload.h   |   3 +
 .../clang/Serialization/TypeBitCodes.def  |   1 +
 clang/lib/AST/ASTContext.cpp  |  69 ++--
 clang/lib/AST/ASTImporter.cpp |   9 ++
 clang/lib/AST/ASTStructuralEquivalence.cpp|   1 +
 clang/lib/AST/Expr.cpp|   1 +
 clang/lib/AST/ExprConstant.cpp|   3 +
 clang/lib/AST/ItaniumMangle.cpp   |   5 +
 clang/lib/AST/MicrosoftMangle.cpp |   5 +
 clang/lib/AST/ODRHash.cpp |   4 +
 clang/lib/AST/Type.cpp|  13 ++-
 clang/lib/AST/TypePrinter.cpp |  11 ++
 clang/lib/CodeGen/CGCall.cpp  |   7 ++
 clang/lib/CodeGen/CGDebugInfo.cpp |   1 +
 clang/lib/CodeGen/CGExpr.cpp  |   1 +
 clang/lib/CodeGen/CGExprAgg.cpp   |   4 +
 clang/lib/CodeGen/CGExprComplex.cpp   |   1 +
 clang/lib/CodeGen/CGExprConstant.cpp  |   1 +
 clang/lib/CodeGen/CGExprScalar.cpp|   3 +-
 clang/lib/CodeGen/CodeGenFunction.cpp |   6 +
 clang/lib/CodeGen/CodeGenTypes.cpp|   1 +
 clang/lib/CodeGen/ItaniumCXXABI.cpp   |   4 +
 clang/lib/Edit/RewriteObjCFoundationAPI.cpp   |   1 +
 clang/lib/Sema/Sema.cpp   |   1 +
 clang/lib/Sema/SemaExpr.cpp   |   5 +
 clang/lib/Sema/SemaExprCXX.cpp|   8 ++
 clang/lib/Sema/SemaInit.cpp   |   5 +-
 clang/lib/Sema/SemaLookup.cpp |   4 +
 clang/lib/Sema/SemaOverload.cpp   |  16 +++
 clang/lib/Sema/SemaTemplate.cpp   |   5 +
 clang/lib/Sema/SemaTemplateDeduction.cpp  |   3 +-
 clang/lib/Sema/SemaType.cpp   |   3 +
 clang/lib/Sema/TreeTransform.h|  17 +++
 clang/lib/Serialization/ASTReader.cpp |   4 +
 clang/lib/Serialization/ASTWriter.cpp |   4 +
 clang/lib/StaticAnalyzer/Core/ExprEngineC.cpp |   3 +-
 clang/test/CodeGenHLSL/ArrayTemporary.hlsl| 104 ++
 clang/test/SemaHLSL/ArrayParams.hlsl  |  29 +
 clang/test/SemaHLSL/ArrayTemporary.hlsl   |  92 
 clang/test/SemaHLSL/ArrayTemporary.ll |  76 +
 clang/tools/libclang/CIndex.cpp   |   1 +
 49 files changed, 616 insertions(+), 42 deletions(-)
 create mode 100644 clang/test/CodeGenHLSL/ArrayTemporary.hlsl
 create mode 100644 clang/test/SemaHLSL/ArrayParams.hlsl
 create mode 100644 clang/test/SemaHLSL/ArrayTemporary.hlsl
 create mode 100644 clang/test/SemaHLSL/ArrayTemporary.ll

diff --git a/clang/docs/HLSL/FunctionCalls.rst 
b/clang/docs/HLSL/FunctionCalls.rst
index 7317de2163f897..34f52004ec30f0 100644
--- a/clang/docs/HLSL/FunctionCalls.rst
+++ b/clang/docs/HLSL/FunctionCalls.rst
@@ -157,22 +157,24 @@ Clang Implementation
   of the changes in the prototype implementation are restoring Clang-3.7 code
   that was previously modified to its original state.
 
-The implementation in clang depends on two new AST nodes and minor extensions 
to
-Clang's existing support for Objective-C write-back arguments. The goal of this
-design is to capture the semantic details of HLSL function calls in the AST, 
and
-minimize the amount of magic that needs to occur during IR generation.
-
-The two new AST nodes are ``HLSLArrayTemporaryExpr`` and ``HLSLOutParamExpr``,
-which respectively represent the temporaries used for passing arrays by value
-and the temporaries created for function outputs.
+The implementation in clang adds a 

[clang] [HLSL] Implement array temporary support (PR #79382)

2024-03-12 Thread Chris B via cfe-commits

llvm-beanz wrote:

I force pushed an update here because the new implementation is basically a 
complete rewrite based on @efriedma-quic's suggestion. I think this is a 
significantly cleaner approach that more accurately models the behavior of HLSL.

https://github.com/llvm/llvm-project/pull/79382
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [HLSL] Implement array temporary support (PR #79382)

2024-03-12 Thread Chris B via cfe-commits

https://github.com/llvm-beanz edited 
https://github.com/llvm/llvm-project/pull/79382
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [HLSL] Implement array temporary support (PR #79382)

2024-03-12 Thread Chris B via cfe-commits

https://github.com/llvm-beanz updated 
https://github.com/llvm/llvm-project/pull/79382

>From 762646ef1d95efa968199156b52a60c3472a7611 Mon Sep 17 00:00:00 2001
From: Chris Bieneman 
Date: Thu, 29 Feb 2024 15:37:50 -0600
Subject: [PATCH] [HLSL] Pass arrays by value

HLSL constant sized array function parameters do not decay to pointers.
Instead constant sized array types are preserved as unique types for
overload resolution, template instantiation and name mangling.

This implements the change by adding a new `ArrayParameterType` which
represents a non-decaying `ConstantArrayType`. The new type behaves the
same as `ConstantArrayType` except that it does not decay to a pointer.

Values of `ConstantArrayType` in HLSL decay during overload resolution
via a new `HLSLArrayRValue` cast to `ArrayParameterType`.

`ArrayParamterType` values are passed indirectly by-value to functions
in IR generation resulting in callee generated memcpy instructions.
---
 clang/docs/HLSL/FunctionCalls.rst |  32 +++---
 clang/include/clang/AST/ASTContext.h  |   7 ++
 clang/include/clang/AST/OperationKinds.def|   3 +
 clang/include/clang/AST/RecursiveASTVisitor.h |  11 ++
 clang/include/clang/AST/Type.h|  57 +++---
 clang/include/clang/AST/TypeLoc.h |   5 +
 clang/include/clang/AST/TypeProperties.td |   7 ++
 clang/include/clang/Basic/TypeNodes.td|   1 +
 clang/include/clang/Sema/Overload.h   |   3 +
 .../clang/Serialization/TypeBitCodes.def  |   1 +
 clang/lib/AST/ASTContext.cpp  |  69 ++--
 clang/lib/AST/ASTImporter.cpp |   9 ++
 clang/lib/AST/ASTStructuralEquivalence.cpp|   1 +
 clang/lib/AST/Expr.cpp|   1 +
 clang/lib/AST/ExprConstant.cpp|   3 +
 clang/lib/AST/ItaniumMangle.cpp   |   5 +
 clang/lib/AST/MicrosoftMangle.cpp |   5 +
 clang/lib/AST/ODRHash.cpp |   4 +
 clang/lib/AST/Type.cpp|  13 ++-
 clang/lib/AST/TypePrinter.cpp |  11 ++
 clang/lib/CodeGen/CGCall.cpp  |   7 ++
 clang/lib/CodeGen/CGDebugInfo.cpp |   1 +
 clang/lib/CodeGen/CGExpr.cpp  |   1 +
 clang/lib/CodeGen/CGExprAgg.cpp   |   4 +
 clang/lib/CodeGen/CGExprComplex.cpp   |   1 +
 clang/lib/CodeGen/CGExprConstant.cpp  |   1 +
 clang/lib/CodeGen/CGExprScalar.cpp|   3 +-
 clang/lib/CodeGen/CodeGenFunction.cpp |   6 +
 clang/lib/CodeGen/CodeGenTypes.cpp|   1 +
 clang/lib/CodeGen/ItaniumCXXABI.cpp   |   4 +
 clang/lib/Edit/RewriteObjCFoundationAPI.cpp   |   1 +
 clang/lib/Sema/Sema.cpp   |   1 +
 clang/lib/Sema/SemaExpr.cpp   |   5 +
 clang/lib/Sema/SemaExprCXX.cpp|   8 ++
 clang/lib/Sema/SemaInit.cpp   |   5 +-
 clang/lib/Sema/SemaLookup.cpp |   4 +
 clang/lib/Sema/SemaOverload.cpp   |  16 +++
 clang/lib/Sema/SemaTemplate.cpp   |   5 +
 clang/lib/Sema/SemaTemplateDeduction.cpp  |   3 +-
 clang/lib/Sema/SemaType.cpp   |   3 +
 clang/lib/Sema/TreeTransform.h|  17 +++
 clang/lib/Serialization/ASTReader.cpp |   4 +
 clang/lib/Serialization/ASTWriter.cpp |   4 +
 clang/lib/StaticAnalyzer/Core/ExprEngineC.cpp |   3 +-
 clang/test/CodeGenHLSL/ArrayTemporary.hlsl| 104 ++
 clang/test/SemaHLSL/ArrayParams.hlsl  |  29 +
 clang/test/SemaHLSL/ArrayTemporary.hlsl   |  92 
 clang/test/SemaHLSL/ArrayTemporary.ll |  76 +
 clang/tools/libclang/CIndex.cpp   |   1 +
 49 files changed, 616 insertions(+), 42 deletions(-)
 create mode 100644 clang/test/CodeGenHLSL/ArrayTemporary.hlsl
 create mode 100644 clang/test/SemaHLSL/ArrayParams.hlsl
 create mode 100644 clang/test/SemaHLSL/ArrayTemporary.hlsl
 create mode 100644 clang/test/SemaHLSL/ArrayTemporary.ll

diff --git a/clang/docs/HLSL/FunctionCalls.rst 
b/clang/docs/HLSL/FunctionCalls.rst
index 7317de2163f897..34f52004ec30f0 100644
--- a/clang/docs/HLSL/FunctionCalls.rst
+++ b/clang/docs/HLSL/FunctionCalls.rst
@@ -157,22 +157,24 @@ Clang Implementation
   of the changes in the prototype implementation are restoring Clang-3.7 code
   that was previously modified to its original state.
 
-The implementation in clang depends on two new AST nodes and minor extensions 
to
-Clang's existing support for Objective-C write-back arguments. The goal of this
-design is to capture the semantic details of HLSL function calls in the AST, 
and
-minimize the amount of magic that needs to occur during IR generation.
-
-The two new AST nodes are ``HLSLArrayTemporaryExpr`` and ``HLSLOutParamExpr``,
-which respectively represent the temporaries used for passing arrays by value
-and the temporaries created for function outputs.
+The implementation in clang adds a new 

[clang] [HLSL] Implement array temporary support (PR #79382)

2024-01-24 Thread Chris B via cfe-commits


@@ -10524,6 +10524,11 @@ Sema::PerformCopyInitialization(const 
InitializedEntity ,
   Expr *InitE = Init.get();
   assert(InitE && "No initialization expression?");
 
+  if (LangOpts.HLSL)
+if (auto AdjTy = dyn_cast(Entity.getType()))
+  if (AdjTy->getOriginalType()->isConstantArrayType())
+InitE = HLSLArrayTemporaryExpr::Create(getASTContext(), InitE);

llvm-beanz wrote:

Ooo. I like this idea a lot. Let me go give that a try. I was trying to avoid 
completely breaking array decay completely because at some point we’re going to 
align the language more with C/C++.

https://github.com/llvm/llvm-project/pull/79382
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [HLSL] Implement array temporary support (PR #79382)

2024-01-24 Thread Eli Friedman via cfe-commits


@@ -10524,6 +10524,11 @@ Sema::PerformCopyInitialization(const 
InitializedEntity ,
   Expr *InitE = Init.get();
   assert(InitE && "No initialization expression?");
 
+  if (LangOpts.HLSL)
+if (auto AdjTy = dyn_cast(Entity.getType()))
+  if (AdjTy->getOriginalType()->isConstantArrayType())
+InitE = HLSLArrayTemporaryExpr::Create(getASTContext(), InitE);

efriedma-quic wrote:

This implementation is surprising.  If arrays are supposed to be passed by 
value, decay shouldn't be happening in the first place.

Allowing decay to happen, then trying to fix it up later, has some weird 
implications: for example, `void f(int[2])` and `void f(int[4])` have the same 
type.  You introduce pointer types in places where they shouldn't really exist, 
and casts which aren't really supposed to happen.  And it's not clear to me the 
non-canonical type wrapper will be preserved in all the cases you need 
(preserving non-canonical types is best-effort; sometimes we're forced to 
canonicalize).

As an alternative approach, maybe we can introduce a new type to represent a 
"function argument of array type".  When we construct the function type, 
instead of decaying to a pointer, it would "decay" to "function argument of 
array type". The result is sort of similar, but instead of a non-canonical 
DecayedType, you have a canonical type which would be reliably preserved across 
the compiler.  For argument passing, initialization would work similarly to 
what you're doing now.

https://github.com/llvm/llvm-project/pull/79382
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [HLSL] Implement array temporary support (PR #79382)

2024-01-24 Thread Eli Friedman via cfe-commits


@@ -3569,6 +3569,7 @@ bool Expr::HasSideEffects(const ASTContext ,
   case ConceptSpecializationExprClass:
   case RequiresExprClass:
   case SYCLUniqueStableNameExprClass:
+  case HLSLArrayTemporaryExprClass:

efriedma-quic wrote:

This looks wrong; I think you need the recursive visit here?

https://github.com/llvm/llvm-project/pull/79382
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [HLSL] Implement array temporary support (PR #79382)

2024-01-24 Thread via cfe-commits

github-actions[bot] wrote:




:warning: C/C++ code formatter, clang-format found issues in your code. 
:warning:



You can test this locally with the following command:


``bash
git-clang-format --diff 6e4930c67508a90bdfd756f6e45417b5253cd741 
d01d3dd8de6e955ad19a0ad8399547dbc59f7a52 -- clang/include/clang/AST/Expr.h 
clang/include/clang/AST/RecursiveASTVisitor.h clang/lib/AST/Expr.cpp 
clang/lib/AST/ExprClassification.cpp clang/lib/AST/ExprConstant.cpp 
clang/lib/AST/ItaniumMangle.cpp clang/lib/AST/StmtPrinter.cpp 
clang/lib/AST/StmtProfile.cpp clang/lib/CodeGen/CGExpr.cpp 
clang/lib/CodeGen/CGExprAgg.cpp clang/lib/Sema/SemaExceptionSpec.cpp 
clang/lib/Sema/SemaInit.cpp clang/lib/Sema/TreeTransform.h 
clang/lib/Serialization/ASTReaderStmt.cpp 
clang/lib/Serialization/ASTWriterStmt.cpp 
clang/lib/StaticAnalyzer/Core/ExprEngine.cpp clang/tools/libclang/CXCursor.cpp
``





View the diff from clang-format here.


``diff
diff --git a/clang/lib/AST/Expr.cpp b/clang/lib/AST/Expr.cpp
index 5903e2763d..35b80ecc39 100644
--- a/clang/lib/AST/Expr.cpp
+++ b/clang/lib/AST/Expr.cpp
@@ -5229,8 +5229,8 @@ OMPIteratorExpr *OMPIteratorExpr::CreateEmpty(const 
ASTContext ,
   return new (Mem) OMPIteratorExpr(EmptyShell(), NumIterators);
 }
 
-HLSLArrayTemporaryExpr *
-HLSLArrayTemporaryExpr::Create(const ASTContext , Expr *Base) {
+HLSLArrayTemporaryExpr *HLSLArrayTemporaryExpr::Create(const ASTContext ,
+   Expr *Base) {
   return new (Ctx) HLSLArrayTemporaryExpr(Base);
 }
 
diff --git a/clang/lib/StaticAnalyzer/Core/ExprEngine.cpp 
b/clang/lib/StaticAnalyzer/Core/ExprEngine.cpp
index 39cdec7886..da29cbeea0 100644
--- a/clang/lib/StaticAnalyzer/Core/ExprEngine.cpp
+++ b/clang/lib/StaticAnalyzer/Core/ExprEngine.cpp
@@ -1821,7 +1821,7 @@ void ExprEngine::Visit(const Stmt *S, ExplodedNode *Pred,
 case Stmt::OMPTargetParallelGenericLoopDirectiveClass:
 case Stmt::CapturedStmtClass:
 case Stmt::OMPUnrollDirectiveClass:
-case Stmt::OMPMetaDirectiveClass: 
+case Stmt::OMPMetaDirectiveClass:
 case Stmt::HLSLArrayTemporaryExprClass: {
   const ExplodedNode *node = Bldr.generateSink(S, Pred, Pred->getState());
   Engine.addAbortedBlock(node, currBldrCtx->getBlock());

``




https://github.com/llvm/llvm-project/pull/79382
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [HLSL] Implement array temporary support (PR #79382)

2024-01-24 Thread via cfe-commits

llvmbot wrote:




@llvm/pr-subscribers-clang-codegen

Author: Chris B (llvm-beanz)


Changes

In HLSL function parameters are passed by value, including array parameters. 
This change introduces a new AST node to represent array temporary expressions. 
They behave as lvalues to temporary arrays and decay to pointers for overload 
resolution and code generation.

The behavior of HLSL function calls is documented in the [draft language 
specification](https://microsoft.github.io/hlsl-specs/specs/hlsl.pdf) under the 
Expr.Post.Call heading.

Additionally the design of this implementation approach is documented in 
[Clang's documentation](https://clang.llvm.org/docs/HLSL/FunctionCalls.html)

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


20 Files Affected:

- (modified) clang/include/clang/AST/Expr.h (+38) 
- (modified) clang/include/clang/AST/RecursiveASTVisitor.h (+2) 
- (modified) clang/include/clang/Basic/StmtNodes.td (+3) 
- (modified) clang/lib/AST/Expr.cpp (+11) 
- (modified) clang/lib/AST/ExprClassification.cpp (+1) 
- (modified) clang/lib/AST/ExprConstant.cpp (+1) 
- (modified) clang/lib/AST/ItaniumMangle.cpp (+4) 
- (modified) clang/lib/AST/StmtPrinter.cpp (+4) 
- (modified) clang/lib/AST/StmtProfile.cpp (+9) 
- (modified) clang/lib/CodeGen/CGExpr.cpp (+1) 
- (modified) clang/lib/CodeGen/CGExprAgg.cpp (+6) 
- (modified) clang/lib/Sema/SemaExceptionSpec.cpp (+1) 
- (modified) clang/lib/Sema/SemaInit.cpp (+5) 
- (modified) clang/lib/Sema/TreeTransform.h (+13) 
- (modified) clang/lib/Serialization/ASTReaderStmt.cpp (+8) 
- (modified) clang/lib/Serialization/ASTWriterStmt.cpp (+8) 
- (modified) clang/lib/StaticAnalyzer/Core/ExprEngine.cpp (+2-1) 
- (added) clang/test/CodeGenHLSL/ArrayTemporary.hlsl (+49) 
- (added) clang/test/SemaHLSL/ArrayTemporary.hlsl (+46) 
- (modified) clang/tools/libclang/CXCursor.cpp (+1) 


``diff
diff --git a/clang/include/clang/AST/Expr.h b/clang/include/clang/AST/Expr.h
index 59f0aee2c0ceddf..c28747e7a3796f3 100644
--- a/clang/include/clang/AST/Expr.h
+++ b/clang/include/clang/AST/Expr.h
@@ -6651,6 +6651,44 @@ class RecoveryExpr final : public Expr,
   friend class ASTStmtWriter;
 };
 
+/// HLSLArrayTemporaryExpr - In HLSL, default parameter passing is by value
+/// including for arrays. This AST node represents a materialized temporary of 
a
+/// constant size arrray.
+class HLSLArrayTemporaryExpr : public Expr {
+  Expr *SourceExpr;
+
+  HLSLArrayTemporaryExpr(Expr *S)
+  : Expr(HLSLArrayTemporaryExprClass, S->getType(), VK_LValue, 
OK_Ordinary),
+SourceExpr(S) {}
+
+  HLSLArrayTemporaryExpr(EmptyShell Empty)
+  : Expr(HLSLArrayTemporaryExprClass, Empty), SourceExpr(nullptr) {}
+
+public:
+  static HLSLArrayTemporaryExpr *Create(const ASTContext , Expr *S);
+  static HLSLArrayTemporaryExpr *CreateEmpty(const ASTContext );
+
+  const Expr *getSourceExpr() const { return SourceExpr; }
+  Expr *getSourceExpr() { return SourceExpr; }
+  void setSourceExpr(Expr *S) { SourceExpr = S; }
+
+  SourceLocation getBeginLoc() const { return SourceExpr->getBeginLoc(); }
+
+  SourceLocation getEndLoc() const { return SourceExpr->getEndLoc(); }
+
+  static bool classof(const Stmt *T) {
+return T->getStmtClass() == HLSLArrayTemporaryExprClass;
+  }
+
+  // Iterators
+  child_range children() {
+return child_range(child_iterator(), child_iterator());
+  }
+  const_child_range children() const {
+return const_child_range(const_child_iterator(), const_child_iterator());
+  }
+};
+
 } // end namespace clang
 
 #endif // LLVM_CLANG_AST_EXPR_H
diff --git a/clang/include/clang/AST/RecursiveASTVisitor.h 
b/clang/include/clang/AST/RecursiveASTVisitor.h
index 2aee6a947141b6c..f7f805cf20c 100644
--- a/clang/include/clang/AST/RecursiveASTVisitor.h
+++ b/clang/include/clang/AST/RecursiveASTVisitor.h
@@ -3171,6 +3171,8 @@ DEF_TRAVERSE_STMT(OMPTargetParallelGenericLoopDirective,
 DEF_TRAVERSE_STMT(OMPErrorDirective,
   { TRY_TO(TraverseOMPExecutableDirective(S)); })
 
+DEF_TRAVERSE_STMT(HLSLArrayTemporaryExpr, {})
+
 // OpenMP clauses.
 template 
 bool RecursiveASTVisitor::TraverseOMPClause(OMPClause *C) {
diff --git a/clang/include/clang/Basic/StmtNodes.td 
b/clang/include/clang/Basic/StmtNodes.td
index cec301dfca2817b..3d14d33a14b8785 100644
--- a/clang/include/clang/Basic/StmtNodes.td
+++ b/clang/include/clang/Basic/StmtNodes.td
@@ -295,3 +295,6 @@ def OMPTargetTeamsGenericLoopDirective : 
StmtNode;
 def OMPParallelGenericLoopDirective : StmtNode;
 def OMPTargetParallelGenericLoopDirective : StmtNode;
 def OMPErrorDirective : StmtNode;
+
+// HLSL Extensions
+def HLSLArrayTemporaryExpr : StmtNode;
diff --git a/clang/lib/AST/Expr.cpp b/clang/lib/AST/Expr.cpp
index f1efa98e175edf5..5903e2763dbe59a 100644
--- a/clang/lib/AST/Expr.cpp
+++ b/clang/lib/AST/Expr.cpp
@@ -3569,6 +3569,7 @@ bool Expr::HasSideEffects(const ASTContext ,
   case ConceptSpecializationExprClass:
   case RequiresExprClass:
   case 

[clang] [HLSL] Implement array temporary support (PR #79382)

2024-01-24 Thread via cfe-commits

llvmbot wrote:




@llvm/pr-subscribers-hlsl

Author: Chris B (llvm-beanz)


Changes

In HLSL function parameters are passed by value, including array parameters. 
This change introduces a new AST node to represent array temporary expressions. 
They behave as lvalues to temporary arrays and decay to pointers for overload 
resolution and code generation.

The behavior of HLSL function calls is documented in the [draft language 
specification](https://microsoft.github.io/hlsl-specs/specs/hlsl.pdf) under the 
Expr.Post.Call heading.

Additionally the design of this implementation approach is documented in 
[Clang's documentation](https://clang.llvm.org/docs/HLSL/FunctionCalls.html)

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


20 Files Affected:

- (modified) clang/include/clang/AST/Expr.h (+38) 
- (modified) clang/include/clang/AST/RecursiveASTVisitor.h (+2) 
- (modified) clang/include/clang/Basic/StmtNodes.td (+3) 
- (modified) clang/lib/AST/Expr.cpp (+11) 
- (modified) clang/lib/AST/ExprClassification.cpp (+1) 
- (modified) clang/lib/AST/ExprConstant.cpp (+1) 
- (modified) clang/lib/AST/ItaniumMangle.cpp (+4) 
- (modified) clang/lib/AST/StmtPrinter.cpp (+4) 
- (modified) clang/lib/AST/StmtProfile.cpp (+9) 
- (modified) clang/lib/CodeGen/CGExpr.cpp (+1) 
- (modified) clang/lib/CodeGen/CGExprAgg.cpp (+6) 
- (modified) clang/lib/Sema/SemaExceptionSpec.cpp (+1) 
- (modified) clang/lib/Sema/SemaInit.cpp (+5) 
- (modified) clang/lib/Sema/TreeTransform.h (+13) 
- (modified) clang/lib/Serialization/ASTReaderStmt.cpp (+8) 
- (modified) clang/lib/Serialization/ASTWriterStmt.cpp (+8) 
- (modified) clang/lib/StaticAnalyzer/Core/ExprEngine.cpp (+2-1) 
- (added) clang/test/CodeGenHLSL/ArrayTemporary.hlsl (+49) 
- (added) clang/test/SemaHLSL/ArrayTemporary.hlsl (+46) 
- (modified) clang/tools/libclang/CXCursor.cpp (+1) 


``diff
diff --git a/clang/include/clang/AST/Expr.h b/clang/include/clang/AST/Expr.h
index 59f0aee2c0cedd..c28747e7a3796f 100644
--- a/clang/include/clang/AST/Expr.h
+++ b/clang/include/clang/AST/Expr.h
@@ -6651,6 +6651,44 @@ class RecoveryExpr final : public Expr,
   friend class ASTStmtWriter;
 };
 
+/// HLSLArrayTemporaryExpr - In HLSL, default parameter passing is by value
+/// including for arrays. This AST node represents a materialized temporary of 
a
+/// constant size arrray.
+class HLSLArrayTemporaryExpr : public Expr {
+  Expr *SourceExpr;
+
+  HLSLArrayTemporaryExpr(Expr *S)
+  : Expr(HLSLArrayTemporaryExprClass, S->getType(), VK_LValue, 
OK_Ordinary),
+SourceExpr(S) {}
+
+  HLSLArrayTemporaryExpr(EmptyShell Empty)
+  : Expr(HLSLArrayTemporaryExprClass, Empty), SourceExpr(nullptr) {}
+
+public:
+  static HLSLArrayTemporaryExpr *Create(const ASTContext , Expr *S);
+  static HLSLArrayTemporaryExpr *CreateEmpty(const ASTContext );
+
+  const Expr *getSourceExpr() const { return SourceExpr; }
+  Expr *getSourceExpr() { return SourceExpr; }
+  void setSourceExpr(Expr *S) { SourceExpr = S; }
+
+  SourceLocation getBeginLoc() const { return SourceExpr->getBeginLoc(); }
+
+  SourceLocation getEndLoc() const { return SourceExpr->getEndLoc(); }
+
+  static bool classof(const Stmt *T) {
+return T->getStmtClass() == HLSLArrayTemporaryExprClass;
+  }
+
+  // Iterators
+  child_range children() {
+return child_range(child_iterator(), child_iterator());
+  }
+  const_child_range children() const {
+return const_child_range(const_child_iterator(), const_child_iterator());
+  }
+};
+
 } // end namespace clang
 
 #endif // LLVM_CLANG_AST_EXPR_H
diff --git a/clang/include/clang/AST/RecursiveASTVisitor.h 
b/clang/include/clang/AST/RecursiveASTVisitor.h
index 2aee6a947141b6..f7f805cf20 100644
--- a/clang/include/clang/AST/RecursiveASTVisitor.h
+++ b/clang/include/clang/AST/RecursiveASTVisitor.h
@@ -3171,6 +3171,8 @@ DEF_TRAVERSE_STMT(OMPTargetParallelGenericLoopDirective,
 DEF_TRAVERSE_STMT(OMPErrorDirective,
   { TRY_TO(TraverseOMPExecutableDirective(S)); })
 
+DEF_TRAVERSE_STMT(HLSLArrayTemporaryExpr, {})
+
 // OpenMP clauses.
 template 
 bool RecursiveASTVisitor::TraverseOMPClause(OMPClause *C) {
diff --git a/clang/include/clang/Basic/StmtNodes.td 
b/clang/include/clang/Basic/StmtNodes.td
index cec301dfca2817..3d14d33a14b878 100644
--- a/clang/include/clang/Basic/StmtNodes.td
+++ b/clang/include/clang/Basic/StmtNodes.td
@@ -295,3 +295,6 @@ def OMPTargetTeamsGenericLoopDirective : 
StmtNode;
 def OMPParallelGenericLoopDirective : StmtNode;
 def OMPTargetParallelGenericLoopDirective : StmtNode;
 def OMPErrorDirective : StmtNode;
+
+// HLSL Extensions
+def HLSLArrayTemporaryExpr : StmtNode;
diff --git a/clang/lib/AST/Expr.cpp b/clang/lib/AST/Expr.cpp
index f1efa98e175edf..5903e2763dbe59 100644
--- a/clang/lib/AST/Expr.cpp
+++ b/clang/lib/AST/Expr.cpp
@@ -3569,6 +3569,7 @@ bool Expr::HasSideEffects(const ASTContext ,
   case ConceptSpecializationExprClass:
   case RequiresExprClass:
   case SYCLUniqueStableNameExprClass:
+  case 

[clang] [HLSL] Implement array temporary support (PR #79382)

2024-01-24 Thread via cfe-commits

llvmbot wrote:



@llvm/pr-subscribers-clang-static-analyzer-1

@llvm/pr-subscribers-clang-modules

Author: Chris B (llvm-beanz)


Changes

In HLSL function parameters are passed by value, including array parameters. 
This change introduces a new AST node to represent array temporary expressions. 
They behave as lvalues to temporary arrays and decay to pointers for overload 
resolution and code generation.

The behavior of HLSL function calls is documented in the [draft language 
specification](https://microsoft.github.io/hlsl-specs/specs/hlsl.pdf) under the 
Expr.Post.Call heading.

Additionally the design of this implementation approach is documented in 
[Clang's documentation](https://clang.llvm.org/docs/HLSL/FunctionCalls.html)

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


20 Files Affected:

- (modified) clang/include/clang/AST/Expr.h (+38) 
- (modified) clang/include/clang/AST/RecursiveASTVisitor.h (+2) 
- (modified) clang/include/clang/Basic/StmtNodes.td (+3) 
- (modified) clang/lib/AST/Expr.cpp (+11) 
- (modified) clang/lib/AST/ExprClassification.cpp (+1) 
- (modified) clang/lib/AST/ExprConstant.cpp (+1) 
- (modified) clang/lib/AST/ItaniumMangle.cpp (+4) 
- (modified) clang/lib/AST/StmtPrinter.cpp (+4) 
- (modified) clang/lib/AST/StmtProfile.cpp (+9) 
- (modified) clang/lib/CodeGen/CGExpr.cpp (+1) 
- (modified) clang/lib/CodeGen/CGExprAgg.cpp (+6) 
- (modified) clang/lib/Sema/SemaExceptionSpec.cpp (+1) 
- (modified) clang/lib/Sema/SemaInit.cpp (+5) 
- (modified) clang/lib/Sema/TreeTransform.h (+13) 
- (modified) clang/lib/Serialization/ASTReaderStmt.cpp (+8) 
- (modified) clang/lib/Serialization/ASTWriterStmt.cpp (+8) 
- (modified) clang/lib/StaticAnalyzer/Core/ExprEngine.cpp (+2-1) 
- (added) clang/test/CodeGenHLSL/ArrayTemporary.hlsl (+49) 
- (added) clang/test/SemaHLSL/ArrayTemporary.hlsl (+46) 
- (modified) clang/tools/libclang/CXCursor.cpp (+1) 


``diff
diff --git a/clang/include/clang/AST/Expr.h b/clang/include/clang/AST/Expr.h
index 59f0aee2c0cedd..c28747e7a3796f 100644
--- a/clang/include/clang/AST/Expr.h
+++ b/clang/include/clang/AST/Expr.h
@@ -6651,6 +6651,44 @@ class RecoveryExpr final : public Expr,
   friend class ASTStmtWriter;
 };
 
+/// HLSLArrayTemporaryExpr - In HLSL, default parameter passing is by value
+/// including for arrays. This AST node represents a materialized temporary of 
a
+/// constant size arrray.
+class HLSLArrayTemporaryExpr : public Expr {
+  Expr *SourceExpr;
+
+  HLSLArrayTemporaryExpr(Expr *S)
+  : Expr(HLSLArrayTemporaryExprClass, S->getType(), VK_LValue, 
OK_Ordinary),
+SourceExpr(S) {}
+
+  HLSLArrayTemporaryExpr(EmptyShell Empty)
+  : Expr(HLSLArrayTemporaryExprClass, Empty), SourceExpr(nullptr) {}
+
+public:
+  static HLSLArrayTemporaryExpr *Create(const ASTContext , Expr *S);
+  static HLSLArrayTemporaryExpr *CreateEmpty(const ASTContext );
+
+  const Expr *getSourceExpr() const { return SourceExpr; }
+  Expr *getSourceExpr() { return SourceExpr; }
+  void setSourceExpr(Expr *S) { SourceExpr = S; }
+
+  SourceLocation getBeginLoc() const { return SourceExpr->getBeginLoc(); }
+
+  SourceLocation getEndLoc() const { return SourceExpr->getEndLoc(); }
+
+  static bool classof(const Stmt *T) {
+return T->getStmtClass() == HLSLArrayTemporaryExprClass;
+  }
+
+  // Iterators
+  child_range children() {
+return child_range(child_iterator(), child_iterator());
+  }
+  const_child_range children() const {
+return const_child_range(const_child_iterator(), const_child_iterator());
+  }
+};
+
 } // end namespace clang
 
 #endif // LLVM_CLANG_AST_EXPR_H
diff --git a/clang/include/clang/AST/RecursiveASTVisitor.h 
b/clang/include/clang/AST/RecursiveASTVisitor.h
index 2aee6a947141b6..f7f805cf20 100644
--- a/clang/include/clang/AST/RecursiveASTVisitor.h
+++ b/clang/include/clang/AST/RecursiveASTVisitor.h
@@ -3171,6 +3171,8 @@ DEF_TRAVERSE_STMT(OMPTargetParallelGenericLoopDirective,
 DEF_TRAVERSE_STMT(OMPErrorDirective,
   { TRY_TO(TraverseOMPExecutableDirective(S)); })
 
+DEF_TRAVERSE_STMT(HLSLArrayTemporaryExpr, {})
+
 // OpenMP clauses.
 template 
 bool RecursiveASTVisitor::TraverseOMPClause(OMPClause *C) {
diff --git a/clang/include/clang/Basic/StmtNodes.td 
b/clang/include/clang/Basic/StmtNodes.td
index cec301dfca2817..3d14d33a14b878 100644
--- a/clang/include/clang/Basic/StmtNodes.td
+++ b/clang/include/clang/Basic/StmtNodes.td
@@ -295,3 +295,6 @@ def OMPTargetTeamsGenericLoopDirective : 
StmtNode;
 def OMPParallelGenericLoopDirective : StmtNode;
 def OMPTargetParallelGenericLoopDirective : StmtNode;
 def OMPErrorDirective : StmtNode;
+
+// HLSL Extensions
+def HLSLArrayTemporaryExpr : StmtNode;
diff --git a/clang/lib/AST/Expr.cpp b/clang/lib/AST/Expr.cpp
index f1efa98e175edf..5903e2763dbe59 100644
--- a/clang/lib/AST/Expr.cpp
+++ b/clang/lib/AST/Expr.cpp
@@ -3569,6 +3569,7 @@ bool Expr::HasSideEffects(const ASTContext ,
   case ConceptSpecializationExprClass:
   case 

[clang] [HLSL] Implement array temporary support (PR #79382)

2024-01-24 Thread Chris B via cfe-commits

https://github.com/llvm-beanz created 
https://github.com/llvm/llvm-project/pull/79382

In HLSL function parameters are passed by value, including array parameters. 
This change introduces a new AST node to represent array temporary expressions. 
They behave as lvalues to temporary arrays and decay to pointers for overload 
resolution and code generation.

The behavior of HLSL function calls is documented in the [draft language 
specification](https://microsoft.github.io/hlsl-specs/specs/hlsl.pdf) under the 
Expr.Post.Call heading.

Additionally the design of this implementation approach is documented in 
[Clang's documentation](https://clang.llvm.org/docs/HLSL/FunctionCalls.html)

>From d01d3dd8de6e955ad19a0ad8399547dbc59f7a52 Mon Sep 17 00:00:00 2001
From: Chris Bieneman 
Date: Wed, 24 Jan 2024 16:37:46 -0600
Subject: [PATCH] [HLSL] Implement array temporary support

In HLSL function parameters are passed by value, including array
parameters. This change introduces a new AST node to represent array
temporary expressions. They behave as lvalues to temporary arrays and
decay to pointers for overload resolution and code generation.

The behavior of HLSL function calls is documented in the [draft
language
specification](https://microsoft.github.io/hlsl-specs/specs/hlsl.pdf)
under the Expr.Post.Call heading.

Additionally the design of this implementation approach is documented
in [Clang's
documentation](https://clang.llvm.org/docs/HLSL/FunctionCalls.html)
---
 clang/include/clang/AST/Expr.h| 38 ++
 clang/include/clang/AST/RecursiveASTVisitor.h |  2 +
 clang/include/clang/Basic/StmtNodes.td|  3 ++
 clang/lib/AST/Expr.cpp| 11 +
 clang/lib/AST/ExprClassification.cpp  |  1 +
 clang/lib/AST/ExprConstant.cpp|  1 +
 clang/lib/AST/ItaniumMangle.cpp   |  4 ++
 clang/lib/AST/StmtPrinter.cpp |  4 ++
 clang/lib/AST/StmtProfile.cpp |  9 
 clang/lib/CodeGen/CGExpr.cpp  |  1 +
 clang/lib/CodeGen/CGExprAgg.cpp   |  6 +++
 clang/lib/Sema/SemaExceptionSpec.cpp  |  1 +
 clang/lib/Sema/SemaInit.cpp   |  5 ++
 clang/lib/Sema/TreeTransform.h| 13 +
 clang/lib/Serialization/ASTReaderStmt.cpp |  8 +++
 clang/lib/Serialization/ASTWriterStmt.cpp |  8 +++
 clang/lib/StaticAnalyzer/Core/ExprEngine.cpp  |  3 +-
 clang/test/CodeGenHLSL/ArrayTemporary.hlsl| 49 +++
 clang/test/SemaHLSL/ArrayTemporary.hlsl   | 46 +
 clang/tools/libclang/CXCursor.cpp |  1 +
 20 files changed, 213 insertions(+), 1 deletion(-)
 create mode 100644 clang/test/CodeGenHLSL/ArrayTemporary.hlsl
 create mode 100644 clang/test/SemaHLSL/ArrayTemporary.hlsl

diff --git a/clang/include/clang/AST/Expr.h b/clang/include/clang/AST/Expr.h
index 59f0aee2c0cedd..c28747e7a3796f 100644
--- a/clang/include/clang/AST/Expr.h
+++ b/clang/include/clang/AST/Expr.h
@@ -6651,6 +6651,44 @@ class RecoveryExpr final : public Expr,
   friend class ASTStmtWriter;
 };
 
+/// HLSLArrayTemporaryExpr - In HLSL, default parameter passing is by value
+/// including for arrays. This AST node represents a materialized temporary of 
a
+/// constant size arrray.
+class HLSLArrayTemporaryExpr : public Expr {
+  Expr *SourceExpr;
+
+  HLSLArrayTemporaryExpr(Expr *S)
+  : Expr(HLSLArrayTemporaryExprClass, S->getType(), VK_LValue, 
OK_Ordinary),
+SourceExpr(S) {}
+
+  HLSLArrayTemporaryExpr(EmptyShell Empty)
+  : Expr(HLSLArrayTemporaryExprClass, Empty), SourceExpr(nullptr) {}
+
+public:
+  static HLSLArrayTemporaryExpr *Create(const ASTContext , Expr *S);
+  static HLSLArrayTemporaryExpr *CreateEmpty(const ASTContext );
+
+  const Expr *getSourceExpr() const { return SourceExpr; }
+  Expr *getSourceExpr() { return SourceExpr; }
+  void setSourceExpr(Expr *S) { SourceExpr = S; }
+
+  SourceLocation getBeginLoc() const { return SourceExpr->getBeginLoc(); }
+
+  SourceLocation getEndLoc() const { return SourceExpr->getEndLoc(); }
+
+  static bool classof(const Stmt *T) {
+return T->getStmtClass() == HLSLArrayTemporaryExprClass;
+  }
+
+  // Iterators
+  child_range children() {
+return child_range(child_iterator(), child_iterator());
+  }
+  const_child_range children() const {
+return const_child_range(const_child_iterator(), const_child_iterator());
+  }
+};
+
 } // end namespace clang
 
 #endif // LLVM_CLANG_AST_EXPR_H
diff --git a/clang/include/clang/AST/RecursiveASTVisitor.h 
b/clang/include/clang/AST/RecursiveASTVisitor.h
index 2aee6a947141b6..f7f805cf20 100644
--- a/clang/include/clang/AST/RecursiveASTVisitor.h
+++ b/clang/include/clang/AST/RecursiveASTVisitor.h
@@ -3171,6 +3171,8 @@ DEF_TRAVERSE_STMT(OMPTargetParallelGenericLoopDirective,
 DEF_TRAVERSE_STMT(OMPErrorDirective,
   { TRY_TO(TraverseOMPExecutableDirective(S)); })
 
+DEF_TRAVERSE_STMT(HLSLArrayTemporaryExpr, {})
+
 // OpenMP