Explicitly use large code models in AArch64 code gen

This appears to be necessary to compile our programs on AArch64 boxes.
My guess is special-cased code due to PIEs for large code models in
LLVM's codebase but I really have no idea.
This commit is contained in:
Ayaz Hafiz 2022-04-26 16:42:55 -04:00
parent a5cc18cd0f
commit 0f863bdd85
No known key found for this signature in database
GPG key ID: 0E2A37416A25EF58
3 changed files with 15 additions and 9 deletions

View file

@ -1072,7 +1072,7 @@ pub fn module_to_dylib(
opt_level: OptLevel, opt_level: OptLevel,
) -> Result<Library, Error> { ) -> Result<Library, Error> {
use crate::target::{self, convert_opt_level}; use crate::target::{self, convert_opt_level};
use inkwell::targets::{CodeModel, FileType, RelocMode}; use inkwell::targets::{FileType, RelocMode};
let dir = tempfile::tempdir().unwrap(); let dir = tempfile::tempdir().unwrap();
let filename = PathBuf::from("Test.roc"); let filename = PathBuf::from("Test.roc");
@ -1083,9 +1083,8 @@ pub fn module_to_dylib(
// Emit the .o file using position-independent code (PIC) - needed for dylibs // Emit the .o file using position-independent code (PIC) - needed for dylibs
let reloc = RelocMode::PIC; let reloc = RelocMode::PIC;
let model = CodeModel::Default;
let target_machine = let target_machine =
target::target_machine(target, convert_opt_level(opt_level), reloc, model).unwrap(); target::target_machine(target, convert_opt_level(opt_level), reloc).unwrap();
target_machine target_machine
.write_to_file(module, FileType::Object, &app_o_file) .write_to_file(module, FileType::Object, &app_o_file)

View file

@ -249,7 +249,7 @@ pub fn gen_from_mono_module_llvm(
use inkwell::attributes::{Attribute, AttributeLoc}; use inkwell::attributes::{Attribute, AttributeLoc};
use inkwell::context::Context; use inkwell::context::Context;
use inkwell::module::Linkage; use inkwell::module::Linkage;
use inkwell::targets::{CodeModel, FileType, RelocMode}; use inkwell::targets::{FileType, RelocMode};
let code_gen_start = SystemTime::now(); let code_gen_start = SystemTime::now();
@ -437,10 +437,8 @@ pub fn gen_from_mono_module_llvm(
match target.architecture { match target.architecture {
Architecture::X86_64 | Architecture::X86_32(_) | Architecture::Aarch64(_) => { Architecture::X86_64 | Architecture::X86_32(_) | Architecture::Aarch64(_) => {
let reloc = RelocMode::PIC; let reloc = RelocMode::PIC;
let model = CodeModel::Default;
let target_machine = let target_machine =
target::target_machine(target, convert_opt_level(opt_level), reloc, model) target::target_machine(target, convert_opt_level(opt_level), reloc).unwrap();
.unwrap();
target_machine target_machine
.write_to_file(env.module, FileType::Object, app_o_file) .write_to_file(env.module, FileType::Object, app_o_file)

View file

@ -147,19 +147,28 @@ pub fn target_machine(
target: &Triple, target: &Triple,
opt: OptimizationLevel, opt: OptimizationLevel,
reloc: RelocMode, reloc: RelocMode,
model: CodeModel,
) -> Option<TargetMachine> { ) -> Option<TargetMachine> {
let arch = arch_str(target); let arch = arch_str(target);
init_arch(target); init_arch(target);
let code_model = match target.architecture {
// LLVM 12 will not compile our programs without a large code model.
// The reason is not totally clear to me, but my guess is a few special-cases in
// llvm/lib/Target/AArch64/AArch64ISelLowering.cpp (instructions)
// llvm/lib/Target/AArch64/AArch64Subtarget.cpp (GoT tables)
// Revisit when upgrading to LLVM 13.
Architecture::Aarch64(..) => CodeModel::Large,
_ => CodeModel::Default,
};
Target::from_name(arch).unwrap().create_target_machine( Target::from_name(arch).unwrap().create_target_machine(
&TargetTriple::create(target_triple_str(target)), &TargetTriple::create(target_triple_str(target)),
"generic", "generic",
"", // TODO: this probably should be TargetMachine::get_host_cpu_features() to enable all features. "", // TODO: this probably should be TargetMachine::get_host_cpu_features() to enable all features.
opt, opt,
reloc, reloc,
model, code_model,
) )
} }