[clang] [HLSL] Fix move assignment of `this` (PR #108445)

Chris B via cfe-commits cfe-commits at lists.llvm.org
Fri Sep 13 10:54:39 PDT 2024


https://github.com/llvm-beanz updated https://github.com/llvm/llvm-project/pull/108445

>From a5be65fa3b763659dda7fd9ab2fda9ca9dae4391 Mon Sep 17 00:00:00 2001
From: Chris Bieneman <chris.bieneman at me.com>
Date: Thu, 12 Sep 2024 12:06:02 -0500
Subject: [PATCH 1/2] [HLSL] Fix move assignment of `this`

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+.
---
 clang/lib/Sema/SemaDeclCXX.cpp              | 10 ++++---
 clang/test/CodeGenHLSL/this-assignment.hlsl | 31 +++++++++++++++++++--
 2 files changed, 34 insertions(+), 7 deletions(-)

diff --git a/clang/lib/Sema/SemaDeclCXX.cpp b/clang/lib/Sema/SemaDeclCXX.cpp
index 6b6fa98bf394ca..d8cdfcf8c6ec05 100644
--- a/clang/lib/Sema/SemaDeclCXX.cpp
+++ b/clang/lib/Sema/SemaDeclCXX.cpp
@@ -15331,6 +15331,7 @@ void Sema::DefineImplicitMoveAssignment(SourceLocation CurrentLocation,
   std::optional<DerefBuilder> DerefThis;
   std::optional<RefBuilder> ExplicitObject;
   QualType ObjectType;
+  bool IsArrow = false;
   if (MoveAssignOperator->isExplicitObjectMemberFunction()) {
     ObjectType = MoveAssignOperator->getParamDecl(0)->getType();
     if (ObjectType->isReferenceType())
@@ -15340,6 +15341,7 @@ void Sema::DefineImplicitMoveAssignment(SourceLocation CurrentLocation,
     ObjectType = getCurrentThisType();
     This.emplace();
     DerefThis.emplace(*This);
+    IsArrow = !getLangOpts().HLSL;
   }
   ExprBuilder &ObjectParameter =
       ExplicitObject ? *ExplicitObject : static_cast<ExprBuilder &>(*This);
@@ -15441,8 +15443,7 @@ void Sema::DefineImplicitMoveAssignment(SourceLocation CurrentLocation,
     MemberLookup.resolveKind();
     MemberBuilder From(MoveOther, OtherRefType,
                        /*IsArrow=*/false, MemberLookup);
-    MemberBuilder To(ObjectParameter, ObjectType, /*IsArrow=*/!ExplicitObject,
-                     MemberLookup);
+    MemberBuilder To(ObjectParameter, ObjectType, IsArrow, MemberLookup);
 
     assert(!From.build(*this, Loc)->isLValue() && // could be xvalue or prvalue
         "Member reference with rvalue base must be rvalue except for reference "
@@ -15465,8 +15466,9 @@ void Sema::DefineImplicitMoveAssignment(SourceLocation CurrentLocation,
   if (!Invalid) {
     // Add a "return *this;"
     Expr *ThisExpr =
-        (ExplicitObject ? static_cast<ExprBuilder &>(*ExplicitObject)
-                        : static_cast<ExprBuilder &>(*DerefThis))
+        (ExplicitObject  ? static_cast<ExprBuilder &>(*ExplicitObject)
+         : LangOpts.HLSL ? static_cast<ExprBuilder &>(*This)
+                         : static_cast<ExprBuilder &>(*DerefThis))
             .build(*this, Loc);
 
     StmtResult Return = BuildReturnStmt(Loc, ThisExpr);
diff --git a/clang/test/CodeGenHLSL/this-assignment.hlsl b/clang/test/CodeGenHLSL/this-assignment.hlsl
index 5c8de0a18ef7ca..252c71a0fd2245 100644
--- a/clang/test/CodeGenHLSL/this-assignment.hlsl
+++ b/clang/test/CodeGenHLSL/this-assignment.hlsl
@@ -1,4 +1,5 @@
-// RUN: %clang_cc1 -triple dxil-pc-shadermodel6.0-compute -x hlsl -emit-llvm -disable-llvm-passes -o - -hlsl-entry main %s | FileCheck %s
+// RUN: %clang_cc1 -triple dxil-pc-shadermodel6.0-compute -emit-llvm -disable-llvm-passes -o - -hlsl-entry main %s | FileCheck %s
+// RUN: %clang_cc1 -triple dxil-pc-shadermodel6.0-compute -std=hlsl202x -emit-llvm -disable-llvm-passes -o - -hlsl-entry main %s | FileCheck %s
 
 struct Pair {
   int First;
@@ -10,10 +11,18 @@ struct Pair {
 	  return this.First;
   }
 
+  // In HLSL 202x, this is a move assignment rather than a copy.
   int getSecond() {
     this = Pair();
     return Second;
   }
+
+  // In HLSL 202x, this is a copy assignment.
+  Pair DoSilly(Pair Obj) {
+    this = Obj;
+    First += 2;
+    return Obj;
+  }
 };
 
 [numthreads(1, 1, 1)]
@@ -21,10 +30,11 @@ void main() {
   Pair Vals = {1, 2.0};
   Vals.First = Vals.getFirst();
   Vals.Second = Vals.getSecond();
+  (void) Vals.DoSilly(Vals);
 }
 
 // This tests reference like implicit this in HLSL
-// CHECK:     define linkonce_odr noundef i32 @"?getFirst at Pair@@QAAHXZ"(ptr noundef nonnull align 4 dereferenceable(8) %this) #0 align 2 {
+// CHECK-LABEL:     define {{.*}}getFirst
 // CHECK-NEXT:entry:
 // CHECK-NEXT:%this.addr = alloca ptr, align 4
 // CHECK-NEXT:%Another = alloca %struct.Pair, align 4
@@ -34,7 +44,7 @@ void main() {
 // CHECK-NEXT:call void @llvm.memcpy.p0.p0.i32(ptr align 4 %this1, ptr align 4 %Another, i32 8, i1 false)
 // CHECK-NEXT:%First = getelementptr inbounds nuw %struct.Pair, ptr %this1, i32 0, i32 0
 
-// CHECK:     define linkonce_odr noundef i32 @"?getSecond at Pair@@QAAHXZ"(ptr noundef nonnull align 4 dereferenceable(8) %this) #0 align 2 {
+// CHECK-LABEL:     define {{.*}}getSecond
 // CHECK-NEXT:entry:
 // CHECK-NEXT:%this.addr = alloca ptr, align 4
 // CHECK-NEXT:%ref.tmp = alloca %struct.Pair, align 4
@@ -43,3 +53,18 @@ void main() {
 // CHECK-NEXT:call void @llvm.memset.p0.i32(ptr align 4 %ref.tmp, i8 0, i32 8, i1 false)
 // CHECK-NEXT:call void @llvm.memcpy.p0.p0.i32(ptr align 4 %this1, ptr align 4 %ref.tmp, i32 8, i1 false)
 // CHECK-NEXT:%Second = getelementptr inbounds nuw %struct.Pair, ptr %this1, i32 0, i32 1
+
+// CHECK-LABEL:     define {{.*}}DoSilly
+// CHECK-NEXT:entry:
+
+// CHECK-NEXT: [[ResPtr:%.*]] = alloca ptr
+// CHECK-NEXT: [[ThisPtrAddr:%.*]] = alloca ptr
+// CHECK-NEXT: store ptr [[AggRes:%.*]], ptr [[ResPtr]]
+// CHECK-NEXT: store ptr {{.*}}, ptr [[ThisPtrAddr]]
+// CHECK-NEXT: [[ThisPtr:%.*]] = load ptr, ptr [[ThisPtrAddr]]
+// CHECK-NEXT: call void @llvm.memcpy.p0.p0.i32(ptr align 4 [[ThisPtr]], ptr align 4 [[Obj:%.*]], i32 8, i1 false)
+// CHECK-NEXT: [[FirstAddr:%.*]] = getelementptr inbounds nuw %struct.Pair, ptr [[ThisPtr]], i32 0, i32 0
+// CHECK-NEXT: [[First:%.*]] = load i32, ptr [[FirstAddr]]
+// CHECK-NEXT: [[FirstPlusTwo:%.*]] = add nsw i32 [[First]], 2
+// CHECK-NEXT: store i32 [[FirstPlusTwo]], ptr [[FirstAddr]]
+// CHECK-NEXT: call void @llvm.memcpy.p0.p0.i32(ptr align 4 [[AggRes]], ptr align 4 [[Obj]], i32 8, i1 false)

>From ae19efc8b26c6b5b387024e1a3772438a4cf37fe Mon Sep 17 00:00:00 2001
From: Chris Bieneman <chris.bieneman at me.com>
Date: Fri, 13 Sep 2024 12:54:18 -0500
Subject: [PATCH 2/2] Remove spurious newline

---
 clang/test/CodeGenHLSL/this-assignment.hlsl | 1 -
 1 file changed, 1 deletion(-)

diff --git a/clang/test/CodeGenHLSL/this-assignment.hlsl b/clang/test/CodeGenHLSL/this-assignment.hlsl
index 252c71a0fd2245..7408d199910e5c 100644
--- a/clang/test/CodeGenHLSL/this-assignment.hlsl
+++ b/clang/test/CodeGenHLSL/this-assignment.hlsl
@@ -56,7 +56,6 @@ void main() {
 
 // CHECK-LABEL:     define {{.*}}DoSilly
 // CHECK-NEXT:entry:
-
 // CHECK-NEXT: [[ResPtr:%.*]] = alloca ptr
 // CHECK-NEXT: [[ThisPtrAddr:%.*]] = alloca ptr
 // CHECK-NEXT: store ptr [[AggRes:%.*]], ptr [[ResPtr]]



More information about the cfe-commits mailing list