Skip to content

Commit 03618ce

Browse files
authored
[HLSL] Fix move assignment of this (#108445)
Under HLSL 202x+ move assignment can occur and when targeting `this` move assignment was generating some really odd errors. This corrects the errors by properly generating the `this` object reference for HLSL and always treating it as a reference. This mirrors the implementation added eariler for copy assignment, and extends the test case to cover both move and copy assignment under HLSL 202x+.
1 parent 77bab2a commit 03618ce

File tree

2 files changed

+33
-7
lines changed

2 files changed

+33
-7
lines changed

clang/lib/Sema/SemaDeclCXX.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15331,6 +15331,7 @@ void Sema::DefineImplicitMoveAssignment(SourceLocation CurrentLocation,
1533115331
std::optional<DerefBuilder> DerefThis;
1533215332
std::optional<RefBuilder> ExplicitObject;
1533315333
QualType ObjectType;
15334+
bool IsArrow = false;
1533415335
if (MoveAssignOperator->isExplicitObjectMemberFunction()) {
1533515336
ObjectType = MoveAssignOperator->getParamDecl(0)->getType();
1533615337
if (ObjectType->isReferenceType())
@@ -15340,6 +15341,7 @@ void Sema::DefineImplicitMoveAssignment(SourceLocation CurrentLocation,
1534015341
ObjectType = getCurrentThisType();
1534115342
This.emplace();
1534215343
DerefThis.emplace(*This);
15344+
IsArrow = !getLangOpts().HLSL;
1534315345
}
1534415346
ExprBuilder &ObjectParameter =
1534515347
ExplicitObject ? *ExplicitObject : static_cast<ExprBuilder &>(*This);
@@ -15441,8 +15443,7 @@ void Sema::DefineImplicitMoveAssignment(SourceLocation CurrentLocation,
1544115443
MemberLookup.resolveKind();
1544215444
MemberBuilder From(MoveOther, OtherRefType,
1544315445
/*IsArrow=*/false, MemberLookup);
15444-
MemberBuilder To(ObjectParameter, ObjectType, /*IsArrow=*/!ExplicitObject,
15445-
MemberLookup);
15446+
MemberBuilder To(ObjectParameter, ObjectType, IsArrow, MemberLookup);
1544615447

1544715448
assert(!From.build(*this, Loc)->isLValue() && // could be xvalue or prvalue
1544815449
"Member reference with rvalue base must be rvalue except for reference "
@@ -15465,8 +15466,9 @@ void Sema::DefineImplicitMoveAssignment(SourceLocation CurrentLocation,
1546515466
if (!Invalid) {
1546615467
// Add a "return *this;"
1546715468
Expr *ThisExpr =
15468-
(ExplicitObject ? static_cast<ExprBuilder &>(*ExplicitObject)
15469-
: static_cast<ExprBuilder &>(*DerefThis))
15469+
(ExplicitObject ? static_cast<ExprBuilder &>(*ExplicitObject)
15470+
: LangOpts.HLSL ? static_cast<ExprBuilder &>(*This)
15471+
: static_cast<ExprBuilder &>(*DerefThis))
1547015472
.build(*this, Loc);
1547115473

1547215474
StmtResult Return = BuildReturnStmt(Loc, ThisExpr);

clang/test/CodeGenHLSL/this-assignment.hlsl

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
// RUN: %clang_cc1 -triple dxil-pc-shadermodel6.0-compute -x hlsl -emit-llvm -disable-llvm-passes -o - -hlsl-entry main %s | FileCheck %s
1+
// RUN: %clang_cc1 -triple dxil-pc-shadermodel6.0-compute -emit-llvm -disable-llvm-passes -o - -hlsl-entry main %s | FileCheck %s
2+
// RUN: %clang_cc1 -triple dxil-pc-shadermodel6.0-compute -std=hlsl202x -emit-llvm -disable-llvm-passes -o - -hlsl-entry main %s | FileCheck %s
23

34
struct Pair {
45
int First;
@@ -10,21 +11,30 @@ struct Pair {
1011
return this.First;
1112
}
1213

14+
// In HLSL 202x, this is a move assignment rather than a copy.
1315
int getSecond() {
1416
this = Pair();
1517
return Second;
1618
}
19+
20+
// In HLSL 202x, this is a copy assignment.
21+
Pair DoSilly(Pair Obj) {
22+
this = Obj;
23+
First += 2;
24+
return Obj;
25+
}
1726
};
1827

1928
[numthreads(1, 1, 1)]
2029
void main() {
2130
Pair Vals = {1, 2.0};
2231
Vals.First = Vals.getFirst();
2332
Vals.Second = Vals.getSecond();
33+
(void) Vals.DoSilly(Vals);
2434
}
2535

2636
// This tests reference like implicit this in HLSL
27-
// CHECK: define linkonce_odr noundef i32 @"?getFirst@Pair@@QAAHXZ"(ptr noundef nonnull align 4 dereferenceable(8) %this) #0 align 2 {
37+
// CHECK-LABEL: define {{.*}}getFirst
2838
// CHECK-NEXT:entry:
2939
// CHECK-NEXT:%this.addr = alloca ptr, align 4
3040
// CHECK-NEXT:%Another = alloca %struct.Pair, align 4
@@ -34,7 +44,7 @@ void main() {
3444
// CHECK-NEXT:call void @llvm.memcpy.p0.p0.i32(ptr align 4 %this1, ptr align 4 %Another, i32 8, i1 false)
3545
// CHECK-NEXT:%First = getelementptr inbounds nuw %struct.Pair, ptr %this1, i32 0, i32 0
3646

37-
// CHECK: define linkonce_odr noundef i32 @"?getSecond@Pair@@QAAHXZ"(ptr noundef nonnull align 4 dereferenceable(8) %this) #0 align 2 {
47+
// CHECK-LABEL: define {{.*}}getSecond
3848
// CHECK-NEXT:entry:
3949
// CHECK-NEXT:%this.addr = alloca ptr, align 4
4050
// CHECK-NEXT:%ref.tmp = alloca %struct.Pair, align 4
@@ -43,3 +53,17 @@ void main() {
4353
// CHECK-NEXT:call void @llvm.memset.p0.i32(ptr align 4 %ref.tmp, i8 0, i32 8, i1 false)
4454
// CHECK-NEXT:call void @llvm.memcpy.p0.p0.i32(ptr align 4 %this1, ptr align 4 %ref.tmp, i32 8, i1 false)
4555
// CHECK-NEXT:%Second = getelementptr inbounds nuw %struct.Pair, ptr %this1, i32 0, i32 1
56+
57+
// CHECK-LABEL: define {{.*}}DoSilly
58+
// CHECK-NEXT:entry:
59+
// CHECK-NEXT: [[ResPtr:%.*]] = alloca ptr
60+
// CHECK-NEXT: [[ThisPtrAddr:%.*]] = alloca ptr
61+
// CHECK-NEXT: store ptr [[AggRes:%.*]], ptr [[ResPtr]]
62+
// CHECK-NEXT: store ptr {{.*}}, ptr [[ThisPtrAddr]]
63+
// CHECK-NEXT: [[ThisPtr:%.*]] = load ptr, ptr [[ThisPtrAddr]]
64+
// CHECK-NEXT: call void @llvm.memcpy.p0.p0.i32(ptr align 4 [[ThisPtr]], ptr align 4 [[Obj:%.*]], i32 8, i1 false)
65+
// CHECK-NEXT: [[FirstAddr:%.*]] = getelementptr inbounds nuw %struct.Pair, ptr [[ThisPtr]], i32 0, i32 0
66+
// CHECK-NEXT: [[First:%.*]] = load i32, ptr [[FirstAddr]]
67+
// CHECK-NEXT: [[FirstPlusTwo:%.*]] = add nsw i32 [[First]], 2
68+
// CHECK-NEXT: store i32 [[FirstPlusTwo]], ptr [[FirstAddr]]
69+
// CHECK-NEXT: call void @llvm.memcpy.p0.p0.i32(ptr align 4 [[AggRes]], ptr align 4 [[Obj]], i32 8, i1 false)

0 commit comments

Comments
 (0)