Skip to content

fix: add target datalayout and triple to IR #1472

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
  •  
  •  
  •  
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ rustc-hash.workspace = true

[dev-dependencies]
num = "0.4"
insta = "1.31.0"
insta.workspace = true
pretty_assertions = "1.3.0"
driver = { path = "./compiler/plc_driver/", package = "plc_driver" }
project = { path = "./compiler/plc_project/", package = "plc_project", features = [
Expand Down Expand Up @@ -84,6 +84,7 @@ members = [
default-members = [".", "compiler/plc_driver", "compiler/plc_xml"]

[workspace.dependencies]
insta = { version = "1.31.0", features = ["filters"] }
inkwell = { version = "0.2", features = ["llvm14-0"] }
encoding_rs = "0.8"
encoding_rs_io = "0.1"
Expand Down
3 changes: 2 additions & 1 deletion compiler/plc_driver/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ itertools.workspace = true

[dev-dependencies]
pretty_assertions = "1.3.0"
insta = "1.20.0"
insta.workspace = true
plc_util = { path = "../plc_util" }


[lib]
Expand Down
2 changes: 1 addition & 1 deletion compiler/plc_driver/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ fn generate_to_string_internal<T: SourceContainer>(
project.validate(&pipeline.context, &mut pipeline.diagnostician)?;
let context = CodegenContext::create();
let module =
project.generate_single_module(&context, pipeline.get_compile_options().as_ref().unwrap())?;
project.generate_single_module(&context, pipeline.get_compile_options().as_ref().unwrap(), None)?;

// Generate
module.map(|it| it.persist_to_string()).ok_or_else(|| Diagnostic::new("Cannot generate module"))
Expand Down
51 changes: 43 additions & 8 deletions compiler/plc_driver/src/pipelines.rs
Original file line number Diff line number Diff line change
Expand Up @@ -388,11 +388,12 @@ impl<T: SourceContainer> Pipeline for BuildPipeline<T> {
HashMap::default()
};
let got_layout = Mutex::new(got_layout);
let target = self.compile_parameters.as_ref().and_then(|it| it.target.as_ref());
if compile_options.single_module || matches!(compile_options.output_format, FormatOption::Object) {
log::info!("Using single module mode");
let context = CodegenContext::create();
project
.generate_single_module(&context, &compile_options)?
.generate_single_module(&context, &compile_options, target)?
.map(|module| {
self.participants.iter_mut().try_fold((), |_, participant| participant.generate(&module))
})
Expand All @@ -410,6 +411,7 @@ impl<T: SourceContainer> Pipeline for BuildPipeline<T> {
dependencies,
literals,
&got_layout,
target,
)?;
self.participants.iter().try_fold((), |_, participant| participant.generate(&module))
})
Expand Down Expand Up @@ -684,8 +686,16 @@ impl AnnotatedProject {
.iter()
.map(|AnnotatedUnit { unit, dependencies, literals }| {
let context = CodegenContext::create();
self.generate_module(&context, compile_options, unit, dependencies, literals, &got_layout)
.map(|it| it.persist_to_string())
self.generate_module(
&context,
compile_options,
unit,
dependencies,
literals,
&got_layout,
None,
)
.map(|it| it.persist_to_string())
})
.collect()
}
Expand All @@ -694,6 +704,7 @@ impl AnnotatedProject {
&self,
context: &'ctx CodegenContext,
compile_options: &CompileOptions,
target: Option<&Target>,
) -> Result<Option<GeneratedModule<'ctx>>, Diagnostic> {
let got_layout = if let OnlineChange::Enabled { file_name, format } = &compile_options.online_change {
read_got_layout(file_name, *format)?
Expand All @@ -707,7 +718,15 @@ impl AnnotatedProject {
.iter()
// TODO: this can be parallelized
.map(|AnnotatedUnit { unit, dependencies, literals }| {
self.generate_module(context, compile_options, unit, dependencies, literals, &got_layout)
self.generate_module(
context,
compile_options,
unit,
dependencies,
literals,
&got_layout,
target,
)
})
.reduce(|a, b| {
let a = a?;
Expand All @@ -720,6 +739,7 @@ impl AnnotatedProject {
module.map(Some)
}

#[allow(clippy::too_many_arguments)]
fn generate_module<'ctx>(
&self,
context: &'ctx CodegenContext,
Expand All @@ -728,7 +748,11 @@ impl AnnotatedProject {
dependencies: &FxIndexSet<Dependency>,
literals: &StringLiterals,
got_layout: &Mutex<HashMap<String, u64>>,
target: Option<&Target>,
) -> Result<GeneratedModule<'ctx>, Diagnostic> {
// Determine target from compile_options or use default
let target = target.unwrap_or(&Target::System);

let mut code_generator = plc::codegen::CodeGen::new(
context,
compile_options.root.as_deref(),
Expand All @@ -737,6 +761,7 @@ impl AnnotatedProject {
compile_options.debug_level,
//FIXME don't clone here
compile_options.online_change.clone(),
target,
);
//Create a types codegen, this contains all the type declarations
//Associate the index type with LLVM types
Expand All @@ -763,10 +788,12 @@ impl AnnotatedProject {
ensure_compile_dirs(targets, &compile_directory)?;
let context = CodegenContext::create(); //Create a build location for the generated object files
let targets = if targets.is_empty() { &[Target::System] } else { targets };
let module = self.generate_single_module(&context, compile_options)?.unwrap();
let modules =
targets.iter().map(|target| self.generate_single_module(&context, compile_options, Some(target)));
let mut result = vec![];
for target in targets {
let obj: Object = module
for (target, module) in targets.iter().zip(modules) {
let obj: Object = module?
.unwrap()
.persist(
Some(&compile_directory),
&compile_options.output,
Expand Down Expand Up @@ -796,7 +823,15 @@ impl AnnotatedProject {
self.units
.iter()
.map(|AnnotatedUnit { unit, dependencies, literals }| {
self.generate_module(context, compile_options, unit, dependencies, literals, &got_layout)
self.generate_module(
context,
compile_options,
unit,
dependencies,
literals,
&got_layout,
None,
)
})
.collect()
}
Expand Down
2 changes: 1 addition & 1 deletion compiler/plc_driver/src/runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ pub fn compile<T: Compilable>(codegen_context: &CodegenContext, source: T) -> Ge
..Default::default()
};

match project.generate_single_module(codegen_context, &compile_options) {
match project.generate_single_module(codegen_context, &compile_options, None) {
Ok(res) => res.unwrap(),
Err(e) => panic!("{e}"),
}
Expand Down
7 changes: 4 additions & 3 deletions compiler/plc_driver/src/tests/external_files.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use plc::DebugLevel;
use source_code::SourceCode;

use crate::tests::compile_to_string;
use plc_util::filtered_assert_snapshot;

#[test]
fn external_file_function_call() {
Expand All @@ -25,7 +26,7 @@ fn external_file_function_call() {
//When they are generated
let results = compile_to_string(vec![prog], vec![ext], None, DebugLevel::None).unwrap();
//Expect external to only be declared in the result
insta::assert_snapshot!(results.join("\n"));
filtered_assert_snapshot!(results.join("\n"));
}

#[test]
Expand Down Expand Up @@ -58,7 +59,7 @@ fn external_file_global_var() {
//When they are generated
let results = compile_to_string(vec![prog], vec![ext], None, DebugLevel::None).unwrap();
//x should be external
insta::assert_snapshot!(results.join("\n"));
filtered_assert_snapshot!(results.join("\n"));
}

#[test]
Expand All @@ -76,7 +77,7 @@ fn calling_external_file_function_without_including_file_results_in_error() {
let res = compile_to_string(vec![prog], vec![], None, DebugLevel::None);

if let Err(msg) = res {
insta::assert_snapshot!(msg.to_string())
filtered_assert_snapshot!(msg.to_string())
} else {
panic!("expected code-gen error but got none")
}
Expand Down
7 changes: 4 additions & 3 deletions compiler/plc_driver/src/tests/multi_files.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use plc::DebugLevel;
use source_code::SourceCode;

use crate::tests::compile_with_root;
use plc_util::filtered_assert_snapshot;

#[test]
fn multiple_source_files_generated() {
Expand Down Expand Up @@ -36,7 +37,7 @@ fn multiple_source_files_generated() {
assert_eq!(results.len(), 4);
//The datatypes do not conflics
//The functions are defined correctly
insta::assert_snapshot!(results.join("\n"));
filtered_assert_snapshot!(results.join("\n"));
}

#[test]
Expand Down Expand Up @@ -75,7 +76,7 @@ fn multiple_files_with_debug_info() {
assert_eq!(results.len(), 4);
//The datatypes do not conflics
//The functions are defined correctly
insta::assert_snapshot!(results.join("\n"));
filtered_assert_snapshot!(results.join("\n"));
}

#[test]
Expand Down Expand Up @@ -114,5 +115,5 @@ fn multiple_files_in_different_locations_with_debug_info() {
assert_eq!(results.len(), 4);
//The datatypes do not conflics
//The functions are defined correctly
insta::assert_snapshot!(results.join("\n"));
filtered_assert_snapshot!(results.join("\n"));
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
---
source: compiler/plc_driver/./src/tests/external_files.rs
expression: "results.join(\"\\n\")"
snapshot_kind: text
---
; ModuleID = 'main.st'
source_filename = "main.st"
target datalayout = "[filtered]"
target triple = "[filtered]"

define i16 @main() {
entry:
Expand All @@ -18,11 +21,15 @@ declare i16 @external()

; ModuleID = 'external.st'
source_filename = "external.st"
target datalayout = "[filtered]"
target triple = "[filtered]"

declare i16 @external()

; ModuleID = '__init___TestProject'
source_filename = "__init___TestProject"
target datalayout = "[filtered]"
target triple = "[filtered]"

@llvm.global_ctors = appending global [1 x { i32, void ()*, i8* }] [{ i32, void ()*, i8* } { i32 0, void ()* @__init___TestProject, i8* null }]

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
---
source: compiler/plc_driver/./src/tests/external_files.rs
expression: "results.join(\"\\n\")"
snapshot_kind: text
---
; ModuleID = 'main.st'
source_filename = "main.st"
target datalayout = "[filtered]"
target triple = "[filtered]"

@x = external global i16
@y = external global i16
Expand All @@ -23,6 +26,8 @@ declare i16 @external()

; ModuleID = 'external.st'
source_filename = "external.st"
target datalayout = "[filtered]"
target triple = "[filtered]"

@x = external global i16
@y = external global i16
Expand All @@ -31,6 +36,8 @@ declare i16 @external()

; ModuleID = '__init___TestProject'
source_filename = "__init___TestProject"
target datalayout = "[filtered]"
target triple = "[filtered]"

@llvm.global_ctors = appending global [1 x { i32, void ()*, i8* }] [{ i32, void ()*, i8* } { i32 0, void ()* @__init___TestProject, i8* null }]

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ snapshot_kind: text
---
; ModuleID = 'app/file1.st'
source_filename = "app/file1.st"
target datalayout = "[filtered]"
target triple = "[filtered]"

%mainProg = type {}

Expand Down Expand Up @@ -55,6 +57,8 @@ attributes #0 = { nofree nosync nounwind readnone speculatable willreturn }

; ModuleID = 'lib/file2.st'
source_filename = "lib/file2.st"
target datalayout = "[filtered]"
target triple = "[filtered]"

%mainProg = type {}

Expand Down Expand Up @@ -92,6 +96,8 @@ attributes #0 = { nofree nosync nounwind readnone speculatable willreturn }

; ModuleID = '__initializers'
source_filename = "__initializers"
target datalayout = "[filtered]"
target triple = "[filtered]"

%mainProg = type {}

Expand All @@ -115,6 +121,8 @@ entry:

; ModuleID = '__init___TestProject'
source_filename = "__init___TestProject"
target datalayout = "[filtered]"
target triple = "[filtered]"

%mainProg = type {}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ snapshot_kind: text
---
; ModuleID = 'file1.st'
source_filename = "file1.st"
target datalayout = "[filtered]"
target triple = "[filtered]"

%mainProg = type {}

Expand Down Expand Up @@ -55,6 +57,8 @@ attributes #0 = { nofree nosync nounwind readnone speculatable willreturn }

; ModuleID = 'file2.st'
source_filename = "file2.st"
target datalayout = "[filtered]"
target triple = "[filtered]"

%mainProg = type {}

Expand Down Expand Up @@ -92,6 +96,8 @@ attributes #0 = { nofree nosync nounwind readnone speculatable willreturn }

; ModuleID = '__initializers'
source_filename = "__initializers"
target datalayout = "[filtered]"
target triple = "[filtered]"

%mainProg = type {}

Expand All @@ -115,6 +121,8 @@ entry:

; ModuleID = '__init___TestProject'
source_filename = "__init___TestProject"
target datalayout = "[filtered]"
target triple = "[filtered]"

%mainProg = type {}

Expand Down
Loading