msil bytecode machine code msil mdil c# msil mdil native code

23
Andrew Pardoe ([email protected] ) .NET Native ([email protected] ) Program Manager, .NET Runtime .NET Native deep dive

Upload: edwin-goodman

Post on 22-Dec-2015

242 views

Category:

Documents


2 download

TRANSCRIPT

Andrew Pardoe ([email protected]).NET Native ([email protected])Program Manager, .NET Runtime

.NET Native deep dive

Questions?

.NET code generation: A primer

Source code(C#/VB/F#)

MSIL bytecode Machine code

C#/VB/F# compiler

Code generation

Code generation is done either….• Lazily at runtime with a Just-In-Time, or JIT, compiler• Optimistically at build time with NGen, Triton or .NET Native

.NET Code Execution today

C++ PerformanceSelf-contained apps

Dynamic experience“Unlimited” class libraries

Code generation One JIT compiler for each architecture

64-bit JIT emphasizes code quality over throughput for server scenarios

32-bit JITs emphasize throughput over code quality for app scenarios Two native code generators with different deployment properties

NGen runs on the user’s machine, at install time or automatically Triton runs in the Windows Phone 8 Store, app bound to libraries at

install

One runtime (VM or VEE) spans multiple scenarios Desktop CLR for Windows Client and Server workloads (different GC

modes) CoreCLR for Mobile and targeted Server workloads built from the same

codebase

Code execution in CLR 4.5

MSIL MDIL

Triton: Native code on Windows Phone

C#MSIL

MDIL nativecode

Windows Phone apps are compiled

to native code in the cloud on WP8

The properties of code execution

Code generation properties

• Throughput• Code quality• Memory usage

Deployment properties

• Distribution format• Acquisition process• Serviceability

Security properties

• Typesafe execution• Code identity &

isolation• Security functionality

Developer experience

• Everything is a tradeoff!

• “Make simple things easy and hard things possible.”

.NET Code Execution tomorrow

C# Productivity with C++ Performance

C# Language withdynamic experiences

RyuJIT & CLR VM.NET Native

Today: One runtime engine & same code execution for all scenariosTomorrow: Refactored engine & multiple execution strategies provide best of class on both extremes. From there, we target the middle.

.NET Native (Project N)

.NET Native

.NET Native

Scenario Improvement (%)

Cold Startup 39.32%

Warm Startup 31.21%

Memory Usage

12.68%

Wordament on .NET Native:

Next Generation Compiler in the Cloud for Store Apps represents an evolution of Triton & NGen

Uses lean runtime and VC++ optimizer for fast code execution and reduced memory usage

Based on a refactored CLR engine. Native code is shared, managed code has moved into the libraries

Provides converged developer experience for .NET across devices

.

Visual Studio Experience for .NET Native

Enable .NET Native for your project

Debug and test your app with .NET Native

.NET Native Compiler Toolchain

IL Compiler: C# -> MSIL

- Compile C# source code

- Generate & compile marshalling and serialization code

Today, interop code is generated as MSIL at runtime, JIT-compiled, executed and thrown out JIT compilation takes time (but the methods are small) If your P/Invoke signature has an issue your compiled code is gone

In .NET Native, the Marshalling Code Generator creates interop code as a C# file at compile-time Compilation is done at build-time The code can be debugged as C#, not assembly The code can be inspected and optimized before compilation to MSIL

Pregenerated interop code

IL Compiler: Merge & reduce app code

- Merge all app & library code

- “Reduce” app to throw away unneeded types, code, metadata, etc.

A bit of a conundrum… Static compilation means any code that can run is generated at

compile-time Reflection requires that anything you can inspect at runtime can be

executed

The solution: preserve the set of classes, methods, generic types, etc., that the code might want to call

And if the compiler gets it wrong? Missing metadata results in an exception File a bug with the .NET Native team?

Static dynamic code (dynamic static code?)

The type specifies the class, namespace or assembly affected Types: class, interface, or struct Group of types: namespace, assembly (*Application* is all types in the app

assemblies) Type member: method, field, property, or event

The degree tells the compiler what behavior is needed Reflection behaviors, Serialization behaviors, Interop behaviors

The policy notes the scope to apply that behavior inside the type Accessibility: All, Public, Private, Internal Required: Including this keyword means “Include these types in the final binary”

Runtime directives (default.rd.xml)<Directives xmlns="http://schemas.microsoft.com/netfx/2013/01/metadata"> <Application> <Assembly Name="*Application*" Dynamic="Required All" /> <!-- Add your application specific runtime directives here. --> </Application></Directives>

IL Compiler: MSIL -> MDIL -> machine code

- C++ optimizer compiles MSIL to MDIL, Machine Dependent Intermediate Format. App is mostly machine code but cannot execute.

- “Bind” MDIL app code into machine code. App distributed as a stub, app.exe, and app code in a dll. MRT Runtime is distributed in the app package, making the app package self-sufficient.

for (long j = 0; j < numBodies; j++) // From Nbody sample{ Float_4 r;

r.x = A[j].pos.x - B.pos.x; r.y = A[j].pos.y - B.pos.y; r.z = A[j].pos.z - B.pos.z; float distSqr = r.x * r.x + r.y * r.y + r.z * r.z; distSqr += softeningSquared;

float invDist = 1.0f / (float)Math1.Sqrt(distSqr); float invDistCube = invDist * invDist * invDist;

float s = fParticleMass * invDistCube;

acc.x += r.x * s; acc.y += r.y * s; acc.z += r.z * s;}

Example: automatic vectorization/parallelization

The C++ optimizer can recognize this code as parallel and generate vector (SIMD) instructions

.NET Native performance gains

Optimized runtime

Pregenerated interop

Tree-shaken code

C++ optimizer Global optimizations

Pay-for-play metadata

Pregenerated serializers

Refactored core libraries

.NET Native: Call to actionLearn more about .NET Native

Download the .NET Native VS add-InTry out your Store app with .NET Native

Help us make .NET Native better

http://aka.ms/dotnetnativeRequires Visual Studio 2013 Update 2 RC

http://aka.ms/dotnetnativehttp://blogs.msdn.com/b/dotnet

Do you have a top Store app or library?Reach out to us: [email protected] Participate in the .NET Native ForumReach out to us: [email protected]