We recently implemented a new option allowing relinking of bitcode modules via the "-mllvm -relink-builtin-bitcode-postop" option. This implementation relied on llvm::CloneModule() in order to pass copies to modules and preserve the original modules for later relinking. However, cloning modules has been found to be prohibitively expensive, significantly increasing compilation time for large bitcode libraries. In this patch, we shift the relink option implementation to instead link the original modules initially, and reload modules from the file system if relinking is requested. This approach results in significantly reduced overhead. We accomplish this by creating a new ReloadModules() routine that can be called from a BackendConsumer class, to mimic the behavior of ASTConsumer's loadLinkModules(), but without access to the CompilerInstance. Because loading the bitcodes from the filesystem requires access to the FileManager class, we also forward a reference to the CompilerInstance class to the BackendConsumer. This mirrors what is already done for several CompilerInstance members, such as TargetOptions and CodeGenOptions. Finally, we needed to add a const specifier to the FileManager::getBufferForFile() routine to allow it to be called using the const reference returned from CompilerInstance::getFileManager()
40 lines
1.3 KiB
C++
40 lines
1.3 KiB
C++
//===-- LinkInModulesPass.cpp - Module Linking pass --------------- 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
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
/// \file
|
|
///
|
|
/// LinkInModulesPass implementation.
|
|
///
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
#include "LinkInModulesPass.h"
|
|
#include "BackendConsumer.h"
|
|
|
|
#include "clang/Basic/CodeGenOptions.h"
|
|
#include "clang/Basic/FileManager.h"
|
|
#include "clang/Basic/SourceManager.h"
|
|
|
|
using namespace llvm;
|
|
|
|
LinkInModulesPass::LinkInModulesPass(clang::BackendConsumer *BC,
|
|
bool ShouldLinkFiles)
|
|
: BC(BC), ShouldLinkFiles(ShouldLinkFiles) {}
|
|
|
|
PreservedAnalyses LinkInModulesPass::run(Module &M, ModuleAnalysisManager &AM) {
|
|
if (!BC)
|
|
return PreservedAnalyses::all();
|
|
|
|
// Re-load bitcode modules from files
|
|
if (BC->ReloadModules(&M))
|
|
report_fatal_error("Bitcode module re-loading failed, aborted!");
|
|
|
|
if (BC->LinkInModules(&M, ShouldLinkFiles))
|
|
report_fatal_error("Bitcode module re-linking failed, aborted!");
|
|
|
|
return PreservedAnalyses::all();
|
|
}
|