SlideShare una empresa de Scribd logo
1 de 16
Descargar para leer sin conexión
Clang! Clang!
Who’s there?
WebAssembly!
Paulo Matos (pmatos@igalia.com)
Alex Bradbury (asb@igalia.com)
Andy Wingo (wingo@igalia.com)
November 9, 2022
Thanks to
My teammates Alex Bradbury and Andy Wingo!
Clang! Clang! Who's there? WebAssembly!
Paulo Matos, October 21, 2022
The Wasm Tools Team for suggestions, discussions and
reviews!
For Sponsoring this work!
WebAssembly for those in a Rush
Clang! Clang! Who's there? WebAssembly!
Paulo Matos, October 21, 2022
The WebAssembly VM (spec: https://webassembly.github.io/spec/core/)
● Harvard Architecture (Linear memory separate from code)
● Well-typed functions and globals organized into modules
● Module-level globals and Function-level locals not addressable
Initially:
● 4 numeric types { i32, i64, f32, f64 }
● Spec provides instructions to manipulate values of these types
● Load-store instructions to manipulate memory
Compile to Wasm
Clang! Clang! Who's there? WebAssembly!
Paulo Matos, October 21, 2022
1 double vals[100];
2 void store_double(size_t idx, double val) {
3 vals[idx] = val;
4 }
5 double fetch_double(size_t idx) {
6 return vals[idx];
7 }
https://godbolt.org/z/r4WMncWKb
1 store_double:
2 local.get 0
3 i32.const 3
4 i32.shl
5 i32.const vals
6 i32.add
7 local.get 1
8 f64.store 0
9 end_function
10 fetch_double:
11 local.get 0
12 i32.const 3
13 i32.shl
14 i32.const vals
15 i32.add
16 f64.load 0
17 end_function
18 vals:
Evolving WebAssembly
Clang! Clang! Who's there? WebAssembly!
Paulo Matos, October 21, 2022
What if instead of doubles we have JS values? 1 JSVal vals[100];
2 void store_jsval(size_t idx, JSVal val) {
3 vals[idx] = val;
4 }
5 JSVal fetch_jsval(size_t idx) {
6 return vals[idx];
7 }
Enter Reference Types:
● These types are opaque and host-managed
● Can’t be stored to linear memory
○ but they can be arguments and return
values from functions
○ can be stored to globals
● Currently two different types: funcref and
externref.
○ funcref is a callable reference type
Doesn’t work for reftypes!
The main challenge we are trying to solve is how to represent these values internally in
Clang and LLVM but also how to expose them to the user as C/C++ extensions.
WebAssembly Tables
Clang! Clang! Who's there? WebAssembly!
Paulo Matos, October 21, 2022
Tables are used to store reference types
● Even weirder than the reftypes themselves!
They have a bunch of constraints:
● Can’t be stored to linear memory, or stack!
● Can’t be arguments or return values of functions!
● They are global static values in a module.
1 externref_t vals[100] __attribute__((wasm_table));
2 void store_externref(size_t idx, JSVal val) {
3 vals[idx] = val;
4 }
5 JSVal fetch_externref(size_t idx) {
6 return vals[idx];
7 }
1 store_jsval:
2 local.get 1
3 local.get 0
4 table.set vals
5 end_function
6
7 fetch_jsval:
8 local.get 0
9 table.get vals
10 end_function
11
12 vals:
Reference Types in LLVM IR
Clang! Clang! Who's there? WebAssembly!
Paulo Matos, October 21, 2022
🥳 Support for Reference Types (including tables has landed).
%externref = type ptr addrspace(10)
%funcref = type ptr addrspace(20)
@table = local_unnamed_addr addrspace(1) global [0 x %funcref] undef
● Addrspace 1, 10, 20 are non-integral
● Tables are represented as global arrays
and accessed via intrinsics.
MVT::externref
MVT::funcref
addrspace(1) for
object that don’t
have an in-memory
representation
Reference Types in LLVM IR
Clang! Clang! Who's there? WebAssembly!
Paulo Matos, October 21, 2022
1 define void @store_jsval(%externref %g, i32 %i) {
2 call void @llvm.wasm.table.set.externref(ptr addrspace(1) @vals, i32 %i, %externref %g)
3 ret void
4 }
5
6 define %externref @fetch_jsval(i32 %i) {
7 %ref = call %externref @llvm.wasm.table.get.externref(ptr addrspace(1) @vals, i32 %i)
8 ret %externref %ref
9 }
1 store_jsval:
2 local.get 1
3 local.get 0
4 table.set
vals
5 end_function
6
7 fetch_jsval:
8 local.get 0
9 table.get
vals
10 end_function
11
12 vals:
https://godbolt.org/z/jnb8x93a4
What’s the story in Clang?
Reference Types in Clang
Clang! Clang! Who's there? WebAssembly!
Paulo Matos, October 21, 2022
In Clang we need:
● a syntax to represent reference types and
● lowering to LLVM IR
Currently being worked out in a downstream public branch (initial
prototypes as D122215, D128440, D123510, D124162)
__externref_t is a new type
● with the expected reftype
constraints
● lowered to LLVM IR as a ptr to
addrspace(10)
__externref_t JSVAL;
funcref is dealt with differently!
● attribute is attached to function
pointers: __funcref
● lowered to LLVM IR as a ptr to
addrspace(20)
typedef void (*__funcref fn_vv_t)();
typedef int (*__funcref fn_ii_t)(int);
Reference Types in Clang
Clang! Clang! Who's there? WebAssembly!
Paulo Matos, October 21, 2022
Tables store reference types!
● Given it’s indexed by an integer, sounds like an array representation is best.
○ However, internally representing a table indexing as ArraySubscript causes
issues with over-optimization!
foo[i] ~> ArraySubscript
● Alternatively, implement a new AST node TableSubscript
○ own set of problems as it requires new debug information impl, ABI info, etc.
foo[i] ~> TableSubscript (new)
● Simpler alternative is to model tables and operations as Intrinsics.
○ Syntax not as ergonomic but possibly quicker path to goal and easier to
upstream (?).
foo[i] instead __wasm_table_get(foo, i)
Have patch for first approach - wip patch for second - trying out third approach! 😅
WebAssembly GC
Clang! Clang! Who's there? WebAssembly!
Paulo Matos, October 21, 2022
Reference types were just a taster for what’s to come!
● GC managed objects: arrays and structs
○ (this is unrelated to GC support in LLVM)
● New instructions introduced to manipulate this type
○ i.e access struct fields, array access, etc
● A lot of the work we are doing at the moment is understanding how to
represent these in LLVM!
○ but we want to take this all the way to Clang!
Problem: Current AS approach won’t scale for all GC types!
Need to produce correctly typed locals, globals, function argos and returns
● WebAssembly GC types need to be maintained from LLVM IR through to the
backend.
This includes parameterised types, typed function references, etc.
● Therefore defining a new MVT for each won’t work.
WebAssembly GC
Clang! Clang! Who's there? WebAssembly!
Paulo Matos, October 21, 2022
Our current approach:
● it’s key that type identity is maintained
● Approach is to have one AS ID (currently > 255) as an index into a metadata
● Module-level metadata table index by AS ID tracks value types
The goal is to pass these types through LLVM IR all the way to WebAssembly emission
as we cannot translate these to LLVM’s limited type system.
This approach is meant as a prototype - not the one we intend to use going forwards.
Hopefully we can work together upstream to find a better solution for IR-level opaque
type support!
WebAssembly GC Example 1
Clang! Clang! Who's there? WebAssembly!
Paulo Matos, October 21, 2022
1 !0 = !{!"externref"}
2 !wasm.type_info = !{!0}
3
4 %wasmref = type ptr addrspace(256)
5 %externref = type ptr addrspace(257)
6
7 @externref_table = local_unnamed_addr addrspace(1) global [0 x %externref] undef
8
9 declare %wasmref @llvm.wasm.table.get.wasmref(ptr addrspace(1), i32) nounwind
10
11 define %externref @get_externref_from_table(i32 %i) {
12 %ref_u = call %wasmref @llvm.wasm.table.get.wasmref(ptr addrspace(1) @externref_table, i32 %i)
13 %ref = addrspacecast %wasmref %ref_u to %externref
14 ret %externref %ref
15 }
WebAssembly GC Example 2
Clang! Clang! Who's there? WebAssembly!
Paulo Matos, October 21, 2022
1 !0 = !{!"externref"}
2 !wasm.type_info = !{!0}
3
4 %wasmref = type ptr addrspace(256)
5 %externref = type ptr addrspace(257)
6
7 @externref_table = local_unnamed_addr addrspace(1) global [0 x %externref] undef
8
9 declare %wasmref @llvm.wasm.table.get.wasmref(ptr addrspace(1), i32) nounwind
10
11 define %externref @get_externref_from_table(i32 %i) {
12 %ref_u = call %wasmref @llvm.wasm.table.get.wasmref(ptr addrspace(1) @externref_table, i32 %i)
13 %ref = addrspacecast %wasmref %ref_u to %externref
14 ret %externref %ref
15 }
1 !0 = !{!"array i32"}
2 !wasm.type_info = !{!0}
3 %array_i32 = type ptr addrspace(257)
4
5 %alloca_cell = type ptr addrspace(1)
6
7 declare void @inhibit_store_to_load_forwarding()
8
9 define %array_i32 @ir_local_array_i32(%array_i32 %arg) {
10 %retval = alloca %array_i32, addrspace(1)
11 store %array_i32 %arg, %alloca_cell %retval
12 call void @inhibit_store_to_load_forwarding()
13 %reloaded = load %array_i32, %alloca_cell %retval
14 ret %array_i32 %reloaded
15 }
Lets Talk Strings - stringref
Clang! Clang! Who's there? WebAssembly!
Paulo Matos, October 21, 2022
Strings as GC reference types!
● Need to support GC strings across the toolchain
○ currently focusing on LLVM atm
● Using the same mechanism as other Wasm GC types, where stringref would inherit
an AS ID.
1 !0 = !{!"externref", !"stringref", !"array i32"}
2 !wasm.type_info = !{!0}
3
4 %externref = type ptr addrspace(256)
5 %stringref = type ptr addrspace(257)
6 %array_i32 = type ptr addrspace(258)
Summary
Clang! Clang! Who's there? WebAssembly!
Paulo Matos, October 21, 2022
✅ Reference types in LLVM IR
󰘶 Reference types in Clang (D122215, D128440, D123510, D124162)
󰘶 GC types in LLVM IR (public branch downstream)
󰘶 Stringref in LLVM IR (public branch downstream)
🤔 GC types in Clang
🤔 Stringref in Clang We are hiring!
https://www.igalia.com/jobs/

Más contenido relacionado

Similar a Clang, Clang: Who's there? WebAssembly!

Construction Techniques For Domain Specific Languages
Construction Techniques For Domain Specific LanguagesConstruction Techniques For Domain Specific Languages
Construction Techniques For Domain Specific Languages
ThoughtWorks
 
Enabling White-Box Reuse in a Pure Composition Language
Enabling White-Box Reuse in a Pure Composition LanguageEnabling White-Box Reuse in a Pure Composition Language
Enabling White-Box Reuse in a Pure Composition Language
elliando dias
 

Similar a Clang, Clang: Who's there? WebAssembly! (20)

Bridging the gap between designers and developers at the Guardian
Bridging the gap between designers and developers at the GuardianBridging the gap between designers and developers at the Guardian
Bridging the gap between designers and developers at the Guardian
 
XConf 2022 - Code As Data: How data insights on legacy codebases can fill the...
XConf 2022 - Code As Data: How data insights on legacy codebases can fill the...XConf 2022 - Code As Data: How data insights on legacy codebases can fill the...
XConf 2022 - Code As Data: How data insights on legacy codebases can fill the...
 
The Dynamic Language is not Enough
The Dynamic Language is not EnoughThe Dynamic Language is not Enough
The Dynamic Language is not Enough
 
String Comparison Surprises: Did Postgres lose my data?
String Comparison Surprises: Did Postgres lose my data?String Comparison Surprises: Did Postgres lose my data?
String Comparison Surprises: Did Postgres lose my data?
 
Construction Techniques For Domain Specific Languages
Construction Techniques For Domain Specific LanguagesConstruction Techniques For Domain Specific Languages
Construction Techniques For Domain Specific Languages
 
WebCamp: Developer Day: DDD in PHP on example of Symfony - Олег Зинченко
WebCamp: Developer Day: DDD in PHP on example of Symfony - Олег ЗинченкоWebCamp: Developer Day: DDD in PHP on example of Symfony - Олег Зинченко
WebCamp: Developer Day: DDD in PHP on example of Symfony - Олег Зинченко
 
Angular JS in 2017
Angular JS in 2017Angular JS in 2017
Angular JS in 2017
 
iOS Visual F/X Using GLSL
iOS Visual F/X Using GLSLiOS Visual F/X Using GLSL
iOS Visual F/X Using GLSL
 
Grails Introduction - IJTC 2007
Grails Introduction - IJTC 2007Grails Introduction - IJTC 2007
Grails Introduction - IJTC 2007
 
This upload requires better support for ODP format
This upload requires better support for ODP formatThis upload requires better support for ODP format
This upload requires better support for ODP format
 
Generative AI for Reengineering Variants into Software Product Lines: An Expe...
Generative AI for Reengineering Variants into Software Product Lines: An Expe...Generative AI for Reengineering Variants into Software Product Lines: An Expe...
Generative AI for Reengineering Variants into Software Product Lines: An Expe...
 
Computer Graphics - Lecture 01 - 3D Programming I
Computer Graphics - Lecture 01 - 3D Programming IComputer Graphics - Lecture 01 - 3D Programming I
Computer Graphics - Lecture 01 - 3D Programming I
 
Grails 101
Grails 101Grails 101
Grails 101
 
Facete - Exploring the web of spatial data with facete
Facete - Exploring the web of spatial data with faceteFacete - Exploring the web of spatial data with facete
Facete - Exploring the web of spatial data with facete
 
Inspecting Block Closures To Generate Shaders for GPU Execution
Inspecting Block Closures To Generate Shaders for GPU ExecutionInspecting Block Closures To Generate Shaders for GPU Execution
Inspecting Block Closures To Generate Shaders for GPU Execution
 
"Xapi-lang For declarative code generation" By James Nelson
"Xapi-lang For declarative code generation" By James Nelson"Xapi-lang For declarative code generation" By James Nelson
"Xapi-lang For declarative code generation" By James Nelson
 
Enabling White-Box Reuse in a Pure Composition Language
Enabling White-Box Reuse in a Pure Composition LanguageEnabling White-Box Reuse in a Pure Composition Language
Enabling White-Box Reuse in a Pure Composition Language
 
Mongo-Drupal
Mongo-DrupalMongo-Drupal
Mongo-Drupal
 
Glorp Tutorial Guide
Glorp Tutorial GuideGlorp Tutorial Guide
Glorp Tutorial Guide
 
Zero con2019
Zero con2019Zero con2019
Zero con2019
 

Más de Igalia

Building End-user Applications on Embedded Devices with WPE
Building End-user Applications on Embedded Devices with WPEBuilding End-user Applications on Embedded Devices with WPE
Building End-user Applications on Embedded Devices with WPE
Igalia
 
Automated Testing for Web-based Systems on Embedded Devices
Automated Testing for Web-based Systems on Embedded DevicesAutomated Testing for Web-based Systems on Embedded Devices
Automated Testing for Web-based Systems on Embedded Devices
Igalia
 
Running JS via WASM faster with JIT
Running JS via WASM      faster with JITRunning JS via WASM      faster with JIT
Running JS via WASM faster with JIT
Igalia
 
Introducción a Mesa. Caso específico dos dispositivos Raspberry Pi por Igalia
Introducción a Mesa. Caso específico dos dispositivos Raspberry Pi por IgaliaIntroducción a Mesa. Caso específico dos dispositivos Raspberry Pi por Igalia
Introducción a Mesa. Caso específico dos dispositivos Raspberry Pi por Igalia
Igalia
 

Más de Igalia (20)

A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
Building End-user Applications on Embedded Devices with WPE
Building End-user Applications on Embedded Devices with WPEBuilding End-user Applications on Embedded Devices with WPE
Building End-user Applications on Embedded Devices with WPE
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 
Automated Testing for Web-based Systems on Embedded Devices
Automated Testing for Web-based Systems on Embedded DevicesAutomated Testing for Web-based Systems on Embedded Devices
Automated Testing for Web-based Systems on Embedded Devices
 
Embedding WPE WebKit - from Bring-up to Maintenance
Embedding WPE WebKit - from Bring-up to MaintenanceEmbedding WPE WebKit - from Bring-up to Maintenance
Embedding WPE WebKit - from Bring-up to Maintenance
 
Optimizing Scheduler for Linux Gaming.pdf
Optimizing Scheduler for Linux Gaming.pdfOptimizing Scheduler for Linux Gaming.pdf
Optimizing Scheduler for Linux Gaming.pdf
 
Running JS via WASM faster with JIT
Running JS via WASM      faster with JITRunning JS via WASM      faster with JIT
Running JS via WASM faster with JIT
 
To crash or not to crash: if you do, at least recover fast!
To crash or not to crash: if you do, at least recover fast!To crash or not to crash: if you do, at least recover fast!
To crash or not to crash: if you do, at least recover fast!
 
Implementing a Vulkan Video Encoder From Mesa to GStreamer
Implementing a Vulkan Video Encoder From Mesa to GStreamerImplementing a Vulkan Video Encoder From Mesa to GStreamer
Implementing a Vulkan Video Encoder From Mesa to GStreamer
 
8 Years of Open Drivers, including the State of Vulkan in Mesa
8 Years of Open Drivers, including the State of Vulkan in Mesa8 Years of Open Drivers, including the State of Vulkan in Mesa
8 Years of Open Drivers, including the State of Vulkan in Mesa
 
Introducción a Mesa. Caso específico dos dispositivos Raspberry Pi por Igalia
Introducción a Mesa. Caso específico dos dispositivos Raspberry Pi por IgaliaIntroducción a Mesa. Caso específico dos dispositivos Raspberry Pi por Igalia
Introducción a Mesa. Caso específico dos dispositivos Raspberry Pi por Igalia
 
2023 in Chimera Linux
2023 in Chimera                    Linux2023 in Chimera                    Linux
2023 in Chimera Linux
 
Building a Linux distro with LLVM
Building a Linux distro        with LLVMBuilding a Linux distro        with LLVM
Building a Linux distro with LLVM
 
turnip: Update on Open Source Vulkan Driver for Adreno GPUs
turnip: Update on Open Source Vulkan Driver for Adreno GPUsturnip: Update on Open Source Vulkan Driver for Adreno GPUs
turnip: Update on Open Source Vulkan Driver for Adreno GPUs
 
Graphics stack updates for Raspberry Pi devices
Graphics stack updates for Raspberry Pi devicesGraphics stack updates for Raspberry Pi devices
Graphics stack updates for Raspberry Pi devices
 
Delegated Compositing - Utilizing Wayland Protocols for Chromium on ChromeOS
Delegated Compositing - Utilizing Wayland Protocols for Chromium on ChromeOSDelegated Compositing - Utilizing Wayland Protocols for Chromium on ChromeOS
Delegated Compositing - Utilizing Wayland Protocols for Chromium on ChromeOS
 
MessageFormat: The future of i18n on the web
MessageFormat: The future of i18n on the webMessageFormat: The future of i18n on the web
MessageFormat: The future of i18n on the web
 
Replacing the geometry pipeline with mesh shaders
Replacing the geometry pipeline with mesh shadersReplacing the geometry pipeline with mesh shaders
Replacing the geometry pipeline with mesh shaders
 
I'm not an AMD expert, but...
I'm not an AMD expert, but...I'm not an AMD expert, but...
I'm not an AMD expert, but...
 
Status of Vulkan on Raspberry
Status of Vulkan on RaspberryStatus of Vulkan on Raspberry
Status of Vulkan on Raspberry
 

Último

Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
vu2urc
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
Joaquim Jorge
 

Último (20)

Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
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
 
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...
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
Advantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessAdvantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your Business
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
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
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
HTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation StrategiesHTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation Strategies
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 
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
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 

Clang, Clang: Who's there? WebAssembly!

  • 1. Clang! Clang! Who’s there? WebAssembly! Paulo Matos (pmatos@igalia.com) Alex Bradbury (asb@igalia.com) Andy Wingo (wingo@igalia.com) November 9, 2022
  • 2. Thanks to My teammates Alex Bradbury and Andy Wingo! Clang! Clang! Who's there? WebAssembly! Paulo Matos, October 21, 2022 The Wasm Tools Team for suggestions, discussions and reviews! For Sponsoring this work!
  • 3. WebAssembly for those in a Rush Clang! Clang! Who's there? WebAssembly! Paulo Matos, October 21, 2022 The WebAssembly VM (spec: https://webassembly.github.io/spec/core/) ● Harvard Architecture (Linear memory separate from code) ● Well-typed functions and globals organized into modules ● Module-level globals and Function-level locals not addressable Initially: ● 4 numeric types { i32, i64, f32, f64 } ● Spec provides instructions to manipulate values of these types ● Load-store instructions to manipulate memory
  • 4. Compile to Wasm Clang! Clang! Who's there? WebAssembly! Paulo Matos, October 21, 2022 1 double vals[100]; 2 void store_double(size_t idx, double val) { 3 vals[idx] = val; 4 } 5 double fetch_double(size_t idx) { 6 return vals[idx]; 7 } https://godbolt.org/z/r4WMncWKb 1 store_double: 2 local.get 0 3 i32.const 3 4 i32.shl 5 i32.const vals 6 i32.add 7 local.get 1 8 f64.store 0 9 end_function 10 fetch_double: 11 local.get 0 12 i32.const 3 13 i32.shl 14 i32.const vals 15 i32.add 16 f64.load 0 17 end_function 18 vals:
  • 5. Evolving WebAssembly Clang! Clang! Who's there? WebAssembly! Paulo Matos, October 21, 2022 What if instead of doubles we have JS values? 1 JSVal vals[100]; 2 void store_jsval(size_t idx, JSVal val) { 3 vals[idx] = val; 4 } 5 JSVal fetch_jsval(size_t idx) { 6 return vals[idx]; 7 } Enter Reference Types: ● These types are opaque and host-managed ● Can’t be stored to linear memory ○ but they can be arguments and return values from functions ○ can be stored to globals ● Currently two different types: funcref and externref. ○ funcref is a callable reference type Doesn’t work for reftypes! The main challenge we are trying to solve is how to represent these values internally in Clang and LLVM but also how to expose them to the user as C/C++ extensions.
  • 6. WebAssembly Tables Clang! Clang! Who's there? WebAssembly! Paulo Matos, October 21, 2022 Tables are used to store reference types ● Even weirder than the reftypes themselves! They have a bunch of constraints: ● Can’t be stored to linear memory, or stack! ● Can’t be arguments or return values of functions! ● They are global static values in a module. 1 externref_t vals[100] __attribute__((wasm_table)); 2 void store_externref(size_t idx, JSVal val) { 3 vals[idx] = val; 4 } 5 JSVal fetch_externref(size_t idx) { 6 return vals[idx]; 7 } 1 store_jsval: 2 local.get 1 3 local.get 0 4 table.set vals 5 end_function 6 7 fetch_jsval: 8 local.get 0 9 table.get vals 10 end_function 11 12 vals:
  • 7. Reference Types in LLVM IR Clang! Clang! Who's there? WebAssembly! Paulo Matos, October 21, 2022 🥳 Support for Reference Types (including tables has landed). %externref = type ptr addrspace(10) %funcref = type ptr addrspace(20) @table = local_unnamed_addr addrspace(1) global [0 x %funcref] undef ● Addrspace 1, 10, 20 are non-integral ● Tables are represented as global arrays and accessed via intrinsics. MVT::externref MVT::funcref addrspace(1) for object that don’t have an in-memory representation
  • 8. Reference Types in LLVM IR Clang! Clang! Who's there? WebAssembly! Paulo Matos, October 21, 2022 1 define void @store_jsval(%externref %g, i32 %i) { 2 call void @llvm.wasm.table.set.externref(ptr addrspace(1) @vals, i32 %i, %externref %g) 3 ret void 4 } 5 6 define %externref @fetch_jsval(i32 %i) { 7 %ref = call %externref @llvm.wasm.table.get.externref(ptr addrspace(1) @vals, i32 %i) 8 ret %externref %ref 9 } 1 store_jsval: 2 local.get 1 3 local.get 0 4 table.set vals 5 end_function 6 7 fetch_jsval: 8 local.get 0 9 table.get vals 10 end_function 11 12 vals: https://godbolt.org/z/jnb8x93a4 What’s the story in Clang?
  • 9. Reference Types in Clang Clang! Clang! Who's there? WebAssembly! Paulo Matos, October 21, 2022 In Clang we need: ● a syntax to represent reference types and ● lowering to LLVM IR Currently being worked out in a downstream public branch (initial prototypes as D122215, D128440, D123510, D124162) __externref_t is a new type ● with the expected reftype constraints ● lowered to LLVM IR as a ptr to addrspace(10) __externref_t JSVAL; funcref is dealt with differently! ● attribute is attached to function pointers: __funcref ● lowered to LLVM IR as a ptr to addrspace(20) typedef void (*__funcref fn_vv_t)(); typedef int (*__funcref fn_ii_t)(int);
  • 10. Reference Types in Clang Clang! Clang! Who's there? WebAssembly! Paulo Matos, October 21, 2022 Tables store reference types! ● Given it’s indexed by an integer, sounds like an array representation is best. ○ However, internally representing a table indexing as ArraySubscript causes issues with over-optimization! foo[i] ~> ArraySubscript ● Alternatively, implement a new AST node TableSubscript ○ own set of problems as it requires new debug information impl, ABI info, etc. foo[i] ~> TableSubscript (new) ● Simpler alternative is to model tables and operations as Intrinsics. ○ Syntax not as ergonomic but possibly quicker path to goal and easier to upstream (?). foo[i] instead __wasm_table_get(foo, i) Have patch for first approach - wip patch for second - trying out third approach! 😅
  • 11. WebAssembly GC Clang! Clang! Who's there? WebAssembly! Paulo Matos, October 21, 2022 Reference types were just a taster for what’s to come! ● GC managed objects: arrays and structs ○ (this is unrelated to GC support in LLVM) ● New instructions introduced to manipulate this type ○ i.e access struct fields, array access, etc ● A lot of the work we are doing at the moment is understanding how to represent these in LLVM! ○ but we want to take this all the way to Clang! Problem: Current AS approach won’t scale for all GC types! Need to produce correctly typed locals, globals, function argos and returns ● WebAssembly GC types need to be maintained from LLVM IR through to the backend. This includes parameterised types, typed function references, etc. ● Therefore defining a new MVT for each won’t work.
  • 12. WebAssembly GC Clang! Clang! Who's there? WebAssembly! Paulo Matos, October 21, 2022 Our current approach: ● it’s key that type identity is maintained ● Approach is to have one AS ID (currently > 255) as an index into a metadata ● Module-level metadata table index by AS ID tracks value types The goal is to pass these types through LLVM IR all the way to WebAssembly emission as we cannot translate these to LLVM’s limited type system. This approach is meant as a prototype - not the one we intend to use going forwards. Hopefully we can work together upstream to find a better solution for IR-level opaque type support!
  • 13. WebAssembly GC Example 1 Clang! Clang! Who's there? WebAssembly! Paulo Matos, October 21, 2022 1 !0 = !{!"externref"} 2 !wasm.type_info = !{!0} 3 4 %wasmref = type ptr addrspace(256) 5 %externref = type ptr addrspace(257) 6 7 @externref_table = local_unnamed_addr addrspace(1) global [0 x %externref] undef 8 9 declare %wasmref @llvm.wasm.table.get.wasmref(ptr addrspace(1), i32) nounwind 10 11 define %externref @get_externref_from_table(i32 %i) { 12 %ref_u = call %wasmref @llvm.wasm.table.get.wasmref(ptr addrspace(1) @externref_table, i32 %i) 13 %ref = addrspacecast %wasmref %ref_u to %externref 14 ret %externref %ref 15 }
  • 14. WebAssembly GC Example 2 Clang! Clang! Who's there? WebAssembly! Paulo Matos, October 21, 2022 1 !0 = !{!"externref"} 2 !wasm.type_info = !{!0} 3 4 %wasmref = type ptr addrspace(256) 5 %externref = type ptr addrspace(257) 6 7 @externref_table = local_unnamed_addr addrspace(1) global [0 x %externref] undef 8 9 declare %wasmref @llvm.wasm.table.get.wasmref(ptr addrspace(1), i32) nounwind 10 11 define %externref @get_externref_from_table(i32 %i) { 12 %ref_u = call %wasmref @llvm.wasm.table.get.wasmref(ptr addrspace(1) @externref_table, i32 %i) 13 %ref = addrspacecast %wasmref %ref_u to %externref 14 ret %externref %ref 15 } 1 !0 = !{!"array i32"} 2 !wasm.type_info = !{!0} 3 %array_i32 = type ptr addrspace(257) 4 5 %alloca_cell = type ptr addrspace(1) 6 7 declare void @inhibit_store_to_load_forwarding() 8 9 define %array_i32 @ir_local_array_i32(%array_i32 %arg) { 10 %retval = alloca %array_i32, addrspace(1) 11 store %array_i32 %arg, %alloca_cell %retval 12 call void @inhibit_store_to_load_forwarding() 13 %reloaded = load %array_i32, %alloca_cell %retval 14 ret %array_i32 %reloaded 15 }
  • 15. Lets Talk Strings - stringref Clang! Clang! Who's there? WebAssembly! Paulo Matos, October 21, 2022 Strings as GC reference types! ● Need to support GC strings across the toolchain ○ currently focusing on LLVM atm ● Using the same mechanism as other Wasm GC types, where stringref would inherit an AS ID. 1 !0 = !{!"externref", !"stringref", !"array i32"} 2 !wasm.type_info = !{!0} 3 4 %externref = type ptr addrspace(256) 5 %stringref = type ptr addrspace(257) 6 %array_i32 = type ptr addrspace(258)
  • 16. Summary Clang! Clang! Who's there? WebAssembly! Paulo Matos, October 21, 2022 ✅ Reference types in LLVM IR 󰘶 Reference types in Clang (D122215, D128440, D123510, D124162) 󰘶 GC types in LLVM IR (public branch downstream) 󰘶 Stringref in LLVM IR (public branch downstream) 🤔 GC types in Clang 🤔 Stringref in Clang We are hiring! https://www.igalia.com/jobs/