forked from JuliaLang/julia
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathllvm-gc-invariant-verifier.cpp
209 lines (181 loc) · 7.2 KB
/
llvm-gc-invariant-verifier.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
// This file is a part of Julia. License is MIT: https://julialang.org/license
// This LLVM pass verifies invariants required for correct GC root placement.
// See the devdocs for a description of these invariants.
#include "llvm-version.h"
#include <llvm-c/Core.h>
#include <llvm-c/Types.h>
#include <llvm/ADT/BitVector.h>
#include <llvm/ADT/PostOrderIterator.h>
#include <llvm/Analysis/CFG.h>
#include <llvm/IR/Value.h>
#include <llvm/IR/Constants.h>
#include <llvm/IR/LegacyPassManager.h>
#include <llvm/IR/Dominators.h>
#include <llvm/IR/Function.h>
#include <llvm/IR/Instructions.h>
#include <llvm/IR/IntrinsicInst.h>
#include <llvm/IR/InstVisitor.h>
#include <llvm/IR/Module.h>
#include <llvm/IR/IRBuilder.h>
#include <llvm/IR/Verifier.h>
#include <llvm/Pass.h>
#include <llvm/Support/Debug.h>
#include "codegen_shared.h"
#include "julia.h"
#define DEBUG_TYPE "verify_gc_invariants"
#undef DEBUG
using namespace llvm;
struct GCInvariantVerifier : public FunctionPass, public InstVisitor<GCInvariantVerifier> {
static char ID;
bool Broken = false;
bool Strong;
GCInvariantVerifier(bool Strong = false) : FunctionPass(ID), Strong(Strong) {}
private:
void Check(bool Cond, const char *message, Value *Val) {
if (!Cond) {
dbgs() << message << "\n\t" << *Val << "\n";
Broken = true;
}
}
public:
void getAnalysisUsage(AnalysisUsage &AU) const override {
FunctionPass::getAnalysisUsage(AU);
AU.setPreservesAll();
}
bool runOnFunction(Function &F) override;
void visitAddrSpaceCastInst(AddrSpaceCastInst &I);
void visitLoadInst(LoadInst &LI);
void visitStoreInst(StoreInst &SI);
void visitAtomicCmpXchgInst(AtomicCmpXchgInst &SI);
void visitAtomicRMWInst(AtomicRMWInst &SI);
void visitReturnInst(ReturnInst &RI);
void visitGetElementPtrInst(GetElementPtrInst &GEP);
void visitIntToPtrInst(IntToPtrInst &IPI);
void visitPtrToIntInst(PtrToIntInst &PII);
void visitCallInst(CallInst &CI);
void checkStoreInst(Type *VTy, unsigned AS, Value &SI);
};
void GCInvariantVerifier::visitAddrSpaceCastInst(AddrSpaceCastInst &I) {
unsigned FromAS = cast<PointerType>(I.getSrcTy())->getAddressSpace();
unsigned ToAS = cast<PointerType>(I.getDestTy())->getAddressSpace();
if (FromAS == 0)
return;
Check(ToAS != AddressSpace::Loaded && FromAS != AddressSpace::Loaded,
"Illegal address space cast involving loaded ptr", &I);
Check(FromAS != AddressSpace::Tracked ||
ToAS == AddressSpace::CalleeRooted ||
ToAS == AddressSpace::Derived,
"Illegal address space cast from tracked ptr", &I);
Check(FromAS != AddressSpace::CalleeRooted &&
FromAS != AddressSpace::Derived,
"Illegal address space cast from decayed ptr", &I);
}
void GCInvariantVerifier::checkStoreInst(Type *VTy, unsigned AS, Value &SI) {
if (VTy->isPointerTy()) {
/* We currently don't obey this for arguments. That's ok - they're
externally rooted. */
unsigned AS = cast<PointerType>(VTy)->getAddressSpace();
Check(AS != AddressSpace::CalleeRooted &&
AS != AddressSpace::Derived,
"Illegal store of decayed value", &SI);
}
Check(AS != AddressSpace::CalleeRooted,
"Illegal store to callee rooted value", &SI);
}
void GCInvariantVerifier::visitStoreInst(StoreInst &SI) {
Type *VTy = SI.getValueOperand()->getType();
checkStoreInst(VTy, SI.getPointerAddressSpace(), SI);
}
void GCInvariantVerifier::visitAtomicRMWInst(AtomicRMWInst &SI) {
Type *VTy = SI.getValOperand()->getType();
checkStoreInst(VTy, SI.getPointerAddressSpace(), SI);
}
void GCInvariantVerifier::visitAtomicCmpXchgInst(AtomicCmpXchgInst &SI) {
Type *VTy = SI.getNewValOperand()->getType();
checkStoreInst(VTy, SI.getPointerAddressSpace(), SI);
}
void GCInvariantVerifier::visitLoadInst(LoadInst &LI) {
Type *Ty = LI.getType();
if (Ty->isPointerTy()) {
unsigned AS = cast<PointerType>(Ty)->getAddressSpace();
Check(AS != AddressSpace::CalleeRooted &&
AS != AddressSpace::Derived,
"Illegal load of gc relevant value", &LI);
}
Ty = LI.getPointerOperand()->getType();
if (Ty->isPointerTy()) {
unsigned AS = cast<PointerType>(Ty)->getAddressSpace();
Check(AS != AddressSpace::CalleeRooted,
"Illegal store of callee rooted value", &LI);
}
}
static bool isSpecialAS(unsigned AS) {
return AddressSpace::FirstSpecial <= AS && AS <= AddressSpace::LastSpecial;
}
void GCInvariantVerifier::visitReturnInst(ReturnInst &RI) {
if (!RI.getReturnValue())
return;
Type *RTy = RI.getReturnValue()->getType();
if (!RTy->isPointerTy())
return;
unsigned AS = cast<PointerType>(RTy)->getAddressSpace();
Check(!isSpecialAS(AS) || AS == AddressSpace::Tracked,
"Only gc tracked values may be directly returned", &RI);
}
void GCInvariantVerifier::visitGetElementPtrInst(GetElementPtrInst &GEP) {
Type *Ty = GEP.getType();
if (!Ty->isPointerTy())
return;
unsigned AS = cast<PointerType>(Ty)->getAddressSpace();
if (!isSpecialAS(AS))
return;
/* We're actually ok with GEPs here, as long as they don't feed into any
uses. Upstream is currently still debating whether CAST(GEP) == GEP(CAST).
In the frontend, we always perform CAST(GEP), so while we can enforce
this invariant when we run directly after the frontend (Strong == 1),
the optimizer will introduce the other form. Thus, we need to allow it
while upstream hasn't decided whether the optimizer is allowed to
introduce these.
*/
if (Strong) {
Check(AS != AddressSpace::Tracked,
"GC tracked values may not appear in GEP expressions."
" You may have to decay the value first", &GEP);
}
}
void GCInvariantVerifier::visitCallInst(CallInst &CI) {
CallingConv::ID CC = CI.getCallingConv();
if (CC == JLCALL_F_CC || CC == JLCALL_F2_CC) {
for (Value *Arg : CI.arg_operands()) {
Type *Ty = Arg->getType();
Check(Ty->isPointerTy() && cast<PointerType>(Ty)->getAddressSpace() == AddressSpace::Tracked,
"Invalid derived pointer in jlcall", &CI);
}
}
}
/* These next two are caught by the regular verifier on LLVM 5.0+, but we
may want to run this on earlier LLVM versions. */
void GCInvariantVerifier::visitIntToPtrInst(IntToPtrInst &IPI) {
Check(!isSpecialAS(IPI.getAddressSpace()),
"Illegal inttoptr", &IPI);
}
void GCInvariantVerifier::visitPtrToIntInst(PtrToIntInst &PII) {
Check(!isSpecialAS(PII.getPointerAddressSpace()),
"Illegal inttoptr", &PII);
}
bool GCInvariantVerifier::runOnFunction(Function &F) {
visit(F);
if (Broken) {
abort();
}
return false;
}
char GCInvariantVerifier::ID = 0;
static RegisterPass<GCInvariantVerifier> X("GCInvariantVerifier", "GC Invariant Verification Pass", false, false);
Pass *createGCInvariantVerifierPass(bool Strong) {
return new GCInvariantVerifier(Strong);
}
extern "C" JL_DLLEXPORT void LLVMExtraAddGCInvariantVerifierPass_impl(LLVMPassManagerRef PM, LLVMBool Strong)
{
unwrap(PM)->add(createGCInvariantVerifierPass(Strong));
}