IR(intermediate representation), optimize the IR, translate the IR back to Ruby Code. Optimizer parse Ruby ఔࣜᛰɼ༌ग़ IR (தհᛰʣɼ࠷ՂԽ IRɼ IR ᩄճ Ruby ఔࣜ ᛰɻ • Use a Ruby IR based on MLIR. ༻جԙ MLIR త Ruby IR ɻ • https://github.com/johnlinvc/mlir-dialect-ruby ዎኄ၏౸తʁ How ?
VM) has a instruction set (ISeq). CRuby 㐘ٖػ YARV ༗ఆٛҰݸࢦྩू ISEQɻ • YARV is a stack machine. YARV ༻ Stack ိ၏ܭࢉɻ • Dynamic typed, with some optimization like tagged pointer. ಈଶܕผɼෆա༗Ұࠣ࠷ՂԽ૾ੋ tagged pointerɻ Ruby ݱࡏత IR Ruby's current IR Ruby ISEQ YARV VM
to implement and port across di ff erent CPU Arch. But harder to optimize and runs slower. [1] YARV ੋ stack machineɻ༰қመ࡞Ҡ২ɼୠੋൺֱຫֱ࠷ՂԽɻ • YARV is dynamic typed, making Monomorphization harder. ಈଶܕผൺֱ Monomorphization • YARV is made for CPU, we don't want to miss the AI train on GPU. YARV ੋҝ CPU ઃܭతɼᔒ㭎๏䋯ࡏ GPU ্ɻ Why do we need a new IR for Ruby?
to optimize ༻ଘثိᩋᏈೳߋɼߋ༰қ࠷ՂԽ • Static typed to have Monomorphization optimization ᯩଶܕผɼߋ༰қ၏Monomorphization • Support Heterogeneous computing ࢧԉҟߏܭࢉ զ၇ՄҎ༗Ұݸߋత Ruby IR 䆩ʁ Can we have a better IR for Ruby?
of LLVM project. LLVM తҰ෦ • A extensible common IR syntax for di ff erent compiler backend & frontend. ՄᎷॆత௨༻ IR ޠ๏ɼՄ༻ԙଟछฤᩄثલޙɻ • Provide tool & framework for developing your own IR. ఏڙ۩ᐽՍိᩋ㟬։ᚙࣗݾత IR MLIR
fi c IR for optimization & diagnostic ᩋฤᩄثՄҎೳⴺ༗ߋషۙޠݴత IR ိ၏࠷ՂԽੳɻ • Support compile to non-CPU based targets like GPU, FPGA. ՄҎฤᩄ౸ CPU Ҏ֎తฏ૾ੋ GPU, FPGAɻ • Act as a cross language compatibility platform. ၏ҝލޠݴత૬༰ੑฏɻ MLIR తඪ Goals of MLIR
Parser for custom IR ٬੍ IR త Parser • Framework to translate between source language & MLIR ࡏိݯޠݴ MLIR ೭ؒᩄతᐽՍɻ • Framework to optimize IR ࠷ՂԽ IR తᐽՍɻ • Framework to convert between IRs. ࡏ IR ؒతᐽՍɻ MLIR ޭೳ MLIR features
fi ne their own operations. ࢦྩڣ၏ Operationɼ༻ऀՄҎఆٛࣗݾత Operation • Static single assignment form(SSA) , make optimization easier. ᄸҰොᆴɼߋ༰қ࠷ՂԽɻ • Strong static typed. ᯩଶڧܕผ • Block based syntax instead of Φ(PHI) node. ༻ block ိऔ Φ(PHI) nodeɻ MLIR IR ޠ๏ಛ৭ MLIR IR syntax features
with it's own tools is called a dialect in MLIR. Ұ IR ఆٛɼଞࣗݾత۩बڣ၏Ұݸ MLIR ํݴɻ • Operation(IR) de fi nitions. Operation ఆٛ • A translator that convert to/from MLIR to target language. ࡏඪޠݴ MLIR ೭ؒతᩄ۩ɻ • Optimization passes to optimize the IR. IR త࠷ՂԽྲྀఔɻ • Converter to convert from/to other MLIR dialects. ࡏ MLIR ํݴؒత۩ɻ ॄኄੋ MLIR ํݴ? What is a MLIR dialect?
de fi nition in MLIR MLIR Ruby Operation ఆٛ • Ruby Optimization Passes. Ruby ࠷ՂԽྲྀఔ • Translate from Ruby code to IR. ኺ Ruby ᩄ IRɻ • Translate from IR back to Ruby code. ኺ IR ᩄճ Ruby MLIR Ruby dialect
for Ruby. Prism ੋ৽తލመ࡞ Ruby Parserɻ • Has richer info than the 'Parser' gem. ൺى 'Parser' gem ༗ߋଟతࢿ㘤ɻ • Provides Ruby,C & JS API. ఏڙ Ruby, C & JS API ༻ Prism ိ Parse Ruby Parse Ruby with Prism
= "visit_#{type.split("_")[..-2].join("_")}" raise "not implemented: #{method_name}" unless respond_to? (method_name) send(method_name, node) end end ኺ AST ༌ग़ IR Emit IR from AST
with_new_ssa_var do |ssa_var, attr_dict| ret_type = "!ruby.int" @stmts << " #{ssa_var} = ruby.constant_int \"#{value}\" #{attr_dict} : #{ret_type}" ret_type end end end ኺ AST ༌ग़ IR Emit IR from AST
the IR. ༻ MLIR Pass Սߏိ࠷ՂԽ IR • Rewrite Patterns: Pattern match an Operation DAG, then perform edit/replace/delete on the DAG. ሣ Operation DAG ࡞ pattern matchɼ࠶ሣDAG ၏मվ/औ/႟আ • Fold: Replace an Operation with another Operation or a Value. औಛఆత Operation ҃ੋ Valueɻ ࠷ՂԽԋࢉ๏ Optimization passes
• Convert the `call` op with an `add` op when method name is "+". and has 2 arguments. Ռ `call` op త method ੋ "+" ࣕ༗ၷݸჩᏐతɼ `add` opɻ Specialization pass
so the return type must keep the same. ܕผࡨޡ Type error after specialization %0 = ruby.constant_int "40" {} : !ruby.int %1 = ruby.constant_int "2" {} : !ruby.int %2 = ruby.add %0, %1 : (!ruby.int, !ruby.int) -> !ruby.int %3 = ruby.local_variable_write "coscup"= %2 {rb_stmt = true} : !ruby.opaque_object error: use of value '%2' expects different type than prior uses: '!ruby.opaque_object' vs '!ruby.int' coscup = 40 + 2
following passes. ᩋ IR ࡏޙ᠃త Pass தߋ࠷ՂԽɻ • Run the fold method automatically. ။ࣗಈ䋯 foldɻ • Can add additional pass as part of canonicalize. ՄҎ⃧Ճֹ֎త pass ਐنൣԽ pass نൣԽ pass Canonicalization pass
ᩄ۩ɻ • Translate an op when visited by the visitor. ᩄඃ visitor ๚౸త op. • Use a rb_stmt attribute to identify the top level statements. ༻ rb_stmt attribute ိผ࠷্త statementsɻ ༌ग़ Ruby ఔࣜᛰ Emit Ruby code
optimize & validate programs. தհᛰ(IR) ੋฤᩄث༻ိ࠷ՂԽᱛᨽఔࣜత۩ • It's time to have a new IR for Ruby. Ruby ࠩෆଟՄҎ༗Ұݸ৽ IR ྃ • MLIR is a state of art IR tool. MLIR ੋҰݸઌਐత IR ۩ • MLIR Ruby can optimize Ruby. MLIR Ruby ՄҎ࠷ՂԽ Ruby ݁ Takeaways