Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[flang] Do not write implicit SAVE attribute into the mod file. #67215

Merged
merged 1 commit into from
Sep 25, 2023

Conversation

vzakhari
Copy link
Contributor

If it happens that a symbol has an implicit SAVE attribute,
we have to omit it in the mod file writer. Otherwise it may
violate F202X C862:
The SAVE attribute shall not be specified for... an object that is
in a common block.

If it happens that a symbol has an implicit SAVE attribute,
we have to omit it in the mod file writer. Otherwise it may
violate F202X C862:
The SAVE attribute shall not be specified for... an object that is
in a common block.
@vzakhari
Copy link
Contributor Author

Follow-up fix for #67078

@llvmbot llvmbot added flang Flang issues not falling into any other category flang:semantics labels Sep 23, 2023
@llvmbot
Copy link
Collaborator

llvmbot commented Sep 23, 2023

@llvm/pr-subscribers-flang-semantics

Changes

If it happens that a symbol has an implicit SAVE attribute,
we have to omit it in the mod file writer. Otherwise it may
violate F202X C862:
The SAVE attribute shall not be specified for... an object that is
in a common block.


Full diff: https://github.com/llvm/llvm-project/pull/67215.diff

2 Files Affected:

  • (modified) flang/lib/Semantics/mod-file.cpp (+10-2)
  • (added) flang/test/Semantics/modfile58.f90 (+18)
diff --git a/flang/lib/Semantics/mod-file.cpp b/flang/lib/Semantics/mod-file.cpp
index 15d6f62f706c922..cee267a894ffde1 100644
--- a/flang/lib/Semantics/mod-file.cpp
+++ b/flang/lib/Semantics/mod-file.cpp
@@ -244,6 +244,14 @@ bool ModFileWriter::PutComponents(const Symbol &typeSymbol) {
   }
 }
 
+// Return the symbol's attributes that should be written
+// into the mod file.
+static Attrs getSymbolAttrsToWrite(const Symbol &symbol) {
+  // Is SAVE attribute is implicit, it should be omitted
+  // to not violate F202x C862 for a common block member.
+  return symbol.attrs() & ~(symbol.implicitAttrs() & Attrs{Attr::SAVE});
+}
+
 static llvm::raw_ostream &PutGenericName(
     llvm::raw_ostream &os, const Symbol &symbol) {
   if (IsGenericDefinedOp(symbol)) {
@@ -314,7 +322,7 @@ void ModFileWriter::PutSymbol(
             }
             decls_ << '\n';
             if (symbol.attrs().test(Attr::BIND_C)) {
-              PutAttrs(decls_, symbol.attrs(), x.bindName(),
+              PutAttrs(decls_, getSymbolAttrsToWrite(symbol), x.bindName(),
                   x.isExplicitBindName(), ""s);
               decls_ << "::/" << symbol.name() << "/\n";
             }
@@ -723,7 +731,7 @@ void ModFileWriter::PutObjectEntity(
   }
   PutEntity(
       os, symbol, [&]() { PutType(os, DEREF(symbol.GetType())); },
-      symbol.attrs());
+      getSymbolAttrsToWrite(symbol));
   PutShape(os, details.shape(), '(', ')');
   PutShape(os, details.coshape(), '[', ']');
   PutInit(os, symbol, details.init(), details.unanalyzedPDTComponentInit());
diff --git a/flang/test/Semantics/modfile58.f90 b/flang/test/Semantics/modfile58.f90
new file mode 100644
index 000000000000000..4c5fac09c07550c
--- /dev/null
+++ b/flang/test/Semantics/modfile58.f90
@@ -0,0 +1,18 @@
+! RUN: %python %S/test_modfile.py %s %flang_fc1
+
+! Test that the implicit SAVE attribute (set
+! for the equivalenced symbols) is not written
+! into the mod file.
+module implicit_save
+  real dx,dy
+  common /blk/ dx
+  equivalence(dx,dy)
+end module implicit_save
+
+!Expect: implicit_save.mod
+!moduleimplicit_save
+!real(4)::dx
+!real(4)::dy
+!common/blk/dx
+!equivalence(dx,dy)
+!end

static Attrs getSymbolAttrsToWrite(const Symbol &symbol) {
// Is SAVE attribute is implicit, it should be omitted
// to not violate F202x C862 for a common block member.
return symbol.attrs() & ~(symbol.implicitAttrs() & Attrs{Attr::SAVE});
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should apply to all implicit attributes, not just SAVE, yes?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure about this. For example:

module equiv
  private
  real :: p
end module

p gets an implicit PRIVATE attribute resulting in this mod file:

module equiv
real(4),private::p
end

If I filter it out, I will get this:

module equiv
real(4)::p
end

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, good point; ship it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the review!

@vzakhari vzakhari merged commit 1db42fa into llvm:main Sep 25, 2023
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
flang:semantics flang Flang issues not falling into any other category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants