Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(31)

Side by Side Diff: lib/Transforms/NaCl/ResolvePNaClIntrinsics.cpp

Issue 17777004: Concurrency support for PNaCl ABI (Closed) Base URL: http://git.chromium.org/native_client/pnacl-llvm.git@master
Patch Set: Clean up suggested by mseaborn. Created 7 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 //===- ResolvePNaClIntrinsics.cpp - Resolve calls to PNaCl intrinsics ----====// 1 //===- ResolvePNaClIntrinsics.cpp - Resolve calls to PNaCl intrinsics ----====//
2 // 2 //
3 // The LLVM Compiler Infrastructure 3 // The LLVM Compiler Infrastructure
4 // 4 //
5 // This file is distributed under the University of Illinois Open Source 5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details. 6 // License. See LICENSE.TXT for details.
7 // 7 //
8 //===----------------------------------------------------------------------===// 8 //===----------------------------------------------------------------------===//
9 // 9 //
10 // This pass resolves calls to PNaCl stable bitcode intrinsics. It is 10 // This pass resolves calls to PNaCl stable bitcode intrinsics. It is
11 // normally run in the PNaCl translator. 11 // normally run in the PNaCl translator.
12 // 12 //
13 // Running AddPNaClExternalDeclsPass is a precondition for running this pass. 13 // Running AddPNaClExternalDeclsPass is a precondition for running this
14 // They are separate because one is a ModulePass and the other is a 14 // pass. They are separate because one is a ModulePass and the other is
15 // FunctionPass. 15 // a FunctionPass.
16 // 16 //
17 //===----------------------------------------------------------------------===// 17 //===----------------------------------------------------------------------===//
18 18
19 #include "llvm/ADT/SmallVector.h" 19 #include "llvm/ADT/SmallVector.h"
20 #include "llvm/IR/Constant.h"
20 #include "llvm/IR/Instructions.h" 21 #include "llvm/IR/Instructions.h"
22 #include "llvm/IR/IntrinsicInst.h"
21 #include "llvm/IR/Intrinsics.h" 23 #include "llvm/IR/Intrinsics.h"
22 #include "llvm/IR/Module.h" 24 #include "llvm/IR/Module.h"
25 #include "llvm/IR/NaClIntrinsics.h"
26 #include "llvm/IR/Value.h"
23 #include "llvm/Pass.h" 27 #include "llvm/Pass.h"
28 #include "llvm/Support/Compiler.h"
29 #include "llvm/Support/MathExtras.h"
24 #include "llvm/Transforms/NaCl.h" 30 #include "llvm/Transforms/NaCl.h"
25 31
26 using namespace llvm; 32 using namespace llvm;
27 33
28 namespace { 34 namespace {
29 class ResolvePNaClIntrinsics : public FunctionPass { 35 class ResolvePNaClIntrinsics : public FunctionPass {
36 public:
37 ResolvePNaClIntrinsics() : FunctionPass(ID) {
38 initializeResolvePNaClIntrinsicsPass(*PassRegistry::getPassRegistry());
39 }
40
41 static char ID;
42 virtual bool runOnFunction(Function &F);
43
44 // Interface specifying how calls should be resolved.
eliben 2013/07/03 16:06:05 Explain how it's used and what has to be implement
JF 2013/07/03 20:58:35 Done. I edited this comment and a few others, and
45 class CallResolver {
30 public: 46 public:
31 ResolvePNaClIntrinsics() : FunctionPass(ID) { 47 // Called once per call to the intrinsic in the module.
32 initializeResolvePNaClIntrinsicsPass(*PassRegistry::getPassRegistry()); 48 bool Resolve(IntrinsicInst *Call) {
33 } 49 // To be a well-behaving FunctionPass, don't touch uses in other
34 50 // functions. These will be handled when the pass manager gets to
35 static char ID; 51 // those functions.
36 virtual bool runOnFunction(Function &F); 52 if (Call->getParent()->getParent() == &F)
53 return DoResolve(Call);
54 return false;
55 }
56 Function *getDeclaration() const { return DoGetDeclaration(); }
57 std::string getName() { return Intrinsic::getName(IntrinsicID); }
58
59 protected:
60 Function &F;
61 Module *M;
62 Intrinsic::ID IntrinsicID;
63
64 CallResolver(Function &F, Intrinsic::ID IntrinsicID)
65 : F(F), M(F.getParent()), IntrinsicID(IntrinsicID) {}
66 virtual ~CallResolver() {}
67 virtual Function *DoGetDeclaration() const = 0;
68 virtual bool DoResolve(IntrinsicInst *Call) = 0;
69
37 private: 70 private:
38 // Some intrinsic calls are resolved simply by replacing the call with a 71 CallResolver(const CallResolver &) LLVM_DELETED_FUNCTION;
39 // call to an alternative function with exactly the same type. 72 CallResolver &operator=(const CallResolver &) LLVM_DELETED_FUNCTION;
40 bool resolveSimpleCall(Function &F, Intrinsic::ID IntrinsicID,
41 const char *TargetFunctionName);
42 }; 73 };
74
75 private:
76 // Walk calls matching the resolver's declaration, and invoke the
77 // CallResolver methods on each of them.
78 bool walkCalls(CallResolver &Resolver);
79 };
80
81 // Rewrite intrinsic calls to another function.
82 class SimpleCallResolver : public ResolvePNaClIntrinsics::CallResolver {
83 public:
84 SimpleCallResolver(Function &F, Intrinsic::ID IntrinsicID,
85 const char *TargetFunctionName)
86 : CallResolver(F, IntrinsicID),
87 TargetFunction(M->getFunction(TargetFunctionName)) {
88 // Expect to find the target function for this intrinsic already
89 // declared, even if it is never used.
90 if (!TargetFunction)
91 report_fatal_error(std::string(
92 "Expected to find external declaration of ") + TargetFunctionName);
93 }
94 virtual ~SimpleCallResolver() {}
95
96 private:
97 Function *TargetFunction;
98
99 virtual Function *DoGetDeclaration() const {
100 return Intrinsic::getDeclaration(M, IntrinsicID);
101 }
102
103 virtual bool DoResolve(IntrinsicInst *Call) {
104 Call->setCalledFunction(TargetFunction);
105 return true;
106 }
107
108 SimpleCallResolver(const SimpleCallResolver &) LLVM_DELETED_FUNCTION;
109 SimpleCallResolver &operator=(const SimpleCallResolver &)
110 LLVM_DELETED_FUNCTION;
111 };
112
113 // Rewrite atomic intrinsics to LLVM IR instructions.
114 class AtomicCallResolver : public ResolvePNaClIntrinsics::CallResolver {
115 public:
116 AtomicCallResolver(Function &F, NaCl::AtomicIntrinsics::const_iterator I)
117 : CallResolver(F, I->ID), AI(I) {}
118 virtual ~AtomicCallResolver() {}
119
120 private:
121 NaCl::AtomicIntrinsics::const_iterator AI;
122
123 virtual Function *DoGetDeclaration() const { return AI->getDeclaration(M); }
124
125 virtual bool DoResolve(IntrinsicInst *Call) {
126 // Assume the @llvm.nacl.atomic.* intrinsics follow the PNaCl ABI:
127 // this should have been checked by the verifier.
128 const Twine Name("");
129 bool isVolatile = false;
130 SynchronizationScope SS = CrossThread;
131 Instruction *I;
132
133 switch (Call->getIntrinsicID()) {
134 default:
135 llvm_unreachable("unknown atomic intrinsic");
136 case Intrinsic::nacl_atomic_load:
137 I = new LoadInst(Call->getArgOperand(0), Name, isVolatile,
138 alignmentFromPointer(Call->getArgOperand(0)),
139 thawMemoryOrder(Call->getArgOperand(1)), SS, Call);
140 break;
141 case Intrinsic::nacl_atomic_store:
142 I = new StoreInst(Call->getArgOperand(0), Call->getArgOperand(1),
143 isVolatile,
144 alignmentFromPointer(Call->getArgOperand(1)),
145 thawMemoryOrder(Call->getArgOperand(2)), SS, Call);
146 break;
147 case Intrinsic::nacl_atomic_rmw:
148 I = new AtomicRMWInst(thawRMWOperation(Call->getArgOperand(0)),
149 Call->getArgOperand(1), Call->getArgOperand(2),
150 thawMemoryOrder(Call->getArgOperand(3)), SS, Call);
151 break;
152 case Intrinsic::nacl_atomic_cmpxchg:
153 // TODO LLVM currently doesn't support specifying separate memory
154 // orders for compare exchange's success and failure cases:
155 // LLVM IR implicitly drops the Release part of the specified
156 // memory order on failure. It is therefore correct to map
157 // the success memory order onto the LLVM IR and ignore the
158 // failure one.
159 I = new AtomicCmpXchgInst(Call->getArgOperand(0), Call->getArgOperand(1),
160 Call->getArgOperand(2),
161 thawMemoryOrder(Call->getArgOperand(3)), SS,
162 Call);
163 break;
164 case Intrinsic::nacl_atomic_fence:
165 I = new FenceInst(M->getContext(),
166 thawMemoryOrder(Call->getArgOperand(0)), SS, Call);
167 break;
168 }
169 I->setDebugLoc(Call->getDebugLoc());
170 Call->replaceAllUsesWith(I);
171 Call->eraseFromParent();
172
173 return true;
174 }
175
176 unsigned alignmentFromPointer(const Value *Ptr) const {
177 unsigned BitWidth = cast<IntegerType>(
178 cast<PointerType>(Ptr->getType())->getElementType())->getBitWidth();
179 return 1 << (CountTrailingZeros_32(BitWidth) - 3);
180 }
181
182 AtomicOrdering thawMemoryOrder(const Value *MemoryOrder) const {
183 NaCl::MemoryOrder MO = (NaCl::MemoryOrder)
184 cast<Constant>(MemoryOrder)->getUniqueInteger().getLimitedValue();
185 switch (MO) {
186 // Only valid values should pass validation.
187 default: llvm_unreachable("unknown memory order");
188 case NaCl::MemoryOrderRelaxed: return Monotonic;
189 // TODO Consume is unspecified by LLVM's internal IR.
190 case NaCl::MemoryOrderConsume: return SequentiallyConsistent;
191 case NaCl::MemoryOrderAcquire: return Acquire;
192 case NaCl::MemoryOrderRelease: return Release;
193 case NaCl::MemoryOrderAcquireRelease: return AcquireRelease;
194 case NaCl::MemoryOrderSequentiallyConsistent: return SequentiallyConsistent;
195 }
196 }
197
198 AtomicRMWInst::BinOp thawRMWOperation(const Value *Operation) const {
199 NaCl::AtomicRMWOperation Op = (NaCl::AtomicRMWOperation)
200 cast<Constant>(Operation)->getUniqueInteger().getLimitedValue();
201 switch (Op) {
202 // Only valid values should pass validation.
203 default: llvm_unreachable("unknown atomic RMW operation");
204 case NaCl::AtomicAdd: return AtomicRMWInst::Add;
205 case NaCl::AtomicSub: return AtomicRMWInst::Sub;
206 case NaCl::AtomicOr: return AtomicRMWInst::Or;
207 case NaCl::AtomicAnd: return AtomicRMWInst::And;
208 case NaCl::AtomicXor: return AtomicRMWInst::Xor;
209 case NaCl::AtomicExchange: return AtomicRMWInst::Xchg;
210 }
211 }
212
213 AtomicCallResolver(const AtomicCallResolver &);
214 AtomicCallResolver &operator=(const AtomicCallResolver &);
215 };
43 } 216 }
44 217
45 bool ResolvePNaClIntrinsics::resolveSimpleCall(Function &F, 218 bool ResolvePNaClIntrinsics::walkCalls(
46 Intrinsic::ID IntrinsicID, 219 ResolvePNaClIntrinsics::CallResolver &Resolver) {
47 const char *TargetFunctionName) {
48 Module *M = F.getParent();
49 bool Changed = false; 220 bool Changed = false;
50 Function *IntrinsicFunction = Intrinsic::getDeclaration(M, IntrinsicID); 221 Function *IntrinsicFunction = Resolver.getDeclaration();
51 222 if (!IntrinsicFunction)
52 if (!IntrinsicFunction) {
53 return false; 223 return false;
54 }
55
56 // Expect to find the target function for this intrinsic already declared
57 Function *TargetFunction = M->getFunction(TargetFunctionName);
58 if (!TargetFunction) {
59 report_fatal_error(
60 std::string("Expected to find external declaration of ") +
61 TargetFunctionName);
62 }
63 224
64 for (Value::use_iterator UI = IntrinsicFunction->use_begin(), 225 for (Value::use_iterator UI = IntrinsicFunction->use_begin(),
65 UE = IntrinsicFunction->use_end(); UI != UE;) { 226 UE = IntrinsicFunction->use_end();
227 UI != UE;) {
66 // At this point, the only uses of the intrinsic can be calls, since 228 // At this point, the only uses of the intrinsic can be calls, since
67 // we assume this pass runs on bitcode that passed ABI verification. 229 // we assume this pass runs on bitcode that passed ABI verification.
68 CallInst *Call = dyn_cast<CallInst>(*UI++); 230 IntrinsicInst *Call = dyn_cast<IntrinsicInst>(*UI++);
69 231 if (!Call)
70 if (!Call) { 232 report_fatal_error("Expected use of intrinsic to be a call: " +
71 report_fatal_error( 233 Resolver.getName());
72 std::string("Expected use of intrinsic to be a call: ") + 234
73 Intrinsic::getName(IntrinsicID)); 235 Changed |= Resolver.Resolve(Call);
74 }
75
76 // To be a well-behaving FunctionPass, don't touch uses in other
77 // functions. These will be handled when the pass manager gets to those
78 // functions.
79 if (Call->getParent()->getParent() == &F) {
80 Call->setCalledFunction(TargetFunction);
81 Changed = true;
82 }
83 } 236 }
84 237
85 return Changed; 238 return Changed;
86 } 239 }
87 240
88 bool ResolvePNaClIntrinsics::runOnFunction(Function &F) { 241 bool ResolvePNaClIntrinsics::runOnFunction(Function &F) {
89 bool Changed = resolveSimpleCall(F, Intrinsic::nacl_setjmp, "setjmp"); 242 bool Changed = false;
90 Changed |= resolveSimpleCall(F, Intrinsic::nacl_longjmp, "longjmp"); 243
244 SimpleCallResolver SetJmpResolver(F, Intrinsic::nacl_setjmp, "setjmp");
245 SimpleCallResolver LongJmpResolver(F, Intrinsic::nacl_longjmp, "longjmp");
246 Changed |= walkCalls(SetJmpResolver);
247 Changed |= walkCalls(LongJmpResolver);
248
249 NaCl::AtomicIntrinsics AI(F.getParent()->getContext());
250 for (NaCl::AtomicIntrinsics::const_iterator I = AI.begin(), E = AI.end();
251 I != E; ++I) {
252 AtomicCallResolver AtomicResolver(F, I);
253 Changed |= walkCalls(AtomicResolver);
254 }
255
91 return Changed; 256 return Changed;
92 } 257 }
93 258
94 char ResolvePNaClIntrinsics::ID = 0; 259 char ResolvePNaClIntrinsics::ID = 0;
95 INITIALIZE_PASS(ResolvePNaClIntrinsics, "resolve-pnacl-intrinsics", 260 INITIALIZE_PASS(ResolvePNaClIntrinsics, "resolve-pnacl-intrinsics",
96 "Resolve PNaCl intrinsic calls", false, false) 261 "Resolve PNaCl intrinsic calls", false, false)
97 262
98 FunctionPass *llvm::createResolvePNaClIntrinsicsPass() { 263 FunctionPass *llvm::createResolvePNaClIntrinsicsPass() {
99 return new ResolvePNaClIntrinsics(); 264 return new ResolvePNaClIntrinsics();
100 } 265 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698