Skip to content

Commit 4a556b6

Browse files
committed
Merge remote-tracking branch 'embecosm/improve-online-change-flag-behavior' into embecosm-online-chane
2 parents 2d4af36 + 3382d26 commit 4a556b6

File tree

439 files changed

+4259
-4178
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

439 files changed

+4259
-4178
lines changed

compiler/plc_driver/src/cli.rs

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ use encoding_rs::Encoding;
55
use plc_diagnostics::diagnostics::{diagnostics_registry::DiagnosticsConfiguration, Diagnostic};
66
use std::{env, ffi::OsStr, num::ParseIntError, path::PathBuf};
77

8-
use plc::{output::FormatOption, ConfigFormat, DebugLevel, ErrorFormat, Target, Threads};
8+
use plc::output::FormatOption;
9+
use plc::{ConfigFormat, DebugLevel, ErrorFormat, Target, Threads, DEFAULT_GOT_LAYOUT_FILE};
910

1011
pub type ParameterError = clap::Error;
1112

@@ -117,9 +118,12 @@ pub struct CompileParameters {
117118
Save information about the generated custom GOT layout to the given file.
118119
Format is detected by extension.
119120
Supported formats : json, toml",
120-
parse(try_from_str = validate_config)
121+
default_value = DEFAULT_GOT_LAYOUT_FILE,
122+
parse(try_from_str = validate_config),
123+
// FIXME: For some reason, this does not work at the moment but it really should
124+
// requires = "online_change"
121125
) ]
122-
pub got_layout_file: Option<String>,
126+
pub got_layout_file: String,
123127

124128
#[clap(
125129
name = "optimization",
@@ -214,6 +218,12 @@ pub struct CompileParameters {
214218
#[clap(name = "check", long, help = "Check only, do not generate any output", global = true)]
215219
pub check_only: bool,
216220

221+
#[clap(
222+
long,
223+
help = "Emit a binary with specific compilation information, suitable for online changes when ran under a conforming runtime"
224+
)]
225+
pub online_change: bool,
226+
217227
#[clap(subcommand)]
218228
pub commands: Option<SubCommands>,
219229
}
@@ -329,9 +339,7 @@ pub fn get_config_format(name: &str) -> Option<ConfigFormat> {
329339

330340
impl CompileParameters {
331341
pub fn parse<T: AsRef<OsStr> + AsRef<str>>(args: &[T]) -> Result<CompileParameters, ParameterError> {
332-
CompileParameters::try_parse_from(args).and_then(|mut result| {
333-
result.got_layout_file = Some(String::from("tmp.json"));
334-
342+
CompileParameters::try_parse_from(args).and_then(|result| {
335343
if result.sysroot.len() > result.target.len() {
336344
let mut cmd = CompileParameters::command();
337345
Err(cmd.error(
@@ -402,8 +410,9 @@ impl CompileParameters {
402410
self.hardware_config.as_deref().and_then(get_config_format)
403411
}
404412

405-
pub fn got_layout_format(&self) -> Option<ConfigFormat> {
406-
self.got_layout_file.as_deref().and_then(get_config_format)
413+
pub fn got_layout_format(&self) -> ConfigFormat {
414+
// It is safe to unwrap here, since the provided argument to `--got-online-change` has been checked with `validate_config`
415+
get_config_format(&self.got_layout_file).unwrap()
407416
}
408417

409418
/// Returns the location where the build artifacts should be stored / output

compiler/plc_driver/src/lib.rs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ use std::{
1919

2020
use cli::{CompileParameters, ParameterError, SubCommands};
2121
use plc::{
22-
codegen::CodegenContext, linker::LinkerType, output::FormatOption, ConfigFormat, DebugLevel,
23-
ErrorFormat, OptimizationLevel, Target, Threads,
22+
codegen::CodegenContext, linker::LinkerType, output::FormatOption, DebugLevel, ErrorFormat,
23+
OnlineChange, OptimizationLevel, Target, Threads,
2424
};
2525

2626
use plc_diagnostics::{diagnostician::Diagnostician, diagnostics::Diagnostic};
@@ -50,12 +50,11 @@ pub struct CompileOptions {
5050
/// The name of the resulting compiled file
5151
pub output: String,
5252
pub output_format: FormatOption,
53-
pub got_layout_file: Option<String>,
54-
pub got_layout_format: Option<ConfigFormat>,
5553
pub optimization: OptimizationLevel,
5654
pub error_format: ErrorFormat,
5755
pub debug_level: DebugLevel,
5856
pub single_module: bool,
57+
pub online_change: OnlineChange,
5958
}
6059

6160
impl Default for CompileOptions {
@@ -65,12 +64,11 @@ impl Default for CompileOptions {
6564
build_location: None,
6665
output: String::new(),
6766
output_format: Default::default(),
68-
got_layout_file: None,
69-
got_layout_format: None,
7067
optimization: OptimizationLevel::None,
7168
error_format: ErrorFormat::None,
7269
debug_level: DebugLevel::None,
7370
single_module: false,
71+
online_change: OnlineChange::Disabled,
7472
}
7573
}
7674
}
@@ -176,12 +174,15 @@ pub fn get_compilation_context<T: AsRef<str> + AsRef<OsStr> + Debug>(
176174
build_location: compile_parameters.get_build_location(),
177175
output: project.get_output_name(),
178176
output_format,
179-
got_layout_file: compile_parameters.got_layout_file.clone(),
180-
got_layout_format: compile_parameters.got_layout_format(),
181177
optimization: compile_parameters.optimization,
182178
error_format: compile_parameters.error_format,
183179
debug_level: compile_parameters.debug_level(),
184180
single_module: compile_parameters.single_module,
181+
online_change: if compile_parameters.online_change {
182+
OnlineChange::Enabled((compile_parameters.got_layout_file.clone(),compile_parameters.got_layout_format()))
183+
} else {
184+
OnlineChange::Disabled
185+
},
185186
};
186187

187188
let libraries =

compiler/plc_driver/src/pipelines.rs

Lines changed: 25 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ use plc::{
2424
TypeAnnotator,
2525
},
2626
validation::Validator,
27-
ConfigFormat, Target,
27+
ConfigFormat, OnlineChange, Target,
2828
};
2929
use plc_diagnostics::{
3030
diagnostician::Diagnostician,
@@ -309,27 +309,19 @@ impl<T: SourceContainer + Sync> AnnotatedProject<T> {
309309
}
310310

311311
pub fn codegen_to_string(&self, compile_options: &CompileOptions) -> Result<Vec<String>, Diagnostic> {
312-
let got_layout = compile_options
313-
.got_layout_file
314-
.as_ref()
315-
.map(|path| read_got_layout(path, ConfigFormat::JSON))
316-
.transpose()?;
317-
312+
let got_layout = if let OnlineChange::Enabled((file, format)) = &compile_options.online_change {
313+
read_got_layout(file, *format)?
314+
} else {
315+
HashMap::default()
316+
};
318317
let got_layout = Mutex::new(got_layout);
319318

320319
self.units
321320
.iter()
322321
.map(|AnnotatedUnit { unit, dependencies, literals }| {
323322
let context = CodegenContext::create();
324-
self.generate_module(
325-
&context,
326-
compile_options,
327-
unit,
328-
dependencies,
329-
literals,
330-
&got_layout
331-
)
332-
.map(|it| it.persist_to_string())
323+
self.generate_module(&context, compile_options, unit, dependencies, literals, &got_layout)
324+
.map(|it| it.persist_to_string())
333325
})
334326
.collect()
335327
}
@@ -339,26 +331,18 @@ impl<T: SourceContainer + Sync> AnnotatedProject<T> {
339331
context: &'ctx CodegenContext,
340332
compile_options: &CompileOptions,
341333
) -> Result<Option<GeneratedModule<'ctx>>, Diagnostic> {
342-
let got_layout = compile_options
343-
.got_layout_file
344-
.as_ref()
345-
.map(|path| read_got_layout(path, ConfigFormat::JSON))
346-
.transpose()?;
347-
334+
let got_layout = if let OnlineChange::Enabled((file, format)) = &compile_options.online_change {
335+
read_got_layout(file, *format)?
336+
} else {
337+
HashMap::default()
338+
};
348339
let got_layout = Mutex::new(got_layout);
349340

350341
let Some(module) = self
351342
.units
352343
.iter()
353344
.map(|AnnotatedUnit { unit, dependencies, literals }| {
354-
self.generate_module(
355-
context,
356-
compile_options,
357-
unit,
358-
dependencies,
359-
literals,
360-
&got_layout
361-
)
345+
self.generate_module(context, compile_options, unit, dependencies, literals, &got_layout)
362346
})
363347
.reduce(|a, b| {
364348
let a = a?;
@@ -378,15 +362,16 @@ impl<T: SourceContainer + Sync> AnnotatedProject<T> {
378362
unit: &CompilationUnit,
379363
dependencies: &FxIndexSet<Dependency>,
380364
literals: &StringLiterals,
381-
got_layout: &Mutex<Option<HashMap<String, u64>>>,
365+
got_layout: &Mutex<HashMap<String, u64>>,
382366
) -> Result<GeneratedModule<'ctx>, Diagnostic> {
383367
let mut code_generator = plc::codegen::CodeGen::new(
384368
context,
385369
compile_options.root.as_deref(),
386370
&unit.file_name,
387-
compile_options.got_layout_file.clone().zip(compile_options.got_layout_format),
388371
compile_options.optimization,
389372
compile_options.debug_level,
373+
//(compile_options.got_layout_file.clone(), compile_options.got_layout_format),
374+
compile_options.online_change.clone(),
390375
);
391376
//Create a types codegen, this contains all the type declarations
392377
//Associate the index type with LLVM types
@@ -444,12 +429,11 @@ impl<T: SourceContainer + Sync> AnnotatedProject<T> {
444429
ensure_compile_dirs(targets, &compile_directory)?;
445430
let targets = if targets.is_empty() { &[Target::System] } else { targets };
446431

447-
let got_layout = compile_options
448-
.got_layout_file
449-
.as_ref()
450-
.map(|path| read_got_layout(path, ConfigFormat::JSON))
451-
.transpose()?;
452-
432+
let got_layout = if let OnlineChange::Enabled((file, format)) = &compile_options.online_change {
433+
read_got_layout(file, *format)?
434+
} else {
435+
HashMap::default()
436+
};
453437
let got_layout = Mutex::new(got_layout);
454438

455439
let res = targets
@@ -516,9 +500,9 @@ impl<T: SourceContainer + Sync> AnnotatedProject<T> {
516500
})
517501
.collect::<Result<Vec<_>, Diagnostic>>()?;
518502

519-
compile_options.got_layout_file.as_ref().map(|path| {
520-
write_got_layout(got_layout.into_inner().unwrap().unwrap(), path, ConfigFormat::JSON)
521-
});
503+
if let OnlineChange::Enabled((file, format)) = &compile_options.online_change {
504+
write_got_layout(got_layout.into_inner().unwrap(), file, *format)?;
505+
}
522506

523507
Ok(res)
524508
}

compiler/plc_driver/src/tests/snapshots/plc_driver__tests__external_files__external_file_function_call.snap

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ expression: "results.join(\"\\n\")"
55
; ModuleID = 'main.st'
66
source_filename = "main.st"
77

8-
define i16 @main() section "$RUSTY$fn-main:i16[]" {
8+
define i16 @main() {
99
entry:
1010
%main = alloca i16, align 2
1111
store i16 0, i16* %main, align 2
@@ -14,17 +14,17 @@ entry:
1414
ret i16 %main_ret
1515
}
1616

17-
declare i16 @external() section "$RUSTY$fn-external:i16[]"
17+
declare i16 @external()
1818

1919
; ModuleID = 'external.st'
2020
source_filename = "external.st"
2121

22-
declare i16 @external() section "$RUSTY$fn-external:i16[]"
22+
declare i16 @external()
2323

2424
; ModuleID = '__init___TestProject'
2525
source_filename = "__init___TestProject"
2626

27-
define void @__init___TestProject() section "$RUSTY$fn-__init___testproject:v[]" {
27+
define void @__init___TestProject() {
2828
entry:
2929
ret void
3030
}

compiler/plc_driver/src/tests/snapshots/plc_driver__tests__external_files__external_file_global_var.snap

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ source_filename = "main.st"
88
@x = external global i16, section "$RUSTY$var-x:i16"
99
@y = external global i16, section "$RUSTY$var-y:i16"
1010

11-
define i16 @main() section "$RUSTY$fn-main:i16[]" {
11+
define i16 @main() {
1212
entry:
1313
%main = alloca i16, align 2
1414
store i16 0, i16* %main, align 2
@@ -19,20 +19,20 @@ entry:
1919
ret i16 %main_ret
2020
}
2121

22-
declare i16 @external() section "$RUSTY$fn-external:i16[]"
22+
declare i16 @external()
2323

2424
; ModuleID = 'external.st'
2525
source_filename = "external.st"
2626

2727
@x = external global i16, section "$RUSTY$var-x:i16"
2828
@y = external global i16, section "$RUSTY$var-y:i16"
2929

30-
declare i16 @external() section "$RUSTY$fn-external:i16[]"
30+
declare i16 @external()
3131

3232
; ModuleID = '__init___TestProject'
3333
source_filename = "__init___TestProject"
3434

35-
define void @__init___TestProject() section "$RUSTY$fn-__init___testproject:v[]" {
35+
define void @__init___TestProject() {
3636
entry:
3737
ret void
3838
}

compiler/plc_driver/src/tests/snapshots/plc_driver__tests__multi_files__multiple_files_in_different_locations_with_debug_info.snap

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ source_filename = "app/file1.st"
99

1010
@mainProg_instance = external global %mainProg, section "$RUSTY$var-mainprog_instance:r0", !dbg !0
1111

12-
define i16 @main() section "$RUSTY$fn-main:i16[]" !dbg !10 {
12+
define i16 @main() !dbg !10 {
1313
entry:
1414
%main = alloca i16, align 2, !dbg !14
1515
call void @llvm.dbg.declare(metadata i16* %main, metadata !15, metadata !DIExpression()), !dbg !17
@@ -19,7 +19,7 @@ entry:
1919
ret i16 %main_ret, !dbg !14
2020
}
2121

22-
declare !dbg !18 void @mainProg(%mainProg*) section "$RUSTY$fn-mainprog:v[]"
22+
declare !dbg !18 void @mainProg(%mainProg*)
2323

2424
; Function Attrs: nofree nosync nounwind readnone speculatable willreturn
2525
declare void @llvm.dbg.declare(metadata, metadata, metadata) #0
@@ -56,7 +56,7 @@ source_filename = "lib/file2.st"
5656

5757
@mainProg_instance = global %mainProg zeroinitializer, section "$RUSTY$var-mainprog_instance:r0", !dbg !0
5858

59-
define void @mainProg(%mainProg* %0) section "$RUSTY$fn-mainprog:v[]" !dbg !10 {
59+
define void @mainProg(%mainProg* %0) !dbg !10 {
6060
entry:
6161
call void @llvm.dbg.declare(metadata %mainProg* %0, metadata !13, metadata !DIExpression()), !dbg !14
6262
ret void, !dbg !14
@@ -93,15 +93,15 @@ source_filename = "__initializers"
9393

9494
@mainProg_instance = external global %mainProg, section "$RUSTY$var-mainprog_instance:r0", !dbg !0
9595

96-
define void @__init_mainprog(%mainProg* %0) section "$RUSTY$fn-__init_mainprog:v[pr0]" !dbg !10 {
96+
define void @__init_mainprog(%mainProg* %0) !dbg !10 {
9797
entry:
9898
%self = alloca %mainProg*, align 8, !dbg !14
9999
call void @llvm.dbg.declare(metadata %mainProg** %self, metadata !15, metadata !DIExpression()), !dbg !14
100100
store %mainProg* %0, %mainProg** %self, align 8, !dbg !14
101101
ret void, !dbg !14
102102
}
103103

104-
declare !dbg !16 void @mainProg(%mainProg*) section "$RUSTY$fn-mainprog:v[]"
104+
declare !dbg !16 void @mainProg(%mainProg*)
105105

106106
; Function Attrs: nofree nosync nounwind readnone speculatable willreturn
107107
declare void @llvm.dbg.declare(metadata, metadata, metadata) #0
@@ -138,15 +138,15 @@ source_filename = "__init___TestProject"
138138

139139
@mainProg_instance = external global %mainProg, section "$RUSTY$var-mainprog_instance:r0", !dbg !0
140140

141-
define void @__init___TestProject() section "$RUSTY$fn-__init___testproject:v[]" !dbg !10 {
141+
define void @__init___TestProject() !dbg !10 {
142142
entry:
143143
call void @__init_mainprog(%mainProg* @mainProg_instance), !dbg !14
144144
ret void, !dbg !14
145145
}
146146

147-
declare !dbg !15 void @__init_mainprog(%mainProg*) section "$RUSTY$fn-__init_mainprog:v[pr0]"
147+
declare !dbg !15 void @__init_mainprog(%mainProg*)
148148

149-
declare !dbg !19 void @mainProg(%mainProg*) section "$RUSTY$fn-mainprog:v[]"
149+
declare !dbg !19 void @mainProg(%mainProg*)
150150

151151
!llvm.module.flags = !{!5, !6}
152152
!llvm.dbg.cu = !{!7}

0 commit comments

Comments
 (0)