[clang] [Driver] Default LoongArch to -fno-direct-access-external-data for non-PIC (PR #72221)
via cfe-commits
cfe-commits at lists.llvm.org
Mon Nov 13 23:56:34 PST 2023
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang-driver
@llvm/pr-subscribers-clang
Author: Fangrui Song (MaskRay)
<details>
<summary>Changes</summary>
For -fno-pic, if an extern variable is defined in a DSO, a copy
relocation will be needed. However, loongarch*-linux does not and will
not support copy relocations.
Change Driver to default to -fno-direct-access-external-data for
LoongArch && non-PIC.
Keep Frontend conditions unchanged (-fdirect-access-external-data ||
-fno-direct-access-external-data && PIC>0 => direct access).
Fix #<!-- -->71645
---
Full diff: https://github.com/llvm/llvm-project/pull/72221.diff
2 Files Affected:
- (modified) clang/lib/Driver/ToolChains/Clang.cpp (+6-1)
- (modified) clang/test/Driver/fdirect-access-external-data.c (+6)
``````````diff
diff --git a/clang/lib/Driver/ToolChains/Clang.cpp b/clang/lib/Driver/ToolChains/Clang.cpp
index 3b98c7ae6e6ec66..b462f5a44057d94 100644
--- a/clang/lib/Driver/ToolChains/Clang.cpp
+++ b/clang/lib/Driver/ToolChains/Clang.cpp
@@ -5663,10 +5663,15 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA,
// defaults to -fno-direct-access-external-data. Pass the option if different
// from the default.
if (Arg *A = Args.getLastArg(options::OPT_fdirect_access_external_data,
- options::OPT_fno_direct_access_external_data))
+ options::OPT_fno_direct_access_external_data)) {
if (A->getOption().matches(options::OPT_fdirect_access_external_data) !=
(PICLevel == 0))
A->render(Args, CmdArgs);
+ } else if (PICLevel == 0 && Triple.isLoongArch()) {
+ // Some targets default to -fno-direct-access-external-data even for
+ // -fno-pic.
+ CmdArgs.push_back("-fno-direct-access-external-data");
+ }
if (Args.hasFlag(options::OPT_fno_plt, options::OPT_fplt, false)) {
CmdArgs.push_back("-fno-plt");
diff --git a/clang/test/Driver/fdirect-access-external-data.c b/clang/test/Driver/fdirect-access-external-data.c
index f132b1b088af35d..a6da776e6977742 100644
--- a/clang/test/Driver/fdirect-access-external-data.c
+++ b/clang/test/Driver/fdirect-access-external-data.c
@@ -9,6 +9,12 @@
// RUN: %clang -### -c -target aarch64 %s -fpic 2>&1 | FileCheck %s --check-prefix=DEFAULT
// RUN: %clang -### -c -target aarch64 %s -fpic -fdirect-access-external-data 2>&1 | FileCheck %s --check-prefix=DIRECT
+/// loongarch* targets default to -fno-direct-access-external-data even for -fno-pic.
+// RUN: %clang -### -c --target=loongarch64 -fno-pic %s 2>&1 | FileCheck %s --check-prefix=INDIRECT
+// RUN: %clang -### -c --target=loongarch64 -fpie %s 2>&1 | FileCheck %s --check-prefix=DEFAULT
+// RUN: %clang -### -c --target=loongarch32 -fno-pic -fdirect-access-external-data %s 2>&1 | FileCheck %s --check-prefix=DEFAULT
+// RUN: %clang -### -c --target=loongarch32 -fpie -fdirect-access-external-data %s 2>&1 | FileCheck %s --check-prefix=DIRECT
+
// DEFAULT-NOT: direct-access-external-data"
// DIRECT: "-fdirect-access-external-data"
// INDIRECT: "-fno-direct-access-external-data"
``````````
</details>
https://github.com/llvm/llvm-project/pull/72221
More information about the cfe-commits
mailing list