Skip to content

[KeyInstr][JumpThreading] Remap atoms duping bb with cond br on phi into pred #133488

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 2 commits into from
May 7, 2025

Conversation

OCHyams
Copy link
Contributor

@OCHyams OCHyams commented Mar 28, 2025

See test for details.

Copy link
Contributor Author

OCHyams commented Mar 28, 2025

Warning

This pull request is not mergeable via GitHub because a downstack PR is open. Once all requirements are satisfied, merge this PR as a stack on Graphite.
Learn more

This stack of pull requests is managed by Graphite. Learn more about stacking.

@llvmbot
Copy link
Member

llvmbot commented Mar 28, 2025

@llvm/pr-subscribers-debuginfo

@llvm/pr-subscribers-llvm-transforms

Author: Orlando Cazalet-Hyams (OCHyams)

Changes

See test for details.


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

2 Files Affected:

  • (modified) llvm/lib/Transforms/Scalar/JumpThreading.cpp (+9)
  • (added) llvm/test/DebugInfo/KeyInstructions/Generic/jump-threading-dup-cond-br-on-phi-into-pred.ll (+87)
diff --git a/llvm/lib/Transforms/Scalar/JumpThreading.cpp b/llvm/lib/Transforms/Scalar/JumpThreading.cpp
index d320f7dce11db..67f59b9864b1e 100644
--- a/llvm/lib/Transforms/Scalar/JumpThreading.cpp
+++ b/llvm/lib/Transforms/Scalar/JumpThreading.cpp
@@ -2683,6 +2683,9 @@ bool JumpThreadingPass::duplicateCondBranchOnPHIIntoPred(
   // PredBB block.  Evaluate PHI nodes in BB.
   ValueToValueMapTy ValueMapping;
 
+  // Remember the position before the inserted instructions.
+  auto RItBeforeInsertPt = std::next(OldPredBranch->getReverseIterator());
+
   BasicBlock::iterator BI = BB->begin();
   for (; PHINode *PN = dyn_cast<PHINode>(BI); ++BI)
     ValueMapping[PN] = PN->getIncomingValueForBlock(PredBB);
@@ -2702,6 +2705,8 @@ bool JumpThreadingPass::duplicateCondBranchOnPHIIntoPred(
 
     // Remap debug variable operands.
     remapDebugVariable(ValueMapping, New);
+    if (const DebugLoc &DL = New->getDebugLoc())
+      mapAtomInstance(DL, ValueMapping);
 
     // If this instruction can be simplified after the operands are updated,
     // just use the simplified value instead.  This frequently happens due to
@@ -2740,6 +2745,10 @@ bool JumpThreadingPass::duplicateCondBranchOnPHIIntoPred(
   addPHINodeEntriesForMappedBlock(BBBranch->getSuccessor(1), BB, PredBB,
                                   ValueMapping);
 
+  // KeyInstructions: Remap the cloned instructions' atoms only.
+  remapSourceAtoms(ValueMapping, std::prev(RItBeforeInsertPt)->getIterator(),
+                   OldPredBranch->getIterator());
+
   updateSSA(BB, PredBB, ValueMapping);
 
   // PredBB no longer jumps to BB, remove entries in the PHI node for the edge
diff --git a/llvm/test/DebugInfo/KeyInstructions/Generic/jump-threading-dup-cond-br-on-phi-into-pred.ll b/llvm/test/DebugInfo/KeyInstructions/Generic/jump-threading-dup-cond-br-on-phi-into-pred.ll
new file mode 100644
index 0000000000000..ca1c748cc787a
--- /dev/null
+++ b/llvm/test/DebugInfo/KeyInstructions/Generic/jump-threading-dup-cond-br-on-phi-into-pred.ll
@@ -0,0 +1,87 @@
+; RUN: opt %s --passes=jump-threading -S -o - -S | FileCheck %s
+
+;;        +-> T1 -+
+;;        |       v      +-> T2
+;; Entry -+       Merge -+
+;;        |       ^      +-> F2
+;;        +-> F1 -+
+;;
+;; Duplicate Merge into T1 then fold Merge into its only pred F1 (taking its name).
+;;
+;;        +-> T1 -----> T2
+;;        |     \       ^
+;;        |      \     /
+;;        |       \   /
+;; Entry -+        +----+
+;;        |         /   v 
+;;        +--> Merge -> F2
+;;
+;; Check the duplicated  (into T1) instructions' atoms are remapped.
+
+; CHECK: T1:
+; CHECK-NEXT: %v1 = call i32 @f1()
+; CHECK-NEXT: %cond3 = icmp eq i32 %v1, 412
+; CHECK-NEXT: %C1 = add i32 %v1, 1, !dbg [[G3R2:!.*]]
+; CHECK-NEXT: store i32 %C1, ptr %p, align 4, !dbg [[G3R1:!.*]]
+
+; CHECK: Merge:
+; CHECK-NEXT: %v2 = call i32 @f2()
+; CHECK-NEXT: store i32 1, ptr %p, align 4, !dbg [[G1R1:!.*]]
+; CHECK-NEXT: %C = add i32 %v2, 1, !dbg [[G2R2:!.*]]
+; CHECK-NEXT: store i32 %C, ptr %p, align 4, !dbg [[G2R1:!.*]]
+
+; CHECK: [[G3R2]] = !DILocation({{.*}}, atomGroup: 3, atomRank: 2)
+; CHECK: [[G3R1]] = !DILocation({{.*}}, atomGroup: 3, atomRank: 1)
+; CHECK: [[G1R1]] = !DILocation({{.*}}, atomGroup: 1, atomRank: 1)
+; CHECK: [[G2R2]] = !DILocation({{.*}}, atomGroup: 2, atomRank: 2)
+; CHECK: [[G2R1]] = !DILocation({{.*}}, atomGroup: 2, atomRank: 1)
+
+define i32 @test5(i1 %cond, i1 %cond2, ptr %p) !dbg !5 {
+  br i1 %cond, label %T1, label %F1
+
+T1:                                               ; preds = %0
+  %v1 = call i32 @f1()
+  %cond3 = icmp eq i32 %v1, 412
+  br label %Merge
+
+F1:                                               ; preds = %0
+  %v2 = call i32 @f2()
+  store i32 1, ptr %p, align 4, !dbg !8
+  br label %Merge
+
+Merge:                                            ; preds = %F1, %T1
+  %A = phi i1 [ %cond3, %T1 ], [ %cond2, %F1 ]
+  %B = phi i32 [ %v1, %T1 ], [ %v2, %F1 ]
+  %C = add i32 %B, 1, !dbg !9
+  store i32 %C, ptr %p, align 4, !dbg !10
+  br i1 %A, label %T2, label %F2
+
+T2:                                               ; preds = %Merge
+  call void @f3()
+  ret i32 %B
+
+F2:                                               ; preds = %Merge
+  ret i32 %B
+}
+
+declare i32 @f1()
+
+declare i32 @f2()
+
+declare void @f3()
+
+!llvm.dbg.cu = !{!0}
+!llvm.debugify = !{!2, !3}
+!llvm.module.flags = !{!4}
+
+!0 = distinct !DICompileUnit(language: DW_LANG_C, file: !1, producer: "debugify", isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug)
+!1 = !DIFile(filename: "test.ll", directory: "/")
+!2 = !{i32 12}
+!3 = !{i32 0}
+!4 = !{i32 2, !"Debug Info Version", i32 3}
+!5 = distinct !DISubprogram(name: "test5", linkageName: "test5", scope: null, file: !1, line: 1, type: !6, scopeLine: 1, spFlags: DISPFlagDefinition | DISPFlagOptimized, unit: !0)
+!6 = !DISubroutineType(types: !7)
+!7 = !{}
+!8 = !DILocation(line: 1, column: 1, scope: !5, atomGroup: 1, atomRank: 1)
+!9 = !DILocation(line: 2, column: 1, scope: !5, atomGroup: 2, atomRank: 2)
+!10 = !DILocation(line: 2, column: 1, scope: !5, atomGroup: 2, atomRank: 1)

Copy link
Member

@jmorse jmorse left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Whilst being some complicated threading, LGTM

@OCHyams OCHyams force-pushed the users/OCHyams/ki-llvm-jumpthreading2 branch 2 times, most recently from 35da29f to 6b480a2 Compare May 7, 2025 14:34
@OCHyams OCHyams force-pushed the users/OCHyams/ki-llvm-jumpthreading3 branch from 6488f4b to 4dda51f Compare May 7, 2025 14:35
@OCHyams OCHyams force-pushed the users/OCHyams/ki-llvm-jumpthreading3 branch from 4dda51f to 3c02939 Compare May 7, 2025 15:01
@OCHyams OCHyams changed the base branch from users/OCHyams/ki-llvm-jumpthreading2 to main May 7, 2025 15:01
@OCHyams OCHyams merged commit 9602216 into main May 7, 2025
4 of 7 checks passed
@OCHyams OCHyams deleted the users/OCHyams/ki-llvm-jumpthreading3 branch May 7, 2025 15:01
OCHyams added a commit to OCHyams/llvm-project that referenced this pull request May 9, 2025
OCHyams added a commit to OCHyams/llvm-project that referenced this pull request May 9, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants