Skip to content

[CIR][CodeGen] Enable -fno-PIE #940

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Oct 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion clang/lib/CIR/CodeGen/CIRGenModule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -449,7 +449,24 @@ static bool shouldAssumeDSOLocal(const CIRGenModule &CGM,
return false;

if (CGOpts.DirectAccessExternalData) {
llvm_unreachable("-fdirect-access-external-data not supported");
// If -fdirect-access-external-data (default for -fno-pic), set dso_local
// for non-thread-local variables. If the symbol is not defined in the
// executable, a copy relocation will be needed at link time. dso_local is
// excluded for thread-local variables because they generally don't support
// copy relocations.
if (auto gv = dyn_cast<mlir::cir::GlobalOp>(GV.getOperation()))
if (!gv.getTlsModelAttr())
return true;

// -fno-pic sets dso_local on a function declaration to allow direct
// accesses when taking its address (similar to a data symbol). If the
// function is not defined in the executable, a canonical PLT entry will be
// needed at link time. -fno-direct-access-external-data can avoid the
// canonical PLT entry. We don't generalize this condition to -fpie/-fpic as
// it could just cause trouble without providing perceptible benefits.
if (isa<mlir::cir::FuncOp>(GV) && !CGOpts.NoPLT &&
RM == llvm::Reloc::Static)
return true;
}

// If we can use copy relocations we can assume it is local.
Expand Down
11 changes: 11 additions & 0 deletions clang/test/CIR/CodeGen/no-pie.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// RUN: %clang -target x86_64-unknown-linux-gnu -fclangir -fno-PIE -S -Xclang -emit-cir %s -o %t1.cir
// RUN: FileCheck --input-file=%t1.cir %s -check-prefix=CIR
// RUN: %clang -target x86_64-unknown-linux-gnu -fclangir -fno-PIE -S -Xclang -emit-llvm %s -o %t1.ll
// RUN: FileCheck --input-file=%t1.ll %s -check-prefix=LLVM

extern int var;
int get() {
return var;
}
// CIR: cir.global "private" external dsolocal @var : !s32i {alignment = 4 : i64}
// LLVM: @var = external dso_local global i32