This adds a cast operation that allows to perform an explicit type
conversion. The cast op is emitted as a C-style cast. It can be applied
to integer, float, index and EmitC types.
Details
Diff Detail
- Repository
- rG LLVM Github Monorepo
Event Timeline
mlir/include/mlir/Dialect/EmitC/IR/EmitC.td | ||
---|---|---|
99 | Why the restriction? E.g., it would seem that one can't know the safety or validity of conversions between EmitC types here, so this restriction doesn't seem to add anything additional here. | |
mlir/lib/Dialect/EmitC/IR/EmitC.cpp | ||
70 | Is this possible with a valid op? Seems fr the op definition it can only have one operand and one result. |
mlir/include/mlir/Dialect/EmitC/IR/EmitC.td | ||
---|---|---|
99 | It depends. The restrictions actually forbids to cast from/to tensor types (which the emitter supports) and vector types (which the emitter doesn't supports at all). Since tensors can be only expressed as a more complex C++ datatype, allowing C-style casts seemed not useful to me. As additional types might be added in the future to MLIR, I explicitly listed the types that are supported instead of listing what isn't supported. However, it is correct that one can write unsafe/invalid conversions especially when using !emitc.opaque types. This would be up to the one implementing the conversions. | |
mlir/lib/Dialect/EmitC/IR/EmitC.cpp | ||
70 | This is something done similar (ArithmeticOps.cpp#L824), or rather identical (MemRefOps.cpp#L566-L567 or Shape.cpp#L1688-L1689), through the code base, so I assume this should be fine. |
mlir/lib/Dialect/EmitC/IR/EmitC.cpp | ||
---|---|---|
70 | The arith one makes sense to me as it is a standalone helper function, the other two are a bit weird. This is probably leftovers from when there wasn't a verification order enforced. With it enforced now these should never get triggered. | |
75 | Just return book value directly. Instead of "if (X) return true; return false;" just "return X;". | |
mlir/lib/Target/Cpp/TranslateToCpp.cpp | ||
388 | Why not just use castOp-> below where Operation is needed? (Not sure if it is actually needed looking at uses) | |
mlir/test/Target/Cpp/cast.mlir | ||
8 | Could you add an invalid test too? |
Thanks for the review @jpienaar! I will address your comments before landing.
mlir/test/Target/Cpp/cast.mlir | ||
---|---|---|
8 | There is func @cast_tensor(%arg : tensor<f32>) { // expected-error @+1 {{'emitc.cast' op operand type 'tensor<f32>' and result type 'tensor<f32>' are cast incompatible}} %1 = emitc.cast %arg: tensor<f32> to tensor<f32> return } in invalid_ops.mlir which fails since tensors aren't supported. Or do you have something different in mind? |
Why the restriction? E.g., it would seem that one can't know the safety or validity of conversions between EmitC types here, so this restriction doesn't seem to add anything additional here.