1 of 23

Replacing DRuntime Hooks with Templates Across Three SAoCs

Teodor Duțu (Teo)

teodor.dutu@gmail.com

Răzvan Nițu

17 September 2024

1

University POLITEHNICA of Bucharest

Faculty of Automatic Control and Computers

Computer Science and Engineering Department

2 of 23

DRuntime - What?

  • string[string] map;
  • arr[0 .. i - 1] ~ arr[i + 1 .. $];
  • throw new Exception(“I use DRuntime, bro!”);
  • new Class();
  • int[] arr = [1, 2, 3];

2

-betterC

3 of 23

DRuntime - How (High Level)?

3

IR Generator

a ~= b

D code

_d_arrayappendT(a, b)

Compiler

Executable file

_d_arrayctor:

_d_arrayappendT:

_d_arrayctor:

_d_arrayappendT:

_Dmain:

call _d_arrayappendT

Linking

libdruntime.a

4 of 23

Current Approach - Disadvantages

DRuntime - How? Non-Template Hook

4

5 of 23

Hooks - Proposed Approach

5

D code

Semantic Analysis

Compiler

Executable file

Tarr _d_arrayctor(Tarr : T[], T)(...) {...}

Tarr _d_arrayappendT(Tarr : T[], T)(...) {...}

_d_arrayappendT(a, b)

_d_arrayappendT:

_Dmain:

call _d_arrayappendT

Linking

a ~= b

DRuntime - New How (High Level)?

6 of 23

DRuntime - How? Template Hook

6

7 of 23

CommaExp, CommaExp, CommaExp

CommaExps: Good-ish

7

8 of 23

CommaExp, CommaExp, CommaExp

CommaExps: Meh

8

9 of 23

CommaExp, CommaExp, CommaExp

CommaExps: Meh - SAoC 2021

9

10 of 23

CommaExp, CommaExp, CommaExp

CommaExps: Oh Ok…

10

return (

_tmp = _d_newclass!C(),

_tmp.__ctor());

lowering

11 of 23

CommaExp, CommaExp, CommaExp

CommaExps: Oh No…

11

class C

{

int x;

void* vthis;

}

closure

val

C.x

C.vthis

12 of 23

CommaExp, CommaExp, CommaExp

CommaExps: Oh No…

12

lowering

return (

_tmp = _d_newclass!C(),

_tmp.vthis = ...,

_tmp.__ctor());

13 of 23

What to Lower When? (SAoC 2021)

13

new C()

High-level expression:

Semantic Analysis:

IR Generator:

toElem(newExp.lowering)

newExp.lowering = (

_tmp = _d_newclass!C(),

_tmp.vthis = ...,

_tmp.__ctor());

14 of 23

What to Lower When? Previously

14

new C()

High-level expression:

IR Generator:

ex = _d_newclass(TypeInfo_Class(C));

ey = ...; // copy vthis

ez = __ctor();

el_combine(ex, ey, ez);

15 of 23

Split Lowering (SAoC 2022)

15

new C()

High-level expression:

IR Generator:

ex = toElem(newExp.lowering);

ey = ...; // copy vthis

ez = __ctor();

el_combine(ex, ey, ez);

Semantic Analysis:

newExp.lowering = _d_newclass!C();

16 of 23

What to Intet?

What to Interpret? SAoC 2021

16

S[3] a = b

High-level expression:

_d_arrayctor(a, b)

Semantic Analysis:

IR Generator:

_d_arrayctor(a, b)

CTFE:

S[3] a = b

17 of 23

What to Intet?

What to Interpret? SAoC 2022

17

S[3] a = b

High-level expression:

exp.lowering = _d_arrayctor(a, b)

Semantic Analysis:

IR Generator:

toElem(exp.lowering)

CTFE:

S[3] a = b

18 of 23

What to Intet?

DMD Architecture (SAoC 202{1,2})

18

[1 , 2, 3]

High-level expression:

Semantic Analysis:

IR Generator:

[1, 2, 3, 4, 5]

Const Folding + Optimisation:

[5, 6]

[1 , 2, 3] ~ [4, 5]

lowering = ???

_d_arraylitetralTX(12)

_d_arraylitetralTX(8)

19 of 23

What to Intet?

DMD Architecture (SAoC 2023 - TODO)

19

[1 , 2, 3]

High-level expression:

Semantic Analysis:

Lowering Compiler Pass:

[1, 2, 3, 4, 5]

+ tracking

Const Folding + Optimisation:

[5, 6]

[1 , 2, 3] ~ [4, 5]

_d_arrayLiteralTX(20)

Track AST Nodes that need lowerings

IR Generator:

toElem(exp.lowering)

20 of 23

DRuntime Hooks Status

_d_newarrayiT

_d_newarrayT

_d_newarrayU

_d_newarraymTX

_d_newarrayOpT

_d_arrayliteralTX

_d_assocarrayliteralTX

_d_arraysetcapacity

_d_arraysetlengthiT

_d_arraysetlengthT

_d_arrayshrinkfit

_d_interface_cast

_d_isbaseof

_d_isbaseof2

20

SAoC

2023

_d_arrayctor

_d_arraysetctor

_d_arrayassign

_d_arrayassign_l

_d_arrayassign_r

_d_delstruct

_d_newThrowable

_d_arrayappendT

_d_arrayappendcTX

_d_arraycatT

_d_arraycatnTX

_d_newitemiT

_d_newitemT

_d_newitemU

_d_newclass

SAoC

2021

SAoC

2022

21 of 23

Performance Increase - All Array Hooks

21

22 of 23

Compilation Times

22

23 of 23

Takeaways

  • SAoC 2021: Exploration
  • SAoC 2022: Fine Tuning
  • SAoC 2023: Revolution

23