weld 0.4.0

Weld is a language and runtime for improving the performance of data-intensive applications.
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
//! Defines intrinsics in the LLVM IR.
//!
//! An intrinsic is any function that is defined but not generated in the current module. This
//! module provides utilities for calling some default intrinsics, as well as a utility for adding
//! an calling new ones.
//!
//! For documentation on the default intrinsics, see `weld::runtime::strt`.

use fnv;
use libc;
use llvm_sys;

use fnv::FnvHashMap;

use libc::c_char;

use crate::ast::ScalarKind;
use crate::error::*;

use std::ffi::CString;

use super::llvm_exts::*;

use super::CodeGenExt;
use super::LLVM_VECTOR_WIDTH;

use self::llvm_sys::core::*;
use self::llvm_sys::prelude::*;

use crate::runtime::ffi;
use libc::c_void;

/// A single intrinsic.
#[derive(Debug, Clone)]
pub enum Intrinsic {
    Builtin(LLVMValueRef),
    FunctionPointer(LLVMValueRef, *mut c_void),
}

impl Intrinsic {
    /// Returns the LLVM value for the intrinsic.
    fn value(&self) -> LLVMValueRef {
        match *self {
            Intrinsic::Builtin(val) | Intrinsic::FunctionPointer(val, _) => val,
        }
    }
}

/// A mapping from a function name to its function pointer.
pub type Mapping = (CString, *mut c_void);

/// Intrinsics defined in the code generator.
///
/// An intrinsic is any function that appears without a definition in the generated module. Code
/// generators must ensure that instrinics are properly linked upon compilation.
pub struct Intrinsics {
    context: LLVMContextRef,
    module: LLVMModuleRef,
    intrinsics: FnvHashMap<String, Intrinsic>,
}

impl CodeGenExt for Intrinsics {
    fn context(&self) -> LLVMContextRef {
        self.context
    }

    fn module(&self) -> LLVMModuleRef {
        self.module
    }
}

impl Intrinsics {
    /// Returns value to function pointer mappings for non-builtin intrinsics.
    ///
    /// Builtins are filtered out of this list.
    pub fn mappings(&self) -> Vec<Mapping> {
        let mut mappings = vec![];
        for (name, entry) in self.intrinsics.iter() {
            if let Intrinsic::FunctionPointer(_, ptr) = *entry {
                mappings.push((CString::new(name.as_str()).unwrap(), ptr))
            }
        }
        mappings
    }

    pub unsafe fn defaults(context: LLVMContextRef, module: LLVMModuleRef) -> Intrinsics {
        let mut intrinsics = Intrinsics {
            context,
            module,
            intrinsics: FnvHashMap::default(),
        };

        intrinsics.populate_defaults();
        intrinsics
    }

    /// Returns a string name for a numeric type's LLVM intrinsic.
    pub fn llvm_numeric<T: AsRef<str>>(name: T, kind: ScalarKind, simd: bool) -> String {
        use crate::ast::ScalarKind::*;
        let mut result = format!("llvm.{}.", name.as_ref());
        if simd {
            result.push_str(&format!("v{}", LLVM_VECTOR_WIDTH));
        }

        result.push_str(match kind {
            Bool => "i1",
            I8 => "i32",
            I16 => "i16",
            I32 => "i32",
            I64 => "i64",
            U8 => "i32",
            U16 => "i16",
            U32 => "i32",
            U64 => "i64",
            F32 => "f32",
            F64 => "f64",
        });
        result
    }

    /// Get the intrinsic function value with the given name.
    pub fn get<T: AsRef<str>>(&self, key: T) -> Option<LLVMValueRef> {
        self.intrinsics.get(key.as_ref()).map(|r| r.value())
    }

    /// Add a new intrinsic function with the given name, return type, and argument types.
    ///
    /// Returns true if the function was added or false if it was already registered. The intrinsic
    /// must be builtin or linked.
    pub unsafe fn add<T: AsRef<str>>(
        &mut self,
        name: T,
        ret_ty: LLVMTypeRef,
        arg_tys: &mut [LLVMTypeRef],
    ) -> bool {
        if !self.intrinsics.contains_key(name.as_ref()) {
            let name = CString::new(name.as_ref()).unwrap();
            let fn_type = LLVMFunctionType(ret_ty, arg_tys.as_mut_ptr(), arg_tys.len() as u32, 0);
            let function = LLVMAddFunction(self.module, name.as_ptr(), fn_type);
            self.intrinsics
                .insert(name.into_string().unwrap(), Intrinsic::Builtin(function));
            true
        } else {
            false
        }
    }

    /// Generate code to call an intrinsic function with the given name and arguments.
    ///
    /// If the intrinsic is not defined, this function throws an error.
    pub unsafe fn call<T: AsRef<str>>(
        &mut self,
        builder: LLVMBuilderRef,
        name: T,
        args: &mut [LLVMValueRef],
    ) -> WeldResult<LLVMValueRef> {
        if let Some(func) = self.intrinsics.get(name.as_ref()) {
            let func = func.value();
            if args.len() != LLVMCountParams(func) as usize {
                panic!("Argument length didn't match number of parameters")
            }
            Ok(LLVMBuildCall(
                builder,
                func,
                args.as_mut_ptr(),
                args.len() as u32,
                c_str!(""),
            ))
        } else {
            unreachable!()
        }
    }

    /// Convinience wrapper for calling the `weld_run_init` intrinsic.
    pub unsafe fn call_weld_run_init(
        &mut self,
        builder: LLVMBuilderRef,
        nworkers: LLVMValueRef,
        memlimit: LLVMValueRef,
        name: Option<*const c_char>,
    ) -> LLVMValueRef {
        let mut args = [nworkers, memlimit];
        LLVMBuildCall(
            builder,
            self.get("weld_runst_init").unwrap(),
            args.as_mut_ptr(),
            args.len() as u32,
            name.unwrap_or(c_str!("")),
        )
    }

    /// Convinience wrapper for calling the `weld_run_get_result` intrinsic.
    pub unsafe fn call_weld_run_get_result(
        &mut self,
        builder: LLVMBuilderRef,
        run: LLVMValueRef,
        name: Option<*const c_char>,
    ) -> LLVMValueRef {
        let mut args = [run];
        LLVMBuildCall(
            builder,
            self.get("weld_runst_get_result").unwrap(),
            args.as_mut_ptr(),
            args.len() as u32,
            name.unwrap_or(c_str!("")),
        )
    }

    /// Convinience wrapper for calling the `weld_run_set_result` intrinsic.
    pub unsafe fn call_weld_run_set_result(
        &mut self,
        builder: LLVMBuilderRef,
        run: LLVMValueRef,
        pointer: LLVMValueRef,
        name: Option<*const c_char>,
    ) -> LLVMValueRef {
        let mut args = [run, pointer];
        LLVMBuildCall(
            builder,
            self.get("weld_runst_set_result").unwrap(),
            args.as_mut_ptr(),
            args.len() as u32,
            name.unwrap_or(c_str!("")),
        )
    }

    /// Convinience wrapper for calling the `weld_run_malloc` intrinsic.
    pub unsafe fn call_weld_run_malloc(
        &mut self,
        builder: LLVMBuilderRef,
        run: LLVMValueRef,
        size: LLVMValueRef,
        name: Option<*const c_char>,
    ) -> LLVMValueRef {
        let mut args = [run, size];
        LLVMBuildCall(
            builder,
            self.get("weld_runst_malloc").unwrap(),
            args.as_mut_ptr(),
            args.len() as u32,
            name.unwrap_or(c_str!("")),
        )
    }

    /// Convinience wrapper for calling the `weld_run_remalloc` intrinsic.
    pub unsafe fn call_weld_run_realloc(
        &mut self,
        builder: LLVMBuilderRef,
        run: LLVMValueRef,
        pointer: LLVMValueRef,
        size: LLVMValueRef,
        name: Option<*const c_char>,
    ) -> LLVMValueRef {
        let mut args = [run, pointer, size];
        LLVMBuildCall(
            builder,
            self.get("weld_runst_realloc").unwrap(),
            args.as_mut_ptr(),
            args.len() as u32,
            name.unwrap_or(c_str!("")),
        )
    }

    /// Convinience wrapper for calling the `weld_run_free` intrinsic.
    pub unsafe fn call_weld_run_free(
        &mut self,
        builder: LLVMBuilderRef,
        run: LLVMValueRef,
        pointer: LLVMValueRef,
    ) -> LLVMValueRef {
        let mut args = [run, pointer];
        LLVMBuildCall(
            builder,
            self.get("weld_runst_free").unwrap(),
            args.as_mut_ptr(),
            args.len() as u32,
            c_str!(""),
        )
    }

    /// Convinience wrapper for calling the `weld_run_get_errno` intrinsic.
    pub unsafe fn call_weld_run_get_errno(
        &mut self,
        builder: LLVMBuilderRef,
        run: LLVMValueRef,
        name: Option<*const c_char>,
    ) -> LLVMValueRef {
        let mut args = [run];
        LLVMBuildCall(
            builder,
            self.get("weld_runst_get_errno").unwrap(),
            args.as_mut_ptr(),
            args.len() as u32,
            name.unwrap_or(c_str!("")),
        )
    }

    /// Convinience wrapper for calling the `weld_runst_set_errno` intrinsic.
    pub unsafe fn call_weld_run_set_errno(
        &mut self,
        builder: LLVMBuilderRef,
        run: LLVMValueRef,
        errno: LLVMValueRef,
        name: Option<*const c_char>,
    ) -> LLVMValueRef {
        let mut args = [run, errno];
        LLVMBuildCall(
            builder,
            self.get("weld_runst_set_errno").unwrap(),
            args.as_mut_ptr(),
            args.len() as u32,
            name.unwrap_or(c_str!("")),
        )
    }

    /// Convinience wrapper for calling the `weld_runst_assert` intrinsic.
    pub unsafe fn call_weld_run_assert(
        &mut self,
        builder: LLVMBuilderRef,
        run: LLVMValueRef,
        cond: LLVMValueRef,
        name: Option<*const c_char>,
    ) -> LLVMValueRef {
        let mut args = [run, cond];
        LLVMBuildCall(
            builder,
            self.get("weld_runst_assert").unwrap(),
            args.as_mut_ptr(),
            args.len() as u32,
            name.unwrap_or(c_str!("")),
        )
    }

    /// Convinience wrapper for calling the `weld_run_print` intrinsic.
    pub unsafe fn call_weld_run_print(
        &mut self,
        builder: LLVMBuilderRef,
        run: LLVMValueRef,
        string: LLVMValueRef,
    ) -> LLVMValueRef {
        let mut args = [run, string];
        LLVMBuildCall(
            builder,
            self.get("weld_runst_print").unwrap(),
            args.as_mut_ptr(),
            args.len() as u32,
            c_str!(""),
        )
    }

    /// Convinience wrapper for calling `memcpy`.
    ///
    /// This assumes the `memcpy` is non-volatile and uses an default alignment value of 8.
    pub unsafe fn call_memcpy(
        &mut self,
        builder: LLVMBuilderRef,
        dst: LLVMValueRef,
        src: LLVMValueRef,
        size: LLVMValueRef,
    ) -> LLVMValueRef {
        let mut args = [dst, src, size, self.i32(8), self.i1(false)];
        LLVMBuildCall(
            builder,
            self.get("llvm.memcpy.p0i8.p0i8.i64").unwrap(),
            args.as_mut_ptr(),
            args.len() as u32,
            c_str!(""),
        )
    }

    /// Convinience wrapper for calling `memset` with 0 bytes value.
    ///
    /// This assumes the `memset` is non-volatile.
    pub unsafe fn call_memset_zero(
        &mut self,
        builder: LLVMBuilderRef,
        dst: LLVMValueRef,
        size: LLVMValueRef,
    ) -> LLVMValueRef {
        let mut args = [dst, self.i8(0), size, self.i32(8), self.i1(false)];
        LLVMBuildCall(
            builder,
            self.get("llvm.memset.p0i8.i64").unwrap(),
            args.as_mut_ptr(),
            args.len() as u32,
            c_str!(""),
        )
    }
}

/// Private methods.
impl Intrinsics {
    /// Populate the default intrinsics.
    ///
    /// By default, the code generator adds the Weld Run API (functions prefixed with `weld_run`)
    /// and a few other utility functions, such as `memcpy`.
    unsafe fn populate_defaults(&mut self) {
        use super::llvm_exts::LLVMExtAttribute::*;

        let int8p = LLVMPointerType(self.i8_type(), 0);

        // Defines the default intrinsics used by the Weld runtime.
        let mut params = vec![self.i32_type(), self.i64_type()];
        let name = CString::new("weld_runst_init").unwrap();
        let fn_type = LLVMFunctionType(
            self.run_handle_type(),
            params.as_mut_ptr(),
            params.len() as u32,
            0,
        );
        let function = LLVMAddFunction(self.module, name.as_ptr(), fn_type);
        self.intrinsics.insert(
            name.into_string().unwrap(),
            Intrinsic::FunctionPointer(function, ffi::weld_runst_init as *mut c_void),
        );

        let mut params = vec![self.run_handle_type()];
        let name = CString::new("weld_runst_get_result").unwrap();
        let fn_type = LLVMFunctionType(int8p, params.as_mut_ptr(), params.len() as u32, 0);
        let function = LLVMAddFunction(self.module, name.as_ptr(), fn_type);
        LLVMExtAddAttrsOnFunction(self.context, function, &[NoUnwind]);
        LLVMExtAddAttrsOnParameter(
            self.context,
            function,
            &[NoCapture, NoAlias, NonNull, ReadOnly],
            0,
        );
        self.intrinsics.insert(
            name.into_string().unwrap(),
            Intrinsic::FunctionPointer(function, ffi::weld_runst_get_result as *mut c_void),
        );

        let mut params = vec![self.run_handle_type(), int8p];
        let name = CString::new("weld_runst_set_result").unwrap();
        let fn_type = LLVMFunctionType(
            self.void_type(),
            params.as_mut_ptr(),
            params.len() as u32,
            0,
        );
        let function = LLVMAddFunction(self.module, name.as_ptr(), fn_type);
        LLVMExtAddAttrsOnFunction(self.context, function, &[NoUnwind]);
        LLVMExtAddAttrsOnParameter(self.context, function, &[NoCapture, NoAlias, NonNull], 0);
        self.intrinsics.insert(
            name.into_string().unwrap(),
            Intrinsic::FunctionPointer(function, ffi::weld_runst_set_result as *mut c_void),
        );

        let mut params = vec![self.run_handle_type(), self.i64_type()];
        let name = CString::new("weld_runst_malloc").unwrap();
        let fn_type = LLVMFunctionType(int8p, params.as_mut_ptr(), params.len() as u32, 0);
        let function = LLVMAddFunction(self.module, name.as_ptr(), fn_type);
        LLVMExtAddAttrsOnParameter(self.context, function, &[NoCapture, NoAlias, NonNull], 0);
        LLVMExtAddAttrsOnReturn(self.context, function, &[NoAlias]);
        self.intrinsics.insert(
            name.into_string().unwrap(),
            Intrinsic::FunctionPointer(function, ffi::weld_runst_malloc as *mut c_void),
        );

        let mut params = vec![self.run_handle_type(), int8p, self.i64_type()];
        let name = CString::new("weld_runst_realloc").unwrap();
        let fn_type = LLVMFunctionType(int8p, params.as_mut_ptr(), params.len() as u32, 0);
        let function = LLVMAddFunction(self.module, name.as_ptr(), fn_type);
        LLVMExtAddAttrsOnParameter(self.context, function, &[NoCapture, NoAlias, NonNull], 0);
        LLVMExtAddAttrsOnReturn(self.context, function, &[NoAlias]);
        self.intrinsics.insert(
            name.into_string().unwrap(),
            Intrinsic::FunctionPointer(function, ffi::weld_runst_realloc as *mut c_void),
        );

        let mut params = vec![self.run_handle_type(), int8p];
        let name = CString::new("weld_runst_free").unwrap();
        let fn_type = LLVMFunctionType(
            self.void_type(),
            params.as_mut_ptr(),
            params.len() as u32,
            0,
        );
        let function = LLVMAddFunction(self.module, name.as_ptr(), fn_type);
        LLVMExtAddAttrsOnParameter(self.context, function, &[NoCapture, NoAlias, NonNull], 0);
        self.intrinsics.insert(
            name.into_string().unwrap(),
            Intrinsic::FunctionPointer(function, ffi::weld_runst_free as *mut c_void),
        );

        let mut params = vec![self.run_handle_type()];
        let name = CString::new("weld_runst_get_errno").unwrap();
        let fn_type =
            LLVMFunctionType(self.i64_type(), params.as_mut_ptr(), params.len() as u32, 0);
        let function = LLVMAddFunction(self.module, name.as_ptr(), fn_type);
        LLVMExtAddAttrsOnFunction(self.context, function, &[NoUnwind]);
        LLVMExtAddAttrsOnParameter(
            self.context,
            function,
            &[NoCapture, NoAlias, NonNull, ReadOnly],
            0,
        );
        self.intrinsics.insert(
            name.into_string().unwrap(),
            Intrinsic::FunctionPointer(function, ffi::weld_runst_get_errno as *mut c_void),
        );

        let mut params = vec![self.run_handle_type(), self.i64_type()];
        let name = CString::new("weld_runst_set_errno").unwrap();
        let fn_type = LLVMFunctionType(
            self.void_type(),
            params.as_mut_ptr(),
            params.len() as u32,
            0,
        );
        let function = LLVMAddFunction(self.module, name.as_ptr(), fn_type);
        LLVMExtAddAttrsOnFunction(self.context, function, &[NoReturn]);
        LLVMExtAddAttrsOnParameter(self.context, function, &[NoCapture, NoAlias, NonNull], 0);
        self.intrinsics.insert(
            name.into_string().unwrap(),
            Intrinsic::FunctionPointer(function, ffi::weld_runst_set_errno as *mut c_void),
        );

        let mut params = vec![self.run_handle_type(), self.bool_type()];
        let name = CString::new("weld_runst_assert").unwrap();
        let fn_type = LLVMFunctionType(
            self.bool_type(),
            params.as_mut_ptr(),
            params.len() as u32,
            0,
        );
        let function = LLVMAddFunction(self.module, name.as_ptr(), fn_type);
        LLVMExtAddAttrsOnParameter(self.context, function, &[NoCapture, NoAlias, NonNull], 0);
        self.intrinsics.insert(
            name.into_string().unwrap(),
            Intrinsic::FunctionPointer(function, ffi::weld_runst_assert as *mut c_void),
        );

        let mut params = vec![self.run_handle_type(), int8p];
        let name = CString::new("weld_runst_print").unwrap();
        let fn_type = LLVMFunctionType(
            self.void_type(),
            params.as_mut_ptr(),
            params.len() as u32,
            0,
        );
        let function = LLVMAddFunction(self.module, name.as_ptr(), fn_type);
        LLVMExtAddAttrsOnParameter(
            self.context,
            function,
            &[NoCapture, NoAlias, NonNull, ReadOnly],
            0,
        );
        LLVMExtAddAttrsOnParameter(
            self.context,
            function,
            &[NoCapture, NoAlias, NonNull, ReadOnly],
            1,
        );
        self.intrinsics.insert(
            name.into_string().unwrap(),
            Intrinsic::FunctionPointer(function, ffi::weld_runst_print as *mut c_void),
        );

        let mut params = vec![
            int8p,
            int8p,
            self.i64_type(),
            self.i32_type(),
            self.i1_type(),
        ];
        let name = CString::new("llvm.memcpy.p0i8.p0i8.i64").unwrap();
        let fn_type = LLVMFunctionType(
            self.void_type(),
            params.as_mut_ptr(),
            params.len() as u32,
            0,
        );
        let function = LLVMAddFunction(self.module, name.as_ptr(), fn_type);
        // LLVM sets attributes on `memcpy` automatically.
        self.intrinsics
            .insert(name.into_string().unwrap(), Intrinsic::Builtin(function));

        let mut params = vec![
            int8p,
            self.i8_type(),
            self.i64_type(),
            self.i32_type(),
            self.i1_type(),
        ];
        let name = CString::new("llvm.memset.p0i8.i64").unwrap();
        let fn_type = LLVMFunctionType(
            self.void_type(),
            params.as_mut_ptr(),
            params.len() as u32,
            0,
        );
        let function = LLVMAddFunction(self.module, name.as_ptr(), fn_type);
        // LLVM sets attributes on `memset` automatically.
        self.intrinsics
            .insert(name.into_string().unwrap(), Intrinsic::Builtin(function));
    }
}