Skip to content

[KeyInstr][Clang] Break and Continue stmt atoms #141618

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

Conversation

OCHyams
Copy link
Contributor

@OCHyams OCHyams commented May 27, 2025

Based on top of #134646

This patch is part of a stack that teaches Clang to generate Key Instructions
metadata for C and C++.

RFC:
https://discourse.llvm.org/t/rfc-improving-is-stmt-placement-for-better-interactive-debugging/82668

The feature is only functional in LLVM if LLVM is built with CMake flag
LLVM_EXPERIMENTAL_KEY_INSTRUCTIONs. Eventually that flag will be removed.

@OCHyams OCHyams requested review from jmorse and SLTozer May 27, 2025 15:08
@llvmbot llvmbot added clang Clang issues not falling into any other category clang:codegen IR generation bugs: mangling, exceptions, etc. labels May 27, 2025
@llvmbot
Copy link
Member

llvmbot commented May 27, 2025

@llvm/pr-subscribers-clang-codegen

@llvm/pr-subscribers-clang

Author: Orlando Cazalet-Hyams (OCHyams)

Changes

Based on top of #134646

This patch is part of a stack that teaches Clang to generate Key Instructions
metadata for C and C++.

RFC:
https://discourse.llvm.org/t/rfc-improving-is-stmt-placement-for-better-interactive-debugging/82668

The feature is only functional in LLVM if LLVM is built with CMake flag
LLVM_EXPERIMENTAL_KEY_INSTRUCTIONs. Eventually that flag will be removed.


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

4 Files Affected:

  • (modified) clang/lib/CodeGen/CGCleanup.cpp (+1)
  • (modified) clang/lib/CodeGen/CGStmt.cpp (+2)
  • (modified) clang/test/DebugInfo/KeyInstructions/for.c (+30)
  • (modified) clang/test/DebugInfo/KeyInstructions/switch.c (+9-9)
diff --git a/clang/lib/CodeGen/CGCleanup.cpp b/clang/lib/CodeGen/CGCleanup.cpp
index 7e1c5b7da9552..4ed2c5183c47e 100644
--- a/clang/lib/CodeGen/CGCleanup.cpp
+++ b/clang/lib/CodeGen/CGCleanup.cpp
@@ -1118,6 +1118,7 @@ void CodeGenFunction::EmitBranchThroughCleanup(JumpDest Dest) {
 
   // Create the branch.
   llvm::BranchInst *BI = Builder.CreateBr(Dest.getBlock());
+  addInstToCurrentSourceAtom(BI, nullptr);
 
   // Calculate the innermost active normal cleanup.
   EHScopeStack::stable_iterator
diff --git a/clang/lib/CodeGen/CGStmt.cpp b/clang/lib/CodeGen/CGStmt.cpp
index cd2f05d419216..fe7b97d226a55 100644
--- a/clang/lib/CodeGen/CGStmt.cpp
+++ b/clang/lib/CodeGen/CGStmt.cpp
@@ -1715,6 +1715,7 @@ void CodeGenFunction::EmitBreakStmt(const BreakStmt &S) {
   if (HaveInsertPoint())
     EmitStopPoint(&S);
 
+  ApplyAtomGroup Grp(getDebugInfo());
   EmitBranchThroughCleanup(BreakContinueStack.back().BreakBlock);
 }
 
@@ -1727,6 +1728,7 @@ void CodeGenFunction::EmitContinueStmt(const ContinueStmt &S) {
   if (HaveInsertPoint())
     EmitStopPoint(&S);
 
+  ApplyAtomGroup Grp(getDebugInfo());
   EmitBranchThroughCleanup(BreakContinueStack.back().ContinueBlock);
 }
 
diff --git a/clang/test/DebugInfo/KeyInstructions/for.c b/clang/test/DebugInfo/KeyInstructions/for.c
index d1fc79292266b..876079924de26 100644
--- a/clang/test/DebugInfo/KeyInstructions/for.c
+++ b/clang/test/DebugInfo/KeyInstructions/for.c
@@ -93,6 +93,32 @@ void d() {
   }
 }
 
+void e() {
+// - Check the `continue` keyword gets an atom group.
+// CHECK: entry:
+// CHECK-NEXT: br label %for.cond
+
+// CHECK: for.cond:
+// CHECK: br label %for.cond, !dbg [[eG1R1:!.*]], !llvm.loop
+  for ( ; ; )
+  {
+    continue;
+  }
+}
+
+void f() {
+// - Check the `break` keyword gets an atom group.
+// CHECK: entry:
+// CHECK-NEXT: br label %for.cond
+
+// CHECK: for.cond:
+// CHECK: br label %for.end, !dbg [[fG1R1:!.*]]
+  for ( ; ; )
+  {
+    break;
+  }
+}
+
 // CHECK: [[G1R1]] = !DILocation({{.*}}, atomGroup: 1, atomRank: 1)
 // CHECK: [[G2R1]] = !DILocation({{.*}}, atomGroup: 2, atomRank: 1)
 // CHECK: [[G3R1]] = !DILocation({{.*}}, atomGroup: 3, atomRank: 1)
@@ -116,3 +142,7 @@ void d() {
 // CHECK: [[cG2R1]] = !DILocation({{.*}}, atomGroup: 2, atomRank: 1)
 
 // CHECK: [[dG1R1]] = !DILocation(line: 91, column: 3, scope: ![[#]], atomGroup: 1, atomRank: 1)
+
+// CHECK: [[eG1R1]] = !DILocation(line: 105, column: 5, scope: ![[#]], atomGroup: 1, atomRank: 1)
+
+// CHECK: [[fG1R1]] = !DILocation(line: 118, column: 5, scope: ![[#]], atomGroup: 1, atomRank: 1)
diff --git a/clang/test/DebugInfo/KeyInstructions/switch.c b/clang/test/DebugInfo/KeyInstructions/switch.c
index 5142f204ff587..96b18592621d5 100644
--- a/clang/test/DebugInfo/KeyInstructions/switch.c
+++ b/clang/test/DebugInfo/KeyInstructions/switch.c
@@ -16,18 +16,19 @@ void a(int A, int B) {
 // CHECK:   i32 1, label %sw.bb1
 // CHECK: ], !dbg [[G2R1:!.*]]
     switch ((g = A)) {
+// CHECK: br label %sw.epilog[[#]], !dbg [[G3R1:!.*]]
     case 0: break;
     case 1: {
 // CHECK: sw.bb1:
-// CHECK: %1 = load i32, ptr %B.addr{{.*}}, !dbg [[G3R2:!.*]]
+// CHECK: %1 = load i32, ptr %B.addr{{.*}}, !dbg [[G4R2:!.*]]
 // CHECK: switch i32 %1, label %{{.*}} [
 // CHECK:   i32 0, label %sw.bb2
-// CHECK: ], !dbg [[G3R1:!.*]]
+// CHECK: ], !dbg [[G4R1:!.*]]
     switch ((B)) {
         case 0: {
 // Test that assignments in constant-folded switches don't go missing.
 // CHECK-CXX: sw.bb2:
-// CHECK-CXX: store i32 1, ptr %C{{.*}}, !dbg [[G4R1:!.*]]
+// CHECK-CXX: store i32 1, ptr %C{{.*}}, !dbg [[G5R1:!.*]]
 #ifdef __cplusplus
             switch (const int C = 1; C) {
                 case 0: break;
@@ -35,17 +36,16 @@ void a(int A, int B) {
                 default: break;
             }
 #endif
-        } break;
-        default: break;
+        };
     }
-    } break;
-    default: break;
+    };
     }
 }
 
 // CHECK: [[G2R2]] = !DILocation({{.*}}, atomGroup: 2, atomRank: 2)
 // CHECK: [[G1R1]] = !DILocation({{.*}}, atomGroup: 1, atomRank: 1)
 // CHECK: [[G2R1]] = !DILocation({{.*}}, atomGroup: 2, atomRank: 1)
-// CHECK: [[G3R2]] = !DILocation({{.*}}, atomGroup: 3, atomRank: 2)
 // CHECK: [[G3R1]] = !DILocation({{.*}}, atomGroup: 3, atomRank: 1)
-// CHECK-CXX: [[G4R1]] = !DILocation({{.*}}, atomGroup: 4, atomRank: 1)
+// CHECK: [[G4R2]] = !DILocation({{.*}}, atomGroup: 4, atomRank: 2)
+// CHECK: [[G4R1]] = !DILocation({{.*}}, atomGroup: 4, atomRank: 1)
+// CHECK-CXX: [[G5R1]] = !DILocation({{.*}}, atomGroup: 5, atomRank: 1)

@OCHyams OCHyams force-pushed the users/OCHyams/ki-clang-for branch from 784005e to 0af9ceb Compare May 28, 2025 10:56
@OCHyams OCHyams force-pushed the users/OCHyams/ki-clang-continue-break branch from 9f9c3e1 to 9ae3232 Compare June 3, 2025 10:26
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.

LGTM, this all makes sense (to make break/continue their own key instructions). This involves cleanup scopes getting their own group: will destructors that run when "break"ing out of a loop get the key-ness of the "break" statement? (This might not be a bad thing at all).

@OCHyams OCHyams deleted the branch llvm:users/OCHyams/ki-clang-for June 3, 2025 12:47
@OCHyams OCHyams closed this Jun 3, 2025
@OCHyams
Copy link
Contributor Author

OCHyams commented Jun 3, 2025

I didn't close this, GitHub did (because I merged the base branch? for others that resulted in rebasing dependant branches on main... I don't understand why this one was different). I don't seem to be able to reopen it or convert it to a draft since the base branch no longer exists. I'll try rebasing...

@OCHyams
Copy link
Contributor Author

OCHyams commented Jun 3, 2025

Nope that didn't seem to work. I'll land it manually...

will destructors that run when "break"ing out of a loop get the key-ness of the "break" statement? (This might not be a bad thing at all).

Similar answer to elsewhere - calls aren't annotated by the front end, instead picked up in DWARF emission. If that changes, yes there's a risk, but imo it's a low risk.

@OCHyams
Copy link
Contributor Author

OCHyams commented Jun 3, 2025

Landed in 8e50e88

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang:codegen IR generation bugs: mangling, exceptions, etc. clang Clang issues not falling into any other category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants