[llvm] [RemoveDIs] C API: Add before-dbg-record versions of IRBuilder position funcs (PR #92417)
Orlando Cazalet-Hyams via llvm-commits
llvm-commits at lists.llvm.org
Fri Jun 7 03:29:42 PDT 2024
https://github.com/OCHyams updated https://github.com/llvm/llvm-project/pull/92417
>From a41b77804bfe6786259c14b95c4f376c623c6123 Mon Sep 17 00:00:00 2001
From: Orlando Cazalet-Hyams <orlando.hyams at sony.com>
Date: Thu, 16 May 2024 10:12:56 +0100
Subject: [PATCH 01/10] Add BeforeDbgRecords option to C API insertion
functions
Inserting a PHI before an instruction with debug records attached results in
the following assert because the PHI needs to be inserted before its debug
records too.
llvm/lib/IR/Instruction.cpp:166: void llvm::Instruction::insertBefore(llvm::BasicBlock&, llvm::iplist_impl<llvm::simple_ilist<llvm::Instruction, llvm::ilist_iterator_bits<true> >, llvm::SymbolTableListTraits<llvm::Instruction, llvm::ilist_iterator_bits<true> > >::iterator): Assertion `!isa<PHINode>(this) && "Inserting PHI after debug-records!"' failed.
---
llvm/bindings/ocaml/llvm/llvm.ml | 16 ++++++------
llvm/bindings/ocaml/llvm/llvm.mli | 25 +++++++++++-------
llvm/bindings/ocaml/llvm/llvm_ocaml.c | 6 ++---
llvm/docs/ReleaseNotes.rst | 26 +++++++++++++++++++
llvm/docs/RemoveDIsDebugInfo.md | 10 +++++++
llvm/include/llvm-c/Core.h | 11 ++++++--
llvm/lib/IR/Core.cpp | 10 ++++---
llvm/test/Bindings/OCaml/core.ml | 4 +--
llvm/test/Bindings/OCaml/debuginfo.ml | 2 +-
llvm/test/Bindings/llvm-c/debug_info.ll | 3 +++
.../Bindings/llvm-c/debug_info_new_format.ll | 3 +++
llvm/tools/llvm-c-test/debuginfo.c | 19 +++++++++++++-
12 files changed, 106 insertions(+), 29 deletions(-)
diff --git a/llvm/bindings/ocaml/llvm/llvm.ml b/llvm/bindings/ocaml/llvm/llvm.ml
index 003fd750cd9f8..f339ea1bae1a4 100644
--- a/llvm/bindings/ocaml/llvm/llvm.ml
+++ b/llvm/bindings/ocaml/llvm/llvm.ml
@@ -1137,22 +1137,22 @@ external delete_instruction : llvalue -> unit = "llvm_delete_instruction"
(*===-- Instruction builders ----------------------------------------------===*)
external builder : llcontext -> llbuilder = "llvm_builder"
-external position_builder : (llbasicblock, llvalue) llpos -> llbuilder -> unit
- = "llvm_position_builder"
+external position_builder : (llbasicblock, llvalue) llpos -> bool -> llbuilder ->
+ unit = "llvm_position_builder"
external insertion_block : llbuilder -> llbasicblock = "llvm_insertion_block"
external insert_into_builder : llvalue -> string -> llbuilder -> unit
= "llvm_insert_into_builder"
-let builder_at context ip =
+let builder_at context ip before_dbg_records =
let b = builder context in
- position_builder ip b;
+ position_builder ip before_dbg_records b;
b
-let builder_before context i = builder_at context (Before i)
-let builder_at_end context bb = builder_at context (At_end bb)
+let builder_before context i before_dbg_records = builder_at context (Before i) before_dbg_records
+let builder_at_end context bb = builder_at context (At_end bb) false
-let position_before i = position_builder (Before i)
-let position_at_end bb = position_builder (At_end bb)
+let position_before i before_dbg_records = position_builder (Before i) before_dbg_records
+let position_at_end bb = position_builder (At_end bb) false
(*--... Metadata ...........................................................--*)
diff --git a/llvm/bindings/ocaml/llvm/llvm.mli b/llvm/bindings/ocaml/llvm/llvm.mli
index 93540c619efba..e2999fae656b2 100644
--- a/llvm/bindings/ocaml/llvm/llvm.mli
+++ b/llvm/bindings/ocaml/llvm/llvm.mli
@@ -1867,26 +1867,33 @@ val incoming : llvalue -> (llvalue * llbasicblock) list
for [llvm::LLVMBuilder]. *)
val builder : llcontext -> llbuilder
-(** [builder_at ip] creates an instruction builder positioned at [ip].
+(** [builder_at ip before_dbg_records] creates an instruction builder
+ positioned at [ip]. [before_dbg_records] determines whether the insertion
+ point is before debug records attached to [ip].
See the constructor for [llvm::LLVMBuilder]. *)
-val builder_at : llcontext -> (llbasicblock, llvalue) llpos -> llbuilder
+val builder_at : llcontext -> (llbasicblock, llvalue) llpos ->
+ bool -> llbuilder
-(** [builder_before ins] creates an instruction builder positioned before the
- instruction [isn]. See the constructor for [llvm::LLVMBuilder]. *)
-val builder_before : llcontext -> llvalue -> llbuilder
+(** [builder_before ins before_dbg_records] creates an instruction builder
+ positioned before the instruction [isn]. [before_dbg_records] determines
+ whether the insertion point is before debug records attached to [isn].
+ See the constructor for [llvm::LLVMBuilder]. *)
+val builder_before : llcontext -> llvalue -> bool -> llbuilder
(** [builder_at_end bb] creates an instruction builder positioned at the end of
the basic block [bb]. See the constructor for [llvm::LLVMBuilder]. *)
val builder_at_end : llcontext -> llbasicblock -> llbuilder
-(** [position_builder ip bb] moves the instruction builder [bb] to the position
- [ip].
+(** [position_builder ip bb before_dbg_records] moves the instruction builder
+ [bb] to the position [ip]. [before_dbg_records] determines whether the
+ insertion point is before debug records attached to [ip].
See the constructor for [llvm::LLVMBuilder]. *)
-val position_builder : (llbasicblock, llvalue) llpos -> llbuilder -> unit
+val position_builder : (llbasicblock, llvalue) llpos -> bool -> llbuilder ->
+ unit
(** [position_before ins b] moves the instruction builder [b] to before the
instruction [isn]. See the method [llvm::LLVMBuilder::SetInsertPoint]. *)
-val position_before : llvalue -> llbuilder -> unit
+val position_before : llvalue -> bool -> llbuilder -> unit
(** [position_at_end bb b] moves the instruction builder [b] to the end of the
basic block [bb]. See the method [llvm::LLVMBuilder::SetInsertPoint]. *)
diff --git a/llvm/bindings/ocaml/llvm/llvm_ocaml.c b/llvm/bindings/ocaml/llvm/llvm_ocaml.c
index 6d08d78b84455..cb0f6f8ce71c9 100644
--- a/llvm/bindings/ocaml/llvm/llvm_ocaml.c
+++ b/llvm/bindings/ocaml/llvm/llvm_ocaml.c
@@ -2016,14 +2016,14 @@ value llvm_builder(value C) {
return alloc_builder(LLVMCreateBuilderInContext(Context_val(C)));
}
-/* (llbasicblock, llvalue) llpos -> llbuilder -> unit */
-value llvm_position_builder(value Pos, value B) {
+/* (llbasicblock, llvalue) llpos -> bool -> llbuilder -> unit */
+value llvm_position_builder(value Pos, value BeforeDbgRecords, value B) {
if (Tag_val(Pos) == 0) {
LLVMBasicBlockRef BB = BasicBlock_val(Field(Pos, 0));
LLVMPositionBuilderAtEnd(Builder_val(B), BB);
} else {
LLVMValueRef I = Value_val(Field(Pos, 0));
- LLVMPositionBuilderBefore(Builder_val(B), I);
+ LLVMPositionBuilderBefore(Builder_val(B), I, Bool_val(BeforeDbgRecords));
}
return Val_unit;
}
diff --git a/llvm/docs/ReleaseNotes.rst b/llvm/docs/ReleaseNotes.rst
index 84320461fa9e1..23b249ae22ff5 100644
--- a/llvm/docs/ReleaseNotes.rst
+++ b/llvm/docs/ReleaseNotes.rst
@@ -180,6 +180,32 @@ Changes to the C API
* ``LLVMGetCallBrNumIndirectDests``
* ``LLVMGetCallBrIndirectDest``
+* Added the following functions for querying and setting the debug info format and for explicitly building either debug intrinsics (old format) or records (new format). These functions will be deprecated and removed in the future as we move towards removing support for the old debug format. See the `debug info migration guide <https://llvm.org/docs/RemoveDIsDebugInfo.html>`_ for more info.
+
+ * ``LLVMIsNewDbgInfoFormat``
+ * ``LLVMSetIsNewDbgInfoFormat``
+ * ``LLVMDIBuilderInsertDeclareIntrinsicBefore``
+ * ``LLVMDIBuilderInsertDeclareIntrinsicAtEnd``
+ * ``LLVMDIBuilderInsertDbgValueIntrinsicBefore``
+ * ``LLVMDIBuilderInsertDbgValueIntrinsicAtEnd``
+ * ``LLVMDIBuilderInsertDeclareRecordBefore``
+ * ``LLVMDIBuilderInsertDeclareRecordAtEnd``
+ * ``LLVMDIBuilderInsertDbgValueRecordBefore``
+ * ``LLVMDIBuilderInsertDbgValueRecordAtEnd``
+
+* Modified the following functions which now always insert debug records (new debug info format) rather than debug intrinsics (old debug info format). See the `debug info migration guide <https://llvm.org/docs/RemoveDIsDebugInfo.html>`_ for more info.
+
+ * ``LLVMDIBuilderInsertDeclareBefore``
+ * ``LLVMDIBuilderInsertDeclareBefore``
+ * ``LLVMDIBuilderInsertDeclareAtEnd``
+ * ``LLVMDIBuilderInsertDbgValueBefore``
+ * ``LLVMDIBuilderInsertDbgValueAtEnd``
+
+* Modified the following functions, adding a new `BeforeDbgRecords` parameter which indicates whether or not the insertion position should also be before the debug records that precede the instruction. See the `debug info migration guide <https://llvm.org/docs/RemoveDIsDebugInfo.html>`_ for more info.
+
+ * ``LLVMPositionBuilder``
+ * ``LLVMPositionBuilderBefore``
+
Changes to the CodeGen infrastructure
-------------------------------------
diff --git a/llvm/docs/RemoveDIsDebugInfo.md b/llvm/docs/RemoveDIsDebugInfo.md
index 9e50a2a604aa6..7036ead9e092c 100644
--- a/llvm/docs/RemoveDIsDebugInfo.md
+++ b/llvm/docs/RemoveDIsDebugInfo.md
@@ -56,8 +56,18 @@ LLVMDIBuilderInsertDeclareBefore # Insert a debug record (new debug info forma
LLVMDIBuilderInsertDeclareAtEnd # Same as above.
LLVMDIBuilderInsertDbgValueBefore # Same as above.
LLVMDIBuilderInsertDbgValueAtEnd # Same as above.
+
+Existing functions (new parameter)
+----------------------------------
+LLVMPositionBuilder # Added BeforeDbgRecords parameter. See info below.
+LLVMPositionBuilderBefore # Same as above.
```
+`LLVMPositionBuilder` and `LLVMPositionBuilderBefore` have gained a parameter `BeforeDbgRecords` which indicates whether or not the insertion position should also be before the debug records that precede the instruction. Note that this doesn't mean that debug intrinsics before the chosen instruction are skipped, only debug records (which unlike debug records are not themselves instructions).
+
+If you don't know whether it should be true or false then follow this rule:
+If you are trying to insert at the start of a block, or purposfully skip debug intrinsics to determine the insertion point for any other reason, then set it to true. Otherwise set it to false.
+
# Anything else?
Not really, but here's an "old vs new" comparison of how to do certain things and quickstart for how this "new" debug info is structured.
diff --git a/llvm/include/llvm-c/Core.h b/llvm/include/llvm-c/Core.h
index 9d09546513f0e..12f9b5e2e3cd2 100644
--- a/llvm/include/llvm-c/Core.h
+++ b/llvm/include/llvm-c/Core.h
@@ -3952,14 +3952,21 @@ const unsigned *LLVMGetIndices(LLVMValueRef Inst);
* An instruction builder represents a point within a basic block and is
* the exclusive means of building instructions using the C interface.
*
+ * Some insertion-position-setting methods have a "BeforeDbgRecords"
+ * parameter. Set to true if the insertion point should be in front of debug
+ * records, for example when inserting a phi. If you are trying to insert at the
+ * start of a block, or purposfully skip debug intrinsics to determine the
+ * insertion point for any other reason, then set it to true. Otherwise set it
+ * to false.
* @{
*/
LLVMBuilderRef LLVMCreateBuilderInContext(LLVMContextRef C);
LLVMBuilderRef LLVMCreateBuilder(void);
void LLVMPositionBuilder(LLVMBuilderRef Builder, LLVMBasicBlockRef Block,
- LLVMValueRef Instr);
-void LLVMPositionBuilderBefore(LLVMBuilderRef Builder, LLVMValueRef Instr);
+ LLVMValueRef Instr, LLVMBool BeforeDbgRecords);
+void LLVMPositionBuilderBefore(LLVMBuilderRef Builder, LLVMValueRef Instr,
+ LLVMBool BeforeDbgRecords);
void LLVMPositionBuilderAtEnd(LLVMBuilderRef Builder, LLVMBasicBlockRef Block);
LLVMBasicBlockRef LLVMGetInsertBlock(LLVMBuilderRef Builder);
void LLVMClearInsertionPosition(LLVMBuilderRef Builder);
diff --git a/llvm/lib/IR/Core.cpp b/llvm/lib/IR/Core.cpp
index df90b88341123..6e97a4222a7dc 100644
--- a/llvm/lib/IR/Core.cpp
+++ b/llvm/lib/IR/Core.cpp
@@ -3158,15 +3158,19 @@ LLVMBuilderRef LLVMCreateBuilder(void) {
}
void LLVMPositionBuilder(LLVMBuilderRef Builder, LLVMBasicBlockRef Block,
- LLVMValueRef Instr) {
+ LLVMValueRef Instr, LLVMBool BeforeDbgRecords) {
BasicBlock *BB = unwrap(Block);
auto I = Instr ? unwrap<Instruction>(Instr)->getIterator() : BB->end();
+ I.setHeadBit(BeforeDbgRecords);
unwrap(Builder)->SetInsertPoint(BB, I);
}
-void LLVMPositionBuilderBefore(LLVMBuilderRef Builder, LLVMValueRef Instr) {
+void LLVMPositionBuilderBefore(LLVMBuilderRef Builder, LLVMValueRef Instr,
+ LLVMBool BeforeDbgRecords) {
Instruction *I = unwrap<Instruction>(Instr);
- unwrap(Builder)->SetInsertPoint(I->getParent(), I->getIterator());
+ BasicBlock::iterator It = I->getIterator();
+ It.setHeadBit(BeforeDbgRecords);
+ unwrap(Builder)->SetInsertPoint(I->getParent(), It);
}
void LLVMPositionBuilderAtEnd(LLVMBuilderRef Builder, LLVMBasicBlockRef Block) {
diff --git a/llvm/test/Bindings/OCaml/core.ml b/llvm/test/Bindings/OCaml/core.ml
index 64bfa8ee412df..cd3e87c00628c 100644
--- a/llvm/test/Bindings/OCaml/core.ml
+++ b/llvm/test/Bindings/OCaml/core.ml
@@ -832,7 +832,7 @@ let test_instructions () =
let fty = function_type void_type [| i32_type; i32_type |] in
let f = define_function "f" fty m in
let bb = entry_block f in
- let b = builder_at context (At_end bb) in
+ let b = builder_at context (At_end bb) false in
insist (At_end bb = instr_begin bb);
insist (At_start bb = instr_end bb);
@@ -1152,7 +1152,7 @@ let test_builder () =
(* CHECK: ret{{.*}}P1
*)
let ret = build_ret p1 atentry in
- position_before ret atentry
+ position_before ret false atentry
end;
(* see test/Feature/exception.ll *)
diff --git a/llvm/test/Bindings/OCaml/debuginfo.ml b/llvm/test/Bindings/OCaml/debuginfo.ml
index f95800dfcb025..4f779743adaa0 100644
--- a/llvm/test/Bindings/OCaml/debuginfo.ml
+++ b/llvm/test/Bindings/OCaml/debuginfo.ml
@@ -273,7 +273,7 @@ let test_variables f dibuilder file_di fun_di =
stdout_metadata auto_var;
(* CHECK: [[LOCAL_VAR_PTR:<0x[0-9a-f]*>]] = !DILocalVariable(name: "my_local", scope: <{{0x[0-9a-f]*}}>, file: <{{0x[0-9a-f]*}}>, line: 10, type: [[INT64TY_PTR]])
*)
- let builder = Llvm.builder_before context entry_term in
+ let builder = Llvm.builder_before context entry_term false in
let all = Llvm.build_alloca (Llvm.i64_type context) "my_alloca" builder in
let scope =
Llvm_debuginfo.dibuild_create_lexical_block dibuilder ~scope:fun_di
diff --git a/llvm/test/Bindings/llvm-c/debug_info.ll b/llvm/test/Bindings/llvm-c/debug_info.ll
index 9358bac59bd21..71986fbb2348f 100644
--- a/llvm/test/Bindings/llvm-c/debug_info.ll
+++ b/llvm/test/Bindings/llvm-c/debug_info.ll
@@ -10,7 +10,10 @@
; CHECK-NEXT: call void @llvm.dbg.declare(metadata i64 0, metadata !40, metadata !DIExpression()), !dbg !43
; CHECK-NEXT: br label %vars
; CHECK: vars:
+; CHECK-NEXT: %p1 = phi i64 [ 0, %entry ]
+; CHECK-NEXT: %p2 = phi i64 [ 0, %entry ]
; CHECK-NEXT: call void @llvm.dbg.value(metadata i64 0, metadata !41, metadata !DIExpression(DW_OP_constu, 0, DW_OP_stack_value)), !dbg !44
+; CHECK-NEXT: %a = add i64 %p1, %p2
; CHECK-NEXT: ret i64 0
; CHECK-NEXT: }
diff --git a/llvm/test/Bindings/llvm-c/debug_info_new_format.ll b/llvm/test/Bindings/llvm-c/debug_info_new_format.ll
index 05b6ef4de9adb..1b6f2c4ea403a 100644
--- a/llvm/test/Bindings/llvm-c/debug_info_new_format.ll
+++ b/llvm/test/Bindings/llvm-c/debug_info_new_format.ll
@@ -11,7 +11,10 @@
; CHECK-NEXT: #dbg_declare(i64 0, !40, !DIExpression(), !43)
; CHECK-NEXT: br label %vars
; CHECK: vars:
+; CHECK-NEXT: %p1 = phi i64 [ 0, %entry ]
+; CHECK-NEXT: %p2 = phi i64 [ 0, %entry ]
; CHECK-NEXT: #dbg_value(i64 0, !41, !DIExpression(DW_OP_constu, 0, DW_OP_stack_value), !44)
+; CHECK-NEXT: %a = add i64 %p1, %p2
; CHECK-NEXT: ret i64 0
; CHECK-NEXT: }
diff --git a/llvm/tools/llvm-c-test/debuginfo.c b/llvm/tools/llvm-c-test/debuginfo.c
index 35c65f885af32..ed1cb4e30223a 100644
--- a/llvm/tools/llvm-c-test/debuginfo.c
+++ b/llvm/tools/llvm-c-test/debuginfo.c
@@ -228,7 +228,24 @@ int llvm_test_dibuilder(bool NewDebugInfoFormat) {
LLVMPositionBuilderAtEnd(Builder, FooVarBlock);
LLVMTypeRef I64 = LLVMInt64TypeInContext(Ctx);
LLVMValueRef Zero = LLVMConstInt(I64, 0, false);
- LLVMBuildRet(Builder, Zero);
+ LLVMValueRef Ret = LLVMBuildRet(Builder, Zero);
+
+ // Insert a `phi` before the `ret`. In the new debug info mode we need to
+ // be careful to insert before debug records too, else the debug records
+ // will come before the `phi` (and be absorbed onto it) which is an invalid
+ // state.
+ LLVMValueRef InsertPos = LLVMGetFirstInstruction(FooVarBlock);
+ LLVMPositionBuilderBefore(Builder, InsertPos, true);
+ LLVMValueRef Phi1 = LLVMBuildPhi(Builder, I64, "p1");
+ LLVMAddIncoming(Phi1, &Zero, &FooEntryBlock, 1);
+ // Do the same again using the other position-setting function.
+ LLVMPositionBuilder(Builder, FooVarBlock, InsertPos, true);
+ LLVMValueRef Phi2 = LLVMBuildPhi(Builder, I64, "p2");
+ LLVMAddIncoming(Phi2, &Zero, &FooEntryBlock, 1);
+ // Insert a non-phi before the `ret` but not before the debug records to
+ // test that works as expected.
+ LLVMPositionBuilder(Builder, FooVarBlock, Ret, false);
+ LLVMBuildAdd(Builder, Phi1, Phi2, "a");
char *MStr = LLVMPrintModuleToString(M);
puts(MStr);
>From 9d5fdfa5a590958ae42a3934d87ca1eebceaf13c Mon Sep 17 00:00:00 2001
From: Orlando Cazalet-Hyams <orlando.hyams at sony.com>
Date: Thu, 16 May 2024 16:27:30 +0100
Subject: [PATCH 02/10] remove other docs updates
---
llvm/docs/ReleaseNotes.rst | 21 ---------------------
1 file changed, 21 deletions(-)
diff --git a/llvm/docs/ReleaseNotes.rst b/llvm/docs/ReleaseNotes.rst
index 23b249ae22ff5..37e2827ed76cd 100644
--- a/llvm/docs/ReleaseNotes.rst
+++ b/llvm/docs/ReleaseNotes.rst
@@ -180,27 +180,6 @@ Changes to the C API
* ``LLVMGetCallBrNumIndirectDests``
* ``LLVMGetCallBrIndirectDest``
-* Added the following functions for querying and setting the debug info format and for explicitly building either debug intrinsics (old format) or records (new format). These functions will be deprecated and removed in the future as we move towards removing support for the old debug format. See the `debug info migration guide <https://llvm.org/docs/RemoveDIsDebugInfo.html>`_ for more info.
-
- * ``LLVMIsNewDbgInfoFormat``
- * ``LLVMSetIsNewDbgInfoFormat``
- * ``LLVMDIBuilderInsertDeclareIntrinsicBefore``
- * ``LLVMDIBuilderInsertDeclareIntrinsicAtEnd``
- * ``LLVMDIBuilderInsertDbgValueIntrinsicBefore``
- * ``LLVMDIBuilderInsertDbgValueIntrinsicAtEnd``
- * ``LLVMDIBuilderInsertDeclareRecordBefore``
- * ``LLVMDIBuilderInsertDeclareRecordAtEnd``
- * ``LLVMDIBuilderInsertDbgValueRecordBefore``
- * ``LLVMDIBuilderInsertDbgValueRecordAtEnd``
-
-* Modified the following functions which now always insert debug records (new debug info format) rather than debug intrinsics (old debug info format). See the `debug info migration guide <https://llvm.org/docs/RemoveDIsDebugInfo.html>`_ for more info.
-
- * ``LLVMDIBuilderInsertDeclareBefore``
- * ``LLVMDIBuilderInsertDeclareBefore``
- * ``LLVMDIBuilderInsertDeclareAtEnd``
- * ``LLVMDIBuilderInsertDbgValueBefore``
- * ``LLVMDIBuilderInsertDbgValueAtEnd``
-
* Modified the following functions, adding a new `BeforeDbgRecords` parameter which indicates whether or not the insertion position should also be before the debug records that precede the instruction. See the `debug info migration guide <https://llvm.org/docs/RemoveDIsDebugInfo.html>`_ for more info.
* ``LLVMPositionBuilder``
>From 2654011bf14ecc1a112845d0fa472cc605b31915 Mon Sep 17 00:00:00 2001
From: Orlando Cazalet-Hyams <orlando.hyams at sony.com>
Date: Mon, 20 May 2024 14:42:35 +0100
Subject: [PATCH 03/10] add new functions rather than update existing
---
llvm/docs/ReleaseNotes.rst | 5 +----
llvm/docs/RemoveDIsDebugInfo.md | 12 +++++++-----
llvm/include/llvm-c/Core.h | 16 +++++++++++-----
llvm/lib/IR/Core.cpp | 15 ++++++++++++---
4 files changed, 31 insertions(+), 17 deletions(-)
diff --git a/llvm/docs/ReleaseNotes.rst b/llvm/docs/ReleaseNotes.rst
index 37e2827ed76cd..cacd7ff2ffd1b 100644
--- a/llvm/docs/ReleaseNotes.rst
+++ b/llvm/docs/ReleaseNotes.rst
@@ -180,10 +180,7 @@ Changes to the C API
* ``LLVMGetCallBrNumIndirectDests``
* ``LLVMGetCallBrIndirectDest``
-* Modified the following functions, adding a new `BeforeDbgRecords` parameter which indicates whether or not the insertion position should also be before the debug records that precede the instruction. See the `debug info migration guide <https://llvm.org/docs/RemoveDIsDebugInfo.html>`_ for more info.
-
- * ``LLVMPositionBuilder``
- * ``LLVMPositionBuilderBefore``
+* Added ``LLVMPositionBuilder2`` and ``LLVMPositionBuilderBefore2``. Same as ``LLVMPositionBuilder`` and ``LLVMPositionBuilderBefore2`` with a new ``BeforeDbgRecords`` parameter which indicates whether or not the insertion position should also be before the debug records that precede the instruction. See the `debug info migration guide <https://llvm.org/docs/RemoveDIsDebugInfo.html>`_ for more info. ``LLVMPositionBuilder`` and ``LLVMPositionBuilderBefore`` are unchanged. They insert before the indicated instruction but after any attached debug records (equivalent to calling the new variants with ``BeforeDbgRecords`` set to ``false``).
Changes to the CodeGen infrastructure
-------------------------------------
diff --git a/llvm/docs/RemoveDIsDebugInfo.md b/llvm/docs/RemoveDIsDebugInfo.md
index 7036ead9e092c..1e767ea4d6c36 100644
--- a/llvm/docs/RemoveDIsDebugInfo.md
+++ b/llvm/docs/RemoveDIsDebugInfo.md
@@ -32,7 +32,7 @@ The second matter is that if you transfer sequences of instructions from one pla
# C-API changes
-All the functions that have been added are temporary and will be deprecated in the future. The intention is that they'll help downstream projects adapt during the transition period.
+Some new functions that have been added are temporary and will be deprecated in the future. The intention is that they'll help downstream projects adapt during the transition period.
```
New functions (all to be deprecated)
@@ -57,17 +57,19 @@ LLVMDIBuilderInsertDeclareAtEnd # Same as above.
LLVMDIBuilderInsertDbgValueBefore # Same as above.
LLVMDIBuilderInsertDbgValueAtEnd # Same as above.
-Existing functions (new parameter)
+New functions (no plans to deprecate)
----------------------------------
-LLVMPositionBuilder # Added BeforeDbgRecords parameter. See info below.
-LLVMPositionBuilderBefore # Same as above.
+LLVMPositionBuilder2 # LLVMPositionBuilder but with BeforeDbgRecords parameter. See info below.
+LLVMPositionBuilderBefore2 # Same as above.
```
-`LLVMPositionBuilder` and `LLVMPositionBuilderBefore` have gained a parameter `BeforeDbgRecords` which indicates whether or not the insertion position should also be before the debug records that precede the instruction. Note that this doesn't mean that debug intrinsics before the chosen instruction are skipped, only debug records (which unlike debug records are not themselves instructions).
+`LLVMPositionBuilder2` and `LLVMPositionBuilderBefore2` have a parameter `BeforeDbgRecords` which indicates whether or not the insertion position should also be before the debug records that precede the instruction. Note that this doesn't mean that debug intrinsics before the chosen instruction are skipped, only debug records (which unlike debug records are not themselves instructions).
If you don't know whether it should be true or false then follow this rule:
If you are trying to insert at the start of a block, or purposfully skip debug intrinsics to determine the insertion point for any other reason, then set it to true. Otherwise set it to false.
+`LLVMPositionBuilder` and `LLVMPositionBuilderBefore` are unchanged. They insert before the indicated instruction but after any attached debug records (equivalent to calling the new variants with `BeforeDbgRecords` set to `false`).
+
# Anything else?
Not really, but here's an "old vs new" comparison of how to do certain things and quickstart for how this "new" debug info is structured.
diff --git a/llvm/include/llvm-c/Core.h b/llvm/include/llvm-c/Core.h
index 12f9b5e2e3cd2..ce414bb58795f 100644
--- a/llvm/include/llvm-c/Core.h
+++ b/llvm/include/llvm-c/Core.h
@@ -3952,21 +3952,27 @@ const unsigned *LLVMGetIndices(LLVMValueRef Inst);
* An instruction builder represents a point within a basic block and is
* the exclusive means of building instructions using the C interface.
*
- * Some insertion-position-setting methods have a "BeforeDbgRecords"
+ * Some insertion-position-setting functions have a "BeforeDbgRecords"
* parameter. Set to true if the insertion point should be in front of debug
* records, for example when inserting a phi. If you are trying to insert at the
* start of a block, or purposfully skip debug intrinsics to determine the
* insertion point for any other reason, then set it to true. Otherwise set it
- * to false.
+ * to false. Insertion-position-setting functions without the parameter always
+ * set the position after any attached debug records (i.e., as if
+ * "BeforeDbgRecords" is false).
+ *
* @{
*/
LLVMBuilderRef LLVMCreateBuilderInContext(LLVMContextRef C);
LLVMBuilderRef LLVMCreateBuilder(void);
void LLVMPositionBuilder(LLVMBuilderRef Builder, LLVMBasicBlockRef Block,
- LLVMValueRef Instr, LLVMBool BeforeDbgRecords);
-void LLVMPositionBuilderBefore(LLVMBuilderRef Builder, LLVMValueRef Instr,
- LLVMBool BeforeDbgRecords);
+ LLVMValueRef Instr);
+void LLVMPositionBuilder2(LLVMBuilderRef Builder, LLVMBasicBlockRef Block,
+ LLVMValueRef Instr, LLVMBool BeforeDbgRecords);
+void LLVMPositionBuilderBefore(LLVMBuilderRef Builder, LLVMValueRef Instr);
+void LLVMPositionBuilderBefore2(LLVMBuilderRef Builder, LLVMValueRef Instr,
+ LLVMBool BeforeDbgRecords);
void LLVMPositionBuilderAtEnd(LLVMBuilderRef Builder, LLVMBasicBlockRef Block);
LLVMBasicBlockRef LLVMGetInsertBlock(LLVMBuilderRef Builder);
void LLVMClearInsertionPosition(LLVMBuilderRef Builder);
diff --git a/llvm/lib/IR/Core.cpp b/llvm/lib/IR/Core.cpp
index 6e97a4222a7dc..0fe806a8d171d 100644
--- a/llvm/lib/IR/Core.cpp
+++ b/llvm/lib/IR/Core.cpp
@@ -3158,15 +3158,24 @@ LLVMBuilderRef LLVMCreateBuilder(void) {
}
void LLVMPositionBuilder(LLVMBuilderRef Builder, LLVMBasicBlockRef Block,
- LLVMValueRef Instr, LLVMBool BeforeDbgRecords) {
+ LLVMValueRef Instr) {
+ return LLVMPositionBuilder2(Builder, Block, Instr, false);
+}
+
+void LLVMPositionBuilder2(LLVMBuilderRef Builder, LLVMBasicBlockRef Block,
+ LLVMValueRef Instr, LLVMBool BeforeDbgRecords) {
BasicBlock *BB = unwrap(Block);
auto I = Instr ? unwrap<Instruction>(Instr)->getIterator() : BB->end();
I.setHeadBit(BeforeDbgRecords);
unwrap(Builder)->SetInsertPoint(BB, I);
}
-void LLVMPositionBuilderBefore(LLVMBuilderRef Builder, LLVMValueRef Instr,
- LLVMBool BeforeDbgRecords) {
+void LLVMPositionBuilderBefore(LLVMBuilderRef Builder, LLVMValueRef Instr) {
+ return LLVMPositionBuilderBefore2(Builder, Instr, false);
+}
+
+void LLVMPositionBuilderBefore2(LLVMBuilderRef Builder, LLVMValueRef Instr,
+ LLVMBool BeforeDbgRecords) {
Instruction *I = unwrap<Instruction>(Instr);
BasicBlock::iterator It = I->getIterator();
It.setHeadBit(BeforeDbgRecords);
>From d198234b05c38948d6f5c2437f857dd1ed55d0af Mon Sep 17 00:00:00 2001
From: Orlando Cazalet-Hyams <orlando.hyams at sony.com>
Date: Mon, 20 May 2024 14:57:15 +0100
Subject: [PATCH 04/10] update llvm-c-test/debuginfo.c
---
llvm/tools/llvm-c-test/debuginfo.c | 6 +++---
1 file changed, 3 insertions(+), 3 deletions(-)
diff --git a/llvm/tools/llvm-c-test/debuginfo.c b/llvm/tools/llvm-c-test/debuginfo.c
index ed1cb4e30223a..476168aedd19d 100644
--- a/llvm/tools/llvm-c-test/debuginfo.c
+++ b/llvm/tools/llvm-c-test/debuginfo.c
@@ -235,16 +235,16 @@ int llvm_test_dibuilder(bool NewDebugInfoFormat) {
// will come before the `phi` (and be absorbed onto it) which is an invalid
// state.
LLVMValueRef InsertPos = LLVMGetFirstInstruction(FooVarBlock);
- LLVMPositionBuilderBefore(Builder, InsertPos, true);
+ LLVMPositionBuilderBefore2(Builder, InsertPos, true);
LLVMValueRef Phi1 = LLVMBuildPhi(Builder, I64, "p1");
LLVMAddIncoming(Phi1, &Zero, &FooEntryBlock, 1);
// Do the same again using the other position-setting function.
- LLVMPositionBuilder(Builder, FooVarBlock, InsertPos, true);
+ LLVMPositionBuilder2(Builder, FooVarBlock, InsertPos, true);
LLVMValueRef Phi2 = LLVMBuildPhi(Builder, I64, "p2");
LLVMAddIncoming(Phi2, &Zero, &FooEntryBlock, 1);
// Insert a non-phi before the `ret` but not before the debug records to
// test that works as expected.
- LLVMPositionBuilder(Builder, FooVarBlock, Ret, false);
+ LLVMPositionBuilder2(Builder, FooVarBlock, Ret, false);
LLVMBuildAdd(Builder, Phi1, Phi2, "a");
char *MStr = LLVMPrintModuleToString(M);
>From d856fc33d48187ada933f422ab3eb5fb3e6e6ce2 Mon Sep 17 00:00:00 2001
From: Orlando Cazalet-Hyams <orlando.hyams at sony.com>
Date: Mon, 20 May 2024 14:57:02 +0100
Subject: [PATCH 05/10] undo ocaml changes
---
llvm/bindings/ocaml/llvm/llvm.ml | 16 ++++++++--------
llvm/bindings/ocaml/llvm/llvm.mli | 25 +++++++++----------------
llvm/bindings/ocaml/llvm/llvm_ocaml.c | 6 +++---
llvm/test/Bindings/OCaml/core.ml | 4 ++--
llvm/test/Bindings/OCaml/debuginfo.ml | 2 +-
5 files changed, 23 insertions(+), 30 deletions(-)
diff --git a/llvm/bindings/ocaml/llvm/llvm.ml b/llvm/bindings/ocaml/llvm/llvm.ml
index f339ea1bae1a4..003fd750cd9f8 100644
--- a/llvm/bindings/ocaml/llvm/llvm.ml
+++ b/llvm/bindings/ocaml/llvm/llvm.ml
@@ -1137,22 +1137,22 @@ external delete_instruction : llvalue -> unit = "llvm_delete_instruction"
(*===-- Instruction builders ----------------------------------------------===*)
external builder : llcontext -> llbuilder = "llvm_builder"
-external position_builder : (llbasicblock, llvalue) llpos -> bool -> llbuilder ->
- unit = "llvm_position_builder"
+external position_builder : (llbasicblock, llvalue) llpos -> llbuilder -> unit
+ = "llvm_position_builder"
external insertion_block : llbuilder -> llbasicblock = "llvm_insertion_block"
external insert_into_builder : llvalue -> string -> llbuilder -> unit
= "llvm_insert_into_builder"
-let builder_at context ip before_dbg_records =
+let builder_at context ip =
let b = builder context in
- position_builder ip before_dbg_records b;
+ position_builder ip b;
b
-let builder_before context i before_dbg_records = builder_at context (Before i) before_dbg_records
-let builder_at_end context bb = builder_at context (At_end bb) false
+let builder_before context i = builder_at context (Before i)
+let builder_at_end context bb = builder_at context (At_end bb)
-let position_before i before_dbg_records = position_builder (Before i) before_dbg_records
-let position_at_end bb = position_builder (At_end bb) false
+let position_before i = position_builder (Before i)
+let position_at_end bb = position_builder (At_end bb)
(*--... Metadata ...........................................................--*)
diff --git a/llvm/bindings/ocaml/llvm/llvm.mli b/llvm/bindings/ocaml/llvm/llvm.mli
index e2999fae656b2..93540c619efba 100644
--- a/llvm/bindings/ocaml/llvm/llvm.mli
+++ b/llvm/bindings/ocaml/llvm/llvm.mli
@@ -1867,33 +1867,26 @@ val incoming : llvalue -> (llvalue * llbasicblock) list
for [llvm::LLVMBuilder]. *)
val builder : llcontext -> llbuilder
-(** [builder_at ip before_dbg_records] creates an instruction builder
- positioned at [ip]. [before_dbg_records] determines whether the insertion
- point is before debug records attached to [ip].
+(** [builder_at ip] creates an instruction builder positioned at [ip].
See the constructor for [llvm::LLVMBuilder]. *)
-val builder_at : llcontext -> (llbasicblock, llvalue) llpos ->
- bool -> llbuilder
+val builder_at : llcontext -> (llbasicblock, llvalue) llpos -> llbuilder
-(** [builder_before ins before_dbg_records] creates an instruction builder
- positioned before the instruction [isn]. [before_dbg_records] determines
- whether the insertion point is before debug records attached to [isn].
- See the constructor for [llvm::LLVMBuilder]. *)
-val builder_before : llcontext -> llvalue -> bool -> llbuilder
+(** [builder_before ins] creates an instruction builder positioned before the
+ instruction [isn]. See the constructor for [llvm::LLVMBuilder]. *)
+val builder_before : llcontext -> llvalue -> llbuilder
(** [builder_at_end bb] creates an instruction builder positioned at the end of
the basic block [bb]. See the constructor for [llvm::LLVMBuilder]. *)
val builder_at_end : llcontext -> llbasicblock -> llbuilder
-(** [position_builder ip bb before_dbg_records] moves the instruction builder
- [bb] to the position [ip]. [before_dbg_records] determines whether the
- insertion point is before debug records attached to [ip].
+(** [position_builder ip bb] moves the instruction builder [bb] to the position
+ [ip].
See the constructor for [llvm::LLVMBuilder]. *)
-val position_builder : (llbasicblock, llvalue) llpos -> bool -> llbuilder ->
- unit
+val position_builder : (llbasicblock, llvalue) llpos -> llbuilder -> unit
(** [position_before ins b] moves the instruction builder [b] to before the
instruction [isn]. See the method [llvm::LLVMBuilder::SetInsertPoint]. *)
-val position_before : llvalue -> bool -> llbuilder -> unit
+val position_before : llvalue -> llbuilder -> unit
(** [position_at_end bb b] moves the instruction builder [b] to the end of the
basic block [bb]. See the method [llvm::LLVMBuilder::SetInsertPoint]. *)
diff --git a/llvm/bindings/ocaml/llvm/llvm_ocaml.c b/llvm/bindings/ocaml/llvm/llvm_ocaml.c
index cb0f6f8ce71c9..6d08d78b84455 100644
--- a/llvm/bindings/ocaml/llvm/llvm_ocaml.c
+++ b/llvm/bindings/ocaml/llvm/llvm_ocaml.c
@@ -2016,14 +2016,14 @@ value llvm_builder(value C) {
return alloc_builder(LLVMCreateBuilderInContext(Context_val(C)));
}
-/* (llbasicblock, llvalue) llpos -> bool -> llbuilder -> unit */
-value llvm_position_builder(value Pos, value BeforeDbgRecords, value B) {
+/* (llbasicblock, llvalue) llpos -> llbuilder -> unit */
+value llvm_position_builder(value Pos, value B) {
if (Tag_val(Pos) == 0) {
LLVMBasicBlockRef BB = BasicBlock_val(Field(Pos, 0));
LLVMPositionBuilderAtEnd(Builder_val(B), BB);
} else {
LLVMValueRef I = Value_val(Field(Pos, 0));
- LLVMPositionBuilderBefore(Builder_val(B), I, Bool_val(BeforeDbgRecords));
+ LLVMPositionBuilderBefore(Builder_val(B), I);
}
return Val_unit;
}
diff --git a/llvm/test/Bindings/OCaml/core.ml b/llvm/test/Bindings/OCaml/core.ml
index cd3e87c00628c..64bfa8ee412df 100644
--- a/llvm/test/Bindings/OCaml/core.ml
+++ b/llvm/test/Bindings/OCaml/core.ml
@@ -832,7 +832,7 @@ let test_instructions () =
let fty = function_type void_type [| i32_type; i32_type |] in
let f = define_function "f" fty m in
let bb = entry_block f in
- let b = builder_at context (At_end bb) false in
+ let b = builder_at context (At_end bb) in
insist (At_end bb = instr_begin bb);
insist (At_start bb = instr_end bb);
@@ -1152,7 +1152,7 @@ let test_builder () =
(* CHECK: ret{{.*}}P1
*)
let ret = build_ret p1 atentry in
- position_before ret false atentry
+ position_before ret atentry
end;
(* see test/Feature/exception.ll *)
diff --git a/llvm/test/Bindings/OCaml/debuginfo.ml b/llvm/test/Bindings/OCaml/debuginfo.ml
index 4f779743adaa0..f95800dfcb025 100644
--- a/llvm/test/Bindings/OCaml/debuginfo.ml
+++ b/llvm/test/Bindings/OCaml/debuginfo.ml
@@ -273,7 +273,7 @@ let test_variables f dibuilder file_di fun_di =
stdout_metadata auto_var;
(* CHECK: [[LOCAL_VAR_PTR:<0x[0-9a-f]*>]] = !DILocalVariable(name: "my_local", scope: <{{0x[0-9a-f]*}}>, file: <{{0x[0-9a-f]*}}>, line: 10, type: [[INT64TY_PTR]])
*)
- let builder = Llvm.builder_before context entry_term false in
+ let builder = Llvm.builder_before context entry_term in
let all = Llvm.build_alloca (Llvm.i64_type context) "my_alloca" builder in
let scope =
Llvm_debuginfo.dibuild_create_lexical_block dibuilder ~scope:fun_di
>From 568e5b64ed80079b6c9b02a55d26ac557d4bfe8b Mon Sep 17 00:00:00 2001
From: Orlando Cazalet-Hyams <orlando.hyams at sony.com>
Date: Mon, 20 May 2024 15:23:26 +0100
Subject: [PATCH 06/10] update ocaml bindings
---
llvm/bindings/ocaml/llvm/llvm.ml | 3 +++
llvm/bindings/ocaml/llvm/llvm.mli | 12 ++++++++++++
llvm/bindings/ocaml/llvm/llvm_ocaml.c | 16 +++++++++++++---
llvm/test/Bindings/OCaml/core.ml | 2 +-
4 files changed, 29 insertions(+), 4 deletions(-)
diff --git a/llvm/bindings/ocaml/llvm/llvm.ml b/llvm/bindings/ocaml/llvm/llvm.ml
index 003fd750cd9f8..f168b7b9bb5fd 100644
--- a/llvm/bindings/ocaml/llvm/llvm.ml
+++ b/llvm/bindings/ocaml/llvm/llvm.ml
@@ -1139,6 +1139,8 @@ external delete_instruction : llvalue -> unit = "llvm_delete_instruction"
external builder : llcontext -> llbuilder = "llvm_builder"
external position_builder : (llbasicblock, llvalue) llpos -> llbuilder -> unit
= "llvm_position_builder"
+external position_builder2 : (llbasicblock, llvalue) llpos -> bool -> llbuilder ->
+ unit = "llvm_position_builder2"
external insertion_block : llbuilder -> llbasicblock = "llvm_insertion_block"
external insert_into_builder : llvalue -> string -> llbuilder -> unit
= "llvm_insert_into_builder"
@@ -1152,6 +1154,7 @@ let builder_before context i = builder_at context (Before i)
let builder_at_end context bb = builder_at context (At_end bb)
let position_before i = position_builder (Before i)
+let position_before_dbg_records i = position_builder2 (Before i) true
let position_at_end bb = position_builder (At_end bb)
diff --git a/llvm/bindings/ocaml/llvm/llvm.mli b/llvm/bindings/ocaml/llvm/llvm.mli
index 93540c619efba..aede24be30b95 100644
--- a/llvm/bindings/ocaml/llvm/llvm.mli
+++ b/llvm/bindings/ocaml/llvm/llvm.mli
@@ -1884,10 +1884,22 @@ val builder_at_end : llcontext -> llbasicblock -> llbuilder
See the constructor for [llvm::LLVMBuilder]. *)
val position_builder : (llbasicblock, llvalue) llpos -> llbuilder -> unit
+(** [position_builder2 ip bb before_dbg_records] moves the instruction builder
+ [bb] to the position [ip]. [before_dbg_records] determines whether the
+ insertion point is before debug records attached to [ip].
+ See the constructor for [llvm::LLVMBuilder]. *)
+val position_builder2 : (llbasicblock, llvalue) llpos -> bool -> llbuilder ->
+ unit
+
(** [position_before ins b] moves the instruction builder [b] to before the
instruction [isn]. See the method [llvm::LLVMBuilder::SetInsertPoint]. *)
val position_before : llvalue -> llbuilder -> unit
+(** [position_before_dbg_records ins b] moves the instruction builder [b] to
+ before the instruction [isn] and any debug records attached to it.
+ See the method [llvm::LLVMBuilder::SetInsertPoint]. *)
+val position_before : llvalue -> llbuilder -> unit
+
(** [position_at_end bb b] moves the instruction builder [b] to the end of the
basic block [bb]. See the method [llvm::LLVMBuilder::SetInsertPoint]. *)
val position_at_end : llbasicblock -> llbuilder -> unit
diff --git a/llvm/bindings/ocaml/llvm/llvm_ocaml.c b/llvm/bindings/ocaml/llvm/llvm_ocaml.c
index 6d08d78b84455..3d53ad758814a 100644
--- a/llvm/bindings/ocaml/llvm/llvm_ocaml.c
+++ b/llvm/bindings/ocaml/llvm/llvm_ocaml.c
@@ -2016,18 +2016,28 @@ value llvm_builder(value C) {
return alloc_builder(LLVMCreateBuilderInContext(Context_val(C)));
}
-/* (llbasicblock, llvalue) llpos -> llbuilder -> unit */
-value llvm_position_builder(value Pos, value B) {
+static value llvm_position_builder_impl(value Pos, value B,
+ LLVMBool BeforeDbgRecords) {
if (Tag_val(Pos) == 0) {
LLVMBasicBlockRef BB = BasicBlock_val(Field(Pos, 0));
LLVMPositionBuilderAtEnd(Builder_val(B), BB);
} else {
LLVMValueRef I = Value_val(Field(Pos, 0));
- LLVMPositionBuilderBefore(Builder_val(B), I);
+ LLVMPositionBuilderBefore2(Builder_val(B), I, BeforeDbgRecords);
}
return Val_unit;
}
+/* (llbasicblock, llvalue) llpos -> bool -> llbuilder -> unit */
+value llvm_position_builder2(value Pos, value BeforeDbgRecords, value B) {
+ return llvm_position_builder_impl(Pos, B, Bool_val(BeforeDbgRecords));
+}
+
+/* (llbasicblock, llvalue) llpos -> llbuilder -> unit */
+value llvm_position_builder(value Pos, value B) {
+ return llvm_position_builder_impl(Pos, B, /*BeforeDbgRecords=*/0);
+}
+
/* llbuilder -> llbasicblock */
value llvm_insertion_block(value B) {
LLVMBasicBlockRef InsertBlock = LLVMGetInsertBlock(Builder_val(B));
diff --git a/llvm/test/Bindings/OCaml/core.ml b/llvm/test/Bindings/OCaml/core.ml
index 64bfa8ee412df..609a56266df9d 100644
--- a/llvm/test/Bindings/OCaml/core.ml
+++ b/llvm/test/Bindings/OCaml/core.ml
@@ -1152,7 +1152,7 @@ let test_builder () =
(* CHECK: ret{{.*}}P1
*)
let ret = build_ret p1 atentry in
- position_before ret atentry
+ position_before_dbg_records ret atentry
end;
(* see test/Feature/exception.ll *)
>From 652708cf591e0bae3bc8f3472555327b0293cb75 Mon Sep 17 00:00:00 2001
From: Orlando Cazalet-Hyams <orlando.hyams at sony.com>
Date: Mon, 20 May 2024 15:38:54 +0100
Subject: [PATCH 07/10] typo
---
llvm/docs/ReleaseNotes.rst | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/llvm/docs/ReleaseNotes.rst b/llvm/docs/ReleaseNotes.rst
index cacd7ff2ffd1b..020308b7bbbe7 100644
--- a/llvm/docs/ReleaseNotes.rst
+++ b/llvm/docs/ReleaseNotes.rst
@@ -180,7 +180,7 @@ Changes to the C API
* ``LLVMGetCallBrNumIndirectDests``
* ``LLVMGetCallBrIndirectDest``
-* Added ``LLVMPositionBuilder2`` and ``LLVMPositionBuilderBefore2``. Same as ``LLVMPositionBuilder`` and ``LLVMPositionBuilderBefore2`` with a new ``BeforeDbgRecords`` parameter which indicates whether or not the insertion position should also be before the debug records that precede the instruction. See the `debug info migration guide <https://llvm.org/docs/RemoveDIsDebugInfo.html>`_ for more info. ``LLVMPositionBuilder`` and ``LLVMPositionBuilderBefore`` are unchanged. They insert before the indicated instruction but after any attached debug records (equivalent to calling the new variants with ``BeforeDbgRecords`` set to ``false``).
+* Added ``LLVMPositionBuilder2`` and ``LLVMPositionBuilderBefore2``. Same as ``LLVMPositionBuilder`` and ``LLVMPositionBuilderBefore`` with a new ``BeforeDbgRecords`` parameter which indicates whether or not the insertion position should also be before the debug records that precede the instruction. See the `debug info migration guide <https://llvm.org/docs/RemoveDIsDebugInfo.html>`_ for more info. ``LLVMPositionBuilder`` and ``LLVMPositionBuilderBefore`` are unchanged. They insert before the indicated instruction but after any attached debug records (equivalent to calling the new variants with ``BeforeDbgRecords`` set to ``false``).
Changes to the CodeGen infrastructure
-------------------------------------
>From 4495e4f4307ade7a3cf73028e46086f15f5d32d9 Mon Sep 17 00:00:00 2001
From: Orlando Cazalet-Hyams <orlando.hyams at sony.com>
Date: Thu, 6 Jun 2024 13:43:23 +0100
Subject: [PATCH 08/10] rework patch
* C: add new versions without bool param
* ocaml: position_before_dbg_records stays the same (equ. to LLVMPositionBuilderBeforeInstrAndDbgRecords)
* ocaml: position_builder2 -> position_builder_before_dbg_records (equ. to LLVMPositionBuilderBeforeDbgRecords)
---
llvm/bindings/ocaml/llvm/llvm.ml | 8 ++++---
llvm/bindings/ocaml/llvm/llvm.mli | 16 +++++++-------
llvm/bindings/ocaml/llvm/llvm_ocaml.c | 20 +++++++++--------
llvm/docs/ReleaseNotes.rst | 2 +-
llvm/docs/RemoveDIsDebugInfo.md | 12 +++++-----
llvm/include/llvm-c/Core.h | 9 ++++----
llvm/lib/IR/Core.cpp | 32 ++++++++++++++++-----------
llvm/tools/llvm-c-test/debuginfo.c | 6 ++---
8 files changed, 58 insertions(+), 47 deletions(-)
diff --git a/llvm/bindings/ocaml/llvm/llvm.ml b/llvm/bindings/ocaml/llvm/llvm.ml
index f168b7b9bb5fd..8bd0331990ecc 100644
--- a/llvm/bindings/ocaml/llvm/llvm.ml
+++ b/llvm/bindings/ocaml/llvm/llvm.ml
@@ -1139,8 +1139,9 @@ external delete_instruction : llvalue -> unit = "llvm_delete_instruction"
external builder : llcontext -> llbuilder = "llvm_builder"
external position_builder : (llbasicblock, llvalue) llpos -> llbuilder -> unit
= "llvm_position_builder"
-external position_builder2 : (llbasicblock, llvalue) llpos -> bool -> llbuilder ->
- unit = "llvm_position_builder2"
+external position_builder_before_dbg_records : (llbasicblock, llvalue) llpos ->
+ llbuilder -> unit
+ = "llvm_position_builder_before_dbg_records"
external insertion_block : llbuilder -> llbasicblock = "llvm_insertion_block"
external insert_into_builder : llvalue -> string -> llbuilder -> unit
= "llvm_insert_into_builder"
@@ -1154,7 +1155,8 @@ let builder_before context i = builder_at context (Before i)
let builder_at_end context bb = builder_at context (At_end bb)
let position_before i = position_builder (Before i)
-let position_before_dbg_records i = position_builder2 (Before i) true
+let position_before_dbg_records i =
+ position_builder_before_dbg_records (Before i)
let position_at_end bb = position_builder (At_end bb)
diff --git a/llvm/bindings/ocaml/llvm/llvm.mli b/llvm/bindings/ocaml/llvm/llvm.mli
index aede24be30b95..1598576cc02fe 100644
--- a/llvm/bindings/ocaml/llvm/llvm.mli
+++ b/llvm/bindings/ocaml/llvm/llvm.mli
@@ -1884,21 +1884,21 @@ val builder_at_end : llcontext -> llbasicblock -> llbuilder
See the constructor for [llvm::LLVMBuilder]. *)
val position_builder : (llbasicblock, llvalue) llpos -> llbuilder -> unit
-(** [position_builder2 ip bb before_dbg_records] moves the instruction builder
- [bb] to the position [ip]. [before_dbg_records] determines whether the
- insertion point is before debug records attached to [ip].
+(** [position_builder_before_dbg_records ip bb before_dbg_records] moves the
+ instruction builder [bb] to the position [ip], before any debug records
+ there.
See the constructor for [llvm::LLVMBuilder]. *)
-val position_builder2 : (llbasicblock, llvalue) llpos -> bool -> llbuilder ->
- unit
+val position_builder_before_dbg_records : (llbasicblock, llvalue) llpos ->
+ llbuilder -> unit
(** [position_before ins b] moves the instruction builder [b] to before the
instruction [isn]. See the method [llvm::LLVMBuilder::SetInsertPoint]. *)
val position_before : llvalue -> llbuilder -> unit
-(** [position_before_dbg_records ins b] moves the instruction builder [b] to
- before the instruction [isn] and any debug records attached to it.
+(** [position_before_dbg_records ins b] moves the instruction builder [b]
+ to before the instruction [isn] and any debug records attached to it.
See the method [llvm::LLVMBuilder::SetInsertPoint]. *)
-val position_before : llvalue -> llbuilder -> unit
+val position_before_dbg_records : llvalue -> llbuilder -> unit
(** [position_at_end bb b] moves the instruction builder [b] to the end of the
basic block [bb]. See the method [llvm::LLVMBuilder::SetInsertPoint]. *)
diff --git a/llvm/bindings/ocaml/llvm/llvm_ocaml.c b/llvm/bindings/ocaml/llvm/llvm_ocaml.c
index 3d53ad758814a..8d55084a8a015 100644
--- a/llvm/bindings/ocaml/llvm/llvm_ocaml.c
+++ b/llvm/bindings/ocaml/llvm/llvm_ocaml.c
@@ -2016,26 +2016,28 @@ value llvm_builder(value C) {
return alloc_builder(LLVMCreateBuilderInContext(Context_val(C)));
}
-static value llvm_position_builder_impl(value Pos, value B,
- LLVMBool BeforeDbgRecords) {
+/* (llbasicblock, llvalue) llpos -> llbuilder -> unit */
+value llvm_position_builder_before_dbg_records(value Pos, value B) {
if (Tag_val(Pos) == 0) {
LLVMBasicBlockRef BB = BasicBlock_val(Field(Pos, 0));
LLVMPositionBuilderAtEnd(Builder_val(B), BB);
} else {
LLVMValueRef I = Value_val(Field(Pos, 0));
- LLVMPositionBuilderBefore2(Builder_val(B), I, BeforeDbgRecords);
+ LLVMPositionBuilderBeforeInstrAndDbgRecords(Builder_val(B), I);
}
return Val_unit;
}
-/* (llbasicblock, llvalue) llpos -> bool -> llbuilder -> unit */
-value llvm_position_builder2(value Pos, value BeforeDbgRecords, value B) {
- return llvm_position_builder_impl(Pos, B, Bool_val(BeforeDbgRecords));
-}
-
/* (llbasicblock, llvalue) llpos -> llbuilder -> unit */
value llvm_position_builder(value Pos, value B) {
- return llvm_position_builder_impl(Pos, B, /*BeforeDbgRecords=*/0);
+ if (Tag_val(Pos) == 0) {
+ LLVMBasicBlockRef BB = BasicBlock_val(Field(Pos, 0));
+ LLVMPositionBuilderAtEnd(Builder_val(B), BB);
+ } else {
+ LLVMValueRef I = Value_val(Field(Pos, 0));
+ LLVMPositionBuilderBefore(Builder_val(B), I);
+ }
+ return Val_unit;
}
/* llbuilder -> llbasicblock */
diff --git a/llvm/docs/ReleaseNotes.rst b/llvm/docs/ReleaseNotes.rst
index 020308b7bbbe7..c3ab2808ff7c5 100644
--- a/llvm/docs/ReleaseNotes.rst
+++ b/llvm/docs/ReleaseNotes.rst
@@ -180,7 +180,7 @@ Changes to the C API
* ``LLVMGetCallBrNumIndirectDests``
* ``LLVMGetCallBrIndirectDest``
-* Added ``LLVMPositionBuilder2`` and ``LLVMPositionBuilderBefore2``. Same as ``LLVMPositionBuilder`` and ``LLVMPositionBuilderBefore`` with a new ``BeforeDbgRecords`` parameter which indicates whether or not the insertion position should also be before the debug records that precede the instruction. See the `debug info migration guide <https://llvm.org/docs/RemoveDIsDebugInfo.html>`_ for more info. ``LLVMPositionBuilder`` and ``LLVMPositionBuilderBefore`` are unchanged. They insert before the indicated instruction but after any attached debug records (equivalent to calling the new variants with ``BeforeDbgRecords`` set to ``false``).
+* Added ``LLVMPositionBuilderBeforeDbgRecords`` and ``LLVMPositionBuilderBeforeInstrAndDbgRecords``. Same as ``LLVMPositionBuilder`` and ``LLVMPositionBuilderBefore`` except the insertion position is set to before the debug records that precede the target instruction. See the `debug info migration guide <https://llvm.org/docs/RemoveDIsDebugInfo.html>`_ for more info. ``LLVMPositionBuilder`` and ``LLVMPositionBuilderBefore`` are unchanged; they insert before the indicated instruction but after any attached debug records.
Changes to the CodeGen infrastructure
-------------------------------------
diff --git a/llvm/docs/RemoveDIsDebugInfo.md b/llvm/docs/RemoveDIsDebugInfo.md
index 1e767ea4d6c36..7a8a82e76209b 100644
--- a/llvm/docs/RemoveDIsDebugInfo.md
+++ b/llvm/docs/RemoveDIsDebugInfo.md
@@ -59,16 +59,16 @@ LLVMDIBuilderInsertDbgValueAtEnd # Same as above.
New functions (no plans to deprecate)
----------------------------------
-LLVMPositionBuilder2 # LLVMPositionBuilder but with BeforeDbgRecords parameter. See info below.
-LLVMPositionBuilderBefore2 # Same as above.
+LLVMPositionBuilderBeforeDbgRecords # See info below.
+LLVMPositionBuilderBeforeInstrAndDbgRecords # See info below.
```
-`LLVMPositionBuilder2` and `LLVMPositionBuilderBefore2` have a parameter `BeforeDbgRecords` which indicates whether or not the insertion position should also be before the debug records that precede the instruction. Note that this doesn't mean that debug intrinsics before the chosen instruction are skipped, only debug records (which unlike debug records are not themselves instructions).
+`LLVMPositionBuilderBeforeDbgRecords` and `LLVMPositionBuilderBeforeInstrAndDbgRecords` behave the same as `LLVMPositionBuilder` and `LLVMPositionBuilderBefore` except the insition position is set before the debug records that precede the target instruction. Note that this doesn't mean that debug intrinsics before the chosen instruction are skipped, only debug records (which unlike debug records are not themselves instructions).
-If you don't know whether it should be true or false then follow this rule:
-If you are trying to insert at the start of a block, or purposfully skip debug intrinsics to determine the insertion point for any other reason, then set it to true. Otherwise set it to false.
+If you don't know which function to call then follow this rule:
+If you are trying to insert at the start of a block, or purposfully skip debug intrinsics to determine the insertion point for any other reason, then call the new functions.
-`LLVMPositionBuilder` and `LLVMPositionBuilderBefore` are unchanged. They insert before the indicated instruction but after any attached debug records (equivalent to calling the new variants with `BeforeDbgRecords` set to `false`).
+`LLVMPositionBuilder` and `LLVMPositionBuilderBefore` are unchanged. They insert before the indicated instruction but after any attached debug records.
# Anything else?
diff --git a/llvm/include/llvm-c/Core.h b/llvm/include/llvm-c/Core.h
index ce414bb58795f..c0865878d73e5 100644
--- a/llvm/include/llvm-c/Core.h
+++ b/llvm/include/llvm-c/Core.h
@@ -3968,11 +3968,12 @@ LLVMBuilderRef LLVMCreateBuilderInContext(LLVMContextRef C);
LLVMBuilderRef LLVMCreateBuilder(void);
void LLVMPositionBuilder(LLVMBuilderRef Builder, LLVMBasicBlockRef Block,
LLVMValueRef Instr);
-void LLVMPositionBuilder2(LLVMBuilderRef Builder, LLVMBasicBlockRef Block,
- LLVMValueRef Instr, LLVMBool BeforeDbgRecords);
+void LLVMPositionBuilderBeforeDbgRecords(LLVMBuilderRef Builder,
+ LLVMBasicBlockRef Block,
+ LLVMValueRef Inst);
void LLVMPositionBuilderBefore(LLVMBuilderRef Builder, LLVMValueRef Instr);
-void LLVMPositionBuilderBefore2(LLVMBuilderRef Builder, LLVMValueRef Instr,
- LLVMBool BeforeDbgRecords);
+void LLVMPositionBuilderBeforeInstrAndDbgRecords(LLVMBuilderRef Builder,
+ LLVMValueRef Instr);
void LLVMPositionBuilderAtEnd(LLVMBuilderRef Builder, LLVMBasicBlockRef Block);
LLVMBasicBlockRef LLVMGetInsertBlock(LLVMBuilderRef Builder);
void LLVMClearInsertionPosition(LLVMBuilderRef Builder);
diff --git a/llvm/lib/IR/Core.cpp b/llvm/lib/IR/Core.cpp
index 0fe806a8d171d..ced60e5b063e4 100644
--- a/llvm/lib/IR/Core.cpp
+++ b/llvm/lib/IR/Core.cpp
@@ -3157,29 +3157,35 @@ LLVMBuilderRef LLVMCreateBuilder(void) {
return LLVMCreateBuilderInContext(LLVMGetGlobalContext());
}
+static void LLVMPositionBuilderImpl(IRBuilder<> *Builder, BasicBlock *Block,
+ Instruction *Instr, bool BeforeDbgRecords) {
+ BasicBlock::iterator I = Instr ? Instr->getIterator() : Block->end();
+ I.setHeadBit(BeforeDbgRecords);
+ Builder->SetInsertPoint(Block, I);
+}
+
void LLVMPositionBuilder(LLVMBuilderRef Builder, LLVMBasicBlockRef Block,
LLVMValueRef Instr) {
- return LLVMPositionBuilder2(Builder, Block, Instr, false);
+ return LLVMPositionBuilderImpl(unwrap(Builder), unwrap(Block),
+ unwrap<Instruction>(Instr), false);
}
-void LLVMPositionBuilder2(LLVMBuilderRef Builder, LLVMBasicBlockRef Block,
- LLVMValueRef Instr, LLVMBool BeforeDbgRecords) {
- BasicBlock *BB = unwrap(Block);
- auto I = Instr ? unwrap<Instruction>(Instr)->getIterator() : BB->end();
- I.setHeadBit(BeforeDbgRecords);
- unwrap(Builder)->SetInsertPoint(BB, I);
+void LLVMPositionBuilderBeforeDbgRecords(LLVMBuilderRef Builder,
+ LLVMBasicBlockRef Block,
+ LLVMValueRef Instr) {
+ return LLVMPositionBuilderImpl(unwrap(Builder), unwrap(Block),
+ unwrap<Instruction>(Instr), true);
}
void LLVMPositionBuilderBefore(LLVMBuilderRef Builder, LLVMValueRef Instr) {
- return LLVMPositionBuilderBefore2(Builder, Instr, false);
+ Instruction *I = unwrap<Instruction>(Instr);
+ return LLVMPositionBuilderImpl(unwrap(Builder), I->getParent(), I, false);
}
-void LLVMPositionBuilderBefore2(LLVMBuilderRef Builder, LLVMValueRef Instr,
- LLVMBool BeforeDbgRecords) {
+void LLVMPositionBuilderBeforeInstrAndDbgRecords(LLVMBuilderRef Builder,
+ LLVMValueRef Instr) {
Instruction *I = unwrap<Instruction>(Instr);
- BasicBlock::iterator It = I->getIterator();
- It.setHeadBit(BeforeDbgRecords);
- unwrap(Builder)->SetInsertPoint(I->getParent(), It);
+ return LLVMPositionBuilderImpl(unwrap(Builder), I->getParent(), I, true);
}
void LLVMPositionBuilderAtEnd(LLVMBuilderRef Builder, LLVMBasicBlockRef Block) {
diff --git a/llvm/tools/llvm-c-test/debuginfo.c b/llvm/tools/llvm-c-test/debuginfo.c
index 476168aedd19d..032657203b5f2 100644
--- a/llvm/tools/llvm-c-test/debuginfo.c
+++ b/llvm/tools/llvm-c-test/debuginfo.c
@@ -235,16 +235,16 @@ int llvm_test_dibuilder(bool NewDebugInfoFormat) {
// will come before the `phi` (and be absorbed onto it) which is an invalid
// state.
LLVMValueRef InsertPos = LLVMGetFirstInstruction(FooVarBlock);
- LLVMPositionBuilderBefore2(Builder, InsertPos, true);
+ LLVMPositionBuilderBeforeInstrAndDbgRecords(Builder, InsertPos);
LLVMValueRef Phi1 = LLVMBuildPhi(Builder, I64, "p1");
LLVMAddIncoming(Phi1, &Zero, &FooEntryBlock, 1);
// Do the same again using the other position-setting function.
- LLVMPositionBuilder2(Builder, FooVarBlock, InsertPos, true);
+ LLVMPositionBuilderBeforeDbgRecords(Builder, FooVarBlock, InsertPos);
LLVMValueRef Phi2 = LLVMBuildPhi(Builder, I64, "p2");
LLVMAddIncoming(Phi2, &Zero, &FooEntryBlock, 1);
// Insert a non-phi before the `ret` but not before the debug records to
// test that works as expected.
- LLVMPositionBuilder2(Builder, FooVarBlock, Ret, false);
+ LLVMPositionBuilder(Builder, FooVarBlock, Ret);
LLVMBuildAdd(Builder, Phi1, Phi2, "a");
char *MStr = LLVMPrintModuleToString(M);
>From 790008a4dc62d235816cfe91ae5ab6f21af8169b Mon Sep 17 00:00:00 2001
From: Orlando Cazalet-Hyams <orlando.hyams at sony.com>
Date: Thu, 6 Jun 2024 16:53:11 +0100
Subject: [PATCH 09/10] update C API comments
---
llvm/include/llvm-c/Core.h | 23 ++++++++++++++---------
1 file changed, 14 insertions(+), 9 deletions(-)
diff --git a/llvm/include/llvm-c/Core.h b/llvm/include/llvm-c/Core.h
index c0865878d73e5..19d27cdbbf7bd 100644
--- a/llvm/include/llvm-c/Core.h
+++ b/llvm/include/llvm-c/Core.h
@@ -3952,26 +3952,31 @@ const unsigned *LLVMGetIndices(LLVMValueRef Inst);
* An instruction builder represents a point within a basic block and is
* the exclusive means of building instructions using the C interface.
*
- * Some insertion-position-setting functions have a "BeforeDbgRecords"
- * parameter. Set to true if the insertion point should be in front of debug
- * records, for example when inserting a phi. If you are trying to insert at the
- * start of a block, or purposfully skip debug intrinsics to determine the
- * insertion point for any other reason, then set it to true. Otherwise set it
- * to false. Insertion-position-setting functions without the parameter always
- * set the position after any attached debug records (i.e., as if
- * "BeforeDbgRecords" is false).
- *
* @{
*/
LLVMBuilderRef LLVMCreateBuilderInContext(LLVMContextRef C);
LLVMBuilderRef LLVMCreateBuilder(void);
+/**
+ * Set the builder poisiton before Instr but after any attached debug records,
+ * or if Instr is null set the position to the end of Block.
+ */
void LLVMPositionBuilder(LLVMBuilderRef Builder, LLVMBasicBlockRef Block,
LLVMValueRef Instr);
+/**
+ * Set the builder poisiton before Instr and any attached debug records,
+ * or if Instr is null set the position to the end of Block.
+ */
void LLVMPositionBuilderBeforeDbgRecords(LLVMBuilderRef Builder,
LLVMBasicBlockRef Block,
LLVMValueRef Inst);
+/**
+ * Set the builder poisiton before Instr but after any attached debug records.
+ */
void LLVMPositionBuilderBefore(LLVMBuilderRef Builder, LLVMValueRef Instr);
+/**
+ * Set the builder poisiton before Instr and any attached debug records.
+ */
void LLVMPositionBuilderBeforeInstrAndDbgRecords(LLVMBuilderRef Builder,
LLVMValueRef Instr);
void LLVMPositionBuilderAtEnd(LLVMBuilderRef Builder, LLVMBasicBlockRef Block);
>From d16ab0f3ad08365fa1657d34b0b4eb16ffcce8ef Mon Sep 17 00:00:00 2001
From: Orlando Cazalet-Hyams <orlandoch.och at gmail.com>
Date: Fri, 7 Jun 2024 11:29:31 +0100
Subject: [PATCH 10/10] Fix comment typo in Core.h
Co-authored-by: Nikita Popov <github at npopov.com>
---
llvm/include/llvm-c/Core.h | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/llvm/include/llvm-c/Core.h b/llvm/include/llvm-c/Core.h
index 19d27cdbbf7bd..e81d37147fd09 100644
--- a/llvm/include/llvm-c/Core.h
+++ b/llvm/include/llvm-c/Core.h
@@ -3958,7 +3958,7 @@ const unsigned *LLVMGetIndices(LLVMValueRef Inst);
LLVMBuilderRef LLVMCreateBuilderInContext(LLVMContextRef C);
LLVMBuilderRef LLVMCreateBuilder(void);
/**
- * Set the builder poisiton before Instr but after any attached debug records,
+ * Set the builder position before Instr but after any attached debug records,
* or if Instr is null set the position to the end of Block.
*/
void LLVMPositionBuilder(LLVMBuilderRef Builder, LLVMBasicBlockRef Block,
More information about the llvm-commits
mailing list