This is an archive of the discontinued LLVM Phabricator instance.

[mlir][emitc] Add a cast op
ClosedPublic

Authored by marbre on Apr 11 2022, 7:30 AM.

Details

Summary

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.

Diff Detail

Event Timeline

marbre created this revision.Apr 11 2022, 7:30 AM
Herald added a project: Restricted Project. · View Herald TranscriptApr 11 2022, 7:30 AM
marbre requested review of this revision.Apr 11 2022, 7:30 AM
jpienaar added inline comments.Apr 17 2022, 3:04 PM
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.

marbre added inline comments.Apr 19 2022, 6:38 AM
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.

jpienaar accepted this revision.Apr 25 2022, 7:03 AM
jpienaar added inline comments.
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?

This revision is now accepted and ready to land.Apr 25 2022, 7:03 AM

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?

This revision was automatically updated to reflect the committed changes.