Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add llvm codegen for collections.static_array #2003

Merged
merged 18 commits into from
Mar 20, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -12,6 +12,9 @@ target/
# Failing proptest artifacts
proptest-regressions/

# snaphsot test failures
*.snap.new

# Devenv
.devenv*
devenv.local.nix
59 changes: 34 additions & 25 deletions hugr-core/src/std_extensions/collections/static_array.rs
Original file line number Diff line number Diff line change
@@ -14,23 +14,27 @@
//! * `get<T: Copyable>: [static_array<T>, prelude.usize] -> [[] + [T]]`
//! * `len<T: Copyable>: [static_array<T>] -> [prelude.usize]`
use std::{
hash, iter,
hash::{self, Hash as _},
iter,
sync::{self, Arc},
};

use crate::{
builder::{BuildError, Dataflow},
extension::{
prelude::{option_type, usize_t},
resolution::{ExtensionResolutionError, WeakExtensionRegistry},
resolution::{
resolve_type_extensions, resolve_value_extensions, ExtensionResolutionError,
WeakExtensionRegistry,
},
simple_op::{
try_from_name, HasConcrete, HasDef, MakeExtensionOp, MakeOpDef, MakeRegisteredOp,
OpLoadError,
},
ExtensionId, ExtensionSet, OpDef, SignatureError, SignatureFunc, TypeDef,
},
ops::{
constant::{CustomConst, TryHash, ValueName},
constant::{maybe_hash_values, CustomConst, TryHash, ValueName},
ExtensionOp, NamedOp, OpName, Value,
},
types::{
@@ -41,9 +45,6 @@ use crate::{
Extension, Wire,
};

use super::array::ArrayValue;

use delegate::delegate;
use lazy_static::lazy_static;

/// Reported unique name of the extension
@@ -53,22 +54,25 @@ pub const STATIC_ARRAY_TYPENAME: TypeName = TypeName::new_inline("static_array")
/// Extension version.
pub const VERSION: semver::Version = semver::Version::new(0, 1, 0);

#[derive(Debug, Clone, serde::Serialize, serde::Deserialize, PartialEq, derive_more::From)]
#[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize, derive_more::From)]
/// Statically sized array of values, all of the same [TypeBound::Copyable]
/// type.
pub struct StaticArrayValue {
/// The contents of the `StaticArrayValue`.
pub value: ArrayValue,
values: Vec<Value>,
typ: Type,
/// The name of the `StaticArrayValue`.
pub name: String,
}

impl StaticArrayValue {
delegate! {
to self.value {
/// Returns the type of values inside the `[StaticArrayValue]`.
pub fn get_element_type(&self) -> &Type;
}
/// Returns the type of values inside the `[StaticArrayValue]`.
pub fn get_element_type(&self) -> &Type {
&self.typ
}

/// Returns the values contained inside the `[StaticArrayValue]`.
pub fn get_contents(&self) -> &[Value] {
&self.values
}

/// Create a new [CustomConst] for an array of values of type `typ`.
@@ -85,7 +89,8 @@ impl StaticArrayValue {
.into());
}
Ok(Self {
value: ArrayValue::new(typ, contents),
typ,
values: contents.into_iter().collect(),
name: name.to_string(),
})
}
@@ -102,9 +107,12 @@ impl StaticArrayValue {
}

impl TryHash for StaticArrayValue {
fn try_hash(&self, st: &mut dyn hash::Hasher) -> bool {
st.write(self.name.as_bytes());
self.value.try_hash(st)
fn try_hash(&self, mut st: &mut dyn hash::Hasher) -> bool {
maybe_hash_values(&self.values, &mut st) && {
self.name.hash(&mut st);
self.typ.hash(&mut st);
true
}
}
}

@@ -123,17 +131,18 @@ impl CustomConst for StaticArrayValue {
}

fn extension_reqs(&self) -> ExtensionSet {
ExtensionSet::union_over(self.value.get_contents().iter().map(Value::extension_reqs))
ExtensionSet::union_over(self.values.iter().map(Value::extension_reqs))
.union(EXTENSION_ID.into())
}

delegate! {
to self.value {
fn update_extensions(
&mut self,
extensions: &WeakExtensionRegistry,
) -> Result<(), ExtensionResolutionError>;
fn update_extensions(
&mut self,
extensions: &WeakExtensionRegistry,
) -> Result<(), ExtensionResolutionError> {
for val in &mut self.values {
resolve_value_extensions(val, extensions)?;
}
resolve_type_extensions(&mut self.typ, extensions)
}
}

This file was deleted.

This file was deleted.

1 change: 1 addition & 0 deletions hugr-llvm/src/emit/test.rs
Original file line number Diff line number Diff line change
@@ -163,6 +163,7 @@ impl SimpleHugrConfig {
logic::EXTENSION_ID,
collections::array::EXTENSION_ID,
collections::list::EXTENSION_ID,
collections::static_array::EXTENSION_ID,
]),
),
)
1 change: 1 addition & 0 deletions hugr-llvm/src/extension/collections.rs
Original file line number Diff line number Diff line change
@@ -2,3 +2,4 @@
pub mod array;
pub mod list;
pub mod static_array;
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
---
source: hugr-llvm/src/extension/collections/static_array.rs
expression: mod_str
---
; ModuleID = 'test_context'
source_filename = "test_context"

@sa.inner.6acc1b76.0 = constant { i64, [0 x i64] } zeroinitializer
@sa.inner.e637bb5.0 = constant { i64, [1 x i64] } { i64 1, [1 x i64] [i64 1] }
@sa.inner.2b6593f.0 = constant { i64, [2 x i64] } { i64 2, [2 x i64] [i64 2, i64 2] }
@sa.inner.1b9ad7c.0 = constant { i64, [3 x i64] } { i64 3, [3 x i64] [i64 3, i64 3, i64 3] }
@sa.inner.e67fbfa4.0 = constant { i64, [4 x i64] } { i64 4, [4 x i64] [i64 4, i64 4, i64 4, i64 4] }
@sa.inner.15dc27f6.0 = constant { i64, [5 x i64] } { i64 5, [5 x i64] [i64 5, i64 5, i64 5, i64 5, i64 5] }
@sa.inner.c43a2bb2.0 = constant { i64, [6 x i64] } { i64 6, [6 x i64] [i64 6, i64 6, i64 6, i64 6, i64 6, i64 6] }
@sa.inner.7f5d5e16.0 = constant { i64, [7 x i64] } { i64 7, [7 x i64] [i64 7, i64 7, i64 7, i64 7, i64 7, i64 7, i64 7] }
@sa.inner.a0bc9c53.0 = constant { i64, [8 x i64] } { i64 8, [8 x i64] [i64 8, i64 8, i64 8, i64 8, i64 8, i64 8, i64 8, i64 8] }
@sa.inner.1e8aada3.0 = constant { i64, [9 x i64] } { i64 9, [9 x i64] [i64 9, i64 9, i64 9, i64 9, i64 9, i64 9, i64 9, i64 9, i64 9] }
@sa.outer.c4a5911a.0 = constant { i64, [10 x { i64, [0 x i64] }*] } { i64 10, [10 x { i64, [0 x i64] }*] [{ i64, [0 x i64] }* @sa.inner.6acc1b76.0, { i64, [0 x i64] }* bitcast ({ i64, [1 x i64] }* @sa.inner.e637bb5.0 to { i64, [0 x i64] }*), { i64, [0 x i64] }* bitcast ({ i64, [2 x i64] }* @sa.inner.2b6593f.0 to { i64, [0 x i64] }*), { i64, [0 x i64] }* bitcast ({ i64, [3 x i64] }* @sa.inner.1b9ad7c.0 to { i64, [0 x i64] }*), { i64, [0 x i64] }* bitcast ({ i64, [4 x i64] }* @sa.inner.e67fbfa4.0 to { i64, [0 x i64] }*), { i64, [0 x i64] }* bitcast ({ i64, [5 x i64] }* @sa.inner.15dc27f6.0 to { i64, [0 x i64] }*), { i64, [0 x i64] }* bitcast ({ i64, [6 x i64] }* @sa.inner.c43a2bb2.0 to { i64, [0 x i64] }*), { i64, [0 x i64] }* bitcast ({ i64, [7 x i64] }* @sa.inner.7f5d5e16.0 to { i64, [0 x i64] }*), { i64, [0 x i64] }* bitcast ({ i64, [8 x i64] }* @sa.inner.a0bc9c53.0 to { i64, [0 x i64] }*), { i64, [0 x i64] }* bitcast ({ i64, [9 x i64] }* @sa.inner.1e8aada3.0 to { i64, [0 x i64] }*)] }

define i64 @_hl.main.1() {
alloca_block:
br label %entry_block

entry_block: ; preds = %alloca_block
%0 = getelementptr inbounds { i64, [0 x { i64, [0 x i64] }*] }, { i64, [0 x { i64, [0 x i64] }*] }* bitcast ({ i64, [10 x { i64, [0 x i64] }*] }* @sa.outer.c4a5911a.0 to { i64, [0 x { i64, [0 x i64] }*] }*), i32 0, i32 0
%1 = load i64, i64* %0, align 4
ret i64 %1
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
---
source: hugr-llvm/src/extension/collections/static_array.rs
expression: mod_str
---
; ModuleID = 'test_context'
source_filename = "test_context"

@sa.inner.6acc1b76.0 = constant { i64, [0 x i64] } zeroinitializer
@sa.inner.e637bb5.0 = constant { i64, [1 x i64] } { i64 1, [1 x i64] [i64 1] }
@sa.inner.2b6593f.0 = constant { i64, [2 x i64] } { i64 2, [2 x i64] [i64 2, i64 2] }
@sa.inner.1b9ad7c.0 = constant { i64, [3 x i64] } { i64 3, [3 x i64] [i64 3, i64 3, i64 3] }
@sa.inner.e67fbfa4.0 = constant { i64, [4 x i64] } { i64 4, [4 x i64] [i64 4, i64 4, i64 4, i64 4] }
@sa.inner.15dc27f6.0 = constant { i64, [5 x i64] } { i64 5, [5 x i64] [i64 5, i64 5, i64 5, i64 5, i64 5] }
@sa.inner.c43a2bb2.0 = constant { i64, [6 x i64] } { i64 6, [6 x i64] [i64 6, i64 6, i64 6, i64 6, i64 6, i64 6] }
@sa.inner.7f5d5e16.0 = constant { i64, [7 x i64] } { i64 7, [7 x i64] [i64 7, i64 7, i64 7, i64 7, i64 7, i64 7, i64 7] }
@sa.inner.a0bc9c53.0 = constant { i64, [8 x i64] } { i64 8, [8 x i64] [i64 8, i64 8, i64 8, i64 8, i64 8, i64 8, i64 8, i64 8] }
@sa.inner.1e8aada3.0 = constant { i64, [9 x i64] } { i64 9, [9 x i64] [i64 9, i64 9, i64 9, i64 9, i64 9, i64 9, i64 9, i64 9, i64 9] }
@sa.outer.c4a5911a.0 = constant { i64, [10 x { i64, [0 x i64] }*] } { i64 10, [10 x { i64, [0 x i64] }*] [{ i64, [0 x i64] }* @sa.inner.6acc1b76.0, { i64, [0 x i64] }* bitcast ({ i64, [1 x i64] }* @sa.inner.e637bb5.0 to { i64, [0 x i64] }*), { i64, [0 x i64] }* bitcast ({ i64, [2 x i64] }* @sa.inner.2b6593f.0 to { i64, [0 x i64] }*), { i64, [0 x i64] }* bitcast ({ i64, [3 x i64] }* @sa.inner.1b9ad7c.0 to { i64, [0 x i64] }*), { i64, [0 x i64] }* bitcast ({ i64, [4 x i64] }* @sa.inner.e67fbfa4.0 to { i64, [0 x i64] }*), { i64, [0 x i64] }* bitcast ({ i64, [5 x i64] }* @sa.inner.15dc27f6.0 to { i64, [0 x i64] }*), { i64, [0 x i64] }* bitcast ({ i64, [6 x i64] }* @sa.inner.c43a2bb2.0 to { i64, [0 x i64] }*), { i64, [0 x i64] }* bitcast ({ i64, [7 x i64] }* @sa.inner.7f5d5e16.0 to { i64, [0 x i64] }*), { i64, [0 x i64] }* bitcast ({ i64, [8 x i64] }* @sa.inner.a0bc9c53.0 to { i64, [0 x i64] }*), { i64, [0 x i64] }* bitcast ({ i64, [9 x i64] }* @sa.inner.1e8aada3.0 to { i64, [0 x i64] }*)] }

define i64 @_hl.main.1() {
alloca_block:
%"0" = alloca i64, align 8
%"5_0" = alloca { i64, [0 x { i64, [0 x i64] }*] }*, align 8
%"6_0" = alloca i64, align 8
br label %entry_block

entry_block: ; preds = %alloca_block
store { i64, [0 x { i64, [0 x i64] }*] }* bitcast ({ i64, [10 x { i64, [0 x i64] }*] }* @sa.outer.c4a5911a.0 to { i64, [0 x { i64, [0 x i64] }*] }*), { i64, [0 x { i64, [0 x i64] }*] }** %"5_0", align 8
%"5_01" = load { i64, [0 x { i64, [0 x i64] }*] }*, { i64, [0 x { i64, [0 x i64] }*] }** %"5_0", align 8
%0 = getelementptr inbounds { i64, [0 x { i64, [0 x i64] }*] }, { i64, [0 x { i64, [0 x i64] }*] }* %"5_01", i32 0, i32 0
%1 = load i64, i64* %0, align 4
store i64 %1, i64* %"6_0", align 4
%"6_02" = load i64, i64* %"6_0", align 4
store i64 %"6_02", i64* %"0", align 4
%"03" = load i64, i64* %"0", align 4
ret i64 %"03"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
---
source: hugr-llvm/src/extension/collections/static_array.rs
expression: mod_str
---
; ModuleID = 'test_context'
source_filename = "test_context"

@sa.a.97cb22bf.0 = constant { i64, [10 x i64] } { i64 10, [10 x i64] [i64 0, i64 1, i64 2, i64 3, i64 4, i64 5, i64 6, i64 7, i64 8, i64 9] }

define { i64, [0 x i64] }* @_hl.main.1() {
alloca_block:
br label %entry_block

entry_block: ; preds = %alloca_block
ret { i64, [0 x i64] }* bitcast ({ i64, [10 x i64] }* @sa.a.97cb22bf.0 to { i64, [0 x i64] }*)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
---
source: hugr-llvm/src/extension/collections/static_array.rs
expression: mod_str
---
; ModuleID = 'test_context'
source_filename = "test_context"

@sa.b.150c532c.0 = constant { i64, [10 x double] } { i64 10, [10 x double] [double 0.000000e+00, double 1.000000e+00, double 2.000000e+00, double 3.000000e+00, double 4.000000e+00, double 5.000000e+00, double 6.000000e+00, double 7.000000e+00, double 8.000000e+00, double 9.000000e+00] }

define { i64, [0 x double] }* @_hl.main.1() {
alloca_block:
br label %entry_block

entry_block: ; preds = %alloca_block
ret { i64, [0 x double] }* bitcast ({ i64, [10 x double] }* @sa.b.150c532c.0 to { i64, [0 x double] }*)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
---
source: hugr-llvm/src/extension/collections/static_array.rs
expression: mod_str
---
; ModuleID = 'test_context'
source_filename = "test_context"

@sa.c.911aa16d.0 = constant { i64, [10 x i1] } { i64 10, [10 x i1] [i1 true, i1 false, i1 true, i1 false, i1 true, i1 false, i1 true, i1 false, i1 true, i1 false] }

define { i64, [0 x i1] }* @_hl.main.1() {
alloca_block:
br label %entry_block

entry_block: ; preds = %alloca_block
ret { i64, [0 x i1] }* bitcast ({ i64, [10 x i1] }* @sa.c.911aa16d.0 to { i64, [0 x i1] }*)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
---
source: hugr-llvm/src/extension/collections/static_array.rs
expression: mod_str
---
; ModuleID = 'test_context'
source_filename = "test_context"

@sa.d.4c6da27.0 = constant { i64, [10 x { i1, i64 }] } { i64 10, [10 x { i1, i64 }] [{ i1, i64 } { i1 true, i64 0 }, { i1, i64 } { i1 true, i64 1 }, { i1, i64 } { i1 true, i64 2 }, { i1, i64 } { i1 true, i64 3 }, { i1, i64 } { i1 true, i64 4 }, { i1, i64 } { i1 true, i64 5 }, { i1, i64 } { i1 true, i64 6 }, { i1, i64 } { i1 true, i64 7 }, { i1, i64 } { i1 true, i64 8 }, { i1, i64 } { i1 true, i64 9 }] }

define { i64, [0 x { i1, i64 }] }* @_hl.main.1() {
alloca_block:
br label %entry_block

entry_block: ; preds = %alloca_block
ret { i64, [0 x { i1, i64 }] }* bitcast ({ i64, [10 x { i1, i64 }] }* @sa.d.4c6da27.0 to { i64, [0 x { i1, i64 }] }*)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
---
source: hugr-llvm/src/extension/collections/static_array.rs
expression: mod_str
---
; ModuleID = 'test_context'
source_filename = "test_context"

@sa.a.97cb22bf.0 = constant { i64, [10 x i64] } { i64 10, [10 x i64] [i64 0, i64 1, i64 2, i64 3, i64 4, i64 5, i64 6, i64 7, i64 8, i64 9] }

define { i64, [0 x i64] }* @_hl.main.1() {
alloca_block:
%"0" = alloca { i64, [0 x i64] }*, align 8
%"5_0" = alloca { i64, [0 x i64] }*, align 8
br label %entry_block

entry_block: ; preds = %alloca_block
store { i64, [0 x i64] }* bitcast ({ i64, [10 x i64] }* @sa.a.97cb22bf.0 to { i64, [0 x i64] }*), { i64, [0 x i64] }** %"5_0", align 8
%"5_01" = load { i64, [0 x i64] }*, { i64, [0 x i64] }** %"5_0", align 8
store { i64, [0 x i64] }* %"5_01", { i64, [0 x i64] }** %"0", align 8
%"02" = load { i64, [0 x i64] }*, { i64, [0 x i64] }** %"0", align 8
ret { i64, [0 x i64] }* %"02"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
---
source: hugr-llvm/src/extension/collections/static_array.rs
expression: mod_str
---
; ModuleID = 'test_context'
source_filename = "test_context"

@sa.b.150c532c.0 = constant { i64, [10 x double] } { i64 10, [10 x double] [double 0.000000e+00, double 1.000000e+00, double 2.000000e+00, double 3.000000e+00, double 4.000000e+00, double 5.000000e+00, double 6.000000e+00, double 7.000000e+00, double 8.000000e+00, double 9.000000e+00] }

define { i64, [0 x double] }* @_hl.main.1() {
alloca_block:
%"0" = alloca { i64, [0 x double] }*, align 8
%"5_0" = alloca { i64, [0 x double] }*, align 8
br label %entry_block

entry_block: ; preds = %alloca_block
store { i64, [0 x double] }* bitcast ({ i64, [10 x double] }* @sa.b.150c532c.0 to { i64, [0 x double] }*), { i64, [0 x double] }** %"5_0", align 8
%"5_01" = load { i64, [0 x double] }*, { i64, [0 x double] }** %"5_0", align 8
store { i64, [0 x double] }* %"5_01", { i64, [0 x double] }** %"0", align 8
%"02" = load { i64, [0 x double] }*, { i64, [0 x double] }** %"0", align 8
ret { i64, [0 x double] }* %"02"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
---
source: hugr-llvm/src/extension/collections/static_array.rs
expression: mod_str
---
; ModuleID = 'test_context'
source_filename = "test_context"

@sa.c.911aa16d.0 = constant { i64, [10 x i1] } { i64 10, [10 x i1] [i1 true, i1 false, i1 true, i1 false, i1 true, i1 false, i1 true, i1 false, i1 true, i1 false] }

define { i64, [0 x i1] }* @_hl.main.1() {
alloca_block:
%"0" = alloca { i64, [0 x i1] }*, align 8
%"5_0" = alloca { i64, [0 x i1] }*, align 8
br label %entry_block

entry_block: ; preds = %alloca_block
store { i64, [0 x i1] }* bitcast ({ i64, [10 x i1] }* @sa.c.911aa16d.0 to { i64, [0 x i1] }*), { i64, [0 x i1] }** %"5_0", align 8
%"5_01" = load { i64, [0 x i1] }*, { i64, [0 x i1] }** %"5_0", align 8
store { i64, [0 x i1] }* %"5_01", { i64, [0 x i1] }** %"0", align 8
%"02" = load { i64, [0 x i1] }*, { i64, [0 x i1] }** %"0", align 8
ret { i64, [0 x i1] }* %"02"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
---
source: hugr-llvm/src/extension/collections/static_array.rs
expression: mod_str
---
; ModuleID = 'test_context'
source_filename = "test_context"

@sa.d.4c6da27.0 = constant { i64, [10 x { i1, i64 }] } { i64 10, [10 x { i1, i64 }] [{ i1, i64 } { i1 true, i64 0 }, { i1, i64 } { i1 true, i64 1 }, { i1, i64 } { i1 true, i64 2 }, { i1, i64 } { i1 true, i64 3 }, { i1, i64 } { i1 true, i64 4 }, { i1, i64 } { i1 true, i64 5 }, { i1, i64 } { i1 true, i64 6 }, { i1, i64 } { i1 true, i64 7 }, { i1, i64 } { i1 true, i64 8 }, { i1, i64 } { i1 true, i64 9 }] }

define { i64, [0 x { i1, i64 }] }* @_hl.main.1() {
alloca_block:
%"0" = alloca { i64, [0 x { i1, i64 }] }*, align 8
%"5_0" = alloca { i64, [0 x { i1, i64 }] }*, align 8
br label %entry_block

entry_block: ; preds = %alloca_block
store { i64, [0 x { i1, i64 }] }* bitcast ({ i64, [10 x { i1, i64 }] }* @sa.d.4c6da27.0 to { i64, [0 x { i1, i64 }] }*), { i64, [0 x { i1, i64 }] }** %"5_0", align 8
%"5_01" = load { i64, [0 x { i1, i64 }] }*, { i64, [0 x { i1, i64 }] }** %"5_0", align 8
store { i64, [0 x { i1, i64 }] }* %"5_01", { i64, [0 x { i1, i64 }] }** %"0", align 8
%"02" = load { i64, [0 x { i1, i64 }] }*, { i64, [0 x { i1, i64 }] }** %"0", align 8
ret { i64, [0 x { i1, i64 }] }* %"02"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
---
source: hugr-llvm/src/extension/collections/static_array.rs
expression: mod_str
---
; ModuleID = 'test_context'
source_filename = "test_context"

define { i1, i64 } @_hl.main.1({ i64, [0 x i64] }* %0, i64 %1) {
alloca_block:
br label %entry_block

static_array_get_out_of_bounds: ; preds = %entry_block
br label %static_array_get_exit

static_array_get_in_bounds: ; preds = %entry_block
%2 = getelementptr inbounds { i64, [0 x i64] }, { i64, [0 x i64] }* %0, i32 0, i32 1, i64 %1
%3 = load i64, i64* %2, align 4
%4 = insertvalue { i1, i64 } { i1 true, i64 poison }, i64 %3, 1
br label %static_array_get_exit

static_array_get_exit: ; preds = %static_array_get_in_bounds, %static_array_get_out_of_bounds
%"03.0" = phi { i1, i64 } [ %4, %static_array_get_in_bounds ], [ { i1 false, i64 poison }, %static_array_get_out_of_bounds ]
ret { i1, i64 } %"03.0"

entry_block: ; preds = %alloca_block
%5 = getelementptr inbounds { i64, [0 x i64] }, { i64, [0 x i64] }* %0, i32 0, i32 0
%6 = load i64, i64* %5, align 4
%7 = icmp ult i64 %1, %6
br i1 %7, label %static_array_get_in_bounds, label %static_array_get_out_of_bounds
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
---
source: hugr-llvm/src/extension/collections/static_array.rs
expression: mod_str
---
; ModuleID = 'test_context'
source_filename = "test_context"

define { i1, i1 } @_hl.main.1({ i64, [0 x i1] }* %0, i64 %1) {
alloca_block:
br label %entry_block

static_array_get_out_of_bounds: ; preds = %entry_block
br label %static_array_get_exit

static_array_get_in_bounds: ; preds = %entry_block
%2 = getelementptr inbounds { i64, [0 x i1] }, { i64, [0 x i1] }* %0, i32 0, i32 1, i64 %1
%3 = load i1, i1* %2, align 1
%4 = insertvalue { i1, i1 } { i1 true, i1 poison }, i1 %3, 1
br label %static_array_get_exit

static_array_get_exit: ; preds = %static_array_get_in_bounds, %static_array_get_out_of_bounds
%"03.0" = phi { i1, i1 } [ %4, %static_array_get_in_bounds ], [ { i1 false, i1 poison }, %static_array_get_out_of_bounds ]
ret { i1, i1 } %"03.0"

entry_block: ; preds = %alloca_block
%5 = getelementptr inbounds { i64, [0 x i1] }, { i64, [0 x i1] }* %0, i32 0, i32 0
%6 = load i64, i64* %5, align 4
%7 = icmp ult i64 %1, %6
br i1 %7, label %static_array_get_in_bounds, label %static_array_get_out_of_bounds
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
---
source: hugr-llvm/src/extension/collections/static_array.rs
expression: mod_str
---
; ModuleID = 'test_context'
source_filename = "test_context"

define i64 @_hl.main.1({ i64, [0 x i64] }* %0) {
alloca_block:
br label %entry_block

entry_block: ; preds = %alloca_block
%1 = getelementptr inbounds { i64, [0 x i64] }, { i64, [0 x i64] }* %0, i32 0, i32 0
%2 = load i64, i64* %1, align 4
ret i64 %2
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
---
source: hugr-llvm/src/extension/collections/static_array.rs
expression: mod_str
---
; ModuleID = 'test_context'
source_filename = "test_context"

define i64 @_hl.main.1({ i64, [0 x i1] }* %0) {
alloca_block:
br label %entry_block

entry_block: ; preds = %alloca_block
%1 = getelementptr inbounds { i64, [0 x i1] }, { i64, [0 x i1] }* %0, i32 0, i32 0
%2 = load i64, i64* %1, align 4
ret i64 %2
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
---
source: hugr-llvm/src/extension/collections/static_array.rs
expression: mod_str
---
; ModuleID = 'test_context'
source_filename = "test_context"

define { i1, i64 } @_hl.main.1({ i64, [0 x i64] }* %0, i64 %1) {
alloca_block:
%"0" = alloca { i1, i64 }, align 8
%"2_0" = alloca { i64, [0 x i64] }*, align 8
%"2_1" = alloca i64, align 8
%"4_0" = alloca { i1, i64 }, align 8
%"03" = alloca { i1, i64 }, align 8
br label %entry_block

static_array_get_out_of_bounds: ; preds = %entry_block
store { i1, i64 } { i1 false, i64 poison }, { i1, i64 }* %"03", align 4
br label %static_array_get_exit

static_array_get_in_bounds: ; preds = %entry_block
%2 = getelementptr inbounds { i64, [0 x i64] }, { i64, [0 x i64] }* %"2_01", i32 0, i32 1, i64 %"2_12"
%3 = load i64, i64* %2, align 4
%4 = insertvalue { i1, i64 } { i1 true, i64 poison }, i64 %3, 1
store { i1, i64 } %4, { i1, i64 }* %"03", align 4
br label %static_array_get_exit

static_array_get_exit: ; preds = %static_array_get_in_bounds, %static_array_get_out_of_bounds
%"04" = load { i1, i64 }, { i1, i64 }* %"03", align 4
store { i1, i64 } %"04", { i1, i64 }* %"4_0", align 4
%"4_05" = load { i1, i64 }, { i1, i64 }* %"4_0", align 4
store { i1, i64 } %"4_05", { i1, i64 }* %"0", align 4
%"06" = load { i1, i64 }, { i1, i64 }* %"0", align 4
ret { i1, i64 } %"06"

entry_block: ; preds = %alloca_block
store { i64, [0 x i64] }* %0, { i64, [0 x i64] }** %"2_0", align 8
store i64 %1, i64* %"2_1", align 4
%"2_01" = load { i64, [0 x i64] }*, { i64, [0 x i64] }** %"2_0", align 8
%"2_12" = load i64, i64* %"2_1", align 4
%5 = getelementptr inbounds { i64, [0 x i64] }, { i64, [0 x i64] }* %"2_01", i32 0, i32 0
%6 = load i64, i64* %5, align 4
%7 = icmp ult i64 %"2_12", %6
br i1 %7, label %static_array_get_in_bounds, label %static_array_get_out_of_bounds
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
---
source: hugr-llvm/src/extension/collections/static_array.rs
expression: mod_str
---
; ModuleID = 'test_context'
source_filename = "test_context"

define { i1, i1 } @_hl.main.1({ i64, [0 x i1] }* %0, i64 %1) {
alloca_block:
%"0" = alloca { i1, i1 }, align 8
%"2_0" = alloca { i64, [0 x i1] }*, align 8
%"2_1" = alloca i64, align 8
%"4_0" = alloca { i1, i1 }, align 8
%"03" = alloca { i1, i1 }, align 8
br label %entry_block

static_array_get_out_of_bounds: ; preds = %entry_block
store { i1, i1 } { i1 false, i1 poison }, { i1, i1 }* %"03", align 1
br label %static_array_get_exit

static_array_get_in_bounds: ; preds = %entry_block
%2 = getelementptr inbounds { i64, [0 x i1] }, { i64, [0 x i1] }* %"2_01", i32 0, i32 1, i64 %"2_12"
%3 = load i1, i1* %2, align 1
%4 = insertvalue { i1, i1 } { i1 true, i1 poison }, i1 %3, 1
store { i1, i1 } %4, { i1, i1 }* %"03", align 1
br label %static_array_get_exit

static_array_get_exit: ; preds = %static_array_get_in_bounds, %static_array_get_out_of_bounds
%"04" = load { i1, i1 }, { i1, i1 }* %"03", align 1
store { i1, i1 } %"04", { i1, i1 }* %"4_0", align 1
%"4_05" = load { i1, i1 }, { i1, i1 }* %"4_0", align 1
store { i1, i1 } %"4_05", { i1, i1 }* %"0", align 1
%"06" = load { i1, i1 }, { i1, i1 }* %"0", align 1
ret { i1, i1 } %"06"

entry_block: ; preds = %alloca_block
store { i64, [0 x i1] }* %0, { i64, [0 x i1] }** %"2_0", align 8
store i64 %1, i64* %"2_1", align 4
%"2_01" = load { i64, [0 x i1] }*, { i64, [0 x i1] }** %"2_0", align 8
%"2_12" = load i64, i64* %"2_1", align 4
%5 = getelementptr inbounds { i64, [0 x i1] }, { i64, [0 x i1] }* %"2_01", i32 0, i32 0
%6 = load i64, i64* %5, align 4
%7 = icmp ult i64 %"2_12", %6
br i1 %7, label %static_array_get_in_bounds, label %static_array_get_out_of_bounds
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
---
source: hugr-llvm/src/extension/collections/static_array.rs
expression: mod_str
---
; ModuleID = 'test_context'
source_filename = "test_context"

define i64 @_hl.main.1({ i64, [0 x i64] }* %0) {
alloca_block:
%"0" = alloca i64, align 8
%"2_0" = alloca { i64, [0 x i64] }*, align 8
%"4_0" = alloca i64, align 8
br label %entry_block

entry_block: ; preds = %alloca_block
store { i64, [0 x i64] }* %0, { i64, [0 x i64] }** %"2_0", align 8
%"2_01" = load { i64, [0 x i64] }*, { i64, [0 x i64] }** %"2_0", align 8
%1 = getelementptr inbounds { i64, [0 x i64] }, { i64, [0 x i64] }* %"2_01", i32 0, i32 0
%2 = load i64, i64* %1, align 4
store i64 %2, i64* %"4_0", align 4
%"4_02" = load i64, i64* %"4_0", align 4
store i64 %"4_02", i64* %"0", align 4
%"03" = load i64, i64* %"0", align 4
ret i64 %"03"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
---
source: hugr-llvm/src/extension/collections/static_array.rs
expression: mod_str
---
; ModuleID = 'test_context'
source_filename = "test_context"

define i64 @_hl.main.1({ i64, [0 x i1] }* %0) {
alloca_block:
%"0" = alloca i64, align 8
%"2_0" = alloca { i64, [0 x i1] }*, align 8
%"4_0" = alloca i64, align 8
br label %entry_block

entry_block: ; preds = %alloca_block
store { i64, [0 x i1] }* %0, { i64, [0 x i1] }** %"2_0", align 8
%"2_01" = load { i64, [0 x i1] }*, { i64, [0 x i1] }** %"2_0", align 8
%1 = getelementptr inbounds { i64, [0 x i1] }, { i64, [0 x i1] }* %"2_01", i32 0, i32 0
%2 = load i64, i64* %1, align 4
store i64 %2, i64* %"4_0", align 4
%"4_02" = load i64, i64* %"4_0", align 4
store i64 %"4_02", i64* %"0", align 4
%"03" = load i64, i64* %"0", align 4
ret i64 %"03"
}
572 changes: 572 additions & 0 deletions hugr-llvm/src/extension/collections/static_array.rs

Large diffs are not rendered by default.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

5 changes: 3 additions & 2 deletions hugr-py/src/hugr/std/collections/static_array.py
Original file line number Diff line number Diff line change
@@ -41,7 +41,7 @@
"""Constant value for a statically sized immutable array of elements."""

v: list[val.Value]
ty: tys.Type
ty: StaticArray
name: str

def __init__(self, v: list[val.Value], elem_ty: tys.Type, name: str) -> None:
@@ -53,7 +53,8 @@
# The value list must be serialized at this point, otherwise the
# `Extension` value would not be serializable.
vs = [v._to_serial_root() for v in self.v]
serial_val = {"values": vs, "name": self.name}
element_ty = self.ty.ty._to_serial_root()
serial_val = {"values": vs, "name": self.name, "typ": element_ty}

Check warning on line 57 in hugr-py/src/hugr/std/collections/static_array.py

Codecov / codecov/patch

hugr-py/src/hugr/std/collections/static_array.py#L56-L57

Added lines #L56 - L57 were not covered by tests
return val.Extension(
"StaticArrayValue", typ=self.ty, val=serial_val, extensions=[EXTENSION.name]
)