<commit_before>/* 
 * Copyright 2012 <NAME> and other contributors
 * http://chirrup.org/
 * 
 * See the file LICENSE for copying permission.
 */

package edu.syr.pcpratts.rootbeer;

import edu.syr.pcpratts.rootbeer.classloader.FastWholeProgram;
import edu.syr.pcpratts.rootbeer.compiler.*;
import edu.syr.pcpratts.rootbeer.generate.opencl.tweaks.CudaTweaks;
import edu.syr.pcpratts.rootbeer.generate.opencl.tweaks.NativeCpuTweaks;
import edu.syr.pcpratts.rootbeer.generate.opencl.tweaks.Tweaks;
import edu.syr.pcpratts.rootbeer.util.*;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.util.*;
import java.util.zip.CRC32;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
import pack.Pack;
import soot.*;
import soot.options.Options;
import soot.util.JasminOutputStream;

public class RootbeerCompiler {

  private String m_classOutputFolder;
  private String m_jimpleOutputFolder;
  private FastWholeProgram m_fastLoader;
  private String m_provider;
  
  public RootbeerCompiler(){
    clearOutputFolders();
    
    m_classOutputFolder = Constants.OUTPUT_CLASS_FOLDER;
    m_jimpleOutputFolder = "output-jimple";
    
    if(Configuration.compilerInstance().getMode() == Configuration.MODE_GPU){      
      Tweaks.setInstance(new CudaTweaks());
    } else {
      Tweaks.setInstance(new NativeCpuTweaks());
    }
    
    m_fastLoader = FastWholeProgram.v();
  }
  
  public void compile(String main_jar, List<String> lib_jars, List<String> dirs, String dest_jar) {
    
  }
    
  private List<String> getRuntimeJars(){
    List<String> ret = new ArrayList<String>();
    String s = File.separator;
    if(System.getProperty("os.name").equals("Mac OS X")) {
	    //in Mac OS X, rt.jar is split into classes.jar and ui.jar
      ret.add(System.getProperty("java.home")+s+".."+s+"Classes"+s+"classes.jar");
      ret.add(System.getProperty("java.home")+s+".."+s+"Classes"+s+"ui.jar");
	  } else {
      //if windows or linux
      ret.add(System.getProperty("java.home")+s+"lib"+s+"rt.jar");
      ret.add(System.getProperty("java.home")+s+"lib"+s+"jce.jar");
      ret.add(System.getProperty("java.home")+s+"lib"+s+"charsets.jar");
      ret.add(System.getProperty("java.home")+s+"lib"+s+"jsse.jar");
    }
    return ret;
  }  
    
  public void compile(String jar_filename, String outname, String test_case) throws Exception {
    Options.v().set_allow_phantom_refs(true);
    
    m_fastLoader.addPath(jar_filename);
    m_fastLoader.addClassPath(getRuntimeJars());
    m_fastLoader.init();
    m_fastLoader.singleKernel();
    
    FindKernelForTestCase finder = new FindKernelForTestCase();
    String kernel = finder.get(test_case, m_fastLoader.getApplicationClasses());
    m_provider = finder.getProvider();
    m_fastLoader.findKernelClasses(kernel); 
    
    List<String> kernel_classes = new ArrayList<String>();
    kernel_classes.add(kernel);
    
    compileForKernels(outname, kernel_classes);
  }
  
  public void compile(String jar_filename, String outname) throws Exception {
    Options.v().set_allow_phantom_refs(true);
    
    m_fastLoader.addPath(jar_filename);
    m_fastLoader.addClassPath(getRuntimeJars());
    m_fastLoader.init();
    
    List<String> kernel_classes = m_fastLoader.findKernelClasses();    
    compileForKernels(outname, kernel_classes);
  }
  
  private void compileForKernels(String outname, List<String> kernel_classes) throws Exception {
    
    String[] sorted = new String[kernel_classes.size()];
    sorted = kernel_classes.toArray(sorted);
    Arrays.sort(sorted);
    kernel_classes.clear();
    for(String cls : sorted){
      kernel_classes.add(cls);
    }
    
    Map<String, List<String>> reachables = new HashMap<String, List<String>>();
    Map<String, List<String>> forward_reachables = new HashMap<String, List<String>>();
    List<String> all_reachables = new ArrayList<String>();
    
    for(String kernel : kernel_classes){
      SootClass soot_class = Scene.v().getSootClass(kernel);
      SootMethod kernel_method = soot_class.getMethod("void gpuMethod()");
      FastWholeProgram.v().getDfsMethods(kernel_method);
      
      System.out.println("finding kernel reachable methods for: "+soot_class.getShortName()+"...");
    
      KernelReachableMethods reachable_finder = new KernelReachableMethods();
      List<String> curr_reachables = reachable_finder.get(kernel);
      forward_reachables.put(kernel, reachable_finder.getForward());
      
      all_reachables.addAll(curr_reachables);
      reachables.put(kernel, curr_reachables);
    }
      
    ClassRemappingTransform transform = null;
    
    if(!Main.disable_class_remapping()){
      System.out.println("remapping some classes to GPU versions...");
      Iterator<String> iter = reachables.keySet().iterator();
      while(iter.hasNext()){
        List<String> curr_reachables = reachables.get(iter.next());
        transform = new ClassRemappingTransform(false);
        transform.run(curr_reachables);
        transform.finishClone();
      }
    }
    
    Transform2 transform2 = new Transform2();
    for(String cls : kernel_classes){
      List<String> curr_reachables = reachables.get(cls);
      RootbeerScene.v().setReachableMethods(curr_reachables);
      RootbeerScene.v().setForwardReachables(forward_reachables.get(cls));
      
      SootClass soot_class = Scene.v().getSootClass(cls);
      SootMethod kernel_method = soot_class.getMethod("void gpuMethod()");
      FastWholeProgram.v().getDfsMethods(kernel_method);
      
      transform2.run(cls);
    }
    
    Set<String> app_classes = new HashSet<String>();
    
    if(!Main.disable_class_remapping()){
      app_classes.addAll(transform.getModifiedClasses());
    }<commit_msg>running find reachables after class remapping also<commit_after>/* 
 * Copyright 2012 <NAME> and other contributors
 * http://chirrup.org/
 * 
 * See the file LICENSE for copying permission.
 */

package edu.syr.pcpratts.rootbeer;

import edu.syr.pcpratts.rootbeer.classloader.FastWholeProgram;
import edu.syr.pcpratts.rootbeer.compiler.*;
import edu.syr.pcpratts.rootbeer.generate.opencl.tweaks.CudaTweaks;
import edu.syr.pcpratts.rootbeer.generate.opencl.tweaks.NativeCpuTweaks;
import edu.syr.pcpratts.rootbeer.generate.opencl.tweaks.Tweaks;
import edu.syr.pcpratts.rootbeer.util.*;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.util.*;
import java.util.zip.CRC32;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
import pack.Pack;
import soot.*;
import soot.options.Options;
import soot.util.JasminOutputStream;

public class RootbeerCompiler {

  private String m_classOutputFolder;
  private String m_jimpleOutputFolder;
  private FastWholeProgram m_fastLoader;
  private String m_provider;
  
  public RootbeerCompiler(){
    clearOutputFolders();
    
    m_classOutputFolder = Constants.OUTPUT_CLASS_FOLDER;
    m_jimpleOutputFolder = "output-jimple";
    
    if(Configuration.compilerInstance().getMode() == Configuration.MODE_GPU){      
      Tweaks.setInstance(new CudaTweaks());
    } else {
      Tweaks.setInstance(new NativeCpuTweaks());
    }
    
    m_fastLoader = FastWholeProgram.v();
  }
  
  public void compile(String main_jar, List<String> lib_jars, List<String> dirs, String dest_jar) {
    
  }
    
  private List<String> getRuntimeJars(){
    List<String> ret = new ArrayList<String>();
    String s = File.separator;
    if(System.getProperty("os.name").equals("Mac OS X")) {
	    //in Mac OS X, rt.jar is split into classes.jar and ui.jar
      ret.add(System.getProperty("java.home")+s+".."+s+"Classes"+s+"classes.jar");
      ret.add(System.getProperty("java.home")+s+".."+s+"Classes"+s+"ui.jar");
	  } else {
      //if windows or linux
      ret.add(System.getProperty("java.home")+s+"lib"+s+"rt.jar");
      ret.add(System.getProperty("java.home")+s+"lib"+s+"jce.jar");
      ret.add(System.getProperty("java.home")+s+"lib"+s+"charsets.jar");
      ret.add(System.getProperty("java.home")+s+"lib"+s+"jsse.jar");
    }
    return ret;
  }  
    
  public void compile(String jar_filename, String outname, String test_case) throws Exception {
    Options.v().set_allow_phantom_refs(true);
    
    m_fastLoader.addPath(jar_filename);
    m_fastLoader.addClassPath(getRuntimeJars());
    m_fastLoader.init();
    m_fastLoader.singleKernel();
    
    FindKernelForTestCase finder = new FindKernelForTestCase();
    String kernel = finder.get(test_case, m_fastLoader.getApplicationClasses());
    m_provider = finder.getProvider();
    m_fastLoader.findKernelClasses(kernel); 
    
    List<String> kernel_classes = new ArrayList<String>();
    kernel_classes.add(kernel);
    
    compileForKernels(outname, kernel_classes);
  }
  
  public void compile(String jar_filename, String outname) throws Exception {
    Options.v().set_allow_phantom_refs(true);
    
    m_fastLoader.addPath(jar_filename);
    m_fastLoader.addClassPath(getRuntimeJars());
    m_fastLoader.init();
    
    List<String> kernel_classes = m_fastLoader.findKernelClasses();    
    compileForKernels(outname, kernel_classes);
  }
  
  private void compileForKernels(String outname, List<String> kernel_classes) throws Exception {
    
    String[] sorted = new String[kernel_classes.size()];
    sorted = kernel_classes.toArray(sorted);
    Arrays.sort(sorted);
    kernel_classes.clear();
    for(String cls : sorted){
      kernel_classes.add(cls);
    }
    
    Map<String, List<String>> reachables = new HashMap<String, List<String>>();
    Map<String, List<String>> forward_reachables = new HashMap<String, List<String>>();
    List<String> all_reachables = new ArrayList<String>();
    
    for(String kernel : kernel_classes){
      SootClass soot_class = Scene.v().getSootClass(kernel);
      SootMethod kernel_method = soot_class.getMethod("void gpuMethod()");
      FastWholeProgram.v().getDfsMethods(kernel_method);
      
      System.out.println("finding kernel reachable methods for: "+soot_class.getShortName()+"...");
    
      KernelReachableMethods reachable_finder = new KernelReachableMethods();
      List<String> curr_reachables = reachable_finder.get(kernel);
      forward_reachables.put(kernel, reachable_finder.getForward());
      
      all_reachables.addAll(curr_reachables);
      reachables.put(kernel, curr_reachables);
    }
      
    ClassRemappingTransform transform = null;
    
    if(!Main.disable_class_remapping()){
      System.out.println("remapping some classes to GPU versions...");
      Iterator<String> iter = reachables.keySet().iterator();
      while(iter.hasNext()){
        List<String> curr_reachables = reachables.get(iter.next());
        transform = new ClassRemappingTransform(false);
        transform.run(curr_reachables);
        transform.finishClone();
      }
    }
    
    //re-run finding kernel reachables with remap in
    reachables = new HashMap<String, List<String>>();
    forward_reachables = new HashMap<String, List<String>>();
    all_reachables = new ArrayList<String>();
    
    for(String kernel : kernel_classes){
      SootClass soot_class = Scene.v().getSootClass(kernel);
      SootMethod kernel_method = soot_class.getMethod("void gpuMethod()");
      FastWholeProgram.v().getDfsMethods(kernel_method);
      
      System.out.println("finding kernel reachable methods for: "+soot_class.getShortName()+"...");
    
      KernelReachableMethods reachable_finder = new KernelReachableMethods();
      List<String> curr_reachables = reachable_finder.get(kernel);
      forward_reachables.put(kernel, reachable_finder.getForward());
      
      all_reachables.addAll(curr_reachables);
      reachables.put(kernel, curr_reachables);
    }
    
    Transform2 transform2 = new Transform2();
    for(String cls : kernel_classes){
      List<String> curr_reachables = reachables.get(cls);
      RootbeerScene.v().setReachableMethods(curr_reachables);
      RootbeerScene.v().setForwardReachables(forward_reachables.get(cls));
      
      SootClass soot_class = Scene.v().getSootClass(cls);
      SootMethod kernel_method = soot_class.getMethod("void gpuMethod()");
      FastWholeProgram.v().getDfsMethods(kernel_method);
      
      transform2.run(cls);
    }
    
    Set<String> app_classes = new HashSet<String>();
    
    if(!Main.disable_class_remapping()){
      app_classes.addAll(transform.getModifiedClasses());
    }
{'fraction_non_alphanumeric': 0.12515683814303638, 'fraction_numerical': 0.0015683814303638645, 'mean_word_length': 3.9992159937279497, 'pattern_counts': {'":': 0, '<': 61, '<?xml version=': 0, '>': 61, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '821221', 'n_tokens_mistral': 4066, 'n_tokens_neox': 4002, 'n_words': 659}