JIMC
JIMC
Explore posts from servers
MModular
Created by JIMC on 12/4/2023 in #questions
Trying to do compile time checking with 'const' type arguments.
11 replies
MModular
Created by JIMC on 12/4/2023 in #questions
Trying to do compile time checking with 'const' type arguments.
Error:
root@DESKTOP-N84UN90:~/mojo_projs# mojo sosecrets-mojo/src/secret.mojo
/root/mojo_projs/sosecrets-mojo/src/secret.mojo:35:62: error: 'kgen.pack.get' op result type '!kgen.anyregtype' does not match pack element type at index 1 : index: #kgen.concretetype.constant<!kgen.struct<(index) memoryOnly>> : !kgen.anyregtype
let my_new_secret = my_69_secret_tuple.get[1, AnyRegType]()
^
/root/mojo_projs/sosecrets-mojo/src/secret.mojo:35:62: note: see current operation: %7 = "kgen.pack.get"(%4) {index = 1 : index} : (!kgen.pack<[index, struct<(index) memoryOnly>]>) -> !kgen.anyregtype
mojo: error: failed to run the pass manager
root@DESKTOP-N84UN90:~/mojo_projs# mojo sosecrets-mojo/src/secret.mojo
/root/mojo_projs/sosecrets-mojo/src/secret.mojo:35:62: error: 'kgen.pack.get' op result type '!kgen.anyregtype' does not match pack element type at index 1 : index: #kgen.concretetype.constant<!kgen.struct<(index) memoryOnly>> : !kgen.anyregtype
let my_new_secret = my_69_secret_tuple.get[1, AnyRegType]()
^
/root/mojo_projs/sosecrets-mojo/src/secret.mojo:35:62: note: see current operation: %7 = "kgen.pack.get"(%4) {index = 1 : index} : (!kgen.pack<[index, struct<(index) memoryOnly>]>) -> !kgen.anyregtype
mojo: error: failed to run the pass manager
11 replies
MModular
Created by JIMC on 12/4/2023 in #questions
Trying to do compile time checking with 'const' type arguments.
struct ExposedSecret[T: AnyRegType]:
var borrowed_value: T

fn __init__(inout self, borrowed borrowed_value: T):
self.borrowed_value = borrowed_value

struct Secret[T: AnyRegType, MEC: Int, EC: Int]:
var inner: T

fn __init__[EC: Int=EC](inout self, owned value: T):
constrained[EC == 0, "`Secret[T, MEC, EC]` can only be constructed iff `EC == 0`"]()
self.inner = value

@staticmethod
fn ec_add_one(owned other: Self) -> Secret[T, MEC, EC + 1]:
return other^

fn expose_secret[ReturnedType: AnyRegType](owned self, owned closure:
fn(exposed_secret: ExposedSecret[T]) capturing -> ReturnedType)
-> ListLiteral[ReturnedType, Secret[T, MEC, EC + 1]]:
constrained[EC < MEC, "`expose_secret` can only be called if `EC < MEC`"]()
let returned_value = closure(ExposedSecret[T](self.inner))
let new_self = Secret.ec_add_one(self^)
return [returned_value, new_self^]

fn main():
alias my_secret = Secret[Int, 3, 0](69)

@parameter
fn capturing_closure_but_actually_never_capture(borrowed exposed_secret: ExposedSecret[Int]) -> Int:
return exposed_secret.borrowed_value

let my_69_secret_tuple = my_secret.expose_secret[Int](capturing_closure_but_actually_never_capture)
let returned_69_secret = my_69_secret_tuple.get[0, Int]()
let my_new_secret = my_69_secret_tuple.get[1, AnyRegType]()
struct ExposedSecret[T: AnyRegType]:
var borrowed_value: T

fn __init__(inout self, borrowed borrowed_value: T):
self.borrowed_value = borrowed_value

struct Secret[T: AnyRegType, MEC: Int, EC: Int]:
var inner: T

fn __init__[EC: Int=EC](inout self, owned value: T):
constrained[EC == 0, "`Secret[T, MEC, EC]` can only be constructed iff `EC == 0`"]()
self.inner = value

@staticmethod
fn ec_add_one(owned other: Self) -> Secret[T, MEC, EC + 1]:
return other^

fn expose_secret[ReturnedType: AnyRegType](owned self, owned closure:
fn(exposed_secret: ExposedSecret[T]) capturing -> ReturnedType)
-> ListLiteral[ReturnedType, Secret[T, MEC, EC + 1]]:
constrained[EC < MEC, "`expose_secret` can only be called if `EC < MEC`"]()
let returned_value = closure(ExposedSecret[T](self.inner))
let new_self = Secret.ec_add_one(self^)
return [returned_value, new_self^]

fn main():
alias my_secret = Secret[Int, 3, 0](69)

@parameter
fn capturing_closure_but_actually_never_capture(borrowed exposed_secret: ExposedSecret[Int]) -> Int:
return exposed_secret.borrowed_value

let my_69_secret_tuple = my_secret.expose_secret[Int](capturing_closure_but_actually_never_capture)
let returned_69_secret = my_69_secret_tuple.get[0, Int]()
let my_new_secret = my_69_secret_tuple.get[1, AnyRegType]()
11 replies
MModular
Created by JIMC on 12/4/2023 in #questions
Trying to do compile time checking with 'const' type arguments.
@Moderator
11 replies
MModular
Created by JIMC on 12/4/2023 in #questions
Trying to do compile time checking with 'const' type arguments.
Trying to create a Secret class in Mojo such that if EC is greater than MEC, i.e. expose_secret() method has been called too many times, then it raises a compile time error. Sadly, I cannot really use Mojo ListLiteral or Tuple for this as I am returing a Tuple with my Secret[T, MEC, EC+1] type therein but I cannot retrieve it.
11 replies
MModular
Created by JIMC on 9/20/2023 in #questions
Cross-directory Imports
@Jack Clayton do you have any answer for this?
4 replies
MModular
Created by TeamPuzel on 9/12/2023 in #questions
`llvm.add` is interpreted as `llvm.addrspacecast`
have u read up on MLIR?
102 replies
MModular
Created by TeamPuzel on 9/12/2023 in #questions
`llvm.add` is interpreted as `llvm.addrspacecast`
no MLIR ops are supposed and can happen at compile time
102 replies
MModular
Created by TeamPuzel on 9/12/2023 in #questions
`llvm.add` is interpreted as `llvm.addrspacecast`
@register_passable('trivial')
struct UInt8:
var value: __mlir_type.ui8

fn __init__(value: Int) -> Self:
let value_as_index = value.__mlir_index__()
let _255 = __mlir_op.`index.castu`[_type : __mlir_type.ui8](__mlir_attr.`255:index`)
let _255_index = __mlir_op.`index.castu`[_type : __mlir_type.index](_255)
let greater_than_255 = __mlir_op.`index.cmp`[pred : __mlir_attr.`#index<cmp_predicate ugt>`](value_as_index, _255_index)
let _0 = __mlir_op.`index.castu`[_type : __mlir_type.index](__mlir_attr.`0`)
let lesser_than_0 = __mlir_op.`index.cmp`[pred : __mlir_attr.`#index<cmp_predicate ult>`](value_as_index, _0)
@parameter
if greater_than_255 or lesser_than_0:
constrained[False, "compile error"]()
return Self {
value: __mlir_op.`index.castu`[_type: __mlir_type.ui8](value_as_index)
}

fn into_int(self) -> Int:
return __mlir_op.`index.castu`[_type : __mlir_type.index](self.value)

fn main():
alias a: UInt8 = 4
print(a.into_int())
@register_passable('trivial')
struct UInt8:
var value: __mlir_type.ui8

fn __init__(value: Int) -> Self:
let value_as_index = value.__mlir_index__()
let _255 = __mlir_op.`index.castu`[_type : __mlir_type.ui8](__mlir_attr.`255:index`)
let _255_index = __mlir_op.`index.castu`[_type : __mlir_type.index](_255)
let greater_than_255 = __mlir_op.`index.cmp`[pred : __mlir_attr.`#index<cmp_predicate ugt>`](value_as_index, _255_index)
let _0 = __mlir_op.`index.castu`[_type : __mlir_type.index](__mlir_attr.`0`)
let lesser_than_0 = __mlir_op.`index.cmp`[pred : __mlir_attr.`#index<cmp_predicate ult>`](value_as_index, _0)
@parameter
if greater_than_255 or lesser_than_0:
constrained[False, "compile error"]()
return Self {
value: __mlir_op.`index.castu`[_type: __mlir_type.ui8](value_as_index)
}

fn into_int(self) -> Int:
return __mlir_op.`index.castu`[_type : __mlir_type.index](self.value)

fn main():
alias a: UInt8 = 4
print(a.into_int())
Error:
/marray# mojo array3.mojo
array3.mojo:13:10: error: cannot use a dynamic value in operator argument
if greater_than_255 or lesser_than_0:
^~~~~~~~~~~~~~~~
array3.mojo:13:10: error: cannot use a dynamic value in '@parameter if' condition
if greater_than_255 or lesser_than_0:
^~~~~~~~~~~~~~~~
array3.mojo:13:30: error: cannot use a dynamic value in boolean condition
if greater_than_255 or lesser_than_0:
^~~~~~~~~~~~~
mojo: error: failed to parse the provided Mojo
/marray# mojo array3.mojo
array3.mojo:13:10: error: cannot use a dynamic value in operator argument
if greater_than_255 or lesser_than_0:
^~~~~~~~~~~~~~~~
array3.mojo:13:10: error: cannot use a dynamic value in '@parameter if' condition
if greater_than_255 or lesser_than_0:
^~~~~~~~~~~~~~~~
array3.mojo:13:30: error: cannot use a dynamic value in boolean condition
if greater_than_255 or lesser_than_0:
^~~~~~~~~~~~~
mojo: error: failed to parse the provided Mojo
102 replies
MModular
Created by TeamPuzel on 9/12/2023 in #questions
`llvm.add` is interpreted as `llvm.addrspacecast`
i created an issue
102 replies
MModular
Created by TeamPuzel on 9/12/2023 in #questions
`llvm.add` is interpreted as `llvm.addrspacecast`
102 replies
MModular
Created by JIMC on 9/11/2023 in #questions
Compile time `U8`
@Chris Lattner Thank you for your reply, I have created an issue here: https://github.com/modularml/mojo/issues/776
21 replies
MModular
Created by Honour on 9/10/2023 in #questions
unable to pass by value
lol
28 replies
MModular
Created by Honour on 9/10/2023 in #questions
unable to pass by value
yeah value semantics has another name: copy semantics
28 replies
MModular
Created by tac on 9/12/2023 in #questions
__moveinit__ and __del__
Because move init is always favored over copy init
14 replies
MModular
Created by msthil2 on 9/12/2023 in #questions
Mojo Install Hanging in Codespaces
@msthil2 close the post if resolved
5 replies
MModular
Created by AndrewZ on 9/12/2023 in #questions
WSL2 - Mojo installation
please close the post if resolved
19 replies
MModular
Created by TeamPuzel on 9/12/2023 in #questions
`llvm.add` is interpreted as `llvm.addrspacecast`
@TeamPuzel you have any lots of lit dialect etc?
102 replies
MModular
Created by JIMC on 9/11/2023 in #questions
Compile time `U8`
fn main():
alias a: UInt8 = 4
print(a.into_int())
alias b: UInt8 = 257
print(b.into_int())
fn main():
alias a: UInt8 = 4
print(a.into_int())
alias b: UInt8 = 257
print(b.into_int())
21 replies
MModular
Created by JIMC on 9/11/2023 in #questions
Compile time `U8`
i dont unds why this fails
21 replies