[llvm-branch-commits] [llvm] [BOLT] Improve file handling in NFC-Mode (PR #146513)
Paschalis Mpeis via llvm-branch-commits
llvm-branch-commits at lists.llvm.org
Thu Jul 3 02:55:55 PDT 2025
https://github.com/paschalis-mpeis updated https://github.com/llvm/llvm-project/pull/146513
>From 625f9ee79af68a121afd92e06d9b4f91007a9c38 Mon Sep 17 00:00:00 2001
From: Paschalis Mpeis <Paschalis.Mpeis at arm.com>
Date: Tue, 1 Jul 2025 12:37:31 +0100
Subject: [PATCH 1/4] [BOLT] Improve file handling in NFC-Mode
This patch introduce the following improvements:
- Catch an exception when the CMakeCache.txt is not present
- Bail out gracefully when llvm-bolt did not build successfully the
current or previous revision.
---
bolt/utils/nfc-check-setup.py | 26 +++++++++++++++++++-------
1 file changed, 19 insertions(+), 7 deletions(-)
diff --git a/bolt/utils/nfc-check-setup.py b/bolt/utils/nfc-check-setup.py
index 7d634d7a88b83..2ff27e5c40b63 100755
--- a/bolt/utils/nfc-check-setup.py
+++ b/bolt/utils/nfc-check-setup.py
@@ -91,18 +91,26 @@ def main():
source_dir = None
# find the repo directory
- with open(f"{args.build_dir}/CMakeCache.txt") as f:
- for line in f:
- m = re.match(r"LLVM_SOURCE_DIR:STATIC=(.*)", line)
- if m:
- source_dir = m.groups()[0]
- if not source_dir:
- sys.exit("Source directory is not found")
+ try:
+ CMCacheFilename=f"{args.build_dir}/CMakeCache.txt"
+ with open(CMCacheFilename) as f:
+ for line in f:
+ m = re.match(r"LLVM_SOURCE_DIR:STATIC=(.*)", line)
+ if m:
+ source_dir = m.groups()[0]
+ if not source_dir:
+ raise Exception(f"Source directory not found: '{CMCacheFilename}'")
+ except Exception as e:
+ sys.exit(e)
# build the current commit
subprocess.run(
shlex.split("cmake --build . --target llvm-bolt"), cwd=args.build_dir
)
+
+ if not os.path.exists(bolt_path):
+ sys.exit(f"Failed to build the current revision: '{bolt_path}'")
+
# rename llvm-bolt
os.replace(bolt_path, f"{bolt_path}.new")
# memorize the old hash for logging
@@ -133,11 +141,15 @@ def main():
subprocess.run(shlex.split(f"git checkout -f {args.cmp_rev}"), cwd=source_dir)
# get the parent commit hash for logging
new_ref = get_git_ref_or_rev(source_dir)
+
# build the previous commit
subprocess.run(
shlex.split("cmake --build . --target llvm-bolt"), cwd=args.build_dir
)
+
# rename llvm-bolt
+ if not os.path.exists(bolt_path):
+ sys.exit(f"Failed to build the previous revision: '{bolt_path}'")
os.replace(bolt_path, f"{bolt_path}.old")
# symlink llvm-bolt-wrapper
>From 26e7b9f05f8a365f117f14a0975a232e1ec74202 Mon Sep 17 00:00:00 2001
From: Paschalis Mpeis <Paschalis.Mpeis at arm.com>
Date: Tue, 1 Jul 2025 12:50:08 +0100
Subject: [PATCH 2/4] python formatter and nits
---
bolt/utils/nfc-check-setup.py | 4 +++-
1 file changed, 3 insertions(+), 1 deletion(-)
diff --git a/bolt/utils/nfc-check-setup.py b/bolt/utils/nfc-check-setup.py
index 2ff27e5c40b63..22e8cc646a1c5 100755
--- a/bolt/utils/nfc-check-setup.py
+++ b/bolt/utils/nfc-check-setup.py
@@ -92,7 +92,7 @@ def main():
source_dir = None
# find the repo directory
try:
- CMCacheFilename=f"{args.build_dir}/CMakeCache.txt"
+ CMCacheFilename = f"{args.build_dir}/CMakeCache.txt"
with open(CMCacheFilename) as f:
for line in f:
m = re.match(r"LLVM_SOURCE_DIR:STATIC=(.*)", line)
@@ -104,6 +104,7 @@ def main():
sys.exit(e)
# build the current commit
+ print ("NFC-Setup: Building current revision..")
subprocess.run(
shlex.split("cmake --build . --target llvm-bolt"), cwd=args.build_dir
)
@@ -143,6 +144,7 @@ def main():
new_ref = get_git_ref_or_rev(source_dir)
# build the previous commit
+ print ("NFC-Setup: Building previous revision..")
subprocess.run(
shlex.split("cmake --build . --target llvm-bolt"), cwd=args.build_dir
)
>From ca36aa02effc6c5e5da140940a5c55d4183e0422 Mon Sep 17 00:00:00 2001
From: Paschalis Mpeis <Paschalis.Mpeis at arm.com>
Date: Tue, 1 Jul 2025 12:55:46 +0100
Subject: [PATCH 3/4] code formatter (2)
---
bolt/utils/nfc-check-setup.py | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/bolt/utils/nfc-check-setup.py b/bolt/utils/nfc-check-setup.py
index 22e8cc646a1c5..d3248050f16e3 100755
--- a/bolt/utils/nfc-check-setup.py
+++ b/bolt/utils/nfc-check-setup.py
@@ -104,7 +104,7 @@ def main():
sys.exit(e)
# build the current commit
- print ("NFC-Setup: Building current revision..")
+ print("NFC-Setup: Building current revision..")
subprocess.run(
shlex.split("cmake --build . --target llvm-bolt"), cwd=args.build_dir
)
@@ -144,7 +144,7 @@ def main():
new_ref = get_git_ref_or_rev(source_dir)
# build the previous commit
- print ("NFC-Setup: Building previous revision..")
+ print("NFC-Setup: Building previous revision..")
subprocess.run(
shlex.split("cmake --build . --target llvm-bolt"), cwd=args.build_dir
)
>From 09363a84ffb653c943c0e6da85f5d50132e90a1e Mon Sep 17 00:00:00 2001
From: Paschalis Mpeis <Paschalis.Mpeis at arm.com>
Date: Tue, 1 Jul 2025 13:22:14 +0100
Subject: [PATCH 4/4] Handling switch back
---
bolt/utils/nfc-check-setup.py | 46 +++++++++++++++++++++++------------
1 file changed, 31 insertions(+), 15 deletions(-)
diff --git a/bolt/utils/nfc-check-setup.py b/bolt/utils/nfc-check-setup.py
index d3248050f16e3..18bf7522de17b 100755
--- a/bolt/utils/nfc-check-setup.py
+++ b/bolt/utils/nfc-check-setup.py
@@ -42,6 +42,23 @@ def get_git_ref_or_rev(dir: str) -> str:
cmd_rev = "git rev-parse --short HEAD"
return subprocess.check_output(shlex.split(cmd_rev), cwd=dir, text=True).strip()
+def switch_back(
+ switch_back: bool, stash: bool, source_dir: str, old_ref: str, new_ref: str
+):
+ # Switch back to the current revision if needed and inform the user of where
+ # the HEAD is. Must be called after checking out the previous commit on all
+ # exit paths.
+ if switch_back:
+ print("Switching back to current revision..")
+ if stash:
+ subprocess.run(shlex.split("git stash pop"), cwd=source_dir)
+ subprocess.run(shlex.split(f"git checkout {old_ref}"), cwd=source_dir)
+ else:
+ print(
+ f"The repository {source_dir} has been switched from {old_ref} "
+ f"to {new_ref}. Local changes were stashed. Switch back using\n\t"
+ f"git checkout {old_ref}\n"
+ )
def main():
parser = argparse.ArgumentParser(
@@ -87,10 +104,8 @@ def main():
if not args.create_wrapper and len(wrapper_args) > 0:
parser.parse_args()
- bolt_path = f"{args.build_dir}/bin/llvm-bolt"
-
- source_dir = None
# find the repo directory
+ source_dir = None
try:
CMCacheFilename = f"{args.build_dir}/CMakeCache.txt"
with open(CMCacheFilename) as f:
@@ -103,6 +118,11 @@ def main():
except Exception as e:
sys.exit(e)
+ # clean the previous llvm-bolt if it exists
+ bolt_path = f"{args.build_dir}/bin/llvm-bolt"
+ if os.path.exists(bolt_path):
+ os.remove(bolt_path)
+
# build the current commit
print("NFC-Setup: Building current revision..")
subprocess.run(
@@ -151,7 +171,9 @@ def main():
# rename llvm-bolt
if not os.path.exists(bolt_path):
- sys.exit(f"Failed to build the previous revision: '{bolt_path}'")
+ print(f"Failed to build the previous revision: '{bolt_path}'")
+ switch_back(args.switch_back, stash, source_dir, old_ref, new_ref)
+ sys.exit(1)
os.replace(bolt_path, f"{bolt_path}.old")
# symlink llvm-bolt-wrapper
@@ -170,18 +192,12 @@ def main():
# symlink llvm-bolt-wrapper
os.symlink(wrapper_path, bolt_path)
except Exception as e:
- sys.exit("Failed to create a wrapper:\n" + str(e))
+ print("Failed to create a wrapper:\n" + str(e))
+ switch_back(args.switch_back, stash, source_dir, old_ref, new_ref)
+ sys.exit(1)
+
+ switch_back(args.switch_back, stash, source_dir, old_ref, new_ref)
- if args.switch_back:
- if stash:
- subprocess.run(shlex.split("git stash pop"), cwd=source_dir)
- subprocess.run(shlex.split(f"git checkout {old_ref}"), cwd=source_dir)
- else:
- print(
- f"The repository {source_dir} has been switched from {old_ref} "
- f"to {new_ref}. Local changes were stashed. Switch back using\n\t"
- f"git checkout {old_ref}\n"
- )
print(
f"Build directory {args.build_dir} is ready to run BOLT tests, e.g.\n"
"\tbin/llvm-lit -sv tools/bolt/test\nor\n"
More information about the llvm-branch-commits
mailing list