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] Pass Argv0 to getIntriniscDir and getOpenMPHeadersDir #73254

Merged
merged 2 commits into from
Dec 4, 2023

Conversation

madanial0
Copy link
Contributor

The llvm::sys::fs::getMainExecutable(nullptr, nullptr) is not able to obtain the correct executable path on AIX without Argv0 due to the lack of a current process on AIX's proc filesystem. This causes a build failure on AIX as intrinsic module directory is missing.

@madanial0 madanial0 added the flang Flang issues not falling into any other category label Nov 23, 2023
@madanial0 madanial0 self-assigned this Nov 23, 2023
@llvmbot
Copy link
Collaborator

llvmbot commented Nov 23, 2023

@llvm/pr-subscribers-flang-driver

Author: None (madanial0)

Changes

The llvm::sys::fs::getMainExecutable(nullptr, nullptr) is not able to obtain the correct executable path on AIX without Argv0 due to the lack of a current process on AIX's proc filesystem. This causes a build failure on AIX as intrinsic module directory is missing.


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

2 Files Affected:

  • (modified) flang/include/flang/Frontend/CompilerInvocation.h (+6)
  • (modified) flang/lib/Frontend/CompilerInvocation.cpp (+10-6)
diff --git a/flang/include/flang/Frontend/CompilerInvocation.h b/flang/include/flang/Frontend/CompilerInvocation.h
index 229aa75748f725d..b345806586e04ef 100644
--- a/flang/include/flang/Frontend/CompilerInvocation.h
+++ b/flang/include/flang/Frontend/CompilerInvocation.h
@@ -101,6 +101,8 @@ class CompilerInvocation : public CompilerInvocationBase {
 
   bool warnAsErr = false;
 
+  const char *argv0;
+
   /// This flag controls the unparsing and is used to decide whether to print
   /// out the semantically analyzed version of an object or expression or the
   /// plain version that does not include any information from semantic
@@ -190,6 +192,8 @@ class CompilerInvocation : public CompilerInvocationBase {
     return enableConformanceChecks;
   }
 
+  const char *getArgv0() { return argv0; }
+
   bool &getEnableUsageChecks() { return enableUsageChecks; }
   const bool &getEnableUsageChecks() const { return enableUsageChecks; }
 
@@ -223,6 +227,8 @@ class CompilerInvocation : public CompilerInvocationBase {
   void setEnableUsageChecks() { enableUsageChecks = true; }
 
   /// Useful setters
+  void setArgv0(const char *dir) { argv0 = dir; }
+
   void setModuleDir(std::string &dir) { moduleDir = dir; }
 
   void setModuleFileSuffix(const char *suffix) {
diff --git a/flang/lib/Frontend/CompilerInvocation.cpp b/flang/lib/Frontend/CompilerInvocation.cpp
index cb4f2d6a6225205..2afea5ad6b9d97c 100644
--- a/flang/lib/Frontend/CompilerInvocation.cpp
+++ b/flang/lib/Frontend/CompilerInvocation.cpp
@@ -687,19 +687,19 @@ static bool parseFrontendArgs(FrontendOptions &opts, llvm::opt::ArgList &args,
 }
 
 // Generate the path to look for intrinsic modules
-static std::string getIntrinsicDir() {
+static std::string getIntrinsicDir(const char *argv) {
   // TODO: Find a system independent API
   llvm::SmallString<128> driverPath;
-  driverPath.assign(llvm::sys::fs::getMainExecutable(nullptr, nullptr));
+  driverPath.assign(llvm::sys::fs::getMainExecutable(argv, nullptr));
   llvm::sys::path::remove_filename(driverPath);
   driverPath.append("/../include/flang/");
   return std::string(driverPath);
 }
 
 // Generate the path to look for OpenMP headers
-static std::string getOpenMPHeadersDir() {
+static std::string getOpenMPHeadersDir(const char *argv) {
   llvm::SmallString<128> includePath;
-  includePath.assign(llvm::sys::fs::getMainExecutable(nullptr, nullptr));
+  includePath.assign(llvm::sys::fs::getMainExecutable(argv, nullptr));
   llvm::sys::path::remove_filename(includePath);
   includePath.append("/../include/flang/OpenMP/");
   return std::string(includePath);
@@ -1203,6 +1203,8 @@ bool CompilerInvocation::createFromArgs(
     }
   }
 
+  res.setArgv0(argv0);
+
   return success;
 }
 
@@ -1245,7 +1247,8 @@ void CompilerInvocation::setDefaultFortranOpts() {
 
   // Add the location of omp_lib.h to the search directories. Currently this is
   // identical to the modules' directory.
-  fortranOptions.searchDirectories.emplace_back(getOpenMPHeadersDir());
+  fortranOptions.searchDirectories.emplace_back(
+      getOpenMPHeadersDir(getArgv0()));
 
   fortranOptions.isFixedForm = false;
 }
@@ -1310,7 +1313,8 @@ void CompilerInvocation::setFortranOpts() {
       preprocessorOptions.searchDirectoriesFromIntrModPath.end());
 
   //  Add the default intrinsic module directory
-  fortranOptions.intrinsicModuleDirectories.emplace_back(getIntrinsicDir());
+  fortranOptions.intrinsicModuleDirectories.emplace_back(
+      getIntrinsicDir(getArgv0()));
 
   // Add the directory supplied through -J/-module-dir to the list of search
   // directories

@brad0
Copy link
Contributor

brad0 commented Nov 24, 2023

This fixes building the Flang modules on OpenBSD, NetBSD and Haiku.

@brad0
Copy link
Contributor

brad0 commented Nov 27, 2023

I see you re-based 2 times. Does this look Ok as is? It would be good to add the AIX Driver bits once this goes in.

@madanial0
Copy link
Contributor Author

I see you re-based 2 times. Does this look Ok as is? It would be good to add the AIX Driver bits once this goes in.

This should be good as is pending any review comments/approvals

@brad0
Copy link
Contributor

brad0 commented Nov 29, 2023

It would be nice to get this in so Flang is buildable.

@DanielCChen
Copy link
Contributor

The changes look good to me. I will wait a day to see if other reviewers have any more comments before approving it.

Copy link
Contributor

@kiranchandramohan kiranchandramohan left a comment

Choose a reason for hiding this comment

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

LG.

@@ -101,6 +101,8 @@ class CompilerInvocation : public CompilerInvocationBase {

bool warnAsErr = false;

const char *argv0;
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit: This is probably obvious but will be good to add executable name as a comment above.

@madanial0 madanial0 merged commit 8dc474c into llvm:main Dec 4, 2023
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
flang:driver flang Flang issues not falling into any other category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants