From de93a79fc6bfecf546f0e83fae67048c20ca947d Mon Sep 17 00:00:00 2001 From: bruteforceboy Date: Mon, 7 Oct 2024 11:10:26 +0300 Subject: [PATCH 1/4] [CIR][CodeGen] Enable -fno-PIE --- clang/lib/CIR/CodeGen/CIRGenModule.cpp | 11 +++++++---- clang/test/CIR/CodeGen/no-pie.c | 3 +++ 2 files changed, 10 insertions(+), 4 deletions(-) create mode 100644 clang/test/CIR/CodeGen/no-pie.c diff --git a/clang/lib/CIR/CodeGen/CIRGenModule.cpp b/clang/lib/CIR/CodeGen/CIRGenModule.cpp index 68e8e93e5b77..ee72acefffd5 100644 --- a/clang/lib/CIR/CodeGen/CIRGenModule.cpp +++ b/clang/lib/CIR/CodeGen/CIRGenModule.cpp @@ -74,8 +74,8 @@ #include "llvm/Support/Casting.h" #include "llvm/Support/ErrorHandling.h" #include "llvm/Support/FileSystem.h" -#include "llvm/Support/raw_ostream.h" #include "llvm/Support/TimeProfiler.h" +#include "llvm/Support/raw_ostream.h" #include #include @@ -192,8 +192,7 @@ CIRGenModule::CIRGenModule(mlir::MLIRContext &context, theModule->setAttr("cir.sob", mlir::cir::SignedOverflowBehaviorAttr::get(&context, sob)); auto lang = SourceLanguageAttr::get(&context, getCIRSourceLanguage()); - theModule->setAttr( - "cir.lang", mlir::cir::LangAttr::get(&context, lang)); + theModule->setAttr("cir.lang", mlir::cir::LangAttr::get(&context, lang)); theModule->setAttr("cir.triple", builder.getStringAttr(getTriple().str())); // Set the module name to be the name of the main file. TranslationUnitDecl // often contains invalid source locations and isn't a reliable source for the @@ -449,7 +448,11 @@ static bool shouldAssumeDSOLocal(const CIRGenModule &CGM, return false; if (CGOpts.DirectAccessExternalData) { - llvm_unreachable("-fdirect-access-external-data not supported"); + if (auto gv = dyn_cast(GV.getOperation())) + return !gv.getTlsModel().has_value(); + if (isa(GV) && !CGOpts.NoPLT && + RM == llvm::Reloc::Static) + return true; } // If we can use copy relocations we can assume it is local. diff --git a/clang/test/CIR/CodeGen/no-pie.c b/clang/test/CIR/CodeGen/no-pie.c new file mode 100644 index 000000000000..45dc04db0fed --- /dev/null +++ b/clang/test/CIR/CodeGen/no-pie.c @@ -0,0 +1,3 @@ +// RUN: %clang --target=x86_64-unknown-linux-gnu -fclangir %s -fno-PIE -c + +void empty(void) {} \ No newline at end of file From 0532af5bdc486204b77353f80261b726e34a9adf Mon Sep 17 00:00:00 2001 From: bruteforceboy Date: Mon, 7 Oct 2024 11:33:25 +0300 Subject: [PATCH 2/4] undo clang-format --- clang/lib/CIR/CodeGen/CIRGenModule.cpp | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/clang/lib/CIR/CodeGen/CIRGenModule.cpp b/clang/lib/CIR/CodeGen/CIRGenModule.cpp index ee72acefffd5..81646239c2e8 100644 --- a/clang/lib/CIR/CodeGen/CIRGenModule.cpp +++ b/clang/lib/CIR/CodeGen/CIRGenModule.cpp @@ -74,8 +74,8 @@ #include "llvm/Support/Casting.h" #include "llvm/Support/ErrorHandling.h" #include "llvm/Support/FileSystem.h" -#include "llvm/Support/TimeProfiler.h" #include "llvm/Support/raw_ostream.h" +#include "llvm/Support/TimeProfiler.h" #include #include @@ -192,7 +192,8 @@ CIRGenModule::CIRGenModule(mlir::MLIRContext &context, theModule->setAttr("cir.sob", mlir::cir::SignedOverflowBehaviorAttr::get(&context, sob)); auto lang = SourceLanguageAttr::get(&context, getCIRSourceLanguage()); - theModule->setAttr("cir.lang", mlir::cir::LangAttr::get(&context, lang)); + theModule->setAttr( + "cir.lang", mlir::cir::LangAttr::get(&context, lang)); theModule->setAttr("cir.triple", builder.getStringAttr(getTriple().str())); // Set the module name to be the name of the main file. TranslationUnitDecl // often contains invalid source locations and isn't a reliable source for the From 367b09ff8e3f249b8918474489337e39f1b40b90 Mon Sep 17 00:00:00 2001 From: bruteforceboy Date: Tue, 8 Oct 2024 14:27:21 +0300 Subject: [PATCH 3/4] add comments from original cg --- clang/lib/CIR/CodeGen/CIRGenModule.cpp | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/clang/lib/CIR/CodeGen/CIRGenModule.cpp b/clang/lib/CIR/CodeGen/CIRGenModule.cpp index 81646239c2e8..f8d39e40b6c9 100644 --- a/clang/lib/CIR/CodeGen/CIRGenModule.cpp +++ b/clang/lib/CIR/CodeGen/CIRGenModule.cpp @@ -449,8 +449,21 @@ static bool shouldAssumeDSOLocal(const CIRGenModule &CGM, return false; if (CGOpts.DirectAccessExternalData) { + // 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(GV.getOperation())) - return !gv.getTlsModel().has_value(); + 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(GV) && !CGOpts.NoPLT && RM == llvm::Reloc::Static) return true; From d797bd4d3b8407ebed44d90fc58843e0f1cae528 Mon Sep 17 00:00:00 2001 From: bruteforceboy Date: Wed, 9 Oct 2024 11:07:03 +0300 Subject: [PATCH 4/4] update no-pie.c --- clang/test/CIR/CodeGen/no-pie.c | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/clang/test/CIR/CodeGen/no-pie.c b/clang/test/CIR/CodeGen/no-pie.c index 45dc04db0fed..c0ffd9790392 100644 --- a/clang/test/CIR/CodeGen/no-pie.c +++ b/clang/test/CIR/CodeGen/no-pie.c @@ -1,3 +1,11 @@ -// RUN: %clang --target=x86_64-unknown-linux-gnu -fclangir %s -fno-PIE -c +// 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 -void empty(void) {} \ No newline at end of file +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 \ No newline at end of file