# Optimize Solidity/Yul/Ewasm bytecode via LLVM framework by Hung Ying Tai

- Channel: [Devcon](https://streameth.org/devcon)
- Date: 2020-10-09
- Duration: 32:58
- Topics: blockchain, smart contracts, distributed ledger, ethereum, ethereum solidity, solidity, solidity summit, language design
- Watch: https://streameth.org/watch/yt-cKbfYYM8qQc
- YouTube: https://www.youtube.com/watch?v=cKbfYYM8qQc

## Description

"SOLL 0.0.6 release(eta. 2020-03-06) will support Yul constructor and deployer. SOLL can compile both Solidity(partial of the grammar) and Yul(most of the major grammar) to LLVM IR. When the LLVM IR is generated, SOLL will apply LLVM optimization passes to reduce the code size, improve the performance, and eliminate redundant codes. And then SOLL transforms these optimized LLVM IR into Ewasm bytecodes.

In this topic, I want to share about how we optimize via LLVM framework and a new profile-guided optimization mechanism to analyze wasm runtime execution and adjust the optimization strategies back to the optimization phase to gain more improvement."

## Transcript

I'm hoping die or you can call me I die just like this ID whole shirt and today I want to share about how my team the I saw him to do several active mine station on 3030 on you all and he was on the other album can walk so here she is our agenda we have a full session to talk about first is row map and how I saw our warps and then we will just mention just a little you will automatically be a master then we want to excuse a very experimental a new mechanics and do some static analysis and feedback diversity organization okay so let's talk about our room at first actually this whole Partridge star phone since that's your April Fool's and actually has do lots of staff to create new languages tensioning for 32-bit code if you remember we have to talk about that and DEFCON 5 and after la cool we want to just more folks on some Aquaman's if you research because we believe that smoke on travel abroad sham will become more and more popular so if engineered who is right in the smoker jerk and now we want to help a land that they don't need to really care about the detail Kotian above almost all the closed eyes went through leverage laws of humanity of steel and to help them to generate more a small size bike home so here is our our stop point and and after we decide one reduce the IOPS our infrastructure we want to choose that's one for em and one back end which is the foreigners which use Sulli language because I believe he's the most impoverished 100 language renew all so we choose sorry 13 and then for the back end I believe there's the solitude compiler has two very great work to generate the indian by poll very efficiently so we just want to create a new over choose a new pocket back end so that's why we chose the solidity and he wasn't for first and then we go go to the audio one the really the very first release that is we want to create a very small demo which is just i believe everyone has used this library called safe mask and we just do very little primer just you can see here if else or revert require and buried immediate integers of code but before the they've come five we want to just join the community and we want to occur to the cellular team to ask for more conversation about you boston or or something like that so in least to Bertram we focused on how to compile or Yeltsin twenties mantra and we want to show our soil has the ability can we can just support a constructor and we can deploy the it was a Michael to the ISA really wasn't a snack and every we we just finished that and and we have a very good conversation between us and the team and after this event I believe this is very key then because after our discussion we found less that's like I remember next Arace helpers there is another project called stolen and they also do as interested a sense in just compile the subtlety into emotion or into the subject so we just change our goal for support in nursery and just in stealth sorry we want to support a new afar and instead of study that himself Michael mentioned yesterday tree oil now can Cass is 80% of a test suite fun wo and I want to the as I want to accept that of Iwo by coal which is cherry pie la Sol C compiler and we apply the airwave green accommodation options include a whole whole culture and whole by cogeneration and also we also support even Elvia knows our EVM burger so now the essence of very limited Street a fun n and I seen almost you all from an and we have to pack and one is it wasn't Becca and another is the eevn LVM that gang okay so here is something we doesn't support now just like balance or self and something I less and we will just keep going to finish this an implement did instructions just a few months later okay so was our future remember future ago that is we wonderfully suppose you of an and yesterday that will fundamentally is a very good extension code that you will pass and I believe if there are some features very interesting so we will also add a suppo of Lego glass after we finished your phone em and we found final that they are some very interesting since less actually traditional optimization applies on the II wasn't may not be the best choice for the follow real wall because when we deploy a small country on an historian we will we will very care about cold ice rather than the efficiency because the co size will have that of a story crystal I believe this is more expensive than than a native computer ok so this second goal we want to achieve and the third one is actually we have several at mine there you pass in our atomizer and we will discuss that later okay so there's our roadmap for the past from the current and the future okay now I want to talk about how as a walk whoops so the first thing is that we accept the contract which is right right in solidity language and when you get say that he solidity control you can just bypass the into the SOL sea and with my mind's eye are in my mind's eye hot master and then you can get atomo atomo you of michael and at this stage we establish you will control into our compiler and if you want to see how you want to tell the the point the warrior class the runtime you just bypass this your country and we worked if you see that wasa but if you want to know why is the detail of the one hi Michael but you have to remove the outside powerful a new object because the also how is the constructor and and some something that is just a record also so you have to remove you all objects are your arbitrary first and then just passed the remain hard to our compiled and you can get a run wasn't so voice the people seen is that as compared to the first thing is when we get the you O Smaug shred the we will pass it and check the semantics and create our own ast in in our camera pipeline and the second thing is we will come soon this st and with our co-chair module and then we work at lvl match right here so at this stage now we get lots of LV and I are now there is no you are anymore so it's Y of the your statement will just maybe do several I of okay now who gets the area mantra here and the next step is to use our back-end our peak and we'll just apply several lb optimism test and we do Lhasa was death like like the pits swap because the the eevn is big endian integer type and the webassembly is the little endian it is attack so we need to do lots of converging scenes and then after we finished loss of walks now the last step is we will use our documentation and incurred to generate the it was a bike hope or the Indian of Enda hello vir yeah let's our the whole internal process of I saw works okay so this our this my second session to just describe how the soil work and the circle we want to just mention some very different scenes you are atomizer and LV atomizer and the first thing is I was in the rows between these two is is very different because you are tea master every is transformed for you or to you so you can have very high level information here and so for we will have said Colette you automatic and do lots of oppressive elimination to do the inline scenes to remove the function code but when we goes to the Oviatt atomizer there is a big limitation for us because that's we we just convert all the UL statement into the vir statement so we were lost as of in piloting prevention first and the second thing is when we want to apply to generate it was effect and we will meet you at lowering integer like because the EPA has 250 say speaker in the integer but the we're passing only suppose this forbids initial so we have to that of a commercial and also just piss walk I mentioned before so after we add the several scenes in our vehicle you will be more hard to do some analysis and to reduce with some very heuristic rule so I will give you more details in the same code okay so the first example is we code the a very common compiler optimization there is loop roading or armory which means here you have a country with an array and we just write followed to feel every demand with index and if you compare this smoke hole to the native comp computer for example usually write this person in this equation in steepest pass and just comparing it to the negative you will find as layer no more for Luke because all the sins will be converted into a residual because as I assign you to assign your truth a ready room so it will just be 10 like off of this statement but in the you will optimizer you will find that actually it just keeps structure so you will have a fall off here and each iteration will just update the storage marry or once so I see it's very great but when we apply a voice sample we apply they'll be optimized so with the Audrey frack and we will find that all of the roof is be in Amman that because in this stage LVN and the wasn't back and believe that we just replaced the roof with loss of consensus concerns store is more efficiency but actually in the in the Suren I don't believe luckier because if this loop become more larger if he is not from 0 to 10 is from zero to a hundred or a thousand then we will find a closed eyes will hello then then our respective so when we do the loop conne rody will find the final wasn't for size will be ' but if we disable loop loading a Ruben Rodin's we work and 2.5 k pass here so this is the first very different scene that but we have to be more careful when we apply any LVN optimization because lots of our key management is designed for that his computer not assigned for some very special cats okay legacy is in line I believe less a very good example that that you will atomizer can do much better than then I'll be another mine sir though you can find this is the DEF CON - Sam Botta you'll and is a store here and we just call ERISA and in the result we will call loss of reload so after allah you will admire so we I means one slide here oh okay I miss one slide here and I will I will give you a web moment check if I can get you back now okay and let me just pass a it was on pest sorry yes but copy and we'll move on to me sure okay okay so we can find if if we just sorry I just go back so after you are at matter then we will all just convert these lace you go to a very short one here you can find all of the budget code is all the function code is PDV right so less very very good since that as you will automatic - but when we go back to the - oh okay Tula took a I'll be at my 403 then we will figure out that the ERISA is be happy in light but the remote has not been invited because in our vehicle make sure we figure that the function body and the basic block is is larger than in Ireland so in a noise ratio so there will be so there will be only the rasam has been 9 and the other one okay so this is very different between the LVN atomizer and and you automatically cuz because our VR my sword in my love has is almost vertical and we have to to a lot of the research and to figure out which virtual is enough for us and cost model so less services we will have to do more research here and sir one is the storage SSC Sam ho so I just create a new integer storage and and we will do a ten and ten tries okay so when the contrasts become higher to the UO and after the UART master and you can find that there is the sensing like the first body and II just update a storage barrel twice and that's the same thing with the LLVM at monster like you can find that here's a add first to add ten into this is a temporary ball means the original story ready and just store it over here is don't worry about that and we called a soldier so just to a stall activated back to the storage but we believe if we can do a very trivial which is this tool I can be combined into one line which is I plus equal and we don't need to do just like a two line of ADD and to line of the storage updates we we can just simple while statement into one minus is several since this we can do more about these case okay so here is three here are three examples we we want to mention that both of these to atomizer has still has lots of research and and optimization technology we can do more so we want to introduce the final part which is we do research to an ERISA cost model so what's the motivation this is motivation is like I believe the gas cost is just like the energy consumption if you run if you run okay if we run a very very bad program on your cell phone then your cell phone well eat more poorer and if you write a very bad smart control and you will its guess as is versus recall so we apply a very I will apply the law same analysis and intimidation just from some paper to deal with energy consumption so we create a project called the GPS cat which means a general-purpose stayed here cost analysis okay okay you just very long name and in this tool we have to face to ocean the first one is we we don't analyze this evil territory we analyze every ayah because we can do lots of the debug info we can insert us of the papadum to help us to to evaluate the arrow via our cost equals to lots of web assembly instruction cost so we will start on our first so when we can just calculate it into a cost function and then we will use at once over to resolve this function to a figure out his score okay so here is our our total analysis diagram so the first Osteen's we will define some assembly cost model some slack if we add a number we will attack let's just say a hundred or 200 some that and Oh vir biko will be a no hate with debug info with floor debugger location because we want to know that every lvii will be generated to what kind of webassembly enforcing so the whole idea is likely so after we do that of the location we can give you know we can get the vir with blood cause information then when we get to this information we need to have to to apply to framework from the outside of four twenties we need to destroy the causal relationship so we use the LVN to people and ii since is after we have this space causal relationship we need we have to apply the upon solving advanced over to to calculate the scenes so here is we use to Eastern all harder to help us to do the piece okay so after we go through lots of things now we can get if we apply some optimization on the LVN ir then we can get a new score and a new formula and we can calculate if we apply this stake and asked work hours well you will cost will be increased or decreased then we will we can just adjust the circle or other dress atomization options to to help us to create more most suitable at the management phase for the he wasn't host model okay so i see i have time so the following slide is just mention how we know cats that debug info for I just like we will say each LVN I are well met in Toluca July and lowering it into the Assembly of the Weber assembly then we can just get a mapping between the load lvi our instruction will be do nothing but the ad will be number two three insulting all the weather center then with this model we can just say we have a table to the of the Magnum reason ship and we have lacrosse table so we will know each vir is made into what kind of the cost and we can pick you a final basic black hose and we will know the hope open the whole course of of this mantra when when he just is cute okay I see that's my whole whole talk about our some-something atomization so if I you are interesting you can cut all these things to our soil repository to use them or you can just Michael show the video yesterday and if you are interested in actually we have because we have me to mentor PI co efficients and the backhoe and a panel performance so we creates ourself or wasn't but a machine to to do things and if you're interesting about Jabez cat here is our story and all is them host you can find in this repository here ok ok let's all thank you so much great talk thank you so because we have almost 1 minute delay in the live stream we will give people the chance to ask questions now either if you're watching the live stream please make sure to comment in the guitar chat or if you have a question here in the room feel free to use the raise your hand feature that I can see you have a question yeah Chris go ahead yeah thanks for the talk I'm wondering so you mentioned that you need to extract the runtime code from the you object in order to deploy it is there a way you could support the deploy routine that is written in you or what is the reason for that oh because Jared hit you or country is just like a what a co and Depot or a constructor partly they are two apart right i I seen as in their output two cars and so if because if you if we get the gift Sol with the one Michael and a controller hold a different code we will just compile the one I call just directly into the what was the Michael so you have no choice you have no chance to see the area inside of this one huh so if you want to do some research on just remove the outside cause that's not required for running it it's just if you want to see further details on this year's you have to of course you have to give the run heckle and control flow and well we need yes yeah thanks okay another question here muli oh you still muted can you hear me now yes thanks thank you very very very nice talk can you elaborate a little bit about how much you save in particular how much gas saving are you getting sort of a you oh I will say they are no no any any benchmark or we doesn't we doesn't have you that test reduced number here because in our say least a reverie experimental easily will be is where this Raymundo stage so in this moment I believe they are they also don't have very very strong or very complicated very completes of the web assembly cost model so we just we just assume we just says that of number layer so so we don't have no real number sorry for that yes here muted again oh sorry so on the conceptual level if you implemented this directly on the you'll how much more difficult would it be what are you getting actually from the LLVM as opposed to just running it on the you'll there is my question clear actually I believe this there's different scenes because why we choose that I will be enfermo because it just like the GCC that's there are several people and do of automation research and creates lots of administrability later so for us it will be very difficult because we have to choose which strategy is is which history is suitable for you all and then the second thing we just told the algorithm from the FBI our into my source or items so if we can do that of a researcher and these parts years would impose laws of management 1lv and in true that you were a team experts I believe we should just just steal or we just used the framework and just add lots of analysis and management structure next layer because you will have to lots of things to do because you have to you have to analyze there and create lots of information so with loss information we can just do more aggressive atomization because you will have a smoother too much time to to rebuild all the all those things again myself
