Skip to content

Conversation

@imciner2
Copy link
Contributor

@imciner2 imciner2 commented Aug 13, 2025

Predefine the __pic__/__pie__/__PIC__/__PIE__ macros based on the configured relocation level. This logic mirrors that of the clang driver, where __pic__/__PIC__ are defined for both PIC and PIE modes, but __pie__/__PIE__ are only defined for PIE mode.

Fixes #135275

Predefine the __pic__/__pie__/__PIC__/__PIE__ macros based on the
configured relocation level. This logic mirrors that of the clang
driver, where __pic__/__PIC__ are defined for both PIC and PIE modes,
but __pie__/__PIE__ are only defined for PIE mode.
@github-actions
Copy link

Thank you for submitting a Pull Request (PR) to the LLVM Project!

This PR will be automatically labeled and the relevant teams will be notified.

If you wish to, you can add reviewers by using the "Reviewers" section on this page.

If this is not working for you, it is probably because you do not have write permissions for the repository. In which case you can instead tag reviewers by name in a comment by using @ followed by their GitHub username.

If you have received no comments on your PR for a week, you can request a review by "ping"ing the PR by adding a comment “Ping”. The common courtesy "ping" rate is once a week. Please remember that you are asking for valuable time from other developers.

If you have further questions, they may be answered by the LLVM GitHub User Guide.

You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums.

@llvmbot llvmbot added flang:driver flang Flang issues not falling into any other category labels Aug 13, 2025
@llvmbot
Copy link
Member

llvmbot commented Aug 13, 2025

@llvm/pr-subscribers-flang-driver

Author: Ian McInerney (imciner2)

Changes

Predefine the pic/pie/PIC/PIE macros based on the configured relocation level. This logic mirrors that of the clang driver, where pic/PIC are defined for both PIC and PIE modes, but pie/PIE are only defined for PIE mode.

Fixes #135275


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

3 Files Affected:

  • (modified) flang/lib/Frontend/CompilerInvocation.cpp (+10)
  • (added) flang/test/Preprocessing/defines_pic_compiler.F90 (+31)
  • (added) flang/test/Preprocessing/defines_pic_frontend.F90 (+38)
diff --git a/flang/lib/Frontend/CompilerInvocation.cpp b/flang/lib/Frontend/CompilerInvocation.cpp
index 111c5aa48726f..7ea11a0809104 100644
--- a/flang/lib/Frontend/CompilerInvocation.cpp
+++ b/flang/lib/Frontend/CompilerInvocation.cpp
@@ -1696,6 +1696,16 @@ void CompilerInvocation::setDefaultPredefinitions() {
   fortranOptions.predefinitions.emplace_back("__flang_patchlevel__",
                                              FLANG_VERSION_PATCHLEVEL_STRING);
 
+  // Add predefinitions based on the relocation model
+  if (unsigned PICLevel = getCodeGenOpts().PICLevel) {
+    fortranOptions.predefinitions.emplace_back("__PIC__", std::to_string(PICLevel));
+    fortranOptions.predefinitions.emplace_back("__pic__", std::to_string(PICLevel));
+    if (getCodeGenOpts().IsPIE) {
+      fortranOptions.predefinitions.emplace_back("__PIE__", std::to_string(PICLevel));
+      fortranOptions.predefinitions.emplace_back("__pie__", std::to_string(PICLevel));
+    }
+  }
+
   // Add predefinitions based on extensions enabled
   if (frontendOptions.features.IsEnabled(
           Fortran::common::LanguageFeature::OpenACC)) {
diff --git a/flang/test/Preprocessing/defines_pic_compiler.F90 b/flang/test/Preprocessing/defines_pic_compiler.F90
new file mode 100644
index 0000000000000..9b17f329a56d5
--- /dev/null
+++ b/flang/test/Preprocessing/defines_pic_compiler.F90
@@ -0,0 +1,31 @@
+! Check that the pie/pic/PIE/PIC macros are defined properly through the compiler driver
+
+! RUN: %flang -fpic -dM -E -o - %s \
+! RUN:   | FileCheck --check-prefix=CHECK-PIC1 %s
+! CHECK-PIC1: #define __PIC__ 1
+! CHECK-PIC1-NOT: #define __PIE__
+! CHECK-PIC1: #define __pic__ 1
+! CHECK-PIC1-NOT: #define __pie__
+!
+! RUN: %flang -fPIC -dM -E -o - %s \
+! RUN:   | FileCheck --check-prefix=CHECK-PIC2 %s
+! CHECK-PIC2: #define __PIC__ 2
+! CHECK-PIC2-NOT: #define __PIE__
+! CHECK-PIC2: #define __pic__ 2
+! CHECK-PIC2-NOT: #define __pie__
+!
+! RUN: %flang -fpie -dM -E -o - %s \
+! RUN:   | FileCheck --check-prefix=CHECK-PIE1 %s
+! CHECK-PIE1: #define __PIC__ 1
+! CHECK-PIE1: #define __PIE__ 1
+! CHECK-PIE1: #define __pic__ 1
+! CHECK-PIE1: #define __pie__ 1
+!
+! RUN: %flang -fPIE -dM -E -o - %s \
+! RUN:   | FileCheck --check-prefix=CHECK-PIE2 %s
+! CHECK-PIE2: #define __PIC__ 2
+! CHECK-PIE2: #define __PIE__ 2
+! CHECK-PIE2: #define __pic__ 2
+! CHECK-PIE2: #define __pie__ 2
+
+integer, parameter :: pic_level = __pic__
diff --git a/flang/test/Preprocessing/defines_pic_frontend.F90 b/flang/test/Preprocessing/defines_pic_frontend.F90
new file mode 100644
index 0000000000000..ad871e0acfab3
--- /dev/null
+++ b/flang/test/Preprocessing/defines_pic_frontend.F90
@@ -0,0 +1,38 @@
+! Check that the pie/pic/PIE/PIC macros are defined properly through the frontend driver
+
+! RUN: %flang_fc1 -dM -E -o - %s \
+! RUN:   | FileCheck %s
+! CHECK-NOT: #define __PIC__
+! CHECK-NOT: #define __PIE__
+! CHECK-NOT: #define __pic__
+! CHECK-NOT: #define __pie__
+!
+! RUN: %flang_fc1 -pic-level 1 -dM -E -o - %s \
+! RUN:   | FileCheck --check-prefix=CHECK-PIC1 %s
+! CHECK-PIC1: #define __PIC__ 1
+! CHECK-PIC1-NOT: #define __PIE__
+! CHECK-PIC1: #define __pic__ 1
+! CHECK-PIC1-NOT: #define __pie__
+!
+! RUN: %flang_fc1 -pic-level 2 -dM -E -o - %s \
+! RUN:   | FileCheck --check-prefix=CHECK-PIC2 %s
+! CHECK-PIC2: #define __PIC__ 2
+! CHECK-PIC2-NOT: #define __PIE__
+! CHECK-PIC2: #define __pic__ 2
+! CHECK-PIC2-NOT: #define __pie__
+!
+! RUN: %flang_fc1 -pic-level 1 -pic-is-pie -dM -E -o - %s \
+! RUN:   | FileCheck --check-prefix=CHECK-PIE1 %s
+! CHECK-PIE1: #define __PIC__ 1
+! CHECK-PIE1: #define __PIE__ 1
+! CHECK-PIE1: #define __pic__ 1
+! CHECK-PIE1: #define __pie__ 1
+!
+! RUN: %flang_fc1 -pic-level 2 -pic-is-pie -dM -E -o - %s \
+! RUN:   | FileCheck --check-prefix=CHECK-PIE2 %s
+! CHECK-PIE2: #define __PIC__ 2
+! CHECK-PIE2: #define __PIE__ 2
+! CHECK-PIE2: #define __pic__ 2
+! CHECK-PIE2: #define __pie__ 2
+
+integer, parameter :: pic_level = __pic__

@github-actions
Copy link

github-actions bot commented Aug 13, 2025

✅ With the latest revision this PR passed the C/C++ code formatter.

Copy link
Contributor

@tblah tblah left a comment

Choose a reason for hiding this comment

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

LGTM, thank you for contributing this!

@tblah
Copy link
Contributor

tblah commented Aug 14, 2025

But see the windows CI failure, at a glance it seems relevant.

@imciner2
Copy link
Contributor Author

Yea, the Windows CI failure was because I had both a compiler driver and frontend driver test, and the compiler driver doesn't actually support this on Windows. So I've changed it to be just the frontend driver now, which is also what clang only tests for this option as well.

Copy link
Contributor

@tblah tblah left a comment

Choose a reason for hiding this comment

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

Thanks. Do you want help to merge?

@imciner2
Copy link
Contributor Author

Thanks. Do you want help to merge?

Yes please. If you could squash merge this, that would be wonderful.

@tblah tblah merged commit 28d5bc5 into llvm:main Aug 14, 2025
9 checks passed
@github-actions
Copy link

@imciner2 Congratulations on having your first Pull Request (PR) merged into the LLVM Project!

Your changes will be combined with recent changes from other authors, then tested by our build bots. If there is a problem with a build, you may receive a report in an email or a comment on this PR.

Please check whether problems have been caused by your change specifically, as the builds can include changes from many authors. It is not uncommon for your change to be included in a build that fails due to someone else's changes, or infrastructure issues.

How to do this, and the rest of the post-merge process, is covered in detail here.

If your change does cause a problem, it may be reverted, or you can revert it yourself. This is a normal part of LLVM development. You can fix your changes and open a new PR to merge them again.

If you don't get any reports, no action is required from you. Your changes are working as expected, well done!

@imciner2 imciner2 deleted the im/picdefines branch August 15, 2025 09:09
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.

[Flang] Define PIC/PIE macros

3 participants