1 // SPDX-License-Identifier: LGPL-2.1-or-later
2 // Copyright 2018 Jacob Lifshay
4 use shader_compiler_backend as backend;
5 use std::cell::RefCell;
6 use std::collections::HashMap;
7 use std::collections::HashSet;
8 use std::ffi::{CStr, CString};
12 use std::mem::ManuallyDrop;
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};
19 fn to_bool(v: llvm::LLVMBool) -> bool {
24 pub struct LLVM7CompilerConfig {
25 pub variable_vector_length_multiplier: u32,
26 pub optimization_mode: backend::OptimizationMode,
29 impl Default for LLVM7CompilerConfig {
30 fn default() -> Self {
31 backend::CompilerIndependentConfig::default().into()
35 impl From<backend::CompilerIndependentConfig> for LLVM7CompilerConfig {
36 fn from(v: backend::CompilerIndependentConfig) -> Self {
37 let backend::CompilerIndependentConfig { optimization_mode } = v;
39 variable_vector_length_multiplier: 1,
46 struct LLVM7String(NonNull<c_char>);
48 impl Drop for LLVM7String {
51 llvm::LLVMDisposeMessage(self.0.as_ptr());
56 impl Deref for LLVM7String {
58 fn deref(&self) -> &CStr {
59 unsafe { CStr::from_ptr(self.0.as_ptr()) }
63 impl Clone for LLVM7String {
64 fn clone(&self) -> Self {
70 fn new(v: &CStr) -> Self {
71 unsafe { Self::from_ptr(llvm::LLVMCreateMessage(v.as_ptr())).unwrap() }
73 unsafe fn from_nonnull(v: NonNull<c_char>) -> Self {
76 unsafe fn from_ptr(v: *mut c_char) -> Option<Self> {
77 NonNull::new(v).map(|v| Self::from_nonnull(v))
81 impl fmt::Debug for LLVM7String {
82 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
87 #[derive(Clone, Eq, PartialEq, Hash)]
89 pub struct LLVM7Type(llvm::LLVMTypeRef);
91 impl fmt::Debug for LLVM7Type {
92 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
95 LLVM7String::from_ptr(llvm::LLVMPrintTypeToString(self.0)).ok_or(fmt::Error)?;
96 f.write_str(&string.to_string_lossy())
101 impl<'a> backend::types::Type<'a> for LLVM7Type {
102 type Context = LLVM7Context;
105 pub struct LLVM7TypeBuilder {
106 context: llvm::LLVMContextRef,
107 variable_vector_length_multiplier: u32,
110 impl<'a> backend::types::TypeBuilder<'a, LLVM7Type> for LLVM7TypeBuilder {
111 fn build_bool(&self) -> LLVM7Type {
112 unsafe { LLVM7Type(llvm::LLVMInt1TypeInContext(self.context)) }
114 fn build_i8(&self) -> LLVM7Type {
115 unsafe { LLVM7Type(llvm::LLVMInt8TypeInContext(self.context)) }
117 fn build_i16(&self) -> LLVM7Type {
118 unsafe { LLVM7Type(llvm::LLVMInt16TypeInContext(self.context)) }
120 fn build_i32(&self) -> LLVM7Type {
121 unsafe { LLVM7Type(llvm::LLVMInt32TypeInContext(self.context)) }
123 fn build_i64(&self) -> LLVM7Type {
124 unsafe { LLVM7Type(llvm::LLVMInt64TypeInContext(self.context)) }
126 fn build_f32(&self) -> LLVM7Type {
127 unsafe { LLVM7Type(llvm::LLVMFloatTypeInContext(self.context)) }
129 fn build_f64(&self) -> LLVM7Type {
130 unsafe { LLVM7Type(llvm::LLVMDoubleTypeInContext(self.context)) }
132 fn build_pointer(&self, target: LLVM7Type) -> LLVM7Type {
133 unsafe { LLVM7Type(llvm::LLVMPointerType(target.0, 0)) }
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)) }
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)
147 assert_ne!(length, 0);
148 unsafe { LLVM7Type(llvm::LLVMVectorType(element.0, length)) }
150 fn build_struct(&self, members: &[LLVM7Type]) -> LLVM7Type {
151 assert_eq!(members.len() as c_uint as usize, members.len());
153 LLVM7Type(llvm::LLVMStructTypeInContext(
155 members.as_ptr() as *mut llvm::LLVMTypeRef,
156 members.len() as c_uint,
157 false as llvm::LLVMBool,
161 fn build_function(&self, arguments: &[LLVM7Type], return_type: Option<LLVM7Type>) -> LLVM7Type {
162 assert_eq!(arguments.len() as c_uint as usize, arguments.len());
164 LLVM7Type(llvm::LLVMFunctionType(
166 .unwrap_or_else(|| LLVM7Type(llvm::LLVMVoidTypeInContext(self.context)))
168 arguments.as_ptr() as *mut llvm::LLVMTypeRef,
169 arguments.len() as c_uint,
170 false as llvm::LLVMBool,
178 pub struct LLVM7Value(llvm::LLVMValueRef);
180 impl fmt::Debug for LLVM7Value {
181 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
184 LLVM7String::from_ptr(llvm::LLVMPrintValueToString(self.0)).ok_or(fmt::Error)?;
185 f.write_str(&string.to_string_lossy())
190 impl<'a> backend::Value<'a> for LLVM7Value {
191 type Context = LLVM7Context;
196 pub struct LLVM7BasicBlock(llvm::LLVMBasicBlockRef);
198 impl fmt::Debug for LLVM7BasicBlock {
199 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
200 use self::backend::BasicBlock;
202 let string = LLVM7String::from_ptr(llvm::LLVMPrintValueToString(self.as_value().0))
204 f.write_str(&string.to_string_lossy())
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)) }
216 impl<'a> backend::BuildableBasicBlock<'a> for LLVM7BasicBlock {
217 type Context = LLVM7Context;
218 fn as_basic_block(&self) -> LLVM7BasicBlock {
223 pub struct LLVM7Function {
224 context: llvm::LLVMContextRef,
225 function: llvm::LLVMValueRef,
226 parameters: Box<[LLVM7Value]>,
229 impl fmt::Debug for LLVM7Function {
230 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
232 let string = LLVM7String::from_ptr(llvm::LLVMPrintValueToString(self.function))
234 f.write_str(&string.to_string_lossy())
239 impl<'a> backend::Function<'a> for LLVM7Function {
240 type Context = LLVM7Context;
241 fn as_value(&self) -> LLVM7Value {
242 LLVM7Value(self.function)
244 fn append_new_basic_block(&mut self, name: Option<&str>) -> LLVM7BasicBlock {
245 let name = CString::new(name.unwrap_or("")).unwrap();
247 LLVM7BasicBlock(llvm::LLVMAppendBasicBlockInContext(
254 fn parameters(&self) -> &[LLVM7Value] {
259 pub struct LLVM7Context {
260 context: Option<ManuallyDrop<OwnedContext>>,
261 modules: ManuallyDrop<RefCell<Vec<OwnedModule>>>,
262 config: LLVM7CompilerConfig,
265 impl Drop for LLVM7Context {
268 ManuallyDrop::drop(&mut self.modules);
269 if let Some(context) = &mut self.context {
270 ManuallyDrop::drop(context);
276 impl<'a> backend::Context<'a> for LLVM7Context {
277 type Value = LLVM7Value;
278 type BasicBlock = LLVM7BasicBlock;
279 type BuildableBasicBlock = LLVM7BasicBlock;
280 type Function = LLVM7Function;
281 type Type = LLVM7Type;
282 type TypeBuilder = LLVM7TypeBuilder;
283 type Module = LLVM7Module;
284 type VerifiedModule = LLVM7Module;
285 type AttachedBuilder = LLVM7Builder;
286 type DetachedBuilder = LLVM7Builder;
287 fn create_module(&self, name: &str) -> LLVM7Module {
288 let name = CString::new(name).unwrap();
289 let mut modules = self.modules.borrow_mut();
291 let module = OwnedModule(llvm::LLVMModuleCreateWithNameInContext(
293 self.context.as_ref().unwrap().0,
295 let module_ref = module.0;
296 modules.push(module);
298 context: self.context.as_ref().unwrap().0,
300 name_set: HashSet::new(),
304 fn create_builder(&self) -> LLVM7Builder {
306 LLVM7Builder(llvm::LLVMCreateBuilderInContext(
307 self.context.as_ref().unwrap().0,
311 fn create_type_builder(&self) -> LLVM7TypeBuilder {
313 context: self.context.as_ref().unwrap().0,
314 variable_vector_length_multiplier: self.config.variable_vector_length_multiplier,
320 pub struct LLVM7Builder(llvm::LLVMBuilderRef);
322 impl Drop for LLVM7Builder {
325 llvm::LLVMDisposeBuilder(self.0);
330 impl<'a> backend::AttachedBuilder<'a> for LLVM7Builder {
331 type Context = LLVM7Context;
332 fn current_basic_block(&self) -> LLVM7BasicBlock {
333 unsafe { LLVM7BasicBlock(llvm::LLVMGetInsertBlock(self.0)) }
335 fn build_return(self, value: Option<LLVM7Value>) -> LLVM7Builder {
338 Some(value) => llvm::LLVMBuildRet(self.0, value.0),
339 None => llvm::LLVMBuildRetVoid(self.0),
341 llvm::LLVMClearInsertionPosition(self.0);
347 impl<'a> backend::DetachedBuilder<'a> for LLVM7Builder {
348 type Context = LLVM7Context;
349 fn attach(self, basic_block: LLVM7BasicBlock) -> LLVM7Builder {
351 llvm::LLVMPositionBuilderAtEnd(self.0, basic_block.0);
357 struct OwnedModule(llvm::LLVMModuleRef);
359 impl Drop for OwnedModule {
362 llvm::LLVMDisposeModule(self.0);
368 unsafe fn take(mut self) -> llvm::LLVMModuleRef {
375 struct OwnedContext(llvm::LLVMContextRef);
377 impl Drop for OwnedContext {
380 llvm::LLVMContextDispose(self.0);
385 pub struct LLVM7Module {
386 context: llvm::LLVMContextRef,
387 module: llvm::LLVMModuleRef,
388 name_set: HashSet<String>,
391 impl fmt::Debug for LLVM7Module {
392 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
394 let string = LLVM7String::from_ptr(llvm::LLVMPrintModuleToString(self.module))
396 f.write_str(&string.to_string_lossy())
401 impl<'a> backend::Module<'a> for LLVM7Module {
402 type Context = LLVM7Context;
403 fn set_source_file_name(&mut self, source_file_name: &str) {
405 llvm::LLVMSetSourceFileName(
407 source_file_name.as_ptr() as *const c_char,
408 source_file_name.len(),
412 fn add_function(&mut self, name: &str, ty: LLVM7Type) -> LLVM7Function {
413 fn is_start_char(c: char) -> bool {
414 if c.is_ascii_alphabetic() {
418 '_' | '.' | '$' | '-' => true,
423 fn is_continue_char(c: char) -> bool {
424 is_start_char(c) || c.is_ascii_digit()
426 assert!(is_start_char(name.chars().next().unwrap()));
427 assert!(name.chars().all(is_continue_char));
428 assert!(self.name_set.insert(name.into()));
429 let name = CString::new(name).unwrap();
431 let function = llvm::LLVMAddFunction(self.module, name.as_ptr(), ty.0);
432 let mut parameters = Vec::new();
433 parameters.resize(llvm::LLVMCountParams(function) as usize, null_mut());
434 llvm::LLVMGetParams(function, parameters.as_mut_ptr());
435 let parameters: Vec<_> = parameters.into_iter().map(LLVM7Value).collect();
437 context: self.context,
438 function: llvm::LLVMAddFunction(self.module, name.as_ptr(), ty.0),
439 parameters: parameters.into_boxed_slice(),
443 fn verify(self) -> Result<LLVM7Module, backend::VerificationFailure<'a, LLVM7Module>> {
445 let mut message = null_mut();
446 let broken = to_bool(llvm::LLVMVerifyModule(
448 llvm::LLVMReturnStatusAction,
452 let message = LLVM7String::from_ptr(message).unwrap();
453 let message = message.to_string_lossy();
454 Err(backend::VerificationFailure::new(self, message.as_ref()))
460 unsafe fn to_verified_module_unchecked(self) -> LLVM7Module {
465 impl<'a> backend::VerifiedModule<'a> for LLVM7Module {
466 type Context = LLVM7Context;
467 fn into_module(self) -> LLVM7Module {
472 struct LLVM7TargetMachine(llvm::LLVMTargetMachineRef);
474 impl Drop for LLVM7TargetMachine {
477 llvm::LLVMDisposeTargetMachine(self.0);
482 impl LLVM7TargetMachine {
483 fn take(mut self) -> llvm::LLVMTargetMachineRef {
490 struct LLVM7OrcJITStack(llvm::LLVMOrcJITStackRef);
492 impl Drop for LLVM7OrcJITStack {
495 match llvm::LLVMOrcDisposeInstance(self.0) {
496 llvm::LLVMOrcErrSuccess => {}
498 panic!("LLVMOrcDisposeInstance failed");
505 fn initialize_native_target() {
506 static ONCE: Once = ONCE_INIT;
507 ONCE.call_once(|| unsafe {
508 llvm::LLVM_InitializeNativeTarget();
509 llvm::LLVM_InitializeNativeAsmPrinter();
510 llvm::LLVM_InitializeNativeAsmParser();
514 extern "C" fn symbol_resolver_fn<Void>(name: *const c_char, _lookup_context: *mut Void) -> u64 {
515 let name = unsafe { CStr::from_ptr(name) };
516 panic!("symbol_resolver_fn is unimplemented: name = {:?}", name)
519 #[derive(Copy, Clone)]
520 pub struct LLVM7Compiler;
522 impl backend::Compiler for LLVM7Compiler {
523 type Config = LLVM7CompilerConfig;
524 fn name(self) -> &'static str {
527 fn run<U: backend::CompilerUser>(
530 config: LLVM7CompilerConfig,
531 ) -> Result<Box<dyn backend::CompiledCode<U::FunctionKey>>, U::Error> {
533 initialize_native_target();
534 let context = OwnedContext(llvm::LLVMContextCreate());
535 let modules = Vec::new();
536 let mut context = LLVM7Context {
537 context: Some(ManuallyDrop::new(context)),
538 modules: ManuallyDrop::new(RefCell::new(modules)),
539 config: config.clone(),
541 let backend::CompileInputs {
544 } = user.run(&context)?;
545 let callable_functions: Vec<_> = callable_functions
547 .map(|(key, callable_function)| {
549 llvm::LLVMGetGlobalParent(callable_function.function),
553 CStr::from_ptr(llvm::LLVMGetValueName(callable_function.function)).into();
554 assert_ne!(name.to_bytes().len(), 0);
562 .find(|v| v.0 == module.module)
564 let target_triple = LLVM7String::from_ptr(llvm::LLVMGetDefaultTargetTriple()).unwrap();
565 let mut target = null_mut();
566 let mut error = null_mut();
567 let success = !to_bool(llvm::LLVMGetTargetFromTriple(
568 target_triple.as_ptr(),
573 let error = LLVM7String::from_ptr(error).unwrap();
574 return Err(U::create_error(error.to_string_lossy().into()));
576 if !to_bool(llvm::LLVMTargetHasJIT(target)) {
577 return Err(U::create_error(format!(
578 "target {:?} doesn't support JIT",
582 let host_cpu_name = LLVM7String::from_ptr(llvm::LLVMGetHostCPUName()).unwrap();
583 let host_cpu_features = LLVM7String::from_ptr(llvm::LLVMGetHostCPUFeatures()).unwrap();
584 let target_machine = LLVM7TargetMachine(llvm::LLVMCreateTargetMachine(
586 target_triple.as_ptr(),
587 host_cpu_name.as_ptr(),
588 host_cpu_features.as_ptr(),
589 match config.optimization_mode {
590 backend::OptimizationMode::NoOptimizations => llvm::LLVMCodeGenLevelNone,
591 backend::OptimizationMode::Normal => llvm::LLVMCodeGenLevelDefault,
593 llvm::LLVMRelocDefault,
594 llvm::LLVMCodeModelJITDefault,
596 assert!(!target_machine.0.is_null());
598 LLVM7OrcJITStack(llvm::LLVMOrcCreateInstance(target_machine.take()));
599 let mut module_handle = 0;
600 if llvm::LLVMOrcErrSuccess != llvm::LLVMOrcAddEagerlyCompiledIR(
604 Some(symbol_resolver_fn),
607 return Err(U::create_error("compilation failed".into()));
609 let mut functions: HashMap<_, _> = HashMap::new();
610 for (key, name) in callable_functions {
611 let mut address: llvm::LLVMOrcTargetAddress = mem::zeroed();
612 if llvm::LLVMOrcErrSuccess != llvm::LLVMOrcGetSymbolAddressIn(
618 return Err(U::create_error(format!(
619 "function not found in compiled module: {:?}",
623 let address: Option<unsafe extern "C" fn()> = mem::transmute(address as usize);
624 if functions.insert(key, address.unwrap()).is_some() {
625 return Err(U::create_error(format!("duplicate function: {:?}", name)));
628 struct CompiledCode<K: Hash + Eq + Send + Sync + 'static> {
629 functions: HashMap<K, unsafe extern "C" fn()>,
630 orc_jit_stack: ManuallyDrop<LLVM7OrcJITStack>,
631 context: ManuallyDrop<OwnedContext>,
633 unsafe impl<K: Hash + Eq + Send + Sync + 'static> Send for CompiledCode<K> {}
634 unsafe impl<K: Hash + Eq + Send + Sync + 'static> Sync for CompiledCode<K> {}
635 impl<K: Hash + Eq + Send + Sync + 'static> Drop for CompiledCode<K> {
638 ManuallyDrop::drop(&mut self.orc_jit_stack);
639 ManuallyDrop::drop(&mut self.context);
643 impl<K: Hash + Eq + Send + Sync + 'static> backend::CompiledCode<K> for CompiledCode<K> {
644 fn get(&self, key: &K) -> Option<unsafe extern "C" fn()> {
645 Some(*self.functions.get(key)?)
648 Ok(Box::new(CompiledCode {
650 orc_jit_stack: ManuallyDrop::new(orc_jit_stack),
651 context: context.context.take().unwrap(),