[clang] [HLSL] Handle init list with OpaqueValueExprs in CGExprScalar (PR #138541)

via cfe-commits cfe-commits at lists.llvm.org
Mon May 5 08:33:26 PDT 2025


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-hlsl

Author: Sarah Spall (spall)

<details>
<summary>Changes</summary>

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

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


2 Files Affected:

- (modified) clang/lib/CodeGen/CGExprScalar.cpp (+12) 
- (modified) clang/test/CodeGenHLSL/BasicFeatures/InitLists.hlsl (+18) 


``````````diff
diff --git a/clang/lib/CodeGen/CGExprScalar.cpp b/clang/lib/CodeGen/CGExprScalar.cpp
index 15a6177746403..c4d7409d212cf 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,17 @@ 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:
+  // * 0005-strict-initializer-lists.md
+  // * https://github.com/microsoft/hlsl-specs/pull/325
+  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..93246d326734a 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)};
+}
\ No newline at end of file

``````````

</details>


https://github.com/llvm/llvm-project/pull/138541


More information about the cfe-commits mailing list