llvm.org GIT mirror llvm / e0ae1ed
Update Kaleidoscope tutorial and improve Windows support Many quoted code blocks were not in sync with the actual toy.cpp files. Improve tutorial text slightly in several places. Added some step descriptions crucial to avoid crashes (like InitializeNativeTarget* calls). Solve/workaround problems with Windows (JIT'ed method not found, using custom and standard library functions from host process). Patch by: Moritz Kroll <moritz.kroll@gmx.de> Differential Revision: https://reviews.llvm.org/D29864 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@294870 91177308-0d34-0410-b5e6-96231b3b80d8 Mehdi Amini 2 years ago
16 changed file(s) with 339 addition(s) and 208 deletion(s). Raw diff Collapse all Expand all
124124
125125 class KaleidoscopeJIT {
126126 private:
127
128127 std::unique_ptr TM;
129128 const DataLayout DL;
130129 ObjectLinkingLayer<> ObjectLayer;
131130 IRCompileLayer CompileLayer;
132131
133132 public:
134
135133 typedef decltype(CompileLayer)::ModuleSetHandleT ModuleHandleT;
136134
137135 Our class begins with four members: A TargetMachine, TM, which will be used
151149 object linking layer below.
152150
153151 That's it for member variables, after that we have a single typedef:
154 ModuleHandle. This is the handle type that will be returned from our JIT's
152 ModuleHandleT. This is the handle type that will be returned from our JIT's
155153 addModule method, and can be passed to the removeModule method to remove a
156154 module. The IRCompileLayer class already provides a convenient handle type
157 (IRCompileLayer::ModuleSetHandleT), so we just alias our ModuleHandle to this.
155 (IRCompileLayer::ModuleSetHandleT), so we just alias our ModuleHandleT to this.
158156
159157 .. code-block:: c++
160158
161159 KaleidoscopeJIT()
162160 : TM(EngineBuilder().selectTarget()), DL(TM->createDataLayout()),
163 CompileLayer(ObjectLayer, SimpleCompiler(*TM)) {
161 CompileLayer(ObjectLayer, SimpleCompiler(*TM)) {
164162 llvm::sys::DynamicLibrary::LoadLibraryPermanently(nullptr);
165163 }
166164
199197 return JITSymbol(nullptr);
200198 });
201199
202 // Build a singlton module set to hold our module.
200 // Build a singleton module set to hold our module.
203201 std::vector> Ms;
204202 Ms.push_back(std::move(M));
205203
258256 first lambda (the one defining findSymbolInLogicalDylib) will just search for
259257 JIT'd code by calling the CompileLayer's findSymbol method. If we don't find a
260258 symbol in the JIT itself we'll fall back to our second lambda, which implements
261 findSymbol. This will use the RTDyldMemoyrManager::getSymbolAddressInProcess
259 findSymbol. This will use the RTDyldMemoryManager::getSymbolAddressInProcess
262260 method to search for the symbol within the program itself. If we can't find a
263 symbol definition via either of these paths the JIT will refuse to accept our
261 symbol definition via either of these paths, the JIT will refuse to accept our
264262 module, returning a "symbol not found" error.
265263
266 Now that we've built our symbol resolver we're ready to add our module to the
264 Now that we've built our symbol resolver, we're ready to add our module to the
267265 JIT. We do this by calling the CompileLayer's addModuleSet method [4]_. Since
268266 we only have a single Module and addModuleSet expects a collection, we will
269267 create a vector of modules and add our module as the only member. Since we
270 have already typedef'd our ModuleHandle type to be the same as the
268 have already typedef'd our ModuleHandleT type to be the same as the
271269 CompileLayer's handle type, we can return the handle from addModuleSet
272270 directly from our addModule method.
273271
303301 entered. It is generally good to free any module that you know you won't need
304302 to call further, just to free up the resources dedicated to it. However, you
305303 don't strictly need to do this: All resources will be cleaned up when your
306 JIT class is destructed, if the haven't been freed before then.
304 JIT class is destructed, if they haven't been freed before then.
307305
308306 This brings us to the end of Chapter 1 of Building a JIT. You now have a basic
309307 but fully functioning JIT stack that you can use to take LLVM IR and make it
118118 public:
119119 PrototypeAST(const std::string &name, std::vector Args)
120120 : Name(name), Args(std::move(Args)) {}
121
122 const std::string &getName() const { return Name; }
121123 };
122124
123125 /// FunctionAST - This class represents a function definition itself.
121121 .. code-block:: c++
122122
123123 Value *NumberExprAST::codegen() {
124 return ConstantFP::get(LLVMContext, APFloat(Val));
124 return ConstantFP::get(TheContext, APFloat(Val));
125125 }
126126
127127 In the LLVM IR, numeric constants are represented with the
170170 case '<':
171171 L = Builder.CreateFCmpULT(L, R, "cmptmp");
172172 // Convert bool 0/1 to double 0.0 or 1.0
173 return Builder.CreateUIToFP(L, Type::getDoubleTy(LLVMContext),
173 return Builder.CreateUIToFP(L, Type::getDoubleTy(TheContext),
174174 "booltmp");
175175 default:
176176 return LogErrorV("invalid binary operator");
269269 Function *PrototypeAST::codegen() {
270270 // Make the function type: double(double,double) etc.
271271 std::vector Doubles(Args.size(),
272 Type::getDoubleTy(LLVMContext));
272 Type::getDoubleTy(TheContext));
273273 FunctionType *FT =
274 FunctionType::get(Type::getDoubleTy(LLVMContext), Doubles, false);
274 FunctionType::get(Type::getDoubleTy(TheContext), Doubles, false);
275275
276276 Function *F =
277277 Function::Create(FT, Function::ExternalLinkage, Name, TheModule);
345345 .. code-block:: c++
346346
347347 // Create a new basic block to start insertion into.
348 BasicBlock *BB = BasicBlock::Create(LLVMContext, "entry", TheFunction);
348 BasicBlock *BB = BasicBlock::Create(TheContext, "entry", TheFunction);
349349 Builder.SetInsertPoint(BB);
350350
351351 // Record the function arguments in the NamedValues map.
532532 ret double %calltmp
533533 }
534534
535 When you quit the current demo, it dumps out the IR for the entire
535 When you quit the current demo (by sending an EOF via CTRL+D on Linux
536 or CTRL+Z and ENTER on Windows), it dumps out the IR for the entire
536537 module generated. Here you can see the big picture with all the
537538 functions referencing each other.
538539
130130
131131 void InitializeModuleAndPassManager(void) {
132132 // Open a new module.
133 Context LLVMContext;
134 TheModule = llvm::make_unique("my cool jit", LLVMContext);
135 TheModule->setDataLayout(TheJIT->getTargetMachine().createDataLayout());
133 TheModule = llvm::make_unique("my cool jit", TheContext);
136134
137135 // Create a new pass manager attached to it.
138136 TheFPM = llvm::make_unique(TheModule.get());
139137
140 // Provide basic AliasAnalysis support for GVN.
141 TheFPM.add(createBasicAliasAnalysisPass());
142138 // Do simple "peephole" optimizations and bit-twiddling optzns.
143 TheFPM.add(createInstructionCombiningPass());
139 TheFPM->add(createInstructionCombiningPass());
144140 // Reassociate expressions.
145 TheFPM.add(createReassociatePass());
141 TheFPM->add(createReassociatePass());
146142 // Eliminate Common SubExpressions.
147 TheFPM.add(createGVNPass());
143 TheFPM->add(createGVNPass());
148144 // Simplify the control flow graph (deleting unreachable blocks, etc).
149 TheFPM.add(createCFGSimplificationPass());
150
151 TheFPM.doInitialization();
145 TheFPM->add(createCFGSimplificationPass());
146
147 TheFPM->doInitialization();
152148 }
153149
154150 This code initializes the global module ``TheModule``, and the function pass
155151 manager ``TheFPM``, which is attached to ``TheModule``. Once the pass manager is
156152 set up, we use a series of "add" calls to add a bunch of LLVM passes.
157153
158 In this case, we choose to add five passes: one analysis pass (alias analysis),
159 and four optimization passes. The passes we choose here are a pretty standard set
154 In this case, we choose to add four optimization passes.
155 The passes we choose here are a pretty standard set
160156 of "cleanup" optimizations that are useful for a wide variety of code. I won't
161157 delve into what they do but, believe me, they are a good starting place :).
162158
226222 should evaluate and print out 3. If they define a function, they should
227223 be able to call it from the command line.
228224
229 In order to do this, we first declare and initialize the JIT. This is
230 done by adding a global variable ``TheJIT``, and initializing it in
225 In order to do this, we first prepare the environment to create code for
226 the current native target and declare and initialize the JIT. This is
227 done by calling some ``InitializeNativeTarget\*`` functions and
228 adding a global variable ``TheJIT``, and initializing it in
231229 ``main``:
232230
233231 .. code-block:: c++
235233 static std::unique_ptr TheJIT;
236234 ...
237235 int main() {
238 ..
236 InitializeNativeTarget();
237 InitializeNativeTargetAsmPrinter();
238 InitializeNativeTargetAsmParser();
239
240 // Install standard binary operators.
241 // 1 is lowest precedence.
242 BinopPrecedence['<'] = 10;
243 BinopPrecedence['+'] = 20;
244 BinopPrecedence['-'] = 20;
245 BinopPrecedence['*'] = 40; // highest.
246
247 // Prime the first token.
248 fprintf(stderr, "ready> ");
249 getNextToken();
250
239251 TheJIT = llvm::make_unique();
240252
241253 // Run the main "interpreter loop" now.
244256 return 0;
245257 }
246258
259 We also need to setup the data layout for the JIT:
260
261 .. code-block:: c++
262
263 void InitializeModuleAndPassManager(void) {
264 // Open a new module.
265 TheModule = llvm::make_unique("my cool jit", TheContext);
266 TheModule->setDataLayout(TheJIT->getTargetMachine().createDataLayout());
267
268 // Create a new pass manager attached to it.
269 TheFPM = llvm::make_unique(TheModule.get());
270 ...
271
247272 The KaleidoscopeJIT class is a simple JIT built specifically for these
248 tutorials. In later chapters we will look at how it works and extend it with
249 new features, but for now we will take it as given. Its API is very simple::
273 tutorials, available inside the LLVM source code
274 at llvm-src/examples/Kaleidoscope/include/KaleidoscopeJIT.h.
275 In later chapters we will look at how it works and extend it with
276 new features, but for now we will take it as given. Its API is very simple:
250277 ``addModule`` adds an LLVM IR module to the JIT, making its functions
251278 available for execution; ``removeModule`` removes a module, freeing any
252279 memory associated with the code in that module; and ``findSymbol`` allows us
553580 found inside the JIT, it falls back to calling "``dlsym("sin")``" on the
554581 Kaleidoscope process itself. Since "``sin``" is defined within the JIT's
555582 address space, it simply patches up calls in the module to call the libm
556 version of ``sin`` directly.
583 version of ``sin`` directly. But in some cases this even goes further:
584 as sin and cos are names of standard math functions, the constant folder
585 will directly evaluate the function calls to the correct result when called
586 with constants like in the "``sin(1.0)``" above.
557587
558588 In the future we'll see how tweaking this symbol resolution rule can be used to
559589 enable all sorts of useful features, from security (restricting the set of
566596
567597 .. code-block:: c++
568598
599 #ifdef LLVM_ON_WIN32
600 #define DLLEXPORT __declspec(dllexport)
601 #else
602 #define DLLEXPORT
603 #endif
604
569605 /// putchard - putchar that takes a double and returns 0.
570 extern "C" double putchard(double X) {
606 extern "C" DLLEXPORT double putchard(double X) {
571607 fputc((char)X, stderr);
572608 return 0;
573609 }
610
611 Note, that for Windows we need to actually export the functions because
612 the dynamic symbol loader will use GetProcAddress to find the symbols.
574613
575614 Now we can produce simple output to the console by using things like:
576615 "``extern putchard(x); putchard(120);``", which prints a lowercase 'x'
102102 IfExprAST(std::unique_ptr Cond, std::unique_ptr Then,
103103 std::unique_ptr Else)
104104 : Cond(std::move(Cond)), Then(std::move(Then)), Else(std::move(Else)) {}
105 virtual Value *codegen();
105
106 Value *codegen() override;
106107 };
107108
108109 The AST node just has pointers to the various subexpressions.
289290 if (!CondV)
290291 return nullptr;
291292
292 // Convert condition to a bool by comparing equal to 0.0.
293 // Convert condition to a bool by comparing non-equal to 0.0.
293294 CondV = Builder.CreateFCmpONE(
294 CondV, ConstantFP::get(LLVMContext, APFloat(0.0)), "ifcond");
295 CondV, ConstantFP::get(TheContext, APFloat(0.0)), "ifcond");
295296
296297 This code is straightforward and similar to what we saw before. We emit
297298 the expression for the condition, then compare that value to zero to get
304305 // Create blocks for the then and else cases. Insert the 'then' block at the
305306 // end of the function.
306307 BasicBlock *ThenBB =
307 BasicBlock::Create(LLVMContext, "then", TheFunction);
308 BasicBlock *ElseBB = BasicBlock::Create(LLVMContext, "else");
309 BasicBlock *MergeBB = BasicBlock::Create(LLVMContext, "ifcont");
308 BasicBlock::Create(TheContext, "then", TheFunction);
309 BasicBlock *ElseBB = BasicBlock::Create(TheContext, "else");
310 BasicBlock *MergeBB = BasicBlock::Create(TheContext, "ifcont");
310311
311312 Builder.CreateCondBr(CondV, ThenBB, ElseBB);
312313
399400 TheFunction->getBasicBlockList().push_back(MergeBB);
400401 Builder.SetInsertPoint(MergeBB);
401402 PHINode *PN =
402 Builder.CreatePHI(Type::getDoubleTy(LLVMContext), 2, "iftmp");
403 Builder.CreatePHI(Type::getDoubleTy(TheContext), 2, "iftmp");
403404
404405 PN->addIncoming(ThenV, ThenBB);
405406 PN->addIncoming(ElseV, ElseBB);
432433
433434 ::
434435
435 extern putchard(char)
436 extern putchard(char);
436437 def printstar(n)
437438 for i = 1, i < n, 1.0 in
438439 putchard(42); # ascii 42 = '*'
499500 std::unique_ptr Body)
500501 : VarName(VarName), Start(std::move(Start)), End(std::move(End)),
501502 Step(std::move(Step)), Body(std::move(Body)) {}
502 virtual Value *codegen();
503
504 Value *codegen() override;
503505 };
504506
505507 Parser Extensions for the 'for' Loop
560562 std::move(Body));
561563 }
562564
565 And again we hook it up as a primary expression:
566
567 .. code-block:: c++
568
569 static std::unique_ptr ParsePrimary() {
570 switch (CurTok) {
571 default:
572 return LogError("unknown token when expecting an expression");
573 case tok_identifier:
574 return ParseIdentifierExpr();
575 case tok_number:
576 return ParseNumberExpr();
577 case '(':
578 return ParseParenExpr();
579 case tok_if:
580 return ParseIfExpr();
581 case tok_for:
582 return ParseForExpr();
583 }
584 }
585
563586 LLVM IR for the 'for' Loop
564587 --------------------------
565588
609632 Value *ForExprAST::codegen() {
610633 // Emit the start code first, without 'variable' in scope.
611634 Value *StartVal = Start->codegen();
612 if (StartVal == 0) return 0;
635 if (!StartVal)
636 return nullptr;
613637
614638 With this out of the way, the next step is to set up the LLVM basic
615639 block for the start of the loop body. In the case above, the whole loop
624648 Function *TheFunction = Builder.GetInsertBlock()->getParent();
625649 BasicBlock *PreheaderBB = Builder.GetInsertBlock();
626650 BasicBlock *LoopBB =
627 BasicBlock::Create(LLVMContext, "loop", TheFunction);
651 BasicBlock::Create(TheContext, "loop", TheFunction);
628652
629653 // Insert an explicit fall through from the current block to the LoopBB.
630654 Builder.CreateBr(LoopBB);
641665 Builder.SetInsertPoint(LoopBB);
642666
643667 // Start the PHI node with an entry for Start.
644 PHINode *Variable = Builder.CreatePHI(Type::getDoubleTy(LLVMContext),
668 PHINode *Variable = Builder.CreatePHI(Type::getDoubleTy(TheContext),
645669 2, VarName.c_str());
646670 Variable->addIncoming(StartVal, PreheaderBB);
647671
692716 return nullptr;
693717 } else {
694718 // If not specified, use 1.0.
695 StepVal = ConstantFP::get(LLVMContext, APFloat(1.0));
719 StepVal = ConstantFP::get(TheContext, APFloat(1.0));
696720 }
697721
698722 Value *NextVar = Builder.CreateFAdd(Variable, StepVal, "nextvar");
709733 if (!EndCond)
710734 return nullptr;
711735
712 // Convert condition to a bool by comparing equal to 0.0.
736 // Convert condition to a bool by comparing non-equal to 0.0.
713737 EndCond = Builder.CreateFCmpONE(
714 EndCond, ConstantFP::get(LLVMContext, APFloat(0.0)), "loopcond");
738 EndCond, ConstantFP::get(TheContext, APFloat(0.0)), "loopcond");
715739
716740 Finally, we evaluate the exit value of the loop, to determine whether
717741 the loop should exit. This mirrors the condition evaluation for the
722746 // Create the "after loop" block and insert it.
723747 BasicBlock *LoopEndBB = Builder.GetInsertBlock();
724748 BasicBlock *AfterBB =
725 BasicBlock::Create(LLVMContext, "afterloop", TheFunction);
749 BasicBlock::Create(TheContext, "afterloop", TheFunction);
726750
727751 // Insert the conditional branch into the end of LoopEndBB.
728752 Builder.CreateCondBr(EndCond, LoopBB, AfterBB);
750774 NamedValues.erase(VarName);
751775
752776 // for expr always returns 0.0.
753 return Constant::getNullValue(Type::getDoubleTy(LLVMContext));
777 return Constant::getNullValue(Type::getDoubleTy(TheContext));
754778 }
755779
756780 The final code handles various cleanups: now that we have the "NextVar"
771795 =================
772796
773797 Here is the complete code listing for our running example, enhanced with
774 the if/then/else and for expressions.. To build this example, use:
798 the if/then/else and for expressions. To build this example, use:
775799
776800 .. code-block:: bash
777801
3030 ================================
3131
3232 The "operator overloading" that we will add to Kaleidoscope is more
33 general than languages like C++. In C++, you are only allowed to
33 general than in languages like C++. In C++, you are only allowed to
3434 redefine existing operators: you can't programmatically change the
3535 grammar, introduce new operators, change precedence levels, etc. In this
3636 chapter, we will add this capability to Kaleidoscope, which will let the
4040 is to show the power and flexibility of using a hand-written parser.
4141 Thus far, the parser we have been implementing uses recursive descent
4242 for most parts of the grammar and operator precedence parsing for the
43 expressions. See `Chapter 2 `_ for details. Without
44 using operator precedence parsing, it would be very difficult to allow
43 expressions. See `Chapter 2 `_ for details. By
44 using operator precedence parsing, it is very easy to allow
4545 the programmer to introduce new operators into the grammar: the grammar
4646 is dynamically extensible as the JIT runs.
4747
142142 : Name(name), Args(std::move(Args)), IsOperator(IsOperator),
143143 Precedence(Prec) {}
144144
145 Function *codegen();
146 const std::string &getName() const { return Name; }
147
145148 bool isUnaryOp() const { return IsOperator && Args.size() == 1; }
146149 bool isBinaryOp() const { return IsOperator && Args.size() == 2; }
147150
148151 char getOperatorName() const {
149152 assert(isUnaryOp() || isBinaryOp());
150 return Name[Name.size()-1];
153 return Name[Name.size() - 1];
151154 }
152155
153156 unsigned getBinaryPrecedence() const { return Precedence; }
154
155 Function *codegen();
156157 };
157158
158159 Basically, in addition to knowing a name for the prototype, we now keep
193194 // Read the precedence if present.
194195 if (CurTok == tok_number) {
195196 if (NumVal < 1 || NumVal > 100)
196 return LogErrorP("Invalid precedecnce: must be 1..100");
197 return LogErrorP("Invalid precedence: must be 1..100");
197198 BinaryPrecedence = (unsigned)NumVal;
198199 getNextToken();
199200 }
224225 seen a lot of similar code in the past. One interesting part about the
225226 code above is the couple lines that set up ``FnName`` for binary
226227 operators. This builds names like "binary@" for a newly defined "@"
227 operator. This then takes advantage of the fact that symbol names in the
228 operator. It then takes advantage of the fact that symbol names in the
228229 LLVM symbol table are allowed to have any character in them, including
229230 embedded nul characters.
230231
250251 case '<':
251252 L = Builder.CreateFCmpULT(L, R, "cmptmp");
252253 // Convert bool 0/1 to double 0.0 or 1.0
253 return Builder.CreateUIToFP(L, Type::getDoubleTy(LLVMContext),
254 return Builder.CreateUIToFP(L, Type::getDoubleTy(TheContext),
254255 "booltmp");
255256 default:
256257 break;
258259
259260 // If it wasn't a builtin binary operator, it must be a user defined one. Emit
260261 // a call to it.
261 Function *F = TheModule->getFunction(std::string("binary") + Op);
262 Function *F = getFunction(std::string("binary") + Op);
262263 assert(F && "binary operator not found!");
263264
264265 Value *Ops[2] = { L, R };
276277 .. code-block:: c++
277278
278279 Function *FunctionAST::codegen() {
279 NamedValues.clear();
280
281 Function *TheFunction = Proto->codegen();
280 // Transfer ownership of the prototype to the FunctionProtos map, but keep a
281 // reference to it for use below.
282 auto &P = *Proto;
283 FunctionProtos[Proto->getName()] = std::move(Proto);
284 Function *TheFunction = getFunction(P.getName());
282285 if (!TheFunction)
283286 return nullptr;
284287
285288 // If this is an operator, install it.
286 if (Proto->isBinaryOp())
287 BinopPrecedence[Proto->getOperatorName()] = Proto->getBinaryPrecedence();
289 if (P.isBinaryOp())
290 BinopPrecedence[P.getOperatorName()] = P.getBinaryPrecedence();
288291
289292 // Create a new basic block to start insertion into.
290 BasicBlock *BB = BasicBlock::Create(LLVMContext, "entry", TheFunction);
291 Builder.SetInsertPoint(BB);
292
293 if (Value *RetVal = Body->codegen()) {
294 ...
293 BasicBlock *BB = BasicBlock::Create(TheContext, "entry", TheFunction);
294 ...
295295
296296 Basically, before codegening a function, if it is a user-defined
297297 operator, we register it in the precedence table. This allows the binary
322322 public:
323323 UnaryExprAST(char Opcode, std::unique_ptr Operand)
324324 : Opcode(Opcode), Operand(std::move(Operand)) {}
325 virtual Value *codegen();
325
326 Value *codegen() override;
326327 };
327328
328329 This AST node is very simple and obvious by now. It directly mirrors the
344345 int Opc = CurTok;
345346 getNextToken();
346347 if (auto Operand = ParseUnary())
347 return llvm::unique_ptr(Opc, std::move(Operand));
348 return llvm::make_unique(Opc, std::move(Operand));
348349 return nullptr;
349350 }
350351
432433 if (!OperandV)
433434 return nullptr;
434435
435 Function *F = TheModule->getFunction(std::string("unary")+Opcode);
436 Function *F = getFunction(std::string("unary") + Opcode);
436437 if (!F)
437438 return LogErrorV("Unknown unary operator");
438439
460461 declare double @printd(double)
461462
462463 ready> def binary : 1 (x y) 0; # Low-precedence operator that ignores operands.
463 ..
464 ...
464465 ready> printd(123) : printd(456) : printd(789);
465466 123.000000
466467 456.000000
517518
518519 ::
519520
520 ready>
521
522 extern putchard(char)
523 def printdensity(d)
521 ready> extern putchard(char);
522 ...
523 ready> def printdensity(d)
524524 if d > 8 then
525525 putchard(32) # ' '
526526 else if d > 4 then
537537 Evaluated to 0.000000
538538
539539 Based on these simple primitive operations, we can start to define more
540 interesting things. For example, here's a little function that solves
541 for the number of iterations it takes a function in the complex plane to
542 converge:
540 interesting things. For example, here's a little function that determines
541 the number of iterations it takes for a certain function in the complex
542 plane to diverge:
543543
544544 ::
545545
741741 =================
742742
743743 Here is the complete code listing for our running example, enhanced with
744 the if/then/else and for expressions.. To build this example, use:
744 the support for user-defined operators. To build this example, use:
745745
746746 .. code-block:: bash
747747
326326
327327 static std::map NamedValues;
328328
329 Also, since we will need to create these alloca's, we'll use a helper
329 Also, since we will need to create these allocas, we'll use a helper
330330 function that ensures that the allocas are created in the entry block of
331331 the function:
332332
338338 const std::string &VarName) {
339339 IRBuilder<> TmpB(&TheFunction->getEntryBlock(),
340340 TheFunction->getEntryBlock().begin());
341 return TmpB.CreateAlloca(Type::getDoubleTy(LLVMContext), 0,
341 return TmpB.CreateAlloca(Type::getDoubleTy(TheContext), 0,
342342 VarName.c_str());
343343 }
344344
347347 alloca with the expected name and returns it. Because all values in
348348 Kaleidoscope are doubles, there is no need to pass in a type to use.
349349
350 With this in place, the first functionality change we want to make is to
350 With this in place, the first functionality change we want to make belongs to
351351 variable references. In our new scheme, variables live on the stack, so
352352 code generating a reference to them actually needs to produce a load
353353 from the stack slot:
376376 // Create an alloca for the variable in the entry block.
377377 AllocaInst *Alloca = CreateEntryBlockAlloca(TheFunction, VarName);
378378
379 // Emit the start code first, without 'variable' in scope.
379 // Emit the start code first, without 'variable' in scope.
380380 Value *StartVal = Start->codegen();
381381 if (!StartVal)
382382 return nullptr;
407407
408408 .. code-block:: c++
409409
410 /// CreateArgumentAllocas - Create an alloca for each argument and register the
411 /// argument in the symbol table so that references to it will succeed.
412 void PrototypeAST::CreateArgumentAllocas(Function *F) {
413 Function::arg_iterator AI = F->arg_begin();
414 for (unsigned Idx = 0, e = Args.size(); Idx != e; ++Idx, ++AI) {
410 Function *FunctionAST::codegen() {
411 ...
412 Builder.SetInsertPoint(BB);
413
414 // Record the function arguments in the NamedValues map.
415 NamedValues.clear();
416 for (auto &Arg : TheFunction->args()) {
415417 // Create an alloca for this variable.
416 AllocaInst *Alloca = CreateEntryBlockAlloca(F, Args[Idx]);
418 AllocaInst *Alloca = CreateEntryBlockAlloca(TheFunction, Arg.getName());
417419
418420 // Store the initial value into the alloca.
419 Builder.CreateStore(AI, Alloca);
421 Builder.CreateStore(&Arg, Alloca);
420422
421423 // Add arguments to variable symbol table.
422 NamedValues[Args[Idx]] = Alloca;
424 NamedValues[Arg.getName()] = Alloca;
423425 }
424 }
426
427 if (Value *RetVal = Body->codegen()) {
428 ...
425429
426430 For each argument, we make an alloca, store the input value to the
427431 function into the alloca, and register the alloca as the memory location
433437
434438 .. code-block:: c++
435439
436 // Set up the optimizer pipeline. Start with registering info about how the
437 // target lays out data structures.
438 OurFPM.add(new DataLayout(*TheExecutionEngine->getDataLayout()));
439440 // Promote allocas to registers.
440 OurFPM.add(createPromoteMemoryToRegisterPass());
441 TheFPM->add(createPromoteMemoryToRegisterPass());
441442 // Do simple "peephole" optimizations and bit-twiddling optzns.
442 OurFPM.add(createInstructionCombiningPass());
443 TheFPM->add(createInstructionCombiningPass());
443444 // Reassociate expressions.
444 OurFPM.add(createReassociatePass());
445 TheFPM->add(createReassociatePass());
446 ...
445447
446448 It is interesting to see what the code looks like before and after the
447449 mem2reg optimization runs. For example, this is the before/after code
453455 entry:
454456 %x1 = alloca double
455457 store double %x, double* %x1
456 %x2 = load double* %x1
458 %x2 = load double, double* %x1
457459 %cmptmp = fcmp ult double %x2, 3.000000e+00
458460 %booltmp = uitofp i1 %cmptmp to double
459461 %ifcond = fcmp one double %booltmp, 0.000000e+00
463465 br label %ifcont
464466
465467 else: ; preds = %entry
466 %x3 = load double* %x1
468 %x3 = load double, double* %x1
467469 %subtmp = fsub double %x3, 1.000000e+00
468470 %calltmp = call double @fib(double %subtmp)
469 %x4 = load double* %x1
471 %x4 = load double, double* %x1
470472 %subtmp5 = fsub double %x4, 2.000000e+00
471473 %calltmp6 = call double @fib(double %subtmp5)
472474 %addtmp = fadd double %calltmp, %calltmp6
676678
677679 public:
678680 VarExprAST(std::vector>> VarNames,
679 std::unique_ptr body)
680 : VarNames(std::move(VarNames)), Body(std::move(Body)) {}
681
682 virtual Value *codegen();
681 std::unique_ptr Body)
682 : VarNames(std::move(VarNames)), Body(std::move(Body)) {}
683
684 Value *codegen() override;
683685 };
684686
685687 var/in allows a list of names to be defined all at once, and each name
811813 if (!InitVal)
812814 return nullptr;
813815 } else { // If not specified, use 0.0.
814 InitVal = ConstantFP::get(LLVMContext, APFloat(0.0));
816 InitVal = ConstantFP::get(TheContext, APFloat(0.0));
815817 }
816818
817819 AllocaInst *Alloca = CreateEntryBlockAlloca(TheFunction, VarName);
1717 translate from binary and the state of the machine back to the
1818 source that the programmer wrote. In LLVM we generally use a format
1919 called `DWARF `_. DWARF is a compact encoding
20 that represents types, source locations, and variable locations.
20 that represents types, source locations, and variable locations.
2121
2222 The short summary of this chapter is that we'll go through the
2323 various things you have to add to a programming language to
9393 return;
9494 @@ -1184,7 +1183,6 @@ int main() {
9595 BinopPrecedence['*'] = 40; // highest.
96
96
9797 // Prime the first token.
9898 - fprintf(stderr, "ready> ");
9999 getNextToken();
100
100
101101 Lastly we're going to disable all of the optimization passes and the JIT so
102102 that the only thing that happens after we're done parsing and generating
103 code is that the llvm IR goes to standard error:
103 code is that the LLVM IR goes to standard error:
104104
105105 .. code-block:: udiff
106106
139139 -
140140 + #endif
141141 OurFPM.doInitialization();
142
142
143143 // Set the global so the code gen can use this.
144144
145145 This relatively small set of changes get us to the point that we can compile
165165
166166 Similar to the ``IRBuilder`` class we have a
167167 `DIBuilder `_ class
168 that helps in constructing debug metadata for an llvm IR file. It
169 corresponds 1:1 similarly to ``IRBuilder`` and llvm IR, but with nicer names.
168 that helps in constructing debug metadata for an LLVM IR file. It
169 corresponds 1:1 similarly to ``IRBuilder`` and LLVM IR, but with nicer names.
170170 Using it does require that you be more familiar with DWARF terminology than
171171 you needed to be with ``IRBuilder`` and ``Instruction`` names, but if you
172172 read through the general documentation on the
193193 } KSDbgInfo;
194194
195195 DIType *DebugInfo::getDoubleTy() {
196 if (DblTy.isValid())
196 if (DblTy)
197197 return DblTy;
198198
199199 DblTy = DBuilder->createBasicType("double", 64, 64, dwarf::DW_ATE_float);
213213 compile unit for a language called Kaleidoscope we used the language
214214 constant for C. This is because a debugger wouldn't necessarily understand
215215 the calling conventions or default ABI for a language it doesn't recognize
216 and we follow the C ABI in our llvm code generation so it's the closest
216 and we follow the C ABI in our LLVM code generation so it's the closest
217217 thing to accurate. This ensures we can actually call functions from the
218218 debugger and have them execute. Secondly, you'll see the "fib.ks" in the
219219 call to ``createCompileUnit``. This is a default hard coded value since
258258 unsigned LineNo = 0;
259259 unsigned ScopeLine = 0;
260260 DISubprogram *SP = DBuilder->createFunction(
261 FContext, Name, StringRef(), Unit, LineNo,
262 CreateFunctionType(Args.size(), Unit), false /* internal linkage */,
263 true /* definition */, ScopeLine, DINode::FlagPrototyped, false);
264 F->setSubprogram(SP);
261 FContext, P.getName(), StringRef(), Unit, LineNo,
262 CreateFunctionType(TheFunction->arg_size(), Unit),
263 false /* internal linkage */, true /* definition */, ScopeLine,
264 DINode::FlagPrototyped, false);
265 TheFunction->setSubprogram(SP);
265266
266267 and we now have an DISubprogram that contains a reference to all of our
267268 metadata for the function.
325326
326327 giving us locations for each of our expressions and variables.
327328
328 From this we can make sure to tell ``DIBuilder`` when we're at a new source
329 location so it can use that when we generate the rest of our code and make
330 sure that each instruction has source location information. We do this
331 by constructing another small function:
329 To make sure that every instruction gets proper source location information,
330 we have to tell ``Builder`` whenever we're at a new source location.
331 We use a small helper function for this:
332332
333333 .. code-block:: c++
334334
342342 DebugLoc::get(AST->getLine(), AST->getCol(), Scope));
343343 }
344344
345 that both tells the main ``IRBuilder`` where we are, but also what scope
346 we're in. Since we've just created a function above we can either be in
347 the main file scope (like when we created our function), or now we can be
348 in the function scope we just created. To represent this we create a stack
349 of scopes:
345 This both tells the main ``IRBuilder`` where we are, but also what scope
346 we're in. The scope can either be on compile-unit level or be the nearest
347 enclosing lexical block like the current function.
348 To represent this we create a stack of scopes:
350349
351350 .. code-block:: c++
352351
353352 std::vector LexicalBlocks;
354 std::map FnScopeMap;
355
356 and keep a map of each function to the scope that it represents (an
357 DISubprogram is also an DIScope).
358
359 Then we make sure to:
360
361 .. code-block:: c++
362
363 KSDbgInfo.emitLocation(this);
364
365 emit the location every time we start to generate code for a new AST, and
366 also:
367
368 .. code-block:: c++
369
370 KSDbgInfo.FnScopeMap[this] = SP;
371
372 store the scope (function) when we create it and use it:
373
374 KSDbgInfo.LexicalBlocks.push_back(&KSDbgInfo.FnScopeMap[Proto]);
375
376 when we start generating the code for each function.
377
378 also, don't forget to pop the scope back off of your scope stack at the
353
354 and push the scope (function) to the top of the stack when we start
355 generating the code for each function:
356
357 .. code-block:: c++
358
359 KSDbgInfo.LexicalBlocks.push_back(SP);
360
361 Also, we may not forget to pop the scope back off of the scope stack at the
379362 end of the code generation for the function:
380363
381364 .. code-block:: c++
383366 // Pop off the lexical block for the function since we added it
384367 // unconditionally.
385368 KSDbgInfo.LexicalBlocks.pop_back();
369
370 Then we make sure to emit the location every time we start to generate code
371 for a new AST object:
372
373 .. code-block:: c++
374
375 KSDbgInfo.emitLocation(this);
386376
387377 Variables
388378 =========
391381 we have in scope. Let's get our function arguments set up so we can get
392382 decent backtraces and see how our functions are being called. It isn't
393383 a lot of code, and we generally handle it when we're creating the
394 argument allocas in ``PrototypeAST::CreateArgumentAllocas``.
395
396 .. code-block:: c++
397
398 DIScope *Scope = KSDbgInfo.LexicalBlocks.back();
399 DIFile *Unit = DBuilder->createFile(KSDbgInfo.TheCU.getFilename(),
400 KSDbgInfo.TheCU.getDirectory());
401 DILocalVariable D = DBuilder->createParameterVariable(
402 Scope, Args[Idx], Idx + 1, Unit, Line, KSDbgInfo.getDoubleTy(), true);
403
404 DBuilder->insertDeclare(Alloca, D, DBuilder->createExpression(),
405 DebugLoc::get(Line, 0, Scope),
406 Builder.GetInsertBlock());
407
408 Here we're doing a few things. First, we're grabbing our current scope
409 for the variable so we can say what range of code our variable is valid
410 through. Second, we're creating the variable, giving it the scope,
384 argument allocas in ``FunctionAST::codegen``.
385
386 .. code-block:: c++
387
388 // Record the function arguments in the NamedValues map.
389 NamedValues.clear();
390 unsigned ArgIdx = 0;
391 for (auto &Arg : TheFunction->args()) {
392 // Create an alloca for this variable.
393 AllocaInst *Alloca = CreateEntryBlockAlloca(TheFunction, Arg.getName());
394
395 // Create a debug descriptor for the variable.
396 DILocalVariable *D = DBuilder->createParameterVariable(
397 SP, Arg.getName(), ++ArgIdx, Unit, LineNo, KSDbgInfo.getDoubleTy(),
398 true);
399
400 DBuilder->insertDeclare(Alloca, D, DBuilder->createExpression(),
401 DebugLoc::get(LineNo, 0, SP),
402 Builder.GetInsertBlock());
403
404 // Store the initial value into the alloca.
405 Builder.CreateStore(&Arg, Alloca);
406
407 // Add arguments to variable symbol table.
408 NamedValues[Arg.getName()] = Alloca;
409 }
410
411
412 Here we're first creating the variable, giving it the scope (``SP``),
411413 the name, source location, type, and since it's an argument, the argument
412 index. Third, we create an ``lvm.dbg.declare`` call to indicate at the IR
414 index. Next, we create an ``lvm.dbg.declare`` call to indicate at the IR
413415 level that we've got a variable in an alloca (and it gives a starting
414416 location for the variable), and setting a source location for the
415417 beginning of the scope on the declare.
419421 in the past. In this case we need to do a little bit of a hack to avoid
420422 generating line information for the function prologue so that the debugger
421423 knows to skip over those instructions when setting a breakpoint. So in
422 ``FunctionAST::CodeGen`` we add a couple of lines:
424 ``FunctionAST::CodeGen`` we add some more lines:
423425
424426 .. code-block:: c++
425427
433435
434436 .. code-block:: c++
435437
436 KSDbgInfo.emitLocation(Body);
438 KSDbgInfo.emitLocation(Body.get());
437439
438440 With this we have enough debug information to set breakpoints in functions,
439441 print out argument variables, and call functions. Not too bad for just a
139139 public:
140140 PrototypeAST(const std::string &Name, std::vector Args)
141141 : Name(Name), Args(std::move(Args)) {}
142
143 const std::string &getName() const { return Name; }
142144 };
143145
144146 /// FunctionAST - This class represents a function definition itself.
649649 // "Library" functions that can be "extern'd" from user code.
650650 //===----------------------------------------------------------------------===//
651651
652 #ifdef LLVM_ON_WIN32
653 #define DLLEXPORT __declspec(dllexport)
654 #else
655 #define DLLEXPORT
656 #endif
657
652658 /// putchard - putchar that takes a double and returns 0.
653 extern "C" double putchard(double X) {
659 extern "C" DLLEXPORT double putchard(double X) {
654660 fputc((char)X, stderr);
655661 return 0;
656662 }
657663
658664 /// printd - printf that takes a double prints it as "%f\n", returning 0.
659 extern "C" double printd(double X) {
665 extern "C" DLLEXPORT double printd(double X) {
660666 fprintf(stderr, "%f\n", X);
661667 return 0;
662668 }
621621 if (!CondV)
622622 return nullptr;
623623
624 // Convert condition to a bool by comparing equal to 0.0.
624 // Convert condition to a bool by comparing non-equal to 0.0.
625625 CondV = Builder.CreateFCmpONE(
626626 CondV, ConstantFP::get(TheContext, APFloat(0.0)), "ifcond");
627627
735735 if (!EndCond)
736736 return nullptr;
737737
738 // Convert condition to a bool by comparing equal to 0.0.
738 // Convert condition to a bool by comparing non-equal to 0.0.
739739 EndCond = Builder.CreateFCmpONE(
740740 EndCond, ConstantFP::get(TheContext, APFloat(0.0)), "loopcond");
741741
923923 // "Library" functions that can be "extern'd" from user code.
924924 //===----------------------------------------------------------------------===//
925925
926 #ifdef LLVM_ON_WIN32
927 #define DLLEXPORT __declspec(dllexport)
928 #else
929 #define DLLEXPORT
930 #endif
931
926932 /// putchard - putchar that takes a double and returns 0.
927 extern "C" double putchard(double X) {
933 extern "C" DLLEXPORT double putchard(double X) {
928934 fputc((char)X, stderr);
929935 return 0;
930936 }
931937
932938 /// printd - printf that takes a double prints it as "%f\n", returning 0.
933 extern "C" double printd(double X) {
939 extern "C" DLLEXPORT double printd(double X) {
934940 fprintf(stderr, "%f\n", X);
935941 return 0;
936942 }
566566 // Read the precedence if present.
567567 if (CurTok == tok_number) {
568568 if (NumVal < 1 || NumVal > 100)
569 return LogErrorP("Invalid precedecnce: must be 1..100");
569 return LogErrorP("Invalid precedence: must be 1..100");
570570 BinaryPrecedence = (unsigned)NumVal;
571571 getNextToken();
572572 }
733733 if (!CondV)
734734 return nullptr;
735735
736 // Convert condition to a bool by comparing equal to 0.0.
736 // Convert condition to a bool by comparing non-equal to 0.0.
737737 CondV = Builder.CreateFCmpONE(
738738 CondV, ConstantFP::get(TheContext, APFloat(0.0)), "ifcond");
739739
847847 if (!EndCond)
848848 return nullptr;
849849
850 // Convert condition to a bool by comparing equal to 0.0.
850 // Convert condition to a bool by comparing non-equal to 0.0.
851851 EndCond = Builder.CreateFCmpONE(
852852 EndCond, ConstantFP::get(TheContext, APFloat(0.0)), "loopcond");
853853
10421042 // "Library" functions that can be "extern'd" from user code.
10431043 //===----------------------------------------------------------------------===//
10441044
1045 #ifdef LLVM_ON_WIN32
1046 #define DLLEXPORT __declspec(dllexport)
1047 #else
1048 #define DLLEXPORT
1049 #endif
1050
10451051 /// putchard - putchar that takes a double and returns 0.
1046 extern "C" double putchard(double X) {
1052 extern "C" DLLEXPORT double putchard(double X) {
10471053 fputc((char)X, stderr);
10481054 return 0;
10491055 }
10501056
10511057 /// printd - printf that takes a double prints it as "%f\n", returning 0.
1052 extern "C" double printd(double X) {
1058 extern "C" DLLEXPORT double printd(double X) {
10531059 fprintf(stderr, "%f\n", X);
10541060 return 0;
10551061 }
638638 // Read the precedence if present.
639639 if (CurTok == tok_number) {
640640 if (NumVal < 1 || NumVal > 100)
641 return LogErrorP("Invalid precedecnce: must be 1..100");
641 return LogErrorP("Invalid precedence: must be 1..100");
642642 BinaryPrecedence = (unsigned)NumVal;
643643 getNextToken();
644644 }
839839 if (!CondV)
840840 return nullptr;
841841
842 // Convert condition to a bool by comparing equal to 0.0.
842 // Convert condition to a bool by comparing non-equal to 0.0.
843843 CondV = Builder.CreateFCmpONE(
844844 CondV, ConstantFP::get(TheContext, APFloat(0.0)), "ifcond");
845845
962962 Value *NextVar = Builder.CreateFAdd(CurVar, StepVal, "nextvar");
963963 Builder.CreateStore(NextVar, Alloca);
964964
965 // Convert condition to a bool by comparing equal to 0.0.
965 // Convert condition to a bool by comparing non-equal to 0.0.
966966 EndCond = Builder.CreateFCmpONE(
967967 EndCond, ConstantFP::get(TheContext, APFloat(0.0)), "loopcond");
968968
11141114 // Create a new pass manager attached to it.
11151115 TheFPM = llvm::make_unique(TheModule.get());
11161116
1117 // Promote allocas to registers.
1118 TheFPM->add(createPromoteMemoryToRegisterPass());
11171119 // Do simple "peephole" optimizations and bit-twiddling optzns.
11181120 TheFPM->add(createInstructionCombiningPass());
11191121 // Reassociate expressions.
12091211 // "Library" functions that can be "extern'd" from user code.
12101212 //===----------------------------------------------------------------------===//
12111213
1214 #ifdef LLVM_ON_WIN32
1215 #define DLLEXPORT __declspec(dllexport)
1216 #else
1217 #define DLLEXPORT
1218 #endif
1219
12121220 /// putchard - putchar that takes a double and returns 0.
1213 extern "C" double putchard(double X) {
1221 extern "C" DLLEXPORT double putchard(double X) {
12141222 fputc((char)X, stderr);
12151223 return 0;
12161224 }
12171225
12181226 /// printd - printf that takes a double prints it as "%f\n", returning 0.
1219 extern "C" double printd(double X) {
1227 extern "C" DLLEXPORT double printd(double X) {
12201228 fprintf(stderr, "%f\n", X);
12211229 return 0;
12221230 }
641641 // Read the precedence if present.
642642 if (CurTok == tok_number) {
643643 if (NumVal < 1 || NumVal > 100)
644 return LogErrorP("Invalid precedecnce: must be 1..100");
644 return LogErrorP("Invalid precedence: must be 1..100");
645645 BinaryPrecedence = (unsigned)NumVal;
646646 getNextToken();
647647 }
840840 if (!CondV)
841841 return nullptr;
842842
843 // Convert condition to a bool by comparing equal to 0.0.
843 // Convert condition to a bool by comparing non-equal to 0.0.
844844 CondV = Builder.CreateFCmpONE(
845845 CondV, ConstantFP::get(TheContext, APFloat(0.0)), "ifcond");
846846
963963 Value *NextVar = Builder.CreateFAdd(CurVar, StepVal, "nextvar");
964964 Builder.CreateStore(NextVar, Alloca);
965965
966 // Convert condition to a bool by comparing equal to 0.0.
966 // Convert condition to a bool by comparing non-equal to 0.0.
967967 EndCond = Builder.CreateFCmpONE(
968968 EndCond, ConstantFP::get(TheContext, APFloat(0.0)), "loopcond");
969969
11721172 // "Library" functions that can be "extern'd" from user code.
11731173 //===----------------------------------------------------------------------===//
11741174
1175 #ifdef LLVM_ON_WIN32
1176 #define DLLEXPORT __declspec(dllexport)
1177 #else
1178 #define DLLEXPORT
1179 #endif
1180
11751181 /// putchard - putchar that takes a double and returns 0.
1176 extern "C" double putchard(double X) {
1182 extern "C" DLLEXPORT double putchard(double X) {
11771183 fputc((char)X, stderr);
11781184 return 0;
11791185 }
11801186
11811187 /// printd - printf that takes a double prints it as "%f\n", returning 0.
1182 extern "C" double printd(double X) {
1188 extern "C" DLLEXPORT double printd(double X) {
11831189 fprintf(stderr, "%f\n", X);
11841190 return 0;
11851191 }
755755 // Read the precedence if present.
756756 if (CurTok == tok_number) {
757757 if (NumVal < 1 || NumVal > 100)
758 return LogErrorP("Invalid precedecnce: must be 1..100");
758 return LogErrorP("Invalid precedence: must be 1..100");
759759 BinaryPrecedence = (unsigned)NumVal;
760760 getNextToken();
761761 }
10031003 if (!CondV)
10041004 return nullptr;
10051005
1006 // Convert condition to a bool by comparing equal to 0.0.
1006 // Convert condition to a bool by comparing non-equal to 0.0.
10071007 CondV = Builder.CreateFCmpONE(
10081008 CondV, ConstantFP::get(TheContext, APFloat(0.0)), "ifcond");
10091009
11281128 Value *NextVar = Builder.CreateFAdd(CurVar, StepVal, "nextvar");
11291129 Builder.CreateStore(NextVar, Alloca);
11301130
1131 // Convert condition to a bool by comparing equal to 0.0.
1131 // Convert condition to a bool by comparing non-equal to 0.0.
11321132 EndCond = Builder.CreateFCmpONE(
11331133 EndCond, ConstantFP::get(TheContext, APFloat(0.0)), "loopcond");
11341134
13781378 // "Library" functions that can be "extern'd" from user code.
13791379 //===----------------------------------------------------------------------===//
13801380
1381 #ifdef LLVM_ON_WIN32
1382 #define DLLEXPORT __declspec(dllexport)
1383 #else
1384 #define DLLEXPORT
1385 #endif
1386
13811387 /// putchard - putchar that takes a double and returns 0.
1382 extern "C" double putchard(double X) {
1388 extern "C" DLLEXPORT double putchard(double X) {
13831389 fputc((char)X, stderr);
13841390 return 0;
13851391 }
13861392
13871393 /// printd - printf that takes a double prints it as "%f\n", returning 0.
1388 extern "C" double printd(double X) {
1394 extern "C" DLLEXPORT double printd(double X) {
13891395 fprintf(stderr, "%f\n", X);
13901396 return 0;
13911397 }
9696 }
9797
9898 JITSymbol findMangledSymbol(const std::string &Name) {
99 #ifdef LLVM_ON_WIN32
100 // The symbol lookup of ObjectLinkingLayer uses the SymbolRef::SF_Exported
101 // flag to decide whether a symbol will be visible or not, when we call
102 // IRCompileLayer::findSymbolIn with ExportedSymbolsOnly set to true.
103 //
104 // But for Windows COFF objects, this flag is currently never set.
105 // For a potential solution see: https://reviews.llvm.org/rL258665
106 // For now, we allow non-exported symbols on Windows as a workaround.
107 const bool ExportedSymbolsOnly = false;
108 #else
109 const bool ExportedSymbolsOnly = true;
110 #endif
111
99112 // Search modules in reverse order: from last added to first added.
100113 // This is the opposite of the usual search order for dlsym, but makes more
101114 // sense in a REPL where we want to bind to the newest available definition.
102115 for (auto H : make_range(ModuleHandles.rbegin(), ModuleHandles.rend()))
103 if (auto Sym = CompileLayer.findSymbolIn(H, Name, true))
116 if (auto Sym = CompileLayer.findSymbolIn(H, Name, ExportedSymbolsOnly))
104117 return Sym;
105118
106119 // If we can't find the symbol in the JIT, try looking in the host process.
107120 if (auto SymAddr = RTDyldMemoryManager::getSymbolAddressInProcess(Name))
108121 return JITSymbol(SymAddr, JITSymbolFlags::Exported);
122
123 #ifdef LLVM_ON_WIN32
124 // For Windows retry without "_" at begining, as RTDyldMemoryManager uses
125 // GetProcAddress and standard libraries like msvcrt.dll use names
126 // with and without "_" (for example "_itoa" but "sin").
127 if (Name.length() > 2 && Name[0] == '_')
128 if (auto SymAddr =
129 RTDyldMemoryManager::getSymbolAddressInProcess(Name.substr(1)))
130 return JITSymbol(SymAddr, JITSymbolFlags::Exported);
131 #endif
109132
110133 return nullptr;
111134 }