Skip to content

[HLSL] Handle init list with OpaqueValueExprs in CGExprScalar #138541

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 3 commits into from
May 5, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions clang/lib/CodeGen/CGExprScalar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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");

Expand Down
18 changes: 18 additions & 0 deletions clang/test/CodeGenHLSL/BasicFeatures/InitLists.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -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)};
}
Loading