Index: flang/lib/Optimizer/Dialect/FIRAttr.cpp =================================================================== --- flang/lib/Optimizer/Dialect/FIRAttr.cpp +++ flang/lib/Optimizer/Dialect/FIRAttr.cpp @@ -163,8 +163,10 @@ 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}); } Index: flang/lib/Optimizer/Dialect/FIROps.cpp =================================================================== --- flang/lib/Optimizer/Dialect/FIROps.cpp +++ flang/lib/Optimizer/Dialect/FIROps.cpp @@ -760,19 +760,9 @@ } static void print(mlir::OpAsmPrinter &p, fir::ConstcOp &op) { - p << " (0x"; - auto f1 = op.getOperation() - ->getAttr(fir::ConstcOp::realAttrName()) - .cast(); - auto i1 = f1.getValue().bitcastToAPInt(); - p.getStream().write_hex(i1.getZExtValue()); - p << ", 0x"; - auto f2 = op.getOperation() - ->getAttr(fir::ConstcOp::imagAttrName()) - .cast(); - 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()); } Index: flang/test/Fir/fir-ops.fir =================================================================== --- flang/test/Fir/fir-ops.fir +++ flang/test/Fir/fir-ops.fir @@ -647,6 +647,23 @@ 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.ref { %c4 = arith.constant 4 : index