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

Diana Picus via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Wed Nov 17 05:07:36 PST 2021


rovka created this revision.
rovka added reviewers: kiranchandramohan, jeanPerier, clementval, schweitz.
rovka added a project: Flang.
Herald added subscribers: Chia-hungDuan, mehdi_amini, rriddle, jdoerfert, kristof.beyls.
rovka requested review of this revision.
Herald added subscribers: llvm-commits, stephenneuendorffer.
Herald added a project: LLVM.

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 at arm.com> for noticing
that we were missing tests for constc in fir-ops.fir.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D114081

Files:
  flang/lib/Optimizer/Dialect/FIRAttr.cpp
  flang/lib/Optimizer/Dialect/FIROps.cpp
  flang/test/Fir/fir-ops.fir


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.array<?xf32>>) -> !fir.ref<f32> {
   %c4 = arith.constant 4 : index
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<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());
 }
 
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});
 }


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D114081.387905.patch
Type: text/x-patch
Size: 3390 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20211117/5d79a39b/attachment.bin>


More information about the llvm-commits mailing list