- 
                Notifications
    
You must be signed in to change notification settings  - Fork 86
 
Add Optional JIT compilation #91
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
          
     Draft
      
      
            richardpringle
  wants to merge
  10
  commits into
  ithacaxyz:main
  
    
      
        
          
  
    
      Choose a base branch
      
     
    
      
        
      
      
        
          
          
        
        
          
            
              
              
              
  
           
        
        
          
            
              
              
           
        
       
     
  
        
          
            
          
            
          
        
       
    
      
from
richardpringle:wip
  
      
      
   
  
    
  
  
  
 
  
      
    base: main
Could not load branches
            
              
  
    Branch not found: {{ refName }}
  
            
                
      Loading
              
            Could not load tags
            
            
              Nothing to show
            
              
  
            
                
      Loading
              
            Are you sure you want to change the base?
            Some commits from the old base branch may be removed from the timeline,
            and old review comments may become outdated.
          
          
  
     Draft
                    Changes from all commits
      Commits
    
    
            Show all changes
          
          
            10 commits
          
        
        Select commit
          Hold shift + click to select a range
      
      6b54ccd
              
                WIP
              
              
                 3609a7f
              
                WIP
              
              
                richardpringle 3824f00
              
                WIP
              
              
                richardpringle 777066c
              
                WIP
              
              
                richardpringle 8600d16
              
                Don't mess around with the interpreter's memory
              
              
                richardpringle 6ecbdf0
              
                Create a new compiler on every message received
              
              
                richardpringle 22e9290
              
                Send spec-id with each compilation
              
              
                richardpringle dce6241
              
                Move the compiler to the Odyssey config
              
              
                richardpringle 14427e7
              
                Move the compiling logic back to the compiler module
              
              
                richardpringle 06c3a11
              
                Add comment
              
              
                richardpringle File filter
Filter by extension
Conversations
          Failed to load comments.   
        
        
          
      Loading
        
  Jump to
        
          Jump to file
        
      
      
          Failed to load files.   
        
        
          
      Loading
        
  Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
      
      Oops, something went wrong.
      
    
  
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| 
          
            
          
           | 
    @@ -208,3 +208,6 @@ futures = "0.3" | |
| 
     | 
||
| # misc-testing | ||
| rstest = "0.18.2" | ||
| 
     | 
||
| [patch.crates-io] | ||
| revmc = { path = "../revmc/crates/revmc" } | ||
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| #![allow(missing_docs)] | ||
| 
     | 
||
| fn main() { | ||
| revmc_build::emit(); | ||
| } | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,162 @@ | ||
| /*! The compiler module is responsible for compiling EVM bytecode to machine code using LLVM. */ | ||
| 
     | 
||
| use alloy_primitives::{hex, Bytes, B256}; | ||
| use core::panic; | ||
| use reth_revm::{ | ||
| handler::register::EvmHandler, | ||
| interpreter::{InterpreterAction, SharedMemory}, | ||
| Context as RevmContext, Database, Frame, | ||
| }; | ||
| use revmc::{ | ||
| llvm::Context as LlvmContext, primitives::SpecId, EvmCompiler, EvmCompilerFn, EvmLlvmBackend, | ||
| OptimizationLevel, | ||
| }; | ||
| use std::{ | ||
| collections::HashMap, | ||
| sync::{mpsc::Sender, Arc, Mutex}, | ||
| thread, | ||
| }; | ||
| 
     | 
||
| /// The [Compiler] struct is a client for passing functions to the compiler thread. It also contains a cache of compiled functions | ||
| #[derive(Debug, Clone)] | ||
| pub struct Compiler { | ||
| sender: Sender<(SpecId, B256, Bytes)>, | ||
| fn_cache: Arc<Mutex<HashMap<B256, Option<EvmCompilerFn>>>>, | ||
| } | ||
| 
     | 
||
| // TODO: probably shouldn't have a default for something that spawns a thread? | ||
| impl Default for Compiler { | ||
| fn default() -> Self { | ||
| Self::new() | ||
| } | ||
| } | ||
| 
     | 
||
| impl Compiler { | ||
| /// Create a new compiler instance. This spawns a new compiler thread and the returned struct contains a [Sender](std::sync::mpsc::Sender) for sending functions to the compiler thread, | ||
| /// as well as a cache to compiled functions | ||
| pub fn new() -> Self { | ||
| let fn_cache = Arc::new(Mutex::new(HashMap::new())); | ||
| let (sender, receiver) = std::sync::mpsc::channel(); | ||
| 
     | 
||
| // TODO: graceful shutdown | ||
| thread::spawn({ | ||
| let fn_cache = fn_cache.clone(); | ||
| 
     | 
||
| move || { | ||
| let ctx = LlvmContext::create(); | ||
| // let mut compilers = Vec::new(); | ||
| 
     | 
||
| while let Ok((spec_id, hash, code)) = receiver.recv() { | ||
| fn_cache.lock().unwrap().insert(hash, None); | ||
| 
     | 
||
| // TODO: fail properly here. | ||
| let backend = | ||
| EvmLlvmBackend::new(&ctx, false, OptimizationLevel::Aggressive).unwrap(); | ||
| let compiler = Box::leak(Box::new(EvmCompiler::new(backend))); | ||
| 
     | 
||
| // Do we have to allocate here? Not sure there's a better option | ||
| let name = hex::encode(hash); | ||
| dbg!("compiled", &name); | ||
| 
     | 
||
| let result = | ||
| unsafe { compiler.jit(&name, &code, spec_id) }.expect("catastrophe"); | ||
| 
     | 
||
| fn_cache.lock().unwrap().insert(hash, Some(result)); | ||
| 
     | 
||
| // compilers.push(compiler); | ||
| } | ||
| } | ||
| }); | ||
| 
     | 
||
| Self { sender, fn_cache } | ||
| } | ||
| 
     | 
||
| // TODO: | ||
| // For safety, we should also borrow the EvmCompiler that holds the actual module with code to | ||
| // make sure that it's not dropped while before or during the function call. | ||
| fn get_compiled_fn(&self, spec_id: SpecId, hash: B256, code: Bytes) -> Option<EvmCompilerFn> { | ||
| match self.fn_cache.lock().unwrap().get(&hash) { | ||
| Some(maybe_f) => *maybe_f, | ||
| None => { | ||
| // TODO: put rules here for whether or not to compile the function | ||
| self.sender.send((spec_id, hash, code)).unwrap(); | ||
| None | ||
| } | ||
| } | ||
| } | ||
| } | ||
| 
     | 
||
| /// The [ExternalContext] struct is a container for the [Compiler] struct. | ||
| #[derive(Debug)] | ||
| pub struct ExternalContext { | ||
| compiler: Compiler, | ||
| } | ||
| 
     | 
||
| impl ExternalContext { | ||
| /// Create a new [ExternalContext] instance from a given [Compiler] instance. | ||
| pub const fn new(compiler: Compiler) -> Self { | ||
| Self { compiler } | ||
| } | ||
| 
     | 
||
| /// Get a compiled function if one exists, otherwise send the bytecode to the compiler to be compiled. | ||
| pub fn get_compiled_fn( | ||
| &self, | ||
| spec_id: SpecId, | ||
| hash: B256, | ||
| code: Bytes, | ||
| ) -> Option<EvmCompilerFn> { | ||
| self.compiler.get_compiled_fn(spec_id, hash, code) | ||
| } | ||
| } | ||
| 
     | 
||
| /// Registers the compiler handler with the EVM handler. | ||
| pub fn register_compiler_handler<DB>(handler: &mut EvmHandler<'_, ExternalContext, DB>) | ||
| where | ||
| DB: Database, | ||
| { | ||
| let f = handler.execution.execute_frame.clone(); | ||
| let spec_id = handler.cfg.spec_id; | ||
| 
     | 
||
| handler.execution.execute_frame = Arc::new(move |frame, memory, table, context| { | ||
| let Some(action) = execute_frame(spec_id, frame, memory, context) else { | ||
| dbg!("fallback"); | ||
| return f(frame, memory, table, context); | ||
| }; | ||
| 
     | 
||
| Ok(action) | ||
| }); | ||
| } | ||
| 
     | 
||
| fn execute_frame<DB: Database>( | ||
| spec_id: SpecId, | ||
| frame: &mut Frame, | ||
| memory: &mut SharedMemory, | ||
| context: &mut RevmContext<ExternalContext, DB>, | ||
| ) -> Option<InterpreterAction> { | ||
| // let library = context.external.get_or_load_library(context.evm.spec_id())?; | ||
| let interpreter = frame.interpreter_mut(); | ||
| 
     | 
||
| let hash = match interpreter.contract.hash { | ||
| Some(hash) => hash, | ||
| // TODO: is this an issue with EOF? | ||
| None => unreachable_no_hash(), | ||
| }; | ||
| 
     | 
||
| // should be cheap enough to clone because it's backed by bytes::Bytes | ||
| let code = interpreter.contract.bytecode.bytes(); | ||
| 
     | 
||
| let f = context.external.get_compiled_fn(spec_id, hash, code)?; | ||
| 
     | 
||
| // Safety: as long as the function is still in the cache, this is safe to call | ||
| let result = unsafe { f.call_with_interpreter_and_memory(interpreter, memory, context) }; | ||
| 
     | 
||
| dbg!("EXECUTED", &hash); | ||
| 
     | 
||
| Some(result) | ||
| } | ||
| 
     | 
||
| #[cold] | ||
| #[inline(never)] | ||
| const fn unreachable_no_hash() -> ! { | ||
| panic!("unreachable: bytecode hash is not set in the interpreter") | ||
| } | ||
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| 
          
            
          
           | 
    @@ -16,5 +16,6 @@ | |
| #![warn(unused_crate_dependencies)] | ||
| 
     | 
||
| pub mod chainspec; | ||
| pub mod compiler; | ||
| pub mod evm; | ||
| pub mod node; | ||
  Add this suggestion to a batch that can be applied as a single commit.
  This suggestion is invalid because no changes were made to the code.
  Suggestions cannot be applied while the pull request is closed.
  Suggestions cannot be applied while viewing a subset of changes.
  Only one suggestion per line can be applied in a batch.
  Add this suggestion to a batch that can be applied as a single commit.
  Applying suggestions on deleted lines is not supported.
  You must change the existing code in this line in order to create a valid suggestion.
  Outdated suggestions cannot be applied.
  This suggestion has been applied or marked resolved.
  Suggestions cannot be applied from pending reviews.
  Suggestions cannot be applied on multi-line comments.
  Suggestions cannot be applied while the pull request is queued to merge.
  Suggestion cannot be applied right now. Please check back later.
  
    
  
    
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm a little confused as to the relationship between a compiler, a module, and a contract.
Should I be compiling all the code with the same compiler?