M
Modular•15mo ago
JIMC

Compile time `U8`

@Modular Staff I'm writing to write a struct U8 that kind of models after Int. Here is my impl:
struct U8:
var value: __mlir_type.ui8

fn __init__(inout self, value: Int):
let value_as_index = value.__mlir_index__()
let _255 = __mlir_op.`index.castu`[_type : __mlir_type.index](__mlir_attr.`255`)
let greater_than_255 = __mlir_op.`index.cmp`[pred : __mlir_attr.`#index<cmp_predicate ugt>`](value_as_index, _255)
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)
self.value = __mlir_op.`index.castu`[_type: __mlir_type.ui8](value_as_index)
struct U8:
var value: __mlir_type.ui8

fn __init__(inout self, value: Int):
let value_as_index = value.__mlir_index__()
let _255 = __mlir_op.`index.castu`[_type : __mlir_type.index](__mlir_attr.`255`)
let greater_than_255 = __mlir_op.`index.cmp`[pred : __mlir_attr.`#index<cmp_predicate ugt>`](value_as_index, _255)
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)
self.value = __mlir_op.`index.castu`[_type: __mlir_type.ui8](value_as_index)
It does check for the range (0-255) but not shown because the code snippet is sufficient to showcase the error I faced which is:
root@DESKTOP-N84UN90:~/mojo_projs/marray# mojo array3.mojo
/__w/modular/modular/Kernels/mojo/builtin/_startup.mojo:70:1: error: no viable expansions found
/__w/modular/modular/Kernels/mojo/builtin/_startup.mojo:70:1: note: call expansion failed - no concrete specializations
/__w/modular/modular/Kernels/mojo/builtin/_startup.mojo:11:1: note: no viable expansions found
/__w/modular/modular/Kernels/mojo/builtin/_startup.mojo:23:14: note: call expansion failed - no concrete specializations
array3.mojo:31:1: note: no viable expansions found
fn main():
^
array3.mojo:32:4: note: failed to evaluate 'apply'
alias a: U8 = 4
^
array3.mojo:5:4: note: failed to interpret function @$array3::U8::__init__($array3::U8=&,$builtin::$int::Int)_concrete
fn __init__(inout self, value: Int):
^
array3.mojo:13:67: note: failed to fold operation index.castu(4 : index)
self.value = __mlir_op.`index.castu`[_type: __mlir_type.ui8](value_as_index)
^
mojo: error: failed to run the pass manager
root@DESKTOP-N84UN90:~/mojo_projs/marray# mojo array3.mojo
/__w/modular/modular/Kernels/mojo/builtin/_startup.mojo:70:1: error: no viable expansions found
/__w/modular/modular/Kernels/mojo/builtin/_startup.mojo:70:1: note: call expansion failed - no concrete specializations
/__w/modular/modular/Kernels/mojo/builtin/_startup.mojo:11:1: note: no viable expansions found
/__w/modular/modular/Kernels/mojo/builtin/_startup.mojo:23:14: note: call expansion failed - no concrete specializations
array3.mojo:31:1: note: no viable expansions found
fn main():
^
array3.mojo:32:4: note: failed to evaluate 'apply'
alias a: U8 = 4
^
array3.mojo:5:4: note: failed to interpret function @$array3::U8::__init__($array3::U8=&,$builtin::$int::Int)_concrete
fn __init__(inout self, value: Int):
^
array3.mojo:13:67: note: failed to fold operation index.castu(4 : index)
self.value = __mlir_op.`index.castu`[_type: __mlir_type.ui8](value_as_index)
^
mojo: error: failed to run the pass manager
15 Replies
JIMC
JIMCOP•15mo ago
I think this error happens because 1. There is certain 'translation' between MLIR operations and Mojo syntax with 'generic' type parameters [...] and function calls (...) so it is not clear how the _type type parameter appears, probably is to avoid collision with Python's input type function. 2. In the MLIR's docs, it says the type of $value can be an index or an integer, yet it doesn't say where can one get an attr-dict of type integer.
JIMC
JIMCOP•15mo ago
array3.mojo:13:64: error: 'index.castu' op MLIR verification error: 'index.castu' op operand #0 must be integer or index, but got '!kgen.declref<@"$builtin"::@"$int"::@Int>'
value: __mlir_op.`index.castu`[_type: __mlir_type.ui8](value)
array3.mojo:13:64: error: 'index.castu' op MLIR verification error: 'index.castu' op operand #0 must be integer or index, but got '!kgen.declref<@"$builtin"::@"$int"::@Int>'
value: __mlir_op.`index.castu`[_type: __mlir_type.ui8](value)
__mlir_type.`integer`
__mlir_type.`integer`
Doesn't work either any hlep?
Jack Clayton
Jack Clayton•15mo ago
@JIMC don't ping modular staff please There are a lot of questions, and we'll get to this when we can, but pinging the entire staff is not going to accelerate that.
jmky
jmky•15mo ago
Oh, I just need to know how do I get the integer type in the index dialect within Mojo. I can't seem to convert index to ui8 within Mojo.
Jack Clayton
Jack Clayton•15mo ago
Hi @JIMC that code runs for me without an error, can you please provide the full code that's causing the problem, if you don't have enough space here can you make a gist?
jmky
jmky•15mo ago
Thank you for your reply, I am actually in the office now and has no access to the full codes. Would you mind, if it is convenient for you, to share your codes here, which had worked for you? So I can study it on the go, before I regain access to my codes tonight. 😄
ModularBot
ModularBot•15mo ago
Congrats @jmky, you just advanced to level 2!
Jack Clayton
Jack Clayton•15mo ago
Exactly what you posted in that first code block compiled for me in mojo SDK version 0.2.1
JIMC
JIMCOP•15mo ago
Hi, good day to all. Snippet:
@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)
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())
alias b: UInt8 = 257
print(b.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)
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())
alias b: UInt8 = 257
print(b.into_int())
Error:
root@DESKTOP-N84UN90:~/mojo_projs/marray# mojo array3.mojo
/__w/modular/modular/Kernels/mojo/builtin/_startup.mojo:70:1: error: no viable expansions found
/__w/modular/modular/Kernels/mojo/builtin/_startup.mojo:70:1: note: call expansion failed - no concrete specializations
/__w/modular/modular/Kernels/mojo/builtin/_startup.mojo:11:1: note: no viable expansions found
/__w/modular/modular/Kernels/mojo/builtin/_startup.mojo:23:14: note: call expansion failed - no concrete specializations
array3.mojo:19:1: note: no viable expansions found
fn main():
^
array3.mojo:20:4: note: failed to evaluate 'apply'
alias a: UInt8 = 4
^
array3.mojo:5:4: note: failed to interpret function @$array3::UInt8::__init__($builtin::$int::Int)_concrete
fn __init__(value: Int) -> Self:
^
array3.mojo:13:64: note: failed to fold operation index.castu(4 : index)
value: __mlir_op.`index.castu`[_type: __mlir_type.ui8](value_as_index)
^
mojo: error: failed to run the pass manager
root@DESKTOP-N84UN90:~/mojo_projs/marray# mojo array3.mojo
/__w/modular/modular/Kernels/mojo/builtin/_startup.mojo:70:1: error: no viable expansions found
/__w/modular/modular/Kernels/mojo/builtin/_startup.mojo:70:1: note: call expansion failed - no concrete specializations
/__w/modular/modular/Kernels/mojo/builtin/_startup.mojo:11:1: note: no viable expansions found
/__w/modular/modular/Kernels/mojo/builtin/_startup.mojo:23:14: note: call expansion failed - no concrete specializations
array3.mojo:19:1: note: no viable expansions found
fn main():
^
array3.mojo:20:4: note: failed to evaluate 'apply'
alias a: UInt8 = 4
^
array3.mojo:5:4: note: failed to interpret function @$array3::UInt8::__init__($builtin::$int::Int)_concrete
fn __init__(value: Int) -> Self:
^
array3.mojo:13:64: note: failed to fold operation index.castu(4 : index)
value: __mlir_op.`index.castu`[_type: __mlir_type.ui8](value_as_index)
^
mojo: error: failed to run the pass manager
Questions 1. Seems like Mojo is unable to 'cast' a value at compile time to fit into a self-defined UInt8 struct. 2. How to make it work?
ModularBot
ModularBot•15mo ago
Congrats @JIMC, you just advanced to level 3!
JIMC
JIMCOP•15mo ago
array3.mojo:13:64: note: failed to fold operation index.castu(4 : index) value: mlir_op.index.castu[_type: mlir_type.ui8](value_as_index) i dont unds why this fails
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())
Chris Lattner
Chris Lattner•15mo ago
That looks like a compiler bug! Could you file an issue for that plz?
JIMC
JIMCOP•15mo ago
@Chris Lattner Thank you for your reply, I have created an issue here: https://github.com/modularml/mojo/issues/776
GitHub
[BUG]: Unable to convert a MLIR type to another MLIR type at compil...
Bug description I am attempting to create my own UInt8 following the tutorial on Mojo's documentation on the creation of a new boolean. Below is my implementation: @register_passable('trivi...
Jack Clayton
Jack Clayton•15mo ago
Thanks @JIMC much appreciated 🙂
Want results from more Discord servers?
Add your server