Author: Sarah Spall
Date: 2025-05-05T13:04:41-07:00
New Revision: 02e0a954a014bc44796c84abe5445ae0d20eeeb5

URL: 
https://github.com/llvm/llvm-project/commit/02e0a954a014bc44796c84abe5445ae0d20eeeb5
DIFF: 
https://github.com/llvm/llvm-project/commit/02e0a954a014bc44796c84abe5445ae0d20eeeb5.diff

LOG: [HLSL] Handle init list with OpaqueValueExprs in CGExprScalar (#138541)

When an HLSL Init list is producing a Scalar, handle OpaqueValueExprs in
the Init List with 'emitInitListOpaqueValues'
Copied from 'AggExprEmitter::VisitCXXParenListOrInitListExpr'
Closes #136408

---------

Co-authored-by: Chris B <be...@abolishcrlf.org>

Added: 
    

Modified: 
    clang/lib/CodeGen/CGExprScalar.cpp
    clang/test/CodeGenHLSL/BasicFeatures/InitLists.hlsl

Removed: 
    


################################################################################
diff  --git a/clang/lib/CodeGen/CGExprScalar.cpp 
b/clang/lib/CodeGen/CGExprScalar.cpp
index 15a6177746403..f639a87e3ad0b 100644
--- a/clang/lib/CodeGen/CGExprScalar.cpp
+++ b/clang/lib/CodeGen/CGExprScalar.cpp
@@ -13,6 +13,7 @@
 #include "CGCXXABI.h"
 #include "CGCleanup.h"
 #include "CGDebugInfo.h"
+#include "CGHLSLRuntime.h"
 #include "CGObjCRuntime.h"
 #include "CGOpenMPRuntime.h"
 #include "CGRecordLayout.h"
@@ -2095,6 +2096,18 @@ Value *ScalarExprEmitter::VisitInitListExpr(InitListExpr 
*E) {
   assert (Ignore == false && "init list ignored");
   unsigned NumInitElements = E->getNumInits();
 
+  // HLSL initialization lists in the AST are an expansion which can contain
+  // side-effecting expressions wrapped in opaque value expressions. To 
properly
+  // emit these we need to emit the opaque values before we emit the argument
+  // expressions themselves. This is a little hacky, but it prevents us needing
+  // to do a bigger AST-level change for a language feature that we need
+  // deprecate in the near future. See related HLSL language proposals in the
+  // proposals (https://github.com/microsoft/hlsl-specs/blob/main/proposals):
+  // * 0005-strict-initializer-lists.md
+  // * 0032-constructors.md
+  if (CGF.getLangOpts().HLSL)
+    CGF.CGM.getHLSLRuntime().emitInitListOpaqueValues(CGF, E);
+
   if (E->hadArrayRangeDesignator())
     CGF.ErrorUnsupported(E, "GNU array range designator extension");
 

diff  --git a/clang/test/CodeGenHLSL/BasicFeatures/InitLists.hlsl 
b/clang/test/CodeGenHLSL/BasicFeatures/InitLists.hlsl
index d04583e4fc51a..371f31c9e4afc 100644
--- a/clang/test/CodeGenHLSL/BasicFeatures/InitLists.hlsl
+++ b/clang/test/CodeGenHLSL/BasicFeatures/InitLists.hlsl
@@ -941,3 +941,21 @@ FourFloats case16() {
     FourFloats FF = {0, makeTwo(X), 3};
     return FF;
 }
+
+
+int case17Helper(int x) {
+  return x;
+}
+
+// InitList with OpaqueValueExpr
+// CHECK-LABEL: define void {{.*}}case17
+// CHECK: [[X:%.*]] = alloca <2 x i32>, align 8
+// CHECK-NEXT: [[C:%.*]] = call noundef i32 {{.*}}case17Helper{{.*}}(i32 
noundef 0)
+// CHECK-NEXT: [[C1:%.*]] = call noundef i32 {{.*}}case17Helper{{.*}}(i32 
noundef 1)
+// CHECK-NEXT: [[VI:%.*]] = insertelement <2 x i32> poison, i32 [[C]], i32 0
+// CHECK-NEXT: [[VI2:%.*]] = insertelement <2 x i32> [[VI]], i32 [[C1]], i32 1
+// CHECK-NEXT: store <2 x i32> [[VI2]], ptr [[X]], align 8
+// CHECK-NEXT: ret void
+void case17() {
+  int2 X = {case17Helper(0), case17Helper(1)};
+}


        
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to