It isn’t clear that we intended to support manipulating i0 in these dialects, but most of the code (folders, canonicalizers, etc.) isn’t safe and leads to crashes (lot of fuzzer-reported bugs).
But I wanted to check if this would break someone?
We can also easily revert if it is found later to have broken someone, and we decide we want to support it.
It’s okay for end users (like arith) to reject i0 if no use case can be imagined, as long as library functions continue to support it as it’s very useful elsewhere.
But also I can’t imagine why arith verifiers could not support i0. I know when implementing i0 support in other places I don’t remember a situation where I could not write a general formula for whatever I was checking that also worked for the 0 case. I guess I can look at the code to see what is blocking.
Actually come to think of it this may be annoying for CIRCT to arith/vector conversions. @fabianschuiki is there such thing in Arcilator?
I believe that most MLIR dialects do not anticipate the occurrence of i0 during their development and maintenance, nor do they invest extra effort to account for the corner cases caused by i0. Therefore, it is perfectly reasonable to exclude the occurrence of i0 in these dialects.
I think we should remove i0 completely. In fact, it is contrary to the spec, and I’m surprised it existed. The IntegerType grammar says i0, ui0, and si0 can’t exist: see Builtin Dialect - MLIR
I guess it’s one way to spell the type with no inhabitants? Or is it the zero-sized type with one inhabitant?
It’d be the single SSA Value that can’t take any values (given 2^0 = 1) - effectively, saying it shouldn’t exist. The void return type of C/C++ functions would be the closest match - but in that case, we’d have 0 return values/results as an equivalent.
For me an i0 would most likely have represented a dependency. So its “value” is in the dependency (ordering) as a control dependency/token as used in many other cases. I’d consider it to have one inhabitant, it has consumers of the value. Different ops could interpret that singular value differently (same as any other signless integer).
Is it all folders? Indeed, just doing a round of fuzzing could address this too (easy to generate a test with all i0 variants). I’d not have expected canonicalizers to hit this often. They do have little value in arith and vector. It feels unfortunate to have verifiers run constantly for these, where they have a reasonable interpretation (e.g., its an algebra with 1 item where all actions are back to it except for invalid IR that verifiers have to handle).
Indeed, in the conversion to LLVM, one would have to guard the conversion, while in the CIRCT case one may not be able to represent it anymore in arith. Both of these would though result in failures, but I think some (most?) of the cases comb is raised to arith would be for codegen and so work would be needed anyway.
It’d be the single SSA Value that can’t take any values (given 2^0 = 1) - effectively, saying it shouldn’t exist. The void return type of C/C++ functions would be the closest match - but in that case, we’d have 0 return values/results as an equivalent.
i0 seems just a special unit type (note that MLIR supports none as a unit type, though it’s more like a void instead of a classic unit type). Compared to unit type, i0 differs from:
i0isa integer type, unit type is not;
i0 has the only instance 0 (unit type may require a new operation to construct its instance?)
I’m not familiar with CIRCT but maybe these features make them prefer i0 than some types like Union[unit, integer]?
Not to speak for them as I wasn’t involved, but it is just nice and uniform, everything is a sign less int (bag of bits) of various amounts. You have an input module, you do range reduction, you figure out new widths, it’s all just same simple type and just changing width param.
I’m not sure about the actual arith / vector uses within CIRCT, but in general disallowing i0 is a stance CIRCT has taken for the core combinational logic dialect: `comb` Dialect Rationale - CIRCT
From CIRCT’s side, I need to page back in exactly what corners do support i0, because it does have its uses and I think we may have allowed it in places. At least on the path from Chisel, we may have accepted it. It comes up when you’re creating a signal that is log2(some array’s length), and the array happens to be of length 1. Not supporting i0 there means special cases, but supporting i0 of course brings its own bag of special caess.
From a CIRCT perspective, I don’t see any issues with rejecting i0 support in arith, vector, or other core MLIR dialects. However, i0 itself is very useful in CIRCT and the hardware space in general for handling parameterized data widths, acting as an unit in concat, or representing the index type of a single-element array.
Also, I don’t believe it will be a problem even when rejecting it in common MLIR ODS constraints like AnyInteger or AnySignlessInteger. CIRCT mostly uses its type constraints for our core dialects anyway, and while there are a few use cases relying on the standard MLIR constraints, I think we can easily migrate them to our own.
Just to shed some light on the CIRCT usecase outside of arith/vector: It’s very handy in hardware to have integers and the set of allowed values follow
i2: {0,1,2,3}
i1: {0,1}
i0: {0}
mainly for the reason @mikeurbach pointed out: hardware often ends up consisting of things that look like an array of N elements, plus a corresponding integer to address the elements:
hw.module @lookup.N4(in %addr: i2) -> T {
// ...
%0 = hw.array_create %e0, %e1, %e2, %e3 : !hw.array<4 x T>
%1 = hw.array_get %0[%addr] : !hw.array<4 x T> // addr width clog2(4) = 2
hw.output %1 : T
}
hw.module @lookup.N2(in %addr: i1) -> T {
// ...
%0 = hw.array_create %e0, %e1 : !hw.array<2 x T>
%1 = hw.array_get %0[%addr] : !hw.array<2 x T> // addr width clog2(2) = 1
hw.output %1 : T
}
hw.module @lookup.N1(in %addr: i0) -> T {
// ...
%0 = hw.array_create %e0 : !hw.array<1 x T>
%1 = hw.array_get %0[%addr] : !hw.array<1 x T> // addr width clog2(1) = 0
hw.output %1 : T
}
As @mehdi_amini pointed out, if the IntegerType continues to support this, but arith makes restrictions, that sounds totally reasonable for CIRCT. We only lower into arith to build simulation binaries, where we can work around a lack of i0 support in core dialects.
To me this seems more that the spec is wrong and should be updated, as it is implemented and works well in practice. i0 is very much used, at least in CIRCT.
As you wrote, 2^0 = 1, so it’s the SSA value that can take a single value (the value 0), not no value. The arithmetic structure of (unsigned) integer types is the ring of integers modulo 2^bitwidth (Z/mZ with m = 2^bitwidth). Z/1Z and its operations are well defined, I go into more details in this CIRCT RFC.
This is not the case and the confusion is my bad, I just now discover that I had completely forgotten to address the final comments of my PR (mentioned above) that removed this piece of documentation. Comb supports i0 perfectly right now as far as I remember.
In any case this is all about removing support in arith and vector. Considering LLVM does not support i0 and it seems herculean to want to change this, we need to draw a support line somewhere. It’s a bit unfortunate that the “arith” dialect, which is to support integer arithmetic, does not support all integer arithmetic, but if it is the only practical solution it’s probably fine. I guess this comes from canonicalizers being written in a way that assumes bitwidth != 0, but would it really be hard to reformulate them so they don’t make that hypothesis?
It is about canonicalizer, folders, and also lowerings from arith/vector to llvm/emitc/…(you own target) that wouldn’t support i0. All these need to be made aware of i0, that means bailing out in canonicalization/folding when appropriate, and erroring out properly in lowering/translation to target that don’t support i0 (LLVM or other).
Nothing impossible, it’s just about tradeoff and so about identifying what use case supporting this in these dialects would enable.
I’ve looked into this test case (with very limited knowledge of CIRCT), and it’s not clear to me how its use of the arith/vector dialect caused the lowering process to fail. Could you elaborate on this?