-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[llvm][fatlto] Add FatLTOCleanup pass #125911
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
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
//===- FatLtoCleanup.h - clean up IR for the FatLTO pipeline ----*- C++ -*-===// | ||
// | ||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// This file defines operations used to clean up IR for the FatLTO pipeline. | ||
// Instrumentation that is beneficial for bitcode sections used in LTO may | ||
// need to be cleaned up to finish non-LTO compilation. llvm.checked.load is | ||
// an example of an instruction that we want to preserve for LTO, but is | ||
// incorrect to leave unchanged during the per-TU compilation in FatLTO. | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#ifndef LLVM_TRANSFORMS_IPO_FATLTOCLEANUP_H | ||
#define LLVM_TRANSFORMS_IPO_FATLTOCLEANUP_H | ||
|
||
#include "llvm/IR/PassManager.h" | ||
|
||
namespace llvm { | ||
|
||
class Module; | ||
class ModuleSummaryIndex; | ||
|
||
class FatLtoCleanup : public PassInfoMixin<FatLtoCleanup> { | ||
public: | ||
FatLtoCleanup() {} | ||
PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM); | ||
nikic marked this conversation as resolved.
Show resolved
Hide resolved
|
||
static bool isRequired() { return true; } | ||
}; | ||
|
||
} // end namespace llvm | ||
|
||
#endif // LLVM_TRANSFORMS_IPO_FATLTOCLEANUP_H |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
//===- FatLtoCleanup.cpp - clean up IR for the FatLTO pipeline --*- C++ -*-===// | ||
// | ||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// This file defines operations used to clean up IR for the FatLTO pipeline. | ||
// Instrumentation that is beneficial for bitcode sections used in LTO may | ||
// need to be cleaned up to finish non-LTO compilation. llvm.checked.load is | ||
// an example of an instruction that we want to preserve for LTO, but is | ||
// incorrect to leave unchanged during the per-TU compilation in FatLTO. | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#include "llvm/Transforms/IPO/FatLTOCleanup.h" | ||
#include "llvm/IR/Function.h" | ||
#include "llvm/IR/IRBuilder.h" | ||
#include "llvm/IR/Intrinsics.h" | ||
#include "llvm/IR/Module.h" | ||
#include "llvm/IR/PassManager.h" | ||
#include "llvm/IR/Use.h" | ||
#include "llvm/Support/Debug.h" | ||
|
||
using namespace llvm; | ||
|
||
#define DEBUG_TYPE "fatlto-cleanup" | ||
|
||
namespace { | ||
// Replaces uses of llvm.type.checked.load instructions with unchecked loads. | ||
// In essence, we're undoing the frontends instrumentation, since it isn't | ||
// correct for the non-LTO part of a FatLTO object. | ||
// | ||
// llvm.type.checked.load instruction sequences always have a particular form: | ||
// | ||
// clang-format off | ||
// | ||
// %0 = tail call { ptr, i1 } @llvm.type.checked.load(ptr %vtable, i32 0, metadata !"foo"), !nosanitize !0 | ||
// %1 = extractvalue { ptr, i1 } %0, 1, !nosanitize !0 | ||
// br i1 %1, label %cont2, label %trap1, !nosanitize !0 | ||
// | ||
// trap1: ; preds = %entry | ||
// tail call void @llvm.ubsantrap(i8 2) #3, !nosanitize !0 | ||
// unreachable, !nosanitize !0 | ||
// | ||
// cont2: ; preds = %entry | ||
// %2 = extractvalue { ptr, i1 } %0, 0, !nosanitize !0 | ||
// %call = tail call noundef i64 %2(ptr noundef nonnull align 8 dereferenceable(8) %p1) #4 | ||
// | ||
// clang-format on | ||
// | ||
// In this sequence, the vtable pointer is first loaded and checked against some | ||
// metadata. The result indicates failure, then the program traps. On the | ||
// success path, the pointer is used to make an indirect call to the function | ||
// pointer loaded from the vtable. | ||
// | ||
// Since we won't be able to lower this correctly later in non-LTO builds, we | ||
// need to drop the special load and trap, and emit a normal load of the | ||
// function pointer from the vtable. | ||
// | ||
// This is straight forward, since the checked load can be replaced w/ a load | ||
// of the vtable pointer and a GEP instruction to index into the vtable and get | ||
// the correct method/function pointer. We replace the "check" with a constant | ||
// indicating success, which allows later passes to simplify control flow and | ||
// remove any now dead instructions. | ||
// | ||
// This logic holds for both llvm.type.checked.load and | ||
// llvm.type.checked.load.relative instructions. | ||
static bool cleanUpTypeCheckedLoad(Module &M, Function &CheckedLoadFn, | ||
bool IsRelative) { | ||
bool Changed = false; | ||
for (User *User : llvm::make_early_inc_range(CheckedLoadFn.users())) { | ||
Instruction *I = dyn_cast<Instruction>(User); | ||
if (!I) | ||
continue; | ||
IRBuilder<> IRB(I); | ||
Value *Ptr = I->getOperand(0); | ||
Value *Offset = I->getOperand(1); | ||
Type *PtrTy = I->getType()->getStructElementType(0); | ||
ConstantInt *True = ConstantInt::getTrue(M.getContext()); | ||
Instruction *Load; | ||
if (IsRelative) { | ||
Load = | ||
IRB.CreateIntrinsic(Intrinsic::load_relative, {Offset->getType()}, | ||
{Ptr, Offset}, /*FMFSource=*/nullptr, "rel_load"); | ||
} else { | ||
Value *PtrAdd = IRB.CreatePtrAdd(Ptr, Offset); | ||
Load = IRB.CreateLoad(PtrTy, PtrAdd, "vfunc"); | ||
} | ||
|
||
Value *Replacement = PoisonValue::get(I->getType()); | ||
Replacement = IRB.CreateInsertValue(Replacement, True, {1}); | ||
Replacement = IRB.CreateInsertValue(Replacement, Load, {0}); | ||
I->replaceAllUsesWith(Replacement); | ||
|
||
LLVM_DEBUG(dbgs() << DEBUG_TYPE << ": erase " << *I << "\n"); | ||
I->eraseFromParent(); | ||
Changed = true; | ||
} | ||
if (Changed) | ||
CheckedLoadFn.eraseFromParent(); | ||
return Changed; | ||
} | ||
} // namespace | ||
|
||
PreservedAnalyses FatLtoCleanup::run(Module &M, ModuleAnalysisManager &AM) { | ||
Function *TypeCheckedLoadFn = | ||
Intrinsic::getDeclarationIfExists(&M, Intrinsic::type_checked_load); | ||
Function *TypeCheckedLoadRelFn = Intrinsic::getDeclarationIfExists( | ||
&M, Intrinsic::type_checked_load_relative); | ||
|
||
bool Changed = false; | ||
if (TypeCheckedLoadFn) | ||
Changed |= cleanUpTypeCheckedLoad(M, *TypeCheckedLoadFn, false); | ||
if (TypeCheckedLoadRelFn) | ||
Changed |= cleanUpTypeCheckedLoad(M, *TypeCheckedLoadRelFn, true); | ||
|
||
if (Changed) | ||
return PreservedAnalyses::none(); | ||
return PreservedAnalyses::all(); | ||
} |
75 changes: 75 additions & 0 deletions
75
llvm/test/Transforms/FatLTOCleanup/remove-type-checked-load.ll
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 5 | ||
|
||
; RUN: opt -passes="fatlto-cleanup" < %s -S | FileCheck %s | ||
|
||
declare void @llvm.ubsantrap(i8 immarg) | ||
declare { ptr, i1 } @llvm.type.checked.load(ptr, i32, metadata) | ||
declare { ptr, i1 } @llvm.type.checked.load.relative(ptr, i32, metadata) | ||
|
||
define hidden void @foo(ptr %p1) { | ||
; CHECK-LABEL: define hidden void @foo( | ||
; CHECK-SAME: ptr [[P1:%.*]]) { | ||
; CHECK-NEXT: [[ENTRY:.*:]] | ||
; CHECK-NEXT: [[VTABLE:%.*]] = load ptr, ptr [[P1]], align 8 | ||
; CHECK-NEXT: [[TMP0:%.*]] = getelementptr i8, ptr [[VTABLE]], i32 0 | ||
; CHECK-NEXT: [[VFUNC:%.*]] = load ptr, ptr [[TMP0]], align 8 | ||
; CHECK-NEXT: [[TMP2:%.*]] = insertvalue { ptr, i1 } { ptr poison, i1 true }, ptr [[VFUNC]], 0 | ||
; CHECK-NEXT: [[TMP3:%.*]] = extractvalue { ptr, i1 } [[TMP2]], 1 | ||
; CHECK-NEXT: br i1 [[TMP3]], label %[[CONT2:.*]], label %[[TRAP1:.*]] | ||
; CHECK: [[TRAP1]]: | ||
; CHECK-NEXT: tail call void @llvm.ubsantrap(i8 2) | ||
; CHECK-NEXT: unreachable | ||
; CHECK: [[CONT2]]: | ||
; CHECK-NEXT: [[TMP4:%.*]] = extractvalue { ptr, i1 } [[TMP2]], 0 | ||
; CHECK-NEXT: [[CALL:%.*]] = tail call noundef i64 [[TMP4]](ptr noundef nonnull align 8 dereferenceable(8) [[P1]]) | ||
; CHECK-NEXT: ret void | ||
; | ||
entry: | ||
%vtable = load ptr, ptr %p1, align 8 | ||
%0 = tail call { ptr, i1 } @llvm.type.checked.load(ptr %vtable, i32 0, metadata !"_ZTS1a") | ||
%1 = extractvalue { ptr, i1 } %0, 1 | ||
br i1 %1, label %cont2, label %trap1 | ||
|
||
trap1: | ||
tail call void @llvm.ubsantrap(i8 2) | ||
unreachable | ||
|
||
cont2: | ||
%2 = extractvalue { ptr, i1 } %0, 0 | ||
%call = tail call noundef i64 %2(ptr noundef nonnull align 8 dereferenceable(8) %p1) | ||
ret void | ||
} | ||
|
||
define hidden void @relative.vtable(ptr %p1) { | ||
; CHECK-LABEL: define hidden void @relative.vtable( | ||
; CHECK-SAME: ptr [[P1:%.*]]) { | ||
; CHECK-NEXT: [[ENTRY:.*:]] | ||
; CHECK-NEXT: [[VTABLE:%.*]] = load ptr, ptr [[P1]], align 8 | ||
; CHECK-NEXT: [[REL_LOAD:%.*]] = call ptr @llvm.load.relative.i32(ptr [[VTABLE]], i32 0) | ||
; CHECK-NEXT: [[TMP2:%.*]] = insertvalue { ptr, i1 } { ptr poison, i1 true }, ptr [[REL_LOAD]], 0 | ||
; CHECK-NEXT: [[TMP3:%.*]] = extractvalue { ptr, i1 } [[TMP2]], 1 | ||
; CHECK-NEXT: br i1 [[TMP3]], label %[[CONT2:.*]], label %[[TRAP1:.*]] | ||
; CHECK: [[TRAP1]]: | ||
; CHECK-NEXT: tail call void @llvm.ubsantrap(i8 2) | ||
; CHECK-NEXT: unreachable | ||
; CHECK: [[CONT2]]: | ||
; CHECK-NEXT: [[TMP4:%.*]] = extractvalue { ptr, i1 } [[TMP2]], 0 | ||
; CHECK-NEXT: [[CALL:%.*]] = tail call noundef i64 [[TMP4]](ptr noundef nonnull align 8 dereferenceable(8) [[P1]]) | ||
; CHECK-NEXT: ret void | ||
; | ||
entry: | ||
%vtable = load ptr, ptr %p1, align 8 | ||
%0 = tail call { ptr, i1 } @llvm.type.checked.load.relative(ptr %vtable, i32 0, metadata !"rel.vtable.type") | ||
%1 = extractvalue { ptr, i1 } %0, 1 | ||
br i1 %1, label %cont2, label %trap1 | ||
|
||
trap1: | ||
tail call void @llvm.ubsantrap(i8 2) | ||
unreachable | ||
|
||
cont2: | ||
%2 = extractvalue { ptr, i1 } %0, 0 | ||
%call = tail call noundef i64 %2(ptr noundef nonnull align 8 dereferenceable(8) %p1) | ||
ret void | ||
} | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Clang should technically emit a
llvm.type.checked.load.relative
here, but that's because of something I missed. I'll send out a patch that fixes this.