[llvm] [IR] Add non-const DbgRecord::getInstruction() overload (PR #206059)

Konrad Kreczko via llvm-commits llvm-commits at lists.llvm.org
Fri Jun 26 06:41:08 PDT 2026


https://github.com/kkreczko updated https://github.com/llvm/llvm-project/pull/206059

>From e249b5820b6a55d03bebb5544068d9caf10294e8 Mon Sep 17 00:00:00 2001
From: "Kreczko, Konrad" <konrad.kreczko at intel.com>
Date: Fri, 26 Jun 2026 15:05:56 +0200
Subject: [PATCH 1/2] [IR] Add non-const DbgRecord::getInstruction() overload

DbgRecord exposes const+non-const overload pairs for getParent(),
getModule() and getContext(), but getInstruction() was const-only and
returned a const Instruction*. Code holding a non-const DbgRecord that
needs a mutable host instruction, for example to use as a DIBuilder
insert position, was therefore forced to const_cast the result.

Add a non-const getInstruction() overload returning a mutable
Instruction*, matching the existing accessor pairs and removing the need
for those casts. The const overload is unchanged.
---
 llvm/include/llvm/IR/DebugProgramInstruction.h | 6 ++++--
 llvm/lib/IR/DebugProgramInstruction.cpp        | 2 ++
 2 files changed, 6 insertions(+), 2 deletions(-)

diff --git a/llvm/include/llvm/IR/DebugProgramInstruction.h b/llvm/include/llvm/IR/DebugProgramInstruction.h
index b7cedc60689c7..66c312a113e54 100644
--- a/llvm/include/llvm/IR/DebugProgramInstruction.h
+++ b/llvm/include/llvm/IR/DebugProgramInstruction.h
@@ -190,6 +190,8 @@ class DbgRecord : public ilist_node<DbgRecord> {
   LLVM_ABI const LLVMContext &getContext() const;
 
   LLVM_ABI const Instruction *getInstruction() const;
+  LLVM_ABI Instruction *getInstruction();
+
   LLVM_ABI const BasicBlock *getParent() const;
   LLVM_ABI BasicBlock *getParent();
 
@@ -370,8 +372,8 @@ class DbgVariableRecord : public DbgRecord, protected DebugValueUser {
                         const DILocation *DI, DbgVariableRecord &InsertBefore);
 
   /// Iterator for ValueAsMetadata that internally uses direct pointer iteration
-  /// over either a ValueAsMetadata* or a ValueAsMetadata**, dereferencing to the
-  /// ValueAsMetadata .
+  /// over either a ValueAsMetadata* or a ValueAsMetadata**, dereferencing to
+  /// the ValueAsMetadata .
   class location_op_iterator
       : public iterator_facade_base<location_op_iterator,
                                     std::bidirectional_iterator_tag, Value *> {
diff --git a/llvm/lib/IR/DebugProgramInstruction.cpp b/llvm/lib/IR/DebugProgramInstruction.cpp
index 98335728665ba..fe25cd3959bfb 100644
--- a/llvm/lib/IR/DebugProgramInstruction.cpp
+++ b/llvm/lib/IR/DebugProgramInstruction.cpp
@@ -526,6 +526,8 @@ const Instruction *DbgRecord::getInstruction() const {
   return Marker->MarkedInstr;
 }
 
+Instruction *DbgRecord::getInstruction() { return Marker->MarkedInstr; }
+
 const BasicBlock *DbgRecord::getParent() const {
   return Marker->MarkedInstr->getParent();
 }

>From 6f09987b34f490ede9493dad5d7a7e380f3d7872 Mon Sep 17 00:00:00 2001
From: "Kreczko, Konrad" <konrad.kreczko at intel.com>
Date: Fri, 26 Jun 2026 15:36:38 +0200
Subject: [PATCH 2/2] Switch order of functions

---
 llvm/include/llvm/IR/DebugProgramInstruction.h | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/llvm/include/llvm/IR/DebugProgramInstruction.h b/llvm/include/llvm/IR/DebugProgramInstruction.h
index 66c312a113e54..50bb686ee3816 100644
--- a/llvm/include/llvm/IR/DebugProgramInstruction.h
+++ b/llvm/include/llvm/IR/DebugProgramInstruction.h
@@ -189,11 +189,11 @@ class DbgRecord : public ilist_node<DbgRecord> {
   LLVM_ABI LLVMContext &getContext();
   LLVM_ABI const LLVMContext &getContext() const;
 
-  LLVM_ABI const Instruction *getInstruction() const;
   LLVM_ABI Instruction *getInstruction();
+  LLVM_ABI const Instruction *getInstruction() const;
 
-  LLVM_ABI const BasicBlock *getParent() const;
   LLVM_ABI BasicBlock *getParent();
+  LLVM_ABI const BasicBlock *getParent() const;
 
   LLVM_ABI void removeFromParent();
   LLVM_ABI void eraseFromParent();



More information about the llvm-commits mailing list