From 47f6cd04434e11796f15a1f37189c8cf6ac58ac7 Mon Sep 17 00:00:00 2001 From: Kepontry Date: Sat, 16 Sep 2023 11:31:04 +0800 Subject: [PATCH 1/3] [BOLT] Incorporate umask into the output file permission Fix https://github.com/llvm/llvm-project/issues/65061 --- bolt/lib/Rewrite/MachORewriteInstance.cpp | 6 ++++-- bolt/lib/Rewrite/RewriteInstance.cpp | 5 ++++- bolt/test/permission.test | 13 +++++++++++++ 3 files changed, 21 insertions(+), 3 deletions(-) create mode 100644 bolt/test/permission.test diff --git a/bolt/lib/Rewrite/MachORewriteInstance.cpp b/bolt/lib/Rewrite/MachORewriteInstance.cpp index de890eb17d0c..0181c386fb7a 100644 --- a/bolt/lib/Rewrite/MachORewriteInstance.cpp +++ b/bolt/lib/Rewrite/MachORewriteInstance.cpp @@ -598,8 +598,10 @@ void MachORewriteInstance::rewriteFile() { writeInstrumentationSection("I__literal16", OS); Out->keep(); - EC = sys::fs::setPermissions(opts::OutputFilename, - sys::fs::perms::all_all); + EC = sys::fs::setPermissions( + opts::OutputFilename, + static_cast(sys::fs::perms::all_all & + ~sys::fs::getUmask())); check_error(EC, "cannot set permissions of output file"); } diff --git a/bolt/lib/Rewrite/RewriteInstance.cpp b/bolt/lib/Rewrite/RewriteInstance.cpp index fd21724c5a35..10189e1a476d 100644 --- a/bolt/lib/Rewrite/RewriteInstance.cpp +++ b/bolt/lib/Rewrite/RewriteInstance.cpp @@ -5413,7 +5413,10 @@ void RewriteInstance::rewriteFile() { } Out->keep(); - EC = sys::fs::setPermissions(opts::OutputFilename, sys::fs::perms::all_all); + EC = sys::fs::setPermissions( + opts::OutputFilename, + static_cast(sys::fs::perms::all_all & + ~sys::fs::getUmask())); check_error(EC, "cannot set permissions of output file"); } diff --git a/bolt/test/permission.test b/bolt/test/permission.test new file mode 100644 index 000000000000..a5a98599eb83 --- /dev/null +++ b/bolt/test/permission.test @@ -0,0 +1,13 @@ +# Ensure that the permissions of the optimized binary file comply with the +# system's umask. + +# This test performs a logical AND operation on the results of the `stat -c %a +# %t.bolt` and `umask` commands (both results are displayed in octal), and +# checks whether the result is equal to 0. +REQUIRES: system-linux + +RUN: %clang %cflags %p/Inputs/hello.c -o %t -Wl,-q +RUN: llvm-bolt %t -o %t.bolt +RUN: echo $(( 8#$(stat -c %a %t.bolt) & 8#$(umask) )) | FileCheck %s + +CHECK: 0 -- Gitee From a5d455e54963f088365831396a23bb7b17a9b099 Mon Sep 17 00:00:00 2001 From: Kepontry Date: Sat, 16 Sep 2023 11:32:52 +0800 Subject: [PATCH 2/3] [BOLT] Skip the validation of CFG after it is finalized When current state is `CFG_Finalized`, function `validateCFG()` should return true directly. --- bolt/lib/Core/BinaryFunction.cpp | 4 ++++ bolt/test/verify-cfg.test | 8 ++++++++ 2 files changed, 12 insertions(+) create mode 100644 bolt/test/verify-cfg.test diff --git a/bolt/lib/Core/BinaryFunction.cpp b/bolt/lib/Core/BinaryFunction.cpp index b571b320f544..ed0d20b2aa4e 100644 --- a/bolt/lib/Core/BinaryFunction.cpp +++ b/bolt/lib/Core/BinaryFunction.cpp @@ -3136,6 +3136,10 @@ void BinaryFunction::dumpGraphToFile(std::string Filename) const { } bool BinaryFunction::validateCFG() const { + // Skip the validation of CFG after it is finalized + if (CurrentState == State::CFG_Finalized) + return true; + bool Valid = true; for (BinaryBasicBlock *BB : BasicBlocks) Valid &= BB->validateSuccessorInvariants(); diff --git a/bolt/test/verify-cfg.test b/bolt/test/verify-cfg.test new file mode 100644 index 000000000000..4a7de85cd427 --- /dev/null +++ b/bolt/test/verify-cfg.test @@ -0,0 +1,8 @@ +# Verify if the `--verify-cfg` option might produce incorrect alerts. + +REQUIRES: system-linux + +RUN: %clang %cflags %p/Inputs/hello.c -o %t -Wl,-q +RUN: llvm-bolt %t -o %t.bolt --verify-cfg 2>&1 | FileCheck %s + +CHECK-NOT: BOLT-ERROR: Invalid CFG detected after pass {{.*}} -- Gitee From 46d30b44813d4d831a948df663474e78c6c412c6 Mon Sep 17 00:00:00 2001 From: Kepontry Date: Sun, 24 Sep 2023 22:44:41 +0800 Subject: [PATCH 3/3] [BOLT] Implement '--assume-abi' option for AArch64 This patch implements the `getCalleeSavedRegs` function for AArch64, addressing the issue where the "not implemented" error occurs when both the `--assume-abi` option and options related to the RegAnalysis Pass (e.g., `--indirect-call-promotion=all`) are enabled. --- bolt/lib/Target/AArch64/AArch64MCPlusBuilder.cpp | 16 ++++++++++++++++ bolt/test/assume-abi.test | 7 +++++++ 2 files changed, 23 insertions(+) create mode 100644 bolt/test/assume-abi.test diff --git a/bolt/lib/Target/AArch64/AArch64MCPlusBuilder.cpp b/bolt/lib/Target/AArch64/AArch64MCPlusBuilder.cpp index e9b494dc3b31..6ba22909b40e 100644 --- a/bolt/lib/Target/AArch64/AArch64MCPlusBuilder.cpp +++ b/bolt/lib/Target/AArch64/AArch64MCPlusBuilder.cpp @@ -310,6 +310,22 @@ public: return true; } + void getCalleeSavedRegs(BitVector &Regs) const override { + Regs |= getAliases(AArch64::X18); + Regs |= getAliases(AArch64::X19); + Regs |= getAliases(AArch64::X20); + Regs |= getAliases(AArch64::X21); + Regs |= getAliases(AArch64::X22); + Regs |= getAliases(AArch64::X23); + Regs |= getAliases(AArch64::X24); + Regs |= getAliases(AArch64::X25); + Regs |= getAliases(AArch64::X26); + Regs |= getAliases(AArch64::X27); + Regs |= getAliases(AArch64::X28); + Regs |= getAliases(AArch64::LR); + Regs |= getAliases(AArch64::FP); + } + const MCExpr *getTargetExprFor(MCInst &Inst, const MCExpr *Expr, MCContext &Ctx, uint64_t RelType) const override { diff --git a/bolt/test/assume-abi.test b/bolt/test/assume-abi.test new file mode 100644 index 000000000000..688ab011441d --- /dev/null +++ b/bolt/test/assume-abi.test @@ -0,0 +1,7 @@ +# Validate the usage of the `--assume-abi` option in conjunction with +# options related to the RegAnalysis Pass. + +REQUIRES: system-linux + +RUN: %clang %cflags %p/Inputs/hello.c -o %t -Wl,-q +RUN: llvm-bolt %t -o %t.bolt --assume-abi --indirect-call-promotion=all -- Gitee