Skip to content

Commit 72f329a

Browse files
[CIR][CodeGen] Enable -fno-PIE (#940)
The title describes the purpose of the PR. The logic was gotten from the original CodeGen, and I added a test to check that `-fno-PIE` is indeed enabled.
1 parent c3e1d1c commit 72f329a

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

clang/lib/CIR/CodeGen/CIRGenModule.cpp

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -449,7 +449,24 @@ static bool shouldAssumeDSOLocal(const CIRGenModule &CGM,
449449
return false;
450450

451451
if (CGOpts.DirectAccessExternalData) {
452-
llvm_unreachable("-fdirect-access-external-data not supported");
452+
// If -fdirect-access-external-data (default for -fno-pic), set dso_local
453+
// for non-thread-local variables. If the symbol is not defined in the
454+
// executable, a copy relocation will be needed at link time. dso_local is
455+
// excluded for thread-local variables because they generally don't support
456+
// copy relocations.
457+
if (auto gv = dyn_cast<mlir::cir::GlobalOp>(GV.getOperation()))
458+
if (!gv.getTlsModelAttr())
459+
return true;
460+
461+
// -fno-pic sets dso_local on a function declaration to allow direct
462+
// accesses when taking its address (similar to a data symbol). If the
463+
// function is not defined in the executable, a canonical PLT entry will be
464+
// needed at link time. -fno-direct-access-external-data can avoid the
465+
// canonical PLT entry. We don't generalize this condition to -fpie/-fpic as
466+
// it could just cause trouble without providing perceptible benefits.
467+
if (isa<mlir::cir::FuncOp>(GV) && !CGOpts.NoPLT &&
468+
RM == llvm::Reloc::Static)
469+
return true;
453470
}
454471

455472
// If we can use copy relocations we can assume it is local.

clang/test/CIR/CodeGen/no-pie.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// RUN: %clang -target x86_64-unknown-linux-gnu -fclangir -fno-PIE -S -Xclang -emit-cir %s -o %t1.cir
2+
// RUN: FileCheck --input-file=%t1.cir %s -check-prefix=CIR
3+
// RUN: %clang -target x86_64-unknown-linux-gnu -fclangir -fno-PIE -S -Xclang -emit-llvm %s -o %t1.ll
4+
// RUN: FileCheck --input-file=%t1.ll %s -check-prefix=LLVM
5+
6+
extern int var;
7+
int get() {
8+
return var;
9+
}
10+
// CIR: cir.global "private" external dsolocal @var : !s32i {alignment = 4 : i64}
11+
// LLVM: @var = external dso_local global i32

0 commit comments

Comments
 (0)