[llvm] [llvm-exegesis][AArch64] Check for PAC keys before disabling them (PR #138643)
Abhilash Majumder via llvm-commits
llvm-commits at lists.llvm.org
Wed Jun 11 03:28:53 PDT 2025
https://github.com/abhilash1910 updated https://github.com/llvm/llvm-project/pull/138643
>From a88910dccb1d13d5945d5d4ca09135c268461205 Mon Sep 17 00:00:00 2001
From: abmajumder <abmajumder at nvidia.com>
Date: Tue, 6 May 2025 10:35:24 +0530
Subject: [PATCH 01/22] fix pac/pag
---
.../llvm-exegesis/lib/AArch64/Target.cpp | 24 +++++++++++++------
1 file changed, 17 insertions(+), 7 deletions(-)
diff --git a/llvm/tools/llvm-exegesis/lib/AArch64/Target.cpp b/llvm/tools/llvm-exegesis/lib/AArch64/Target.cpp
index a1eb5a46f21fc..d1da6f41461a7 100644
--- a/llvm/tools/llvm-exegesis/lib/AArch64/Target.cpp
+++ b/llvm/tools/llvm-exegesis/lib/AArch64/Target.cpp
@@ -207,16 +207,26 @@ class ExegesisAArch64Target : public ExegesisTarget {
if (isPointerAuth(Opcode)) {
#if defined(__aarch64__) && defined(__linux__)
+
+ // Fix for some systems where PAC/PAG keys are present but is
+ // explicitly getting disabled
+ unsigned long pac_keys = 0;
+ if (prctl(PR_PAC_GET_ENABLED_KEYS, &pac_keys, 0, 0, 0) < 0) {
+ return "Failed to get PAC key status";
+ }
+
// Disable all PAC keys. Note that while we expect the measurements to
// be the same with PAC keys disabled, they could potentially be lower
// since authentication checks are bypassed.
- if (prctl(PR_PAC_SET_ENABLED_KEYS,
- PR_PAC_APIAKEY | PR_PAC_APIBKEY | PR_PAC_APDAKEY |
- PR_PAC_APDBKEY, // all keys
- 0, // disable all
- 0, 0) < 0) {
- return "Failed to disable PAC keys";
- }
+ if (pac_keys != 0) {
+ if (prctl(PR_PAC_SET_ENABLED_KEYS,
+ PR_PAC_APIAKEY | PR_PAC_APIBKEY | PR_PAC_APDAKEY |
+ PR_PAC_APDBKEY, // all keys
+ 0, // disable all
+ 0, 0) < 0) {
+ return "Failed to disable PAC keys";
+ }
+ }
#else
return "Unsupported opcode: isPointerAuth";
#endif
>From 009d3ba11c4e19da9c0bede0a0979d2f7f03611a Mon Sep 17 00:00:00 2001
From: abmajumder <abmajumder at nvidia.com>
Date: Tue, 6 May 2025 16:12:57 +0530
Subject: [PATCH 02/22] review comments
---
llvm/tools/llvm-exegesis/lib/AArch64/Target.cpp | 13 ++++++++-----
1 file changed, 8 insertions(+), 5 deletions(-)
diff --git a/llvm/tools/llvm-exegesis/lib/AArch64/Target.cpp b/llvm/tools/llvm-exegesis/lib/AArch64/Target.cpp
index d1da6f41461a7..33082621c11e4 100644
--- a/llvm/tools/llvm-exegesis/lib/AArch64/Target.cpp
+++ b/llvm/tools/llvm-exegesis/lib/AArch64/Target.cpp
@@ -207,14 +207,17 @@ class ExegesisAArch64Target : public ExegesisTarget {
if (isPointerAuth(Opcode)) {
#if defined(__aarch64__) && defined(__linux__)
-
- // Fix for some systems where PAC/PAG keys are present but is
- // explicitly getting disabled
+
+ // For some systems with existing PAC keys set, it is better to
+ // check the existing state of the key before setting it.
+ // For systems without PAC, this is a No-op but with PAC, it is
+ // safer to check the existing key state and then disable/enable them.
+ // Hence the guard placed for switching.
unsigned long pac_keys = 0;
if (prctl(PR_PAC_GET_ENABLED_KEYS, &pac_keys, 0, 0, 0) < 0) {
return "Failed to get PAC key status";
}
-
+
// Disable all PAC keys. Note that while we expect the measurements to
// be the same with PAC keys disabled, they could potentially be lower
// since authentication checks are bypassed.
@@ -226,7 +229,7 @@ class ExegesisAArch64Target : public ExegesisTarget {
0, 0) < 0) {
return "Failed to disable PAC keys";
}
- }
+ }
#else
return "Unsupported opcode: isPointerAuth";
#endif
>From 2c80855603699c6b37bcf6eab1cab98d7ec241e1 Mon Sep 17 00:00:00 2001
From: abmajumder <abmajumder at nvidia.com>
Date: Wed, 7 May 2025 17:12:59 +0530
Subject: [PATCH 03/22] review comments
---
llvm/tools/llvm-exegesis/lib/AArch64/Target.cpp | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/llvm/tools/llvm-exegesis/lib/AArch64/Target.cpp b/llvm/tools/llvm-exegesis/lib/AArch64/Target.cpp
index 33082621c11e4..93c13dca31e5c 100644
--- a/llvm/tools/llvm-exegesis/lib/AArch64/Target.cpp
+++ b/llvm/tools/llvm-exegesis/lib/AArch64/Target.cpp
@@ -213,7 +213,7 @@ class ExegesisAArch64Target : public ExegesisTarget {
// For systems without PAC, this is a No-op but with PAC, it is
// safer to check the existing key state and then disable/enable them.
// Hence the guard placed for switching.
- unsigned long pac_keys = 0;
+ unsigned long PacKeys = 0;
if (prctl(PR_PAC_GET_ENABLED_KEYS, &pac_keys, 0, 0, 0) < 0) {
return "Failed to get PAC key status";
}
@@ -221,7 +221,7 @@ class ExegesisAArch64Target : public ExegesisTarget {
// Disable all PAC keys. Note that while we expect the measurements to
// be the same with PAC keys disabled, they could potentially be lower
// since authentication checks are bypassed.
- if (pac_keys != 0) {
+ if (PacKeys != 0) {
if (prctl(PR_PAC_SET_ENABLED_KEYS,
PR_PAC_APIAKEY | PR_PAC_APIBKEY | PR_PAC_APDAKEY |
PR_PAC_APDBKEY, // all keys
>From 2a9af0f62741e43dd3d6f760a47c0a60833a04bd Mon Sep 17 00:00:00 2001
From: abmajumder <abmajumder at nvidia.com>
Date: Wed, 7 May 2025 20:25:25 +0530
Subject: [PATCH 04/22] typo build error
---
llvm/tools/llvm-exegesis/lib/AArch64/Target.cpp | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/llvm/tools/llvm-exegesis/lib/AArch64/Target.cpp b/llvm/tools/llvm-exegesis/lib/AArch64/Target.cpp
index 93c13dca31e5c..7e6382693c3e9 100644
--- a/llvm/tools/llvm-exegesis/lib/AArch64/Target.cpp
+++ b/llvm/tools/llvm-exegesis/lib/AArch64/Target.cpp
@@ -212,9 +212,9 @@ class ExegesisAArch64Target : public ExegesisTarget {
// check the existing state of the key before setting it.
// For systems without PAC, this is a No-op but with PAC, it is
// safer to check the existing key state and then disable/enable them.
- // Hence the guard placed for switching.
+ // Hence the guard for switching.
unsigned long PacKeys = 0;
- if (prctl(PR_PAC_GET_ENABLED_KEYS, &pac_keys, 0, 0, 0) < 0) {
+ if (prctl(PR_PAC_GET_ENABLED_KEYS, &PacKeys, 0, 0, 0) < 0) {
return "Failed to get PAC key status";
}
>From 7dde4a4a1d0f559d093405680587f9652b7e6174 Mon Sep 17 00:00:00 2001
From: abmajumder <debabhi1396 at gmail.com>
Date: Fri, 9 May 2025 14:19:33 +0530
Subject: [PATCH 05/22] add einval condition and reviews
---
.../llvm-exegesis/lib/AArch64/Target.cpp | 32 ++++++++++++++-----
1 file changed, 24 insertions(+), 8 deletions(-)
diff --git a/llvm/tools/llvm-exegesis/lib/AArch64/Target.cpp b/llvm/tools/llvm-exegesis/lib/AArch64/Target.cpp
index 7e6382693c3e9..a432eb57026d2 100644
--- a/llvm/tools/llvm-exegesis/lib/AArch64/Target.cpp
+++ b/llvm/tools/llvm-exegesis/lib/AArch64/Target.cpp
@@ -10,6 +10,7 @@
#include "AArch64RegisterInfo.h"
#if defined(__aarch64__) && defined(__linux__)
+#include <errno.h>
#include <linux/prctl.h> // For PR_PAC_* constants
#include <sys/prctl.h>
#ifndef PR_PAC_SET_ENABLED_KEYS
@@ -199,6 +200,11 @@ class ExegesisAArch64Target : public ExegesisTarget {
PM.add(createAArch64ExpandPseudoPass());
}
+ static long prctl_wrapper(int op, long arg2 = 0, long arg3 = 0, long arg4 = 0,
+ long arg5 = 0) {
+ return prctl(op, arg2, arg3, arg4, arg5);
+ }
+
const char *getIgnoredOpcodeReasonOrNull(const LLVMState &State,
unsigned Opcode) const override {
if (const char *Reason =
@@ -213,20 +219,30 @@ class ExegesisAArch64Target : public ExegesisTarget {
// For systems without PAC, this is a No-op but with PAC, it is
// safer to check the existing key state and then disable/enable them.
// Hence the guard for switching.
- unsigned long PacKeys = 0;
- if (prctl(PR_PAC_GET_ENABLED_KEYS, &PacKeys, 0, 0, 0) < 0) {
+ errno = 0;
+ unsigned long PacKeys = prctl_wrapper(PR_PAC_GET_ENABLED_KEYS,
+ 0, // unused
+ 0, // unused
+ 0, // unused
+ 0); // unused
+ if ((long)PacKeys < 0) {
+ if (errno == EINVAL) {
+ return "PAuth not supported on this system";
+ }
return "Failed to get PAC key status";
}
// Disable all PAC keys. Note that while we expect the measurements to
// be the same with PAC keys disabled, they could potentially be lower
// since authentication checks are bypassed.
- if (PacKeys != 0) {
- if (prctl(PR_PAC_SET_ENABLED_KEYS,
- PR_PAC_APIAKEY | PR_PAC_APIBKEY | PR_PAC_APDAKEY |
- PR_PAC_APDBKEY, // all keys
- 0, // disable all
- 0, 0) < 0) {
+ if ((long)PacKeys != 0) {
+ if (prctl_wrapper(PR_PAC_SET_ENABLED_KEYS,
+ PR_PAC_APIAKEY | PR_PAC_APIBKEY | PR_PAC_APDAKEY |
+ PR_PAC_APDBKEY, // all keys
+ 0, // disable all
+ 0, // unused
+ 0) // unused
+ < 0) {
return "Failed to disable PAC keys";
}
}
>From 079d268eda9d7d6acb000c71812b14f33a1a46f7 Mon Sep 17 00:00:00 2001
From: Abhilash Majumder <30946547+abhilash1910 at users.noreply.github.com>
Date: Mon, 12 May 2025 18:15:47 +0530
Subject: [PATCH 06/22] Update llvm/tools/llvm-exegesis/lib/AArch64/Target.cpp
Co-authored-by: Anatoly Trosinenko <anatoly.trosinenko at gmail.com>
---
llvm/tools/llvm-exegesis/lib/AArch64/Target.cpp | 6 +-----
1 file changed, 1 insertion(+), 5 deletions(-)
diff --git a/llvm/tools/llvm-exegesis/lib/AArch64/Target.cpp b/llvm/tools/llvm-exegesis/lib/AArch64/Target.cpp
index a432eb57026d2..a1583d13fe4ac 100644
--- a/llvm/tools/llvm-exegesis/lib/AArch64/Target.cpp
+++ b/llvm/tools/llvm-exegesis/lib/AArch64/Target.cpp
@@ -220,11 +220,7 @@ class ExegesisAArch64Target : public ExegesisTarget {
// safer to check the existing key state and then disable/enable them.
// Hence the guard for switching.
errno = 0;
- unsigned long PacKeys = prctl_wrapper(PR_PAC_GET_ENABLED_KEYS,
- 0, // unused
- 0, // unused
- 0, // unused
- 0); // unused
+ unsigned long PacKeys = prctl_wrapper(PR_PAC_GET_ENABLED_KEYS);
if ((long)PacKeys < 0) {
if (errno == EINVAL) {
return "PAuth not supported on this system";
>From 635c646010aa773c8e9372181d6e6d87838adf7c Mon Sep 17 00:00:00 2001
From: Abhilash Majumder <30946547+abhilash1910 at users.noreply.github.com>
Date: Mon, 12 May 2025 18:15:57 +0530
Subject: [PATCH 07/22] Update llvm/tools/llvm-exegesis/lib/AArch64/Target.cpp
Co-authored-by: Anatoly Trosinenko <anatoly.trosinenko at gmail.com>
---
llvm/tools/llvm-exegesis/lib/AArch64/Target.cpp | 14 +++++++-------
1 file changed, 7 insertions(+), 7 deletions(-)
diff --git a/llvm/tools/llvm-exegesis/lib/AArch64/Target.cpp b/llvm/tools/llvm-exegesis/lib/AArch64/Target.cpp
index a1583d13fe4ac..2f4c191c11517 100644
--- a/llvm/tools/llvm-exegesis/lib/AArch64/Target.cpp
+++ b/llvm/tools/llvm-exegesis/lib/AArch64/Target.cpp
@@ -232,13 +232,13 @@ class ExegesisAArch64Target : public ExegesisTarget {
// be the same with PAC keys disabled, they could potentially be lower
// since authentication checks are bypassed.
if ((long)PacKeys != 0) {
- if (prctl_wrapper(PR_PAC_SET_ENABLED_KEYS,
- PR_PAC_APIAKEY | PR_PAC_APIBKEY | PR_PAC_APDAKEY |
- PR_PAC_APDBKEY, // all keys
- 0, // disable all
- 0, // unused
- 0) // unused
- < 0) {
+ // Operate on all keys.
+ const long KeysToControl =
+ PR_PAC_APIAKEY | PR_PAC_APIBKEY | PR_PAC_APDAKEY | PR_PAC_APDBKEY;
+ // Disable all.
+ const long EnabledBitMask = 0;
+ if (prctl_wrapper(PR_PAC_SET_ENABLED_KEYS, KeysToControl,
+ EnabledBitMask) < 0) {
return "Failed to disable PAC keys";
}
}
>From 7ca6ee78aeb00c84c25158062ceb2593631fb1d3 Mon Sep 17 00:00:00 2001
From: Abhilash Majumder <abmajumder at nvidia.com>
Date: Mon, 12 May 2025 18:18:07 +0530
Subject: [PATCH 08/22] review updates
---
llvm/tools/llvm-exegesis/lib/AArch64/Target.cpp | 7 ++++---
1 file changed, 4 insertions(+), 3 deletions(-)
diff --git a/llvm/tools/llvm-exegesis/lib/AArch64/Target.cpp b/llvm/tools/llvm-exegesis/lib/AArch64/Target.cpp
index 2f4c191c11517..2fe3ddf12c08a 100644
--- a/llvm/tools/llvm-exegesis/lib/AArch64/Target.cpp
+++ b/llvm/tools/llvm-exegesis/lib/AArch64/Target.cpp
@@ -200,9 +200,10 @@ class ExegesisAArch64Target : public ExegesisTarget {
PM.add(createAArch64ExpandPseudoPass());
}
- static long prctl_wrapper(int op, long arg2 = 0, long arg3 = 0, long arg4 = 0,
- long arg5 = 0) {
- return prctl(op, arg2, arg3, arg4, arg5);
+ // Converts variadic arguments to `long` and passes zeros for the unused
+ // arg2-arg5, as tested by the Linux kernel.
+ static long prctl_wrapper(int op, long arg2 = 0, long arg3 = 0) {
+ return prctl(op, arg2, arg3, /*arg4=*/0L, /*arg5=*/0L);
}
const char *getIgnoredOpcodeReasonOrNull(const LLVMState &State,
>From e1b6523da8d07025c1da1e42069884ac8a703bce Mon Sep 17 00:00:00 2001
From: Abhilash Majumder <abmajumder at nvidia.com>
Date: Mon, 12 May 2025 18:19:58 +0530
Subject: [PATCH 09/22] format fix
---
llvm/tools/llvm-exegesis/lib/AArch64/Target.cpp | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/llvm/tools/llvm-exegesis/lib/AArch64/Target.cpp b/llvm/tools/llvm-exegesis/lib/AArch64/Target.cpp
index 2fe3ddf12c08a..bc4faedd0b806 100644
--- a/llvm/tools/llvm-exegesis/lib/AArch64/Target.cpp
+++ b/llvm/tools/llvm-exegesis/lib/AArch64/Target.cpp
@@ -234,11 +234,11 @@ class ExegesisAArch64Target : public ExegesisTarget {
// since authentication checks are bypassed.
if ((long)PacKeys != 0) {
// Operate on all keys.
- const long KeysToControl =
+ const long KeysToControl =
PR_PAC_APIAKEY | PR_PAC_APIBKEY | PR_PAC_APDAKEY | PR_PAC_APDBKEY;
// Disable all.
const long EnabledBitMask = 0;
- if (prctl_wrapper(PR_PAC_SET_ENABLED_KEYS, KeysToControl,
+ if (prctl_wrapper(PR_PAC_SET_ENABLED_KEYS, KeysToControl,
EnabledBitMask) < 0) {
return "Failed to disable PAC keys";
}
>From fd254b057fb34686d1f425592d57fe70a894fb44 Mon Sep 17 00:00:00 2001
From: Abhilash Majumder <abmajumder at nvidia.com>
Date: Mon, 12 May 2025 18:22:20 +0530
Subject: [PATCH 10/22] format fix
---
llvm/tools/llvm-exegesis/lib/AArch64/Target.cpp | 1 -
1 file changed, 1 deletion(-)
diff --git a/llvm/tools/llvm-exegesis/lib/AArch64/Target.cpp b/llvm/tools/llvm-exegesis/lib/AArch64/Target.cpp
index bc4faedd0b806..feb798c8ea8d8 100644
--- a/llvm/tools/llvm-exegesis/lib/AArch64/Target.cpp
+++ b/llvm/tools/llvm-exegesis/lib/AArch64/Target.cpp
@@ -214,7 +214,6 @@ class ExegesisAArch64Target : public ExegesisTarget {
if (isPointerAuth(Opcode)) {
#if defined(__aarch64__) && defined(__linux__)
-
// For some systems with existing PAC keys set, it is better to
// check the existing state of the key before setting it.
// For systems without PAC, this is a No-op but with PAC, it is
>From 42075c69605df3aaff814ce1f9ee7c3894e1c1ac Mon Sep 17 00:00:00 2001
From: Abhilash Majumder <abmajumder at nvidia.com>
Date: Tue, 13 May 2025 21:07:24 +0530
Subject: [PATCH 11/22] use nullptr as silentcondition
---
llvm/tools/llvm-exegesis/lib/AArch64/Target.cpp | 12 ++++++++----
1 file changed, 8 insertions(+), 4 deletions(-)
diff --git a/llvm/tools/llvm-exegesis/lib/AArch64/Target.cpp b/llvm/tools/llvm-exegesis/lib/AArch64/Target.cpp
index feb798c8ea8d8..f9396cfdaaca7 100644
--- a/llvm/tools/llvm-exegesis/lib/AArch64/Target.cpp
+++ b/llvm/tools/llvm-exegesis/lib/AArch64/Target.cpp
@@ -200,11 +200,13 @@ class ExegesisAArch64Target : public ExegesisTarget {
PM.add(createAArch64ExpandPseudoPass());
}
+#if defined(__aarch64__) && defined(__linux__)
// Converts variadic arguments to `long` and passes zeros for the unused
// arg2-arg5, as tested by the Linux kernel.
static long prctl_wrapper(int op, long arg2 = 0, long arg3 = 0) {
return prctl(op, arg2, arg3, /*arg4=*/0L, /*arg5=*/0L);
}
+#endif
const char *getIgnoredOpcodeReasonOrNull(const LLVMState &State,
unsigned Opcode) const override {
@@ -222,9 +224,8 @@ class ExegesisAArch64Target : public ExegesisTarget {
errno = 0;
unsigned long PacKeys = prctl_wrapper(PR_PAC_GET_ENABLED_KEYS);
if ((long)PacKeys < 0) {
- if (errno == EINVAL) {
- return "PAuth not supported on this system";
- }
+ if (errno == EINVAL)
+ return nullptr;
return "Failed to get PAC key status";
}
@@ -241,9 +242,12 @@ class ExegesisAArch64Target : public ExegesisTarget {
EnabledBitMask) < 0) {
return "Failed to disable PAC keys";
}
+ llvm::errs() << "llvm-exegesis: PAC keys were disabled at runtime for "
+ "benchmarking.\n";
}
#else
- return "Unsupported opcode: isPointerAuth";
+ // Silently return nullptr to ensure forward progress
+ return nullptr;
#endif
}
>From 2b5c69ae09679bf0c7dc17f7876ead21a5a224da Mon Sep 17 00:00:00 2001
From: Abhilash Majumder <abmajumder at nvidia.com>
Date: Tue, 13 May 2025 21:40:41 +0530
Subject: [PATCH 12/22] use nullptr as silentcondition
---
llvm/tools/llvm-exegesis/lib/AArch64/Target.cpp | 7 ++-----
1 file changed, 2 insertions(+), 5 deletions(-)
diff --git a/llvm/tools/llvm-exegesis/lib/AArch64/Target.cpp b/llvm/tools/llvm-exegesis/lib/AArch64/Target.cpp
index f9396cfdaaca7..96e0c9ec23a9d 100644
--- a/llvm/tools/llvm-exegesis/lib/AArch64/Target.cpp
+++ b/llvm/tools/llvm-exegesis/lib/AArch64/Target.cpp
@@ -223,11 +223,8 @@ class ExegesisAArch64Target : public ExegesisTarget {
// Hence the guard for switching.
errno = 0;
unsigned long PacKeys = prctl_wrapper(PR_PAC_GET_ENABLED_KEYS);
- if ((long)PacKeys < 0) {
- if (errno == EINVAL)
- return nullptr;
- return "Failed to get PAC key status";
- }
+ if ((long)PacKeys < 0 || errno == EINVAL)
+ return nullptr;
// Disable all PAC keys. Note that while we expect the measurements to
// be the same with PAC keys disabled, they could potentially be lower
>From 1be4386af02634f52267cc07a75562a23630aaa9 Mon Sep 17 00:00:00 2001
From: Abhilash Majumder <abmajumder at nvidia.com>
Date: Thu, 15 May 2025 23:16:02 +0530
Subject: [PATCH 13/22] update patch
---
llvm/tools/llvm-exegesis/lib/AArch64/Target.cpp | 9 ++++++---
1 file changed, 6 insertions(+), 3 deletions(-)
diff --git a/llvm/tools/llvm-exegesis/lib/AArch64/Target.cpp b/llvm/tools/llvm-exegesis/lib/AArch64/Target.cpp
index 96e0c9ec23a9d..61822ab42fdd9 100644
--- a/llvm/tools/llvm-exegesis/lib/AArch64/Target.cpp
+++ b/llvm/tools/llvm-exegesis/lib/AArch64/Target.cpp
@@ -219,17 +219,20 @@ class ExegesisAArch64Target : public ExegesisTarget {
// For some systems with existing PAC keys set, it is better to
// check the existing state of the key before setting it.
// For systems without PAC, this is a No-op but with PAC, it is
- // safer to check the existing key state and then disable/enable them.
+ // better to check the existing key state and then disable/enable them
+ // to avoid runtime crashes owing to unsupported prctl opcodes or if the
+ // CPU implements FEAT_PAuth with FEAT_FPAC (in which case this method
+ // would silently return.).
// Hence the guard for switching.
errno = 0;
unsigned long PacKeys = prctl_wrapper(PR_PAC_GET_ENABLED_KEYS);
- if ((long)PacKeys < 0 || errno == EINVAL)
+ if (static_cast<long> PacKeys < 0 || errno == EINVAL)
return nullptr;
// Disable all PAC keys. Note that while we expect the measurements to
// be the same with PAC keys disabled, they could potentially be lower
// since authentication checks are bypassed.
- if ((long)PacKeys != 0) {
+ if (static_cast<long> PacKeys != 0) {
// Operate on all keys.
const long KeysToControl =
PR_PAC_APIAKEY | PR_PAC_APIBKEY | PR_PAC_APDAKEY | PR_PAC_APDBKEY;
>From 6613b7ac165b1f7e16f29ebc6af0e52047c5e032 Mon Sep 17 00:00:00 2001
From: Abhilash Majumder <abmajumder at nvidia.com>
Date: Fri, 23 May 2025 18:55:00 +0530
Subject: [PATCH 14/22] refine
---
.../llvm-exegesis/lib/AArch64/Target.cpp | 64 +++++++++++--------
llvm/tools/llvm-exegesis/llvm-exegesis.cpp | 12 ++--
2 files changed, 46 insertions(+), 30 deletions(-)
diff --git a/llvm/tools/llvm-exegesis/lib/AArch64/Target.cpp b/llvm/tools/llvm-exegesis/lib/AArch64/Target.cpp
index 61822ab42fdd9..b043a09b1d77d 100644
--- a/llvm/tools/llvm-exegesis/lib/AArch64/Target.cpp
+++ b/llvm/tools/llvm-exegesis/lib/AArch64/Target.cpp
@@ -216,34 +216,46 @@ class ExegesisAArch64Target : public ExegesisTarget {
if (isPointerAuth(Opcode)) {
#if defined(__aarch64__) && defined(__linux__)
- // For some systems with existing PAC keys set, it is better to
- // check the existing state of the key before setting it.
- // For systems without PAC, this is a No-op but with PAC, it is
- // better to check the existing key state and then disable/enable them
- // to avoid runtime crashes owing to unsupported prctl opcodes or if the
- // CPU implements FEAT_PAuth with FEAT_FPAC (in which case this method
- // would silently return.).
- // Hence the guard for switching.
- errno = 0;
- unsigned long PacKeys = prctl_wrapper(PR_PAC_GET_ENABLED_KEYS);
- if (static_cast<long> PacKeys < 0 || errno == EINVAL)
- return nullptr;
+ // Only proceed with PAC key control if explicitly requested
+ if (!AArch64DisablePacControl) {
+ // For some systems with existing PAC keys set, it is better to
+ // check the existing state of the key before setting it.
+ // For systems without PAC, this is a No-op but with PAC, it is
+ // better to check the existing key state and then disable/enable them
+ // to avoid runtime crashes owing to unsupported prctl opcodes or if the
+ // CPU implements FEAT_PAuth with FEAT_FPAC (in which case this method
+ // would silently return.). If the CPU implements FEAT_FPAC,
+ // authentication instructions almost certainly crash when being
+ // benchmarked, so disable all the keys by default. On the other hand,
+ // disabling the keys at run-time can probably crash llvm-exegesis at
+ // some later point, depending on how it was built. For that reason, the
+ // user may pass
+ // --COMMAND-LINE-OPTION-NAME in case llvm-exegesis crashes or
+ // instruction timings are affected. Hence the guard for switching.
+ errno = 0;
+ long PacKeys = prctl_wrapper(PR_PAC_GET_ENABLED_KEYS);
+ if (PacKeys < 0 || errno == EINVAL)
+ return nullptr;
- // Disable all PAC keys. Note that while we expect the measurements to
- // be the same with PAC keys disabled, they could potentially be lower
- // since authentication checks are bypassed.
- if (static_cast<long> PacKeys != 0) {
- // Operate on all keys.
- const long KeysToControl =
- PR_PAC_APIAKEY | PR_PAC_APIBKEY | PR_PAC_APDAKEY | PR_PAC_APDBKEY;
- // Disable all.
- const long EnabledBitMask = 0;
- if (prctl_wrapper(PR_PAC_SET_ENABLED_KEYS, KeysToControl,
- EnabledBitMask) < 0) {
- return "Failed to disable PAC keys";
+ // Disable all PAC keys. Note that while we expect the measurements to
+ // be the same with PAC keys disabled, they could potentially be lower
+ // since authentication checks are bypassed.PR_PAC_* prctl operations
+ // return EINVAL when Pointer Authentication is not available, but no
+ // more errors are expected if we got here.
+ if (PacKeys != 0) {
+ // Operate on all keys.
+ const long KeysToControl =
+ PR_PAC_APIAKEY | PR_PAC_APIBKEY | PR_PAC_APDAKEY | PR_PAC_APDBKEY;
+ // Disable all.
+ const long EnabledBitMask = 0;
+ if (prctl_wrapper(PR_PAC_SET_ENABLED_KEYS, KeysToControl,
+ EnabledBitMask) < 0) {
+ return "Failed to disable PAC keys";
+ }
+ llvm::errs()
+ << "llvm-exegesis: PAC keys were disabled at runtime for "
+ "benchmarking.\n";
}
- llvm::errs() << "llvm-exegesis: PAC keys were disabled at runtime for "
- "benchmarking.\n";
}
#else
// Silently return nullptr to ensure forward progress
diff --git a/llvm/tools/llvm-exegesis/llvm-exegesis.cpp b/llvm/tools/llvm-exegesis/llvm-exegesis.cpp
index babcffeb9666a..2454d78853f30 100644
--- a/llvm/tools/llvm-exegesis/llvm-exegesis.cpp
+++ b/llvm/tools/llvm-exegesis/llvm-exegesis.cpp
@@ -107,6 +107,12 @@ static cl::opt<Benchmark::RepetitionModeE> RepetitionMode(
"Middle half loop mode")),
cl::init(Benchmark::Duplicate));
+static cl::opt<bool> AArch64DisablePacControl(
+ "aarch64-disable-pac-control",
+ cl::desc("Disable PAC key control at runtime for benchmarking. Use this if "
+ "llvm-exegesis crashes or instruction timings are affected."),
+ cl::init(false));
+
static cl::opt<bool> BenchmarkMeasurementsPrintProgress(
"measurements-print-progress",
cl::desc("Produce progress indicator when performing measurements"),
@@ -444,8 +450,7 @@ static void runBenchmarkConfigurations(
Benchmark &Result = AllResults.front();
// If any of our measurements failed, pretend they all have failed.
- if (AllResults.size() > 1 &&
- any_of(AllResults, [](const Benchmark &R) {
+ if (AllResults.size() > 1 && any_of(AllResults, [](const Benchmark &R) {
return R.Measurements.empty();
}))
Result.Measurements.clear();
@@ -643,8 +648,7 @@ static void analysisMain() {
errorOrToExpected(MemoryBuffer::getFile(BenchmarkFile, /*IsText=*/true)));
const auto TriplesAndCpus = ExitOnFileError(
- BenchmarkFile,
- Benchmark::readTriplesAndCpusFromYamls(*MemoryBuffer));
+ BenchmarkFile, Benchmark::readTriplesAndCpusFromYamls(*MemoryBuffer));
if (TriplesAndCpus.empty()) {
errs() << "no benchmarks to analyze\n";
return;
>From 977612dd173fe99b75fd8f66ab758b7b8f3bfc54 Mon Sep 17 00:00:00 2001
From: Abhilash Majumder <abmajumder at nvidia.com>
Date: Tue, 27 May 2025 16:47:13 +0530
Subject: [PATCH 15/22] fix duplicate includes
---
llvm/tools/llvm-exegesis/lib/AArch64/Target.cpp | 3 +--
1 file changed, 1 insertion(+), 2 deletions(-)
diff --git a/llvm/tools/llvm-exegesis/lib/AArch64/Target.cpp b/llvm/tools/llvm-exegesis/lib/AArch64/Target.cpp
index b043a09b1d77d..e890919da6b6d 100644
--- a/llvm/tools/llvm-exegesis/lib/AArch64/Target.cpp
+++ b/llvm/tools/llvm-exegesis/lib/AArch64/Target.cpp
@@ -11,8 +11,7 @@
#if defined(__aarch64__) && defined(__linux__)
#include <errno.h>
-#include <linux/prctl.h> // For PR_PAC_* constants
-#include <sys/prctl.h>
+#include <sys/prctl.h> // For PR_PAC_* constants
#ifndef PR_PAC_SET_ENABLED_KEYS
#define PR_PAC_SET_ENABLED_KEYS 60
#endif
>From fb876f19dafdb0d9dab4d9f985beb02eefa0f8ff Mon Sep 17 00:00:00 2001
From: Abhilash Majumder <abmajumder at nvidia.com>
Date: Tue, 27 May 2025 16:51:15 +0530
Subject: [PATCH 16/22] clang-format
---
llvm/tools/llvm-exegesis/lib/AArch64/Target.cpp | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/llvm/tools/llvm-exegesis/lib/AArch64/Target.cpp b/llvm/tools/llvm-exegesis/lib/AArch64/Target.cpp
index e890919da6b6d..c16bd4f7930c3 100644
--- a/llvm/tools/llvm-exegesis/lib/AArch64/Target.cpp
+++ b/llvm/tools/llvm-exegesis/lib/AArch64/Target.cpp
@@ -11,7 +11,7 @@
#if defined(__aarch64__) && defined(__linux__)
#include <errno.h>
-#include <sys/prctl.h> // For PR_PAC_* constants
+#include <sys/prctl.h> // For PR_PAC_* constants
#ifndef PR_PAC_SET_ENABLED_KEYS
#define PR_PAC_SET_ENABLED_KEYS 60
#endif
>From 139eeebf44d4cb42c6083bdc322da694e451c61f Mon Sep 17 00:00:00 2001
From: Abhilash Majumder <abmajumder at nvidia.com>
Date: Tue, 3 Jun 2025 11:39:41 +0530
Subject: [PATCH 17/22] refresh
---
.../tools/llvm-exegesis/lib/AArch64/Target.cpp | 18 +++++++++---------
1 file changed, 9 insertions(+), 9 deletions(-)
diff --git a/llvm/tools/llvm-exegesis/lib/AArch64/Target.cpp b/llvm/tools/llvm-exegesis/lib/AArch64/Target.cpp
index c16bd4f7930c3..084299e95511a 100644
--- a/llvm/tools/llvm-exegesis/lib/AArch64/Target.cpp
+++ b/llvm/tools/llvm-exegesis/lib/AArch64/Target.cpp
@@ -8,6 +8,7 @@
#include "../Target.h"
#include "AArch64.h"
#include "AArch64RegisterInfo.h"
+#include "llvm/Support/CommandLine.h"
#if defined(__aarch64__) && defined(__linux__)
#include <errno.h>
@@ -38,6 +39,8 @@
namespace llvm {
namespace exegesis {
+extern llvm::cl::opt<bool> AArch64DisablePacControl;
+
bool isPointerAuth(unsigned Opcode) {
switch (Opcode) {
default:
@@ -219,18 +222,14 @@ class ExegesisAArch64Target : public ExegesisTarget {
if (!AArch64DisablePacControl) {
// For some systems with existing PAC keys set, it is better to
// check the existing state of the key before setting it.
- // For systems without PAC, this is a No-op but with PAC, it is
- // better to check the existing key state and then disable/enable them
- // to avoid runtime crashes owing to unsupported prctl opcodes or if the
- // CPU implements FEAT_PAuth with FEAT_FPAC (in which case this method
- // would silently return.). If the CPU implements FEAT_FPAC,
+ // If the CPU implements FEAT_FPAC,
// authentication instructions almost certainly crash when being
// benchmarked, so disable all the keys by default. On the other hand,
// disabling the keys at run-time can probably crash llvm-exegesis at
// some later point, depending on how it was built. For that reason, the
- // user may pass
- // --COMMAND-LINE-OPTION-NAME in case llvm-exegesis crashes or
- // instruction timings are affected. Hence the guard for switching.
+ // user may pass --aarch64-disable-pac-control in case
+ // llvm-exegesis crashes or instruction timings are affected.
+ // Hence the guard for switching.
errno = 0;
long PacKeys = prctl_wrapper(PR_PAC_GET_ENABLED_KEYS);
if (PacKeys < 0 || errno == EINVAL)
@@ -245,7 +244,8 @@ class ExegesisAArch64Target : public ExegesisTarget {
// Operate on all keys.
const long KeysToControl =
PR_PAC_APIAKEY | PR_PAC_APIBKEY | PR_PAC_APDAKEY | PR_PAC_APDBKEY;
- // Disable all.
+ // PR_PAC_* prctl operations return EINVAL when Pointer Authentication
+ // is not available but no more errors are expected if we got here.
const long EnabledBitMask = 0;
if (prctl_wrapper(PR_PAC_SET_ENABLED_KEYS, KeysToControl,
EnabledBitMask) < 0) {
>From 5c2b583ba656f78fa9ed01006a48f112908a2c42 Mon Sep 17 00:00:00 2001
From: Abhilash Majumder <abmajumder at nvidia.com>
Date: Tue, 3 Jun 2025 22:35:04 +0530
Subject: [PATCH 18/22] refresh
---
llvm/tools/llvm-exegesis/lib/AArch64/Target.cpp | 6 +++++-
llvm/tools/llvm-exegesis/llvm-exegesis.cpp | 14 +++++---------
2 files changed, 10 insertions(+), 10 deletions(-)
diff --git a/llvm/tools/llvm-exegesis/lib/AArch64/Target.cpp b/llvm/tools/llvm-exegesis/lib/AArch64/Target.cpp
index 084299e95511a..9297711d53612 100644
--- a/llvm/tools/llvm-exegesis/lib/AArch64/Target.cpp
+++ b/llvm/tools/llvm-exegesis/lib/AArch64/Target.cpp
@@ -39,7 +39,11 @@
namespace llvm {
namespace exegesis {
-extern llvm::cl::opt<bool> AArch64DisablePacControl;
+static cl::opt<bool> AArch64DisablePacControl(
+ "aarch64-disable-pac-control",
+ cl::desc("Disable PAC key control at runtime for benchmarking. Use this if "
+ "llvm-exegesis crashes or instruction timings are affected."),
+ cl::init(false));
bool isPointerAuth(unsigned Opcode) {
switch (Opcode) {
diff --git a/llvm/tools/llvm-exegesis/llvm-exegesis.cpp b/llvm/tools/llvm-exegesis/llvm-exegesis.cpp
index 2454d78853f30..f0b3691b28f37 100644
--- a/llvm/tools/llvm-exegesis/llvm-exegesis.cpp
+++ b/llvm/tools/llvm-exegesis/llvm-exegesis.cpp
@@ -107,12 +107,6 @@ static cl::opt<Benchmark::RepetitionModeE> RepetitionMode(
"Middle half loop mode")),
cl::init(Benchmark::Duplicate));
-static cl::opt<bool> AArch64DisablePacControl(
- "aarch64-disable-pac-control",
- cl::desc("Disable PAC key control at runtime for benchmarking. Use this if "
- "llvm-exegesis crashes or instruction timings are affected."),
- cl::init(false));
-
static cl::opt<bool> BenchmarkMeasurementsPrintProgress(
"measurements-print-progress",
cl::desc("Produce progress indicator when performing measurements"),
@@ -450,7 +444,8 @@ static void runBenchmarkConfigurations(
Benchmark &Result = AllResults.front();
// If any of our measurements failed, pretend they all have failed.
- if (AllResults.size() > 1 && any_of(AllResults, [](const Benchmark &R) {
+ if (AllResults.size() > 1 &&
+ any_of(AllResults, [](const Benchmark &R) {
return R.Measurements.empty();
}))
Result.Measurements.clear();
@@ -648,7 +643,8 @@ static void analysisMain() {
errorOrToExpected(MemoryBuffer::getFile(BenchmarkFile, /*IsText=*/true)));
const auto TriplesAndCpus = ExitOnFileError(
- BenchmarkFile, Benchmark::readTriplesAndCpusFromYamls(*MemoryBuffer));
+ BenchmarkFile,
+ Benchmark::readTriplesAndCpusFromYamls(*MemoryBuffer));
if (TriplesAndCpus.empty()) {
errs() << "no benchmarks to analyze\n";
return;
@@ -738,4 +734,4 @@ int main(int Argc, char **Argv) {
exegesis::benchmarkMain();
}
return EXIT_SUCCESS;
-}
+}
\ No newline at end of file
>From 652e0f889ecb698fd1a9a0905f10cdbc1a1d69a9 Mon Sep 17 00:00:00 2001
From: Abhilash Majumder <abmajumder at nvidia.com>
Date: Tue, 3 Jun 2025 22:37:36 +0530
Subject: [PATCH 19/22] newline
---
llvm/tools/llvm-exegesis/llvm-exegesis.cpp | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/llvm/tools/llvm-exegesis/llvm-exegesis.cpp b/llvm/tools/llvm-exegesis/llvm-exegesis.cpp
index f0b3691b28f37..babcffeb9666a 100644
--- a/llvm/tools/llvm-exegesis/llvm-exegesis.cpp
+++ b/llvm/tools/llvm-exegesis/llvm-exegesis.cpp
@@ -734,4 +734,4 @@ int main(int Argc, char **Argv) {
exegesis::benchmarkMain();
}
return EXIT_SUCCESS;
-}
\ No newline at end of file
+}
>From ab57635b3bf60c2cde33fbd50901f45ca5f36b61 Mon Sep 17 00:00:00 2001
From: Abhilash Majumder <30946547+abhilash1910 at users.noreply.github.com>
Date: Wed, 4 Jun 2025 00:14:00 +0530
Subject: [PATCH 20/22] format
---
llvm/tools/llvm-exegesis/lib/AArch64/Target.cpp | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/llvm/tools/llvm-exegesis/lib/AArch64/Target.cpp b/llvm/tools/llvm-exegesis/lib/AArch64/Target.cpp
index 2bd8a7c4750c4..9297711d53612 100644
--- a/llvm/tools/llvm-exegesis/lib/AArch64/Target.cpp
+++ b/llvm/tools/llvm-exegesis/lib/AArch64/Target.cpp
@@ -86,7 +86,7 @@ bool isLoadTagMultiple(unsigned Opcode) {
}
}
- static unsigned getLoadImmediateOpcode(unsigned RegBitWidth) {
+static unsigned getLoadImmediateOpcode(unsigned RegBitWidth) {
switch (RegBitWidth) {
case 32:
return AArch64::MOVi32imm;
>From 5b8011139dfecaf767435b04a9bf38cb2a348164 Mon Sep 17 00:00:00 2001
From: Abhilash Majumder <abmajumder at nvidia.com>
Date: Fri, 6 Jun 2025 17:12:44 +0530
Subject: [PATCH 21/22] refresh
---
llvm/tools/llvm-exegesis/lib/AArch64/Target.cpp | 14 ++++++--------
1 file changed, 6 insertions(+), 8 deletions(-)
diff --git a/llvm/tools/llvm-exegesis/lib/AArch64/Target.cpp b/llvm/tools/llvm-exegesis/lib/AArch64/Target.cpp
index 9297711d53612..aeabc72de5555 100644
--- a/llvm/tools/llvm-exegesis/lib/AArch64/Target.cpp
+++ b/llvm/tools/llvm-exegesis/lib/AArch64/Target.cpp
@@ -40,12 +40,12 @@ namespace llvm {
namespace exegesis {
static cl::opt<bool> AArch64DisablePacControl(
- "aarch64-disable-pac-control",
+ "aarch64-keep-pac-keys",
cl::desc("Disable PAC key control at runtime for benchmarking. Use this if "
"llvm-exegesis crashes or instruction timings are affected."),
cl::init(false));
-bool isPointerAuth(unsigned Opcode) {
+static bool isPointerAuth(unsigned Opcode) {
switch (Opcode) {
default:
return false;
@@ -75,7 +75,7 @@ bool isPointerAuth(unsigned Opcode) {
}
}
-bool isLoadTagMultiple(unsigned Opcode) {
+static bool isLoadTagMultiple(unsigned Opcode) {
switch (Opcode) {
default:
return false;
@@ -231,19 +231,17 @@ class ExegesisAArch64Target : public ExegesisTarget {
// benchmarked, so disable all the keys by default. On the other hand,
// disabling the keys at run-time can probably crash llvm-exegesis at
// some later point, depending on how it was built. For that reason, the
- // user may pass --aarch64-disable-pac-control in case
+ // user may pass --aarch64-keep-pack-keys in case
// llvm-exegesis crashes or instruction timings are affected.
// Hence the guard for switching.
errno = 0;
long PacKeys = prctl_wrapper(PR_PAC_GET_ENABLED_KEYS);
- if (PacKeys < 0 || errno == EINVAL)
+ if (PacKeys < 0 && errno == EINVAL)
return nullptr;
// Disable all PAC keys. Note that while we expect the measurements to
// be the same with PAC keys disabled, they could potentially be lower
- // since authentication checks are bypassed.PR_PAC_* prctl operations
- // return EINVAL when Pointer Authentication is not available, but no
- // more errors are expected if we got here.
+ // since authentication checks are bypassed.
if (PacKeys != 0) {
// Operate on all keys.
const long KeysToControl =
>From 57ca61fbabaaeea0e02a7b6e6096ed08b5ee913c Mon Sep 17 00:00:00 2001
From: Abhilash Majumder <abmajumder at nvidia.com>
Date: Wed, 11 Jun 2025 15:57:53 +0530
Subject: [PATCH 22/22] refresh opcodes
---
.../llvm-exegesis/lib/AArch64/Target.cpp | 35 ++++++++++++-------
1 file changed, 22 insertions(+), 13 deletions(-)
diff --git a/llvm/tools/llvm-exegesis/lib/AArch64/Target.cpp b/llvm/tools/llvm-exegesis/lib/AArch64/Target.cpp
index aeabc72de5555..f8a401ce636fc 100644
--- a/llvm/tools/llvm-exegesis/lib/AArch64/Target.cpp
+++ b/llvm/tools/llvm-exegesis/lib/AArch64/Target.cpp
@@ -54,23 +54,32 @@ static bool isPointerAuth(unsigned Opcode) {
// We would like to measure these instructions, but they can behave
// differently on different platforms, and maybe the snippets need to look
// different for these instructions,
- // Platform-specific handling: On Linux, we disable authentication, may
- // interfere with measurements. On non-Linux platforms, disable opcodes for
- // now.
- case AArch64::AUTDA:
- case AArch64::AUTDB:
- case AArch64::AUTDZA:
- case AArch64::AUTDZB:
- case AArch64::AUTIA:
- case AArch64::AUTIA1716:
- case AArch64::AUTIASP:
+ // Platform-specific handling: On Linux, owing to the fact that disabling
+ // keys can cause exegesis to crash, the user may pass
+ // --aarch64-keep-pack-keys in case we disable authentication to ensure
+ // forward progress. On non-Linux platforms, disable opcodes for now.
case AArch64::AUTIAZ:
- case AArch64::AUTIB:
- case AArch64::AUTIB1716:
- case AArch64::AUTIBSP:
case AArch64::AUTIBZ:
+ case AArch64::AUTIASP:
+ case AArch64::AUTIBSP:
+ case AArch64::AUTIASPPCi:
+ case AArch64::AUTIBSPPCi:
+ case AArch64::AUTIASPPCr:
+ case AArch64::AUTIBSPPCr:
+ case AArch64::AUTIA1716:
+ case AArch64::AUTIB1716:
+ case AArch64::AUTIA171615:
+ case AArch64::AUTIB171615:
+ case AArch64::AUTIA:
+ case AArch64::AUTIB:
+ case AArch64::AUTDA:
+ case AArch64::AUTDB:
case AArch64::AUTIZA:
case AArch64::AUTIZB:
+ case AArch64::AUTDZA:
+ case AArch64::AUTDZB:
+ case AArch64::LDRAAwriteback:
+ case AArch64::LDRABwriteback:
return true;
}
}
More information about the llvm-commits
mailing list