Skip to content

[RemoveDIs] C API: Add before-dbg-record versions of IRBuilder position funcs #92417

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 10 commits into from
Jun 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions llvm/bindings/ocaml/llvm/llvm.ml
Original file line number Diff line number Diff line change
Expand Up @@ -1135,6 +1135,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_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"
Expand All @@ -1148,6 +1151,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_builder_before_dbg_records (Before i)
let position_at_end bb = position_builder (At_end bb)


Expand Down
12 changes: 12 additions & 0 deletions llvm/bindings/ocaml/llvm/llvm.mli
Original file line number Diff line number Diff line change
Expand Up @@ -1874,10 +1874,22 @@ val builder_at_end : llcontext -> llbasicblock -> llbuilder
See the constructor for [llvm::LLVMBuilder]. *)
val position_builder : (llbasicblock, llvalue) llpos -> llbuilder -> unit

(** [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_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.
See the method [llvm::LLVMBuilder::SetInsertPoint]. *)
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]. *)
val position_at_end : llbasicblock -> llbuilder -> unit
Expand Down
12 changes: 12 additions & 0 deletions llvm/bindings/ocaml/llvm/llvm_ocaml.c
Original file line number Diff line number Diff line change
Expand Up @@ -2004,6 +2004,18 @@ value llvm_builder(value C) {
return alloc_builder(LLVMCreateBuilderInContext(Context_val(C)));
}

/* (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));
LLVMPositionBuilderBeforeInstrAndDbgRecords(Builder_val(B), I);
}
return Val_unit;
}

/* (llbasicblock, llvalue) llpos -> llbuilder -> unit */
value llvm_position_builder(value Pos, value B) {
if (Tag_val(Pos) == 0) {
Expand Down
2 changes: 2 additions & 0 deletions llvm/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,8 @@ Changes to the C API
* ``LLVMConstICmp``
* ``LLVMConstFCmp``

* 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
-------------------------------------

Expand Down
14 changes: 13 additions & 1 deletion llvm/docs/RemoveDIsDebugInfo.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ For a more in-depth overview of how to update existing code to support debug rec

# 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)
Expand All @@ -60,8 +60,20 @@ LLVMDIBuilderInsertDeclareBefore # Insert a debug record (new debug info forma
LLVMDIBuilderInsertDeclareAtEnd # Same as above.
LLVMDIBuilderInsertDbgValueBefore # Same as above.
LLVMDIBuilderInsertDbgValueAtEnd # Same as above.

New functions (no plans to deprecate)
----------------------------------
LLVMPositionBuilderBeforeDbgRecords # See info below.
LLVMPositionBuilderBeforeInstrAndDbgRecords # See info below.
```

`LLVMPositionBuilderBeforeDbgRecords` and `LLVMPositionBuilderBeforeInstrAndDbgRecords` behave the same as `LLVMPositionBuilder` and `LLVMPositionBuilderBefore` except the insertion 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 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.

# The new "Debug Record" model

Below is a brief overview of the new representation that replaces debug intrinsics; for an instructive guide on updating old code, see [here](#how-to-update-existing-code).
Expand Down
19 changes: 19 additions & 0 deletions llvm/include/llvm-c/Core.h
Original file line number Diff line number Diff line change
Expand Up @@ -3951,9 +3951,28 @@ const unsigned *LLVMGetIndices(LLVMValueRef Inst);

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);
LLVMBasicBlockRef LLVMGetInsertBlock(LLVMBuilderRef Builder);
void LLVMClearInsertionPosition(LLVMBuilderRef Builder);
Expand Down
27 changes: 23 additions & 4 deletions llvm/lib/IR/Core.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3137,16 +3137,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) {
BasicBlock *BB = unwrap(Block);
auto I = Instr ? unwrap<Instruction>(Instr)->getIterator() : BB->end();
unwrap(Builder)->SetInsertPoint(BB, I);
return LLVMPositionBuilderImpl(unwrap(Builder), unwrap(Block),
unwrap<Instruction>(Instr), false);
}

void LLVMPositionBuilderBeforeDbgRecords(LLVMBuilderRef Builder,
LLVMBasicBlockRef Block,
LLVMValueRef Instr) {
return LLVMPositionBuilderImpl(unwrap(Builder), unwrap(Block),
unwrap<Instruction>(Instr), true);
}

void LLVMPositionBuilderBefore(LLVMBuilderRef Builder, LLVMValueRef Instr) {
Instruction *I = unwrap<Instruction>(Instr);
unwrap(Builder)->SetInsertPoint(I->getParent(), I->getIterator());
return LLVMPositionBuilderImpl(unwrap(Builder), I->getParent(), I, false);
}

void LLVMPositionBuilderBeforeInstrAndDbgRecords(LLVMBuilderRef Builder,
LLVMValueRef Instr) {
Instruction *I = unwrap<Instruction>(Instr);
return LLVMPositionBuilderImpl(unwrap(Builder), I->getParent(), I, true);
}

void LLVMPositionBuilderAtEnd(LLVMBuilderRef Builder, LLVMBasicBlockRef Block) {
Expand Down
2 changes: 1 addition & 1 deletion llvm/test/Bindings/OCaml/core.ml
Original file line number Diff line number Diff line change
Expand Up @@ -1150,7 +1150,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 *)
Expand Down
3 changes: 3 additions & 0 deletions llvm/test/Bindings/llvm-c/debug_info.ll
Original file line number Diff line number Diff line change
Expand Up @@ -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: }

Expand Down
3 changes: 3 additions & 0 deletions llvm/test/Bindings/llvm-c/debug_info_new_format.ll
Original file line number Diff line number Diff line change
Expand Up @@ -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: }

Expand Down
19 changes: 18 additions & 1 deletion llvm/tools/llvm-c-test/debuginfo.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
LLVMPositionBuilderBeforeInstrAndDbgRecords(Builder, InsertPos);
LLVMValueRef Phi1 = LLVMBuildPhi(Builder, I64, "p1");
LLVMAddIncoming(Phi1, &Zero, &FooEntryBlock, 1);
// Do the same again using the other position-setting function.
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.
LLVMPositionBuilder(Builder, FooVarBlock, Ret);
LLVMBuildAdd(Builder, Phi1, Phi2, "a");

char *MStr = LLVMPrintModuleToString(M);
puts(MStr);
Expand Down
Loading