aboutsummaryrefslogtreecommitdiff
path: root/flang/lib/Semantics/mod-file.cpp
AgeCommit message (Collapse)AuthorFilesLines
2025-06-16[flang] Don't generate module file for hermetic USE'd dependency (#144143)Peter Klausler1-21/+23
It's possible for the module file generation code to think that it needs to (re)generate a module file for a dependent module read from a hermetic module file, if it defines contains a procedure imported via renaming due to a name clash. Adjust the logic that determines whether a module file should be written to include a check for having originated in a module file.
2025-06-11[flang] Don't duplicate hermetic module file dependencies (#143605)Peter Klausler1-7/+11
When emitting the modules on which a module depends under the -fhermetic-module-files options, eliminate duplicates by name rather than by symbol addresses. This way, when a dependent module is in the symbol table more than once due to the use of a nested hermetic module, it doesn't get emitted multiple times to the new module file.
2025-06-09[flang][openmp]Add UserReductionDetails and use in DECLARE REDUCTION (#140066)Tom Eccles1-0/+23
This adds another puzzle piece for the support of OpenMP DECLARE REDUCTION functionality. This adds support for operators with derived types, as well as declaring multiple different types with the same name or operator. A new detail class for UserReductionDetials is introduced to hold the list of types supported for a given reduction declaration. Tests for parsing and symbol generation added. Declare reduction is still not supported to lowering, it will generate a "Not yet implemented" fatal error. Fixes #141306 Fixes #97241 Fixes #92832 Fixes #66453 --------- Co-authored-by: Mats Petersson <mats.petersson@arm.com>
2025-05-12[flang] Fix crash with USE of hermetic module file (#138785)Peter Klausler1-1/+2
When one hermetic module file uses another, a later compilation may crash in semantics when it itself is used, since the module file reader sets the "current hermetic module file scope" to null after reading one rather than saving and restoring that pointer.
2025-05-09[flang][openacc] Allow open acc routines from other modules. (#136012)Andre Kuhlenschmidt1-2/+12
OpenACC routines annotations in separate compilation units currently get ignored, which leads to errors in compilation. There are two reason for currently ignoring open acc routine information and this PR is addressing both. - The module file reader doesn't read back in openacc directives from module files. - Simple fix in `flang/lib/Semantics/mod-file.cpp` - The lowering to HLFIR doesn't generate routine directives for symbols imported from other modules that are openacc routines. - This is the majority of this diff, and is address by the changes that start in `flang/lib/Lower/CallInterface.cpp`.
2025-05-09[flang][OpenMP] Pass OpenMP version to getOpenMPDirectiveName (#139131)Krzysztof Parzyszek1-5/+6
The OpenMP version is stored in LangOptions in SemanticsContext. Use the fallback version where SemanticsContext is unavailable (mostly in case of debug dumps). RFC: https://discourse.llvm.org/t/rfc-alternative-spellings-of-openmp-directives/85507 Reland with a fix for build break in f18-parse-demo.
2025-05-09Revert "[flang][OpenMP] Pass OpenMP version to getOpenMPDirectiveName (#139131)"Krzysztof Parzyszek1-6/+5
This reverts commit 41aa67488c3ca33334ec79fb5216145c3644277c. Breaks build: https://lab.llvm.org/buildbot/#/builders/140/builds/22826
2025-05-09[flang][OpenMP] Pass OpenMP version to getOpenMPDirectiveName (#139131)Krzysztof Parzyszek1-5/+6
The OpenMP version is stored in LangOptions in SemanticsContext. Use the fallback version where SemanticsContext is unavailable (mostly in case of debug dumps). RFC: https://discourse.llvm.org/t/rfc-alternative-spellings-of-openmp-directives/85507
2025-03-26[flang] Fix missed case of symbol renaming in module file generation (#132475)Peter Klausler1-1/+1
The map of symbols requiring new local aliases for USE association needs to use the symbols' ultimate resolutions to avoid missing cases that can arise in convoluted codes with lots of confusing renamings. Fixes https://github.com/llvm/llvm-project/issues/132435.
2025-03-10[flang] Fix Cray pointers in module file output (#130315)Peter Klausler1-5/+14
The relationship between a Cray pointee and its pointer is not in the symbol table entry, but instead in a per-scope map. Use this information to ensure that if a pointee/pointer is needed in the module file, so is its pointer/pointee. Fixes https://github.com/llvm/llvm-project/issues/130270.
2025-03-05[flang][Semantics] Ensure deterministic mod file output (#129669)Iñaki Amatria Barral1-3/+3
This PR is a follow-up to #128655. It adds another test to ensure deterministic ordering in `.mod` files and includes related changes to prevent non-deterministic ordering caused by iterating over a set ordered by heap pointers. This issue is particularly noticeable when using Flang as a library and compiling the same files multiple times. The reduced test case is as minimal as possible. We were unable to reproduce the issue with a smaller set of files.
2025-02-27[flang] Silence warnings from hermetic module files (#128763)Peter Klausler1-0/+4
Modules read from module files must have their symbols tagged with the ModFile flag to suppress all warnings messages that might be emitted for their contents. (Actionable warnings will have been emitted when the modules were originally compiled, so we don't want to repeat them later when the modules are USE'd.) The module symbols of the additional modules in hermetic module files were not being tagged with that flag; fix.
2025-02-26[flang][Semantics] Ensure deterministic mod file output (#128655)Iñaki Amatria Barral1-19/+2
This PR adds a test to ensure deterministic ordering in `.mod` files. It also includes related changes to prevent non-deterministic symbol ordering caused by pointers outside the cooked source. This issue is particularly noticeable when using Flang as a library and compiling the same files multiple times.
2025-01-31[flang] Suppress USEs of non-USE'able names in module files (#124980)Peter Klausler1-2/+4
When harvesting and formatting symbols USE'd from other modules, don't emit USE statements to module files for names unless they come from the topmost scope of the module. There was a check to prevent names from derived type scopes from escaping in this way, but it must be made more general to prevent other cases like dummy arguments in interfaces. Fixes https://github.com/llvm/llvm-project/issues/124716.
2025-01-27[flang] Fix crash in module file generation (#123859)Peter Klausler1-2/+2
An assertion in module file generation didn't allow for a case that has arisen in a test; remove it, extend commentary, and add a regression test. Fixes https://github.com/llvm/llvm-project/issues/123534.
2025-01-27[flang] Safer hermetic module file reading (#121002)Peter Klausler1-0/+19
When a hermetic module file is read, use a new scope to hold its dependent modules so that they don't conflict with any modules in the global scope.
2025-01-14[flang] Improve module file error message wording (#122787)Peter Klausler1-8/+9
Instead of "Cannot read ...", distinguish true errors in finding and parsing module files from problems with unexpected hash codes by using "Cannot parse" or "Cannot use" wording as appropriate.
2024-11-06[flang] Prevent errors from being suppressed (#114420)Iñaki Amatria Barral1-11/+5
`ModFileReader::Say()` flags all messages as errors, but Flang was mistakenly suppressing two errors when the `-w` flag was used, as they were incorrectly conditioned to warning suppression. This fix ensures that errors are reported regardless of the `-w` flag. This commit also replaces two uses of `_warn_en_US` with `_err_en_US` to prevent potential confusion in the future.
2024-10-02[flang] Tag warnings with LanguageFeature or UsageWarning (#110304)Peter Klausler1-2/+4
(This is a big patch, but it's nearly an NFC. No test results have changed and all Fortran tests in the LLVM test suites work as expected.) Allow a parser::Message for a warning to be marked with the common::LanguageFeature or common::UsageWarning that controls it. This will allow a later patch to add hooks whereby a driver will be able to decorate warning messages with the names of its options that enable each particular warning, and to add hooks whereby a driver can map those enumerators by name to command-line options that enable/disable the language feature and enable/disable the messages. The default settings in the constructor for LanguageFeatureControl were moved from its header file into its C++ source file. Hooks for a driver to use to map the name of a feature or warning to its enumerator were also added. To simplify the tagging of warnings with their corresponding language feature or usage warning, to ensure that they are properly controlled by ShouldWarn(), and to ensure that warnings never issue at code sites in module files, two new Warn() member function templates were added to SemanticsContext and other contextual frameworks. Warn() can't be used before source locations can be mapped to scopes, but the bulk of existing code blocks testing ShouldWarn() and FindModuleFile() before calling Say() were convertible into calls to Warn(). The ones that were not convertible were extended with explicit calls to Message::set_languageFeature() and set_usageWarning().
2024-09-20[flang] Fix edge case regression (#109350)Peter Klausler1-2/+2
A recent fix to the emission of derived type names to module files exposed a regression in the case of a derived type that (1) has the same name as a generic procedure interface and (2) has undergone renaming through USE association before (3) being used in a declaration significant to a module procedure interface. Fix.
2024-07-30[flang] Detect use-before-decl errors on type parameters (#99947)Peter Klausler1-12/+11
Ensure that type parameters are declared as such before being referenced within the derived type definition. (Previously, such references would resolve to symbols in the enclosing scope.) This change causes the symbols for the type parameters to be created when the TYPE statement is processed in name resolution. They are TypeParamDetails symbols with no KIND/LEN attribute set, and they shadow any symbols of the same name in the enclosing scope. When the type parameter declarations are processed, the KIND/LEN attributes are set. Any earlier reference to a type parameter with no KIND/LEN attribute elicits an error. Some members of TypeParamDetails have been retyped &/or renamed.
2024-07-11[flang] Add -fhermetic-module-files (#98083)Peter Klausler1-10/+32
Module files emitted by this Fortran compiler are valid Fortran source files. Symbols that are USE-associated into modules are represented in their module files with USE statements and special comments with hash codes in them to ensure that those USE statements resolve to the same modules that were used to build the module when its module file was generated. This scheme prevents unchecked module file growth in large applications by not emitting USE-associated symbols redundantly. This problem can be especially bad when derived type definitions must be repeated in the module files of their clients, and the clients of those modules, and so on. However, this scheme has the disadvantage that clients of modules must be compiled with dependent modules in the module search path. This new -fhermetic-module-files option causes module file output to be free of dependences on any non-intrinsic module files; dependent modules are instead emitted as part of the module file, rather than being USE-associated. It is intended for top level library module files that are shipped with binary libraries when it is not convenient to collect and ship their dependent module files as well. Fixes https://github.com/llvm/llvm-project/issues/97398.
2024-05-24[flang] Fix bug introduced by PR#93106 (#93326)Peter Klausler1-0/+3
https://github.com/llvm/llvm-project/pull/93106 introduced some necessary fixes to module file generation, but has also caused a regression. The module file output can include bogus attempts to USE-associate symbols local to derived type scopes, like components and bindings. Fix, and extend a test.
2024-05-23[flang] Better renaming in module files (#93106)Peter Klausler1-55/+98
When a symbol from one module is used in another without an explicit USE association, the module file output code may need to use another name for it -- either with a name that is already available via USE association with renaming, or by means of a new private USE association, possibly with renaming to avoid a clash. Module file output was dealing properly with names of derived types, but wasn't accounting for symbols that appear in expressions other than initializations. This was specifically a problem with an application module that had a call to a NOPASS type-bound procedure in an array bound specification expression, which semantics had resolved to the name of a private module function. This patch implements renaming, when necessary, for all symbols appearing in expressions and type names, and replaces the previous implementation of derived type renaming. It also gets a little smarter about avoiding the creation of compiler-generated names when a name from another module has been brought into scope already by means of USE association with renaming.
2024-05-15[flang] New -fdebug-unparse-with-modules option (#91660)Peter Klausler1-2/+18
This option is a compilation action that parses a source file and performs semantic analysis on it, like the existing -fdebug-unparse option does. Its output, however, is preceded by the effective contents of all of the non-intrinsic modules on which it depends but does not define, transitively preceded by the closure of all of those modules' dependencies. The output from this option is therefore the analyzed parse tree for a source file encapsulated with all of its non-intrinsic module dependencies. This output may be useful for extracting code from large applications for use as an attachment to a bug report, or as input to a test case reduction tool for problem isolation.
2024-05-01[flang] Ensure all warning/portability messages are guarded by Should… ↵Peter Klausler1-5/+9
(#90518) …Warn() Many warning messages were being emitted unconditionally. Ensure that all warnings are conditional on a true result from a call to common::LanguageFeatureControl::ShouldWarn() so that it is easy for a driver to disable them all, or, in the future, to provide per-warning control over them.
2024-05-01[flang] Fix CHECK() crash in module file generator (#90234)Peter Klausler1-1/+1
A sanity CHECK() in mod-file.cpp needs to allow for USE association of a derived type that has the same name as a locally defined generic interface. Fixes https://github.com/llvm/llvm-project/issues/90192.
2024-04-08[flang] Clean up ISO_FORTRAN_ENV, fix NUMERIC_STORAGE_SIZE (#87566)Peter Klausler1-3/+3
Address TODOs in the intrinsic module ISO_FORTRAN_ENV, and extend the implementation of NUMERIC_STORAGE_SIZE so that the calculation of its value is deferred until it is needed so that the effects of -fdefault-integer-8 or -fdefault-real-8 are reflected. Emit a warning when NUMERIC_STORAGE_SIZE is used from the module file and the default integer and real sizes do not match. Fixes https://github.com/llvm/llvm-project/issues/87476.
2024-03-13[flang] Support multiple distinct module files with same name in one … ↵Peter Klausler1-46/+39
(#84838) …compilation Allow multiple module files with the same module name to exist in one compilation; distinct modules are distinguished by their hashes.
2024-03-05[flang] Emit "raw" name for procedure interface in module file (#83915)Peter Klausler1-4/+4
Save both the raw procedure interface symbol as well as the result of passing it through GetUltimate() and BypassGeneric() in symbol table entries with ProcEntityDetails. The raw symbol of the interface needs to be the one used for emitting procedure symbols to module files. Fixes https://github.com/llvm/llvm-project/issues/83836.
2024-03-01[flang] Use module file hashes for more checking and disambiguation (#80354)Peter Klausler1-27/+181
f18's module files are Fortran with a leading header comment containing the module file format version and a hash of the following contents. This hash is currently used only to protect module files against corruption and truncation. Extend the use of these hashes to catch or avoid some error cases. When one module file depends upon another, note its hash in additional module file header comments. This allows the compiler to detect when the module dependency is on a module file that has been updated. Further, it allows the compiler to find the right module file dependency when the same module file name appears in multiple directories on the module search path. The order in which module files are written, when multiple modules appear in a source file, is such that every dependency is written before the module(s) that depend upon it, so that their hashes are known. A warning is emitted when a module file is not the first hit on the module file search path. Further work is needed to add a compiler option that emits (larger) stand-alone module files that incorporate copies of their dependencies rather than relying on search paths. This will be desirable for application libraries that want to ship only "top-level" module files without needing to include their dependencies. Another future work item would be to admit multiple modules in the same compilation with the same name if they have distinct hashes.
2024-01-29[flang] Turn "error" cases into warning for "indistinguishable" specific ↵Peter Klausler1-3/+6
procedures (#79621) When a generic procedure interface, either declared or the result of merging two use-associated generics, has two specific procedures that are not distinguishable according to the rules in F'2023 subclause 15.4.3.4.5, emit a portability warning rather than a hard error message. The rules in that subclause are not adequate to detect pairs of specific procedures that admit an ambiguous reference, as demonstrated by a case that arose in pFUnit. Further, these distinguishability checks, even if sufficient to the task of detecting pairs of specifics capable of ambiguous references, should only apply to pairs where *every* reference would have to be ambiguous -- and this can and is validated at every reference anyway. Last, only XLF enforces these incomplete and needless distinguishability rules -- every other compiler seems to just check that each procedure reference resolves to exactly one specific procedure. If the standard were to complete lose subclause 15.4.3.4.5 and its related note (C.11.6) -- which admits that the rules are incomplete! -- and simply require that each generic procedure reference resolve unambiguously to exactly one specific, nobody would miss them. This patch changes this compiler to give them lip service when requested, but they are now otherwise ignored.
2024-01-25[flang] Fix module file generation when generic shadows derived type (#78618)Peter Klausler1-0/+7
Fortran allows the name of a generic interface to be the same as the name of a derived type or specific procedure. When this happens, it causes the code in module file generation to miss the symbol of a derived type when scanning for symbols in initialization expressions that need to be imported. Fix.
2024-01-15[flang] Refine IMPORT processing in module file generation (#77133)Peter Klausler1-9/+16
Procedure interfaces emitted to module files are including IMPORT statements for some symbols that don't need to be imported (base types and procedure interfaces for components of imported derived types) and omitting others (procedure interfaces for bindings in locally-defined derived types that are material to the interface).
2024-01-11[flang][openacc] Carry device dependent info for routine in the module fileValentin Clement1-17/+37
2023-10-31[flang] Fix mod file generation of derived type initializers... (#70511)Peter Klausler1-27/+111
... when the derived type used in the structure constructor(s) is from another module and not use-associated into the current module. This came up in a test with a derived type component default initializer of "c_null_ptr", which is replaced with the expression "__builtin_c_ptr(address=0_8)"; the derived type name "__builtin_c_ptr" is not available in the current scope, and the module file would fail semantic analysis when USE'd. The best solution that I found was to extend module file generation to detect this case and handle it by inserting the right USE association to the ultimate derived type symbol, possibly with renaming to a compiler-created name in the case of a conflict. To implement this transformation, it was necessary to fix the utility evaluate::CollectSymbols() to include the derived type symbol from a structure constructor. This involved extending the expression traversal framework to visit the derived type spec of a structure constructor. Extending CollectSymbols() caused a lowering test to fail mysteriously, so I tracked down the code in PFTBuilder that didn't expect to see a DerivedTypeDetails symbol and dealt with it there.
2023-10-16[flang] Submodule names can clash only with submodule names (#67361)Peter Klausler1-12/+31
Name resolution creates symbols for submodules in their parents' scopes. This can lead to bogus errors about name clashes between submodule names and other entities in the parents' scopes. Create symbols for submodules but do not add them to a scope's dictionary.
2023-09-25[flang] Do not write implicit SAVE attribute into the mod file. (#67215)Slava Zakharin1-2/+10
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.
2023-09-12[flang] Cray pointer in module (#66119)kkwli1-0/+9
This patch is to add the support of declaring a Cray pointer in a module.
2023-08-23[flang][openacc] Keep routine information in the module fileValentin Clement1-0/+30
This patch propagates the acc routine information to the module file so they can be used by the caller. Reviewed By: razvanlupusoru Differential Revision: https://reviews.llvm.org/D158541
2023-08-08[flang] Allow reference to earlier generic in later interfacePeter Klausler1-26/+18
Name resolutions defers all resolution and checking of specific procedures in non-type-bound generic interfaces to the end of the specification part. This prevents expression analysis of references to generic functions in specification expressions in interfaces from resolving. Example (now a new test case in modfile07.f90): ``` module m12 interface generic module procedure specific end interface interface module subroutine s(a1,a2) character(*) a1 character(generic(a1)) a2 ! <-- end end interface contains pure integer function specific(x) character(*), intent(in) :: x specific = len(x) end end ``` The solution is to partially resolve specific procedures as they are defined for each generic, when they can be resolved, with the final pass at the end of the specification part to finish up any forward references and emit the necessary error messages. Making this fix caused some issues in module file output, which have all been resolved by making this simplifying change: generics are now all emitted to module file specification parts as their own group of declarations at the end of the specification part, followed only by namelists and COMMON blocks. Differential Revision: https://reviews.llvm.org/D157346
2023-08-07[flang][openacc] Support readonly modifier for declare copyin in module fileValentin Clement1-2/+7
Distinguish between copyin and copyin with the readonly modifier. Depends on D157121 Reviewed By: razvanlupusoru Differential Revision: https://reviews.llvm.org/D157125
2023-08-04[flang][NFC] Reorganize directive outputValentin Clement1-4/+12
OpenACC and OpenMP directive are emitted in the module file. Just reorganized the code so this is well separated and do not pollute the main Fortran part. Reviewed By: kiranchandramohan Differential Revision: https://reviews.llvm.org/D157143
2023-08-04[flang][openacc] Retain the declare information in module file declarationValentin Clement1-0/+25
F18 produce a module file with a declaration part. This patch propagates the OpenACC declare information in the module file along the variable declaration. Depends on D157042 Reviewed By: razvanlupusoru Differential Revision: https://reviews.llvm.org/D157121
2023-05-31[flang] CUDA Fortran - part 2/5: symbols & scopesPeter Klausler1-0/+30
Add representations of CUDA Fortran data and subprogram attributes to the symbol table and scopes of semantics. Set them in name resolution, and emit them to module files. Depends on https://reviews.llvm.org/D150159. Differential Revision: https://reviews.llvm.org/D150161
2023-04-19[flang] Semantics for !DIR$ IGNORE_TKRPeter Klausler1-1/+27
Implement semantics for the IGNORE_TKR directive as it is interpreted by the PGI / NVFORTRAN compiler. Differential Revision: https://reviews.llvm.org/D148643
2023-03-02[flang] BIND(C,NAME=...) correctionsPeter Klausler1-7/+14
The Fortran standard's various restrictions on the use of BIND(C) often depend more on the presence or absence of an explicit NAME= specification rather than on its value, but semantics and module file generation aren't making distinctions between explicit NAME= specifications that happen to match the default name and declarations that don't have NAME=. Tweak semantics and module file generation to conform, and also complain when named BIND(C) attributes are erroneously applied to entities that can't support them, like ABSTRACT interfaces. Differential Revision: https://reviews.llvm.org/D145107
2023-02-01[flang] Suppress a warning in D118631 [-Wunused-result]Shao-Ce SUN1-2/+4
Reviewed By: klausler Differential Revision: https://reviews.llvm.org/D142965
2022-12-16[flang] Expunge needless semantics::ProcInterfacePeter Klausler1-10/+9
The ProcInterface structure is used only by ProcEntityDetails; it represents what a program might have put in parentheses in a procedure-declaration-stmt, either the name of a procedure interface or a declaration-type-spec. If a procedure entity has an implicit interface, the function result type (if any) can be kept in EntityDetails::type_, which already exists and is currently redundant for ProcEntityDetails symbols. All that is really needed is a nullable Symbol pointer in ProcEntityDetails to point to the procedure's explicit interface, when it has one. Also, catch the case where a procedure has an explicit interface and a program attempts to also give it a type. Differential Revision: https://reviews.llvm.org/D140134
2022-12-05[flang] Map symbols in expressions when copying interface symbolsPeter Klausler1-1/+4
Given a MODULE SUBROUTINE or MODULE FUNCTION interface followed later by a corresponding separate module subprogram definition in a MODULE PROCEDURE, the copies of the interface's dummy argument and function result symbols that populate the initial scope of that MODULE PROCEDURE need to have any symbol references in their types or bounds adjusted to point to their new counterparts. Differential Revision: https://reviews.llvm.org/D139200