Skip to content

Commit c4b3c4f

Browse files
committed
I can insert passes
1 parent 8d7bd49 commit c4b3c4f

File tree

4 files changed

+56
-3
lines changed

4 files changed

+56
-3
lines changed

mk/rustllvm.mk

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ LLVM_EXTRA_INCDIRS_$(1)= -iquote $(S)src/llvm/include \
2222
-iquote $$(CFG_LLVM_BUILD_DIR_$(1))/include
2323
endif
2424

25-
RUSTLLVM_OBJS_CS_$(1) := $$(addprefix rustllvm/, RustWrapper.cpp PassWrapper.cpp)
25+
RUSTLLVM_OBJS_CS_$(1) := $$(addprefix rustllvm/, RustWrapper.cpp PassWrapper.cpp RustStackPass.cpp)
2626

2727
RUSTLLVM_DEF_$(1) := $(1)/rustllvm/rustllvm$(CFG_DEF_SUFFIX_$(1))
2828

src/rustllvm/PassWrapper.cpp

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,11 @@ LLVMInitializePasses() {
4343
initializeInstCombine(Registry);
4444
initializeInstrumentation(Registry);
4545
initializeTarget(Registry);
46+
47+
// Initialize our pass
48+
PassInfo *PI = new PassInfo("Rust Stack Safety Pass", "rustsafestack", & RustSafeStack::ID,
49+
PassInfo::NormalCtor_t(callDefaultCtor<RustSafeStack>), false, false);
50+
Registry.registerPass(*PI, true);
4651
}
4752

4853
extern "C" bool
@@ -174,8 +179,17 @@ LLVMRustWriteOutputFile(LLVMTargetMachineRef Target,
174179
formatted_raw_ostream FOS(OS);
175180

176181
unwrap(Target)->addPassesToEmitFile(*PM, FOS, FileType, false);
177-
PM->run(*unwrap(M));
178-
return true;
182+
183+
StringRef SR("rustsafestack");
184+
PassRegistry *PR = PassRegistry::getPassRegistry();
185+
186+
const PassInfo *PI = PR->getPassInfo(SR);
187+
if (PI) {
188+
PM->add(PI->createPass());
189+
PM->run(*unwrap(M));
190+
return true;
191+
}
192+
return false;
179193
}
180194

181195
extern "C" void

src/rustllvm/RustStackPass.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#include "rustllvm.h"
2+
3+
using namespace llvm;
4+
5+
void RustSafeStack::getAnalysisUsage(AnalysisUsage &AU) const {
6+
AU.setPreservesCFG();
7+
AU.addPreserved<MachineLoopInfo>();
8+
AU.addPreserved<MachineDominatorTree>();
9+
10+
// Get pass ID for PEI
11+
StringRef SR("prologepilog");
12+
const void *pei_id = PassRegistry::getPassRegistry()->getPassInfo(SR)->getTypeInfo();
13+
AU.addRequiredID(pei_id);
14+
15+
MachineFunctionPass::getAnalysisUsage(AU);
16+
}
17+
18+
bool RustSafeStack::runOnMachineFunction(MachineFunction &Fn) {
19+
errs() << "Rust stack safety running\n";
20+
return false;
21+
}
22+
23+
char RustSafeStack::ID = 0;
24+
//static RegisterPass<RustSafeStack> X("rustsafestack", "Rust Stack Safety Pass", false, false);

src/rustllvm/rustllvm.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,9 @@
5151
#include "llvm-c/BitReader.h"
5252
#include "llvm-c/ExecutionEngine.h"
5353
#include "llvm-c/Object.h"
54+
#include "llvm/CodeGen/MachineFunctionPass.h"
55+
#include "llvm/CodeGen/MachineLoopInfo.h"
56+
#include "llvm/CodeGen/MachineDominators.h"
5457

5558
// Used by RustMCJITMemoryManager::getPointerToNamedFunction()
5659
// to get around glibc issues. See the function for more information.
@@ -61,3 +64,15 @@
6164
#endif
6265

6366
extern const char* LLVMRustError;
67+
68+
namespace llvm {
69+
class RustSafeStack : public MachineFunctionPass {
70+
public:
71+
static char ID;
72+
RustSafeStack() : MachineFunctionPass(ID) {
73+
//initializePEIPass(*PassRegistry::getPassRegistry());
74+
}
75+
virtual void getAnalysisUsage(AnalysisUsage &AU) const;
76+
bool runOnMachineFunction(MachineFunction &Fn);
77+
};
78+
}

0 commit comments

Comments
 (0)