From 50ebbb0103aabc36ac13a70992b079496bc218ca Mon Sep 17 00:00:00 2001 From: Ian McInerney Date: Wed, 13 Aug 2025 17:32:57 +0100 Subject: [PATCH 1/3] [Flang][Driver] Predefine pic/pie macros based on configured level 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. --- flang/lib/Frontend/CompilerInvocation.cpp | 10 +++++ .../Preprocessing/defines_pic_compiler.F90 | 31 +++++++++++++++ .../Preprocessing/defines_pic_frontend.F90 | 38 +++++++++++++++++++ 3 files changed, 79 insertions(+) create mode 100644 flang/test/Preprocessing/defines_pic_compiler.F90 create mode 100644 flang/test/Preprocessing/defines_pic_frontend.F90 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__ From 8994077ba1a868c0c9b780ad0422d1a9ac1e9ad8 Mon Sep 17 00:00:00 2001 From: Ian McInerney Date: Wed, 13 Aug 2025 17:47:18 +0100 Subject: [PATCH 2/3] fixup! [Flang][Driver] Predefine pic/pie macros based on configured level --- flang/lib/Frontend/CompilerInvocation.cpp | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/flang/lib/Frontend/CompilerInvocation.cpp b/flang/lib/Frontend/CompilerInvocation.cpp index 7ea11a0809104..09ddfed80401a 100644 --- a/flang/lib/Frontend/CompilerInvocation.cpp +++ b/flang/lib/Frontend/CompilerInvocation.cpp @@ -1698,11 +1698,15 @@ void CompilerInvocation::setDefaultPredefinitions() { // 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)); + 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)); + fortranOptions.predefinitions.emplace_back("__PIE__", + std::to_string(PICLevel)); + fortranOptions.predefinitions.emplace_back("__pie__", + std::to_string(PICLevel)); } } From b8d541a8fa9f74260706ea522004321f1c965b67 Mon Sep 17 00:00:00 2001 From: Ian McInerney Date: Thu, 14 Aug 2025 15:58:08 +0100 Subject: [PATCH 3/3] fixup! fixup! [Flang][Driver] Predefine pic/pie macros based on configured level --- .../Preprocessing/defines_pic_compiler.F90 | 31 ------------------- 1 file changed, 31 deletions(-) delete mode 100644 flang/test/Preprocessing/defines_pic_compiler.F90 diff --git a/flang/test/Preprocessing/defines_pic_compiler.F90 b/flang/test/Preprocessing/defines_pic_compiler.F90 deleted file mode 100644 index 9b17f329a56d5..0000000000000 --- a/flang/test/Preprocessing/defines_pic_compiler.F90 +++ /dev/null @@ -1,31 +0,0 @@ -! 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__