Skip to content

Commit 84eae9d

Browse files
[NFC][LLVM] Refactor Autoupgrade function attributes from Module attributes.
Refactoring llvm#82763 to cache module attributes.
1 parent bd6eb54 commit 84eae9d

File tree

1 file changed

+24
-47
lines changed

1 file changed

+24
-47
lines changed

llvm/lib/IR/AutoUpgrade.cpp

+24-47
Original file line numberDiff line numberDiff line change
@@ -5185,62 +5185,39 @@ static bool isModuleAttributeSet(Module &M, const StringRef &ModAttr) {
51855185
return Attr && Attr->getZExtValue();
51865186
}
51875187

5188-
// Copy an attribute from module to the function if exists.
5189-
// First value of the pair is used when the module attribute is not zero
5190-
// the second otherwise.
5191-
static void
5192-
CopyModuleAttributeToFunction(Function &F, StringRef FnAttrName,
5193-
StringRef ModAttrName,
5194-
std::pair<StringRef, StringRef> Values) {
5195-
if (F.hasFnAttribute(FnAttrName))
5196-
return;
5197-
F.addFnAttr(FnAttrName, isModuleAttributeSet(*F.getParent(), ModAttrName)
5198-
? Values.first
5199-
: Values.second);
5200-
}
5201-
5202-
// Copy a boolean attribute from module to the function if exists.
5203-
// Module attribute treated false if zero otherwise true.
5204-
static void CopyModuleAttributeToFunction(Function &F, StringRef AttrName) {
5205-
CopyModuleAttributeToFunction(
5206-
F, AttrName, AttrName,
5207-
std::make_pair<StringRef, StringRef>("true", "false"));
5208-
}
5209-
5210-
// Copy an attribute from module to the function if exists.
5211-
// First value of the pair is used when the module attribute is not zero
5212-
// the second otherwise.
5213-
static void
5214-
CopyModuleAttributeToFunction(Function &F, StringRef AttrName,
5215-
std::pair<StringRef, StringRef> Values) {
5216-
CopyModuleAttributeToFunction(F, AttrName, AttrName, Values);
5217-
}
5218-
52195188
void llvm::CopyModuleAttrToFunctions(Module &M) {
52205189
Triple T(M.getTargetTriple());
52215190
if (!T.isThumb() && !T.isARM() && !T.isAArch64())
52225191
return;
52235192

5193+
StringRef SignTypeValue = "none";
5194+
if (isModuleAttributeSet(M, "sign-return-address"))
5195+
SignTypeValue = "non-leaf";
5196+
if (isModuleAttributeSet(M, "sign-return-address-all"))
5197+
SignTypeValue = "all";
5198+
5199+
StringRef BTEValue =
5200+
isModuleAttributeSet(M, "branch-target-enforcement") ? "true" : "false";
5201+
StringRef BPPLValue =
5202+
isModuleAttributeSet(M, "branch-protection-pauth-lr") ? "true" : "false";
5203+
StringRef GCSValue =
5204+
isModuleAttributeSet(M, "guarded-control-stack") ? "true" : "false";
5205+
StringRef SignKeyValue =
5206+
isModuleAttributeSet(M, "sign-return-address-key") ? "b_key" : "a_key";
5207+
52245208
for (Function &F : M.getFunctionList()) {
52255209
if (F.isDeclaration())
52265210
continue;
5211+
auto SetFunctionAttrIfNotSet = [&](StringRef FnAttrName, StringRef Value) {
5212+
if (!F.hasFnAttribute(FnAttrName))
5213+
F.addFnAttr(FnAttrName, Value);
5214+
};
52275215

5228-
if (!F.hasFnAttribute("sign-return-address")) {
5229-
StringRef SignType = "none";
5230-
if (isModuleAttributeSet(M, "sign-return-address"))
5231-
SignType = "non-leaf";
5232-
5233-
if (isModuleAttributeSet(M, "sign-return-address-all"))
5234-
SignType = "all";
5235-
5236-
F.addFnAttr("sign-return-address", SignType);
5237-
}
5238-
CopyModuleAttributeToFunction(F, "branch-target-enforcement");
5239-
CopyModuleAttributeToFunction(F, "branch-protection-pauth-lr");
5240-
CopyModuleAttributeToFunction(F, "guarded-control-stack");
5241-
CopyModuleAttributeToFunction(
5242-
F, "sign-return-address-key",
5243-
std::make_pair<StringRef, StringRef>("b_key", "a_key"));
5216+
SetFunctionAttrIfNotSet("sign-return-address", SignTypeValue);
5217+
SetFunctionAttrIfNotSet("branch-target-enforcement", BTEValue);
5218+
SetFunctionAttrIfNotSet("branch-protection-pauth-lr", BPPLValue);
5219+
SetFunctionAttrIfNotSet("guarded-control-stack", GCSValue);
5220+
SetFunctionAttrIfNotSet("sign-return-address-key", SignKeyValue);
52445221
}
52455222
}
52465223

0 commit comments

Comments
 (0)