diff --git a/cranelift/faerie/src/backend.rs b/cranelift/faerie/src/backend.rs index 0adb564b30..372655f1df 100644 --- a/cranelift/faerie/src/backend.rs +++ b/cranelift/faerie/src/backend.rs @@ -127,9 +127,9 @@ impl Backend for FaerieBackend { .expect("inconsistent declarations"); } - fn declare_data(&mut self, name: &str, linkage: Linkage, writable: bool) { + fn declare_data(&mut self, name: &str, linkage: Linkage, writable: bool, align: Option) { self.artifact - .declare(name, translate_data_linkage(linkage, writable)) + .declare(name, translate_data_linkage(linkage, writable, align)) .expect("inconsistent declarations"); } @@ -190,6 +190,7 @@ impl Backend for FaerieBackend { &mut self, name: &str, _writable: bool, + _align: Option, data_ctx: &DataContext, namespace: &ModuleNamespace, ) -> ModuleResult { @@ -334,12 +335,13 @@ fn translate_function_linkage(linkage: Linkage) -> faerie::Decl { } } -fn translate_data_linkage(linkage: Linkage, writable: bool) -> faerie::Decl { +fn translate_data_linkage(linkage: Linkage, writable: bool, align: Option) -> faerie::Decl { + let align = align.map(|align| usize::from(align)); match linkage { Linkage::Import => faerie::Decl::data_import().into(), - Linkage::Local => faerie::Decl::data().with_writable(writable).into(), - Linkage::Export => faerie::Decl::data().global().with_writable(writable).into(), - Linkage::Preemptible => faerie::Decl::data().weak().with_writable(writable).into(), + Linkage::Local => faerie::Decl::data().with_writable(writable).with_align(align).into(), + Linkage::Export => faerie::Decl::data().global().with_writable(writable).with_align(align).into(), + Linkage::Preemptible => faerie::Decl::data().weak().with_writable(writable).with_align(align).into(), } } diff --git a/cranelift/module/src/backend.rs b/cranelift/module/src/backend.rs index 3f89e18fde..fa23d24f01 100644 --- a/cranelift/module/src/backend.rs +++ b/cranelift/module/src/backend.rs @@ -59,7 +59,7 @@ where fn declare_function(&mut self, name: &str, linkage: Linkage); /// Declare a data object. - fn declare_data(&mut self, name: &str, linkage: Linkage, writable: bool); + fn declare_data(&mut self, name: &str, linkage: Linkage, writable: bool, align: Option); /// Define a function, producing the function body from the given `Context`. /// @@ -79,6 +79,7 @@ where &mut self, name: &str, writable: bool, + align: Option, data_ctx: &DataContext, namespace: &ModuleNamespace, ) -> ModuleResult; diff --git a/cranelift/module/src/module.rs b/cranelift/module/src/module.rs index 5b11a48e15..9eac603a28 100644 --- a/cranelift/module/src/module.rs +++ b/cranelift/module/src/module.rs @@ -185,6 +185,7 @@ pub struct DataDeclaration { pub name: String, pub linkage: Linkage, pub writable: bool, + pub align: Option, } /// A data object belonging to a `Module`. @@ -438,6 +439,7 @@ where name: &str, linkage: Linkage, writable: bool, + align: Option, // An alignment bigger than 128 is unlikely ) -> ModuleResult { // TODO: Can we avoid allocating names so often? use super::hash_map::Entry::*; @@ -447,7 +449,7 @@ where let existing = &mut self.contents.data_objects[id]; existing.merge(linkage, writable); self.backend - .declare_data(name, existing.decl.linkage, existing.decl.writable); + .declare_data(name, existing.decl.linkage, existing.decl.writable, existing.decl.align); Ok(id) } @@ -461,11 +463,12 @@ where name: name.to_owned(), linkage, writable, + align, }, compiled: None, }); entry.insert(FuncOrDataId::Data(id)); - self.backend.declare_data(name, linkage, writable); + self.backend.declare_data(name, linkage, writable, align); Ok(id) } } @@ -563,6 +566,7 @@ where Some(self.backend.define_data( &info.decl.name, info.decl.writable, + info.decl.align, data_ctx, &ModuleNamespace:: { contents: &self.contents, diff --git a/cranelift/simplejit/src/backend.rs b/cranelift/simplejit/src/backend.rs index 7be3b3b2fa..1c6b0b1826 100644 --- a/cranelift/simplejit/src/backend.rs +++ b/cranelift/simplejit/src/backend.rs @@ -209,7 +209,13 @@ impl<'simple_jit_backend> Backend for SimpleJITBackend { // Nothing to do. } - fn declare_data(&mut self, _name: &str, _linkage: Linkage, _writable: bool) { + fn declare_data( + &mut self, + _name: &str, + _linkage: Linkage, + _writable: bool, + _align: Option, + ) { // Nothing to do. } @@ -223,7 +229,7 @@ impl<'simple_jit_backend> Backend for SimpleJITBackend { let size = code_size as usize; let ptr = self .code_memory - .allocate(size) + .allocate(size, 0x10) .expect("TODO: handle OOM etc."); if cfg!(target_os = "linux") && ::std::env::var_os("PERF_BUILDID_DIR").is_some() { @@ -253,6 +259,7 @@ impl<'simple_jit_backend> Backend for SimpleJITBackend { &mut self, _name: &str, writable: bool, + align: Option, data: &DataContext, _namespace: &ModuleNamespace, ) -> ModuleResult { @@ -267,11 +274,11 @@ impl<'simple_jit_backend> Backend for SimpleJITBackend { let size = init.size(); let storage = if writable { self.writable_memory - .allocate(size) + .allocate(size, align.unwrap_or(0x8)) .expect("TODO: handle OOM etc.") } else { self.readonly_memory - .allocate(size) + .allocate(size, align.unwrap_or(1)) .expect("TODO: handle OOM etc.") }; diff --git a/cranelift/simplejit/src/memory.rs b/cranelift/simplejit/src/memory.rs index 9720e2ae17..6b04193310 100644 --- a/cranelift/simplejit/src/memory.rs +++ b/cranelift/simplejit/src/memory.rs @@ -97,7 +97,12 @@ impl Memory { } /// TODO: Use a proper error type. - pub fn allocate(&mut self, size: usize) -> Result<*mut u8, String> { + pub fn allocate(&mut self, size: usize, align: u8) -> Result<*mut u8, String> { + if self.position % align as usize != 0 { + self.position += align as usize - self.position % align as usize; + debug_assert!(self.position % align as usize == 0); + } + if size <= self.current.len - self.position { // TODO: Ensure overflow is not possible. let ptr = unsafe { self.current.ptr.add(self.position) };