Files
derek.holloway 7502bf64a4
CodeQL / Analyze (actions) (push) Canceled after 0s
CodeQL / Analyze (python) (push) Canceled after 0s
pre-commit / pre-commit (push) Canceled after 0s
Multi-Arch CI / setup (push) Canceled after 0s
Publish build_manylinux_x86_64 images / publish_build_manylinux_x86_64 (push) Canceled after 0s
Setup (Multi-Arch) / setup (push) Canceled after 0s
Multi-Arch CI / Linux::${{ fromJSON(needs.setup.outputs.linux_build_config || '{}').build_variant_label || 'skip' }} (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Publish no_rocm_image_ubi10 image / publish_no_rocm_image_ubi10 (push) Canceled after 0s
Multi-Arch CI / Windows::${{ fromJSON(needs.setup.outputs.windows_build_config || '{}').build_variant_label || 'skip' }} (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Multi-Arch CI / Manifest Diff (push) Canceled after 0s
TheRock Manifest Diff Report / Generate Manifest Diff Report (push) Canceled after 0s
Multi-Arch CI / CI Summary (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04 (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04_ocl_rt images / publish_no_rocm_image_ubuntu24_04_ocl_rt (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_rocgdb (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04_rocgdb images / publish_no_rocm_image_ubuntu24_04_rocgdb (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_ocl_rt (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Unit Tests / Unit Tests :: ubuntu-24.04 (push) Canceled after 0s
Unit Tests / Unit Tests :: windows-2022 (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_media (push) Canceled after 0s
Unit Tests / Unit Tests Summary (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_openmpi (push) Canceled after 0s
Flatten subprojects into a single base monorepo
2026-09-02 16:34:47 -07:00

149 lines
4.6 KiB
C++

//===-- ExtractGV.cpp - Global Value extraction pass ----------------------===//
//
// 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 pass extracts global values
//
//===----------------------------------------------------------------------===//
#include "llvm/Transforms/IPO/ExtractGV.h"
#include "llvm/IR/Module.h"
#include "llvm/IR/PassManager.h"
using namespace llvm;
/// Make sure GV is visible from both modules. Delete is true if it is
/// being deleted from this module.
/// This also makes sure GV cannot be dropped so that references from
/// the split module remain valid.
static void makeVisible(GlobalValue &GV, bool Delete) {
bool Local = GV.hasLocalLinkage();
if (Local || Delete) {
GV.setLinkage(GlobalValue::ExternalLinkage);
if (Local)
GV.setVisibility(GlobalValue::HiddenVisibility);
return;
}
if (!GV.hasLinkOnceLinkage()) {
assert(!GV.isDiscardableIfUnused());
return;
}
// Map linkonce* to weak* so that llvm doesn't drop this GV.
switch (GV.getLinkage()) {
default:
llvm_unreachable("Unexpected linkage");
case GlobalValue::LinkOnceAnyLinkage:
GV.setLinkage(GlobalValue::WeakAnyLinkage);
return;
case GlobalValue::LinkOnceODRLinkage:
GV.setLinkage(GlobalValue::WeakODRLinkage);
return;
}
}
/// If deleteS is true, this pass deletes the specified global values.
/// Otherwise, it deletes as much of the module as possible, except for the
/// global values specified.
ExtractGVPass::ExtractGVPass(std::vector<GlobalValue *> &GVs, bool deleteS,
bool keepConstInit)
: Named(llvm::from_range, GVs), deleteStuff(deleteS),
keepConstInit(keepConstInit) {}
PreservedAnalyses ExtractGVPass::run(Module &M, ModuleAnalysisManager &) {
// Visit the global inline asm.
if (!deleteStuff)
M.setModuleInlineAsm("");
// For simplicity, just give all GlobalValues ExternalLinkage. A trickier
// implementation could figure out which GlobalValues are actually
// referenced by the Named set, and which GlobalValues in the rest of
// the module are referenced by the NamedSet, and get away with leaving
// more internal and private things internal and private. But for now,
// be conservative and simple.
// Visit the GlobalVariables.
for (GlobalVariable &GV : M.globals()) {
bool Delete = deleteStuff == (bool)Named.count(&GV) &&
!GV.isDeclaration() && (!GV.isConstant() || !keepConstInit);
if (!Delete) {
if (GV.hasAvailableExternallyLinkage())
continue;
if (GV.getName() == "llvm.global_ctors")
continue;
}
makeVisible(GV, Delete);
if (Delete) {
// Make this a declaration and drop it's comdat.
GV.setInitializer(nullptr);
GV.setComdat(nullptr);
}
}
// Visit the Functions.
for (Function &F : M) {
bool Delete = deleteStuff == (bool)Named.count(&F) && !F.isDeclaration();
if (!Delete) {
if (F.hasAvailableExternallyLinkage())
continue;
}
makeVisible(F, Delete);
if (Delete) {
// Make this a declaration and drop it's comdat.
F.deleteBody();
F.setComdat(nullptr);
}
}
// Visit the Aliases.
for (GlobalAlias &GA : llvm::make_early_inc_range(M.aliases())) {
bool Delete = deleteStuff == (bool)Named.count(&GA);
makeVisible(GA, Delete);
if (Delete) {
Type *Ty = GA.getValueType();
GA.removeFromParent();
llvm::Value *Declaration;
if (FunctionType *FTy = dyn_cast<FunctionType>(Ty)) {
Declaration = Function::Create(FTy, GlobalValue::ExternalLinkage,
GA.getAddressSpace(), GA.getName(), &M);
} else {
Declaration = new GlobalVariable(
M, Ty, false, GlobalValue::ExternalLinkage, nullptr, GA.getName());
}
GA.replaceAllUsesWith(Declaration);
delete &GA;
}
}
// Visit the IFuncs.
for (GlobalIFunc &IF : llvm::make_early_inc_range(M.ifuncs())) {
bool Delete = deleteStuff == (bool)Named.count(&IF);
makeVisible(IF, Delete);
if (!Delete)
continue;
auto *FuncType = dyn_cast<FunctionType>(IF.getValueType());
IF.removeFromParent();
llvm::Value *Declaration =
Function::Create(FuncType, GlobalValue::ExternalLinkage,
IF.getAddressSpace(), IF.getName(), &M);
IF.replaceAllUsesWith(Declaration);
delete &IF;
}
return PreservedAnalyses::none();
}