Skip to content

Commit

Permalink
EmitC: Allow casts between opaque and float types (#428)
Browse files Browse the repository at this point in the history
* EmitC: Allow casts between opaque and float types

* Use EmitC cast compatibility check

* Allow opaque types in casts (also for array types)
  • Loading branch information
TinaAMD authored Dec 17, 2024
1 parent 14e4586 commit 992dad3
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 2 deletions.
4 changes: 2 additions & 2 deletions mlir/lib/Conversion/ArithToEmitC/ArithToEmitC.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -757,7 +757,7 @@ class TruncFConversion : public OpConversionPattern<arith::TruncFOp> {
return rewriter.notifyMatchFailure(castOp,
"unsupported cast destination type");

if (!castOp.areCastCompatible(operandType, dstType))
if (!emitc::CastOp::areCastCompatible(operandType, dstType))
return rewriter.notifyMatchFailure(castOp, "cast-incompatible types");

rewriter.replaceOpWithNewOp<emitc::CastOp>(castOp, dstType,
Expand Down Expand Up @@ -787,7 +787,7 @@ class ExtFConversion : public OpConversionPattern<arith::ExtFOp> {
return rewriter.notifyMatchFailure(castOp,
"unsupported cast destination type");

if (!castOp.areCastCompatible(operandType, dstType))
if (!emitc::CastOp::areCastCompatible(operandType, dstType))
return rewriter.notifyMatchFailure(castOp, "cast-incompatible types");

rewriter.replaceOpWithNewOp<emitc::CastOp>(castOp, dstType,
Expand Down
4 changes: 4 additions & 0 deletions mlir/lib/Dialect/EmitC/IR/EmitC.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,10 @@ LogicalResult emitc::AssignOp::verify() {
bool CastOp::areCastCompatible(TypeRange inputs, TypeRange outputs) {
Type input = inputs.front(), output = outputs.front();

// Opaque types are always allowed
if (isa<emitc::OpaqueType>(input) || isa<emitc::OpaqueType>(output))
return true;

// Cast to array is only possible from an array
if (isa<emitc::ArrayType>(input) != isa<emitc::ArrayType>(output))
return false;
Expand Down
4 changes: 4 additions & 0 deletions mlir/test/Dialect/EmitC/ops.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,15 @@ emitc.func private @extern(i32) attributes {specifiers = ["extern"]}

func.func @cast(%arg0: i32) {
%1 = emitc.cast %arg0: i32 to f32
%2 = emitc.cast %1: f32 to !emitc.opaque<"some type">
%3 = emitc.cast %2: !emitc.opaque<"some type"> to !emitc.size_t
return
}

func.func @cast_array(%arg : !emitc.array<4xf32>) {
%1 = emitc.cast %arg: !emitc.array<4xf32> to !emitc.array<4xf32> ref
%2 = emitc.cast %arg: !emitc.array<4xf32> to !emitc.opaque<"some type">
%3 = emitc.cast %2: !emitc.opaque<"some type"> to !emitc.array<4xf32> ref
return
}

Expand Down

0 comments on commit 992dad3

Please sign in to comment.