Skip to content

Commit b01e2a8

Browse files
authored
[llvm] Allow always dropping all llvm.type.test sequences
Currently, the `DropTypeTests` parameter only fully works with phi nodes and llvm.assume instructions. However, we'd like CFI to work in conjunction with FatLTO, in so far as the bitcode section should be able to contain the CFI instrumentation, while any incompatible bits are dropped when compiling the object code. To do that, we need to drop the llvm.type.test instructions everywhere, and not just their uses in phi nodes. This patch updates the LowerTypeTest pass so that uses are removed, and replaced with `true` in all cases, and not just in phi nodes. Addressing this will allow us to fix #112053 by modifying the FatLTO pipeline. Reviewers: pcc, nikic Reviewed By: pcc Pull Request: #112787
1 parent 51628fa commit b01e2a8

File tree

6 files changed

+75
-25
lines changed

6 files changed

+75
-25
lines changed

clang/lib/CodeGen/BackendUtil.cpp

+4-3
Original file line numberDiff line numberDiff line change
@@ -1013,9 +1013,10 @@ void EmitAssemblyHelper::RunOptimizationPipeline(
10131013
if (IsThinLTOPostLink)
10141014
PB.registerPipelineStartEPCallback(
10151015
[](ModulePassManager &MPM, OptimizationLevel Level) {
1016-
MPM.addPass(LowerTypeTestsPass(/*ExportSummary=*/nullptr,
1017-
/*ImportSummary=*/nullptr,
1018-
/*DropTypeTests=*/true));
1016+
MPM.addPass(LowerTypeTestsPass(
1017+
/*ExportSummary=*/nullptr,
1018+
/*ImportSummary=*/nullptr,
1019+
/*DropTypeTests=*/lowertypetests::DropTestKind::Assume));
10191020
});
10201021

10211022
// Register callbacks to schedule sanitizer passes at the appropriate part

llvm/include/llvm/Transforms/IPO/LowerTypeTests.h

+11-2
Original file line numberDiff line numberDiff line change
@@ -195,20 +195,29 @@ struct ByteArrayBuilder {
195195

196196
bool isJumpTableCanonical(Function *F);
197197

198+
/// Specifies how to drop type tests.
199+
enum class DropTestKind {
200+
None, /// Do not drop type tests (default).
201+
Assume, /// Drop only llvm.assumes using type test value.
202+
All, /// Drop the type test and all uses.
203+
};
204+
198205
} // end namespace lowertypetests
199206

200207
class LowerTypeTestsPass : public PassInfoMixin<LowerTypeTestsPass> {
201208
bool UseCommandLine = false;
202209

203210
ModuleSummaryIndex *ExportSummary = nullptr;
204211
const ModuleSummaryIndex *ImportSummary = nullptr;
205-
bool DropTypeTests = true;
212+
lowertypetests::DropTestKind DropTypeTests =
213+
lowertypetests::DropTestKind::None;
206214

207215
public:
208216
LowerTypeTestsPass() : UseCommandLine(true) {}
209217
LowerTypeTestsPass(ModuleSummaryIndex *ExportSummary,
210218
const ModuleSummaryIndex *ImportSummary,
211-
bool DropTypeTests = false)
219+
lowertypetests::DropTestKind DropTypeTests =
220+
lowertypetests::DropTestKind::None)
212221
: ExportSummary(ExportSummary), ImportSummary(ImportSummary),
213222
DropTypeTests(DropTypeTests) {}
214223
PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM);

llvm/lib/Passes/PassBuilderPipelines.cpp

+10-5
Original file line numberDiff line numberDiff line change
@@ -1135,7 +1135,8 @@ PassBuilder::buildModuleSimplificationPipeline(OptimizationLevel Level,
11351135
// post link pipeline after ICP. This is to enable usage of the type
11361136
// tests in ICP sequences.
11371137
if (Phase == ThinOrFullLTOPhase::ThinLTOPostLink)
1138-
MPM.addPass(LowerTypeTestsPass(nullptr, nullptr, true));
1138+
MPM.addPass(LowerTypeTestsPass(nullptr, nullptr,
1139+
lowertypetests::DropTestKind::Assume));
11391140

11401141
invokePipelineEarlySimplificationEPCallbacks(MPM, Level);
11411142

@@ -1750,7 +1751,8 @@ ModulePassManager PassBuilder::buildThinLTODefaultPipeline(
17501751
if (Level == OptimizationLevel::O0) {
17511752
// Run a second time to clean up any type tests left behind by WPD for use
17521753
// in ICP.
1753-
MPM.addPass(LowerTypeTestsPass(nullptr, nullptr, true));
1754+
MPM.addPass(LowerTypeTestsPass(nullptr, nullptr,
1755+
lowertypetests::DropTestKind::Assume));
17541756
// Drop available_externally and unreferenced globals. This is necessary
17551757
// with ThinLTO in order to avoid leaving undefined references to dead
17561758
// globals in the object file.
@@ -1801,7 +1803,8 @@ PassBuilder::buildLTODefaultPipeline(OptimizationLevel Level,
18011803
MPM.addPass(LowerTypeTestsPass(ExportSummary, nullptr));
18021804
// Run a second time to clean up any type tests left behind by WPD for use
18031805
// in ICP.
1804-
MPM.addPass(LowerTypeTestsPass(nullptr, nullptr, true));
1806+
MPM.addPass(LowerTypeTestsPass(nullptr, nullptr,
1807+
lowertypetests::DropTestKind::Assume));
18051808

18061809
invokeFullLinkTimeOptimizationLastEPCallbacks(MPM, Level);
18071810

@@ -1879,7 +1882,8 @@ PassBuilder::buildLTODefaultPipeline(OptimizationLevel Level,
18791882
// Run a second time to clean up any type tests left behind by WPD for use
18801883
// in ICP (which is performed earlier than this in the regular LTO
18811884
// pipeline).
1882-
MPM.addPass(LowerTypeTestsPass(nullptr, nullptr, true));
1885+
MPM.addPass(LowerTypeTestsPass(nullptr, nullptr,
1886+
lowertypetests::DropTestKind::Assume));
18831887

18841888
invokeFullLinkTimeOptimizationLastEPCallbacks(MPM, Level);
18851889

@@ -2060,7 +2064,8 @@ PassBuilder::buildLTODefaultPipeline(OptimizationLevel Level,
20602064
MPM.addPass(LowerTypeTestsPass(ExportSummary, nullptr));
20612065
// Run a second time to clean up any type tests left behind by WPD for use
20622066
// in ICP (which is performed earlier than this in the regular LTO pipeline).
2063-
MPM.addPass(LowerTypeTestsPass(nullptr, nullptr, true));
2067+
MPM.addPass(LowerTypeTestsPass(nullptr, nullptr,
2068+
lowertypetests::DropTestKind::Assume));
20642069

20652070
// Enable splitting late in the FullLTO post-link pipeline.
20662071
if (EnableHotColdSplit)

llvm/lib/Transforms/IPO/LowerTypeTests.cpp

+27-14
Original file line numberDiff line numberDiff line change
@@ -118,10 +118,16 @@ static cl::opt<std::string> ClWriteSummary(
118118
cl::desc("Write summary to given YAML file after running pass"),
119119
cl::Hidden);
120120

121-
static cl::opt<bool>
121+
static cl::opt<DropTestKind>
122122
ClDropTypeTests("lowertypetests-drop-type-tests",
123-
cl::desc("Simply drop type test assume sequences"),
124-
cl::Hidden, cl::init(false));
123+
cl::desc("Simply drop type test sequences"),
124+
cl::values(clEnumValN(DropTestKind::None, "none",
125+
"Do not drop any type tests"),
126+
clEnumValN(DropTestKind::Assume, "assume",
127+
"Drop type test assume sequences"),
128+
clEnumValN(DropTestKind::All, "all",
129+
"Drop all type test sequences")),
130+
cl::Hidden, cl::init(DropTestKind::None));
125131

126132
bool BitSetInfo::containsGlobalOffset(uint64_t Offset) const {
127133
if (Offset < ByteOffset)
@@ -399,7 +405,7 @@ class LowerTypeTestsModule {
399405
const ModuleSummaryIndex *ImportSummary;
400406
// Set when the client has invoked this to simply drop all type test assume
401407
// sequences.
402-
bool DropTypeTests;
408+
DropTestKind DropTypeTests;
403409

404410
Triple::ArchType Arch;
405411
Triple::OSType OS;
@@ -542,7 +548,7 @@ class LowerTypeTestsModule {
542548
LowerTypeTestsModule(Module &M, ModuleAnalysisManager &AM,
543549
ModuleSummaryIndex *ExportSummary,
544550
const ModuleSummaryIndex *ImportSummary,
545-
bool DropTypeTests);
551+
DropTestKind DropTypeTests);
546552

547553
bool lower();
548554

@@ -1828,9 +1834,10 @@ void LowerTypeTestsModule::buildBitSetsFromDisjointSet(
18281834
/// Lower all type tests in this module.
18291835
LowerTypeTestsModule::LowerTypeTestsModule(
18301836
Module &M, ModuleAnalysisManager &AM, ModuleSummaryIndex *ExportSummary,
1831-
const ModuleSummaryIndex *ImportSummary, bool DropTypeTests)
1837+
const ModuleSummaryIndex *ImportSummary, DropTestKind DropTypeTests)
18321838
: M(M), ExportSummary(ExportSummary), ImportSummary(ImportSummary),
1833-
DropTypeTests(DropTypeTests || ClDropTypeTests) {
1839+
DropTypeTests(ClDropTypeTests > DropTypeTests ? ClDropTypeTests
1840+
: DropTypeTests) {
18341841
assert(!(ExportSummary && ImportSummary));
18351842
Triple TargetTriple(M.getTargetTriple());
18361843
Arch = TargetTriple.getArch();
@@ -1882,7 +1889,7 @@ bool LowerTypeTestsModule::runForTesting(Module &M, ModuleAnalysisManager &AM) {
18821889
M, AM,
18831890
ClSummaryAction == PassSummaryAction::Export ? &Summary : nullptr,
18841891
ClSummaryAction == PassSummaryAction::Import ? &Summary : nullptr,
1885-
/*DropTypeTests*/ false)
1892+
/*DropTypeTests=*/DropTestKind::None)
18861893
.lower();
18871894

18881895
if (!ClWriteSummary.empty()) {
@@ -1949,7 +1956,8 @@ void LowerTypeTestsModule::replaceDirectCalls(Value *Old, Value *New) {
19491956
Old->replaceUsesWithIf(New, isDirectCall);
19501957
}
19511958

1952-
static void dropTypeTests(Module &M, Function &TypeTestFunc) {
1959+
static void dropTypeTests(Module &M, Function &TypeTestFunc,
1960+
bool ShouldDropAll) {
19531961
for (Use &U : llvm::make_early_inc_range(TypeTestFunc.uses())) {
19541962
auto *CI = cast<CallInst>(U.getUser());
19551963
// Find and erase llvm.assume intrinsics for this llvm.type.test call.
@@ -1959,9 +1967,13 @@ static void dropTypeTests(Module &M, Function &TypeTestFunc) {
19591967
// If the assume was merged with another assume, we might have a use on a
19601968
// phi (which will feed the assume). Simply replace the use on the phi
19611969
// with "true" and leave the merged assume.
1970+
//
1971+
// If ShouldDropAll is set, then we we need to update any remaining uses,
1972+
// regardless of the instruction type.
19621973
if (!CI->use_empty()) {
1963-
assert(
1964-
all_of(CI->users(), [](User *U) -> bool { return isa<PHINode>(U); }));
1974+
assert(ShouldDropAll || all_of(CI->users(), [](User *U) -> bool {
1975+
return isa<PHINode>(U);
1976+
}));
19651977
CI->replaceAllUsesWith(ConstantInt::getTrue(M.getContext()));
19661978
}
19671979
CI->eraseFromParent();
@@ -1972,16 +1984,17 @@ bool LowerTypeTestsModule::lower() {
19721984
Function *TypeTestFunc =
19731985
Intrinsic::getDeclarationIfExists(&M, Intrinsic::type_test);
19741986

1975-
if (DropTypeTests) {
1987+
if (DropTypeTests != DropTestKind::None) {
1988+
bool ShouldDropAll = DropTypeTests == DropTestKind::All;
19761989
if (TypeTestFunc)
1977-
dropTypeTests(M, *TypeTestFunc);
1990+
dropTypeTests(M, *TypeTestFunc, ShouldDropAll);
19781991
// Normally we'd have already removed all @llvm.public.type.test calls,
19791992
// except for in the case where we originally were performing ThinLTO but
19801993
// decided not to in the backend.
19811994
Function *PublicTypeTestFunc =
19821995
Intrinsic::getDeclarationIfExists(&M, Intrinsic::public_type_test);
19831996
if (PublicTypeTestFunc)
1984-
dropTypeTests(M, *PublicTypeTestFunc);
1997+
dropTypeTests(M, *PublicTypeTestFunc, ShouldDropAll);
19851998
if (TypeTestFunc || PublicTypeTestFunc) {
19861999
// We have deleted the type intrinsics, so we no longer have enough
19872000
// information to reason about the liveness of virtual function pointers
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
; RUN: opt -S -passes=lowertypetests -lowertypetests-drop-type-tests=all < %s | FileCheck %s
2+
3+
define void @func() {
4+
entry:
5+
%0 = tail call i1 @llvm.type.test(ptr null, metadata !"foo")
6+
br i1 %0, label %exit, label %trap
7+
8+
trap:
9+
unreachable
10+
11+
exit:
12+
ret void
13+
; CHECK-LABEL: entry:
14+
; CHECK-NEXT: br i1 true, label %exit, label %trap
15+
; CHECK-LABEL: trap:
16+
; CHECK-NEXT: unreachable
17+
; CHECK-LABEL: exit:
18+
; CHECK-NEXT: ret void
19+
}
20+
21+
declare i1 @llvm.type.test(ptr, metadata) #0
22+
attributes #0 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }

llvm/test/Transforms/LowerTypeTests/drop_type_test_phi.ll

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
; Test to ensure dropping of type tests can handle a phi feeding the assume.
2-
; RUN: opt -S -passes=lowertypetests -lowertypetests-drop-type-tests -mtriple=x86_64-unknown-linux-gnu %s | FileCheck %s
2+
; RUN: opt -S -passes=lowertypetests -lowertypetests-drop-type-tests=assume -mtriple=x86_64-unknown-linux-gnu %s | FileCheck %s
33

44
target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
55
target triple = "x86_64-grtev4-linux-gnu"

0 commit comments

Comments
 (0)