Skip to content

Commit

Permalink
Merge rust-lang#62
Browse files Browse the repository at this point in the history
62: Add the yk-linkage llvm pass. r=ltratt a=vext01



Co-authored-by: Edd Barrett <vext01@gmail.com>
  • Loading branch information
bors[bot] and vext01 authored Apr 21, 2023
2 parents d6d9aca + 547f154 commit 03da81d
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 0 deletions.
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 @@ -265,6 +266,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 @@ -1132,6 +1137,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
}

0 comments on commit 03da81d

Please sign in to comment.