Skip to content

Commit 69ac39e

Browse files
vaivaswathaIGI-111
andauthored
unique constants in the IR (#6896)
First step as part of #6351 . --------- Co-authored-by: IGI-111 <igi-111@protonmail.com>
1 parent 988be9b commit 69ac39e

25 files changed

+641
-379
lines changed

sway-core/src/asm_generation/fuel/data_section.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
use rustc_hash::FxHashMap;
2-
use sway_ir::{size_bytes_round_up_to_word_alignment, Constant, ConstantValue, Context, Padding};
2+
use sway_ir::{
3+
size_bytes_round_up_to_word_alignment, ConstantContent, ConstantValue, Context, Padding,
4+
};
35

46
use std::{fmt, iter::repeat};
57

@@ -89,7 +91,7 @@ impl Entry {
8991

9092
pub(crate) fn from_constant(
9193
context: &Context,
92-
constant: &Constant,
94+
constant: &ConstantContent,
9395
name: EntryName,
9496
padding: Option<Padding>,
9597
) -> Entry {

sway-core/src/asm_generation/fuel/fuel_asm_builder.rs

+10-5
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ impl AsmBuilder for FuelAsmBuilder<'_, '_> {
102102
ConfigContent::V0 { name, constant, .. } => {
103103
let entry = Entry::from_constant(
104104
self.context,
105-
constant,
105+
constant.get_content(self.context),
106106
EntryName::Configurable(name.clone()),
107107
None,
108108
);
@@ -1130,7 +1130,7 @@ impl<'ir, 'eng> FuelAsmBuilder<'ir, 'eng> {
11301130
idx_val
11311131
.get_constant(self.context)
11321132
.and_then(|idx_const| {
1133-
if let ConstantValue::Uint(idx) = idx_const.value {
1133+
if let ConstantValue::Uint(idx) = idx_const.get_content(self.context).value {
11341134
Some(idx as usize)
11351135
} else {
11361136
None
@@ -2072,7 +2072,7 @@ impl<'ir, 'eng> FuelAsmBuilder<'ir, 'eng> {
20722072
config_name: Option<String>,
20732073
span: Option<Span>,
20742074
) -> (VirtualRegister, Option<DataId>) {
2075-
match &constant.value {
2075+
match &constant.get_content(self.context).value {
20762076
// Use cheaper $zero or $one registers if possible.
20772077
ConstantValue::Unit | ConstantValue::Bool(false) | ConstantValue::Uint(0)
20782078
if config_name.is_none() =>
@@ -2091,7 +2091,12 @@ impl<'ir, 'eng> FuelAsmBuilder<'ir, 'eng> {
20912091
} else {
20922092
EntryName::NonConfigurable
20932093
};
2094-
let entry = Entry::from_constant(self.context, constant, config_name, None);
2094+
let entry = Entry::from_constant(
2095+
self.context,
2096+
constant.get_content(self.context),
2097+
config_name,
2098+
None,
2099+
);
20952100
let data_id = self.data_section.insert_data_value(entry);
20962101

20972102
// Allocate a register for it, and a load instruction.
@@ -2137,7 +2142,7 @@ impl<'ir, 'eng> FuelAsmBuilder<'ir, 'eng> {
21372142
.or_else(|| {
21382143
value.get_constant(self.context).map(|constant| {
21392144
let span = self.md_mgr.val_to_span(self.context, *value);
2140-
match constant.value {
2145+
match constant.get_content(self.context).value {
21412146
// If it's a small enough constant, just initialize using an IMM value.
21422147
// (exceptions for zero and one as they have special registers).
21432148
ConstantValue::Uint(c)

sway-core/src/asm_generation/fuel/functions.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -834,7 +834,7 @@ impl FuelAsmBuilder<'_, '_> {
834834
ptr.is_mutable(self.context),
835835
ptr.get_initializer(self.context),
836836
) {
837-
match constant.value {
837+
match constant.get_content(self.context).value {
838838
ConstantValue::Uint(c) if c <= compiler_constants::EIGHTEEN_BITS => {
839839
self.ptr_map.insert(
840840
*ptr,
@@ -848,7 +848,7 @@ impl FuelAsmBuilder<'_, '_> {
848848
let data_id =
849849
self.data_section.insert_data_value(Entry::from_constant(
850850
self.context,
851-
constant,
851+
constant.get_content(self.context),
852852
EntryName::NonConfigurable,
853853
None,
854854
));
@@ -863,7 +863,7 @@ impl FuelAsmBuilder<'_, '_> {
863863
let var_size = ptr_ty.size(self.context);
864864

865865
if let Some(constant) = ptr.get_initializer(self.context) {
866-
match constant.value {
866+
match constant.get_content(self.context).value {
867867
ConstantValue::Uint(c) if c <= compiler_constants::EIGHTEEN_BITS => {
868868
let imm = VirtualImmediate18::new_unchecked(
869869
c,
@@ -879,7 +879,7 @@ impl FuelAsmBuilder<'_, '_> {
879879
let data_id =
880880
self.data_section.insert_data_value(Entry::from_constant(
881881
self.context,
882-
constant,
882+
constant.get_content(self.context),
883883
EntryName::NonConfigurable,
884884
None,
885885
));

sway-core/src/ir_generation/compile.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -356,7 +356,7 @@ pub(crate) fn compile_configurables(
356356
let opt_metadata = md_mgr.span_to_md(context, &decl.span);
357357

358358
if context.experimental.new_encoding {
359-
let mut encoded_bytes = match constant.value {
359+
let mut encoded_bytes = match constant.get_content(context).value.clone() {
360360
ConstantValue::RawUntypedSlice(bytes) => bytes,
361361
_ => unreachable!(),
362362
};
@@ -651,11 +651,12 @@ fn compile_fn(
651651
// Special case: sometimes the returned value at the end of the function block is hacked
652652
// together and is invalid. This can happen with diverging control flow or with implicit
653653
// returns. We can double check here and make sure the return value type is correct.
654+
let undef = Constant::unique(context, ConstantContent::get_undef(ret_type));
654655
ret_val = match ret_val.get_type(context) {
655656
Some(ret_val_type) if ret_type.eq(context, &ret_val_type) => ret_val,
656657

657658
// Mismatched or unavailable type. Set ret_val to a correctly typed Undef.
658-
_otherwise => Value::new_constant(context, Constant::get_undef(ret_type)),
659+
_otherwise => Value::new_constant(context, undef),
659660
};
660661

661662
// Another special case: if the last expression in a function is a return then we don't want to
@@ -676,7 +677,7 @@ fn compile_fn(
676677
|| compiler.current_block.num_predecessors(context) > 0)
677678
{
678679
if ret_type.is_unit(context) {
679-
ret_val = Constant::get_unit(context);
680+
ret_val = ConstantContent::get_unit(context);
680681
}
681682
compiler
682683
.current_block

0 commit comments

Comments
 (0)