Skip to content

Commit

Permalink
CheriBoundAllocas: Always use the alloca's debug loc for added instru…
Browse files Browse the repository at this point in the history
…ctions

IRBuilder defaults to using the debug loc for the instruction after the
insert point. For the instructions inserted before their uses, this is
somewhat sane, but really the debug loc should be that of the
declaration with the bounding just sunk to the use. For instructions
inserted after the alloca (both for dynamic bounds calculation and for
creating a new padded static alloca if necessary) these inherit the
debug loc of whatever happens to follow it, which is likely to be
either prologue code with no debug loc or something like a call to
@llvm.dbg.declare (as has been seen in -O0 code).

Since all this is logically part of the alloca (even when sunk), just
implemented via a CGP transformation to simplify CodeGen, explicitly set
the debug location to that of the alloca.

Whilst here, use a saner way to point the IRBuilder at one past the
alloca.
  • Loading branch information
jrtc27 committed Oct 24, 2021
1 parent 56ef4dd commit 460f86f
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions llvm/lib/CodeGen/CheriBoundAllocas.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -153,11 +153,13 @@ class CheriBoundAllocas : public ModulePass, public InstVisitor<CheriBoundAlloca
const uint64_t TotalUses = AI->getNumUses();
NumProcessed++;
Function *SetBoundsIntrin = BoundedStackFn;
// Insert immediately after the alloca
B.SetInsertPoint(AI);
B.SetInsertPoint(&*++B.GetInsertPoint());
Align ForcedAlignment;

// Insert immediately after the alloca, but inherit its debug loc rather
// than the next instruction's which is entirely unrelated
B.SetInsertPoint(AI->getNextNode());
B.SetCurrentDebugLocation(AI->getDebugLoc());

Align ForcedAlignment;
PointerType *AllocaTy = AI->getType();
assert(isCheriPointer(AllocaTy, &DL));
Type *AllocationTy = AllocaTy->getElementType();
Expand Down Expand Up @@ -346,6 +348,9 @@ class CheriBoundAllocas : public ModulePass, public InstVisitor<CheriBoundAlloca
// registers when using an alloca in a different basic block.
B.SetInsertPoint(I);
}
// Bounds should have debug loc of the alloca, not the instruction
// that happens to use them
B.SetCurrentDebugLocation(AI->getDebugLoc());
// We need to convert it to an i8* for the intrinisic. Note: we have
// to create a new bitcast every time since reusing the same one can
// cause the stack pointer + alloca offset register to be spilled
Expand Down

0 comments on commit 460f86f

Please sign in to comment.