Skip to content

[KeyInstr] Fix verifier check #149043

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 5 commits into from
Jul 16, 2025
Merged

[KeyInstr] Fix verifier check #149043

merged 5 commits into from
Jul 16, 2025

Conversation

OCHyams
Copy link
Contributor

@OCHyams OCHyams commented Jul 16, 2025

The verifier check was in the wrong place, meaning it wasn't actually checking many instructions.

Fixing that causes a test failure (coro-dwarf-key-instrs.cpp) because coros turn off the feature but still annotate instructions with the metadata (which is a supported situation, but the verifier doesn't like it, and it's hard to teach the verifier to like it).

Fix that by avoiding emitting any key instruction metadata if the DISubprogram has opted out of key instructions.

@llvmbot llvmbot added clang Clang issues not falling into any other category clang:codegen IR generation bugs: mangling, exceptions, etc. debuginfo llvm:ir labels Jul 16, 2025
@llvmbot
Copy link
Member

llvmbot commented Jul 16, 2025

@llvm/pr-subscribers-llvm-ir
@llvm/pr-subscribers-debuginfo

@llvm/pr-subscribers-clang

Author: Orlando Cazalet-Hyams (OCHyams)

Changes

The verifier check was in the wrong place, meaning it wasn't actually checking many instructions.

Fixing that causes a test failure (coro-dwarf-key-instrs.cpp) because coros turn off the feature but still annotate instructions with the metadata (which is a supported situation, but the verifier doesn't like it, and it's hard to teach the verifier to like it).

Fix that by avoiding emitting any key instruction metadata if the DISubprogram has opted out of key instructions.


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

2 Files Affected:

  • (modified) clang/lib/CodeGen/CGDebugInfo.cpp (+4)
  • (modified) llvm/lib/IR/Verifier.cpp (+7-6)
diff --git a/clang/lib/CodeGen/CGDebugInfo.cpp b/clang/lib/CodeGen/CGDebugInfo.cpp
index f97c7b6445984..0dde045453e3a 100644
--- a/clang/lib/CodeGen/CGDebugInfo.cpp
+++ b/clang/lib/CodeGen/CGDebugInfo.cpp
@@ -170,6 +170,10 @@ void CGDebugInfo::addInstToSpecificSourceAtom(llvm::Instruction *KeyInstruction,
   if (!Group || !CGM.getCodeGenOpts().DebugKeyInstructions)
     return;
 
+  llvm::DISubprogram *SP = KeyInstruction->getFunction()->getSubprogram();
+  if (!SP || !SP->getKeyInstructionsEnabled())
+    return;
+
   addInstSourceAtomMetadata(KeyInstruction, Group, /*Rank=*/1);
 
   llvm::Instruction *BackupI =
diff --git a/llvm/lib/IR/Verifier.cpp b/llvm/lib/IR/Verifier.cpp
index 8004077b92665..ccaa8ccba085d 100644
--- a/llvm/lib/IR/Verifier.cpp
+++ b/llvm/lib/IR/Verifier.cpp
@@ -3185,12 +3185,6 @@ void Verifier::visitFunction(const Function &F) {
     CheckDI(SP->describes(&F),
             "!dbg attachment points at wrong subprogram for function", N, &F,
             &I, DL, Scope, SP);
-
-    if (DL->getAtomGroup())
-      CheckDI(DL->getScope()->getSubprogram()->getKeyInstructionsEnabled(),
-              "DbgLoc uses atomGroup but DISubprogram doesn't have Key "
-              "Instructions enabled",
-              DL, DL->getScope()->getSubprogram());
   };
   for (auto &BB : F)
     for (auto &I : BB) {
@@ -5492,6 +5486,13 @@ void Verifier::visitInstruction(Instruction &I) {
   if (MDNode *N = I.getDebugLoc().getAsMDNode()) {
     CheckDI(isa<DILocation>(N), "invalid !dbg metadata attachment", &I, N);
     visitMDNode(*N, AreDebugLocsAllowed::Yes);
+
+    auto *DL = cast<DILocation>(N);
+    if (DL->getAtomGroup())
+      CheckDI(DL->getScope()->getSubprogram()->getKeyInstructionsEnabled(),
+              "DbgLoc uses atomGroup but DISubprogram doesn't have Key "
+              "Instructions enabled",
+              DL, DL->getScope()->getSubprogram());
   }
 
   if (auto *DII = dyn_cast<DbgVariableIntrinsic>(&I)) {

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.

Given that we've found a verifier error that necessitates this patch, it presumably wants a test.

@SLTozer
Copy link
Contributor

SLTozer commented Jul 16, 2025

So just to makes sure my understanding is correct: we have an existing bug and an existing test which should exercise that bug, but the verifier doesn't catch it currently. This patch then fixes both the verifier and the bug, and therefore a new test isn't needed for the non-verifier change because the coverage for the non-verifier change already exists in coro-dwarf-key-instrs.cpp - is that about right? And agreeing with the above, this should include a small test that the verifier check works.

@OCHyams
Copy link
Contributor Author

OCHyams commented Jul 16, 2025

So, to be clear:

  • Verifier check was checking very few instructions per function
  • Fixing the check causes coro-dwarf-key-instrs.cpp to fail
  • Fixing the problem stops coro-dwarf-key-instrs.cpp failing

I suppose I could add a check for the verifier condition independently of coro-dwarf-key-instrs.cpp ... coming right up

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.

LGTM with some inline nits.

Comment on lines 5491 to 5495
if (DL->getAtomGroup())
CheckDI(DL->getScope()->getSubprogram()->getKeyInstructionsEnabled(),
"DbgLoc uses atomGroup but DISubprogram doesn't have Key "
"Instructions enabled",
DL, DL->getScope()->getSubprogram());
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit, braces.

@@ -7,6 +7,8 @@

define dso_local void @f() !dbg !10 {
entry:
; include non-key location to check verifier is checking the whole function.
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
; include non-key location to check verifier is checking the whole function.
; Include non-key location to check verifier is checking the whole function.

@OCHyams OCHyams merged commit 653872f into llvm:main Jul 16, 2025
7 of 9 checks passed
@OCHyams OCHyams added this to the LLVM 21.x Release milestone Jul 16, 2025
@github-project-automation github-project-automation bot moved this to Needs Triage in LLVM Release Status Jul 16, 2025
@OCHyams
Copy link
Contributor Author

OCHyams commented Jul 16, 2025

/cherry-pick 653872f

@llvmbot
Copy link
Member

llvmbot commented Jul 16, 2025

/pull-request #149053

@llvmbot llvmbot moved this from Needs Triage to Done in LLVM Release Status Jul 16, 2025
tru pushed a commit to llvmbot/llvm-project that referenced this pull request Jul 24, 2025
The verifier check was in the wrong place, meaning it wasn't actually
checking many instructions.

Fixing that causes a test failure (coro-dwarf-key-instrs.cpp) because
coros turn off the feature but still annotate instructions with the
metadata (which is a supported situation, but the verifier doesn't like
it, and it's hard to teach the verifier to like it).

Fix that by avoiding emitting any key instruction metadata if the
DISubprogram has opted out of key instructions.

(cherry picked from commit 653872f)
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 debuginfo llvm:ir
Projects
Development

Successfully merging this pull request may close these issues.

4 participants