llvmorg-github-actions[bot] wrote:

<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang

@llvm/pr-subscribers-clang-codegen

Author: Chakram Siri (chakramsiri)

<details>
<summary>Changes</summary>

Fixes [[#<!-- -->235144](https://github.com/llvm/llvm-project/issues/235144)]
The issue was that weak array assignment helpers generated invalid cleanup IR 
when assigning structs containing arrays of weak Objective-C references.

Added a cleanup scope around the per-element visitor call in 
`CGNonTrivialStruct.cpp` and added a regression test in 
`weak-array-assignment.m` to cover both copy assignment and assignment from a 
returned struct.

## Testing

- `llvm-lit -v clang/test/CodeGenObjC/weak-array-assignment.m` — **Passed**
- Related non-trivial struct tests — **6/6 passed**
- `llvm-lit clang/test/CodeGenObjC` — **359 passed, 18 unsupported, 0 failures**
- Verified that the generated IR contains the expected weak-reference load, 
store, and cleanup operations.

GenAI(Chatgpt) Assisted with understanding of the source code and in helping 
with edge case tests.

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


2 Files Affected:

- (modified) clang/lib/CodeGen/CGNonTrivialStruct.cpp (+5-3) 
- (added) clang/test/CodeGenObjC/weak-array-assignment.m (+26) 


``````````diff
diff --git a/clang/lib/CodeGen/CGNonTrivialStruct.cpp 
b/clang/lib/CodeGen/CGNonTrivialStruct.cpp
index 0a383c8f919d95..6ce8f113eca624 100644
--- a/clang/lib/CodeGen/CGNonTrivialStruct.cpp
+++ b/clang/lib/CodeGen/CGNonTrivialStruct.cpp
@@ -414,9 +414,11 @@ template <class Derived> struct GenFuncBase {
                     StartAddrs[I].getAlignment().alignmentAtOffset(EltSize));
 
     EltQT = IsVolatile ? EltQT.withVolatile() : EltQT;
-    this->asDerived().visitWithKind(FK, EltQT, nullptr, CharUnits::Zero(),
-                                    NewAddrs);
-
+    {
+      CodeGenFunction::RunCleanupsScope Scope(CGF);
+      this->asDerived().visitWithKind(FK, EltQT, nullptr, CharUnits::Zero(),
+                                      NewAddrs);
+    }
     LoopBB = CGF.Builder.GetInsertBlock();
 
     for (unsigned I = 0; I < N; ++I) {
diff --git a/clang/test/CodeGenObjC/weak-array-assignment.m 
b/clang/test/CodeGenObjC/weak-array-assignment.m
new file mode 100644
index 00000000000000..7063f382234193
--- /dev/null
+++ b/clang/test/CodeGenObjC/weak-array-assignment.m
@@ -0,0 +1,26 @@
+// RUN: %clang_cc1 -triple arm64-apple-ios11 -fobjc-arc -fblocks 
-fobjc-runtime=ios-11.0 -emit-llvm -o - %s | FileCheck %s
+
+typedef struct {
+  __weak id values[2];
+} WeakArray;
+
+WeakArray getWeakArray(void);
+
+// CHECK-LABEL: define void @copyWeakArray(
+// CHECK: call ptr @llvm.objc.loadWeakRetained
+// CHECK: call ptr @llvm.objc.storeWeak
+// CHECK: call void @llvm.objc.release
+
+void copyWeakArray(WeakArray *dst, WeakArray *src) {
+  *dst = *src;
+}
+
+// CHECK-LABEL: define void @moveWeakArray(
+// CHECK: call ptr @llvm.objc.loadWeakRetained
+// CHECK: call ptr @llvm.objc.storeWeak
+// CHECK: call void @llvm.objc.destroyWeak
+// CHECK: call void @llvm.objc.release
+
+void moveWeakArray(WeakArray *dst) {
+  *dst = getWeakArray();
+}

``````````

</details>


https://github.com/llvm/llvm-project/pull/235334
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to