Skip to content
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

Add the yk-linkage llvm pass. #62

Merged
merged 1 commit into from
Apr 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions llvm/include/llvm/Transforms/Yk/Linkage.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#ifndef LLVM_TRANSFORMS_YK_LINKAGE_H
#define LLVM_TRANSFORMS_YK_LINKAGE_H

#include "llvm/Pass.h"

namespace llvm {
ModulePass *createYkLinkagePass();
} // namespace llvm

#endif
9 changes: 9 additions & 0 deletions llvm/lib/CodeGen/TargetPassConfig.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
#include "llvm/Transforms/Utils.h"
#include "llvm/Transforms/Yk/BlockDisambiguate.h"
#include "llvm/Transforms/Yk/ControlPoint.h"
#include "llvm/Transforms/Yk/Linkage.h"
#include "llvm/Transforms/Yk/ShadowStack.h"
#include "llvm/Transforms/Yk/Stackmaps.h"
#include <cassert>
Expand Down Expand Up @@ -263,6 +264,10 @@ static cl::opt<bool> YkPatchCtrlPoint("yk-patch-control-point", cl::init(false),
cl::NotHidden,
cl::desc("Patch yk_mt_control_point()"));

static cl::opt<bool> YkLinkage("yk-linkage", cl::init(false),
cl::NotHidden,
cl::desc("Change functions with internal linkage to have external linkage"));

static cl::opt<bool>
YkShadowStack("yk-shadow-stack", cl::init(false), cl::NotHidden,
cl::desc("Use a shadow stack for stack values."));
Expand Down Expand Up @@ -1130,6 +1135,10 @@ bool TargetPassConfig::addISelPasses() {
addPass(createYkControlPointPass());
}

if (YkLinkage) {
addPass(createYkLinkagePass());
}

if (YkInsertStackMaps) {
addPass(createYkStackmapsPass());
}
Expand Down
1 change: 1 addition & 0 deletions llvm/lib/Transforms/Yk/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
add_llvm_component_library(LLVMYkPasses
BlockDisambiguate.cpp
ControlPoint.cpp
Linkage.cpp
LivenessAnalysis.cpp
StackMaps.cpp
ShadowStack.cpp
Expand Down
56 changes: 56 additions & 0 deletions llvm/lib/Transforms/Yk/Linkage.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
//===- Linkage.cpp - Ajdust linkage for the Yk JIT -----------------===//
//
// The JIT relies upon the use of `dlsym()` at runtime in order to lookup any
// given function from its virtual address. For this to work the symbols for
// all functions must be in the dynamic symbol table.
//
// `yk-config` already provides the `--export-dynamic` flag in order to ensure
// that all *externally visible* symbols make it in to the dynamic symbol table,
// but that's not enough: functions marked for internal linkage (e.g. `static`
// functions in C) will be missed.
//
// This pass walks the functions of a module and flips any with internal linkage
// to external linkage.
//
// Note that whilst symbols with internal linkage can have the same name and be
// distinct, this is not so for symbols with external linkage. That's OK for
// us because Yk requires the use of LTO, and the LTO module merger will have
// already mangled the names for us so that symbol clashes can't occur.

#include "llvm/Transforms/Yk/Linkage.h"
#include "llvm/IR/Function.h"
#include "llvm/IR/Module.h"
#include "llvm/InitializePasses.h"
#include "llvm/Pass.h"

#define DEBUG_TYPE "yk-linkage"

using namespace llvm;

namespace llvm {
void initializeYkLinkagePass(PassRegistry &);
} // namespace llvm

namespace {
class YkLinkage : public ModulePass {
public:
static char ID;
YkLinkage() : ModulePass(ID) {
initializeYkLinkagePass(*PassRegistry::getPassRegistry());
}

bool runOnModule(Module &M) override {
for (Function &F : M) {
if (F.hasInternalLinkage()) {
F.setLinkage(GlobalVariable::ExternalLinkage);
}
}
return true;
}
};
} // namespace

char YkLinkage::ID = 0;
INITIALIZE_PASS(YkLinkage, DEBUG_TYPE, "yk-linkage", false, false)

ModulePass *llvm::createYkLinkagePass() { return new YkLinkage(); }
10 changes: 10 additions & 0 deletions llvm/test/Transforms/Yk/Linkage/YkLinkage.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
; Checks that the yk-linkage pass changes functions to have external linkage.
;
; RUN: llc --yk-linkage -o - < %s | FileCheck %s
; RUN: llc -o - < %s | FileCheck --check-prefix CHECK-NOPASS %s

; CHECK: .globl myfunc
; CHECK-NOPASS-NOT: .globl myfunc
define internal void @myfunc() noinline {
ret void
}