[flang] Fix printing of constc and parsing of #fir.real

Printing and parsing of constc didn't agree with each other. This patch
treats the parsing of constc as the final word and fixes the printing
accordingly.

More concretely, this patch prints the RealAttrs that make up the
ConstcOp directly instead of casting to mlir::FloatAttr (which blows
up). It also fixes parseFirRealAttr to invoke APFloat's method for
getting the size of a floating point type instead of computing it as
8 * kind (which blows up for BFloat, with kind == 3 and size == 16).

Kudos to Kiran Chandramohan <kiran.chandramohan@arm.com> for noticing
that we were missing tests for constc in fir-ops.fir.

Differential Revision: https://reviews.llvm.org/D114081
This commit is contained in:
Diana Picus 2021-11-17 10:29:42 +00:00
parent 85e03cb7eb
commit ca3795541f
3 changed files with 24 additions and 15 deletions

View File

@ -163,8 +163,10 @@ static mlir::Attribute parseFirRealAttr(FIROpsDialect *dialect,
parser.emitError(parser.getNameLoc(), "expected real constant '>'");
return {};
}
auto bits = llvm::APInt(kind * 8, hex.drop_front(), 16);
value = llvm::APFloat(kindMap.getFloatSemantics(kind), bits);
const llvm::fltSemantics &sem = kindMap.getFloatSemantics(kind);
unsigned int numBits = llvm::APFloat::semanticsSizeInBits(sem);
auto bits = llvm::APInt(numBits, hex.drop_front(), 16);
value = llvm::APFloat(sem, bits);
}
return RealAttr::get(dialect->getContext(), {kind, value});
}

View File

@ -760,19 +760,9 @@ static mlir::ParseResult parseConstcOp(mlir::OpAsmParser &parser,
}
static void print(mlir::OpAsmPrinter &p, fir::ConstcOp &op) {
p << " (0x";
auto f1 = op.getOperation()
->getAttr(fir::ConstcOp::realAttrName())
.cast<mlir::FloatAttr>();
auto i1 = f1.getValue().bitcastToAPInt();
p.getStream().write_hex(i1.getZExtValue());
p << ", 0x";
auto f2 = op.getOperation()
->getAttr(fir::ConstcOp::imagAttrName())
.cast<mlir::FloatAttr>();
auto i2 = f2.getValue().bitcastToAPInt();
p.getStream().write_hex(i2.getZExtValue());
p << ") : ";
p << '(';
p << op.getOperation()->getAttr(fir::ConstcOp::realAttrName()) << ", ";
p << op.getOperation()->getAttr(fir::ConstcOp::imagAttrName()) << ") : ";
p.printType(op.getType());
}

View File

@ -647,6 +647,23 @@ func @test_misc_ops(%arr1 : !fir.ref<!fir.array<?x?xf32>>, %m : index, %n : inde
return
}
// CHECK-LABEL: @test_const_complex
func @test_const_complex() {
// CHECK-DAG: {{%.*}} = fir.constc(#fir.real<2, i x3000>, #fir.real<2, i x4C40>) : !fir.complex<2>
// CHECK-DAG: {{%.*}} = fir.constc(#fir.real<3, i x3E80>, #fir.real<3, i x4202>) : !fir.complex<3>
// CHECK-DAG: {{%.*}} = fir.constc(#fir.real<4, i x3E800000>, #fir.real<4, i x42028000>) : !fir.complex<4>
// CHECK-DAG: {{%.*}} = fir.constc(#fir.real<8, i x3FD0000000000000>, #fir.real<8, i x4040500000000000>) : !fir.complex<8>
// CHECK-DAG: {{%.*}} = fir.constc(#fir.real<10, i x3FFD8000000000000000>, #fir.real<10, i x40048280000000000000>) : !fir.complex<10>
// CHECK-DAG: {{%.*}} = fir.constc(#fir.real<16, i x3FFD0000000000000000000000000000>, #fir.real<16, i x40040500000000000000000000000000>) : !fir.complex<16>
%c2 = fir.constc (#fir.real<2, 0.125>, #fir.real<2, 17.0>) : !fir.complex<2>
%c3 = fir.constc (#fir.real<3, 0.25>, #fir.real<3, 32.625>) : !fir.complex<3>
%c4 = fir.constc (#fir.real<4, 0.25>, #fir.real<4, 32.625>) : !fir.complex<4>
%c8 = fir.constc (#fir.real<8, 0.25>, #fir.real<8, 32.625>) : !fir.complex<8>
%c10 = fir.constc (#fir.real<10, 0.25>, #fir.real<10, 32.625>) : !fir.complex<10>
%c16 = fir.constc (#fir.real<16, 0.25>, #fir.real<16, 32.625>) : !fir.complex<16>
return
}
// CHECK-LABEL: @test_shift
func @test_shift(%arg0: !fir.box<!fir.array<?xf32>>) -> !fir.ref<f32> {
%c4 = arith.constant 4 : index