[flang-commits] [PATCH] D157330: [flang] Don't emit debugging output to module file

Peter Klausler via Phabricator via flang-commits flang-commits at lists.llvm.org
Mon Aug 7 14:25:08 PDT 2023


klausler created this revision.
klausler added a reviewer: PeteSteinfeld.
klausler added a project: Flang.
Herald added a subscriber: jdoerfert.
Herald added a reviewer: sscalpone.
Herald added a project: All.
klausler requested review of this revision.

When a constant array value has a non-default lower bound, the current
expression formatting code uses a non-Fortran syntax to dump the lower
bounds. (There's no way in Fortran to explicitly specify such a constant value,
but they can be created through the use of named constants.)  But we  
don't want this lower bounds syntax from expression dumping to show up
in module files, since it can't be parsed back in.  So disable that
part of expression formatting by default.


https://reviews.llvm.org/D157330

Files:
  flang/lib/Evaluate/formatting.cpp
  flang/test/Semantics/modfile56.f90


Index: flang/test/Semantics/modfile56.f90
===================================================================
--- /dev/null
+++ flang/test/Semantics/modfile56.f90
@@ -0,0 +1,10 @@
+! RUN: %python %S/test_modfile.py %s %flang_fc1
+! Named constant array with non-default lower bound
+module m
+  real, parameter :: x(0:0) = [0.]
+end
+
+!Expect: m.mod
+!module m
+!real(4),parameter::x(0_8:0_8)=[REAL(4)::0._4]
+!end
Index: flang/lib/Evaluate/formatting.cpp
===================================================================
--- flang/lib/Evaluate/formatting.cpp
+++ flang/lib/Evaluate/formatting.cpp
@@ -19,6 +19,14 @@
 
 namespace Fortran::evaluate {
 
+// Constant arrays can have non-default lower bounds, but this can't be
+// expressed in Fortran syntax directly, only implied through the use of
+// named constant (PARAMETER) definitions.  For debugging, setting this flag
+// enables a non-standard %LBOUND=[...] argument to the RESHAPE intrinsic
+// calls used to dumy constants.  It's off by default so that this syntax
+// doesn't show up in module files.
+static const bool printLbounds{false};
+
 static void ShapeAsFortran(llvm::raw_ostream &o,
     const ConstantSubscripts &shape, const ConstantSubscripts &lbounds,
     bool hasNonDefaultLowerBound) {
@@ -46,7 +54,7 @@
 template <typename RESULT, typename VALUE>
 llvm::raw_ostream &ConstantBase<RESULT, VALUE>::AsFortran(
     llvm::raw_ostream &o) const {
-  bool hasNonDefaultLowerBound{HasNonDefaultLowerBound()};
+  bool hasNonDefaultLowerBound{printLbounds && HasNonDefaultLowerBound()};
   if (Rank() > 1 || hasNonDefaultLowerBound) {
     o << "reshape(";
   }
@@ -90,7 +98,7 @@
 template <int KIND>
 llvm::raw_ostream &Constant<Type<TypeCategory::Character, KIND>>::AsFortran(
     llvm::raw_ostream &o) const {
-  bool hasNonDefaultLowerBound{HasNonDefaultLowerBound()};
+  bool hasNonDefaultLowerBound{printLbounds && HasNonDefaultLowerBound()};
   if (Rank() > 1 || hasNonDefaultLowerBound) {
     o << "reshape(";
   }


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D157330.547945.patch
Type: text/x-patch
Size: 2000 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/flang-commits/attachments/20230807/4b9f01db/attachment-0001.bin>


More information about the flang-commits mailing list