DL-based Compiler Optimizations
-Dibyendu Das
Sr. Principal Engineer, Intel India
Agenda
LLVM Social Bangalore Sep 2021
2
9/18/2021
Brief Background
LLVM Social Bangalore Sep 2021
3
9/18/2021
Program
IR
IR
IR
Optimized program in target language
Optimized Machine Code
Analysis
Transformation
Code Generation
Compiler Optimization Pass
Brief Background (Cont’d)
LLVM Social Bangalore Sep 2021
4
9/18/2021
Brief Background (Cont’d)
LLVM Social Bangalore Sep 2021
5
9/18/2021
* Figure taken from: Machine Learning in Compiler Optimisation, Zheng Wang and Michael O’Boyle
DeepTune- End-to-end Deep Learning of Optimization Heuristics
LLVM Social Bangalore Sep 2021
6
9/18/2021
DeepTune
LLVM Social Bangalore Sep 2021
7
9/18/2021
OpenCL Code
Source Rewriter
Sequence Encoder
Embedding
LSTM
Auxiliary Inputs
Batchnorm
Dense
+
Predicted Device Mapping– {CPU,GPU}
Language Model
DeepTune Components
LLVM Social Bangalore Sep 2021
8
9/18/2021
__kernel void memset_kernel(__global char *mem_d,
short val, int num_bytes) {
const int thread_id = get_global_id(0);
mem_d[thread_id] = val;
}
__kernel void A(__global char *a, short b, int c) {
const int d = get_global_id(0);
a[d] = b;
}
DeepTune Components
LLVM Social Bangalore Sep 2021
9
9/18/2021
Auxiliary Inputs for
GPUs - work-group size and data-size
def init(self, seed: int):
from keras.layers import Input, Embedding, LSTM, Dense
from keras.layers.merge import Concatenate
from keras.layers.normalization import BatchNormalization
from keras.models import Model
np.random.seed(seed)
# Language model. Takes as inputs source code sequences.
code_in = Input(shape=(1024,), dtype="int32", name ="code_in")
x = Embedding(input_dim=atomizer.vocab_size + 1, input_length=1024, output_dim=64,
name="embedding")(code_in)
x = LSTM(64, implementation=1, return_sequences=True, name="lstm_1")(x)
x = LSTM(64, implementation=1, name="lstm_2")(x)
langmodel_out = Dense(2, activation="sigmoid")(x)
# Auxiliary inputs. wgsize and dsize.
auxiliary_inputs = Input(shape=(2,))
x = Concatenate()([auxiliary_inputs, x])
x = BatchNormalization()(x)
x = Dense(32, activation="relu")(x)
out = Dense(2, activation="sigmoid")(x)
self.model = Model(inputs=[auxiliary_inputs, code_in], outputs=[out, langmodel_out])
self.model.compile( optimizer="adam", metrics=['accuracy'], loss=["categorical_crossentropy",
"categorical_crossentropy"], loss_weights=[1., .2])
return self
Visualizing DeepTune
LLVM Social Bangalore Sep 2021
10
9/18/2021
* Figure taken from: End-to-end Deep Learning of Optimization Heuristics. Chris Cummins, Pavlos Petoumenos, Zheng Wang, Hugh Leather
Performance
LLVM Social Bangalore Sep 2021
11
9/18/2021
* Figure taken from: End-to-end Deep Learning of Optimization Heuristics. Chris Cummins, Pavlos Petoumenos, Zheng Wang, Hugh Leather
Ithemal : Basic Block Throughput Prediction
LLVM Social Bangalore Sep 2021
12
9/18/2021
Estimating Basic Block Throughput
LLVM Social Bangalore Sep 2021
13
9/18/2021
High-Level Code
Optimizing Compiler
lea r14, [rbx-0x40]
…
lea rdx, [rbp+0x48]
cmp rdi, rax
lea r14, [rbx-0x40]
…
lea rdx, [rbp+0x48]
cmp rdi, rax
lea r14, [rbx-0x40]
…
lea rdx, [rbp+0x48]
cmp rdi, rax
…
40 cycles
44 cycles
36 cycles
Accurate Modeling of Processor Core is Complex
LLVM Social Bangalore Sep 2021
14
9/18/2021
lea r14, [rbx-0x40]
…
lea rdx, [rbp+0x48]
cmp rdi, rax
K cycles
Ithemal: A Data-driven approach
LLVM Social Bangalore Sep 2021
15
9/18/2021
* Figure taken from: Ithemal: Accurate, Portable and Fast Basic Block Throughput Estimation using Deep Neural Networks, C. Mendis et al.
Ithemal DL model
LLVM Social Bangalore Sep 2021
16
9/18/2021
Layer-1 Instruction-level LSTM
Layer-2 BB-level LSTM
* Figure taken from: Ithemal: Accurate, Portable and Fast Basic Block Throughput Estimation using Deep Neural Networks, C. Mendis et al.
Ithemal Comparative Performance
LLVM Social Bangalore Sep 2021
17
9/18/2021
* Figure taken from: Ithemal: Accurate, Portable and Fast Basic Block Throughput Estimation using Deep Neural Networks, C. Mendis et al.
DL-based Graph Coloring Register Allocation
LLVM Social Bangalore Sep 2021
18
9/18/2021
Graph Coloring
LLVM Social Bangalore Sep 2021
19
9/18/2021
A graph coloring for a graph with 6 vertices. It is impossible to color the graph with 2 colors, so the graph has chromatic number 3.
Register Allocation as a Graph-Coloring Problem
LLVM Social Bangalore Sep 2021
20
9/18/2021
g
k
j
h
Interference Graph of Live Ranges
Live Range
Modeling Graph Coloring using LSTMS
LLVM Social Bangalore Sep 2021
21
9/18/2021
1 1 0 0 …
0 1 0 1 …
1 0 0 1…
Adj Matrix
1024 hidden units per LSTM
Inference and Color-Correction phase
LLVM Social Bangalore Sep 2021
22
9/18/2021
DL-model vs LLVM’s Greedy Register Allocator (GRA)
LLVM Social Bangalore Sep 2021
23
9/18/2021
LSTM-based model
Interference graph
Color/register assignment
Compiler Auto-vectorization using Imitation Learning
LLVM Social Bangalore Sep 2021
24
9/18/2021
Auto-Vectorization
LLVM Social Bangalore Sep 2021
25
9/18/2021
a[1] = b[1] + c[1]
a[2] = b[2] + c[2]
a[3] = b[3] + c[3]
a[4] = b[4] + c[4]
a[5] = b[5] * c[5]
a[1:2] = b[1:2] + c[1:2]
a[3] = b[3] + c[3]
a[4] = b[4] + c[4]
a[5] = b[5] * c[5]
Scalar Code
Vector Code
Auto-Vectorization
LLVM Social Bangalore Sep 2021
26
9/18/2021
a[1] = b[1] + c[1]
a[2] = b[2] + c[2]
a[3] = b[3] + c[3]
a[4] = b[4] + c[4]
a[5] = b[5] * c[5]
a[1] = b[1] + c[1]
a[2] = b[2] + c[2]
a[3] = b[3] + c[3]
a[4:5] = b[4:5] * c[4:5]
Scalar Code
Vector Code
Neural Machine Translation
MDP Formulation
LLVM Social Bangalore Sep 2021
27
9/18/2021
a[1] = b[1] + c[1]
a[2] = b[2] + c[2]
a[3] = b[3] + c[3]
a[4] = b[4] + c[4]
a[5] = b[5] * c[5]
a[1:2] = b[1:2] + c[1:2]
a[3] = b[3] + c[3]
a[4] = b[4] + c[4]
a[5] = b[5] * c[5]
Choose a “valid” action
{a[1],a[2]},{a[2],a[3]},{a[3],a[4]}
State
New State
a[1:2] = b[1:2] + c[1:2]
a[3] = b[3] + c[3]
a[4] = b[4] + c[4]
a[5] = b[5] * c[5]
a[1:2] = b[1:2] + c[1:2]
a[3:4] = b[3:4] + c[3:4]
a[5] = b[5] * c[5]
Choose a “valid” action
{a[3],a[4]}
State
New State
Iterate
Get Reward
{a[1],a[2]}
MDP Formulation – State details
LLVM Social Bangalore Sep 2021
28
9/18/2021
* Figure taken from: Compiler 2.0: How to Modernize Compiler Technology, Saman Amarasinghe et al.
Imitation Learning
LLVM Social Bangalore Sep 2021
29
9/18/2021
Choose a “valid” action
{a[1],a[2]},{a[2],a[3]},{a[3],a[4]}
State
New State
{a[1],a[2]}
GGNN
Related Work
LLVM Social Bangalore Sep 2021
30
9/18/2021
Some other work we did not cover
LLVM Social Bangalore Sep 2021
31
9/18/2021
Wrapping it Up
LLVM Social Bangalore Sep 2021
32
9/18/2021
Conclusion and Future Directions
LLVM Social Bangalore Sep 2021
33
9/18/2021
Conclusion and Future Directions
LLVM Social Bangalore Sep 2021
34
9/18/2021
* Figure taken from: Machine Learning in Compilers: Past, Present and Future, Chris Cummins and Hugh Leather
The Future ?
LLVM Social Bangalore Sep 2021
35
9/18/2021
Program
IR
IR
IR
Optimized program in target language
Optimized Machine Code
Learnt Analysis Model
Learnt Transformation Model
Code Generation
Learnt Compiler Optimization Pass
Thanks!
LLVM Social Bangalore Sep 2021
36
9/18/2021