Skip to content

Commit 03da81d

Browse files
bors[bot]vext01
andauthored
62: Add the yk-linkage llvm pass. r=ltratt a=vext01 Co-authored-by: Edd Barrett <[email protected]>
2 parents d6d9aca + 547f154 commit 03da81d

File tree

5 files changed

+86
-0
lines changed

5 files changed

+86
-0
lines changed
+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#ifndef LLVM_TRANSFORMS_YK_LINKAGE_H
2+
#define LLVM_TRANSFORMS_YK_LINKAGE_H
3+
4+
#include "llvm/Pass.h"
5+
6+
namespace llvm {
7+
ModulePass *createYkLinkagePass();
8+
} // namespace llvm
9+
10+
#endif

llvm/lib/CodeGen/TargetPassConfig.cpp

+9
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
#include "llvm/Transforms/Utils.h"
4949
#include "llvm/Transforms/Yk/BlockDisambiguate.h"
5050
#include "llvm/Transforms/Yk/ControlPoint.h"
51+
#include "llvm/Transforms/Yk/Linkage.h"
5152
#include "llvm/Transforms/Yk/ShadowStack.h"
5253
#include "llvm/Transforms/Yk/Stackmaps.h"
5354
#include <cassert>
@@ -265,6 +266,10 @@ static cl::opt<bool> YkPatchCtrlPoint("yk-patch-control-point", cl::init(false),
265266
cl::NotHidden,
266267
cl::desc("Patch yk_mt_control_point()"));
267268

269+
static cl::opt<bool> YkLinkage("yk-linkage", cl::init(false),
270+
cl::NotHidden,
271+
cl::desc("Change functions with internal linkage to have external linkage"));
272+
268273
static cl::opt<bool>
269274
YkShadowStack("yk-shadow-stack", cl::init(false), cl::NotHidden,
270275
cl::desc("Use a shadow stack for stack values."));
@@ -1132,6 +1137,10 @@ bool TargetPassConfig::addISelPasses() {
11321137
addPass(createYkControlPointPass());
11331138
}
11341139

1140+
if (YkLinkage) {
1141+
addPass(createYkLinkagePass());
1142+
}
1143+
11351144
if (YkInsertStackMaps) {
11361145
addPass(createYkStackmapsPass());
11371146
}

llvm/lib/Transforms/Yk/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
add_llvm_component_library(LLVMYkPasses
22
BlockDisambiguate.cpp
33
ControlPoint.cpp
4+
Linkage.cpp
45
LivenessAnalysis.cpp
56
StackMaps.cpp
67
ShadowStack.cpp

llvm/lib/Transforms/Yk/Linkage.cpp

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
//===- Linkage.cpp - Ajdust linkage for the Yk JIT -----------------===//
2+
//
3+
// The JIT relies upon the use of `dlsym()` at runtime in order to lookup any
4+
// given function from its virtual address. For this to work the symbols for
5+
// all functions must be in the dynamic symbol table.
6+
//
7+
// `yk-config` already provides the `--export-dynamic` flag in order to ensure
8+
// that all *externally visible* symbols make it in to the dynamic symbol table,
9+
// but that's not enough: functions marked for internal linkage (e.g. `static`
10+
// functions in C) will be missed.
11+
//
12+
// This pass walks the functions of a module and flips any with internal linkage
13+
// to external linkage.
14+
//
15+
// Note that whilst symbols with internal linkage can have the same name and be
16+
// distinct, this is not so for symbols with external linkage. That's OK for
17+
// us because Yk requires the use of LTO, and the LTO module merger will have
18+
// already mangled the names for us so that symbol clashes can't occur.
19+
20+
#include "llvm/Transforms/Yk/Linkage.h"
21+
#include "llvm/IR/Function.h"
22+
#include "llvm/IR/Module.h"
23+
#include "llvm/InitializePasses.h"
24+
#include "llvm/Pass.h"
25+
26+
#define DEBUG_TYPE "yk-linkage"
27+
28+
using namespace llvm;
29+
30+
namespace llvm {
31+
void initializeYkLinkagePass(PassRegistry &);
32+
} // namespace llvm
33+
34+
namespace {
35+
class YkLinkage : public ModulePass {
36+
public:
37+
static char ID;
38+
YkLinkage() : ModulePass(ID) {
39+
initializeYkLinkagePass(*PassRegistry::getPassRegistry());
40+
}
41+
42+
bool runOnModule(Module &M) override {
43+
for (Function &F : M) {
44+
if (F.hasInternalLinkage()) {
45+
F.setLinkage(GlobalVariable::ExternalLinkage);
46+
}
47+
}
48+
return true;
49+
}
50+
};
51+
} // namespace
52+
53+
char YkLinkage::ID = 0;
54+
INITIALIZE_PASS(YkLinkage, DEBUG_TYPE, "yk-linkage", false, false)
55+
56+
ModulePass *llvm::createYkLinkagePass() { return new YkLinkage(); }
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
; Checks that the yk-linkage pass changes functions to have external linkage.
2+
;
3+
; RUN: llc --yk-linkage -o - < %s | FileCheck %s
4+
; RUN: llc -o - < %s | FileCheck --check-prefix CHECK-NOPASS %s
5+
6+
; CHECK: .globl myfunc
7+
; CHECK-NOPASS-NOT: .globl myfunc
8+
define internal void @myfunc() noinline {
9+
ret void
10+
}

0 commit comments

Comments
 (0)