From 24f4cf68760caa6d49a28098e889b8d6973cb1ab Mon Sep 17 00:00:00 2001 From: Sarah Spall Date: Mon, 5 May 2025 08:20:47 -0700 Subject: [PATCH 1/3] call emitInitListOpaqueValues in CGExprScalar --- clang/lib/CodeGen/CGExprScalar.cpp | 12 ++++++++++++ .../CodeGenHLSL/BasicFeatures/InitLists.hlsl | 18 ++++++++++++++++++ 2 files changed, 30 insertions(+) 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 From 3b495920c6eb880462d16dddac1acd11e79f5fad Mon Sep 17 00:00:00 2001 From: Sarah Spall Date: Mon, 5 May 2025 08:56:24 -0700 Subject: [PATCH 2/3] newline --- clang/test/CodeGenHLSL/BasicFeatures/InitLists.hlsl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/clang/test/CodeGenHLSL/BasicFeatures/InitLists.hlsl b/clang/test/CodeGenHLSL/BasicFeatures/InitLists.hlsl index 93246d326734a..371f31c9e4afc 100644 --- a/clang/test/CodeGenHLSL/BasicFeatures/InitLists.hlsl +++ b/clang/test/CodeGenHLSL/BasicFeatures/InitLists.hlsl @@ -958,4 +958,4 @@ int case17Helper(int x) { // CHECK-NEXT: ret void void case17() { int2 X = {case17Helper(0), case17Helper(1)}; -} \ No newline at end of file +} From 1ba896ae13873fbe2a28e4572e2d02542ca35980 Mon Sep 17 00:00:00 2001 From: Sarah Spall Date: Mon, 5 May 2025 13:03:16 -0700 Subject: [PATCH 3/3] Update clang/lib/CodeGen/CGExprScalar.cpp Co-authored-by: Chris B --- clang/lib/CodeGen/CGExprScalar.cpp | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/clang/lib/CodeGen/CGExprScalar.cpp b/clang/lib/CodeGen/CGExprScalar.cpp index c4d7409d212cf..f639a87e3ad0b 100644 --- a/clang/lib/CodeGen/CGExprScalar.cpp +++ b/clang/lib/CodeGen/CGExprScalar.cpp @@ -2101,9 +2101,10 @@ Value *ScalarExprEmitter::VisitInitListExpr(InitListExpr *E) { // 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: + // 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 - // * https://github.com/microsoft/hlsl-specs/pull/325 + // * 0032-constructors.md if (CGF.getLangOpts().HLSL) CGF.CGM.getHLSLRuntime().emitInitListOpaqueValues(CGF, E);