Skip to content

[flang] Use module file hashes for more checking and disambiguation #80354

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

Merged
merged 1 commit into from
Mar 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions flang/include/flang/Parser/provenance.h
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ class AllSources {

void ClearSearchPath();
void AppendSearchPathDirectory(std::string); // new last directory
const SourceFile *OpenPath(std::string path, llvm::raw_ostream &error);
const SourceFile *Open(std::string path, llvm::raw_ostream &error,
std::optional<std::string> &&prependPath = std::nullopt);
const SourceFile *ReadStandardInput(llvm::raw_ostream &error);
Expand Down
2 changes: 2 additions & 0 deletions flang/include/flang/Parser/source.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ namespace Fortran::parser {
std::string DirectoryName(std::string path);
std::optional<std::string> LocateSourceFile(
std::string name, const std::list<std::string> &searchPath);
std::vector<std::string> LocateSourceFileAll(
std::string name, const std::vector<std::string> &searchPath);

class SourceFile;

Expand Down
51 changes: 51 additions & 0 deletions flang/include/flang/Semantics/module-dependences.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
//===-- include/flang/Semantics/module-dependences.h ------------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#ifndef FORTRAN_SEMANTICS_MODULE_DEPENDENCES_H_
#define FORTRAN_SEMANTICS_MODULE_DEPENDENCES_H_

#include <cinttypes>
#include <map>
#include <optional>
#include <string>

namespace Fortran::semantics {

using ModuleCheckSumType = std::uint64_t;

class ModuleDependences {
public:
void AddDependence(
std::string &&name, bool intrinsic, ModuleCheckSumType hash) {
if (intrinsic) {
intrinsicMap_.emplace(std::move(name), hash);
} else {
nonIntrinsicMap_.emplace(std::move(name), hash);
}
}
std::optional<ModuleCheckSumType> GetRequiredHash(
const std::string &name, bool intrinsic) {
if (intrinsic) {
if (auto iter{intrinsicMap_.find(name)}; iter != intrinsicMap_.end()) {
return iter->second;
}
} else {
if (auto iter{nonIntrinsicMap_.find(name)};
iter != nonIntrinsicMap_.end()) {
return iter->second;
}
}
return std::nullopt;
}

private:
std::map<std::string, ModuleCheckSumType> intrinsicMap_, nonIntrinsicMap_;
};

} // namespace Fortran::semantics
#endif // FORTRAN_SEMANTICS_MODULE_DEPENDENCES_H_
3 changes: 3 additions & 0 deletions flang/include/flang/Semantics/semantics.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include "flang/Evaluate/intrinsics.h"
#include "flang/Evaluate/target.h"
#include "flang/Parser/message.h"
#include "flang/Semantics/module-dependences.h"
#include <iosfwd>
#include <set>
#include <string>
Expand Down Expand Up @@ -108,6 +109,7 @@ class SemanticsContext {
parser::Messages &messages() { return messages_; }
evaluate::FoldingContext &foldingContext() { return foldingContext_; }
parser::AllCookedSources &allCookedSources() { return allCookedSources_; }
ModuleDependences &moduleDependences() { return moduleDependences_; }

SemanticsContext &set_location(
const std::optional<parser::CharBlock> &location) {
Expand Down Expand Up @@ -293,6 +295,7 @@ class SemanticsContext {
const Scope *ppcBuiltinsScope_{nullptr}; // module __ppc_intrinsics
std::list<parser::Program> modFileParseTrees_;
std::unique_ptr<CommonBlockMap> commonBlockMap_;
ModuleDependences moduleDependences_;
};

class Semantics {
Expand Down
8 changes: 7 additions & 1 deletion flang/include/flang/Semantics/symbol.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include "flang/Common/enum-set.h"
#include "flang/Common/reference.h"
#include "flang/Common/visit.h"
#include "flang/Semantics/module-dependences.h"
#include "llvm/ADT/DenseMapInfo.h"

#include <array>
Expand Down Expand Up @@ -86,11 +87,16 @@ class ModuleDetails : public WithOmpDeclarative {
void set_scope(const Scope *);
bool isDefaultPrivate() const { return isDefaultPrivate_; }
void set_isDefaultPrivate(bool yes = true) { isDefaultPrivate_ = yes; }
std::optional<ModuleCheckSumType> moduleFileHash() const {
return moduleFileHash_;
}
void set_moduleFileHash(ModuleCheckSumType x) { moduleFileHash_ = x; }

private:
bool isSubmodule_;
bool isDefaultPrivate_{false};
const Scope *scope_{nullptr};
std::optional<ModuleCheckSumType> moduleFileHash_;
};

class MainProgramDetails : public WithOmpDeclarative {
Expand Down Expand Up @@ -1035,7 +1041,7 @@ struct SymbolAddressCompare {
// Symbol comparison is usually based on the order of cooked source
// stream creation and, when both are from the same cooked source,
// their positions in that cooked source stream.
// Don't use this comparator or OrderedSymbolSet to hold
// Don't use this comparator or SourceOrderedSymbolSet to hold
// Symbols that might be subject to ReplaceName().
struct SymbolSourcePositionCompare {
// These functions are implemented in Evaluate/tools.cpp to
Expand Down
18 changes: 13 additions & 5 deletions flang/lib/Parser/provenance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,16 @@ void AllSources::AppendSearchPathDirectory(std::string directory) {
searchPath_.push_back(directory);
}

const SourceFile *AllSources::OpenPath(
std::string path, llvm::raw_ostream &error) {
std::unique_ptr<SourceFile> source{std::make_unique<SourceFile>(encoding_)};
if (source->Open(path, error)) {
return ownedSourceFiles_.emplace_back(std::move(source)).get();
} else {
return nullptr;
}
}

const SourceFile *AllSources::Open(std::string path, llvm::raw_ostream &error,
std::optional<std::string> &&prependPath) {
std::unique_ptr<SourceFile> source{std::make_unique<SourceFile>(encoding_)};
Expand All @@ -180,12 +190,10 @@ const SourceFile *AllSources::Open(std::string path, llvm::raw_ostream &error,
if (prependPath) {
searchPath_.pop_front();
}
if (!found) {
error << "Source file '" << path << "' was not found";
return nullptr;
} else if (source->Open(*found, error)) {
return ownedSourceFiles_.emplace_back(std::move(source)).get();
if (found) {
return OpenPath(*found, error);
} else {
error << "Source file '" << path << "' was not found";
return nullptr;
}
}
Expand Down
18 changes: 18 additions & 0 deletions flang/lib/Parser/source.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,24 @@ std::optional<std::string> LocateSourceFile(
return std::nullopt;
}

std::vector<std::string> LocateSourceFileAll(
std::string name, const std::vector<std::string> &searchPath) {
if (name == "-" || llvm::sys::path::is_absolute(name)) {
return {name};
}
std::vector<std::string> result;
for (const std::string &dir : searchPath) {
llvm::SmallString<128> path{dir};
llvm::sys::path::append(path, name);
bool isDir{false};
auto er = llvm::sys::fs::is_directory(path, isDir);
if (!er && !isDir) {
result.emplace_back(path.str().str());
}
}
return result;
}

std::size_t RemoveCarriageReturns(llvm::MutableArrayRef<char> buf) {
std::size_t wrote{0};
char *buffer{buf.data()};
Expand Down
Loading