-
Notifications
You must be signed in to change notification settings - Fork 15.4k
[Flang][Driver] Predefine pic/pie macros based on configured level #153449
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
Conversation
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.
|
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 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. |
|
@llvm/pr-subscribers-flang-driver Author: Ian McInerney (imciner2) ChangesPredefine 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:
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__
|
|
✅ With the latest revision this PR passed the C/C++ code formatter. |
tblah
left a comment
There was a problem hiding this 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!
|
But see the windows CI failure, at a glance it seems relevant. |
|
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. |
tblah
left a comment
There was a problem hiding this 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?
Yes please. If you could squash merge this, that would be wonderful. |
|
@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! |
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