diff options
author | Dimitry Andric <dim@FreeBSD.org> | 2017-04-16 16:01:22 +0000 |
---|---|---|
committer | Dimitry Andric <dim@FreeBSD.org> | 2017-04-16 16:01:22 +0000 |
commit | 71d5a2540a98c81f5bcaeb48805e0e2881f530ef (patch) | |
tree | 5343938942df402b49ec7300a1c25a2d4ccd5821 /examples | |
parent | 31bbf64f3a4974a2d6c8b3b27ad2f519caf74057 (diff) |
Diffstat (limited to 'examples')
31 files changed, 186 insertions, 93 deletions
diff --git a/examples/BrainF/BrainF.cpp b/examples/BrainF/BrainF.cpp index 91d813a6c3bb..8af34d04701e 100644 --- a/examples/BrainF/BrainF.cpp +++ b/examples/BrainF/BrainF.cpp @@ -74,18 +74,18 @@ void BrainF::header(LLVMContext& C) { //declare i32 @getchar() getchar_func = cast<Function>(module-> - getOrInsertFunction("getchar", IntegerType::getInt32Ty(C), NULL)); + getOrInsertFunction("getchar", IntegerType::getInt32Ty(C))); //declare i32 @putchar(i32) putchar_func = cast<Function>(module-> getOrInsertFunction("putchar", IntegerType::getInt32Ty(C), - IntegerType::getInt32Ty(C), NULL)); + IntegerType::getInt32Ty(C))); //Function header //define void @brainf() brainf_func = cast<Function>(module-> - getOrInsertFunction("brainf", Type::getVoidTy(C), NULL)); + getOrInsertFunction("brainf", Type::getVoidTy(C))); builder = new IRBuilder<>(BasicBlock::Create(C, label, brainf_func)); @@ -156,7 +156,7 @@ void BrainF::header(LLVMContext& C) { //declare i32 @puts(i8 *) Function *puts_func = cast<Function>(module-> getOrInsertFunction("puts", IntegerType::getInt32Ty(C), - PointerType::getUnqual(IntegerType::getInt8Ty(C)), NULL)); + PointerType::getUnqual(IntegerType::getInt8Ty(C)))); //brainf.aberror: aberrorbb = BasicBlock::Create(C, label, brainf_func); diff --git a/examples/BrainF/BrainFDriver.cpp b/examples/BrainF/BrainFDriver.cpp index 57a86fbf5212..65f8033a7e27 100644 --- a/examples/BrainF/BrainFDriver.cpp +++ b/examples/BrainF/BrainFDriver.cpp @@ -77,7 +77,7 @@ void addMainFunction(Module *mod) { getOrInsertFunction("main", IntegerType::getInt32Ty(mod->getContext()), IntegerType::getInt32Ty(mod->getContext()), PointerType::getUnqual(PointerType::getUnqual( - IntegerType::getInt8Ty(mod->getContext()))), NULL)); + IntegerType::getInt8Ty(mod->getContext()))))); { Function::arg_iterator args = main_func->arg_begin(); Value *arg_0 = &*args++; @@ -166,6 +166,10 @@ int main(int argc, char **argv) { std::vector<GenericValue> args; Function *brainf_func = M.getFunction("brainf"); GenericValue gv = ee->runFunction(brainf_func, args); + // Genereated code calls putchar, and output is not guaranteed without fflush. + // The better place for fflush(stdout) call would be the generated code, but it + // is unmanageable because stdout linkage name depends on stdlib implementation. + fflush(stdout); } else { WriteBitcodeToFile(Mod.get(), *out); } diff --git a/examples/Fibonacci/fibonacci.cpp b/examples/Fibonacci/fibonacci.cpp index 16e52bf04099..662cb01dd37e 100644 --- a/examples/Fibonacci/fibonacci.cpp +++ b/examples/Fibonacci/fibonacci.cpp @@ -54,8 +54,7 @@ static Function *CreateFibFunction(Module *M, LLVMContext &Context) { // to return an int and take an int parameter. Function *FibF = cast<Function>(M->getOrInsertFunction("fib", Type::getInt32Ty(Context), - Type::getInt32Ty(Context), - nullptr)); + Type::getInt32Ty(Context))); // Add a basic block to the function. BasicBlock *BB = BasicBlock::Create(Context, "EntryBlock", FibF); diff --git a/examples/HowToUseJIT/HowToUseJIT.cpp b/examples/HowToUseJIT/HowToUseJIT.cpp index 0050d27b45d7..f141fa5a7f54 100644 --- a/examples/HowToUseJIT/HowToUseJIT.cpp +++ b/examples/HowToUseJIT/HowToUseJIT.cpp @@ -69,11 +69,9 @@ int main() { // Create the add1 function entry and insert this entry into module M. The // function will have a return type of "int" and take an argument of "int". - // The '0' terminates the list of argument types. Function *Add1F = cast<Function>(M->getOrInsertFunction("add1", Type::getInt32Ty(Context), - Type::getInt32Ty(Context), - nullptr)); + Type::getInt32Ty(Context))); // Add a basic block to the function. As before, it automatically inserts // because of the last argument. @@ -102,8 +100,7 @@ int main() { // Now we're going to create function `foo', which returns an int and takes no // arguments. Function *FooF = - cast<Function>(M->getOrInsertFunction("foo", Type::getInt32Ty(Context), - nullptr)); + cast<Function>(M->getOrInsertFunction("foo", Type::getInt32Ty(Context))); // Add a basic block to the FooF function. BB = BasicBlock::Create(Context, "EntryBlock", FooF); diff --git a/examples/Kaleidoscope/BuildingAJIT/Chapter1/KaleidoscopeJIT.h b/examples/Kaleidoscope/BuildingAJIT/Chapter1/KaleidoscopeJIT.h index 22716b2b5215..a14fd1dc20ec 100644 --- a/examples/Kaleidoscope/BuildingAJIT/Chapter1/KaleidoscopeJIT.h +++ b/examples/Kaleidoscope/BuildingAJIT/Chapter1/KaleidoscopeJIT.h @@ -22,7 +22,7 @@ #include "llvm/ExecutionEngine/Orc/CompileUtils.h" #include "llvm/ExecutionEngine/Orc/IRCompileLayer.h" #include "llvm/ExecutionEngine/Orc/LambdaResolver.h" -#include "llvm/ExecutionEngine/Orc/ObjectLinkingLayer.h" +#include "llvm/ExecutionEngine/Orc/RTDyldObjectLinkingLayer.h" #include "llvm/IR/DataLayout.h" #include "llvm/IR/Mangler.h" #include "llvm/Support/DynamicLibrary.h" @@ -40,7 +40,7 @@ class KaleidoscopeJIT { private: std::unique_ptr<TargetMachine> TM; const DataLayout DL; - ObjectLinkingLayer<> ObjectLayer; + RTDyldObjectLinkingLayer<> ObjectLayer; IRCompileLayer<decltype(ObjectLayer)> CompileLayer; public: diff --git a/examples/Kaleidoscope/BuildingAJIT/Chapter1/toy.cpp b/examples/Kaleidoscope/BuildingAJIT/Chapter1/toy.cpp index 50912109e811..945b9706d4d7 100644 --- a/examples/Kaleidoscope/BuildingAJIT/Chapter1/toy.cpp +++ b/examples/Kaleidoscope/BuildingAJIT/Chapter1/toy.cpp @@ -1110,7 +1110,8 @@ static void HandleDefinition() { if (auto FnAST = ParseDefinition()) { if (auto *FnIR = FnAST->codegen()) { fprintf(stderr, "Read function definition:"); - FnIR->dump(); + FnIR->print(errs()); + fprintf(stderr, "\n"); TheJIT->addModule(std::move(TheModule)); InitializeModule(); } @@ -1124,7 +1125,8 @@ static void HandleExtern() { if (auto ProtoAST = ParseExtern()) { if (auto *FnIR = ProtoAST->codegen()) { fprintf(stderr, "Read extern: "); - FnIR->dump(); + FnIR->print(errs()); + fprintf(stderr, "\n"); FunctionProtos[ProtoAST->getName()] = std::move(ProtoAST); } } else { diff --git a/examples/Kaleidoscope/BuildingAJIT/Chapter2/KaleidoscopeJIT.h b/examples/Kaleidoscope/BuildingAJIT/Chapter2/KaleidoscopeJIT.h index 91d903029a00..2039be4571a5 100644 --- a/examples/Kaleidoscope/BuildingAJIT/Chapter2/KaleidoscopeJIT.h +++ b/examples/Kaleidoscope/BuildingAJIT/Chapter2/KaleidoscopeJIT.h @@ -23,7 +23,7 @@ #include "llvm/ExecutionEngine/Orc/IRCompileLayer.h" #include "llvm/ExecutionEngine/Orc/IRTransformLayer.h" #include "llvm/ExecutionEngine/Orc/LambdaResolver.h" -#include "llvm/ExecutionEngine/Orc/ObjectLinkingLayer.h" +#include "llvm/ExecutionEngine/Orc/RTDyldObjectLinkingLayer.h" #include "llvm/IR/DataLayout.h" #include "llvm/IR/LegacyPassManager.h" #include "llvm/IR/Mangler.h" @@ -44,7 +44,7 @@ class KaleidoscopeJIT { private: std::unique_ptr<TargetMachine> TM; const DataLayout DL; - ObjectLinkingLayer<> ObjectLayer; + RTDyldObjectLinkingLayer<> ObjectLayer; IRCompileLayer<decltype(ObjectLayer)> CompileLayer; typedef std::function<std::unique_ptr<Module>(std::unique_ptr<Module>)> diff --git a/examples/Kaleidoscope/BuildingAJIT/Chapter2/toy.cpp b/examples/Kaleidoscope/BuildingAJIT/Chapter2/toy.cpp index 50912109e811..945b9706d4d7 100644 --- a/examples/Kaleidoscope/BuildingAJIT/Chapter2/toy.cpp +++ b/examples/Kaleidoscope/BuildingAJIT/Chapter2/toy.cpp @@ -1110,7 +1110,8 @@ static void HandleDefinition() { if (auto FnAST = ParseDefinition()) { if (auto *FnIR = FnAST->codegen()) { fprintf(stderr, "Read function definition:"); - FnIR->dump(); + FnIR->print(errs()); + fprintf(stderr, "\n"); TheJIT->addModule(std::move(TheModule)); InitializeModule(); } @@ -1124,7 +1125,8 @@ static void HandleExtern() { if (auto ProtoAST = ParseExtern()) { if (auto *FnIR = ProtoAST->codegen()) { fprintf(stderr, "Read extern: "); - FnIR->dump(); + FnIR->print(errs()); + fprintf(stderr, "\n"); FunctionProtos[ProtoAST->getName()] = std::move(ProtoAST); } } else { diff --git a/examples/Kaleidoscope/BuildingAJIT/Chapter3/KaleidoscopeJIT.h b/examples/Kaleidoscope/BuildingAJIT/Chapter3/KaleidoscopeJIT.h index 5ecc869f80f5..d22d41855072 100644 --- a/examples/Kaleidoscope/BuildingAJIT/Chapter3/KaleidoscopeJIT.h +++ b/examples/Kaleidoscope/BuildingAJIT/Chapter3/KaleidoscopeJIT.h @@ -24,7 +24,7 @@ #include "llvm/ExecutionEngine/Orc/IRCompileLayer.h" #include "llvm/ExecutionEngine/Orc/IRTransformLayer.h" #include "llvm/ExecutionEngine/Orc/LambdaResolver.h" -#include "llvm/ExecutionEngine/Orc/ObjectLinkingLayer.h" +#include "llvm/ExecutionEngine/Orc/RTDyldObjectLinkingLayer.h" #include "llvm/IR/DataLayout.h" #include "llvm/IR/LegacyPassManager.h" #include "llvm/IR/Mangler.h" @@ -46,7 +46,7 @@ class KaleidoscopeJIT { private: std::unique_ptr<TargetMachine> TM; const DataLayout DL; - ObjectLinkingLayer<> ObjectLayer; + RTDyldObjectLinkingLayer<> ObjectLayer; IRCompileLayer<decltype(ObjectLayer)> CompileLayer; typedef std::function<std::unique_ptr<Module>(std::unique_ptr<Module>)> @@ -70,7 +70,7 @@ public: CompileCallbackManager( orc::createLocalCompileCallbackManager(TM->getTargetTriple(), 0)), CODLayer(OptimizeLayer, - [this](Function &F) { return std::set<Function*>({&F}); }, + [](Function &F) { return std::set<Function*>({&F}); }, *CompileCallbackManager, orc::createLocalIndirectStubsManagerBuilder( TM->getTargetTriple())) { diff --git a/examples/Kaleidoscope/BuildingAJIT/Chapter3/toy.cpp b/examples/Kaleidoscope/BuildingAJIT/Chapter3/toy.cpp index 50912109e811..945b9706d4d7 100644 --- a/examples/Kaleidoscope/BuildingAJIT/Chapter3/toy.cpp +++ b/examples/Kaleidoscope/BuildingAJIT/Chapter3/toy.cpp @@ -1110,7 +1110,8 @@ static void HandleDefinition() { if (auto FnAST = ParseDefinition()) { if (auto *FnIR = FnAST->codegen()) { fprintf(stderr, "Read function definition:"); - FnIR->dump(); + FnIR->print(errs()); + fprintf(stderr, "\n"); TheJIT->addModule(std::move(TheModule)); InitializeModule(); } @@ -1124,7 +1125,8 @@ static void HandleExtern() { if (auto ProtoAST = ParseExtern()) { if (auto *FnIR = ProtoAST->codegen()) { fprintf(stderr, "Read extern: "); - FnIR->dump(); + FnIR->print(errs()); + fprintf(stderr, "\n"); FunctionProtos[ProtoAST->getName()] = std::move(ProtoAST); } } else { diff --git a/examples/Kaleidoscope/BuildingAJIT/Chapter4/KaleidoscopeJIT.h b/examples/Kaleidoscope/BuildingAJIT/Chapter4/KaleidoscopeJIT.h index 527d4be09f0f..e0a78410f713 100644 --- a/examples/Kaleidoscope/BuildingAJIT/Chapter4/KaleidoscopeJIT.h +++ b/examples/Kaleidoscope/BuildingAJIT/Chapter4/KaleidoscopeJIT.h @@ -24,7 +24,7 @@ #include "llvm/ExecutionEngine/Orc/IRCompileLayer.h" #include "llvm/ExecutionEngine/Orc/IRTransformLayer.h" #include "llvm/ExecutionEngine/Orc/LambdaResolver.h" -#include "llvm/ExecutionEngine/Orc/ObjectLinkingLayer.h" +#include "llvm/ExecutionEngine/Orc/RTDyldObjectLinkingLayer.h" #include "llvm/IR/DataLayout.h" #include "llvm/IR/LegacyPassManager.h" #include "llvm/IR/Mangler.h" @@ -73,7 +73,7 @@ class KaleidoscopeJIT { private: std::unique_ptr<TargetMachine> TM; const DataLayout DL; - ObjectLinkingLayer<> ObjectLayer; + RTDyldObjectLinkingLayer<> ObjectLayer; IRCompileLayer<decltype(ObjectLayer)> CompileLayer; typedef std::function<std::unique_ptr<Module>(std::unique_ptr<Module>)> diff --git a/examples/Kaleidoscope/BuildingAJIT/Chapter4/toy.cpp b/examples/Kaleidoscope/BuildingAJIT/Chapter4/toy.cpp index 07981a8f79b7..ff4b5220105b 100644 --- a/examples/Kaleidoscope/BuildingAJIT/Chapter4/toy.cpp +++ b/examples/Kaleidoscope/BuildingAJIT/Chapter4/toy.cpp @@ -1126,7 +1126,8 @@ static void HandleExtern() { if (auto ProtoAST = ParseExtern()) { if (auto *FnIR = ProtoAST->codegen()) { fprintf(stderr, "Read extern: "); - FnIR->dump(); + FnIR->print(errs()); + fprintf(stderr, "\n"); FunctionProtos[ProtoAST->getName()] = std::move(ProtoAST); } } else { diff --git a/examples/Kaleidoscope/BuildingAJIT/Chapter5/KaleidoscopeJIT.h b/examples/Kaleidoscope/BuildingAJIT/Chapter5/KaleidoscopeJIT.h index d1ef3c9549ff..70a896fe8f00 100644 --- a/examples/Kaleidoscope/BuildingAJIT/Chapter5/KaleidoscopeJIT.h +++ b/examples/Kaleidoscope/BuildingAJIT/Chapter5/KaleidoscopeJIT.h @@ -26,7 +26,7 @@ #include "llvm/ExecutionEngine/Orc/IRCompileLayer.h" #include "llvm/ExecutionEngine/Orc/IRTransformLayer.h" #include "llvm/ExecutionEngine/Orc/LambdaResolver.h" -#include "llvm/ExecutionEngine/Orc/ObjectLinkingLayer.h" +#include "llvm/ExecutionEngine/Orc/RTDyldObjectLinkingLayer.h" #include "llvm/ExecutionEngine/Orc/OrcRemoteTargetClient.h" #include "llvm/IR/DataLayout.h" #include "llvm/IR/LegacyPassManager.h" @@ -79,7 +79,7 @@ class KaleidoscopeJIT { private: std::unique_ptr<TargetMachine> TM; const DataLayout DL; - ObjectLinkingLayer<> ObjectLayer; + RTDyldObjectLinkingLayer<> ObjectLayer; IRCompileLayer<decltype(ObjectLayer)> CompileLayer; typedef std::function<std::unique_ptr<Module>(std::unique_ptr<Module>)> diff --git a/examples/Kaleidoscope/BuildingAJIT/Chapter5/toy.cpp b/examples/Kaleidoscope/BuildingAJIT/Chapter5/toy.cpp index aa08df9ceb53..edd050959d6b 100644 --- a/examples/Kaleidoscope/BuildingAJIT/Chapter5/toy.cpp +++ b/examples/Kaleidoscope/BuildingAJIT/Chapter5/toy.cpp @@ -1150,7 +1150,8 @@ static void HandleExtern() { if (auto ProtoAST = ParseExtern()) { if (auto *FnIR = ProtoAST->codegen()) { fprintf(stderr, "Read extern: "); - FnIR->dump(); + FnIR->print(errs()); + fprintf(stderr, "\n"); FunctionProtos[ProtoAST->getName()] = std::move(ProtoAST); } } else { diff --git a/examples/Kaleidoscope/Chapter2/toy.cpp b/examples/Kaleidoscope/Chapter2/toy.cpp index 8357c5b63fb7..4dc917e3f06f 100644 --- a/examples/Kaleidoscope/Chapter2/toy.cpp +++ b/examples/Kaleidoscope/Chapter2/toy.cpp @@ -140,6 +140,8 @@ class PrototypeAST { public: PrototypeAST(const std::string &Name, std::vector<std::string> Args) : Name(Name), Args(std::move(Args)) {} + + const std::string &getName() const { return Name; } }; /// FunctionAST - This class represents a function definition itself. diff --git a/examples/Kaleidoscope/Chapter3/toy.cpp b/examples/Kaleidoscope/Chapter3/toy.cpp index fdefb50cc209..8aad3f4d7bec 100644 --- a/examples/Kaleidoscope/Chapter3/toy.cpp +++ b/examples/Kaleidoscope/Chapter3/toy.cpp @@ -522,7 +522,8 @@ static void HandleDefinition() { if (auto FnAST = ParseDefinition()) { if (auto *FnIR = FnAST->codegen()) { fprintf(stderr, "Read function definition:"); - FnIR->dump(); + FnIR->print(errs()); + fprintf(stderr, "\n"); } } else { // Skip token for error recovery. @@ -534,7 +535,8 @@ static void HandleExtern() { if (auto ProtoAST = ParseExtern()) { if (auto *FnIR = ProtoAST->codegen()) { fprintf(stderr, "Read extern: "); - FnIR->dump(); + FnIR->print(errs()); + fprintf(stderr, "\n"); } } else { // Skip token for error recovery. @@ -547,7 +549,8 @@ static void HandleTopLevelExpression() { if (auto FnAST = ParseTopLevelExpr()) { if (auto *FnIR = FnAST->codegen()) { fprintf(stderr, "Read top-level expression:"); - FnIR->dump(); + FnIR->print(errs()); + fprintf(stderr, "\n"); } } else { // Skip token for error recovery. @@ -601,7 +604,7 @@ int main() { MainLoop(); // Print out all of the generated code. - TheModule->dump(); + TheModule->print(errs(), nullptr); return 0; } diff --git a/examples/Kaleidoscope/Chapter4/toy.cpp b/examples/Kaleidoscope/Chapter4/toy.cpp index 39b9563bc287..cf7d6c2bee04 100644 --- a/examples/Kaleidoscope/Chapter4/toy.cpp +++ b/examples/Kaleidoscope/Chapter4/toy.cpp @@ -571,7 +571,8 @@ static void HandleDefinition() { if (auto FnAST = ParseDefinition()) { if (auto *FnIR = FnAST->codegen()) { fprintf(stderr, "Read function definition:"); - FnIR->dump(); + FnIR->print(errs()); + fprintf(stderr, "\n"); TheJIT->addModule(std::move(TheModule)); InitializeModuleAndPassManager(); } @@ -585,7 +586,8 @@ static void HandleExtern() { if (auto ProtoAST = ParseExtern()) { if (auto *FnIR = ProtoAST->codegen()) { fprintf(stderr, "Read extern: "); - FnIR->dump(); + FnIR->print(errs()); + fprintf(stderr, "\n"); FunctionProtos[ProtoAST->getName()] = std::move(ProtoAST); } } else { @@ -648,14 +650,20 @@ static void MainLoop() { // "Library" functions that can be "extern'd" from user code. //===----------------------------------------------------------------------===// +#ifdef LLVM_ON_WIN32 +#define DLLEXPORT __declspec(dllexport) +#else +#define DLLEXPORT +#endif + /// putchard - putchar that takes a double and returns 0. -extern "C" double putchard(double X) { +extern "C" DLLEXPORT double putchard(double X) { fputc((char)X, stderr); return 0; } /// printd - printf that takes a double prints it as "%f\n", returning 0. -extern "C" double printd(double X) { +extern "C" DLLEXPORT double printd(double X) { fprintf(stderr, "%f\n", X); return 0; } diff --git a/examples/Kaleidoscope/Chapter5/toy.cpp b/examples/Kaleidoscope/Chapter5/toy.cpp index 568ec8de5c30..6852973bae40 100644 --- a/examples/Kaleidoscope/Chapter5/toy.cpp +++ b/examples/Kaleidoscope/Chapter5/toy.cpp @@ -622,7 +622,7 @@ Value *IfExprAST::codegen() { if (!CondV) return nullptr; - // Convert condition to a bool by comparing equal to 0.0. + // Convert condition to a bool by comparing non-equal to 0.0. CondV = Builder.CreateFCmpONE( CondV, ConstantFP::get(TheContext, APFloat(0.0)), "ifcond"); @@ -736,7 +736,7 @@ Value *ForExprAST::codegen() { if (!EndCond) return nullptr; - // Convert condition to a bool by comparing equal to 0.0. + // Convert condition to a bool by comparing non-equal to 0.0. EndCond = Builder.CreateFCmpONE( EndCond, ConstantFP::get(TheContext, APFloat(0.0)), "loopcond"); @@ -845,7 +845,8 @@ static void HandleDefinition() { if (auto FnAST = ParseDefinition()) { if (auto *FnIR = FnAST->codegen()) { fprintf(stderr, "Read function definition:"); - FnIR->dump(); + FnIR->print(errs()); + fprintf(stderr, "\n"); TheJIT->addModule(std::move(TheModule)); InitializeModuleAndPassManager(); } @@ -859,7 +860,8 @@ static void HandleExtern() { if (auto ProtoAST = ParseExtern()) { if (auto *FnIR = ProtoAST->codegen()) { fprintf(stderr, "Read extern: "); - FnIR->dump(); + FnIR->print(errs()); + fprintf(stderr, "\n"); FunctionProtos[ProtoAST->getName()] = std::move(ProtoAST); } } else { @@ -922,14 +924,20 @@ static void MainLoop() { // "Library" functions that can be "extern'd" from user code. //===----------------------------------------------------------------------===// +#ifdef LLVM_ON_WIN32 +#define DLLEXPORT __declspec(dllexport) +#else +#define DLLEXPORT +#endif + /// putchard - putchar that takes a double and returns 0. -extern "C" double putchard(double X) { +extern "C" DLLEXPORT double putchard(double X) { fputc((char)X, stderr); return 0; } /// printd - printf that takes a double prints it as "%f\n", returning 0. -extern "C" double printd(double X) { +extern "C" DLLEXPORT double printd(double X) { fprintf(stderr, "%f\n", X); return 0; } diff --git a/examples/Kaleidoscope/Chapter6/toy.cpp b/examples/Kaleidoscope/Chapter6/toy.cpp index 0fa128d2e557..1e0ddca29b61 100644 --- a/examples/Kaleidoscope/Chapter6/toy.cpp +++ b/examples/Kaleidoscope/Chapter6/toy.cpp @@ -567,7 +567,7 @@ static std::unique_ptr<PrototypeAST> ParsePrototype() { // Read the precedence if present. if (CurTok == tok_number) { if (NumVal < 1 || NumVal > 100) - return LogErrorP("Invalid precedecnce: must be 1..100"); + return LogErrorP("Invalid precedence: must be 1..100"); BinaryPrecedence = (unsigned)NumVal; getNextToken(); } @@ -734,7 +734,7 @@ Value *IfExprAST::codegen() { if (!CondV) return nullptr; - // Convert condition to a bool by comparing equal to 0.0. + // Convert condition to a bool by comparing non-equal to 0.0. CondV = Builder.CreateFCmpONE( CondV, ConstantFP::get(TheContext, APFloat(0.0)), "ifcond"); @@ -848,7 +848,7 @@ Value *ForExprAST::codegen() { if (!EndCond) return nullptr; - // Convert condition to a bool by comparing equal to 0.0. + // Convert condition to a bool by comparing non-equal to 0.0. EndCond = Builder.CreateFCmpONE( EndCond, ConstantFP::get(TheContext, APFloat(0.0)), "loopcond"); @@ -964,7 +964,8 @@ static void HandleDefinition() { if (auto FnAST = ParseDefinition()) { if (auto *FnIR = FnAST->codegen()) { fprintf(stderr, "Read function definition:"); - FnIR->dump(); + FnIR->print(errs()); + fprintf(stderr, "\n"); TheJIT->addModule(std::move(TheModule)); InitializeModuleAndPassManager(); } @@ -978,7 +979,8 @@ static void HandleExtern() { if (auto ProtoAST = ParseExtern()) { if (auto *FnIR = ProtoAST->codegen()) { fprintf(stderr, "Read extern: "); - FnIR->dump(); + FnIR->print(errs()); + fprintf(stderr, "\n"); FunctionProtos[ProtoAST->getName()] = std::move(ProtoAST); } } else { @@ -1041,14 +1043,20 @@ static void MainLoop() { // "Library" functions that can be "extern'd" from user code. //===----------------------------------------------------------------------===// +#ifdef LLVM_ON_WIN32 +#define DLLEXPORT __declspec(dllexport) +#else +#define DLLEXPORT +#endif + /// putchard - putchar that takes a double and returns 0. -extern "C" double putchard(double X) { +extern "C" DLLEXPORT double putchard(double X) { fputc((char)X, stderr); return 0; } /// printd - printf that takes a double prints it as "%f\n", returning 0. -extern "C" double printd(double X) { +extern "C" DLLEXPORT double printd(double X) { fprintf(stderr, "%f\n", X); return 0; } diff --git a/examples/Kaleidoscope/Chapter7/CMakeLists.txt b/examples/Kaleidoscope/Chapter7/CMakeLists.txt index e67d7928efe7..69e78be6a620 100644 --- a/examples/Kaleidoscope/Chapter7/CMakeLists.txt +++ b/examples/Kaleidoscope/Chapter7/CMakeLists.txt @@ -7,6 +7,7 @@ set(LLVM_LINK_COMPONENTS RuntimeDyld ScalarOpts Support + TransformUtils native ) diff --git a/examples/Kaleidoscope/Chapter7/toy.cpp b/examples/Kaleidoscope/Chapter7/toy.cpp index 58b7ce1ceda2..2f8cb682a847 100644 --- a/examples/Kaleidoscope/Chapter7/toy.cpp +++ b/examples/Kaleidoscope/Chapter7/toy.cpp @@ -639,7 +639,7 @@ static std::unique_ptr<PrototypeAST> ParsePrototype() { // Read the precedence if present. if (CurTok == tok_number) { if (NumVal < 1 || NumVal > 100) - return LogErrorP("Invalid precedecnce: must be 1..100"); + return LogErrorP("Invalid precedence: must be 1..100"); BinaryPrecedence = (unsigned)NumVal; getNextToken(); } @@ -840,7 +840,7 @@ Value *IfExprAST::codegen() { if (!CondV) return nullptr; - // Convert condition to a bool by comparing equal to 0.0. + // Convert condition to a bool by comparing non-equal to 0.0. CondV = Builder.CreateFCmpONE( CondV, ConstantFP::get(TheContext, APFloat(0.0)), "ifcond"); @@ -963,7 +963,7 @@ Value *ForExprAST::codegen() { Value *NextVar = Builder.CreateFAdd(CurVar, StepVal, "nextvar"); Builder.CreateStore(NextVar, Alloca); - // Convert condition to a bool by comparing equal to 0.0. + // Convert condition to a bool by comparing non-equal to 0.0. EndCond = Builder.CreateFCmpONE( EndCond, ConstantFP::get(TheContext, APFloat(0.0)), "loopcond"); @@ -1115,6 +1115,8 @@ static void InitializeModuleAndPassManager() { // Create a new pass manager attached to it. TheFPM = llvm::make_unique<legacy::FunctionPassManager>(TheModule.get()); + // Promote allocas to registers. + TheFPM->add(createPromoteMemoryToRegisterPass()); // Do simple "peephole" optimizations and bit-twiddling optzns. TheFPM->add(createInstructionCombiningPass()); // Reassociate expressions. @@ -1131,7 +1133,8 @@ static void HandleDefinition() { if (auto FnAST = ParseDefinition()) { if (auto *FnIR = FnAST->codegen()) { fprintf(stderr, "Read function definition:"); - FnIR->dump(); + FnIR->print(errs()); + fprintf(stderr, "\n"); TheJIT->addModule(std::move(TheModule)); InitializeModuleAndPassManager(); } @@ -1145,7 +1148,8 @@ static void HandleExtern() { if (auto ProtoAST = ParseExtern()) { if (auto *FnIR = ProtoAST->codegen()) { fprintf(stderr, "Read extern: "); - FnIR->dump(); + FnIR->print(errs()); + fprintf(stderr, "\n"); FunctionProtos[ProtoAST->getName()] = std::move(ProtoAST); } } else { @@ -1208,14 +1212,20 @@ static void MainLoop() { // "Library" functions that can be "extern'd" from user code. //===----------------------------------------------------------------------===// +#ifdef LLVM_ON_WIN32 +#define DLLEXPORT __declspec(dllexport) +#else +#define DLLEXPORT +#endif + /// putchard - putchar that takes a double and returns 0. -extern "C" double putchard(double X) { +extern "C" DLLEXPORT double putchard(double X) { fputc((char)X, stderr); return 0; } /// printd - printf that takes a double prints it as "%f\n", returning 0. -extern "C" double printd(double X) { +extern "C" DLLEXPORT double printd(double X) { fprintf(stderr, "%f\n", X); return 0; } diff --git a/examples/Kaleidoscope/Chapter8/toy.cpp b/examples/Kaleidoscope/Chapter8/toy.cpp index f7bb814da775..cdf650973b86 100644 --- a/examples/Kaleidoscope/Chapter8/toy.cpp +++ b/examples/Kaleidoscope/Chapter8/toy.cpp @@ -642,7 +642,7 @@ static std::unique_ptr<PrototypeAST> ParsePrototype() { // Read the precedence if present. if (CurTok == tok_number) { if (NumVal < 1 || NumVal > 100) - return LogErrorP("Invalid precedecnce: must be 1..100"); + return LogErrorP("Invalid precedence: must be 1..100"); BinaryPrecedence = (unsigned)NumVal; getNextToken(); } @@ -841,7 +841,7 @@ Value *IfExprAST::codegen() { if (!CondV) return nullptr; - // Convert condition to a bool by comparing equal to 0.0. + // Convert condition to a bool by comparing non-equal to 0.0. CondV = Builder.CreateFCmpONE( CondV, ConstantFP::get(TheContext, APFloat(0.0)), "ifcond"); @@ -964,7 +964,7 @@ Value *ForExprAST::codegen() { Value *NextVar = Builder.CreateFAdd(CurVar, StepVal, "nextvar"); Builder.CreateStore(NextVar, Alloca); - // Convert condition to a bool by comparing equal to 0.0. + // Convert condition to a bool by comparing non-equal to 0.0. EndCond = Builder.CreateFCmpONE( EndCond, ConstantFP::get(TheContext, APFloat(0.0)), "loopcond"); @@ -1114,7 +1114,8 @@ static void HandleDefinition() { if (auto FnAST = ParseDefinition()) { if (auto *FnIR = FnAST->codegen()) { fprintf(stderr, "Read function definition:"); - FnIR->dump(); + FnIR->print(errs()); + fprintf(stderr, "\n"); } } else { // Skip token for error recovery. @@ -1126,7 +1127,8 @@ static void HandleExtern() { if (auto ProtoAST = ParseExtern()) { if (auto *FnIR = ProtoAST->codegen()) { fprintf(stderr, "Read extern: "); - FnIR->dump(); + FnIR->print(errs()); + fprintf(stderr, "\n"); FunctionProtos[ProtoAST->getName()] = std::move(ProtoAST); } } else { @@ -1171,14 +1173,20 @@ static void MainLoop() { // "Library" functions that can be "extern'd" from user code. //===----------------------------------------------------------------------===// +#ifdef LLVM_ON_WIN32 +#define DLLEXPORT __declspec(dllexport) +#else +#define DLLEXPORT +#endif + /// putchard - putchar that takes a double and returns 0. -extern "C" double putchard(double X) { +extern "C" DLLEXPORT double putchard(double X) { fputc((char)X, stderr); return 0; } /// printd - printf that takes a double prints it as "%f\n", returning 0. -extern "C" double printd(double X) { +extern "C" DLLEXPORT double printd(double X) { fprintf(stderr, "%f\n", X); return 0; } diff --git a/examples/Kaleidoscope/Chapter9/toy.cpp b/examples/Kaleidoscope/Chapter9/toy.cpp index a482ccd73525..1b13e45ec460 100644 --- a/examples/Kaleidoscope/Chapter9/toy.cpp +++ b/examples/Kaleidoscope/Chapter9/toy.cpp @@ -756,7 +756,7 @@ static std::unique_ptr<PrototypeAST> ParsePrototype() { // Read the precedence if present. if (CurTok == tok_number) { if (NumVal < 1 || NumVal > 100) - return LogErrorP("Invalid precedecnce: must be 1..100"); + return LogErrorP("Invalid precedence: must be 1..100"); BinaryPrecedence = (unsigned)NumVal; getNextToken(); } @@ -1004,7 +1004,7 @@ Value *IfExprAST::codegen() { if (!CondV) return nullptr; - // Convert condition to a bool by comparing equal to 0.0. + // Convert condition to a bool by comparing non-equal to 0.0. CondV = Builder.CreateFCmpONE( CondV, ConstantFP::get(TheContext, APFloat(0.0)), "ifcond"); @@ -1129,7 +1129,7 @@ Value *ForExprAST::codegen() { Value *NextVar = Builder.CreateFAdd(CurVar, StepVal, "nextvar"); Builder.CreateStore(NextVar, Alloca); - // Convert condition to a bool by comparing equal to 0.0. + // Convert condition to a bool by comparing non-equal to 0.0. EndCond = Builder.CreateFCmpONE( EndCond, ConstantFP::get(TheContext, APFloat(0.0)), "loopcond"); @@ -1379,14 +1379,20 @@ static void MainLoop() { // "Library" functions that can be "extern'd" from user code. //===----------------------------------------------------------------------===// +#ifdef LLVM_ON_WIN32 +#define DLLEXPORT __declspec(dllexport) +#else +#define DLLEXPORT +#endif + /// putchard - putchar that takes a double and returns 0. -extern "C" double putchard(double X) { +extern "C" DLLEXPORT double putchard(double X) { fputc((char)X, stderr); return 0; } /// printd - printf that takes a double prints it as "%f\n", returning 0. -extern "C" double printd(double X) { +extern "C" DLLEXPORT double printd(double X) { fprintf(stderr, "%f\n", X); return 0; } @@ -1439,7 +1445,7 @@ int main() { DBuilder->finalize(); // Print out all of the generated code. - TheModule->dump(); + TheModule->print(errs(), nullptr); return 0; } diff --git a/examples/Kaleidoscope/MCJIT/cached/toy.cpp b/examples/Kaleidoscope/MCJIT/cached/toy.cpp index 2b9c3da92188..90e056d2132e 100644 --- a/examples/Kaleidoscope/MCJIT/cached/toy.cpp +++ b/examples/Kaleidoscope/MCJIT/cached/toy.cpp @@ -1395,7 +1395,8 @@ static void HandleDefinition() { if (Function *LF = F->Codegen()) { #ifndef MINIMAL_STDERR_OUTPUT fprintf(stderr, "Read function definition:"); - LF->dump(); + LF->print(errs()); + fprintf(stderr, "\n"); #endif } } else { @@ -1409,7 +1410,8 @@ static void HandleExtern() { if (Function *F = P->Codegen()) { #ifndef MINIMAL_STDERR_OUTPUT fprintf(stderr, "Read extern: "); - F->dump(); + F->print(errs()); + fprintf(stderr, "\n"); #endif } } else { @@ -1540,7 +1542,7 @@ int main(int argc, char **argv) { #ifndef MINIMAL_STDERR_OUTPUT // Print out all of the generated code. - TheHelper->dump(); + TheHelper->print(errs()); #endif return 0; diff --git a/examples/Kaleidoscope/MCJIT/complete/toy.cpp b/examples/Kaleidoscope/MCJIT/complete/toy.cpp index 40a00693e8cb..98bdad1e9907 100644 --- a/examples/Kaleidoscope/MCJIT/complete/toy.cpp +++ b/examples/Kaleidoscope/MCJIT/complete/toy.cpp @@ -1474,7 +1474,8 @@ static void HandleDefinition() { Function *LF = F->Codegen(); if (LF && VerboseOutput) { fprintf(stderr, "Read function definition:"); - LF->dump(); + LF->print(errs()); + fprintf(stderr, "\n"); } } else { // Skip token for error recovery. @@ -1487,7 +1488,8 @@ static void HandleExtern() { Function *F = P->Codegen(); if (F && VerboseOutput) { fprintf(stderr, "Read extern: "); - F->dump(); + F->print(errs()); + fprintf(stderr, "\n"); } } else { // Skip token for error recovery. diff --git a/examples/Kaleidoscope/MCJIT/initial/toy.cpp b/examples/Kaleidoscope/MCJIT/initial/toy.cpp index 1e476aef7862..9b84fd443d65 100644 --- a/examples/Kaleidoscope/MCJIT/initial/toy.cpp +++ b/examples/Kaleidoscope/MCJIT/initial/toy.cpp @@ -1252,7 +1252,8 @@ static void HandleDefinition() { if (Function *LF = F->Codegen()) { #ifndef MINIMAL_STDERR_OUTPUT fprintf(stderr, "Read function definition:"); - LF->dump(); + LF->print(errs()); + fprintf(stderr, "\n"); #endif } } else { @@ -1266,7 +1267,8 @@ static void HandleExtern() { if (Function *F = P->Codegen()) { #ifndef MINIMAL_STDERR_OUTPUT fprintf(stderr, "Read extern: "); - F->dump(); + F->print(errs()); + fprintf(stderr, "\n"); #endif } } else { @@ -1370,7 +1372,7 @@ int main() { #ifndef MINIMAL_STDERR_OUTPUT // Print out all of the generated code. - TheHelper->dump(); + TheHelper->print(errs()); #endif return 0; diff --git a/examples/Kaleidoscope/MCJIT/lazy/toy-jit.cpp b/examples/Kaleidoscope/MCJIT/lazy/toy-jit.cpp index f0c5ad5a46d7..3caeab4c745a 100644 --- a/examples/Kaleidoscope/MCJIT/lazy/toy-jit.cpp +++ b/examples/Kaleidoscope/MCJIT/lazy/toy-jit.cpp @@ -1010,7 +1010,8 @@ static void HandleDefinition() { if (Function *LF = F->Codegen()) { #ifndef MINIMAL_STDERR_OUTPUT fprintf(stderr, "Read function definition:"); - LF->dump(); + LF->print(errs()); + fprintf(stderr, "\n"); #endif } } else { @@ -1024,7 +1025,8 @@ static void HandleExtern() { if (Function *F = P->Codegen()) { #ifndef MINIMAL_STDERR_OUTPUT fprintf(stderr, "Read extern: "); - F->dump(); + F->print(errs()); + fprintf(stderr, "\n"); #endif } } else { @@ -1157,7 +1159,7 @@ int main(int argc, char **argv) { // Print out all of the generated code. TheFPM = 0; #ifndef MINIMAL_STDERR_OUTPUT - TheModule->dump(); + TheModule->print(errs(), nullptr); #endif return 0; } diff --git a/examples/Kaleidoscope/MCJIT/lazy/toy.cpp b/examples/Kaleidoscope/MCJIT/lazy/toy.cpp index 37339b60b448..3511f3ac93bf 100644 --- a/examples/Kaleidoscope/MCJIT/lazy/toy.cpp +++ b/examples/Kaleidoscope/MCJIT/lazy/toy.cpp @@ -1293,7 +1293,8 @@ static void HandleDefinition() { if (Function *LF = F->Codegen()) { #ifndef MINIMAL_STDERR_OUTPUT fprintf(stderr, "Read function definition:"); - LF->dump(); + LF->print(errs()); + fprintf(stderr, "\n"); #endif } } else { @@ -1307,7 +1308,8 @@ static void HandleExtern() { if (Function *F = P->Codegen()) { #ifndef MINIMAL_STDERR_OUTPUT fprintf(stderr, "Read extern: "); - F->dump(); + F->print(errs()); + fprintf(stderr, "\n"); #endif } } else { diff --git a/examples/Kaleidoscope/include/KaleidoscopeJIT.h b/examples/Kaleidoscope/include/KaleidoscopeJIT.h index 6130107bdd94..1dca39deba3c 100644 --- a/examples/Kaleidoscope/include/KaleidoscopeJIT.h +++ b/examples/Kaleidoscope/include/KaleidoscopeJIT.h @@ -24,7 +24,7 @@ #include "llvm/ExecutionEngine/Orc/CompileUtils.h" #include "llvm/ExecutionEngine/Orc/IRCompileLayer.h" #include "llvm/ExecutionEngine/Orc/LambdaResolver.h" -#include "llvm/ExecutionEngine/Orc/ObjectLinkingLayer.h" +#include "llvm/ExecutionEngine/Orc/RTDyldObjectLinkingLayer.h" #include "llvm/IR/DataLayout.h" #include "llvm/IR/Mangler.h" #include "llvm/Support/DynamicLibrary.h" @@ -40,7 +40,7 @@ namespace orc { class KaleidoscopeJIT { public: - typedef ObjectLinkingLayer<> ObjLayerT; + typedef RTDyldObjectLinkingLayer<> ObjLayerT; typedef IRCompileLayer<ObjLayerT> CompileLayerT; typedef CompileLayerT::ModuleSetHandleT ModuleHandleT; @@ -97,17 +97,40 @@ private: } JITSymbol findMangledSymbol(const std::string &Name) { +#ifdef LLVM_ON_WIN32 + // The symbol lookup of ObjectLinkingLayer uses the SymbolRef::SF_Exported + // flag to decide whether a symbol will be visible or not, when we call + // IRCompileLayer::findSymbolIn with ExportedSymbolsOnly set to true. + // + // But for Windows COFF objects, this flag is currently never set. + // For a potential solution see: https://reviews.llvm.org/rL258665 + // For now, we allow non-exported symbols on Windows as a workaround. + const bool ExportedSymbolsOnly = false; +#else + const bool ExportedSymbolsOnly = true; +#endif + // Search modules in reverse order: from last added to first added. // This is the opposite of the usual search order for dlsym, but makes more // sense in a REPL where we want to bind to the newest available definition. for (auto H : make_range(ModuleHandles.rbegin(), ModuleHandles.rend())) - if (auto Sym = CompileLayer.findSymbolIn(H, Name, true)) + if (auto Sym = CompileLayer.findSymbolIn(H, Name, ExportedSymbolsOnly)) return Sym; // If we can't find the symbol in the JIT, try looking in the host process. if (auto SymAddr = RTDyldMemoryManager::getSymbolAddressInProcess(Name)) return JITSymbol(SymAddr, JITSymbolFlags::Exported); +#ifdef LLVM_ON_WIN32 + // For Windows retry without "_" at begining, as RTDyldMemoryManager uses + // GetProcAddress and standard libraries like msvcrt.dll use names + // with and without "_" (for example "_itoa" but "sin"). + if (Name.length() > 2 && Name[0] == '_') + if (auto SymAddr = + RTDyldMemoryManager::getSymbolAddressInProcess(Name.substr(1))) + return JITSymbol(SymAddr, JITSymbolFlags::Exported); +#endif + return nullptr; } diff --git a/examples/ParallelJIT/CMakeLists.txt b/examples/ParallelJIT/CMakeLists.txt index e85b470f5036..deeee072b33c 100644 --- a/examples/ParallelJIT/CMakeLists.txt +++ b/examples/ParallelJIT/CMakeLists.txt @@ -11,4 +11,4 @@ add_llvm_example(ParallelJIT ParallelJIT.cpp ) -target_link_libraries(ParallelJIT ${PTHREAD_LIB}) +target_link_libraries(ParallelJIT ${LLVM_PTHREAD_LIB}) diff --git a/examples/ParallelJIT/ParallelJIT.cpp b/examples/ParallelJIT/ParallelJIT.cpp index 6fb8bd61982b..f1932d2471cb 100644 --- a/examples/ParallelJIT/ParallelJIT.cpp +++ b/examples/ParallelJIT/ParallelJIT.cpp @@ -54,8 +54,7 @@ static Function* createAdd1(Module *M) { Function *Add1F = cast<Function>(M->getOrInsertFunction("add1", Type::getInt32Ty(M->getContext()), - Type::getInt32Ty(M->getContext()), - nullptr)); + Type::getInt32Ty(M->getContext()))); // Add a basic block to the function. As before, it automatically inserts // because of the last argument. @@ -85,8 +84,7 @@ static Function *CreateFibFunction(Module *M) { Function *FibF = cast<Function>(M->getOrInsertFunction("fib", Type::getInt32Ty(M->getContext()), - Type::getInt32Ty(M->getContext()), - nullptr)); + Type::getInt32Ty(M->getContext()))); // Add a basic block to the function. BasicBlock *BB = BasicBlock::Create(M->getContext(), "EntryBlock", FibF); |