Skip to content

Commit

Permalink
[flang] don't allow conversions between logical and floating point
Browse files Browse the repository at this point in the history
Codegen only supports conversions between logicals and integers. The
verifier should reflect this.

Differential Revision: https://reviews.llvm.org/D152935
  • Loading branch information
tblah committed Jun 19, 2023
1 parent 569716f commit f523b9a
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 4 deletions.
1 change: 1 addition & 0 deletions flang/include/flang/Optimizer/Dialect/FIROps.td
Original file line number Diff line number Diff line change
Expand Up @@ -2606,6 +2606,7 @@ def fir_ConvertOp : fir_OneResultOp<"convert", [NoMemoryEffect]> {
let hasVerifier = 1;

let extraClassDeclaration = [{
static bool isInteger(mlir::Type ty);
static bool isIntegerCompatible(mlir::Type ty);
static bool isFloatCompatible(mlir::Type ty);
static bool isPointerCompatible(mlir::Type ty);
Expand Down
11 changes: 7 additions & 4 deletions flang/lib/Optimizer/Dialect/FIROps.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -928,9 +928,12 @@ mlir::OpFoldResult fir::ConvertOp::fold(FoldAdaptor adaptor) {
return {};
}

bool fir::ConvertOp::isInteger(mlir::Type ty) {
return ty.isa<mlir::IntegerType, mlir::IndexType, fir::IntegerType>();
}

bool fir::ConvertOp::isIntegerCompatible(mlir::Type ty) {
return ty.isa<mlir::IntegerType, mlir::IndexType, fir::IntegerType,
fir::LogicalType>();
return isInteger(ty) || mlir::isa<fir::LogicalType>(ty);
}

bool fir::ConvertOp::isFloatCompatible(mlir::Type ty) {
Expand Down Expand Up @@ -1001,8 +1004,8 @@ bool fir::ConvertOp::canBeConverted(mlir::Type inType, mlir::Type outType) {
return true;
return (isPointerCompatible(inType) && isPointerCompatible(outType)) ||
(isIntegerCompatible(inType) && isIntegerCompatible(outType)) ||
(isIntegerCompatible(inType) && isFloatCompatible(outType)) ||
(isFloatCompatible(inType) && isIntegerCompatible(outType)) ||
(isInteger(inType) && isFloatCompatible(outType)) ||
(isFloatCompatible(inType) && isInteger(outType)) ||
(isFloatCompatible(inType) && isFloatCompatible(outType)) ||
(isIntegerCompatible(inType) && isPointerCompatible(outType)) ||
(isPointerCompatible(inType) && isIntegerCompatible(outType)) ||
Expand Down
16 changes: 16 additions & 0 deletions flang/test/Fir/invalid.fir
Original file line number Diff line number Diff line change
Expand Up @@ -946,3 +946,19 @@ func.func @invalid_selector(%arg : !fir.box<!fir.ref<i32>>) -> i32 {
%zero = arith.constant 0 : i32
return %zero : i32
}

// -----

func.func @logical_to_fp(%arg0: !fir.logical<4>) -> f32 {
// expected-error@+1{{'fir.convert' op invalid type conversion}}
%0 = fir.convert %arg0 : (!fir.logical<4>) -> f32
return %0 : f32
}

// -----

func.func @fp_to_logical(%arg0: f32) -> !fir.logical<4> {
// expected-error@+1{{'fir.convert' op invalid type conversion}}
%0 = fir.convert %arg0 : (f32) -> !fir.logical<4>
return %0 : !fir.logical<4>
}

0 comments on commit f523b9a

Please sign in to comment.