tpt-gpu-compiler 0.1.0

Rust port of TPTIR compiler stack
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
use std::fmt;
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum AddressSpace {
    Global,
    Shared,
    Local,
    Constant,
    Generic,
}
impl fmt::Display for AddressSpace {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match self {
            Self::Global => write!(f, "global"),
            Self::Shared => write!(f, "shared"),
            Self::Local => write!(f, "local"),
            Self::Constant => write!(f, "constant"),
            Self::Generic => write!(f, "generic"),
        }
    }
}
#[derive(Debug, Clone, PartialEq)]
pub enum TypeKind {
    I1,
    I8,
    I16,
    I32,
    I64,
    F16,
    BF16,
    F32,
    F64,
    Index,
    Tensor(Vec<i64>, Box<Type>, AddressSpace),
    Vector(u32, Box<Type>),
    MemRef(Vec<i64>, Box<Type>, AddressSpace),
    Function(Vec<Type>, Vec<Type>),
    None,
}
#[derive(Debug, Clone, PartialEq)]
pub struct Type {
    pub kind: TypeKind,
}
impl Type {
    pub fn primitive(name: &str) -> Self {
        let kind = match name {
            "i1" => TypeKind::I1,
            "i8" => TypeKind::I8,
            "i16" => TypeKind::I16,
            "i32" => TypeKind::I32,
            "i64" => TypeKind::I64,
            "f16" => TypeKind::F16,
            "bf16" => TypeKind::BF16,
            "f32" => TypeKind::F32,
            "f64" => TypeKind::F64,
            "index" => TypeKind::Index,
            _ => TypeKind::None,
        };
        Type { kind }
    }
    pub fn tensor(shape: Vec<i64>, el: Type, addr: AddressSpace) -> Self {
        Type {
            kind: TypeKind::Tensor(shape, Box::new(el), addr),
        }
    }
    pub fn vector(lanes: u32, el: Type) -> Self {
        Type {
            kind: TypeKind::Vector(lanes, Box::new(el)),
        }
    }
    pub fn memref(shape: Vec<i64>, el: Type, addr: AddressSpace) -> Self {
        Type {
            kind: TypeKind::MemRef(shape, Box::new(el), addr),
        }
    }
}
impl fmt::Display for Type {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match &self.kind {
            TypeKind::I1 => write!(f, "i1"),
            TypeKind::I8 => write!(f, "i8"),
            TypeKind::I16 => write!(f, "i16"),
            TypeKind::I32 => write!(f, "i32"),
            TypeKind::I64 => write!(f, "i64"),
            TypeKind::F16 => write!(f, "f16"),
            TypeKind::BF16 => write!(f, "bf16"),
            TypeKind::F32 => write!(f, "f32"),
            TypeKind::F64 => write!(f, "f64"),
            TypeKind::Index => write!(f, "index"),
            TypeKind::Tensor(s, e, addr) => {
                let v: Vec<String> = s
                    .iter()
                    .map(|d| if *d < 0 { "*".into() } else { d.to_string() })
                    .collect();
                write!(f, "tensor<{}x{}", v.join("x"), e)?;
                if *addr != AddressSpace::Global {
                    write!(f, ", {}", addr)?;
                }
                write!(f, ">")
            }
            TypeKind::Vector(l, e) => write!(f, "vector<{}x{}>", l, e),
            TypeKind::MemRef(s, e, addr) => {
                let v: Vec<String> = s
                    .iter()
                    .map(|d| if *d < 0 { "*".into() } else { d.to_string() })
                    .collect();
                write!(f, "memref<{}x{}", v.join("x"), e)?;
                if *addr != AddressSpace::Global {
                    write!(f, ", {}", addr)?;
                }
                write!(f, ">")
            }
            TypeKind::Function(i, o) => {
                let a: Vec<String> = i.iter().map(|t| t.to_string()).collect();
                let b: Vec<String> = o.iter().map(|t| t.to_string()).collect();
                write!(f, "({}) -> ({})", a.join(", "), b.join(", "))
            }
            TypeKind::None => write!(f, "none"),
        }
    }
}
#[derive(Debug, Clone)]
pub enum OpKind {
    Addi,
    Subi,
    Muli,
    Addf,
    Subf,
    Mulf,
    And,
    Or,
    Xor,
    CmpEq,
    CmpLt,
    Load,
    Store,
    Branch,
    Return,
    Constant(String),
    Custom(String),
    Gemm,
    Quantize,
    Dequantize,
    QuantGemm,
    QuantAttention,
}

impl fmt::Display for OpKind {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match self {
            OpKind::Addi => write!(f, "addi"),
            OpKind::Subi => write!(f, "subi"),
            OpKind::Muli => write!(f, "muli"),
            OpKind::Addf => write!(f, "addf"),
            OpKind::Subf => write!(f, "subf"),
            OpKind::Mulf => write!(f, "mulf"),
            OpKind::And => write!(f, "andi"),
            OpKind::Or => write!(f, "ori"),
            OpKind::Xor => write!(f, "xori"),
            OpKind::CmpEq => write!(f, "cmpeq"),
            OpKind::CmpLt => write!(f, "cmplt"),
            OpKind::Load => write!(f, "load"),
            OpKind::Store => write!(f, "store"),
            OpKind::Branch => write!(f, "br"),
            OpKind::Return => write!(f, "return"),
            OpKind::Constant(v) => write!(f, "constant {}", v),
            OpKind::Custom(v) => write!(f, "custom {}", v),
            OpKind::Gemm => write!(f, "gemm"),
            OpKind::Quantize => write!(f, "quantize"),
            OpKind::Dequantize => write!(f, "dequantize"),
            OpKind::QuantGemm => write!(f, "quant_gemm"),
            OpKind::QuantAttention => write!(f, "quant_attention"),
        }
    }
}

/// Parse a minimal TPTIR-style assembly into a Region.
pub fn parse_assembly(source: &str) -> Result<Region, String> {
    let mut region = Region::new();
    let mut block = Block::new("entry");
    for line in source.lines() {
        let line = line.trim();
        if line.is_empty() || line.starts_with(';') || line.starts_with('#') {
            continue;
        }
        if line.starts_with('^') && !block.operations.is_empty() {
            region.blocks.push(std::mem::replace(
                &mut block,
                Block::new(line.strip_prefix('^').unwrap()),
            ));
        }
    }
    if !block.operations.is_empty() || region.blocks.is_empty() {
        region.blocks.push(block);
    }
    Ok(region)
}
#[derive(Debug, Clone)]
pub struct Value {
    pub id: u64,
    pub typ: Type,
}
impl Value {
    pub fn new(id: u64, typ: Type) -> Self {
        Value { id, typ }
    }
}
#[derive(Debug, Clone)]
pub struct Operation {
    pub kind: OpKind,
    pub operands: Vec<Value>,
    pub result_type: Option<Type>,
    pub result_id: Option<u64>,
}
impl Operation {
    pub fn new(kind: OpKind) -> Self {
        Operation {
            kind,
            operands: vec![],
            result_type: None,
            result_id: None,
        }
    }
    pub fn display(&self) -> String {
        match &self.kind {
            OpKind::Addi => "addi",
            OpKind::Subi => "subi",
            OpKind::Muli => "muli",
            OpKind::Addf => "addf",
            OpKind::Subf => "subf",
            OpKind::Mulf => "mulf",
            OpKind::And => "andi",
            OpKind::Or => "ori",
            OpKind::Xor => "xori",
            OpKind::CmpEq => "cmpeq",
            OpKind::CmpLt => "cmplt",
            OpKind::Load => "load",
            OpKind::Store => "store",
            OpKind::Branch => "br",
            OpKind::Return => "return",
            OpKind::Constant(v) => return format!("constant {}", v),
            OpKind::Gemm => "gemm",
            OpKind::Quantize => "quantize",
            OpKind::Dequantize => "dequantize",
            OpKind::QuantGemm => "quant_gemm",
            OpKind::QuantAttention => "quant_attention",
            _ => "custom",
        }
        .to_string()
    }
}
#[derive(Debug, Clone)]
pub struct Block {
    pub label: String,
    pub operations: Vec<Operation>,
    pub arguments: Vec<Value>,
}
impl Block {
    pub fn new(label: &str) -> Self {
        Block {
            label: label.to_string(),
            operations: vec![],
            arguments: vec![],
        }
    }
}
#[derive(Debug, Clone)]
pub struct Region {
    pub blocks: Vec<Block>,
}
impl Default for Region {
    fn default() -> Self {
        Self::new()
    }
}
impl Region {
    pub fn new() -> Self {
        Region { blocks: vec![] }
    }
}
impl fmt::Display for Region {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        for b in &self.blocks {
            writeln!(f, "^{}: ", b.label)?;
            for op in &b.operations {
                writeln!(f, "  {}", op.display())?;
            }
        }
        Ok(())
    }
}

/// Supported element types for kernel templates.
#[derive(Debug, Clone, Copy, PartialEq, Hash, Eq)]
pub enum ElemType {
    F32,
    F16,
    BF16,
    I32,
}

impl ElemType {
    pub fn name(self) -> &'static str {
        match self {
            ElemType::F32 => "f32",
            ElemType::F16 => "f16",
            ElemType::BF16 => "bf16",
            ElemType::I32 => "i32",
        }
    }
    pub fn parse(s: &str) -> Option<Self> {
        match s {
            "f32" => Some(ElemType::F32),
            "f16" => Some(ElemType::F16),
            "bf16" => Some(ElemType::BF16),
            "i32" => Some(ElemType::I32),
            _ => None,
        }
    }
    pub fn zero(self) -> String {
        match self {
            ElemType::F32 => "0.0".to_string(),
            ElemType::F16 => "0.0".to_string(),
            ElemType::BF16 => "0.0".to_string(),
            ElemType::I32 => "0".to_string(),
        }
    }
}

/// A kernel variant: template name + element type + shape parameters.
#[derive(Debug, Clone, PartialEq, Hash, Eq)]
pub struct KernelVariant {
    pub name: String,
    pub elem: ElemType,
    pub shape_params: Vec<i64>,
}

impl KernelVariant {
    pub fn dispatch_key(&self) -> String {
        let ss = self
            .shape_params
            .iter()
            .map(|s| s.to_string())
            .collect::<Vec<_>>()
            .join("x");
        format!("{}_{}_{}", self.name, self.elem.name(), ss)
    }
}

/// Generated variant record: the variant metadata and its TPTIR text.
#[derive(Debug, Clone)]
pub struct GeneratedKernel {
    pub variant: KernelVariant,
    pub tptir_text: String,
    pub entry_name: String,
}

/// TPTIR assembly operation kinds produced by the TPTIR text emitter.
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum TptirOp {
    Addi,
    Subi,
    Muli,
    Addf,
    Subf,
    Mulf,
    And,
    Or,
    Xor,
    CmpEq,
    CmpLt,
    Load,
    Store,
    Branch,
    Return,
    Constant,
    Custom,
    Gemm,
    Quantize,
    Dequantize,
    QuantGemm,
    QuantAttention,
}

impl TptirOp {
    pub fn name(self) -> &'static str {
        match self {
            TptirOp::Addi => "addi",
            TptirOp::Subi => "subi",
            TptirOp::Muli => "muli",
            TptirOp::Addf => "addf",
            TptirOp::Subf => "subf",
            TptirOp::Mulf => "mulf",
            TptirOp::And => "andi",
            TptirOp::Or => "ori",
            TptirOp::Xor => "xori",
            TptirOp::CmpEq => "cmpeq",
            TptirOp::CmpLt => "cmplt",
            TptirOp::Load => "load",
            TptirOp::Store => "store",
            TptirOp::Branch => "br",
            TptirOp::Return => "return",
            TptirOp::Constant => "constant",
            TptirOp::Custom => "custom",
            TptirOp::Gemm => "gemm",
            TptirOp::Quantize => "quantize",
            TptirOp::Dequantize => "dequantize",
            TptirOp::QuantGemm => "quant_gemm",
            TptirOp::QuantAttention => "quant_attention",
        }
    }
}

/// Map a Rust-IR `OpKind` to its TPTIR textual operation name.
pub fn opkind_to_tptir(op: &OpKind) -> TptirOp {
    match op {
        OpKind::Addi => TptirOp::Addi,
        OpKind::Subi => TptirOp::Subi,
        OpKind::Muli => TptirOp::Muli,
        OpKind::Addf => TptirOp::Addf,
        OpKind::Subf => TptirOp::Subf,
        OpKind::Mulf => TptirOp::Mulf,
        OpKind::And => TptirOp::And,
        OpKind::Or => TptirOp::Or,
        OpKind::Xor => TptirOp::Xor,
        OpKind::CmpEq => TptirOp::CmpEq,
        OpKind::CmpLt => TptirOp::CmpLt,
        OpKind::Load => TptirOp::Load,
        OpKind::Store => TptirOp::Store,
        OpKind::Branch => TptirOp::Branch,
        OpKind::Return => TptirOp::Return,
        OpKind::Constant(_) => TptirOp::Constant,
        OpKind::Custom(_) => TptirOp::Custom,
        OpKind::Gemm => TptirOp::Gemm,
        OpKind::Quantize => TptirOp::Quantize,
        OpKind::Dequantize => TptirOp::Dequantize,
        OpKind::QuantGemm => TptirOp::QuantGemm,
        OpKind::QuantAttention => TptirOp::QuantAttention,
    }
}

/// Canonical set of supported kernel template names.
pub const KERNEL_TEMPLATES: &[&str] = &[
    "vector_add",
    "matmul",
    "softmax",
    "flash_attention",
    "conv_bn_relu",
    "conv3d",
    "layer_norm",
    "batch_norm",
    "group_norm",
];

/// Emit TPTIR textual assembly from a Region (spec v1.0 section 7.1).
pub fn emit_tptir(region: &Region, entry_name: &str, kernel_attrs: &[(String, String)]) -> String {
    let mut out = String::new();
    out.push_str(&format!("module {{\n  func.func @{}", entry_name));
    if let Some(entry) = region.blocks.first() {
        if !entry.arguments.is_empty() {
            let args: Vec<String> = entry
                .arguments
                .iter()
                .map(|v| format!("%{}: {}", v.id, v.typ))
                .collect();
            out.push_str(&format!("({})", args.join(", ")));
        }
    }
    out.push_str(" attributes {tptir.kernel");
    for (k, v) in kernel_attrs {
        out.push_str(&format!(", {} = {}", k, v));
    }
    out.push_str("} {\n");
    for block in &region.blocks {
        out.push_str(&format!("    ^{}:\n", block.label));
        for op in &block.operations {
            let lhs = match op.result_id {
                Some(id) => format!("%{} = ", id),
                None => String::new(),
            };
            let oo = op
                .operands
                .iter()
                .map(|v| format!("%{}", v.id))
                .collect::<Vec<_>>()
                .join(", ");
            let to = opkind_to_tptir(&op.kind);
            let oper_str = match to {
                TptirOp::Addi
                | TptirOp::Subi
                | TptirOp::Muli
                | TptirOp::Addf
                | TptirOp::Subf
                | TptirOp::Mulf
                | TptirOp::And
                | TptirOp::Or
                | TptirOp::Xor => {
                    let ty = op
                        .result_type
                        .as_ref()
                        .map(|t| t.to_string())
                        .unwrap_or_default();
                    format!("tptir.{}({}) : ({}) -> {}", to.name(), oo, "i32", ty)
                }
                TptirOp::CmpEq | TptirOp::CmpLt => {
                    format!("tptir.{}({}) : (i32, i32) -> i1", to.name(), oo)
                }
                TptirOp::Load => format!("tptir.load({})", oo),
                TptirOp::Store => format!("tptir.store({})", oo),
                TptirOp::Branch => format!("tptir.br {}", oo),
                TptirOp::Return => String::from("tptir.return"),
                TptirOp::Constant => {
                    let val = match &op.kind {
                        OpKind::Constant(v) => v.clone(),
                        _ => String::from("0"),
                    };
                    let ty = op
                        .result_type
                        .as_ref()
                        .map(|t| t.to_string())
                        .unwrap_or_default();
                    format!("tptir.constant {} : {}", val, ty)
                }
                TptirOp::Custom => String::from(""),
                TptirOp::Gemm
                | TptirOp::Quantize
                | TptirOp::Dequantize
                | TptirOp::QuantGemm
                | TptirOp::QuantAttention => {
                    let ty = op
                        .result_type
                        .as_ref()
                        .map(|t| t.to_string())
                        .unwrap_or_default();
                    if ty.is_empty() {
                        format!("tptir.{}({})", to.name(), oo)
                    } else {
                        format!("tptir.{}({}) : {}", to.name(), oo, ty)
                    }
                }
            };
            if !oper_str.is_empty() {
                out.push_str(&format!("      {}{}\n", lhs, oper_str));
            }
        }
    }
    out.push_str("  }\n}\n");
    out
}

/// Build a single-block `Region` from a high-level kernel description.
pub fn build_kernel_region(
    name: &str,
    elem: ElemType,
    _shape_params: &[i64],
) -> Result<Region, String> {
    let mut region = Region::new();
    let mut block = Block::new("entry");
    block.arguments.push(Value::new(
        0,
        Type::memref(vec![-1], Type::primitive(elem.name()), AddressSpace::Global),
    ));
    block.arguments.push(Value::new(
        1,
        Type::memref(vec![-1], Type::primitive(elem.name()), AddressSpace::Global),
    ));
    block.arguments.push(Value::new(
        2,
        Type::memref(vec![-1], Type::primitive(elem.name()), AddressSpace::Global),
    ));
    block.arguments.push(Value::new(3, Type::primitive("i32")));
    match name {
        "vector_add" => {
            let mut op = Operation::new(OpKind::Addf);
            op.operands.push(block.arguments[0].clone());
            op.operands.push(block.arguments[1].clone());
            op.result_type = Some(Type::primitive(elem.name()));
            op.result_id = Some(10);
            block.operations.push(op);
            let mut ret = Operation::new(OpKind::Return);
            ret.operands
                .push(Value::new(10, Type::primitive(elem.name())));
            block.operations.push(ret);
        }
        "softmax" | "flash_attention" | "conv_bn_relu" | "conv3d" | "layer_norm" | "batch_norm"
        | "group_norm" => {
            let mut op = Operation::new(OpKind::Mulf);
            op.operands.push(block.arguments[0].clone());
            op.operands.push(block.arguments[1].clone());
            op.result_type = Some(Type::primitive(elem.name()));
            op.result_id = Some(10);
            block.operations.push(op);
            let mut ret = Operation::new(OpKind::Return);
            ret.operands
                .push(Value::new(10, Type::primitive(elem.name())));
            block.operations.push(ret);
        }
        "matmul" => {
            let mut op = Operation::new(OpKind::Mulf);
            op.operands.push(block.arguments[0].clone());
            op.operands.push(block.arguments[1].clone());
            op.result_type = Some(Type::primitive(elem.name()));
            op.result_id = Some(10);
            block.operations.push(op);
            let mut acc = Operation::new(OpKind::Addf);
            acc.operands
                .push(Value::new(10, Type::primitive(elem.name())));
            acc.operands.push(block.arguments[2].clone());
            acc.result_type = Some(Type::primitive(elem.name()));
            acc.result_id = Some(11);
            block.operations.push(acc);
            let mut ret = Operation::new(OpKind::Return);
            ret.operands
                .push(Value::new(11, Type::primitive(elem.name())));
            block.operations.push(ret);
        }
        _ => return Err(format!("Unknown kernel template: {}", name)),
    }
    region.blocks.push(block);
    Ok(region)
}