Skip to content

Commit 992dad3

Browse files
authored
EmitC: Allow casts between opaque and float types (#428)
* EmitC: Allow casts between opaque and float types * Use EmitC cast compatibility check * Allow opaque types in casts (also for array types)
1 parent 14e4586 commit 992dad3

File tree

3 files changed

+10
-2
lines changed

3 files changed

+10
-2
lines changed

mlir/lib/Conversion/ArithToEmitC/ArithToEmitC.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -757,7 +757,7 @@ class TruncFConversion : public OpConversionPattern<arith::TruncFOp> {
757757
return rewriter.notifyMatchFailure(castOp,
758758
"unsupported cast destination type");
759759

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

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

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

793793
rewriter.replaceOpWithNewOp<emitc::CastOp>(castOp, dstType,

mlir/lib/Dialect/EmitC/IR/EmitC.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,10 @@ LogicalResult emitc::AssignOp::verify() {
313313
bool CastOp::areCastCompatible(TypeRange inputs, TypeRange outputs) {
314314
Type input = inputs.front(), output = outputs.front();
315315

316+
// Opaque types are always allowed
317+
if (isa<emitc::OpaqueType>(input) || isa<emitc::OpaqueType>(output))
318+
return true;
319+
316320
// Cast to array is only possible from an array
317321
if (isa<emitc::ArrayType>(input) != isa<emitc::ArrayType>(output))
318322
return false;

mlir/test/Dialect/EmitC/ops.mlir

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,15 @@ emitc.func private @extern(i32) attributes {specifiers = ["extern"]}
3636

3737
func.func @cast(%arg0: i32) {
3838
%1 = emitc.cast %arg0: i32 to f32
39+
%2 = emitc.cast %1: f32 to !emitc.opaque<"some type">
40+
%3 = emitc.cast %2: !emitc.opaque<"some type"> to !emitc.size_t
3941
return
4042
}
4143

4244
func.func @cast_array(%arg : !emitc.array<4xf32>) {
4345
%1 = emitc.cast %arg: !emitc.array<4xf32> to !emitc.array<4xf32> ref
46+
%2 = emitc.cast %arg: !emitc.array<4xf32> to !emitc.opaque<"some type">
47+
%3 = emitc.cast %2: !emitc.opaque<"some type"> to !emitc.array<4xf32> ref
4448
return
4549
}
4650

0 commit comments

Comments
 (0)