[llvm] 1514c1f - [NFC][AutoUpgrade] Add logging for module flags upgrade (#226165)

via llvm-commits llvm-commits at lists.llvm.org
Thu Oct 8 00:56:42 PDT 2026


Author: Andrew Savonichev
Date: 2026-10-08T16:56:36+09:00
New Revision: 1514c1fd0dc0c787cc6f72ed017cd416a776f040

URL: https://github.com/llvm/llvm-project/commit/1514c1fd0dc0c787cc6f72ed017cd416a776f040
DIFF: https://github.com/llvm/llvm-project/commit/1514c1fd0dc0c787cc6f72ed017cd416a776f040.diff

LOG: [NFC][AutoUpgrade] Add logging for module flags upgrade (#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.

Added: 
    

Modified: 
    llvm/lib/IR/AutoUpgrade.cpp
    llvm/test/Bitcode/upgrade-branch-protection.ll

Removed: 
    


################################################################################
diff  --git a/llvm/lib/IR/AutoUpgrade.cpp b/llvm/lib/IR/AutoUpgrade.cpp
index 18071807c286a..784747b179d1c 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"));
@@ -7580,8 +7583,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.
@@ -7589,19 +7595,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())
@@ -7641,6 +7661,9 @@ void llvm::copyModuleAttrToFunctions(Module &M) {
       *ValPtr = CI->getZExtValue();
       if (*ValPtr == 2)
         return;
+
+      LLVM_DEBUG(dbgs() << "Found module flag: " << IDStr << "(" << *ValPtr
+                        << ")\n");
     }
   }
 
@@ -7677,17 +7700,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 6f60ba543e6c0..76fb369e9b09f 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