Skip to content

Commit 28d5bc5

Browse files
authored
[Flang][Driver] Predefine pic/pie macros based on configured level (#153449)
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
1 parent a5ba606 commit 28d5bc5

File tree

2 files changed

+52
-0
lines changed

2 files changed

+52
-0
lines changed

flang/lib/Frontend/CompilerInvocation.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1703,6 +1703,20 @@ void CompilerInvocation::setDefaultPredefinitions() {
17031703
fortranOptions.predefinitions.emplace_back("__flang_patchlevel__",
17041704
FLANG_VERSION_PATCHLEVEL_STRING);
17051705

1706+
// Add predefinitions based on the relocation model
1707+
if (unsigned PICLevel = getCodeGenOpts().PICLevel) {
1708+
fortranOptions.predefinitions.emplace_back("__PIC__",
1709+
std::to_string(PICLevel));
1710+
fortranOptions.predefinitions.emplace_back("__pic__",
1711+
std::to_string(PICLevel));
1712+
if (getCodeGenOpts().IsPIE) {
1713+
fortranOptions.predefinitions.emplace_back("__PIE__",
1714+
std::to_string(PICLevel));
1715+
fortranOptions.predefinitions.emplace_back("__pie__",
1716+
std::to_string(PICLevel));
1717+
}
1718+
}
1719+
17061720
// Add predefinitions based on extensions enabled
17071721
if (frontendOptions.features.IsEnabled(
17081722
Fortran::common::LanguageFeature::OpenACC)) {
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
! Check that the pie/pic/PIE/PIC macros are defined properly through the frontend driver
2+
3+
! RUN: %flang_fc1 -dM -E -o - %s \
4+
! RUN: | FileCheck %s
5+
! CHECK-NOT: #define __PIC__
6+
! CHECK-NOT: #define __PIE__
7+
! CHECK-NOT: #define __pic__
8+
! CHECK-NOT: #define __pie__
9+
!
10+
! RUN: %flang_fc1 -pic-level 1 -dM -E -o - %s \
11+
! RUN: | FileCheck --check-prefix=CHECK-PIC1 %s
12+
! CHECK-PIC1: #define __PIC__ 1
13+
! CHECK-PIC1-NOT: #define __PIE__
14+
! CHECK-PIC1: #define __pic__ 1
15+
! CHECK-PIC1-NOT: #define __pie__
16+
!
17+
! RUN: %flang_fc1 -pic-level 2 -dM -E -o - %s \
18+
! RUN: | FileCheck --check-prefix=CHECK-PIC2 %s
19+
! CHECK-PIC2: #define __PIC__ 2
20+
! CHECK-PIC2-NOT: #define __PIE__
21+
! CHECK-PIC2: #define __pic__ 2
22+
! CHECK-PIC2-NOT: #define __pie__
23+
!
24+
! RUN: %flang_fc1 -pic-level 1 -pic-is-pie -dM -E -o - %s \
25+
! RUN: | FileCheck --check-prefix=CHECK-PIE1 %s
26+
! CHECK-PIE1: #define __PIC__ 1
27+
! CHECK-PIE1: #define __PIE__ 1
28+
! CHECK-PIE1: #define __pic__ 1
29+
! CHECK-PIE1: #define __pie__ 1
30+
!
31+
! RUN: %flang_fc1 -pic-level 2 -pic-is-pie -dM -E -o - %s \
32+
! RUN: | FileCheck --check-prefix=CHECK-PIE2 %s
33+
! CHECK-PIE2: #define __PIC__ 2
34+
! CHECK-PIE2: #define __PIE__ 2
35+
! CHECK-PIE2: #define __pic__ 2
36+
! CHECK-PIE2: #define __pie__ 2
37+
38+
integer, parameter :: pic_level = __pic__

0 commit comments

Comments
 (0)