Skip to content

Commit

Permalink
Minor refactor to image generation
Browse files Browse the repository at this point in the history
  • Loading branch information
pchintalapudi committed Aug 3, 2023
1 parent 1b9eeca commit eaf131a
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 60 deletions.
112 changes: 52 additions & 60 deletions src/aotcompile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -732,8 +732,11 @@ static inline bool verify_partitioning(const SmallVectorImpl<Partition> &partiti
} else {
if (auto F = dyn_cast<Function>(&GV)) {
// Ignore alwaysinline functions
if (F->hasFnAttribute(Attribute::AlwaysInline))
if (F->hasFnAttribute(Attribute::AlwaysInline)) {
F->setLinkage(GlobalValue::InternalLinkage);
F->setVisibility(GlobalValue::DefaultVisibility);
continue;
}
}
if (!GVNames.count(GV.getName())) {
bad = true;
Expand Down Expand Up @@ -1108,39 +1111,38 @@ static auto serializeModule(const Module &M) {
// consistent.
static void materializePreserved(Module &M, Partition &partition) {
DenseSet<GlobalValue *> Preserve;
for (auto &GV : M.global_values()) {
if (!GV.isDeclaration()) {
if (partition.globals.count(GV.getName())) {
Preserve.insert(&GV);
}
}
for (auto &Name : partition.globals) {
auto *GV = M.getNamedValue(Name);
assert(GV && !GV->isDeclaration() && !GV->hasLocalLinkage());
Preserve.insert(GV);
}

for (auto &F : M.functions()) {
if (!F.isDeclaration()) {
if (!Preserve.contains(&F)) {
if (F.hasFnAttribute(Attribute::AlwaysInline)) {
F.setLinkage(GlobalValue::InternalLinkage);
F.setVisibility(GlobalValue::DefaultVisibility);
F.setDSOLocal(true);
continue;
}
F.deleteBody();
F.setLinkage(GlobalValue::ExternalLinkage);
F.setVisibility(GlobalValue::HiddenVisibility);
F.setDSOLocal(true);
}
}
if (F.isDeclaration())
continue;
if (Preserve.contains(&F))
continue;
if (F.hasLocalLinkage())
continue;
F.deleteBody();
F.setLinkage(GlobalValue::ExternalLinkage);
F.setVisibility(GlobalValue::HiddenVisibility);
F.setDSOLocal(true);
}

for (auto &GV : M.globals()) {
if (!GV.isDeclaration()) {
if (!Preserve.contains(&GV)) {
GV.setInitializer(nullptr);
GV.setLinkage(GlobalValue::ExternalLinkage);
GV.setVisibility(GlobalValue::HiddenVisibility);
GV.setDSOLocal(true);
}
}
if (GV.isDeclaration())
continue;
if (Preserve.contains(&GV))
continue;
if (GV.hasLocalLinkage())
continue;
GV.setInitializer(nullptr);
GV.setLinkage(GlobalValue::ExternalLinkage);
GV.setVisibility(GlobalValue::HiddenVisibility);
GV.setDSOLocal(true);
}

// Global aliases are a pain to deal with. It is illegal to have an alias to a declaration,
// so we need to replace them with either a function or a global variable declaration. However,
// we can't just delete the alias, because that would break the users of the alias. Therefore,
Expand All @@ -1149,25 +1151,27 @@ static void materializePreserved(Module &M, Partition &partition) {
// to deleting the old alias.
SmallVector<std::pair<GlobalAlias *, GlobalValue *>> DeletedAliases;
for (auto &GA : M.aliases()) {
if (!GA.isDeclaration()) {
if (!Preserve.contains(&GA)) {
if (GA.getValueType()->isFunctionTy()) {
auto F = Function::Create(cast<FunctionType>(GA.getValueType()), GlobalValue::ExternalLinkage, "", &M);
// This is an extremely sad hack to make sure the global alias never points to an extern function
auto BB = BasicBlock::Create(M.getContext(), "", F);
new UnreachableInst(M.getContext(), BB);
GA.setAliasee(F);

DeletedAliases.push_back({ &GA, F });
}
else {
auto GV = new GlobalVariable(M, GA.getValueType(), false, GlobalValue::ExternalLinkage, Constant::getNullValue(GA.getValueType()));
DeletedAliases.push_back({ &GA, GV });
}
}
assert(!GA.isDeclaration() && "Global aliases can't be declarations!"); // because LLVM says so
if (Preserve.contains(&GA))
continue;
if (GA.hasLocalLinkage())
continue;
if (GA.getValueType()->isFunctionTy()) {
auto F = Function::Create(cast<FunctionType>(GA.getValueType()), GlobalValue::ExternalLinkage, "", &M);
// This is an extremely sad hack to make sure the global alias never points to an extern function
auto BB = BasicBlock::Create(M.getContext(), "", F);
new UnreachableInst(M.getContext(), BB);
GA.setAliasee(F);
DeletedAliases.push_back({ &GA, F });
}
else {
auto GV = new GlobalVariable(M, GA.getValueType(), false, GlobalValue::ExternalLinkage, Constant::getNullValue(GA.getValueType()));
DeletedAliases.push_back({ &GA, GV });
}
}

cantFail(M.materializeAll());

for (auto &Deleted : DeletedAliases) {
Deleted.second->takeName(Deleted.first);
Deleted.first->replaceAllUsesWith(Deleted.second);
Expand Down Expand Up @@ -1236,20 +1240,6 @@ static void construct_vars(Module &M, Partition &partition) {
gidxs_var->setDSOLocal(true);
}

// Materialization will leave many unused declarations, which multiversioning would otherwise clone.
// This function removes them to avoid unnecessary cloning of declarations.
// The GlobalDCEPass is much better at this, but we only care about removing unused
// declarations, not actually about seeing if code is dead (codegen knows it is live, by construction).
static void dropUnusedGlobals(Module &M) {
std::vector<GlobalValue *> unused;
for (auto &G : M.global_values()) {
if (G.isDeclaration() && G.use_empty())
unused.push_back(&G);
}
for (auto &G : unused)
G->eraseFromParent();
}

// Entrypoint to optionally-multithreaded image compilation. This handles global coordination of the threading,
// as well as partitioning, serialization, and deserialization.
template<typename ModuleReleasedFunc>
Expand Down Expand Up @@ -1875,8 +1865,10 @@ void addOptimizationPasses(legacy::PassManagerBase *PM, int opt_level,
// consider AggressiveInstCombinePass at optlevel > 2
PM->add(createInstructionCombiningPass());
PM->add(createCFGSimplificationPass(basicSimplifyCFGOptions));
if (dump_native)
if (dump_native) {
PM->add(createStripDeadPrototypesPass());
PM->add(createMultiVersioningPass(external_use));
}
PM->add(createCPUFeaturesPass());
PM->add(createSROAPass());
PM->add(createInstSimplifyLegacyPass());
Expand Down
2 changes: 2 additions & 0 deletions src/pipeline.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include <llvm/Transforms/Instrumentation/ThreadSanitizer.h>
#include <llvm/Transforms/Scalar/GVN.h>
#include <llvm/Transforms/IPO/AlwaysInliner.h>
#include <llvm/Transforms/IPO/StripDeadPrototypes.h>
#include <llvm/Transforms/InstCombine/InstCombine.h>
#include <llvm/Transforms/Scalar/InstSimplifyPass.h>
#include <llvm/Transforms/Utils/SimplifyCFGOptions.h>
Expand Down Expand Up @@ -372,6 +373,7 @@ static void buildEarlyOptimizerPipeline(ModulePassManager &MPM, PassBuilder *PB,
MPM.addPass(createModuleToPostOrderCGSCCPassAdaptor(std::move(CGPM)));
}
if (options.dump_native) {
MPM.addPass(StripDeadPrototypesPass());
JULIA_PASS(MPM.addPass(MultiVersioningPass(options.external_use)));
}
JULIA_PASS(MPM.addPass(CPUFeaturesPass()));
Expand Down

0 comments on commit eaf131a

Please sign in to comment.