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
//!
//! This module manages verifying the generated LLVM module, optimizing it using the LLVM
//! optimization passes, and compiling it to machine code.

use libc;
use llvm_sys;
use time;

use std::ffi::{CStr, CString};
use std::mem;
use std::ptr;
use std::sync::Once;

use libc::c_char;

use self::time::PreciseTime;

use crate::conf::ParsedConf;
use crate::error::*;
use crate::util::stats::CompilationStats;

use self::llvm_sys::core::*;
use self::llvm_sys::execution_engine::*;
use self::llvm_sys::prelude::*;
use self::llvm_sys::target::*;
use self::llvm_sys::target_machine::*;

use crate::codegen::Runnable;

use crate::codegen::llvm2::intrinsic;
use crate::codegen::llvm2::llvm_exts::*;

static ONCE: Once = Once::new();
static mut INITIALIZE_FAILED: bool = false;

/// The callable function type.
type I64Func = extern "C" fn(i64) -> i64;

/// A compiled, runnable LLVM module.
pub struct CompiledModule {
    context: LLVMContextRef,
    module: LLVMModuleRef,
    engine: LLVMExecutionEngineRef,
    run_function: I64Func,
}

// The codegen interface requires that modules implement this trait. This allows supporting
// multiple backends via dynamic dispatch.
impl Runnable for CompiledModule {
    fn run(&self, arg: i64) -> i64 {
        (self.run_function)(arg)
    }
}

// LLVM modules are thread-safe.
unsafe impl Send for CompiledModule {}
unsafe impl Sync for CompiledModule {}

impl CompiledModule {
    /// Dumps assembly for this module.
    pub fn asm(&self) -> WeldResult<String> {
        unsafe {
            let mut output_buf = ptr::null_mut();
            let mut err = ptr::null_mut();
            let target = LLVMGetExecutionEngineTargetMachine(self.engine);
            let file_type = LLVMCodeGenFileType::LLVMAssemblyFile;
            let res = LLVMTargetMachineEmitToMemoryBuffer(
                target,
                self.module,
                file_type,
                &mut err,
                &mut output_buf,
            );
            if res == 1 {
                let err_str = CStr::from_ptr(err as *mut c_char)
                    .to_string_lossy()
                    .into_owned();
                libc::free(err as *mut libc::c_void); // err is only allocated if res == 1
                compile_err!("Machine code generation failed with error {}", err_str)
            } else {
                let start = LLVMGetBufferStart(output_buf);
                let c_str = CStr::from_ptr(start as *mut c_char)
                    .to_string_lossy()
                    .into_owned();
                LLVMDisposeMemoryBuffer(output_buf);
                Ok(c_str)
            }
        }
    }

    /// Dumps the optimized LLVM IR for this module.
    pub fn llvm(&self) -> WeldResult<String> {
        unsafe {
            let c_str = LLVMPrintModuleToString(self.module);
            let ir = CStr::from_ptr(c_str)
                .to_str()
                .map_err(|e| WeldCompileError::new(e.to_string()))?;
            let ir = ir.to_string();
            LLVMDisposeMessage(c_str);
            Ok(ir)
        }
    }
}

impl Drop for CompiledModule {
    fn drop(&mut self) {
        unsafe {
            // Engine owns the module, so do not drop it explicitly.
            LLVMDisposeExecutionEngine(self.engine);
            LLVMContextDispose(self.context);
        }
    }
}

pub unsafe fn init() {
    ONCE.call_once(|| initialize());
    if INITIALIZE_FAILED {
        unreachable!()
    }
}

/// Compile a constructed module in the given LLVM context.
pub unsafe fn compile(
    context: LLVMContextRef,
    module: LLVMModuleRef,
    mappings: &[intrinsic::Mapping],
    conf: &ParsedConf,
    stats: &mut CompilationStats,
) -> WeldResult<CompiledModule> {
    init();

    let start = PreciseTime::now();
    verify_module(module)?;
    let end = PreciseTime::now();
    stats
        .llvm_times
        .push(("Module Verification".to_string(), start.to(end)));

    let start = PreciseTime::now();
    optimize_module(module, conf)?;
    let end = PreciseTime::now();
    stats
        .llvm_times
        .push(("Module Optimization".to_string(), start.to(end)));

    let start = PreciseTime::now();
    // Takes ownership of the module.
    let engine = create_exec_engine(module, mappings, conf)?;
    let end = PreciseTime::now();
    stats
        .llvm_times
        .push(("Create Exec Engine".to_string(), start.to(end)));

    let start = PreciseTime::now();
    let run_func = find_function(engine, &conf.llvm.run_func_name)?;
    let end = PreciseTime::now();
    stats
        .llvm_times
        .push(("Find Run Func Address".to_string(), start.to(end)));

    let result = CompiledModule {
        context,
        module,
        engine,
        run_function: run_func,
    };
    Ok(result)
}

/// Initialize LLVM.
///
/// This function should only be called once.
unsafe fn initialize() {
    use self::llvm_sys::target::*;
    if LLVM_InitializeNativeTarget() != 0 {
        INITIALIZE_FAILED = true;
        return;
    }
    if LLVM_InitializeNativeAsmPrinter() != 0 {
        INITIALIZE_FAILED = true;
        return;
    }
    if LLVM_InitializeNativeAsmParser() != 0 {
        INITIALIZE_FAILED = true;
        return;
    }

    // No version that just initializes the current one?
    LLVM_InitializeAllTargetInfos();
    LLVMLinkInMCJIT();

    use self::llvm_sys::initialization::*;

    let registry = LLVMGetGlobalPassRegistry();
    LLVMInitializeCore(registry);
    LLVMInitializeAnalysis(registry);
    LLVMInitializeCodeGen(registry);
    LLVMInitializeIPA(registry);
    LLVMInitializeIPO(registry);
    LLVMInitializeInstrumentation(registry);
    LLVMInitializeObjCARCOpts(registry);
    LLVMInitializeScalarOpts(registry);
    LLVMInitializeTarget(registry);
    LLVMInitializeTransformUtils(registry);
    LLVMInitializeVectorization(registry);
}

unsafe fn target_machine() -> WeldResult<LLVMTargetMachineRef> {
    let mut target = mem::MaybeUninit::uninit();
    let mut err = ptr::null_mut();
    let ret = LLVMGetTargetFromTriple(PROCESS_TRIPLE.as_ptr(), target.as_mut_ptr(), &mut err);
    if ret == 1 {
        let err_msg = CStr::from_ptr(err as *mut c_char)
            .to_string_lossy()
            .into_owned();
        LLVMDisposeMessage(err); // err is only allocated on res == 1
        compile_err!("Target initialization failed with error {}", err_msg)
    } else {
        Ok(LLVMCreateTargetMachine(
            target.assume_init(),
            PROCESS_TRIPLE.as_ptr(),
            HOST_CPU_NAME.as_ptr(),
            HOST_CPU_FEATURES.as_ptr(),
            LLVMCodeGenOptLevel::LLVMCodeGenLevelAggressive,
            LLVMRelocMode::LLVMRelocDefault,
            LLVMCodeModel::LLVMCodeModelDefault,
        ))
    }
}

pub unsafe fn set_triple_and_layout(module: LLVMModuleRef) -> WeldResult<()> {
    LLVMSetTarget(module, PROCESS_TRIPLE.as_ptr() as *const _);
    debug!("Set module target {:?}", PROCESS_TRIPLE.to_str().unwrap());
    let target_machine = target_machine()?;
    let layout = LLVMCreateTargetDataLayout(target_machine);
    LLVMSetModuleDataLayout(module, layout);
    LLVMDisposeTargetMachine(target_machine);
    LLVMDisposeTargetData(layout);
    Ok(())
}

/// Verify a module using LLVM's verifier.
unsafe fn verify_module(module: LLVMModuleRef) -> WeldResult<()> {
    use self::llvm_sys::analysis::LLVMVerifierFailureAction::*;
    use self::llvm_sys::analysis::LLVMVerifyModule;
    let mut error_str = ptr::null_mut();
    let result_code = LLVMVerifyModule(module, LLVMReturnStatusAction, &mut error_str);
    let result = {
        if result_code != 0 {
            let err = CStr::from_ptr(error_str).to_string_lossy().into_owned();
            compile_err!("{}", format!("Module verification failed: {}", err))
        } else {
            Ok(())
        }
    };
    libc::free(error_str as *mut libc::c_void);
    result
}

/// Optimize an LLVM module using a given LLVM optimization level.
///
/// This function is currently modeled after the `AddOptimizationPasses` in the LLVM `opt` tool:
/// https://github.com/llvm-mirror/llvm/blob/master/tools/opt/opt.cpp
unsafe fn optimize_module(module: LLVMModuleRef, conf: &ParsedConf) -> WeldResult<()> {
    info!("Optimizing LLVM module");
    use self::llvm_sys::transforms::pass_manager_builder::*;
    let mpm = LLVMCreatePassManager();
    let fpm = LLVMCreateFunctionPassManagerForModule(module);

    // Target specific analyses so LLVM can query the backend.
    let target_machine = target_machine()?;

    let target = LLVMGetTargetMachineTarget(target_machine);

    // Log some information about the machine...
    let cpu_ptr = LLVMGetTargetMachineCPU(target_machine);
    let cpu = CStr::from_ptr(cpu_ptr).to_str().unwrap();
    let description = CStr::from_ptr(LLVMGetTargetDescription(target))
        .to_str()
        .unwrap();
    let features_ptr = LLVMGetTargetMachineFeatureString(target_machine);
    let features = CStr::from_ptr(features_ptr).to_str().unwrap();

    debug!(
        "CPU: {}, Description: {} Features: {}",
        cpu, description, features
    );
    let start = PreciseTime::now();

    if conf.llvm.target_analysis_passes {
        LLVMExtAddTargetLibraryInfo(mpm);
        LLVMAddAnalysisPasses(target_machine, mpm);
        LLVMExtAddTargetPassConfig(target_machine, mpm);
        LLVMAddAnalysisPasses(target_machine, fpm);
    }

    // Free memory
    libc::free(cpu_ptr as *mut libc::c_void);
    libc::free(features_ptr as *mut libc::c_void);

    // TODO set the size and inliner threshold depending on the optimization level. Right now, we
    // set the inliner to be as aggressive as the -O3 inliner in Clang.
    let builder = LLVMPassManagerBuilderCreate();
    LLVMPassManagerBuilderSetOptLevel(builder, conf.llvm.opt_level);
    LLVMPassManagerBuilderSetSizeLevel(builder, 0);
    LLVMPassManagerBuilderSetDisableUnrollLoops(
        builder,
        if conf.llvm.llvm_unroller { 0 } else { 1 },
    );
    LLVMExtPassManagerBuilderSetDisableVectorize(
        builder,
        if conf.llvm.llvm_vectorizer { 0 } else { 1 },
    );
    // 250 should correspond to OptLevel = 3
    LLVMPassManagerBuilderUseInlinerWithThreshold(builder, 250);

    if conf.llvm.func_optimizations {
        LLVMPassManagerBuilderPopulateFunctionPassManager(builder, fpm);
    }

    if conf.llvm.module_optimizations {
        LLVMPassManagerBuilderPopulateModulePassManager(builder, mpm);
    }

    LLVMPassManagerBuilderDispose(builder);
    let end = PreciseTime::now();
    debug!(
        "LLVM Constructed PassManager in {} ms",
        start.to(end).num_milliseconds()
    );

    let start = PreciseTime::now();
    let mut func = LLVMGetFirstFunction(module);
    while !func.is_null() {
        LLVMRunFunctionPassManager(fpm, func);
        func = LLVMGetNextFunction(func);
    }
    LLVMFinalizeFunctionPassManager(fpm);
    let end = PreciseTime::now();
    debug!(
        "LLVM Function Passes Ran in {} ms",
        start.to(end).num_milliseconds()
    );

    let start = PreciseTime::now();
    LLVMRunPassManager(mpm, module);
    let end = PreciseTime::now();
    debug!(
        "LLVM Module Passes Ran in {} ms",
        start.to(end).num_milliseconds()
    );

    LLVMDisposePassManager(fpm);
    LLVMDisposePassManager(mpm);
    LLVMDisposeTargetMachine(target_machine);

    Ok(())
}

/// Create an MCJIT execution engine for a given module.
unsafe fn create_exec_engine(
    module: LLVMModuleRef,
    mappings: &[intrinsic::Mapping],
    conf: &ParsedConf,
) -> WeldResult<LLVMExecutionEngineRef> {
    // Create a filtered list of globals. Needs to be done before creating the execution engine
    // since we lose ownership of the module. (?)
    let mut globals = vec![];
    for mapping in mappings.iter() {
        let global = LLVMGetNamedFunction(module, mapping.0.as_ptr());
        // The LLVM optimizer can delete globals, so we need this check here!
        if !global.is_null() {
            globals.push((global, mapping.1));
        } else {
            trace!(
                "Function {:?} was deleted from module by optimizer",
                mapping.0
            );
        }
    }

    let mut engine = mem::MaybeUninit::uninit();
    let mut error_str = mem::MaybeUninit::uninit();
    let mut options = mem::MaybeUninit::<LLVMMCJITCompilerOptions>::uninit();
    let options_size = mem::size_of::<LLVMMCJITCompilerOptions>();
    LLVMInitializeMCJITCompilerOptions(options.as_mut_ptr(), options_size);
    let mut options: LLVMMCJITCompilerOptions = options.assume_init();
    options.OptLevel = conf.llvm.opt_level;
    options.CodeModel = LLVMCodeModel::LLVMCodeModelDefault;

    let result_code = LLVMCreateMCJITCompilerForModule(
        engine.as_mut_ptr(),
        module,
        &mut options,
        options_size,
        error_str.as_mut_ptr(),
    );

    if result_code != 0 {
        compile_err!(
            "Creating execution engine failed: {}",
            CStr::from_ptr(error_str.assume_init()).to_str().unwrap()
        )
    } else {
        let engine = engine.assume_init();
        for global in globals {
            LLVMAddGlobalMapping(engine, global.0, global.1);
        }
        Ok(engine)
    }
}

/// Get a pointer to a named function in an execution engine.
unsafe fn find_function(engine: LLVMExecutionEngineRef, name: &str) -> WeldResult<I64Func> {
    let c_name = CString::new(name).unwrap();
    let func_addr = LLVMGetFunctionAddress(engine, c_name.as_ptr());
    if func_addr == 0 {
        return compile_err!("No function named {} in module", name);
    }
    let function: I64Func = mem::transmute(func_addr);
    Ok(function)
}