Skip to content

[KeyInstr][LoopUnswitch] Remap cloned instructions' atoms #133491

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

Conversation

OCHyams
Copy link
Contributor

@OCHyams OCHyams commented Mar 28, 2025

No description provided.

Copy link
Contributor Author

OCHyams commented Mar 28, 2025

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-llvm-transforms

@llvm/pr-subscribers-debuginfo

Author: Orlando Cazalet-Hyams (OCHyams)

Changes

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

2 Files Affected:

  • (modified) llvm/lib/Transforms/Scalar/SimpleLoopUnswitch.cpp (+4)
  • (added) llvm/test/DebugInfo/KeyInstructions/Generic/loop-unswitch.ll (+137)
diff --git a/llvm/lib/Transforms/Scalar/SimpleLoopUnswitch.cpp b/llvm/lib/Transforms/Scalar/SimpleLoopUnswitch.cpp
index 4f7956514b7b5..e0b403a18a718 100644
--- a/llvm/lib/Transforms/Scalar/SimpleLoopUnswitch.cpp
+++ b/llvm/lib/Transforms/Scalar/SimpleLoopUnswitch.cpp
@@ -297,6 +297,10 @@ static void buildPartialInvariantUnswitchConditionalBranch(
   for (auto *Val : reverse(ToDuplicate)) {
     Instruction *Inst = cast<Instruction>(Val);
     Instruction *NewInst = Inst->clone();
+
+    if (const DebugLoc &DL = Inst->getDebugLoc())
+      mapAtomInstance(DL, VMap);
+
     NewInst->insertInto(&BB, BB.end());
     RemapInstruction(NewInst, VMap,
                      RF_NoModuleLevelChanges | RF_IgnoreMissingLocals);
diff --git a/llvm/test/DebugInfo/KeyInstructions/Generic/loop-unswitch.ll b/llvm/test/DebugInfo/KeyInstructions/Generic/loop-unswitch.ll
new file mode 100644
index 0000000000000..9d696e78b2639
--- /dev/null
+++ b/llvm/test/DebugInfo/KeyInstructions/Generic/loop-unswitch.ll
@@ -0,0 +1,137 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 5
+; RUN: opt %s -S --passes="loop-mssa(simple-loop-unswitch<nontrivial>)" -o - \
+; RUN: | FileCheck %s
+
+;; The important thing here is that the instructions duplicated from
+;; LOOP_HEADER into ENTRY, and those duplicated from LOOP_LATCH into
+;; LOOP_LATCH_US, get remapped atom numbers.
+
+define i32 @partial_unswitch_true_successor_hoist_invariant(ptr %ptr, i32 %N) !dbg !5 {
+; CHECK-LABEL: define i32 @partial_unswitch_true_successor_hoist_invariant(
+; CHECK-SAME: ptr [[PTR:%.*]], i32 [[N:%.*]]) !dbg [[DBG5:![0-9]+]] {
+; CHECK-NEXT:  [[ENTRY:.*:]]
+; CHECK-NEXT:    [[TMP0:%.*]] = getelementptr i32, ptr [[PTR]], i64 1, !dbg [[DBG8:![0-9]+]]
+; CHECK-NEXT:    [[TMP1:%.*]] = load i32, ptr [[TMP0]], align 4, !dbg [[DBG9:![0-9]+]]
+; CHECK-NEXT:    [[TMP2:%.*]] = icmp eq i32 [[TMP1]], 100, !dbg [[DBG10:![0-9]+]]
+; CHECK-NEXT:    br i1 [[TMP2]], label %[[ENTRY_SPLIT_US:.*]], label %[[ENTRY_SPLIT:.*]]
+; CHECK:       [[ENTRY_SPLIT_US]]:
+; CHECK-NEXT:    br label %[[LOOP_HEADER_US:.*]], !dbg [[DBG11:![0-9]+]]
+; CHECK:       [[LOOP_HEADER_US]]:
+; CHECK-NEXT:    [[IV_US:%.*]] = phi i32 [ 0, %[[ENTRY_SPLIT_US]] ], [ [[IV_NEXT_US:%.*]], %[[LOOP_LATCH_US:.*]] ], !dbg [[DBG12:![0-9]+]]
+; CHECK-NEXT:    br label %[[NOCLOBBER_US:.*]], !dbg [[DBG13:![0-9]+]]
+; CHECK:       [[NOCLOBBER_US]]:
+; CHECK-NEXT:    br label %[[LOOP_LATCH_US]], !dbg [[DBG14:![0-9]+]]
+; CHECK:       [[LOOP_LATCH_US]]:
+; CHECK-NEXT:    [[C_US:%.*]] = icmp ult i32 [[IV_US]], [[N]], !dbg [[DBG15:![0-9]+]]
+; CHECK-NEXT:    [[IV_NEXT_US]] = add i32 [[IV_US]], 1, !dbg [[DBG16:![0-9]+]]
+; CHECK-NEXT:    br i1 [[C_US]], label %[[LOOP_HEADER_US]], label %[[EXIT_SPLIT_US:.*]], !dbg [[DBG17:![0-9]+]]
+; CHECK:       [[EXIT_SPLIT_US]]:
+; CHECK-NEXT:    br label %[[EXIT:.*]], !dbg [[DBG18:![0-9]+]]
+; CHECK:       [[ENTRY_SPLIT]]:
+; CHECK-NEXT:    br label %[[LOOP_HEADER:.*]], !dbg [[DBG19:![0-9]+]]
+; CHECK:       [[LOOP_HEADER]]:
+; CHECK-NEXT:    [[IV:%.*]] = phi i32 [ 0, %[[ENTRY_SPLIT]] ], [ [[IV_NEXT:%.*]], %[[LOOP_LATCH:.*]] ], !dbg [[DBG20:![0-9]+]]
+; CHECK-NEXT:    [[GEP:%.*]] = getelementptr i32, ptr [[PTR]], i64 1, !dbg [[DBG21:![0-9]+]]
+; CHECK-NEXT:    [[LV:%.*]] = load i32, ptr [[GEP]], align 4, !dbg [[DBG22:![0-9]+]]
+; CHECK-NEXT:    [[SC:%.*]] = icmp eq i32 [[LV]], 100, !dbg [[DBG23:![0-9]+]]
+; CHECK-NEXT:    br i1 [[SC]], label %[[NOCLOBBER:.*]], label %[[CLOBBER:.*]], !dbg [[DBG24:![0-9]+]]
+; CHECK:       [[NOCLOBBER]]:
+; CHECK-NEXT:    br label %[[LOOP_LATCH]], !dbg [[DBG25:![0-9]+]]
+; CHECK:       [[CLOBBER]]:
+; CHECK-NEXT:    call void @clobber(), !dbg [[DBG26:![0-9]+]]
+; CHECK-NEXT:    br label %[[LOOP_LATCH]], !dbg [[DBG27:![0-9]+]]
+; CHECK:       [[LOOP_LATCH]]:
+; CHECK-NEXT:    [[C:%.*]] = icmp ult i32 [[IV]], [[N]], !dbg [[DBG28:![0-9]+]]
+; CHECK-NEXT:    [[IV_NEXT]] = add i32 [[IV]], 1, !dbg [[DBG29:![0-9]+]]
+; CHECK-NEXT:    br i1 [[C]], label %[[LOOP_HEADER]], label %[[EXIT_SPLIT:.*]], !dbg [[DBG30:![0-9]+]], !llvm.loop [[LOOP31:![0-9]+]]
+; CHECK:       [[EXIT_SPLIT]]:
+; CHECK-NEXT:    br label %[[EXIT]], !dbg [[DBG33:![0-9]+]]
+; CHECK:       [[EXIT]]:
+; CHECK-NEXT:    ret i32 10, !dbg [[DBG33]]
+;
+entry:
+  br label %loop.header, !dbg !8
+
+loop.header:                                      ; preds = %loop.latch, %entry
+  %iv = phi i32 [ 0, %entry ], [ %iv.next, %loop.latch ], !dbg !9
+  %gep = getelementptr i32, ptr %ptr, i64 1, !dbg !10
+  %lv = load i32, ptr %gep, align 4, !dbg !11
+  %sc = icmp eq i32 %lv, 100, !dbg !12
+  br i1 %sc, label %noclobber, label %clobber, !dbg !13
+
+noclobber:                                        ; preds = %loop.header
+  br label %loop.latch, !dbg !14
+
+clobber:                                          ; preds = %loop.header
+  call void @clobber(), !dbg !15
+  br label %loop.latch, !dbg !16
+
+loop.latch:                                       ; preds = %clobber, %noclobber
+  %c = icmp ult i32 %iv, %N, !dbg !17
+  %iv.next = add i32 %iv, 1, !dbg !18
+  br i1 %c, label %loop.header, label %exit, !dbg !19
+
+exit:                                             ; preds = %loop.latch
+  ret i32 10, !dbg !20
+}
+
+declare void @clobber()
+
+!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 13}
+!3 = !{i32 0}
+!4 = !{i32 2, !"Debug Info Version", i32 3}
+!5 = distinct !DISubprogram(name: "partial_unswitch_true_successor_hoist_invariant", linkageName: "partial_unswitch_true_successor_hoist_invariant", 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: 1)
+!10 = !DILocation(line: 3, column: 1, scope: !5, atomGroup: 3, atomRank: 1)
+!11 = !DILocation(line: 4, column: 1, scope: !5, atomGroup: 4, atomRank: 1)
+!12 = !DILocation(line: 5, column: 1, scope: !5, atomGroup: 5, atomRank: 1)
+!13 = !DILocation(line: 6, column: 1, scope: !5, atomGroup: 6, atomRank: 1)
+!14 = !DILocation(line: 7, column: 1, scope: !5, atomGroup: 7, atomRank: 1)
+!15 = !DILocation(line: 8, column: 1, scope: !5, atomGroup: 8, atomRank: 1)
+!16 = !DILocation(line: 9, column: 1, scope: !5, atomGroup: 9, atomRank: 1)
+!17 = !DILocation(line: 10, column: 1, scope: !5, atomGroup: 10, atomRank: 1)
+!18 = !DILocation(line: 11, column: 1, scope: !5, atomGroup: 11, atomRank: 1)
+!19 = !DILocation(line: 12, column: 1, scope: !5, atomGroup: 12, atomRank: 1)
+!20 = !DILocation(line: 13, column: 1, scope: !5, atomGroup: 13, atomRank: 1)
+;.
+; CHECK: [[META0:![0-9]+]] = distinct !DICompileUnit(language: DW_LANG_C, file: [[META1:![0-9]+]], producer: "debugify", isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug)
+; CHECK: [[META1]] = !DIFile(filename: "test.ll", directory: {{.*}})
+; CHECK: [[DBG5]] = distinct !DISubprogram(name: "partial_unswitch_true_successor_hoist_invariant", linkageName: "partial_unswitch_true_successor_hoist_invariant", scope: null, file: [[META1]], line: 1, type: [[META6:![0-9]+]], scopeLine: 1, spFlags: DISPFlagDefinition | DISPFlagOptimized, unit: [[META0]])
+; CHECK: [[META6]] = !DISubroutineType(types: [[META7:![0-9]+]])
+; CHECK: [[META7]] = !{}
+; CHECK: [[DBG8]] = !DILocation(line: 3, column: 1, scope: [[DBG5]], atomGroup: 25, atomRank: 1)
+; CHECK: [[DBG9]] = !DILocation(line: 4, column: 1, scope: [[DBG5]], atomGroup: 26, atomRank: 1)
+; CHECK: [[DBG10]] = !DILocation(line: 5, column: 1, scope: [[DBG5]], atomGroup: 27, atomRank: 1)
+; CHECK: [[DBG11]] = !DILocation(line: 1, column: 1, scope: [[DBG5]], atomGroup: 14, atomRank: 1)
+; CHECK: [[DBG12]] = !DILocation(line: 2, column: 1, scope: [[DBG5]], atomGroup: 15, atomRank: 1)
+; CHECK: [[DBG13]] = !DILocation(line: 6, column: 1, scope: [[DBG5]], atomGroup: 19, atomRank: 1)
+; CHECK: [[DBG14]] = !DILocation(line: 7, column: 1, scope: [[DBG5]], atomGroup: 20, atomRank: 1)
+; CHECK: [[DBG15]] = !DILocation(line: 10, column: 1, scope: [[DBG5]], atomGroup: 21, atomRank: 1)
+; CHECK: [[DBG16]] = !DILocation(line: 11, column: 1, scope: [[DBG5]], atomGroup: 22, atomRank: 1)
+; CHECK: [[DBG17]] = !DILocation(line: 12, column: 1, scope: [[DBG5]], atomGroup: 23, atomRank: 1)
+; CHECK: [[DBG18]] = !DILocation(line: 13, column: 1, scope: [[DBG5]], atomGroup: 24, atomRank: 1)
+; CHECK: [[DBG19]] = !DILocation(line: 1, column: 1, scope: [[DBG5]], atomGroup: 1, atomRank: 1)
+; CHECK: [[DBG20]] = !DILocation(line: 2, column: 1, scope: [[DBG5]], atomGroup: 2, atomRank: 1)
+; CHECK: [[DBG21]] = !DILocation(line: 3, column: 1, scope: [[DBG5]], atomGroup: 3, atomRank: 1)
+; CHECK: [[DBG22]] = !DILocation(line: 4, column: 1, scope: [[DBG5]], atomGroup: 4, atomRank: 1)
+; CHECK: [[DBG23]] = !DILocation(line: 5, column: 1, scope: [[DBG5]], atomGroup: 5, atomRank: 1)
+; CHECK: [[DBG24]] = !DILocation(line: 6, column: 1, scope: [[DBG5]], atomGroup: 6, atomRank: 1)
+; CHECK: [[DBG25]] = !DILocation(line: 7, column: 1, scope: [[DBG5]], atomGroup: 7, atomRank: 1)
+; CHECK: [[DBG26]] = !DILocation(line: 8, column: 1, scope: [[DBG5]], atomGroup: 8, atomRank: 1)
+; CHECK: [[DBG27]] = !DILocation(line: 9, column: 1, scope: [[DBG5]], atomGroup: 9, atomRank: 1)
+; CHECK: [[DBG28]] = !DILocation(line: 10, column: 1, scope: [[DBG5]], atomGroup: 10, atomRank: 1)
+; CHECK: [[DBG29]] = !DILocation(line: 11, column: 1, scope: [[DBG5]], atomGroup: 11, atomRank: 1)
+; CHECK: [[DBG30]] = !DILocation(line: 12, column: 1, scope: [[DBG5]], atomGroup: 12, atomRank: 1)
+; CHECK: [[LOOP31]] = distinct !{[[LOOP31]], [[META32:![0-9]+]]}
+; CHECK: [[META32]] = !{!"llvm.loop.unswitch.partial.disable"}
+; CHECK: [[DBG33]] = !DILocation(line: 13, column: 1, scope: [[DBG5]], atomGroup: 13, 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.

Seems fine, but the test is hard to decipher, future generations will praise us if it can be simplified.

@OCHyams OCHyams force-pushed the users/OCHyams/ki-llvm-looprotate branch from 20a8533 to a612a99 Compare May 7, 2025 15:32
Base automatically changed from users/OCHyams/ki-llvm-looprotate to main May 7, 2025 16:10
@OCHyams OCHyams force-pushed the users/OCHyams/ki-llvm-loopunswitch branch from 1f03a5d to 4b9f1c7 Compare May 8, 2025 10:04
@OCHyams
Copy link
Contributor Author

OCHyams commented May 8, 2025

Seems fine, but the test is hard to decipher, future generations will praise us if it can be simplified.

A worthy challenge as it lead to me spotting #139070

(The final test isn't uploaded here yet - I'll wait for that commit first)

@OCHyams OCHyams force-pushed the users/OCHyams/ki-llvm-loopunswitch branch from 4b9f1c7 to 70ba119 Compare May 8, 2025 14:22
@OCHyams
Copy link
Contributor Author

OCHyams commented May 8, 2025

but the test is hard to decipher, future generations will praise us if it can be simplified.

Done my best to explain what's going on and what the expectations are. Does this look ok?

Copy link
Contributor

@SLTozer SLTozer left a comment

Choose a reason for hiding this comment

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

All LGTM, the test comments are particularly appreciated!

@OCHyams OCHyams merged commit fb7d114 into main May 9, 2025
9 of 11 checks passed
@OCHyams OCHyams deleted the users/OCHyams/ki-llvm-loopunswitch branch May 9, 2025 14:24
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.

4 participants