Skip to content
This repository was archived by the owner on Mar 28, 2020. It is now read-only.

Commit 85ab52a

Browse files
hiradityavedantk
authored andcommitted
Invalidate assumption cache before outlining.
Subscribers: llvm-commits Tags: #llvm Reviewers: compnerd, vsk, sebpop, fhahn, tejohnson Reviewed by: vsk Differential Revision: https://reviews.llvm.org/D68478 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@373807 91177308-0d34-0410-b5e6-96231b3b80d8 (cherry picked from commit 83e4df0)
1 parent d3669f5 commit 85ab52a

File tree

3 files changed

+27
-10
lines changed

3 files changed

+27
-10
lines changed

include/llvm/Transforms/Utils/CodeExtractor.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,11 @@ class Value;
106106
/// returns false.
107107
Function *extractCodeRegion();
108108

109+
/// Verify that assumption cache isn't stale after a region is extracted.
110+
/// Returns false when verifier finds errors. AssumptionCache is passed as
111+
/// parameter to make this function stateless.
112+
static bool verifyAssumptionCache(const Function& F, AssumptionCache *AC);
113+
109114
/// Test whether this code extractor is eligible.
110115
///
111116
/// Based on the blocks used when constructing the code extractor,

lib/Transforms/Utils/CodeExtractor.cpp

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1297,13 +1297,6 @@ void CodeExtractor::moveCodeToFunction(Function *newFunction) {
12971297

12981298
// Insert this basic block into the new function
12991299
newBlocks.push_back(Block);
1300-
1301-
// Remove @llvm.assume calls that were moved to the new function from the
1302-
// old function's assumption cache.
1303-
if (AC)
1304-
for (auto &I : *Block)
1305-
if (match(&I, m_Intrinsic<Intrinsic::assume>()))
1306-
AC->unregisterAssumption(cast<CallInst>(&I));
13071300
}
13081301
}
13091302

@@ -1374,6 +1367,15 @@ Function *CodeExtractor::extractCodeRegion() {
13741367
}
13751368
}
13761369

1370+
if (AC) {
1371+
// Remove @llvm.assume calls that were moved to the new function from the
1372+
// old function's assumption cache.
1373+
for (BasicBlock *Block : Blocks)
1374+
for (auto &I : *Block)
1375+
if (match(&I, m_Intrinsic<Intrinsic::assume>()))
1376+
AC->unregisterAssumption(cast<CallInst>(&I));
1377+
}
1378+
13771379
// If we have any return instructions in the region, split those blocks so
13781380
// that the return is not in the region.
13791381
splitReturnBlocks();
@@ -1564,5 +1566,17 @@ Function *CodeExtractor::extractCodeRegion() {
15641566
});
15651567
LLVM_DEBUG(if (verifyFunction(*oldFunction))
15661568
report_fatal_error("verification of oldFunction failed!"));
1569+
LLVM_DEBUG(if (AC && verifyAssumptionCache(*oldFunction, AC))
1570+
report_fatal_error("Stale Asumption cache for old Function!"));
15671571
return newFunction;
15681572
}
1573+
1574+
bool CodeExtractor::verifyAssumptionCache(const Function& F,
1575+
AssumptionCache *AC) {
1576+
for (auto AssumeVH : AC->assumptions()) {
1577+
CallInst *I = cast<CallInst>(AssumeVH);
1578+
if (I->getFunction() != &F)
1579+
return true;
1580+
}
1581+
return false;
1582+
}

unittests/Transforms/Utils/CodeExtractorTest.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ TEST(CodeExtractor, ExtractAndInvalidateAssumptionCache) {
236236
%b = type { i64 }
237237
declare void @g(i8*)
238238
239-
declare void @llvm.assume(i1) #0
239+
declare void @llvm.assume(i1)
240240
241241
define void @test() {
242242
entry:
@@ -259,8 +259,6 @@ TEST(CodeExtractor, ExtractAndInvalidateAssumptionCache) {
259259
call void @llvm.assume(i1 %4)
260260
unreachable
261261
}
262-
263-
attributes #0 = { nounwind willreturn }
264262
)ir",
265263
Err, Ctx));
266264

0 commit comments

Comments
 (0)