Skip to content

Commit d49cf8e

Browse files
authored
compile string constants to global rather than local memory (#7359)
In situations such as in the newly added test (taken from the fuels-rs repo), without this change, we end up returning a pointer to local memory.
1 parent 9e98c0e commit d49cf8e

File tree

31 files changed

+207
-116
lines changed

31 files changed

+207
-116
lines changed

forc-plugins/forc-client/tests/deploy.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -377,7 +377,7 @@ async fn test_simple_deploy() {
377377
node.kill().unwrap();
378378
let expected = vec![DeployedPackage::Contract(DeployedContract {
379379
id: ContractId::from_str(
380-
"440b559604961bdbeaa31421823f34e45d246b5d139aa4886e5e1cf2901fc925",
380+
"cd3c03c273d48ff262ee7caae1b8e3f98d4b0fd61eeb4ba65ad5c761c13e7197",
381381
)
382382
.unwrap(),
383383
proxy: None,
@@ -421,7 +421,7 @@ async fn test_deploy_submit_only() {
421421
node.kill().unwrap();
422422
let expected = vec![DeployedPackage::Contract(DeployedContract {
423423
id: ContractId::from_str(
424-
"440b559604961bdbeaa31421823f34e45d246b5d139aa4886e5e1cf2901fc925",
424+
"cd3c03c273d48ff262ee7caae1b8e3f98d4b0fd61eeb4ba65ad5c761c13e7197",
425425
)
426426
.unwrap(),
427427
proxy: None,
@@ -468,12 +468,12 @@ async fn test_deploy_fresh_proxy() {
468468
node.kill().unwrap();
469469
let impl_contract = DeployedPackage::Contract(DeployedContract {
470470
id: ContractId::from_str(
471-
"440b559604961bdbeaa31421823f34e45d246b5d139aa4886e5e1cf2901fc925",
471+
"cd3c03c273d48ff262ee7caae1b8e3f98d4b0fd61eeb4ba65ad5c761c13e7197",
472472
)
473473
.unwrap(),
474474
proxy: Some(
475475
ContractId::from_str(
476-
"19d465200575ebd085300242002efcda38db99e22449a5c1346588efe9ced7f7",
476+
"76cda1ee36cd11066bc858b4ef2bb32024ef16c338a612ca0fe102c0c31d5b5c",
477477
)
478478
.unwrap(),
479479
),

forc/tests/cli_integration.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,10 @@ fn test_forc_test_raw_logs() -> Result<(), rexpect::error::Error> {
5151
// Assert that the output is correct
5252
process.exp_string(" test test_log_4")?;
5353
process.exp_string("raw logs:")?;
54-
process.exp_string(r#"[{"LogData":{"data":"0000000000000004","digest":"8005f02d43fa06e7d0585fb64c961d57e318b27a145c857bcd3a6bdb413ff7fc","id":"0000000000000000000000000000000000000000000000000000000000000000","is":10368,"len":8,"pc":12456,"ptr":67107840,"ra":0,"rb":1515152261580153489}}]"#)?;
54+
process.exp_string(r#"[{"LogData":{"data":"0000000000000004","digest":"8005f02d43fa06e7d0585fb64c961d57e318b27a145c857bcd3a6bdb413ff7fc","id":"0000000000000000000000000000000000000000000000000000000000000000","is":10368,"len":8,"pc":12448,"ptr":67107840,"ra":0,"rb":1515152261580153489}}]"#)?;
5555
process.exp_string(" test test_log_2")?;
5656
process.exp_string("raw logs:")?;
57-
process.exp_string(r#"[{"LogData":{"data":"0000000000000002","digest":"cd04a4754498e06db5a13c5f371f1f04ff6d2470f24aa9bd886540e5dce77f70","id":"0000000000000000000000000000000000000000000000000000000000000000","is":10368,"len":8,"pc":12456,"ptr":67107840,"ra":0,"rb":1515152261580153489}}]"#)?;
57+
process.exp_string(r#"[{"LogData":{"data":"0000000000000002","digest":"cd04a4754498e06db5a13c5f371f1f04ff6d2470f24aa9bd886540e5dce77f70","id":"0000000000000000000000000000000000000000000000000000000000000000","is":10368,"len":8,"pc":12448,"ptr":67107840,"ra":0,"rb":1515152261580153489}}]"#)?;
5858

5959
process.process.exit()?;
6060
Ok(())
@@ -77,12 +77,12 @@ fn test_forc_test_both_logs() -> Result<(), rexpect::error::Error> {
7777
process.exp_string("decoded log values:")?;
7878
process.exp_string("4, log rb: 1515152261580153489")?;
7979
process.exp_string("raw logs:")?;
80-
process.exp_string(r#"[{"LogData":{"data":"0000000000000004","digest":"8005f02d43fa06e7d0585fb64c961d57e318b27a145c857bcd3a6bdb413ff7fc","id":"0000000000000000000000000000000000000000000000000000000000000000","is":10368,"len":8,"pc":12456,"ptr":67107840,"ra":0,"rb":1515152261580153489}}]"#)?;
80+
process.exp_string(r#"[{"LogData":{"data":"0000000000000004","digest":"8005f02d43fa06e7d0585fb64c961d57e318b27a145c857bcd3a6bdb413ff7fc","id":"0000000000000000000000000000000000000000000000000000000000000000","is":10368,"len":8,"pc":12448,"ptr":67107840,"ra":0,"rb":1515152261580153489}}]"#)?;
8181
process.exp_string(" test test_log_2")?;
8282
process.exp_string("decoded log values:")?;
8383
process.exp_string("2, log rb: 1515152261580153489")?;
8484
process.exp_string("raw logs:")?;
85-
process.exp_string(r#"[{"LogData":{"data":"0000000000000002","digest":"cd04a4754498e06db5a13c5f371f1f04ff6d2470f24aa9bd886540e5dce77f70","id":"0000000000000000000000000000000000000000000000000000000000000000","is":10368,"len":8,"pc":12456,"ptr":67107840,"ra":0,"rb":1515152261580153489}}]"#)?;
85+
process.exp_string(r#"[{"LogData":{"data":"0000000000000002","digest":"cd04a4754498e06db5a13c5f371f1f04ff6d2470f24aa9bd886540e5dce77f70","id":"0000000000000000000000000000000000000000000000000000000000000000","is":10368,"len":8,"pc":12448,"ptr":67107840,"ra":0,"rb":1515152261580153489}}]"#)?;
8686
process.process.exit()?;
8787
Ok(())
8888
}

sway-core/src/ir_generation/function.rs

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -541,12 +541,22 @@ impl<'a> FnCompiler<'a> {
541541
match &ast_expr.expression {
542542
ty::TyExpressionVariant::Literal(Literal::String(s)) => {
543543
let string_data =
544-
ConstantContent::get_string(context, s.as_str().as_bytes().to_vec());
545-
let string_data_ptr =
546-
store_to_memory(self, context, CompiledValue::InRegister(string_data))?
547-
.expect_memory();
544+
ConstantContent::get_string(context, s.as_str().as_bytes().to_vec())
545+
.get_constant(context)
546+
.unwrap();
547+
let string_data_ptr = self.module.new_unique_global_var(
548+
context,
549+
"__const_global".into(),
550+
string_data.get_content(context).ty,
551+
Some(*string_data),
552+
false,
553+
);
554+
let string_ptr = self
555+
.current_block
556+
.append(context)
557+
.get_global(string_data_ptr);
548558
let string_len = s.as_str().len() as u64;
549-
self.compile_string_slice(context, span_md_idx, string_data_ptr, string_len)
559+
self.compile_string_slice(context, span_md_idx, string_ptr, string_len)
550560
}
551561
ty::TyExpressionVariant::Literal(Literal::Numeric(n)) => {
552562
let implied_lit = match &*self.engines.te().get(ast_expr.return_type) {

sway-ir/src/module.rs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,42 @@ impl Module {
8585
.insert(call_path, const_val);
8686
}
8787

88+
/// Add a value to the module global storage, by forcing the name to be unique if needed.
89+
///
90+
/// Will use the provided name as a hint and eventually rename it to guarantee insertion.
91+
pub fn new_unique_global_var(
92+
&self,
93+
context: &mut Context,
94+
name: String,
95+
local_type: Type,
96+
initializer: Option<Constant>,
97+
mutable: bool,
98+
) -> GlobalVar {
99+
let module = &context.modules[self.0];
100+
let new_name = if module.global_variables.contains_key(&vec![name.clone()]) {
101+
// Assuming that we'll eventually find a unique name by appending numbers to the old
102+
// one...
103+
(0..)
104+
.find_map(|n| {
105+
let candidate = format!("{name}{n}");
106+
if module
107+
.global_variables
108+
.contains_key(&vec![candidate.clone()])
109+
{
110+
None
111+
} else {
112+
Some(candidate)
113+
}
114+
})
115+
.unwrap()
116+
} else {
117+
name
118+
};
119+
let gv = GlobalVar::new(context, local_type, initializer, mutable);
120+
self.add_global_variable(context, vec![new_name], gv);
121+
gv
122+
}
123+
88124
/// Get a named global variable from this module, if found.
89125
pub fn get_global_variable(
90126
&self,

test/src/e2e_vm_tests/test_programs/should_pass/language/attributes_deprecated/stdout.snap

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
---
22
source: test/src/snapshot/mod.rs
3+
assertion_line: 162
34
---
45
> forc build --path test/src/e2e_vm_tests/test_programs/should_pass/language/attributes_deprecated --release
56
exit status: 0
@@ -182,4 +183,4 @@ warning: Function is deprecated
182183
____
183184

184185
Compiled contract "attributes_deprecated" with 19 warnings.
185-
Finished release [optimized + fuel] target(s) [920 B] in ???
186+
Finished release [optimized + fuel] target(s) [880 B] in ???

test/src/e2e_vm_tests/test_programs/should_pass/language/configurable_consts/json_abi_oracle_new_encoding.json

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -63,97 +63,97 @@
6363
"concreteTypeId": "b760f44fa5965c2474a3b471467a22c43185152129295af588b022ae50b50903",
6464
"indirect": false,
6565
"name": "BOOL",
66-
"offset": 4928
66+
"offset": 4912
6767
},
6868
{
6969
"concreteTypeId": "c89951a24c6ca28c13fd1cfdc646b2b656d69e61a92b91023be7eb58eb914b6b",
7070
"indirect": false,
7171
"name": "U8",
72-
"offset": 5120
72+
"offset": 5104
7373
},
7474
{
7575
"concreteTypeId": "c89951a24c6ca28c13fd1cfdc646b2b656d69e61a92b91023be7eb58eb914b6b",
7676
"indirect": false,
7777
"name": "ANOTHER_U8",
78-
"offset": 4856
78+
"offset": 4840
7979
},
8080
{
8181
"concreteTypeId": "29881aad8730c5ab11d275376323d8e4ff4179aae8ccb6c13fe4902137e162ef",
8282
"indirect": false,
8383
"name": "U16",
84-
"offset": 5064
84+
"offset": 5048
8585
},
8686
{
8787
"concreteTypeId": "d7649d428b9ff33d188ecbf38a7e4d8fd167fa01b2e10fe9a8f9308e52f1d7cc",
8888
"indirect": false,
8989
"name": "U32",
90-
"offset": 5104
90+
"offset": 5088
9191
},
9292
{
9393
"concreteTypeId": "d7649d428b9ff33d188ecbf38a7e4d8fd167fa01b2e10fe9a8f9308e52f1d7cc",
9494
"indirect": false,
9595
"name": "U64",
96-
"offset": 5112
96+
"offset": 5096
9797
},
9898
{
9999
"concreteTypeId": "1b5759d94094368cfd443019e7ca5ec4074300e544e5ea993a979f5da627261e",
100100
"indirect": false,
101101
"name": "U256",
102-
"offset": 5072
102+
"offset": 5056
103103
},
104104
{
105105
"concreteTypeId": "7c5ee1cecf5f8eacd1284feb5f0bf2bdea533a51e2f0c9aabe9236d335989f3b",
106106
"indirect": false,
107107
"name": "B256",
108-
"offset": 4896
108+
"offset": 4880
109109
},
110110
{
111111
"concreteTypeId": "81fc10c4681a3271cf2d66b2ec6fbc8ed007a442652930844fcf11818c295bff",
112112
"indirect": false,
113113
"name": "CONFIGURABLE_STRUCT",
114-
"offset": 5016
114+
"offset": 5000
115115
},
116116
{
117117
"concreteTypeId": "a2922861f03be8a650595dd76455b95383a61b46dd418f02607fa2e00dc39d5c",
118118
"indirect": false,
119119
"name": "CONFIGURABLE_ENUM_A",
120-
"offset": 4936
120+
"offset": 4920
121121
},
122122
{
123123
"concreteTypeId": "a2922861f03be8a650595dd76455b95383a61b46dd418f02607fa2e00dc39d5c",
124124
"indirect": false,
125125
"name": "CONFIGURABLE_ENUM_B",
126-
"offset": 4976
126+
"offset": 4960
127127
},
128128
{
129129
"concreteTypeId": "4926d35d1a5157936b0a29bc126b8aace6d911209a5c130e9b716b0c73643ea6",
130130
"indirect": false,
131131
"name": "ARRAY_BOOL",
132-
"offset": 4864
132+
"offset": 4848
133133
},
134134
{
135135
"concreteTypeId": "776fb5a3824169d6736138565fdc20aad684d9111266a5ff6d5c675280b7e199",
136136
"indirect": false,
137137
"name": "ARRAY_U64",
138-
"offset": 4872
138+
"offset": 4856
139139
},
140140
{
141141
"concreteTypeId": "c998ca9a5f221fe7b5c66ae70c8a9562b86d964408b00d17f883c906bc1fe4be",
142142
"indirect": false,
143143
"name": "TUPLE_BOOL_U64",
144-
"offset": 5048
144+
"offset": 5032
145145
},
146146
{
147147
"concreteTypeId": "94f0fa95c830be5e4f711963e83259fe7e8bc723278ab6ec34449e791a99b53a",
148148
"indirect": false,
149149
"name": "STR_4",
150-
"offset": 5040
150+
"offset": 5024
151151
},
152152
{
153153
"concreteTypeId": "c89951a24c6ca28c13fd1cfdc646b2b656d69e61a92b91023be7eb58eb914b6b",
154154
"indirect": false,
155155
"name": "NOT_USED",
156-
"offset": 5032
156+
"offset": 5016
157157
}
158158
],
159159
"encodingVersion": "1",

test/src/e2e_vm_tests/test_programs/should_pass/language/const_generics/stdout.snap

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -444,12 +444,12 @@ warning
444444
____
445445

446446
Compiled script "const_generics" with 2 warnings.
447-
Finished debug [unoptimized + fuel] target(s) [9.536 KB] in ???
447+
Finished debug [unoptimized + fuel] target(s) [8.8 KB] in ???
448448
Running 1 test, filtered 0 tests
449449

450450
tested -- const_generics
451451

452-
test run_main ... ok (???, 17350 gas)
452+
test run_main ... ok (???, 17159 gas)
453453
debug output:
454454
[src/main.sw:105:13] a = [1, 2]
455455
[src/main.sw:109:13] [C {}].len() = 1

test/src/e2e_vm_tests/test_programs/should_pass/language/intrinsics/dbg/stdout.snap

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,12 +72,12 @@ output:
7272
Building test/src/e2e_vm_tests/test_programs/should_pass/language/intrinsics/dbg
7373
Compiling library std (sway-lib-std)
7474
Compiling script dbg (test/src/e2e_vm_tests/test_programs/should_pass/language/intrinsics/dbg)
75-
Finished debug [unoptimized + fuel] target(s) [42.456 KB] in ???
75+
Finished debug [unoptimized + fuel] target(s) [38.584 KB] in ???
7676
Running 1 test, filtered 0 tests
7777

7878
tested -- dbg
7979

80-
test call_main ... ok (???, 122721 gas)
80+
test call_main ... ok (???, 121555 gas)
8181
debug output:
8282
[src/main.sw:13:13] () = ()
8383
[src/main.sw:15:13] true = true

test/src/e2e_vm_tests/test_programs/should_pass/language/intrinsics/dbg_release/stdout.snap

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,12 @@ ecal $r1 $r0 $zero $zero ; ecal id fd zero zero
1919
ecal $r4 $r0 $r1 $r3 ; ecal id fd buf count
2020

2121
> forc build --path test/src/e2e_vm_tests/test_programs/should_pass/language/intrinsics/dbg_release --release --asm final | sub ecal
22-
ecal $r4 $r5 $r0 $r3 ; ecal id fd buf count
22+
ecal $r2 $r3 $r0 $r1 ; ecal id fd buf count
2323
ecal $r0 $r1 $zero $zero ; ecal id fd zero zero
2424
ecal $r0 $r1 $zero $zero ; ecal id fd zero zero
2525
ecal $r0 $r1 $zero $zero ; ecal id fd zero zero
2626
ecal $r0 $r1 $zero $zero ; ecal id fd zero zero
27-
ecal $r3 $r4 $r0 $r2 ; ecal id fd buf count
27+
ecal $r2 $r3 $r0 $r1 ; ecal id fd buf count
2828
ecal $r0 $r1 $zero $zero ; ecal id fd zero zero
2929
ecal $r0 $r1 $zero $zero ; ecal id fd zero zero
3030
ecal $r0 $r1 $zero $zero ; ecal id fd zero zero

0 commit comments

Comments
 (0)