Skip to content

Commit 2075f2b

Browse files
committed
[clang] Support -fpic -fno-semantic-interposition for RISCV
-fno-semantic-interposition (only effective with -fpic) can optimize default visibility external linkage (non-ifunc-non-COMDAT) variable access and function calls to avoid GOT/PLT, by using local aliases, e.g. ``` int var; __attribute__((optnone)) int fun(int x) { return x * x; } int test() { return fun(var); } ``` -fpic (var and fun are dso_preemptable) ``` test: .LBB1_1: auipc a0, %got_pcrel_hi(var) ld a0, %pcrel_lo(.LBB1_1)(a0) lw a0, 0(a0) // fun is preemptible by default in ld -shared mode. ld will create a PLT. tail fun@plt ``` vs -fpic -fno-semantic-interposition (var and fun are dso_local) ``` test: .Ltest$local: .LBB1_1: auipc a0, %pcrel_hi(.Lvar$local) addi a0, a0, %pcrel_lo(.LBB1_1) lw a0, 0(a0) // The assembler either resolves .Lfun$local at assembly time (-mno-relax // -fno-function-sections), or produces a relocation referencing a non-preemptible // local symbol (which can avoid PLT). tail .Lfun$local ``` Note: Clang's default -fpic is more aggressive than GCC -fpic: interprocedural optimizations (including inlining) are available but local aliases are not used. -fpic -fsemantic-interposition can disable interprocedural optimizations. Depends on D101875 Reviewed By: luismarques Differential Revision: https://reviews.llvm.org/D101876
1 parent b2f227c commit 2075f2b

File tree

2 files changed

+4
-2
lines changed

2 files changed

+4
-2
lines changed

clang/lib/Driver/ToolChains/Clang.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4716,7 +4716,8 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA,
47164716
options::OPT_fno_semantic_interposition);
47174717
if (RelocationModel != llvm::Reloc::Static && !IsPIE) {
47184718
// The supported targets need to call AsmPrinter::getSymbolPreferLocal.
4719-
bool SupportsLocalAlias = Triple.isAArch64() || Triple.isX86();
4719+
bool SupportsLocalAlias =
4720+
Triple.isAArch64() || Triple.isRISCV() || Triple.isX86();
47204721
if (!A)
47214722
CmdArgs.push_back("-fhalf-no-semantic-interposition");
47224723
else if (A->getOption().matches(options::OPT_fsemantic_interposition))

clang/test/Driver/fsemantic-interposition.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
/// If -fno-semantic-interposition is specified and the target supports local
1212
/// aliases, neither CC1 option is set.
1313
// RUN: %clang -target aarch64 %s -Werror -fPIC -fno-semantic-interposition -c -### 2>&1 | FileCheck --check-prefix=NO %s
14+
// RUN: %clang -target riscv32 %s -Werror -fPIC -fno-semantic-interposition -c -### 2>&1 | FileCheck --check-prefix=NO %s
15+
// RUN: %clang -target riscv64 %s -Werror -fPIC -fno-semantic-interposition -c -### 2>&1 | FileCheck --check-prefix=NO %s
1416
// RUN: %clang -target i386 %s -Werror -fPIC -fno-semantic-interposition -c -### 2>&1 | FileCheck --check-prefix=NO %s
1517
// RUN: %clang -target x86_64 %s -Werror -fPIC -fno-semantic-interposition -c -### 2>&1 | FileCheck --check-prefix=NO %s
1618
// NO-NOT: "-fsemantic-interposition"
@@ -22,7 +24,6 @@
2224
/// optimizations are allowed but local aliases are not used. If references are
2325
/// not optimized out, semantic interposition at runtime is possible.
2426
// RUN: %clang -target ppc64le %s -Werror -fPIC -fno-semantic-interposition -c -### 2>&1 | FileCheck --check-prefix=HALF %s
25-
// RUN: %clang -target riscv64 %s -Werror -fPIC -fno-semantic-interposition -c -### 2>&1 | FileCheck --check-prefix=HALF %s
2627

2728
// RUN: %clang -target x86_64 %s -Werror -fPIC -c -### 2>&1 | FileCheck --check-prefix=HALF %s
2829
//

0 commit comments

Comments
 (0)