Skip to content

Commit 6422650

Browse files
authored
Add backtrace build option (#7285)
## Description This PR adds the `backtrace` build option as described in the [ABI Backtracing RFC](https://github.com/FuelLabs/sway-rfcs/blob/ironcev/abi-backtracing/rfcs/0016-abi-backtracing.md). This is the second step in implementing #7276. Docs for `backtrace` build option will be added in a separate PR that will document the whole backtracking feature. ## Checklist - [x] I have linked to any relevant issues. - [ ] I have commented my code, particularly in hard-to-understand areas. - [ ] I have updated the documentation where relevant (API docs, the reference, and the Sway book). - [ ] If my change requires substantial documentation changes, I have [requested support from the DevRel team](https://github.com/FuelLabs/devrel-requests/issues/new/choose) - [x] I have added tests that prove my fix is effective or that my feature works. - [ ] I have added (or requested a maintainer to add) the necessary `Breaking*` or `New Feature` labels where relevant. - [x] I have done my best to ensure that my PR adheres to [the Fuel Labs Code Review Standards](https://github.com/FuelLabs/rfcs/blob/master/text/code-standards/external-contributors.md). - [x] I have requested a review from the relevant team or maintainers.
1 parent a27d979 commit 6422650

File tree

6 files changed

+51
-11
lines changed

6 files changed

+51
-11
lines changed

forc-pkg/src/manifest/build_profile.rs

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use serde::{Deserialize, Serialize};
2-
use sway_core::{OptLevel, PrintAsm, PrintIr};
2+
use sway_core::{Backtrace, OptLevel, PrintAsm, PrintIr};
33

44
/// Parameters to pass through to the `sway_core::BuildConfig` during compilation.
55
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq)]
@@ -35,6 +35,8 @@ pub struct BuildProfile {
3535
pub reverse_results: bool,
3636
#[serde(default)]
3737
pub optimization_level: OptLevel,
38+
#[serde(default)]
39+
pub backtrace: Backtrace,
3840
}
3941

4042
impl BuildProfile {
@@ -60,6 +62,7 @@ impl BuildProfile {
6062
error_on_warnings: false,
6163
reverse_results: false,
6264
optimization_level: OptLevel::Opt0,
65+
backtrace: Backtrace::AllExceptNever,
6366
}
6467
}
6568

@@ -81,6 +84,7 @@ impl BuildProfile {
8184
error_on_warnings: false,
8285
reverse_results: false,
8386
optimization_level: OptLevel::Opt1,
87+
backtrace: Backtrace::OnlyAlways,
8488
}
8589
}
8690

@@ -98,13 +102,13 @@ impl Default for BuildProfile {
98102
#[cfg(test)]
99103
mod tests {
100104
use crate::{BuildProfile, PackageManifest};
101-
use sway_core::{OptLevel, PrintAsm, PrintIr};
105+
use sway_core::{Backtrace, OptLevel, PrintAsm, PrintIr};
102106

103107
#[test]
104108
fn test_build_profiles() {
105109
let manifest = PackageManifest::from_dir("./tests/sections").expect("manifest");
106110
let build_profiles = manifest.build_profile.expect("build profile");
107-
assert_eq!(build_profiles.len(), 4);
111+
assert_eq!(build_profiles.len(), 5);
108112

109113
// Standard debug profile without adaptations.
110114
let expected = BuildProfile::debug();
@@ -136,6 +140,17 @@ mod tests {
136140
.expect("custom profile for IR");
137141
assert_eq!(*profile, expected);
138142

143+
// Profile based on debug profile with adjusted backtrace option.
144+
let expected = BuildProfile {
145+
name: "".into(),
146+
backtrace: Backtrace::OnlyAlways,
147+
..BuildProfile::debug()
148+
};
149+
let profile = build_profiles
150+
.get("custom_backtrace")
151+
.expect("custom profile for backtrace");
152+
assert_eq!(*profile, expected);
153+
139154
// Adapted release profile.
140155
let expected = BuildProfile {
141156
name: "".into(),
@@ -154,6 +169,7 @@ mod tests {
154169
error_on_warnings: true,
155170
reverse_results: true,
156171
optimization_level: OptLevel::Opt0,
172+
backtrace: Backtrace::None,
157173
};
158174
let profile = build_profiles.get("release").expect("release profile");
159175
assert_eq!(*profile, expected);

forc-pkg/src/pkg.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1579,7 +1579,8 @@ pub fn sway_build_config(
15791579
.with_time_phases(build_profile.time_phases)
15801580
.with_profile(build_profile.profile)
15811581
.with_metrics(build_profile.metrics_outfile.clone())
1582-
.with_optimization_level(build_profile.optimization_level);
1582+
.with_optimization_level(build_profile.optimization_level)
1583+
.with_backtrace(build_profile.backtrace);
15831584
Ok(build_config)
15841585
}
15851586

@@ -2147,7 +2148,6 @@ fn build_profile_from_opts(
21472148
}
21482149
profile.include_tests |= tests;
21492150
profile.error_on_warnings |= error_on_warnings;
2150-
// profile.experimental = *experimental;
21512151

21522152
Ok(profile)
21532153
}

forc-pkg/tests/sections/Forc.toml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ include-tests = true
2020
error-on-warnings = true
2121
reverse-results = true
2222
optimization-level = 0
23-
experimental = { encoding-v1 = true }
23+
backtrace = "none"
2424

2525
[build-profile.custom_asm]
2626
print-asm = { virtual = false, allocated = false, final = true }
@@ -30,3 +30,6 @@ print-ir = { initial = true, final = false, modified = true, passes = [
3030
"dce",
3131
"sroa",
3232
] }
33+
34+
[build-profile.custom_backtrace]
35+
backtrace = "only_always"

sway-core/src/build_config.rs

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,16 @@ impl From<&PrintIr> for PrintPassesOpts {
182182
}
183183
}
184184

185+
#[derive(Serialize, Deserialize, Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Default)]
186+
#[serde(rename_all = "snake_case")]
187+
pub enum Backtrace {
188+
All,
189+
#[default]
190+
AllExceptNever,
191+
OnlyAlways,
192+
None,
193+
}
194+
185195
/// Configuration for the overall build and compilation process.
186196
#[derive(Clone)]
187197
pub struct BuildConfig {
@@ -199,6 +209,10 @@ pub struct BuildConfig {
199209
pub(crate) print_ir: PrintIr,
200210
pub(crate) include_tests: bool,
201211
pub(crate) optimization_level: OptLevel,
212+
// TODO: (ABI-BACKTRACING) Remove `#[allow(dead_code)]` once the `backtrace`
213+
// option is used in IR compilation.
214+
#[allow(dead_code)]
215+
pub(crate) backtrace: Backtrace,
202216
pub time_phases: bool,
203217
pub profile: bool,
204218
pub metrics_outfile: Option<String>,
@@ -251,13 +265,14 @@ impl BuildConfig {
251265
time_phases: false,
252266
profile: false,
253267
metrics_outfile: None,
254-
optimization_level: OptLevel::Opt0,
268+
optimization_level: OptLevel::default(),
269+
backtrace: Backtrace::default(),
255270
lsp_mode: None,
256271
}
257272
}
258273

259274
/// Dummy build config that can be used for testing.
260-
/// This is not not valid generally, but asm generation will accept it.
275+
/// This is not valid generally, but asm generation will accept it.
261276
pub fn dummy_for_asm_generation() -> Self {
262277
Self::root_from_file_name_and_manifest_path(
263278
PathBuf::from("/"),
@@ -325,6 +340,10 @@ impl BuildConfig {
325340
}
326341
}
327342

343+
pub fn with_backtrace(self, backtrace: Backtrace) -> Self {
344+
Self { backtrace, ..self }
345+
}
346+
328347
/// Whether or not to include test functions in parsing, type-checking and codegen.
329348
///
330349
/// This should be set to `true` by invocations like `forc test` or `forc check --tests`.

sway-core/src/lib.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,9 @@ pub use asm_generation::from_ir::compile_ir_context_to_finalized_asm;
3232
use asm_generation::FinalizedAsm;
3333
pub use asm_generation::{CompiledBytecode, FinalizedEntry};
3434
pub use build_config::DbgGeneration;
35-
pub use build_config::{BuildConfig, BuildTarget, LspConfig, OptLevel, PrintAsm, PrintIr};
35+
pub use build_config::{
36+
Backtrace, BuildConfig, BuildTarget, LspConfig, OptLevel, PrintAsm, PrintIr,
37+
};
3638
use control_flow_analysis::ControlFlowGraph;
3739
pub use debug_generation::write_dwarf;
3840
use itertools::Itertools;

sway-core/src/metadata.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -208,8 +208,8 @@ impl MetadataManager {
208208
})
209209
}
210210

211-
// TODO: (BACKTRACING) Remove `#[allow(dead_code)]` once the backtracing is
212-
// implemented in the IR compilation.
211+
// TODO: (ABI-BACKTRACING) Remove `#[allow(dead_code)]` once the backtracing is
212+
// implemented in IR compilation.
213213
#[allow(dead_code)]
214214
pub(crate) fn md_to_trace(
215215
&mut self,

0 commit comments

Comments
 (0)