Share
Embed
LLVM Tutorial - わいわいswiftc
LLVM Tutorialについて発表しました.
公開しようと思っている翻訳メモはこちら.
https://github.com/sonsongithub/llvm-tutorial
sonson
December 17, 2018
Transcript
33 slides
-
Getting start with llvm Θ͍Θ͍swiftc Yuichi Yoshida Senior researcher, DENSO
IT Laboratory, Inc. #conference @sonson_twit © 2018 DENSO IT Laboratory, Inc., All rights reserved. Redistribution or public display not permitted without written permission from DENSO IT Laboratory, Inc. LLVM Tutorial -
ͳͥɼLLVMʁ • ੈͷதͷྲྀΕ • ϜʔΞͷ๏ଇͷݶք • CPUͷྔతਐԽɼݶքʹ͍͍ۙͮͯΔʢཧతʣ • ճආ͢Δʹʁ •
ฒྻԽ • CPUສೳ͗͢ΔˠGPU, FPGA, ASIC • ͲΕ։ൃ͕ࠓ·Ͱͷͱҧ͍͗ͯ͢ࠔۃ·Δ ιϑτΣΞͷ։ൃํ๏͕৭ʑࢼ͞Ε͍ͯΔ -
͜͜ͷ • Halide • LLVMΛ͍ɼC/C++ʹฒྻίʔυΛ͔͚Δ • fixstars͕͜ΕͰHalide FPGAͳͲΛϦϦʔε • temp.split(y,
y, yi, 8).parallel(y).vectorize(x, 4); • Tensorflow for Swift • SwiftͰػցֶशͷߦྻܭࢉฒྻܭࢉΛΓ͍ͨ • Julia • ֶతͳܭࢉΛඳ͖ͨ͘͢͠ݴޠ • LLVMΛ༻ ཁॴɼཁॴͰLLVM͕ग़ͯ͘Δ -
LLVM Tutorialͱʁ • Tutorials about using LLVM. Includes a tutorial
about making a custom language with LLVM. -
LLVM Tutorial • LLVMΛͬͯΦϨΦϨݴޠͷίϯύΠϥΛ࡞Δ • C++ͰllvmΛϥΠϒϥϦͱͯ͠ɼίϯύΠϥΛ։ൃ • ಡΉͷʹඞཁͳεΩϧ • C++
• ӳޠ • ͳΜ͔brokenͩͬͨΓɼ͚ͩͨ͘ײ͡ -
ίϯύΠϧڥ • ίϚϯυͷྫ • llvm-configͳͲXcodeʹؚ·Εͳ͍πʔϧ͕͋Δ • macOSͷ߹ • brew install
llvmͰΠϯετʔϧ͓ͯ͘͠ • Mojave/usr/include͕ͳ͍ͷͰखಈͰɾɾɾɾ • ࢲϋϚͬͨɾɾɾɾɾ • ”Xcode 10 Release Notes” • ڥมͷΓସ͑ΛΕͣʹ clang++ --std=c++14 ./toy.cpp `llvm-config --cxxflags --ldflags --libs --libfiles —system-libs` -
։ൃ͢ΔݴޠKaleidoscope • ඪͱ༷ • doubleܕͷΈ • ͕ؔ͑Δ • Standard libraryͷؔΛ͑Δ
• if/elseจ • forจ • Ϣʔβఆٛԋࢉࢠ • REPL/JIT -
αϯϓϧ # Compute the x'th fibonacci number. def fib(x) if
x < 3 then 1 else fib(x-1)+fib(x-2) # This expression will compute the 40th number. fib(40) # load standard library extern sin(arg); extern cos(arg); extern atan2(arg1, arg2); # call atan2 atan2(sin(.4), cos(42)) -
The error recovery in our parser will not be the
best and is not particular user-friendly, but it will be enough for our tutorial. Note that instead of adding virtual methods to the ExprAST class hierarchy, it could also make sense to use a visitor pattern or some other way to model this. Again, this tutorial won’t dwell on good software engineering practices: for our purposes, adding a virtual method is simplest. -
࠷ॳͷҰาʙޠኮղੳ(Lexer) • ·ͣιʔείʔυΛݻ·Γʹׂ͍ͯ͘͠ extern cos(0.1); ֎෦એݴ tok_extern Կ͔ͷࣝผࢠ tok_identifier τʔΫϯ
tok_number ΧοίͱηϛίϩϯޠኮʹͨΒͳ͍ͷͰɼ ͱΓ͋͑ͣඈ͢ -
ߏจͷϊʔυΫϥε /// ExprAST - Base class for all expression nodes.
class ExprAST { public: virtual ~ExprAST() {} }; /// NumberExprAST - Expression class for numeric literals like "1.0". class NumberExprAST : public ExprAST { double Val; public: NumberExprAST(double Val) : Val(Val) {} }; Λอଘ͢Δϊʔυ -
มͱೋ߲ԋࢉࢠ /// VariableExprAST - มΛอଘ͢ΔͨΊͷཁૉ class VariableExprAST : public ExprAST
{ std::string Name; public: VariableExprAST(const std::string &Name) : Name(Name) {} }; /// BinaryExprAST - +ͱ͔ͷԋࢉࢠΛอଘ͢ΔͨΊͷཁૉʁ class BinaryExprAST : public ExprAST { char Op; std::unique_ptr<ExprAST> LHS, RHS; public: BinaryExprAST(char op, std::unique_ptr<ExprAST> LHS, std::unique_ptr<ExprAST> RHS) : Op(op), LHS(std::move(LHS)), RHS(std::move(RHS)) {} }; +ͱ͔-Έ͍ͨͳԋࢉࢠͱ͔ -
ؔݺͼग़͠ͷͨΊͷϊʔυ /// CallExprAST - ؔݺͼग़͠ͱҾΛอଘ͢ΔͨΊͷ class CallExprAST : public ExprAST
{ std::string Callee; std::vector<std::unique_ptr<ExprAST>> Args; public: CallExprAST(const std::string &Callee, std::vector<std::unique_ptr<ExprAST>> Args) : Callee(Callee), Args(std::move(Args)) {} }; ؔݺͼग़͠ͷϊʔυɽ͔͜͜ΒؔϙΠϯλΛݺͿʁ -
ؔͷϓϩτλΠϓએݴ /// PrototypeAST - This class represents the "prototype" for
a function, /// which captures its name, and its argument names (thus implicitly the number /// of arguments the function takes). class PrototypeAST { std::string Name; std::vector<std::string> Args; 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. class FunctionAST { std::unique_ptr<PrototypeAST> Proto; std::unique_ptr<ExprAST> Body; public: FunctionAST(std::unique_ptr<PrototypeAST> Proto, std::unique_ptr<ExprAST> Body) : Proto(std::move(Proto)), Body(std::move(Body)) {} }; ؔͷ࣮ɽ ϓϩτλΠϓͷϙΠϯλͱؔͷ࣮ຊମͷϙΠϯλ -
நߏจͷϊʔυ͕ἧ͏ • ExprAST • NumberExprAST • VariableExprAST • BinaryExprAST •
CallExprAST • PrototypeAST • FunctionAST -
ιʔε͔ΒؤுͬͯΛ࡞͍ͬͯ͘ /// numberexpr ::= number static std::unique_ptr<ExprAST> ParseNumberExpr() { auto
Result = llvm::make_unique<NumberExprAST>(NumVal); getNextToken(); // consume the number return std::move(Result); } NumVal͕άϩʔόϧมͰɼgetNextToken()Ͱ άϩʔόϧมʹ࣍ͷτʔΫϯΛಡΈࠐΉ ͭ·ΓɼԚ͍ -
ιʔε͔ΒؤுͬͯΛ࡞͍ͬͯ͘ auto LHS = llvm::make_unique<VariableExprAST>("x"); auto RHS = llvm::make_unique<VariableExprAST>("y"); auto
Result = std::make_unique<BinaryExprAST>('+', std::move(LHS), std::move(RHS)); -
codegenͷͨΊͷΦϒδΣΫτ • static LLVMContext TheContext; • γϯάϧτϯʁσʔλܕ͕ͯ͢อଘ͢ΔΦϒδΣ Ϋτɽ • static
IRBuilder<> Builder(TheContext); • llvmͷIRΛੜ͢ΔͨΊͷΦϒδΣΫτɽ • static std::unique_ptr<Module> TheModule; • ͍ΘΏΔϞδϡʔϧɽ͜͜ʹάϩʔόϧม͕อଘ ͞ΕΔɽ͜ͷΦϒΤδΣΫτʹͯ͢ͷσʔλ͕อ ଘ͞Εɼ͋ΒΏΔͷ͜͜ͷϙΠϯλ • static std::map<std::string, Value *> NamedValues; • ม໊ʁͷʹ͏ϚοϓͬΆ͍ɽ -
LLVM IRΛੜ͢Δ • ͖͞΄ͲͷϊʔυʹcodegenΛੜ͢ /// ExprAST - Base class for
all expression nodes. class ExprAST { public: virtual ~ExprAST() {} virtual Value *codegen() = 0; }; /// NumberExprAST - Expression class for numeric literals like "1.0". class NumberExprAST : public ExprAST { double Val; public: NumberExprAST(double Val) : Val(Val) {} virtual Value *codegen(); }; ValueɾɾɾStatic Single Assignment RegisterΛࣔ͢ม -
ೋ߲ԋࢉࢠͷ߹ Value *BinaryExprAST::codegen() { Value *L = LHS->codegen(); Value *R
= RHS->codegen(); if (!L || !R) return nullptr; switch (Op) { case '+': return Builder.CreateFAdd(L, R, "addtmp"); case '-': return Builder.CreateFSub(L, R, "subtmp"); case '*': return Builder.CreateFMul(L, R, "multmp"); case '<': L = Builder.CreateFCmpULT(L, R, "cmptmp"); // Convert bool 0/1 to double 0.0 or 1.0 return Builder.CreateUIToFP(L, Type::getDoubleTy(TheContext), "booltmp"); default: return LogErrorV("invalid binary operator"); } }