SlideShare uma empresa Scribd logo
1 de 26
Baixar para ler offline
Android RenderScript on LLVM

         Shih-wei Liao
          April 2011
Outline

  Overview of RenderScript (what, why, how, ...)
  Components
     Offline Compiler
     Online JIT Compiler
     RenderScript Runtime
  Put Everything Together:
     Producing a .apk: HelloCompute Example
     Running the .apk: Fast launch time & On-device linking
  Conclusions
What is RenderScript?

 It is the future of Android 3D Rendering and Compute
      Portability
      Performance
      Usability
 C99 plus some extensions
      Vector operations
      Function overloading
      rsForEach
 Current clients include:
      Books app
      YouTube app
      MovieStudio app
      Live wallpapers + 3D Launcher
 Challenging compiler + runtime work
RenderScript Components

 Offline compiler (llvm-rs-cc)
     Convert script files into portable bitcode and reflected
     Java layer
 Online JIT compiler (libbcc)
     Translate portable bitcode to appropriate machine code
     (CPU/GPU/DSP/...)
 Runtime library support (libRS)
     Manage scripts from Dalvik layer
     Also provide basic support libraries (math functions, etc.)
Offline Compiler: llvm-rs-cc

   Based on Clang/LLVM
      Clang and LLVM are popular open source compiler
      projects
      Clang - C/C++/ObjC compiler frontend
      LLVM - Optimizing compiler backend

Key features:
    Cleverly reuse Clang abstract syntax tree (AST) to reflect
    information back to Java layer
    Embeds metadata within bitcode (type, ...)
All bitcode supplied as a resource within .apk container
llvm-rs-cc (Cont'd)

Performs aggressive machine-independent optimizations on
host before emitting portable bitcode
   So the online JIT on Android devices can be lighter-weight
==----------------------llvm-rs-cc----------------------------------------===
               ... Pass execution timing report ...
===-------------------------------------------------------------------------===
Total Execution Time: 0.0040 seconds (0.0077 wall clock)
 --System Time-- --System Time-- ---Wall Time---                         --- Name ---
 0.0000 ( 0.0%)           0.0000 ( 0.0%)           0.0090 ( 15.8%) Simplify the CFG
 0.0000 ( 0.0%)           0.0000 ( 0.0%)           0.0008 ( 10.5%) Combine redundant instructions
 0.0000 ( 0.0%)           0.0000 ( 0.0%) 0.0007 ( 9.0%) Canonicalize Induction Variables
 0.0000 ( 0.0%)           0.0000 ( 0.0%) 0.0006 ( 7.6%) Combine redundant instructions
 0.0000 ( 0.0%)           0.0000 ( 0.0%) 0.0005 ( 6.0%) Global Value Numbering
 0.0000 ( 0.0%)           0.0000 ( 0.0%) 0.0004 ( 0.7%) Global Value Numbering
 0.0000 ( 0.0%)           0.0000 ( 0.0%) 0.0004 ( 0.7%) Combine redundant instructions
 0.0000 ( 0.0%)           0.0000 ( 0.0%) 0.0003 ( 0.6%) Scalar Replacement of Aggregates
 0.0000 ( 0.0%)           0.0000 ( 0.0%) 0.0003 ( 0.5%) Combine redundant instructions
 0.0000 ( 0.0%)           0.0000 ( 0.0%) 0.0002 ( 0.4%) Combine redundant instructions
 0.0000 ( 0.0%)           0.0000 ( 0.0%) 0.0002 ( 0.4%) Combine redundant instructions
 0.0000 ( 0.0%)           0.0000 ( 0.0%) 0.0002 ( 0.3%) Combine redundant instructions
 0.0000 ( 0.0%)           0.0000 ( 0.0%) 0.0002 ( 0.3%) Rotate Loops
 0.0000 ( 0.0%)           0.0000 ( 0.0%) 0.0001 ( 0.3%) Natural Loop Information
 0.0000 ( 0.0%)           0.0000 ( 0.0%) 0.0001 ( 0.2%) Interprocedural Sparse Conditional Constant Propagation
 0.0000 ( 0.0%)           0.0000 ( 0.0%) 0.0001 ( 0.2%) Loop Invariant Code Motion
 0.0000 ( 0.0%)           0.0000 ( 0.0%) 0.0001 ( 0.2%) Dominator Tree Construction
 0.0000 ( 0.0%)           0.0000 ( 0.0%) 0.0001 ( 0.1%) Canonicalize natural loops
 0.0000 ( 0.0%)           0.0000 ( 0.0%) 0.0001 ( 0.1%) Jump Threading
 0.0000 ( 0.0%)           0.0000 ( 0.0%) 0.0001 ( 0.1%) Sparse Conditional Constant Propagation
 0.0000 ( 0.0%)           0.0000 ( 0.0%) 0.0001 ( 0.1%) Reassociate expressions
 0.0000 ( 0.0%)           0.0000 ( 0.0%) 0.0001 ( 0.1%) Simplify the CFG
 0.0000 ( 0.0%)           0.0000 ( 0.0%) 0.0001 ( 0.1%) Jump Threading
 0.0000 ( 0.0%)           0.0000 ( 0.0%) 0.0001 ( 0.1%) Dominator Tree Construction
 0.0000 ( 0.0%)           0.0000 ( 0.0%) 0.0001 ( 0.1%) Deduce function attributes
 0.0000 ( 0.0%)           0.0000 ( 0.0%) 0.0001 ( 0.1%) Dominator Tree Construction
 0.0000 ( 0.0%)           0.0000 ( 0.0%) 0.0001 ( 0.1%) Dominance Frontier Construction
 0.0000 ( 0.0%)           0.0000 ( 0.0%) 0.0001 ( 0.1%) Basic CallGraph Construction
 0.0000 ( 0.0%)           0.0000 ( 0.0%) 0.0000 ( 0.1%) Dominance Frontier Construction
 0.0000 ( 0.0%)           0.0000 ( 0.0%) 0.0000 ( 0.1%) Dead Store Elimination
 0.0000 ( 0.0%)           0.0000 ( 0.0%) 0.0000 ( 0.1%) Dominator Tree Construction
 0.0000 ( 0.0%)           0.0000 ( 0.0%) 0.0000 ( 0.1%) Simplify the CFG
Offline Compiler Flow
Online JIT Compiler: libbcc

   Based on LLVM: Lighten it to fit in Android devices
      Currently supports ARM and x86
      Future targets include GPU/DSP
   Performs target-specific optimizations and code generation

Key features:
   Reflection for libRS: Provides hooks for runtime to access
   embedded metadata
   Caches JITed scripts to improve startup time
   On-device linking: Links dynamically against runtime library
   functions (math)
Online JIT Compiler Flow
RenderScript Runtime: libRS

 Manages Scripts and other RenderScript objects
     Script, Type, Element, Allocation, Mesh,
     ProgramFragment, ProgramRaster, ProgramStore,
     ProgramVertex, Sampler, various matrix and primitive
     types
 Provides implementation of runtime libraries (math, time,
 drawing, ref-counting, ...)
 Allows allocation, binding of objects to script globals
 Work distribution (multi-threading)
 Message-passing between Dalvik and script-side
HelloCompute Example

 Available in Honeycomb SDK samples
 Converts a bitmap image to grayscale
 Exploits parallelism by using rsForEach on every pixel of an
 allocation (simple dot product of RGB values)
 mono.rs -> mono.bc + reflected to ScriptC_mono.java
Sample Script (mono.rs)

#pragma version(1)
#pragma rs java_package_name(com.example.android.rs.hellocompute)

rs_allocation gIn;
rs_allocation gOut;
rs_script gScript;

const static float3 gMonoMult = {0.299f, 0.587f, 0.114f};

void root(const uchar4 *v_in, uchar4 *v_out, const void *usrData, uint32_t x, uint32_t y) {
  float4 f4 = rsUnpackColor8888(*v_in);

    float3 mono = dot(f4.rgb, gMonoMult);
    *v_out = rsPackColorTo8888(mono);
}

void filter() {
  rsForEach(gScript, gIn, gOut, 0);
}
ScriptC_mono.java pt. 1 (generated)

package com.example.android.rs.hellocompute;

import android.renderscript.*;
import android.content.res.Resources;

public class ScriptC_mono extends ScriptC {
  public ScriptC_mono(RenderScript rs, Resources resources, int id) {
    super(rs, resources, id);
  }

      private final static int mExportVarIdx_gIn = 0;
      private Allocation mExportVar_gIn;
      public void set_gIn(Allocation v) {
        mExportVar_gIn = v;
        setVar(mExportVarIdx_gIn, v);
      }

      public Allocation get_gIn() {
        return mExportVar_gIn;
      }
...
ScriptC_mono.java pt. 2 (generated)

... // Skipping reflection of gOut
   private final static int mExportVarIdx_gScript = 2;
   private Script mExportVar_gScript;
   public void set_gScript(Script v) {
      mExportVar_gScript = v;
      setVar(mExportVarIdx_gScript, v);
   }

    public Script get_gScript() {
      return mExportVar_gScript;
    }

    private final static int mExportFuncIdx_filter = 0;
    public void invoke_filter() {
      invoke(mExportFuncIdx_filter);
    }
}
HelloCompute.java (partial source)

public class HelloCompute extends Activity {
  ...
  private void createScript() {
      mRS = RenderScript.create(this);

          mInAllocation = Allocation.createFromBitmap(mRS, mBitmapIn,
           Allocation.MipmapControl.MIPMAP_NONE, Allocation.USAGE_SCRIPT);
          mOutAllocation = Allocation.createTyped(mRS,
           mInAllocation.getType());

          mScript = new ScriptC_mono(mRS, getResources(), R.raw.mono);

          mScript.set_gIn(mInAllocation);
          mScript.set_gOut(mOutAllocation);
          mScript.set_gScript(mScript);
          mScript.invoke_filter();
          mOutAllocation.copyTo(mBitmapOut);
    }
    ...
}
Running .apk Needs Fast Launch, but...

LLVM's optimizations may take time. E.g., for -O3, 750ms for
1500 lines of RenderScript app.

There are still many
lines of LLVM code
on a smaller app:
Caching: Launch fast, while allowing
 heavy optimizations ahead-of-time
Apps        libbcc without AOT. libbcc with AOT.
            launch time (libbcc) launch time (libbcc)
Books       1218ms               9ms
Youtube     842ms                4ms
Wallpaper
MagicSmoke 182ms                 3ms

Halo        127ms                3ms
Balls       149ms                3ms

SceneGraph 146ms                 90ms

Model       104ms                4ms
Fountain    57ms                 3ms
On-Device Linking

Design:
  Provide generic runtime library (lib*.bc), while
      allow CPU/GPU vendors to provide their specialized lib*.
      bc, and
      reduce latency of function invocation
  An example: RenderScript's runtime (libclcore.bc) comes
  with vector operations. Xoom's libclcore will have different
  CPU/GPU support (VFP3-16) than Nexus S's (NEON).

Implementation:
   Linking at LLVM bitcode level
   Using Link-Time Optimization to inline functions
   and perform constant propagation
Conclusions

 RenderScript
    Portable, high-performance, and developer-friendly
    3D graphics + compute acceleration path
 Hide complexity through compiler + runtime
    C99-based + rsForEach
    Ample use of reflection
    Library functions
    Opaque managed types + reference counting
 Lightweight JIT:
    Fast launch time
    On-device linking
 See http://developer.android.com/ for the latest info
Backup
Adventures in AST Annotation

  RenderScript runtime manages a bunch of types
     Allocations in the sample script (plus other things too)
     How do we know when they can be cleaned up?
         Java-Side ???
         Script-Side ???
  Reference Counting
     rsSetObject(), rsClearObject()
     Developers do not want to micro-manage opaque blobs
     Solution is to dynamically annotate script code to use
     these functions in the appropriate spots
Annotating the AST

 AST - Abstract Syntax Tree - follows source code in Clang
 Update this in-place before we emit bitcode
 Need to do a few types of conversions on variables with an
 RS object type (rs_* types, not including rs_matrix*)
    Assignments -> rsSetObject(&lhs, rhs)
    Insert destructor calls as rsClearObject(&local) for locals
 Global variables get cleaned up by runtime after script
 object is destroyed
Reference Counting Example

rs_font globalIn[10], globalOut;
void foo(int j) {
 rs_font localUninit;
 localUninit = globalIn[0];

    for (int i = 0; i < j; i++) {
     rs_font forNest = globalIn[i];

        switch (i) {
         case 3:


          return;
         case 7:

          continue;
         default:
          break;
        }
        localUninit = forNest;

    }

    globalOut = localUninit;

    return;
}
RS Object Local Variables

rs_font globalIn[10], globalOut;
void foo(int j) {
 rs_font localUninit;
 localUninit = globalIn[0];

    for (int i = 0; i < j; i++) {
     rs_font forNest = globalIn[i];

        switch (i) {
         case 3:


          return;
         case 7:

          continue;
         default:
          break;
        }
        localUninit = forNest;

    }

    globalOut = localUninit;

    return;
}
Assignment -> rsSetObject()

rs_font globalIn[10], globalOut;
void foo(int j) {
 rs_font localUninit;
 rsSetObject(&localUninit, globalIn[0]); // Simple translation to call-expr

    for (int i = 0; i < j; i++) {
     rs_font forNest;
     rsSetObject(&forNest, globalIn[i]);      // Initializers must be split before conversion
     switch (i) {
      case 3:


          return;
         case 7:

          continue;
         default:
          break;
        }
        rsSetObject(&localUninit, forNest);

    }

    rsSetObject(&globalOut, localUninit);

    return;
}
Insert Destructor Calls

rs_font globalIn[10], globalOut;
void foo(int j) {
 rs_font localUninit;
 rsSetObject(&localUninit, globalIn[0]);

    for (int i = 0; i < j; i++) {
      rs_font forNest;
      rsSetObject(&forNest, globalIn[i]);
      switch (i) {
        case 3:
         rsClearObject(&localUninit); // Return statements always require that you destroy
         rsClearObject(&forNest);           // any in-scope local objects (inside-out).
         return;
        case 7:
         rsClearObject(&forNest);           // continue scopes to for-loop, so destroy forNest
         continue;
        default:
         break;                   // break scopes to switch-stmt, so do nothing
      }
      rsSetObject(&localUninit, forNest);
      rsClearObject(&forNest);              // End of for-loop scope, so destroy forNest
    }

    rsSetObject(&globalOut, localUninit);
    rsClearObject(&localUninit);       // End outer scope (before return)
    return;
}

Mais conteúdo relacionado

Mais procurados

Qt on Real Time Operating Systems
Qt on Real Time Operating SystemsQt on Real Time Operating Systems
Qt on Real Time Operating Systemsaccount inactive
 
OpenGL Shading Language
OpenGL Shading LanguageOpenGL Shading Language
OpenGL Shading LanguageJungsoo Nam
 
Skiron - Experiments in CPU Design in D
Skiron - Experiments in CPU Design in DSkiron - Experiments in CPU Design in D
Skiron - Experiments in CPU Design in DMithun Hunsur
 
Why scala is not my ideal language and what I can do with this
Why scala is not my ideal language and what I can do with thisWhy scala is not my ideal language and what I can do with this
Why scala is not my ideal language and what I can do with thisRuslan Shevchenko
 
A Brief Introduction to the Qt Application Framework
A Brief Introduction to the Qt Application FrameworkA Brief Introduction to the Qt Application Framework
A Brief Introduction to the Qt Application FrameworkZachary Blair
 
Scripting Your Qt Application
Scripting Your Qt ApplicationScripting Your Qt Application
Scripting Your Qt Applicationaccount inactive
 
Understaing Android EGL
Understaing Android EGLUnderstaing Android EGL
Understaing Android EGLSuhan Lee
 
NVIDIA's OpenGL Functionality
NVIDIA's OpenGL FunctionalityNVIDIA's OpenGL Functionality
NVIDIA's OpenGL FunctionalityMark Kilgard
 
Best Practices in Qt Quick/QML - Part I
Best Practices in Qt Quick/QML - Part IBest Practices in Qt Quick/QML - Part I
Best Practices in Qt Quick/QML - Part IICS
 
Visual Studio를 이용한 어셈블리어 학습 part 2
Visual Studio를 이용한 어셈블리어 학습 part 2Visual Studio를 이용한 어셈블리어 학습 part 2
Visual Studio를 이용한 어셈블리어 학습 part 2YEONG-CHEON YOU
 
[GEG1] 10.camera-centric engine design for multithreaded rendering
[GEG1] 10.camera-centric engine design for multithreaded rendering[GEG1] 10.camera-centric engine design for multithreaded rendering
[GEG1] 10.camera-centric engine design for multithreaded rendering종빈 오
 
Using the Android Native Development Kit (NDK)
Using the Android Native Development Kit (NDK)Using the Android Native Development Kit (NDK)
Using the Android Native Development Kit (NDK)DroidConTLV
 
clWrap: Nonsense free control of your GPU
clWrap: Nonsense free control of your GPUclWrap: Nonsense free control of your GPU
clWrap: Nonsense free control of your GPUJohn Colvin
 
Kotlin in action
Kotlin in actionKotlin in action
Kotlin in actionCiro Rizzo
 
06 - Qt Communication
06 - Qt Communication06 - Qt Communication
06 - Qt CommunicationAndreas Jakl
 
JCConf 2018 - Retrospect and Prospect of Java
JCConf 2018 - Retrospect and Prospect of JavaJCConf 2018 - Retrospect and Prospect of Java
JCConf 2018 - Retrospect and Prospect of JavaJoseph Kuo
 

Mais procurados (20)

Qt on Real Time Operating Systems
Qt on Real Time Operating SystemsQt on Real Time Operating Systems
Qt on Real Time Operating Systems
 
OpenGL Shading Language
OpenGL Shading LanguageOpenGL Shading Language
OpenGL Shading Language
 
The Future of Qt Widgets
The Future of Qt WidgetsThe Future of Qt Widgets
The Future of Qt Widgets
 
Skiron - Experiments in CPU Design in D
Skiron - Experiments in CPU Design in DSkiron - Experiments in CPU Design in D
Skiron - Experiments in CPU Design in D
 
Why scala is not my ideal language and what I can do with this
Why scala is not my ideal language and what I can do with thisWhy scala is not my ideal language and what I can do with this
Why scala is not my ideal language and what I can do with this
 
A Brief Introduction to the Qt Application Framework
A Brief Introduction to the Qt Application FrameworkA Brief Introduction to the Qt Application Framework
A Brief Introduction to the Qt Application Framework
 
Scripting Your Qt Application
Scripting Your Qt ApplicationScripting Your Qt Application
Scripting Your Qt Application
 
Understaing Android EGL
Understaing Android EGLUnderstaing Android EGL
Understaing Android EGL
 
NVIDIA's OpenGL Functionality
NVIDIA's OpenGL FunctionalityNVIDIA's OpenGL Functionality
NVIDIA's OpenGL Functionality
 
R ext world/ useR! Kiev
R ext world/ useR!  KievR ext world/ useR!  Kiev
R ext world/ useR! Kiev
 
Best Practices in Qt Quick/QML - Part I
Best Practices in Qt Quick/QML - Part IBest Practices in Qt Quick/QML - Part I
Best Practices in Qt Quick/QML - Part I
 
Visual Studio를 이용한 어셈블리어 학습 part 2
Visual Studio를 이용한 어셈블리어 학습 part 2Visual Studio를 이용한 어셈블리어 학습 part 2
Visual Studio를 이용한 어셈블리어 학습 part 2
 
[GEG1] 10.camera-centric engine design for multithreaded rendering
[GEG1] 10.camera-centric engine design for multithreaded rendering[GEG1] 10.camera-centric engine design for multithreaded rendering
[GEG1] 10.camera-centric engine design for multithreaded rendering
 
Using the Android Native Development Kit (NDK)
Using the Android Native Development Kit (NDK)Using the Android Native Development Kit (NDK)
Using the Android Native Development Kit (NDK)
 
clWrap: Nonsense free control of your GPU
clWrap: Nonsense free control of your GPUclWrap: Nonsense free control of your GPU
clWrap: Nonsense free control of your GPU
 
Kotlin in action
Kotlin in actionKotlin in action
Kotlin in action
 
OpenGL for 2015
OpenGL for 2015OpenGL for 2015
OpenGL for 2015
 
06 - Qt Communication
06 - Qt Communication06 - Qt Communication
06 - Qt Communication
 
Android ndk
Android ndkAndroid ndk
Android ndk
 
JCConf 2018 - Retrospect and Prospect of Java
JCConf 2018 - Retrospect and Prospect of JavaJCConf 2018 - Retrospect and Prospect of Java
JCConf 2018 - Retrospect and Prospect of Java
 

Semelhante a Android RenderScript on LLVM

StrongLoop Overview
StrongLoop OverviewStrongLoop Overview
StrongLoop OverviewShubhra Kar
 
Analyzing the Performance of Mobile Web
Analyzing the Performance of Mobile WebAnalyzing the Performance of Mobile Web
Analyzing the Performance of Mobile WebAriya Hidayat
 
Improving app performance with Kotlin Coroutines
Improving app performance with Kotlin CoroutinesImproving app performance with Kotlin Coroutines
Improving app performance with Kotlin CoroutinesHassan Abid
 
Native Java with GraalVM
Native Java with GraalVMNative Java with GraalVM
Native Java with GraalVMSylvain Wallez
 
Hybrid Apps (Native + Web) via QtWebKit
Hybrid Apps (Native + Web) via QtWebKitHybrid Apps (Native + Web) via QtWebKit
Hybrid Apps (Native + Web) via QtWebKitAriya Hidayat
 
HKG15-300: Art's Quick Compiler: An unofficial overview
HKG15-300: Art's Quick Compiler: An unofficial overviewHKG15-300: Art's Quick Compiler: An unofficial overview
HKG15-300: Art's Quick Compiler: An unofficial overviewLinaro
 
Windows Debugging with WinDbg
Windows Debugging with WinDbgWindows Debugging with WinDbg
Windows Debugging with WinDbgArno Huetter
 
Using Smalltalk for controlling robotics systems
Using Smalltalk for controlling robotics systemsUsing Smalltalk for controlling robotics systems
Using Smalltalk for controlling robotics systemsSerge Stinckwich
 
JVM Mechanics: When Does the JVM JIT & Deoptimize?
JVM Mechanics: When Does the JVM JIT & Deoptimize?JVM Mechanics: When Does the JVM JIT & Deoptimize?
JVM Mechanics: When Does the JVM JIT & Deoptimize?Doug Hawkins
 
Google Developer Fest 2010
Google Developer Fest 2010Google Developer Fest 2010
Google Developer Fest 2010Chris Ramsdale
 
12 Monkeys Inside JS Engine
12 Monkeys Inside JS Engine12 Monkeys Inside JS Engine
12 Monkeys Inside JS EngineChengHui Weng
 
NTT SIC marketplace slide deck at Tokyo Summit
NTT SIC marketplace slide deck at Tokyo SummitNTT SIC marketplace slide deck at Tokyo Summit
NTT SIC marketplace slide deck at Tokyo SummitToshikazu Ichikawa
 
Grow and Shrink - Dynamically Extending the Ruby VM Stack
Grow and Shrink - Dynamically Extending the Ruby VM StackGrow and Shrink - Dynamically Extending the Ruby VM Stack
Grow and Shrink - Dynamically Extending the Ruby VM StackKeitaSugiyama1
 
Google io bootcamp_2010
Google io bootcamp_2010Google io bootcamp_2010
Google io bootcamp_2010Chris Ramsdale
 
Advanced iOS Debbuging (Reloaded)
Advanced iOS Debbuging (Reloaded)Advanced iOS Debbuging (Reloaded)
Advanced iOS Debbuging (Reloaded)Massimo Oliviero
 
Building Web Apps Sanely - EclipseCon 2010
Building Web Apps Sanely - EclipseCon 2010Building Web Apps Sanely - EclipseCon 2010
Building Web Apps Sanely - EclipseCon 2010Chris Ramsdale
 
Fisl - Deployment
Fisl - DeploymentFisl - Deployment
Fisl - DeploymentFabio Akita
 
Hybrid Apps (Native + Web) using WebKit
Hybrid Apps (Native + Web) using WebKitHybrid Apps (Native + Web) using WebKit
Hybrid Apps (Native + Web) using WebKitAriya Hidayat
 

Semelhante a Android RenderScript on LLVM (20)

StrongLoop Overview
StrongLoop OverviewStrongLoop Overview
StrongLoop Overview
 
Analyzing the Performance of Mobile Web
Analyzing the Performance of Mobile WebAnalyzing the Performance of Mobile Web
Analyzing the Performance of Mobile Web
 
Improving app performance with Kotlin Coroutines
Improving app performance with Kotlin CoroutinesImproving app performance with Kotlin Coroutines
Improving app performance with Kotlin Coroutines
 
Native Java with GraalVM
Native Java with GraalVMNative Java with GraalVM
Native Java with GraalVM
 
Hybrid Apps (Native + Web) via QtWebKit
Hybrid Apps (Native + Web) via QtWebKitHybrid Apps (Native + Web) via QtWebKit
Hybrid Apps (Native + Web) via QtWebKit
 
HKG15-300: Art's Quick Compiler: An unofficial overview
HKG15-300: Art's Quick Compiler: An unofficial overviewHKG15-300: Art's Quick Compiler: An unofficial overview
HKG15-300: Art's Quick Compiler: An unofficial overview
 
Windows Debugging with WinDbg
Windows Debugging with WinDbgWindows Debugging with WinDbg
Windows Debugging with WinDbg
 
Using Smalltalk for controlling robotics systems
Using Smalltalk for controlling robotics systemsUsing Smalltalk for controlling robotics systems
Using Smalltalk for controlling robotics systems
 
JVM Mechanics: When Does the JVM JIT & Deoptimize?
JVM Mechanics: When Does the JVM JIT & Deoptimize?JVM Mechanics: When Does the JVM JIT & Deoptimize?
JVM Mechanics: When Does the JVM JIT & Deoptimize?
 
Google Developer Fest 2010
Google Developer Fest 2010Google Developer Fest 2010
Google Developer Fest 2010
 
12 Monkeys Inside JS Engine
12 Monkeys Inside JS Engine12 Monkeys Inside JS Engine
12 Monkeys Inside JS Engine
 
NTT SIC marketplace slide deck at Tokyo Summit
NTT SIC marketplace slide deck at Tokyo SummitNTT SIC marketplace slide deck at Tokyo Summit
NTT SIC marketplace slide deck at Tokyo Summit
 
JBoss World 2010
JBoss World 2010JBoss World 2010
JBoss World 2010
 
Grow and Shrink - Dynamically Extending the Ruby VM Stack
Grow and Shrink - Dynamically Extending the Ruby VM StackGrow and Shrink - Dynamically Extending the Ruby VM Stack
Grow and Shrink - Dynamically Extending the Ruby VM Stack
 
Google io bootcamp_2010
Google io bootcamp_2010Google io bootcamp_2010
Google io bootcamp_2010
 
Advanced iOS Debbuging (Reloaded)
Advanced iOS Debbuging (Reloaded)Advanced iOS Debbuging (Reloaded)
Advanced iOS Debbuging (Reloaded)
 
Build Programming Language Runtime with LLVM
Build Programming Language Runtime with LLVMBuild Programming Language Runtime with LLVM
Build Programming Language Runtime with LLVM
 
Building Web Apps Sanely - EclipseCon 2010
Building Web Apps Sanely - EclipseCon 2010Building Web Apps Sanely - EclipseCon 2010
Building Web Apps Sanely - EclipseCon 2010
 
Fisl - Deployment
Fisl - DeploymentFisl - Deployment
Fisl - Deployment
 
Hybrid Apps (Native + Web) using WebKit
Hybrid Apps (Native + Web) using WebKitHybrid Apps (Native + Web) using WebKit
Hybrid Apps (Native + Web) using WebKit
 

Último

The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxMalak Abu Hammad
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfsudhanshuwaghmare1
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsJoaquim Jorge
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking MenDelhi Call girls
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)wesley chun
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptxHampshireHUG
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEarley Information Science
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfEnterprise Knowledge
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountPuma Security, LLC
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024The Digital Insurer
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUK Journal
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Miguel Araújo
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Scriptwesley chun
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Drew Madelung
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...apidays
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...Neo4j
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Servicegiselly40
 

Último (20)

The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptx
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path Mount
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
 

Android RenderScript on LLVM

  • 1. Android RenderScript on LLVM Shih-wei Liao April 2011
  • 2. Outline Overview of RenderScript (what, why, how, ...) Components Offline Compiler Online JIT Compiler RenderScript Runtime Put Everything Together: Producing a .apk: HelloCompute Example Running the .apk: Fast launch time & On-device linking Conclusions
  • 3. What is RenderScript? It is the future of Android 3D Rendering and Compute Portability Performance Usability C99 plus some extensions Vector operations Function overloading rsForEach Current clients include: Books app YouTube app MovieStudio app Live wallpapers + 3D Launcher Challenging compiler + runtime work
  • 4. RenderScript Components Offline compiler (llvm-rs-cc) Convert script files into portable bitcode and reflected Java layer Online JIT compiler (libbcc) Translate portable bitcode to appropriate machine code (CPU/GPU/DSP/...) Runtime library support (libRS) Manage scripts from Dalvik layer Also provide basic support libraries (math functions, etc.)
  • 5. Offline Compiler: llvm-rs-cc Based on Clang/LLVM Clang and LLVM are popular open source compiler projects Clang - C/C++/ObjC compiler frontend LLVM - Optimizing compiler backend Key features: Cleverly reuse Clang abstract syntax tree (AST) to reflect information back to Java layer Embeds metadata within bitcode (type, ...) All bitcode supplied as a resource within .apk container
  • 6. llvm-rs-cc (Cont'd) Performs aggressive machine-independent optimizations on host before emitting portable bitcode So the online JIT on Android devices can be lighter-weight ==----------------------llvm-rs-cc----------------------------------------=== ... Pass execution timing report ... ===-------------------------------------------------------------------------=== Total Execution Time: 0.0040 seconds (0.0077 wall clock) --System Time-- --System Time-- ---Wall Time--- --- Name --- 0.0000 ( 0.0%) 0.0000 ( 0.0%) 0.0090 ( 15.8%) Simplify the CFG 0.0000 ( 0.0%) 0.0000 ( 0.0%) 0.0008 ( 10.5%) Combine redundant instructions 0.0000 ( 0.0%) 0.0000 ( 0.0%) 0.0007 ( 9.0%) Canonicalize Induction Variables 0.0000 ( 0.0%) 0.0000 ( 0.0%) 0.0006 ( 7.6%) Combine redundant instructions 0.0000 ( 0.0%) 0.0000 ( 0.0%) 0.0005 ( 6.0%) Global Value Numbering 0.0000 ( 0.0%) 0.0000 ( 0.0%) 0.0004 ( 0.7%) Global Value Numbering 0.0000 ( 0.0%) 0.0000 ( 0.0%) 0.0004 ( 0.7%) Combine redundant instructions 0.0000 ( 0.0%) 0.0000 ( 0.0%) 0.0003 ( 0.6%) Scalar Replacement of Aggregates 0.0000 ( 0.0%) 0.0000 ( 0.0%) 0.0003 ( 0.5%) Combine redundant instructions 0.0000 ( 0.0%) 0.0000 ( 0.0%) 0.0002 ( 0.4%) Combine redundant instructions 0.0000 ( 0.0%) 0.0000 ( 0.0%) 0.0002 ( 0.4%) Combine redundant instructions 0.0000 ( 0.0%) 0.0000 ( 0.0%) 0.0002 ( 0.3%) Combine redundant instructions 0.0000 ( 0.0%) 0.0000 ( 0.0%) 0.0002 ( 0.3%) Rotate Loops 0.0000 ( 0.0%) 0.0000 ( 0.0%) 0.0001 ( 0.3%) Natural Loop Information 0.0000 ( 0.0%) 0.0000 ( 0.0%) 0.0001 ( 0.2%) Interprocedural Sparse Conditional Constant Propagation 0.0000 ( 0.0%) 0.0000 ( 0.0%) 0.0001 ( 0.2%) Loop Invariant Code Motion 0.0000 ( 0.0%) 0.0000 ( 0.0%) 0.0001 ( 0.2%) Dominator Tree Construction 0.0000 ( 0.0%) 0.0000 ( 0.0%) 0.0001 ( 0.1%) Canonicalize natural loops 0.0000 ( 0.0%) 0.0000 ( 0.0%) 0.0001 ( 0.1%) Jump Threading 0.0000 ( 0.0%) 0.0000 ( 0.0%) 0.0001 ( 0.1%) Sparse Conditional Constant Propagation 0.0000 ( 0.0%) 0.0000 ( 0.0%) 0.0001 ( 0.1%) Reassociate expressions 0.0000 ( 0.0%) 0.0000 ( 0.0%) 0.0001 ( 0.1%) Simplify the CFG 0.0000 ( 0.0%) 0.0000 ( 0.0%) 0.0001 ( 0.1%) Jump Threading 0.0000 ( 0.0%) 0.0000 ( 0.0%) 0.0001 ( 0.1%) Dominator Tree Construction 0.0000 ( 0.0%) 0.0000 ( 0.0%) 0.0001 ( 0.1%) Deduce function attributes 0.0000 ( 0.0%) 0.0000 ( 0.0%) 0.0001 ( 0.1%) Dominator Tree Construction 0.0000 ( 0.0%) 0.0000 ( 0.0%) 0.0001 ( 0.1%) Dominance Frontier Construction 0.0000 ( 0.0%) 0.0000 ( 0.0%) 0.0001 ( 0.1%) Basic CallGraph Construction 0.0000 ( 0.0%) 0.0000 ( 0.0%) 0.0000 ( 0.1%) Dominance Frontier Construction 0.0000 ( 0.0%) 0.0000 ( 0.0%) 0.0000 ( 0.1%) Dead Store Elimination 0.0000 ( 0.0%) 0.0000 ( 0.0%) 0.0000 ( 0.1%) Dominator Tree Construction 0.0000 ( 0.0%) 0.0000 ( 0.0%) 0.0000 ( 0.1%) Simplify the CFG
  • 8. Online JIT Compiler: libbcc Based on LLVM: Lighten it to fit in Android devices Currently supports ARM and x86 Future targets include GPU/DSP Performs target-specific optimizations and code generation Key features: Reflection for libRS: Provides hooks for runtime to access embedded metadata Caches JITed scripts to improve startup time On-device linking: Links dynamically against runtime library functions (math)
  • 10. RenderScript Runtime: libRS Manages Scripts and other RenderScript objects Script, Type, Element, Allocation, Mesh, ProgramFragment, ProgramRaster, ProgramStore, ProgramVertex, Sampler, various matrix and primitive types Provides implementation of runtime libraries (math, time, drawing, ref-counting, ...) Allows allocation, binding of objects to script globals Work distribution (multi-threading) Message-passing between Dalvik and script-side
  • 11. HelloCompute Example Available in Honeycomb SDK samples Converts a bitmap image to grayscale Exploits parallelism by using rsForEach on every pixel of an allocation (simple dot product of RGB values) mono.rs -> mono.bc + reflected to ScriptC_mono.java
  • 12. Sample Script (mono.rs) #pragma version(1) #pragma rs java_package_name(com.example.android.rs.hellocompute) rs_allocation gIn; rs_allocation gOut; rs_script gScript; const static float3 gMonoMult = {0.299f, 0.587f, 0.114f}; void root(const uchar4 *v_in, uchar4 *v_out, const void *usrData, uint32_t x, uint32_t y) { float4 f4 = rsUnpackColor8888(*v_in); float3 mono = dot(f4.rgb, gMonoMult); *v_out = rsPackColorTo8888(mono); } void filter() { rsForEach(gScript, gIn, gOut, 0); }
  • 13. ScriptC_mono.java pt. 1 (generated) package com.example.android.rs.hellocompute; import android.renderscript.*; import android.content.res.Resources; public class ScriptC_mono extends ScriptC { public ScriptC_mono(RenderScript rs, Resources resources, int id) { super(rs, resources, id); } private final static int mExportVarIdx_gIn = 0; private Allocation mExportVar_gIn; public void set_gIn(Allocation v) { mExportVar_gIn = v; setVar(mExportVarIdx_gIn, v); } public Allocation get_gIn() { return mExportVar_gIn; } ...
  • 14. ScriptC_mono.java pt. 2 (generated) ... // Skipping reflection of gOut private final static int mExportVarIdx_gScript = 2; private Script mExportVar_gScript; public void set_gScript(Script v) { mExportVar_gScript = v; setVar(mExportVarIdx_gScript, v); } public Script get_gScript() { return mExportVar_gScript; } private final static int mExportFuncIdx_filter = 0; public void invoke_filter() { invoke(mExportFuncIdx_filter); } }
  • 15. HelloCompute.java (partial source) public class HelloCompute extends Activity { ... private void createScript() { mRS = RenderScript.create(this); mInAllocation = Allocation.createFromBitmap(mRS, mBitmapIn, Allocation.MipmapControl.MIPMAP_NONE, Allocation.USAGE_SCRIPT); mOutAllocation = Allocation.createTyped(mRS, mInAllocation.getType()); mScript = new ScriptC_mono(mRS, getResources(), R.raw.mono); mScript.set_gIn(mInAllocation); mScript.set_gOut(mOutAllocation); mScript.set_gScript(mScript); mScript.invoke_filter(); mOutAllocation.copyTo(mBitmapOut); } ... }
  • 16. Running .apk Needs Fast Launch, but... LLVM's optimizations may take time. E.g., for -O3, 750ms for 1500 lines of RenderScript app. There are still many lines of LLVM code on a smaller app:
  • 17. Caching: Launch fast, while allowing heavy optimizations ahead-of-time Apps libbcc without AOT. libbcc with AOT. launch time (libbcc) launch time (libbcc) Books 1218ms 9ms Youtube 842ms 4ms Wallpaper MagicSmoke 182ms 3ms Halo 127ms 3ms Balls 149ms 3ms SceneGraph 146ms 90ms Model 104ms 4ms Fountain 57ms 3ms
  • 18. On-Device Linking Design: Provide generic runtime library (lib*.bc), while allow CPU/GPU vendors to provide their specialized lib*. bc, and reduce latency of function invocation An example: RenderScript's runtime (libclcore.bc) comes with vector operations. Xoom's libclcore will have different CPU/GPU support (VFP3-16) than Nexus S's (NEON). Implementation: Linking at LLVM bitcode level Using Link-Time Optimization to inline functions and perform constant propagation
  • 19. Conclusions RenderScript Portable, high-performance, and developer-friendly 3D graphics + compute acceleration path Hide complexity through compiler + runtime C99-based + rsForEach Ample use of reflection Library functions Opaque managed types + reference counting Lightweight JIT: Fast launch time On-device linking See http://developer.android.com/ for the latest info
  • 21. Adventures in AST Annotation RenderScript runtime manages a bunch of types Allocations in the sample script (plus other things too) How do we know when they can be cleaned up? Java-Side ??? Script-Side ??? Reference Counting rsSetObject(), rsClearObject() Developers do not want to micro-manage opaque blobs Solution is to dynamically annotate script code to use these functions in the appropriate spots
  • 22. Annotating the AST AST - Abstract Syntax Tree - follows source code in Clang Update this in-place before we emit bitcode Need to do a few types of conversions on variables with an RS object type (rs_* types, not including rs_matrix*) Assignments -> rsSetObject(&lhs, rhs) Insert destructor calls as rsClearObject(&local) for locals Global variables get cleaned up by runtime after script object is destroyed
  • 23. Reference Counting Example rs_font globalIn[10], globalOut; void foo(int j) { rs_font localUninit; localUninit = globalIn[0]; for (int i = 0; i < j; i++) { rs_font forNest = globalIn[i]; switch (i) { case 3: return; case 7: continue; default: break; } localUninit = forNest; } globalOut = localUninit; return; }
  • 24. RS Object Local Variables rs_font globalIn[10], globalOut; void foo(int j) { rs_font localUninit; localUninit = globalIn[0]; for (int i = 0; i < j; i++) { rs_font forNest = globalIn[i]; switch (i) { case 3: return; case 7: continue; default: break; } localUninit = forNest; } globalOut = localUninit; return; }
  • 25. Assignment -> rsSetObject() rs_font globalIn[10], globalOut; void foo(int j) { rs_font localUninit; rsSetObject(&localUninit, globalIn[0]); // Simple translation to call-expr for (int i = 0; i < j; i++) { rs_font forNest; rsSetObject(&forNest, globalIn[i]); // Initializers must be split before conversion switch (i) { case 3: return; case 7: continue; default: break; } rsSetObject(&localUninit, forNest); } rsSetObject(&globalOut, localUninit); return; }
  • 26. Insert Destructor Calls rs_font globalIn[10], globalOut; void foo(int j) { rs_font localUninit; rsSetObject(&localUninit, globalIn[0]); for (int i = 0; i < j; i++) { rs_font forNest; rsSetObject(&forNest, globalIn[i]); switch (i) { case 3: rsClearObject(&localUninit); // Return statements always require that you destroy rsClearObject(&forNest); // any in-scope local objects (inside-out). return; case 7: rsClearObject(&forNest); // continue scopes to for-loop, so destroy forNest continue; default: break; // break scopes to switch-stmt, so do nothing } rsSetObject(&localUninit, forNest); rsClearObject(&forNest); // End of for-loop scope, so destroy forNest } rsSetObject(&globalOut, localUninit); rsClearObject(&localUninit); // End outer scope (before return) return; }