You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
invalid call to '_merge': argument #0 cannot be converted from
'Span2[type, Origin2(span_life._mlir_origin)]' to 'Span2[type, Origin2((muttoimm span_life._mlir_origin))]'
In the nightly branch _merge[cmp_fn](span1, span2, temp_buff) also doesn't work but if I cast the first _merge[cmp_fn](span1.get_immut(), span2, temp_buff) it does (see PR #3823).
In both cases I think it should infer the whole thing, otherwise it defeats the purpose of having the implicit Mutable -> Immutable casting.
from memory import UnsafePointer
aliasImmutableOrigin= Origin2[False]
aliasMutableOrigin= Origin2[True]
@value@register_passable("trivial")
struct Span2[
is_mutable: Bool, //,
T: CollectionElement,
origin: Origin2[is_mutable],
](CollectionElementNew):
aliasmut= Span2[T, ImmutableOrigin.cast_from[origin].result]
"""The mutable version of the Span."""aliasimmut= Span2[T, MutableOrigin.cast_from[origin].result]
"""The immutable version of the Span."""# Fieldsvar_data: UnsafePointer[T]
var_len: Int
@doc_private@implicit@always_inline("nodebug")
fn__init__(out self: Self.immut, ref other: Self.mut):
"""Implicitly cast the mutable origin of self to an immutable one. Args: other: The Span to cast."""self= rebind[Self.immut](other)
@always_inlinefnget_immut(self) -> Self.immut:
"""Return an immutable version of this Span. Returns: An immutable version of the same Span."""return rebind[Self.immut](self)
@always_inlinefn__init__(out self, *, ptr: UnsafePointer[T], length: Int):
self._data = ptr
self._len = length
@always_inlinefn__init__(out self, *, other: Self):
self._data = other._data
self._len = other._len
fnunsafe_ptr(self) -> UnsafePointer[T]:
returnself._data
fn__len__(self) -> Int:
returnself._len
@always_inlinefn__getitem__(self, idx: Int) -> ref [origin] T:
#TODO: Simplify this with a UInt type.
debug_assert(
-self._len <=int(idx) <self._len, "index must be within bounds"
)
varoffset= idx
if offset <0:
offset +=len(self)
returnself._data[offset]
@always_inlinefn__getitem__(self, slc: Slice) -> Self:
varstart: Int
varend: Int
varstep: Int
start, end, step = slc.indices(len(self))
debug_assert(
step ==1, "Slice must be within bounds and step must be 1"
)
varres= Self(
ptr=(self._data + start), length=len(range(start, end, step))
)
return res
@value@register_passable("trivial")
struct Origin2[is_mutable: Bool]:
"""This represents a origin reference for a memory value. Parameters: is_mutable: Whether the origin is mutable."""alias_mlir_type= __mlir_type[
`!lit.origin<`,
is_mutable.value,
`>`,
]
aliascast_from= _lit_mut_cast[result_mutable=is_mutable]
var_mlir_origin: Self._mlir_type
@doc_private@implicit@always_inline("nodebug")
fn__init__(out self, mlir_origin: Self._mlir_type):
self._mlir_origin = mlir_origin
@doc_private@implicit@always_inline("nodebug")
fn__init__(out self: ImmutableOrigin, origin: MutableOrigin):
"""Initialize an ImmutableOrigin from a MutableOrigin. Args: origin: The origin value."""self= rebind[ImmutableOrigin](origin)
struct _lit_mut_cast[
is_mutable: Bool, //,
result_mutable: Bool,
operand: Origin2[is_mutable],
]:
aliasresult= __mlir_attr[
`#lit.origin.mutcast<`,
operand._mlir_origin,
`> : !lit.origin<`,
result_mutable.value,
`>`,
]
@valuestruct _SortWrapper[type: CollectionElement](CollectionElement):
vardata: type@implicitfn__init__(out self, data: type):
self.data = data
fn__init__(out self, *, other: Self):
self.data = other.data
@always_inlinefn_insertion_sort[
type: CollectionElement,
origin: MutableOrigin, //,
cmp_fn: fn (_SortWrapper[type], _SortWrapper[type]) capturing [_] -> Bool,
](span: Span2[type, origin]):
vararray= span.unsafe_ptr()
varsize=len(span)
for i inrange(1, size):
varvalue= array[i]
varj= i
# Find the placement of the value in the array, shifting as we try to# find the position. Throughout, we assume array[start:i] has already# been sorted.while j >0and cmp_fn(value, array[j -1]):
array[j] = array[j -1]
j -=1
array[j] = value
fn_merge[
type: CollectionElement,
span_origin: ImmutableOrigin,
result_origin: MutableOrigin, //,
cmp_fn: fn (_SortWrapper[type], _SortWrapper[type]) capturing [_] -> Bool,
](
span1: Span2[type, span_origin],
span2: Span2[type, span_origin],
result: Span2[type, result_origin],
):
varspan1_size=len(span1)
varspan2_size=len(span2)
varres_ptr= result.unsafe_ptr()
debug_assert(
span1_size + span2_size <=len(result),
"The merge result does not fit in the span provided",
)
vari=0varj=0vark=0while i < span1_size:
if j == span2_size:
while i < span1_size:
(res_ptr + k).init_pointee_copy(span1[i])
k +=1
i +=1returnif cmp_fn(span2[j], span1[i]):
(res_ptr + k).init_pointee_copy(span2[j])
j +=1else:
(res_ptr + k).init_pointee_copy(span1[i])
i +=1
k +=1while j < span2_size:
(res_ptr + k).init_pointee_copy(span2[j])
k +=1
j +=1aliasinsertion_sort_threshold=32fn_stable_sort_impl[
type: CollectionElement,
span_life: MutableOrigin,
tmp_life: MutableOrigin, //,
cmp_fn: fn (_SortWrapper[type], _SortWrapper[type]) capturing [_] -> Bool,
](span: Span2[type, span_life], temp_buff: Span2[type, tmp_life]):
varsize=len(span)
if size <=1:
returnvari=0while i < size:
_insertion_sort[cmp_fn](
span[i : min(i + insertion_sort_threshold, size)]
)
i += insertion_sort_threshold
varmerge_size= insertion_sort_threshold
while merge_size < size:
varj=0while j + merge_size < size:
varspan1= span[j : j + merge_size]
varspan2= span[j + merge_size : min(size, j +2* merge_size)]
_merge[cmp_fn](span1, span2, temp_buff)
for i inrange(merge_size +len(span2)):
span[j + i] = temp_buff[i]
j +=2* merge_size
merge_size *=2fn_stable_sort[
type: CollectionElement,
origin: MutableOrigin, //,
cmp_fn: fn (_SortWrapper[type], _SortWrapper[type]) capturing [_] -> Bool,
](span: Span2[type, origin]):
vartemp_buff= UnsafePointer[type].alloc(len(span))
vartemp_buff_span= Span2[type, __origin_of(temp_buff)](
ptr=temp_buff, length=len(span)
)
_stable_sort_impl[cmp_fn](span, temp_buff_span)
temp_buff.free()
fn_sort[
type: CollectionElement,
origin: MutableOrigin, //,
cmp_fn: fn (_SortWrapper[type], _SortWrapper[type]) capturing [_] -> Bool,
*,
](span: Span2[type, origin]):
_stable_sort[cmp_fn](span)
fnsort[
type: DType,
origin: MutableOrigin, //,
cmp_fn: fn (Scalar[type], Scalar[type]) capturing [_] -> Bool,
*,
](span: Span2[Scalar[type], origin]):
@parameterfn_cmp_fn(
lhs: _SortWrapper[Scalar[type]], rhs: _SortWrapper[Scalar[type]]
) -> Bool:
return cmp_fn(lhs.data, rhs.data)
_sort[_cmp_fn](span)
fnsort[
origin: MutableOrigin, //,
*,
](span: Span2[Int64, origin]):
@parameterfn_cmp_fn(lhs: Int64, rhs: Int64) -> Bool:
return lhs < rhs
sort[_cmp_fn](span)
fnmain():
items = List[Int64](3, 4, 5, 1, 2)
span = Span2[Int64, origin = __origin_of(items)](
ptr=items.unsafe_ptr(), length=len(items)
)
sort(span)
print(items.__str__())
System information
- What OS did you do install Mojo on ?
- Provide version information for Mojo by pasting the output of `mojo -v``mojo 24.6.0.dev2024120705`
- Provide Magic CLI version by pasting the output of `magic -V` or `magic --version`
- Optionally, provide more information with `magic info`.
The text was updated successfully, but these errors were encountered:
I think that this would also remove the need for the @__unsafe_disable_nested_origin_exclusivity decorator in many functions since they would just be annotated with ImmutableOrigin for the readonly ops, and the implicit constructors would then handle any value with a MutableOrigin that is passed in
Bug description
Shortest repro I could manage. Here it does not infer the type correctly even if the whole thing is specified
output:
In the nightly branch
_merge[cmp_fn](span1, span2, temp_buff)
also doesn't work but if I cast the first_merge[cmp_fn](span1.get_immut(), span2, temp_buff)
it does (see PR #3823).In both cases I think it should infer the whole thing, otherwise it defeats the purpose of having the implicit Mutable -> Immutable casting.
CC: @lattner, @ConnorGray
Steps to reproduce
System information
The text was updated successfully, but these errors were encountered: