3e02ad799075806b1edbdc0144af6c6a062a85a5
[kazan.git] / shader-compiler-llvm-7 / src / backend.rs
1 // SPDX-License-Identifier: LGPL-2.1-or-later
2 // Copyright 2018 Jacob Lifshay
3 use llvm;
4 use shader_compiler::backend;
5 use std::cell::RefCell;
6 use std::collections::HashMap;
7 use std::collections::HashSet;
8 use std::ffi::{CStr, CString};
9 use std::fmt;
10 use std::hash::Hash;
11 use std::mem;
12 use std::mem::ManuallyDrop;
13 use std::ops::Deref;
14 use std::os::raw::{c_char, c_uint};
15 use std::ptr::null_mut;
16 use std::ptr::NonNull;
17 use std::sync::{Once, ONCE_INIT};
18
19 fn to_bool(v: llvm::LLVMBool) -> bool {
20 v != 0
21 }
22
23 #[derive(Clone)]
24 pub struct LLVM7CompilerConfig {
25 pub variable_vector_length_multiplier: u32,
26 pub optimization_mode: backend::OptimizationMode,
27 }
28
29 impl Default for LLVM7CompilerConfig {
30 fn default() -> Self {
31 backend::CompilerIndependentConfig::default().into()
32 }
33 }
34
35 impl From<backend::CompilerIndependentConfig> for LLVM7CompilerConfig {
36 fn from(v: backend::CompilerIndependentConfig) -> Self {
37 let backend::CompilerIndependentConfig { optimization_mode } = v;
38 Self {
39 variable_vector_length_multiplier: 1,
40 optimization_mode,
41 }
42 }
43 }
44
45 #[repr(transparent)]
46 struct LLVM7String(NonNull<c_char>);
47
48 impl Drop for LLVM7String {
49 fn drop(&mut self) {
50 unsafe {
51 llvm::LLVMDisposeMessage(self.0.as_ptr());
52 }
53 }
54 }
55
56 impl Deref for LLVM7String {
57 type Target = CStr;
58 fn deref(&self) -> &CStr {
59 unsafe { CStr::from_ptr(self.0.as_ptr()) }
60 }
61 }
62
63 impl Clone for LLVM7String {
64 fn clone(&self) -> Self {
65 Self::new(self)
66 }
67 }
68
69 impl LLVM7String {
70 fn new(v: &CStr) -> Self {
71 unsafe { Self::from_ptr(llvm::LLVMCreateMessage(v.as_ptr())).unwrap() }
72 }
73 unsafe fn from_nonnull(v: NonNull<c_char>) -> Self {
74 LLVM7String(v)
75 }
76 unsafe fn from_ptr(v: *mut c_char) -> Option<Self> {
77 NonNull::new(v).map(|v| Self::from_nonnull(v))
78 }
79 }
80
81 impl fmt::Debug for LLVM7String {
82 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
83 (**self).fmt(f)
84 }
85 }
86
87 #[derive(Clone, Eq, PartialEq, Hash)]
88 #[repr(transparent)]
89 pub struct LLVM7Type(llvm::LLVMTypeRef);
90
91 impl fmt::Debug for LLVM7Type {
92 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
93 unsafe {
94 let string =
95 LLVM7String::from_ptr(llvm::LLVMPrintTypeToString(self.0)).ok_or(fmt::Error)?;
96 f.write_str(&string.to_string_lossy())
97 }
98 }
99 }
100
101 impl<'a> backend::types::Type<'a> for LLVM7Type {
102 type Context = LLVM7Context;
103 }
104
105 pub struct LLVM7TypeBuilder {
106 context: llvm::LLVMContextRef,
107 variable_vector_length_multiplier: u32,
108 }
109
110 impl<'a> backend::types::TypeBuilder<'a, LLVM7Type> for LLVM7TypeBuilder {
111 fn build_bool(&self) -> LLVM7Type {
112 unsafe { LLVM7Type(llvm::LLVMInt1TypeInContext(self.context)) }
113 }
114 fn build_i8(&self) -> LLVM7Type {
115 unsafe { LLVM7Type(llvm::LLVMInt8TypeInContext(self.context)) }
116 }
117 fn build_i16(&self) -> LLVM7Type {
118 unsafe { LLVM7Type(llvm::LLVMInt16TypeInContext(self.context)) }
119 }
120 fn build_i32(&self) -> LLVM7Type {
121 unsafe { LLVM7Type(llvm::LLVMInt32TypeInContext(self.context)) }
122 }
123 fn build_i64(&self) -> LLVM7Type {
124 unsafe { LLVM7Type(llvm::LLVMInt64TypeInContext(self.context)) }
125 }
126 fn build_f32(&self) -> LLVM7Type {
127 unsafe { LLVM7Type(llvm::LLVMFloatTypeInContext(self.context)) }
128 }
129 fn build_f64(&self) -> LLVM7Type {
130 unsafe { LLVM7Type(llvm::LLVMDoubleTypeInContext(self.context)) }
131 }
132 fn build_pointer(&self, target: LLVM7Type) -> LLVM7Type {
133 unsafe { LLVM7Type(llvm::LLVMPointerType(target.0, 0)) }
134 }
135 fn build_array(&self, element: LLVM7Type, count: usize) -> LLVM7Type {
136 assert_eq!(count as u32 as usize, count);
137 unsafe { LLVM7Type(llvm::LLVMArrayType(element.0, count as u32)) }
138 }
139 fn build_vector(&self, element: LLVM7Type, length: backend::types::VectorLength) -> LLVM7Type {
140 use self::backend::types::VectorLength::*;
141 let length = match length {
142 Fixed { length } => length,
143 Variable { base_length } => base_length
144 .checked_mul(self.variable_vector_length_multiplier)
145 .unwrap(),
146 };
147 assert_ne!(length, 0);
148 unsafe { LLVM7Type(llvm::LLVMVectorType(element.0, length)) }
149 }
150 fn build_struct(&self, members: &[LLVM7Type]) -> LLVM7Type {
151 assert_eq!(members.len() as c_uint as usize, members.len());
152 unsafe {
153 LLVM7Type(llvm::LLVMStructTypeInContext(
154 self.context,
155 members.as_ptr() as *mut llvm::LLVMTypeRef,
156 members.len() as c_uint,
157 false as llvm::LLVMBool,
158 ))
159 }
160 }
161 fn build_function(&self, arguments: &[LLVM7Type], return_type: Option<LLVM7Type>) -> LLVM7Type {
162 assert_eq!(arguments.len() as c_uint as usize, arguments.len());
163 unsafe {
164 LLVM7Type(llvm::LLVMFunctionType(
165 return_type
166 .unwrap_or_else(|| LLVM7Type(llvm::LLVMVoidTypeInContext(self.context)))
167 .0,
168 arguments.as_ptr() as *mut llvm::LLVMTypeRef,
169 arguments.len() as c_uint,
170 false as llvm::LLVMBool,
171 ))
172 }
173 }
174 }
175
176 #[derive(Clone)]
177 #[repr(transparent)]
178 pub struct LLVM7Value(llvm::LLVMValueRef);
179
180 impl fmt::Debug for LLVM7Value {
181 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
182 unsafe {
183 let string =
184 LLVM7String::from_ptr(llvm::LLVMPrintValueToString(self.0)).ok_or(fmt::Error)?;
185 f.write_str(&string.to_string_lossy())
186 }
187 }
188 }
189
190 impl<'a> backend::Value<'a> for LLVM7Value {
191 type Context = LLVM7Context;
192 }
193
194 #[derive(Clone)]
195 #[repr(transparent)]
196 pub struct LLVM7BasicBlock(llvm::LLVMBasicBlockRef);
197
198 impl fmt::Debug for LLVM7BasicBlock {
199 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
200 use self::backend::BasicBlock;
201 unsafe {
202 let string = LLVM7String::from_ptr(llvm::LLVMPrintValueToString(self.as_value().0))
203 .ok_or(fmt::Error)?;
204 f.write_str(&string.to_string_lossy())
205 }
206 }
207 }
208
209 impl<'a> backend::BasicBlock<'a> for LLVM7BasicBlock {
210 type Context = LLVM7Context;
211 fn as_value(&self) -> LLVM7Value {
212 unsafe { LLVM7Value(llvm::LLVMBasicBlockAsValue(self.0)) }
213 }
214 }
215
216 impl<'a> backend::BuildableBasicBlock<'a> for LLVM7BasicBlock {
217 type Context = LLVM7Context;
218 fn as_basic_block(&self) -> LLVM7BasicBlock {
219 self.clone()
220 }
221 }
222
223 pub struct LLVM7Function {
224 context: llvm::LLVMContextRef,
225 function: llvm::LLVMValueRef,
226 }
227
228 impl fmt::Debug for LLVM7Function {
229 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
230 unsafe {
231 let string = LLVM7String::from_ptr(llvm::LLVMPrintValueToString(self.function))
232 .ok_or(fmt::Error)?;
233 f.write_str(&string.to_string_lossy())
234 }
235 }
236 }
237
238 impl<'a> backend::Function<'a> for LLVM7Function {
239 type Context = LLVM7Context;
240 fn as_value(&self) -> LLVM7Value {
241 LLVM7Value(self.function)
242 }
243 fn append_new_basic_block(&mut self, name: Option<&str>) -> LLVM7BasicBlock {
244 let name = CString::new(name.unwrap_or("")).unwrap();
245 unsafe {
246 LLVM7BasicBlock(llvm::LLVMAppendBasicBlockInContext(
247 self.context,
248 self.function,
249 name.as_ptr(),
250 ))
251 }
252 }
253 }
254
255 pub struct LLVM7Context {
256 context: Option<ManuallyDrop<OwnedContext>>,
257 modules: ManuallyDrop<RefCell<Vec<OwnedModule>>>,
258 config: LLVM7CompilerConfig,
259 }
260
261 impl Drop for LLVM7Context {
262 fn drop(&mut self) {
263 unsafe {
264 ManuallyDrop::drop(&mut self.modules);
265 if let Some(context) = &mut self.context {
266 ManuallyDrop::drop(context);
267 }
268 }
269 }
270 }
271
272 impl<'a> backend::Context<'a> for LLVM7Context {
273 type Value = LLVM7Value;
274 type BasicBlock = LLVM7BasicBlock;
275 type BuildableBasicBlock = LLVM7BasicBlock;
276 type Function = LLVM7Function;
277 type Type = LLVM7Type;
278 type TypeBuilder = LLVM7TypeBuilder;
279 type Module = LLVM7Module;
280 type VerifiedModule = LLVM7Module;
281 type AttachedBuilder = LLVM7Builder;
282 type DetachedBuilder = LLVM7Builder;
283 fn create_module(&self, name: &str) -> LLVM7Module {
284 let name = CString::new(name).unwrap();
285 let mut modules = self.modules.borrow_mut();
286 unsafe {
287 let module = OwnedModule(llvm::LLVMModuleCreateWithNameInContext(
288 name.as_ptr(),
289 self.context.as_ref().unwrap().0,
290 ));
291 let module_ref = module.0;
292 modules.push(module);
293 LLVM7Module {
294 context: self.context.as_ref().unwrap().0,
295 module: module_ref,
296 name_set: HashSet::new(),
297 }
298 }
299 }
300 fn create_builder(&self) -> LLVM7Builder {
301 unsafe {
302 LLVM7Builder(llvm::LLVMCreateBuilderInContext(
303 self.context.as_ref().unwrap().0,
304 ))
305 }
306 }
307 fn create_type_builder(&self) -> LLVM7TypeBuilder {
308 LLVM7TypeBuilder {
309 context: self.context.as_ref().unwrap().0,
310 variable_vector_length_multiplier: self.config.variable_vector_length_multiplier,
311 }
312 }
313 }
314
315 #[repr(transparent)]
316 pub struct LLVM7Builder(llvm::LLVMBuilderRef);
317
318 impl Drop for LLVM7Builder {
319 fn drop(&mut self) {
320 unsafe {
321 llvm::LLVMDisposeBuilder(self.0);
322 }
323 }
324 }
325
326 impl<'a> backend::AttachedBuilder<'a> for LLVM7Builder {
327 type Context = LLVM7Context;
328 fn current_basic_block(&self) -> LLVM7BasicBlock {
329 unsafe { LLVM7BasicBlock(llvm::LLVMGetInsertBlock(self.0)) }
330 }
331 fn build_return(self, value: Option<LLVM7Value>) -> LLVM7Builder {
332 unsafe {
333 match value {
334 Some(value) => llvm::LLVMBuildRet(self.0, value.0),
335 None => llvm::LLVMBuildRetVoid(self.0),
336 };
337 llvm::LLVMClearInsertionPosition(self.0);
338 }
339 self
340 }
341 }
342
343 impl<'a> backend::DetachedBuilder<'a> for LLVM7Builder {
344 type Context = LLVM7Context;
345 fn attach(self, basic_block: LLVM7BasicBlock) -> LLVM7Builder {
346 unsafe {
347 llvm::LLVMPositionBuilderAtEnd(self.0, basic_block.0);
348 }
349 self
350 }
351 }
352
353 struct OwnedModule(llvm::LLVMModuleRef);
354
355 impl Drop for OwnedModule {
356 fn drop(&mut self) {
357 unsafe {
358 llvm::LLVMDisposeModule(self.0);
359 }
360 }
361 }
362
363 impl OwnedModule {
364 unsafe fn take(mut self) -> llvm::LLVMModuleRef {
365 let retval = self.0;
366 self.0 = null_mut();
367 retval
368 }
369 }
370
371 struct OwnedContext(llvm::LLVMContextRef);
372
373 impl Drop for OwnedContext {
374 fn drop(&mut self) {
375 unsafe {
376 llvm::LLVMContextDispose(self.0);
377 }
378 }
379 }
380
381 pub struct LLVM7Module {
382 context: llvm::LLVMContextRef,
383 module: llvm::LLVMModuleRef,
384 name_set: HashSet<String>,
385 }
386
387 impl fmt::Debug for LLVM7Module {
388 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
389 unsafe {
390 let string = LLVM7String::from_ptr(llvm::LLVMPrintModuleToString(self.module))
391 .ok_or(fmt::Error)?;
392 f.write_str(&string.to_string_lossy())
393 }
394 }
395 }
396
397 impl<'a> backend::Module<'a> for LLVM7Module {
398 type Context = LLVM7Context;
399 fn set_source_file_name(&mut self, source_file_name: &str) {
400 unsafe {
401 llvm::LLVMSetSourceFileName(
402 self.module,
403 source_file_name.as_ptr() as *const c_char,
404 source_file_name.len(),
405 )
406 }
407 }
408 fn add_function(&mut self, name: &str, ty: LLVM7Type) -> LLVM7Function {
409 fn is_start_char(c: char) -> bool {
410 if c.is_ascii_alphabetic() {
411 true
412 } else {
413 match c {
414 '_' | '.' | '$' | '-' => true,
415 _ => false,
416 }
417 }
418 }
419 fn is_continue_char(c: char) -> bool {
420 is_start_char(c) || c.is_ascii_digit()
421 }
422 assert!(is_start_char(name.chars().next().unwrap()));
423 assert!(name.chars().all(is_continue_char));
424 assert!(self.name_set.insert(name.into()));
425 let name = CString::new(name).unwrap();
426 unsafe {
427 LLVM7Function {
428 context: self.context,
429 function: llvm::LLVMAddFunction(self.module, name.as_ptr(), ty.0),
430 }
431 }
432 }
433 fn verify(self) -> Result<LLVM7Module, backend::VerificationFailure<'a, LLVM7Module>> {
434 unsafe {
435 let mut message = null_mut();
436 let broken = to_bool(llvm::LLVMVerifyModule(
437 self.module,
438 llvm::LLVMReturnStatusAction,
439 &mut message,
440 ));
441 if broken {
442 let message = LLVM7String::from_ptr(message).unwrap();
443 let message = message.to_string_lossy();
444 Err(backend::VerificationFailure::new(self, message.as_ref()))
445 } else {
446 Ok(self)
447 }
448 }
449 }
450 unsafe fn to_verified_module_unchecked(self) -> LLVM7Module {
451 self
452 }
453 }
454
455 impl<'a> backend::VerifiedModule<'a> for LLVM7Module {
456 type Context = LLVM7Context;
457 fn into_module(self) -> LLVM7Module {
458 self
459 }
460 }
461
462 struct LLVM7TargetMachine(llvm::LLVMTargetMachineRef);
463
464 impl Drop for LLVM7TargetMachine {
465 fn drop(&mut self) {
466 unsafe {
467 llvm::LLVMDisposeTargetMachine(self.0);
468 }
469 }
470 }
471
472 impl LLVM7TargetMachine {
473 fn take(mut self) -> llvm::LLVMTargetMachineRef {
474 let retval = self.0;
475 self.0 = null_mut();
476 retval
477 }
478 }
479
480 struct LLVM7OrcJITStack(llvm::LLVMOrcJITStackRef);
481
482 impl Drop for LLVM7OrcJITStack {
483 fn drop(&mut self) {
484 unsafe {
485 match llvm::LLVMOrcDisposeInstance(self.0) {
486 llvm::LLVMOrcErrSuccess => {}
487 _ => {
488 panic!("LLVMOrcDisposeInstance failed");
489 }
490 }
491 }
492 }
493 }
494
495 fn initialize_native_target() {
496 static ONCE: Once = ONCE_INIT;
497 ONCE.call_once(|| unsafe {
498 llvm::LLVM_InitializeNativeTarget();
499 llvm::LLVM_InitializeNativeAsmPrinter();
500 llvm::LLVM_InitializeNativeAsmParser();
501 });
502 }
503
504 extern "C" fn symbol_resolver_fn<Void>(name: *const c_char, _lookup_context: *mut Void) -> u64 {
505 let name = unsafe { CStr::from_ptr(name) };
506 panic!("symbol_resolver_fn is unimplemented: name = {:?}", name)
507 }
508
509 #[derive(Copy, Clone)]
510 pub struct LLVM7Compiler;
511
512 impl backend::Compiler for LLVM7Compiler {
513 type Config = LLVM7CompilerConfig;
514 fn name(self) -> &'static str {
515 "LLVM 7"
516 }
517 fn run<U: backend::CompilerUser>(
518 self,
519 user: U,
520 config: LLVM7CompilerConfig,
521 ) -> Result<Box<dyn backend::CompiledCode<U::FunctionKey>>, U::Error> {
522 unsafe {
523 initialize_native_target();
524 let context = OwnedContext(llvm::LLVMContextCreate());
525 let modules = Vec::new();
526 let mut context = LLVM7Context {
527 context: Some(ManuallyDrop::new(context)),
528 modules: ManuallyDrop::new(RefCell::new(modules)),
529 config: config.clone(),
530 };
531 let backend::CompileInputs {
532 module,
533 callable_functions,
534 } = user.run(&context)?;
535 let callable_functions: Vec<_> = callable_functions
536 .into_iter()
537 .map(|(key, callable_function)| {
538 assert_eq!(
539 llvm::LLVMGetGlobalParent(callable_function.function),
540 module.module
541 );
542 let name: CString =
543 CStr::from_ptr(llvm::LLVMGetValueName(callable_function.function)).into();
544 assert_ne!(name.to_bytes().len(), 0);
545 (key, name)
546 })
547 .collect();
548 let module = context
549 .modules
550 .get_mut()
551 .drain(..)
552 .find(|v| v.0 == module.module)
553 .unwrap();
554 let target_triple = LLVM7String::from_ptr(llvm::LLVMGetDefaultTargetTriple()).unwrap();
555 let mut target = null_mut();
556 let mut error = null_mut();
557 let success = !to_bool(llvm::LLVMGetTargetFromTriple(
558 target_triple.as_ptr(),
559 &mut target,
560 &mut error,
561 ));
562 if !success {
563 let error = LLVM7String::from_ptr(error).unwrap();
564 return Err(U::create_error(error.to_string_lossy().into()));
565 }
566 if !to_bool(llvm::LLVMTargetHasJIT(target)) {
567 return Err(U::create_error(format!(
568 "target {:?} doesn't support JIT",
569 target_triple
570 )));
571 }
572 let host_cpu_name = LLVM7String::from_ptr(llvm::LLVMGetHostCPUName()).unwrap();
573 let host_cpu_features = LLVM7String::from_ptr(llvm::LLVMGetHostCPUFeatures()).unwrap();
574 let target_machine = LLVM7TargetMachine(llvm::LLVMCreateTargetMachine(
575 target,
576 target_triple.as_ptr(),
577 host_cpu_name.as_ptr(),
578 host_cpu_features.as_ptr(),
579 match config.optimization_mode {
580 backend::OptimizationMode::NoOptimizations => llvm::LLVMCodeGenLevelNone,
581 backend::OptimizationMode::Normal => llvm::LLVMCodeGenLevelDefault,
582 },
583 llvm::LLVMRelocDefault,
584 llvm::LLVMCodeModelJITDefault,
585 ));
586 assert!(!target_machine.0.is_null());
587 let orc_jit_stack =
588 LLVM7OrcJITStack(llvm::LLVMOrcCreateInstance(target_machine.take()));
589 let mut module_handle = 0;
590 if llvm::LLVMOrcErrSuccess != llvm::LLVMOrcAddEagerlyCompiledIR(
591 orc_jit_stack.0,
592 &mut module_handle,
593 module.take(),
594 Some(symbol_resolver_fn),
595 null_mut(),
596 ) {
597 return Err(U::create_error("compilation failed".into()));
598 }
599 let mut functions: HashMap<_, _> = HashMap::new();
600 for (key, name) in callable_functions {
601 let mut address: llvm::LLVMOrcTargetAddress = mem::zeroed();
602 if llvm::LLVMOrcErrSuccess != llvm::LLVMOrcGetSymbolAddressIn(
603 orc_jit_stack.0,
604 &mut address,
605 module_handle,
606 name.as_ptr(),
607 ) {
608 return Err(U::create_error(format!(
609 "function not found in compiled module: {:?}",
610 name
611 )));
612 }
613 let address: Option<unsafe extern "C" fn()> = mem::transmute(address as usize);
614 if functions.insert(key, address.unwrap()).is_some() {
615 return Err(U::create_error(format!("duplicate function: {:?}", name)));
616 }
617 }
618 struct CompiledCode<K: Hash + Eq + Send + Sync + 'static> {
619 functions: HashMap<K, unsafe extern "C" fn()>,
620 orc_jit_stack: ManuallyDrop<LLVM7OrcJITStack>,
621 context: ManuallyDrop<OwnedContext>,
622 }
623 unsafe impl<K: Hash + Eq + Send + Sync + 'static> Send for CompiledCode<K> {}
624 unsafe impl<K: Hash + Eq + Send + Sync + 'static> Sync for CompiledCode<K> {}
625 impl<K: Hash + Eq + Send + Sync + 'static> Drop for CompiledCode<K> {
626 fn drop(&mut self) {
627 unsafe {
628 ManuallyDrop::drop(&mut self.orc_jit_stack);
629 ManuallyDrop::drop(&mut self.context);
630 }
631 }
632 }
633 impl<K: Hash + Eq + Send + Sync + 'static> backend::CompiledCode<K> for CompiledCode<K> {
634 fn get(&self, key: &K) -> Option<unsafe extern "C" fn()> {
635 Some(*self.functions.get(key)?)
636 }
637 }
638 Ok(Box::new(CompiledCode {
639 functions,
640 orc_jit_stack: ManuallyDrop::new(orc_jit_stack),
641 context: context.context.take().unwrap(),
642 }))
643 }
644 }
645 }