[clang] [llvm] [ARM][AArch64] BTI, GCS, PAC Module flag update. (PR #86212)
Andrew Savonichev via llvm-commits
llvm-commits at lists.llvm.org
Sat Sep 20 03:25:26 PDT 2025
================
@@ -5296,6 +5296,106 @@ void llvm::UpgradeFunctionAttributes(Function &F) {
}
}
+// Check if the module attribute is present and set to one.
+static bool isModuleAttributeOne(Module &M, const StringRef &ModAttr) {
+ const auto *Attr =
+ mdconst::extract_or_null<ConstantInt>(M.getModuleFlag(ModAttr));
+ return Attr && Attr->isOne();
+}
+
+// Check if the module attribute is present and set to two.
+static bool isModuleAttributeTwo(Module &M, const StringRef &ModAttr) {
+ const auto *Attr =
+ mdconst::extract_or_null<ConstantInt>(M.getModuleFlag(ModAttr));
+ return Attr && Attr->getZExtValue() == 2;
+}
+
+// Check if the function attribute is not present and set it.
+static void SetFunctionAttrIfNotSet(Function &F, StringRef FnAttrName,
+ StringRef Value) {
+ if (!F.hasFnAttribute(FnAttrName))
+ F.addFnAttr(FnAttrName, Value);
+}
+
+// Check if the function attribute is not present and set it if needed.
+// If the attribute is "false" then removes it.
+// 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)
+ F.addFnAttr(FnAttrName);
+ } else {
+ auto A = F.getFnAttribute(FnAttrName);
+ if ("false" == A.getValueAsString())
+ F.removeFnAttr(FnAttrName);
+ else if ("true" == A.getValueAsString()) {
+ F.removeFnAttr(FnAttrName);
+ F.addFnAttr(FnAttrName);
+ }
+ }
+}
+
+void llvm::CopyModuleAttrToFunctions(Module &M) {
+ Triple T(M.getTargetTriple());
+ if (!T.isThumb() && !T.isARM() && !T.isAArch64())
+ return;
+
+ if (isModuleAttributeTwo(M, "branch-target-enforcement"))
----------------
asavonic wrote:
`isModuleAttributeTwo` and `isModuleAttributeOne` call [Module::getModuleFlag](https://github.com/llvm/llvm-project/blob/674491928c9ae8ec25bde79044894570988202ea/llvm/lib/IR/Module.cpp#L357) function, which is a loop for each flag. This may look and perform better if we traverse flags only once, extract them as integers, and then do all the processing, like we do in [UpgradeModuleFlags](https://github.com/llvm/llvm-project/blob/99b6f2bc9c472fe930e216cf4cc72dd5cdbf14d7/llvm/lib/IR/AutoUpgrade.cpp#L5089).
https://github.com/llvm/llvm-project/pull/86212
More information about the llvm-commits
mailing list