[llvm] [NFC][AutoUpgrade] Add logging for module flags upgrade (PR #226165)
Andrew Savonichev via llvm-commits
llvm-commits at lists.llvm.org
Thu Sep 24 06:37:46 PDT 2026
https://github.com/asavonic created https://github.com/llvm/llvm-project/pull/226165
Logging is useful to see how a module changes as it goes through auto-upgrade. It also makes it easier to understand tests.
Note that tests that involve logging are only active for LLVM builds with assertions.
For now, only changes in branch protection attributes are logged.
>From a0f64fac3384bbd4e319d212dbf14da229b53d30 Mon Sep 17 00:00:00 2001
From: Andrew Savonichev <andrew.savonichev at gmail.com>
Date: Thu, 24 Sep 2026 22:17:13 +0900
Subject: [PATCH] [NFC][AutoUpgrade] Add logging for module flags upgrade
Logging is useful to see how a module changes as it goes through auto-upgrade.
It also makes it easier to understand tests. Note that tests that involve
logging are only active for LLVM builds with assertions.
For now, only changes in branch protection attributes are logged.
---
llvm/lib/IR/AutoUpgrade.cpp | 47 ++++++++++++++-----
.../test/Bitcode/upgrade-branch-protection.ll | 34 ++++++++++++++
2 files changed, 70 insertions(+), 11 deletions(-)
diff --git a/llvm/lib/IR/AutoUpgrade.cpp b/llvm/lib/IR/AutoUpgrade.cpp
index cb0e0690a261f1..065ea7db17ee79 100644
--- a/llvm/lib/IR/AutoUpgrade.cpp
+++ b/llvm/lib/IR/AutoUpgrade.cpp
@@ -49,6 +49,7 @@
#include "llvm/Support/AMDGPUAddrSpace.h"
#include "llvm/Support/CodeGen.h"
#include "llvm/Support/CommandLine.h"
+#include "llvm/Support/Debug.h"
#include "llvm/Support/ErrorHandling.h"
#include "llvm/Support/NVPTXAddrSpace.h"
#include "llvm/Support/NVVMAttributes.h"
@@ -61,6 +62,8 @@
using namespace llvm;
+#define DEBUG_TYPE "auto-upgrade"
+
static cl::opt<bool>
DisableAutoUpgradeDebugInfo("disable-auto-upgrade-debug-info",
cl::desc("Disable autoupgrade of debug info"));
@@ -7552,8 +7555,11 @@ void llvm::UpgradeFunctionAttributes(Function &F) {
// Check if the function attribute is not present and set it.
static void setFunctionAttrIfNotSet(Function &F, StringRef FnAttrName,
StringRef Value) {
- if (!F.hasFnAttribute(FnAttrName))
+ if (!F.hasFnAttribute(FnAttrName)) {
F.addFnAttr(FnAttrName, Value);
+ LLVM_DEBUG(dbgs() << "Set attribute: " << FnAttrName << "=\"" << Value
+ << "\", function: " << F.getName() << "\n");
+ }
}
// Check if the function attribute is not present and set it if needed.
@@ -7561,19 +7567,33 @@ static void setFunctionAttrIfNotSet(Function &F, StringRef FnAttrName,
// If the attribute is "true" resets it to a valueless attribute.
static void ConvertFunctionAttr(Function &F, bool Set, StringRef FnAttrName) {
if (!F.hasFnAttribute(FnAttrName)) {
- if (Set)
+ if (Set) {
F.addFnAttr(FnAttrName);
+ LLVM_DEBUG(dbgs() << "Added attribute: " << FnAttrName
+ << ", function: " << F.getName() << "\n");
+ }
} else {
auto A = F.getFnAttribute(FnAttrName);
- if ("false" == A.getValueAsString())
+ if ("false" == A.getValueAsString()) {
F.removeFnAttr(FnAttrName);
- else if ("true" == A.getValueAsString()) {
+ LLVM_DEBUG(dbgs() << "Removed attribute: " << FnAttrName
+ << "=\"false\", function: " << F.getName() << "\n");
+ } else if ("true" == A.getValueAsString()) {
F.removeFnAttr(FnAttrName);
F.addFnAttr(FnAttrName);
+ LLVM_DEBUG(dbgs() << "Converted attribute: " << FnAttrName
+ << "=\"true\", function: " << F.getName() << "\n");
}
}
}
+static void ConvertModuleFlag(Module &M, Module::ModFlagBehavior Behavior,
+ StringRef Key, uint32_t Val) {
+ M.setModuleFlag(Behavior, Key, Val);
+ LLVM_DEBUG(dbgs() << "Converted module flag: " << "{" << Behavior << ", "
+ << Key << ", " << Val << "}\n");
+}
+
void llvm::copyModuleAttrToFunctions(Module &M) {
Triple T(M.getTargetTriple());
if (!T.isThumb() && !T.isARM() && !T.isAArch64())
@@ -7613,6 +7633,9 @@ void llvm::copyModuleAttrToFunctions(Module &M) {
*ValPtr = CI->getZExtValue();
if (*ValPtr == 2)
return;
+
+ LLVM_DEBUG(dbgs() << "Found module flag: " << IDStr << "(" << *ValPtr
+ << ")\n");
}
}
@@ -7649,17 +7672,19 @@ void llvm::copyModuleAttrToFunctions(Module &M) {
}
if (BTE)
- M.setModuleFlag(llvm::Module::Min, "branch-target-enforcement", 2);
+ ConvertModuleFlag(M, llvm::Module::Min, "branch-target-enforcement", 2);
if (BPPLR)
- M.setModuleFlag(llvm::Module::Min, "branch-protection-pauth-lr", 2);
+ ConvertModuleFlag(M, llvm::Module::Min, "branch-protection-pauth-lr", 2);
if (GCS)
- M.setModuleFlag(llvm::Module::Min, "guarded-control-stack", 2);
+ ConvertModuleFlag(M, llvm::Module::Min, "guarded-control-stack", 2);
if (SRA) {
- M.setModuleFlag(llvm::Module::Min, "sign-return-address", 2);
+ ConvertModuleFlag(M, llvm::Module::Min, "sign-return-address", 2);
if (SRAALLValue == 1)
- M.setModuleFlag(llvm::Module::Min, "sign-return-address-all", 2);
- if (SRABKeyValue == 1)
- M.setModuleFlag(llvm::Module::Min, "sign-return-address-with-bkey", 2);
+ ConvertModuleFlag(M, llvm::Module::Min, "sign-return-address-all", 2);
+ if (SRABKeyValue == 1) {
+ ConvertModuleFlag(M, llvm::Module::Min, "sign-return-address-with-bkey",
+ 2);
+ }
}
}
diff --git a/llvm/test/Bitcode/upgrade-branch-protection.ll b/llvm/test/Bitcode/upgrade-branch-protection.ll
index 6f60ba543e6c04..76fb369e9b09fb 100644
--- a/llvm/test/Bitcode/upgrade-branch-protection.ll
+++ b/llvm/test/Bitcode/upgrade-branch-protection.ll
@@ -3,9 +3,23 @@
;; as the module is converted to the semantic.
; RUN: llvm-as %s -o - | llvm-dis - | FileCheck %s
+; RUN: %if asserts %{ llvm-as %s -debug-only=auto-upgrade -o /dev/null 2>&1 | FileCheck %s --check-prefix CHECK-LOG %}
target triple = "aarch64-unknown-linux-gnu"
+define i32 @foo_on() #0 {
+entry:
+ ret i32 42
+}
+
+define i32 @foo_off() #1 {
+entry:
+ ret i32 43
+}
+
+attributes #0 = { noinline nounwind optnone uwtable "branch-target-enforcement"="true"}
+attributes #1 = { noinline nounwind optnone uwtable "branch-target-enforcement"="false" "sign-return-address"="none" }
+
!llvm.module.flags = !{!0, !1, !2, !3}
!0 = !{i32 1, !"branch-target-enforcement", i32 1}
@@ -13,7 +27,27 @@ target triple = "aarch64-unknown-linux-gnu"
!2 = !{i32 1, !"sign-return-address-all", i32 1}
!3 = !{i32 1, !"sign-return-address-with-bkey", i32 1}
+;CHECK: define{{.+}}@foo_on{{.+}}#[[ATTR_ON:[0-9]+]]
+;CHECK: define{{.+}}@foo_off{{.+}}#[[ATTR_OFF:[0-9]+]]
+
+;CHECK: attributes #[[ATTR_ON]] {{.+}}"branch-target-enforcement" "sign-return-address"="all" "sign-return-address-key"="b_key"
+;CHECK: attributes #[[ATTR_OFF]] {{.+}}"sign-return-address"="none" "sign-return-address-key"="b_key"
+
;CHECK: !0 = !{i32 8, !"branch-target-enforcement", i32 2}
;CHECK: !1 = !{i32 8, !"sign-return-address", i32 2}
;CHECK: !2 = !{i32 8, !"sign-return-address-all", i32 2}
;CHECK: !3 = !{i32 8, !"sign-return-address-with-bkey", i32 2}
+
+;CHECK-LOG: Found module flag: branch-target-enforcement(1)
+;CHECK-LOG: Found module flag: sign-return-address(1)
+;CHECK-LOG: Found module flag: sign-return-address-all(1)
+;CHECK-LOG: Found module flag: sign-return-address-with-bkey(1)
+;CHECK-LOG: Set attribute: sign-return-address="all", function: foo_on
+;CHECK-LOG: Set attribute: sign-return-address-key="b_key", function: foo_on
+;CHECK-LOG: Converted attribute: branch-target-enforcement="true", function: foo_on
+;CHECK-LOG: Set attribute: sign-return-address-key="b_key", function: foo_off
+;CHECK-LOG: Removed attribute: branch-target-enforcement="false", function: foo_off
+;CHECK-LOG: Converted module flag: {8, branch-target-enforcement, 2}
+;CHECK-LOG: Converted module flag: {8, sign-return-address, 2}
+;CHECK-LOG: Converted module flag: {8, sign-return-address-all, 2}
+;CHECK-LOG: Converted module flag: {8, sign-return-address-with-bkey, 2}
More information about the llvm-commits
mailing list