Wrap layouts in a LayoutRepr constructor

Part 1 of support semantic layout representations.
This commit is contained in:
Ayaz Hafiz 2023-05-10 13:22:10 -05:00
parent c2d2bd4bb9
commit c3eeb5e2cc
No known key found for this signature in database
GPG key ID: 0E2A37416A25EF58
32 changed files with 1254 additions and 1021 deletions

View file

@ -5,8 +5,8 @@ use crate::ir::{
GuardStmtSpec, JoinPointId, Literal, Param, Procs, Stmt,
};
use crate::layout::{
Builtin, InLayout, Layout, LayoutCache, LayoutInterner, TLLayoutInterner, TagIdIntType,
UnionLayout,
Builtin, InLayout, Layout, LayoutCache, LayoutInterner, LayoutRepr, TLLayoutInterner,
TagIdIntType, UnionLayout,
};
use roc_builtins::bitcode::{FloatWidth, IntWidth};
use roc_collections::all::{MutMap, MutSet};
@ -1015,8 +1015,8 @@ fn to_relevant_branch_help<'a>(
match layout {
UnionLayout::NonRecursive([[arg]])
if matches!(
interner.get(*arg),
Layout::Struct {
interner.get(*arg).repr,
LayoutRepr::Struct {
field_layouts: [_],
..
}
@ -1579,8 +1579,8 @@ fn path_to_expr_help<'a>(
PathInstruction::TagIndex { index, tag_id } => {
let index = *index;
match layout_interner.chase_recursive(layout) {
Layout::Union(union_layout) => {
match layout_interner.chase_recursive(layout).repr {
LayoutRepr::Union(union_layout) => {
let inner_expr = Expr::UnionAtIndex {
tag_id: *tag_id,
structure: symbol,
@ -1600,7 +1600,7 @@ fn path_to_expr_help<'a>(
layout = inner_layout;
}
Layout::Struct { field_layouts, .. } => {
LayoutRepr::Struct { field_layouts, .. } => {
debug_assert!(field_layouts.len() > 1);
let inner_expr = Expr::StructAtIndex {
@ -1632,8 +1632,8 @@ fn path_to_expr_help<'a>(
PathInstruction::ListIndex { index } => {
let list_sym = symbol;
match layout_interner.get(layout) {
Layout::Builtin(Builtin::List(elem_layout)) => {
match layout_interner.get(layout).repr {
LayoutRepr::Builtin(Builtin::List(elem_layout)) => {
let (index_sym, new_stores) = build_list_index_probe(env, list_sym, index);
stores.extend(new_stores);
@ -1679,8 +1679,8 @@ fn test_to_comparison<'a>(
// (e.g. record pattern guard matches)
debug_assert!(union.alternatives.len() > 1);
match layout_interner.chase_recursive(test_layout) {
Layout::Union(union_layout) => {
match layout_interner.chase_recursive(test_layout).repr {
LayoutRepr::Union(union_layout) => {
let lhs = Expr::Literal(Literal::Int((tag_id as i128).to_ne_bytes()));
let rhs = Expr::GetTagId {
@ -1769,8 +1769,8 @@ fn test_to_comparison<'a>(
let list_layout = test_layout;
let list_sym = rhs_symbol;
match layout_interner.get(list_layout) {
Layout::Builtin(Builtin::List(_elem_layout)) => {
match layout_interner.get(list_layout).repr {
LayoutRepr::Builtin(Builtin::List(_elem_layout)) => {
let real_len_expr = Expr::Call(Call {
call_type: CallType::LowLevel {
op: LowLevel::ListLen,
@ -2316,7 +2316,7 @@ fn decide_to_branching<'a>(
// We have learned more about the exact layout of the cond (based on the path)
// but tests are still relative to the original cond symbol
let inner_cond_layout_raw = layout_cache.interner.chase_recursive(inner_cond_layout);
let mut switch = if let Layout::Union(union_layout) = inner_cond_layout_raw {
let mut switch = if let LayoutRepr::Union(union_layout) = inner_cond_layout_raw.repr {
let tag_id_symbol = env.unique_symbol();
let temp = Stmt::Switch {
@ -2338,7 +2338,7 @@ fn decide_to_branching<'a>(
union_layout.tag_id_layout(),
env.arena.alloc(temp),
)
} else if let Layout::Builtin(Builtin::List(_)) = inner_cond_layout_raw {
} else if let LayoutRepr::Builtin(Builtin::List(_)) = inner_cond_layout_raw.repr {
let len_symbol = env.unique_symbol();
let switch = Stmt::Switch {
@ -2396,8 +2396,8 @@ fn boolean_all<'a>(arena: &'a Bump, tests: Vec<(Expr<'a>, Expr<'a>, InLayout<'a>
expr = Expr::RunLowLevel(
LowLevel::And,
arena.alloc([
(test, Layout::Builtin(Builtin::Int1)),
(expr, Layout::Builtin(Builtin::Int1)),
(test, LayoutRepr::Builtin(Builtin::Int1)),
(expr, LayoutRepr::Builtin(Builtin::Int1)),
]),
);
}

View file

@ -4,7 +4,7 @@ use roc_error_macros::internal_error;
use roc_module::symbol::Symbol;
use roc_std::RocDec;
use crate::layout::{Builtin, InLayout, Layout, LayoutInterner, TLLayoutInterner};
use crate::layout::{Builtin, InLayout, LayoutInterner, LayoutRepr, TLLayoutInterner};
use super::pattern::Pattern;
@ -83,22 +83,22 @@ pub fn make_num_literal<'a>(
num_str: &str,
num_value: IntOrFloatValue,
) -> NumLiteral {
match interner.get(layout) {
Layout::Builtin(Builtin::Int(width)) => match num_value {
match interner.get(layout).repr {
LayoutRepr::Builtin(Builtin::Int(width)) => match num_value {
IntOrFloatValue::Int(IntValue::I128(n)) => NumLiteral::Int(n, width),
IntOrFloatValue::Int(IntValue::U128(n)) => NumLiteral::U128(n),
IntOrFloatValue::Float(..) => {
internal_error!("Float value where int was expected, should have been a type error")
}
},
Layout::Builtin(Builtin::Float(width)) => match num_value {
LayoutRepr::Builtin(Builtin::Float(width)) => match num_value {
IntOrFloatValue::Float(n) => NumLiteral::Float(n, width),
IntOrFloatValue::Int(int_value) => match int_value {
IntValue::I128(n) => NumLiteral::Float(i128::from_ne_bytes(n) as f64, width),
IntValue::U128(n) => NumLiteral::Float(u128::from_ne_bytes(n) as f64, width),
},
},
Layout::Builtin(Builtin::Decimal) => {
LayoutRepr::Builtin(Builtin::Decimal) => {
let dec = match RocDec::from_str(num_str) {
Some(d) => d,
None => internal_error!(

View file

@ -1,7 +1,7 @@
use crate::ir::{substitute_in_exprs, Env, Expr, Procs, Stmt};
use crate::layout::{
self, Builtin, InLayout, Layout, LayoutCache, LayoutInterner, LayoutProblem, TagIdIntType,
UnionLayout, WrappedVariant,
self, Builtin, InLayout, Layout, LayoutCache, LayoutInterner, LayoutProblem, LayoutRepr,
TagIdIntType, UnionLayout, WrappedVariant,
};
use bumpalo::collections::Vec;
use roc_builtins::bitcode::{FloatWidth, IntWidth};
@ -344,8 +344,8 @@ fn from_can_pattern_help<'a>(
StrLiteral(v) => Ok(Pattern::StrLiteral(v.clone())),
SingleQuote(var, _, c, _) => {
let layout = layout_cache.from_var(env.arena, *var, env.subs);
match layout.map(|l| layout_cache.get_in(l)) {
Ok(Layout::Builtin(Builtin::Int(width))) => {
match layout.map(|l| layout_cache.get_in(l).repr) {
Ok(LayoutRepr::Builtin(Builtin::Int(width))) => {
Ok(Pattern::IntLiteral((*c as i128).to_ne_bytes(), width))
}
o => internal_error!("an integer width was expected, but we found {:?}", o),
@ -583,11 +583,12 @@ fn from_can_pattern_help<'a>(
// problems down the line because we hash layouts and an unrolled
// version is not the same as the minimal version.
let whole_var_layout = layout_cache.from_var(env.arena, *whole_var, env.subs);
let layout =
match whole_var_layout.map(|l| layout_cache.interner.chase_recursive(l)) {
Ok(Layout::Union(ul)) => ul,
_ => internal_error!(),
};
let layout = match whole_var_layout
.map(|l| layout_cache.interner.chase_recursive(l).repr)
{
Ok(LayoutRepr::Union(ul)) => ul,
_ => internal_error!(),
};
use WrappedVariant::*;
match variant {
@ -1551,9 +1552,11 @@ fn store_tag_pattern<'a>(
for (index, (argument, arg_layout)) in arguments.iter().enumerate().rev() {
let mut arg_layout = *arg_layout;
if let Layout::RecursivePointer(_) = layout_cache.get_in(arg_layout) {
if let LayoutRepr::RecursivePointer(_) = layout_cache.get_in(arg_layout).repr {
// TODO(recursive-layouts): fix after disjoint rec ptrs
arg_layout = layout_cache.put_in(Layout::Union(union_layout));
arg_layout = layout_cache.put_in(Layout {
repr: LayoutRepr::Union(union_layout),
});
}
let load = Expr::UnionAtIndex {
@ -1629,7 +1632,7 @@ fn store_newtype_pattern<'a>(
for (index, (argument, arg_layout)) in arguments.iter().enumerate().rev() {
let mut arg_layout = *arg_layout;
if let Layout::RecursivePointer(_) = layout_cache.get_in(arg_layout) {
if let LayoutRepr::RecursivePointer(_) = layout_cache.get_in(arg_layout).repr {
arg_layout = layout;
}