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
DRuntime - What?
2
-betterC
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
Current Approach - Disadvantages
DRuntime - How? Non-Template Hook
4
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)?
DRuntime - How? Template Hook
6
CommaExp, CommaExp, CommaExp
CommaExps: Good-ish
7
CommaExp, CommaExp, CommaExp
CommaExps: Meh
8
CommaExp, CommaExp, CommaExp
CommaExps: Meh - SAoC 2021
9
CommaExp, CommaExp, CommaExp
CommaExps: Oh Ok…
10
return (
_tmp = _d_newclass!C(),
_tmp.__ctor());
lowering
CommaExp, CommaExp, CommaExp
CommaExps: Oh No…
11
class C
{
int x;
void* vthis;
}
closure
val
C.x
C.vthis
CommaExp, CommaExp, CommaExp
CommaExps: Oh No…
12
lowering
return (
_tmp = _d_newclass!C(),
_tmp.vthis = ...,
_tmp.__ctor());
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());
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);
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();
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
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
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)
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)
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
Performance Increase - All Array Hooks
21
Compilation Times
22
Takeaways
23