aboutsummaryrefslogtreecommitdiff
path: root/llvm/lib/Transforms/Instrumentation/ControlHeightReduction.cpp
diff options
context:
space:
mode:
authorAiden Grossman <aidengrossman@google.com>2025-09-25 06:55:27 -0700
committerGitHub <noreply@github.com>2025-09-25 13:55:27 +0000
commitf9c2565117106467c3c8b1975e67c9750aee34e3 (patch)
tree0d0596317c8f76acbc690b5efbdb50ca984e2f96 /llvm/lib/Transforms/Instrumentation/ControlHeightReduction.cpp
parentf3f5d8bea836e01a2bc12edf5980c3a51de2fd7a (diff)
downloadllvm-f9c2565117106467c3c8b1975e67c9750aee34e3.zip
llvm-f9c2565117106467c3c8b1975e67c9750aee34e3.tar.gz
llvm-f9c2565117106467c3c8b1975e67c9750aee34e3.tar.bz2
Reapply "[ControlHeightReduction] Drop lifetime annotations where necessary" (#160640)
Reapplies #159686 This reverts commit 4f33d7b7a9f39d733b7572f9afbf178bca8da127. The original landing of this patch had an issue where it would try and hoist allocas into the entry block that were in the entry block. This would end up actually moving them lower in the block potentially after users, resulting in invalid IR. This update fixes this by ensuring that we are only hoisting static allocas that have been sunk into a split basic block. A regression test has been added. Integration tested using a three stage build of clang with IRPGO enabled.
Diffstat (limited to 'llvm/lib/Transforms/Instrumentation/ControlHeightReduction.cpp')
-rw-r--r--llvm/lib/Transforms/Instrumentation/ControlHeightReduction.cpp45
1 files changed, 37 insertions, 8 deletions
diff --git a/llvm/lib/Transforms/Instrumentation/ControlHeightReduction.cpp b/llvm/lib/Transforms/Instrumentation/ControlHeightReduction.cpp
index c14bbec..7c78eb3 100644
--- a/llvm/lib/Transforms/Instrumentation/ControlHeightReduction.cpp
+++ b/llvm/lib/Transforms/Instrumentation/ControlHeightReduction.cpp
@@ -1591,7 +1591,16 @@ static void insertTrivialPHIs(CHRScope *Scope,
}
TrivialPHIs.insert(PN);
CHR_DEBUG(dbgs() << "Insert phi " << *PN << "\n");
+ bool FoundLifetimeAnnotation = false;
for (Instruction *UI : Users) {
+ // If we found a lifetime annotation, remove it, but set a flag
+ // to ensure that we remove all other lifetime annotations attached
+ // to the alloca.
+ if (UI->isLifetimeStartOrEnd()) {
+ UI->eraseFromParent();
+ FoundLifetimeAnnotation = true;
+ continue;
+ }
for (unsigned J = 0, NumOps = UI->getNumOperands(); J < NumOps; ++J) {
if (UI->getOperand(J) == &I) {
UI->setOperand(J, PN);
@@ -1599,6 +1608,14 @@ static void insertTrivialPHIs(CHRScope *Scope,
}
CHR_DEBUG(dbgs() << "Updated user " << *UI << "\n");
}
+ // Erase any leftover lifetime annotations for a dynamic alloca.
+ if (FoundLifetimeAnnotation) {
+ for (User *U : make_early_inc_range(I.users())) {
+ if (auto *UI = dyn_cast<Instruction>(U))
+ if (UI->isLifetimeStartOrEnd())
+ UI->eraseFromParent();
+ }
+ }
}
}
}
@@ -1693,14 +1710,12 @@ void CHR::transformScopes(CHRScope *Scope, DenseSet<PHINode *> &TrivialPHIs) {
BasicBlock *ExitBlock = LastRegion->getExit();
std::optional<uint64_t> ProfileCount = BFI.getBlockProfileCount(EntryBlock);
- if (ExitBlock) {
- // Insert a trivial phi at the exit block (where the CHR hot path and the
- // cold path merges) for a value that's defined in the scope but used
- // outside it (meaning it's alive at the exit block). We will add the
- // incoming values for the CHR cold paths to it below. Without this, we'd
- // miss updating phi's for such values unless there happens to already be a
- // phi for that value there.
- insertTrivialPHIs(Scope, EntryBlock, ExitBlock, TrivialPHIs);
+ SmallVector<AllocaInst *> StaticAllocas;
+ for (Instruction &I : *EntryBlock) {
+ if (auto *AI = dyn_cast<AllocaInst>(&I)) {
+ if (AI->isStaticAlloca())
+ StaticAllocas.push_back(AI);
+ }
}
// Split the entry block of the first region. The new block becomes the new
@@ -1719,6 +1734,20 @@ void CHR::transformScopes(CHRScope *Scope, DenseSet<PHINode *> &TrivialPHIs) {
FirstRegion->replaceEntryRecursive(NewEntryBlock);
BasicBlock *PreEntryBlock = EntryBlock;
+ // Move static allocas into the pre-entry block so they stay static.
+ for (AllocaInst *AI : StaticAllocas)
+ AI->moveBefore(EntryBlock->begin());
+
+ if (ExitBlock) {
+ // Insert a trivial phi at the exit block (where the CHR hot path and the
+ // cold path merges) for a value that's defined in the scope but used
+ // outside it (meaning it's alive at the exit block). We will add the
+ // incoming values for the CHR cold paths to it below. Without this, we'd
+ // miss updating phi's for such values unless there happens to already be a
+ // phi for that value there.
+ insertTrivialPHIs(Scope, EntryBlock, ExitBlock, TrivialPHIs);
+ }
+
ValueToValueMapTy VMap;
// Clone the blocks in the scope (excluding the PreEntryBlock) to split into a
// hot path (originals) and a cold path (clones) and update the PHIs at the