Skip to content

Commit ad727ab

Browse files
committed
[NFC] Migrate some callers away from Function/AttributeLists methods that take an index
These methods can be confusing.
1 parent 46cf825 commit ad727ab

19 files changed

+120
-57
lines changed

clang/lib/CodeGen/CGCall.cpp

+2-4
Original file line numberDiff line numberDiff line change
@@ -1885,7 +1885,7 @@ void CodeGenModule::addDefaultFunctionDefinitionAttributes(llvm::Function &F) {
18851885
getDefaultFunctionAttributes(F.getName(), F.hasOptNone(),
18861886
/* AttrOnCallSite = */ false, FuncAttrs);
18871887
// TODO: call GetCPUAndFeaturesAttributes?
1888-
F.addAttributes(llvm::AttributeList::FunctionIndex, FuncAttrs);
1888+
F.addFnAttrs(FuncAttrs);
18891889
}
18901890

18911891
void CodeGenModule::addDefaultFunctionDefinitionAttributes(
@@ -4526,9 +4526,7 @@ maybeRaiseRetAlignmentAttribute(llvm::LLVMContext &Ctx,
45264526
if (CurAlign >= NewAlign)
45274527
return Attrs;
45284528
llvm::Attribute AlignAttr = llvm::Attribute::getWithAlignment(Ctx, NewAlign);
4529-
return Attrs
4530-
.removeAttribute(Ctx, llvm::AttributeList::ReturnIndex,
4531-
llvm::Attribute::AttrKind::Alignment)
4529+
return Attrs.removeRetAttribute(Ctx, llvm::Attribute::AttrKind::Alignment)
45324530
.addAttribute(Ctx, llvm::AttributeList::ReturnIndex, AlignAttr);
45334531
}
45344532

clang/lib/CodeGen/CodeGenModule.cpp

+6-6
Original file line numberDiff line numberDiff line change
@@ -1781,7 +1781,7 @@ void CodeGenModule::SetLLVMFunctionAttributesForDefinition(const Decl *D,
17811781
CodeGenOpts.getInlining() == CodeGenOptions::OnlyAlwaysInlining)
17821782
B.addAttribute(llvm::Attribute::NoInline);
17831783

1784-
F->addAttributes(llvm::AttributeList::FunctionIndex, B);
1784+
F->addFnAttrs(B);
17851785
return;
17861786
}
17871787

@@ -1868,7 +1868,7 @@ void CodeGenModule::SetLLVMFunctionAttributesForDefinition(const Decl *D,
18681868
B.addAttribute(llvm::Attribute::MinSize);
18691869
}
18701870

1871-
F->addAttributes(llvm::AttributeList::FunctionIndex, B);
1871+
F->addFnAttrs(B);
18721872

18731873
unsigned alignment = D->getMaxAlignment() / Context.getCharWidth();
18741874
if (alignment)
@@ -1918,7 +1918,7 @@ void CodeGenModule::setLLVMFunctionFEnvAttributes(const FunctionDecl *D,
19181918
if (D->hasAttr<StrictFPAttr>()) {
19191919
llvm::AttrBuilder FuncAttrs;
19201920
FuncAttrs.addAttribute("strictfp");
1921-
F->addAttributes(llvm::AttributeList::FunctionIndex, FuncAttrs);
1921+
F->addFnAttrs(FuncAttrs);
19221922
}
19231923
}
19241924

@@ -2034,8 +2034,8 @@ void CodeGenModule::setNonAliasAttributes(GlobalDecl GD,
20342034
RemoveAttrs.addAttribute("target-cpu");
20352035
RemoveAttrs.addAttribute("target-features");
20362036
RemoveAttrs.addAttribute("tune-cpu");
2037-
F->removeAttributes(llvm::AttributeList::FunctionIndex, RemoveAttrs);
2038-
F->addAttributes(llvm::AttributeList::FunctionIndex, Attrs);
2037+
F->removeFnAttrs(RemoveAttrs);
2038+
F->addFnAttrs(Attrs);
20392039
}
20402040
}
20412041

@@ -3613,7 +3613,7 @@ llvm::Constant *CodeGenModule::GetOrCreateLLVMFunction(
36133613
SetFunctionAttributes(GD, F, IsIncompleteFunction, IsThunk);
36143614
if (ExtraAttrs.hasFnAttrs()) {
36153615
llvm::AttrBuilder B(ExtraAttrs, llvm::AttributeList::FunctionIndex);
3616-
F->addAttributes(llvm::AttributeList::FunctionIndex, B);
3616+
F->addFnAttrs(B);
36173617
}
36183618

36193619
if (!DontDefer) {

clang/lib/CodeGen/TargetInfo.cpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -827,19 +827,19 @@ class WebAssemblyTargetCodeGenInfo final : public TargetCodeGenInfo {
827827
llvm::Function *Fn = cast<llvm::Function>(GV);
828828
llvm::AttrBuilder B;
829829
B.addAttribute("wasm-import-module", Attr->getImportModule());
830-
Fn->addAttributes(llvm::AttributeList::FunctionIndex, B);
830+
Fn->addFnAttrs(B);
831831
}
832832
if (const auto *Attr = FD->getAttr<WebAssemblyImportNameAttr>()) {
833833
llvm::Function *Fn = cast<llvm::Function>(GV);
834834
llvm::AttrBuilder B;
835835
B.addAttribute("wasm-import-name", Attr->getImportName());
836-
Fn->addAttributes(llvm::AttributeList::FunctionIndex, B);
836+
Fn->addFnAttrs(B);
837837
}
838838
if (const auto *Attr = FD->getAttr<WebAssemblyExportNameAttr>()) {
839839
llvm::Function *Fn = cast<llvm::Function>(GV);
840840
llvm::AttrBuilder B;
841841
B.addAttribute("wasm-export-name", Attr->getExportName());
842-
Fn->addAttributes(llvm::AttributeList::FunctionIndex, B);
842+
Fn->addFnAttrs(B);
843843
}
844844
}
845845

@@ -6401,7 +6401,7 @@ class ARMTargetCodeGenInfo : public TargetCodeGenInfo {
64016401
// the backend to perform a realignment as part of the function prologue.
64026402
llvm::AttrBuilder B;
64036403
B.addStackAlignmentAttr(8);
6404-
Fn->addAttributes(llvm::AttributeList::FunctionIndex, B);
6404+
Fn->addFnAttrs(B);
64056405
}
64066406
};
64076407

llvm/include/llvm/CodeGen/IndirectThunks.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ void ThunkInserter<Derived>::createThunkFunction(MachineModuleInfo &MMI,
6262
AttrBuilder B;
6363
B.addAttribute(llvm::Attribute::NoUnwind);
6464
B.addAttribute(llvm::Attribute::Naked);
65-
F->addAttributes(llvm::AttributeList::FunctionIndex, B);
65+
F->addFnAttrs(B);
6666

6767
// Populate our function a bit so that we can verify.
6868
BasicBlock *Entry = BasicBlock::Create(Ctx, "entry", F);

llvm/include/llvm/IR/Attributes.h

+42
Original file line numberDiff line numberDiff line change
@@ -524,6 +524,48 @@ class AttributeList {
524524
LLVM_NODISCARD AttributeList removeAttributes(LLVMContext &C,
525525
unsigned Index) const;
526526

527+
/// Remove the specified attribute at the function index from this
528+
/// attribute list. Returns a new list because attribute lists are immutable.
529+
LLVM_NODISCARD AttributeList
530+
removeFnAttribute(LLVMContext &C, Attribute::AttrKind Kind) const {
531+
return removeAttribute(C, FunctionIndex, Kind);
532+
}
533+
534+
/// Remove the specified attribute at the function index from this
535+
/// attribute list. Returns a new list because attribute lists are immutable.
536+
LLVM_NODISCARD AttributeList removeFnAttribute(LLVMContext &C,
537+
StringRef Kind) const {
538+
return removeAttribute(C, FunctionIndex, Kind);
539+
}
540+
541+
/// Remove the specified attribute at the function index from this
542+
/// attribute list. Returns a new list because attribute lists are immutable.
543+
LLVM_NODISCARD AttributeList
544+
removeFnAttributes(LLVMContext &C, const AttrBuilder &AttrsToRemove) const {
545+
return removeAttributes(C, FunctionIndex, AttrsToRemove);
546+
}
547+
548+
/// Remove the specified attribute at the return value index from this
549+
/// attribute list. Returns a new list because attribute lists are immutable.
550+
LLVM_NODISCARD AttributeList
551+
removeRetAttribute(LLVMContext &C, Attribute::AttrKind Kind) const {
552+
return removeAttribute(C, ReturnIndex, Kind);
553+
}
554+
555+
/// Remove the specified attribute at the return value index from this
556+
/// attribute list. Returns a new list because attribute lists are immutable.
557+
LLVM_NODISCARD AttributeList removeRetAttribute(LLVMContext &C,
558+
StringRef Kind) const {
559+
return removeAttribute(C, ReturnIndex, Kind);
560+
}
561+
562+
/// Remove the specified attribute at the return value index from this
563+
/// attribute list. Returns a new list because attribute lists are immutable.
564+
LLVM_NODISCARD AttributeList
565+
removeRetAttributes(LLVMContext &C, const AttrBuilder &AttrsToRemove) const {
566+
return removeAttributes(C, ReturnIndex, AttrsToRemove);
567+
}
568+
527569
/// Remove the specified attribute at the specified arg index from this
528570
/// attribute list. Returns a new list because attribute lists are immutable.
529571
LLVM_NODISCARD AttributeList removeParamAttribute(

llvm/include/llvm/IR/Function.h

+21-5
Original file line numberDiff line numberDiff line change
@@ -272,15 +272,26 @@ class Function : public GlobalObject, public ilist_node<Function> {
272272
addAttribute(AttributeList::FunctionIndex, Attr);
273273
}
274274

275+
/// Add function attributes to this function.
276+
void addFnAttrs(const AttrBuilder &Attrs) {
277+
addAttributes(AttributeList::FunctionIndex, Attrs);
278+
}
279+
280+
/// removes the attributes from the list of attributes.
281+
void removeAttributes(unsigned i, const AttrBuilder &Attrs);
282+
275283
/// Remove function attributes from this function.
276284
void removeFnAttr(Attribute::AttrKind Kind) {
277-
removeAttribute(AttributeList::FunctionIndex, Kind);
285+
setAttributes(getAttributes().removeFnAttribute(getContext(), Kind));
278286
}
279287

280288
/// Remove function attribute from this function.
281289
void removeFnAttr(StringRef Kind) {
282-
setAttributes(getAttributes().removeAttribute(
283-
getContext(), AttributeList::FunctionIndex, Kind));
290+
setAttributes(getAttributes().removeFnAttribute(getContext(), Kind));
291+
}
292+
293+
void removeFnAttrs(const AttrBuilder &Attrs) {
294+
setAttributes(getAttributes().removeFnAttributes(getContext(), Attrs));
284295
}
285296

286297
/// A function will have the "coroutine.presplit" attribute if it's
@@ -429,8 +440,13 @@ class Function : public GlobalObject, public ilist_node<Function> {
429440
/// removes the attribute from the list of attributes.
430441
void removeAttribute(unsigned i, StringRef Kind);
431442

432-
/// removes the attributes from the list of attributes.
433-
void removeAttributes(unsigned i, const AttrBuilder &Attrs);
443+
/// removes the attribute from the return value list of attributes.
444+
void removeRetAttr(Attribute::AttrKind Kind);
445+
446+
/// removes the attribute from the return value list of attributes.
447+
void removeRetAttr(StringRef Kind);
448+
449+
void removeRetAttrs(const AttrBuilder &Attrs);
434450

435451
/// removes the attribute from the list of attributes.
436452
void removeParamAttr(unsigned ArgNo, Attribute::AttrKind Kind);

llvm/lib/IR/Attributes.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -1961,11 +1961,11 @@ static void adjustCallerSSPLevel(Function &Caller, const Function &Callee) {
19611961
.addAttribute(Attribute::StackProtectReq);
19621962

19631963
if (Callee.hasFnAttribute(Attribute::StackProtectReq)) {
1964-
Caller.removeAttributes(AttributeList::FunctionIndex, OldSSPAttr);
1964+
Caller.removeFnAttrs(OldSSPAttr);
19651965
Caller.addFnAttr(Attribute::StackProtectReq);
19661966
} else if (Callee.hasFnAttribute(Attribute::StackProtectStrong) &&
19671967
!Caller.hasFnAttribute(Attribute::StackProtectReq)) {
1968-
Caller.removeAttributes(AttributeList::FunctionIndex, OldSSPAttr);
1968+
Caller.removeFnAttrs(OldSSPAttr);
19691969
Caller.addFnAttr(Attribute::StackProtectStrong);
19701970
} else if (Callee.hasFnAttribute(Attribute::StackProtect) &&
19711971
!Caller.hasFnAttribute(Attribute::StackProtectReq) &&

llvm/lib/IR/AutoUpgrade.cpp

+1-2
Original file line numberDiff line numberDiff line change
@@ -4383,8 +4383,7 @@ void llvm::UpgradeFunctionAttributes(Function &F) {
43834383
}
43844384

43854385
// Remove all incompatibile attributes from function.
4386-
F.removeAttributes(AttributeList::ReturnIndex,
4387-
AttributeFuncs::typeIncompatible(F.getReturnType()));
4386+
F.removeRetAttrs(AttributeFuncs::typeIncompatible(F.getReturnType()));
43884387
for (auto &Arg : F.args())
43894388
Arg.removeAttrs(AttributeFuncs::typeIncompatible(Arg.getType()));
43904389
}

llvm/lib/IR/Function.cpp

+15-3
Original file line numberDiff line numberDiff line change
@@ -354,7 +354,7 @@ Function *Function::createWithDefaultAttr(FunctionType *Ty,
354354
B.addAttribute("frame-pointer", "all");
355355
break;
356356
}
357-
F->addAttributes(AttributeList::FunctionIndex, B);
357+
F->addFnAttrs(B);
358358
return F;
359359
}
360360

@@ -577,9 +577,21 @@ void Function::removeAttribute(unsigned i, StringRef Kind) {
577577
setAttributes(PAL);
578578
}
579579

580-
void Function::removeAttributes(unsigned i, const AttrBuilder &Attrs) {
580+
void Function::removeRetAttr(Attribute::AttrKind Kind) {
581581
AttributeList PAL = getAttributes();
582-
PAL = PAL.removeAttributes(getContext(), i, Attrs);
582+
PAL = PAL.removeRetAttribute(getContext(), Kind);
583+
setAttributes(PAL);
584+
}
585+
586+
void Function::removeRetAttr(StringRef Kind) {
587+
AttributeList PAL = getAttributes();
588+
PAL = PAL.removeRetAttribute(getContext(), Kind);
589+
setAttributes(PAL);
590+
}
591+
592+
void Function::removeRetAttrs(const AttrBuilder &Attrs) {
593+
AttributeList PAL = getAttributes();
594+
PAL = PAL.removeRetAttributes(getContext(), Attrs);
583595
setAttributes(PAL);
584596
}
585597

llvm/lib/Target/AMDGPU/AMDGPURewriteOutArguments.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -357,7 +357,7 @@ bool AMDGPURewriteOutArguments::runOnFunction(Function &F) {
357357
RetAttrs.addAttribute(Attribute::SExt);
358358
RetAttrs.addAttribute(Attribute::ZExt);
359359
RetAttrs.addAttribute(Attribute::NoAlias);
360-
NewFunc->removeAttributes(AttributeList::ReturnIndex, RetAttrs);
360+
NewFunc->removeRetAttrs(RetAttrs);
361361
// TODO: How to preserve metadata?
362362

363363
// Move the body of the function into the new rewritten function, and replace

llvm/lib/Target/Mips/Mips16HardFloat.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -485,11 +485,11 @@ static void removeUseSoftFloat(Function &F) {
485485
AttrBuilder B;
486486
LLVM_DEBUG(errs() << "removing -use-soft-float\n");
487487
B.addAttribute("use-soft-float", "false");
488-
F.removeAttributes(AttributeList::FunctionIndex, B);
488+
F.removeFnAttrs(B);
489489
if (F.hasFnAttribute("use-soft-float")) {
490490
LLVM_DEBUG(errs() << "still has -use-soft-float\n");
491491
}
492-
F.addAttributes(AttributeList::FunctionIndex, B);
492+
F.addFnAttrs(B);
493493
}
494494

495495
// This pass only makes sense when the underlying chip has floating point but

llvm/lib/Target/WebAssembly/WebAssemblyLowerEmscriptenEHSjLj.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -352,12 +352,12 @@ static Function *getEmscriptenFunction(FunctionType *Ty, const Twine &Name,
352352
if (!F->hasFnAttribute("wasm-import-module")) {
353353
llvm::AttrBuilder B;
354354
B.addAttribute("wasm-import-module", "env");
355-
F->addAttributes(llvm::AttributeList::FunctionIndex, B);
355+
F->addFnAttrs(B);
356356
}
357357
if (!F->hasFnAttribute("wasm-import-name")) {
358358
llvm::AttrBuilder B;
359359
B.addAttribute("wasm-import-name", F->getName());
360-
F->addAttributes(llvm::AttributeList::FunctionIndex, B);
360+
F->addFnAttrs(B);
361361
}
362362
return F;
363363
}

llvm/lib/Transforms/Coroutines/CoroSplit.cpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -1568,8 +1568,8 @@ static void splitAsyncCoroutine(Function &F, coro::Shape &Shape,
15681568
// Reset various things that the optimizer might have decided it
15691569
// "knows" about the coroutine function due to not seeing a return.
15701570
F.removeFnAttr(Attribute::NoReturn);
1571-
F.removeAttribute(AttributeList::ReturnIndex, Attribute::NoAlias);
1572-
F.removeAttribute(AttributeList::ReturnIndex, Attribute::NonNull);
1571+
F.removeRetAttr(Attribute::NoAlias);
1572+
F.removeRetAttr(Attribute::NonNull);
15731573

15741574
auto &Context = F.getContext();
15751575
auto *Int8PtrTy = Type::getInt8PtrTy(Context);
@@ -1667,8 +1667,8 @@ static void splitRetconCoroutine(Function &F, coro::Shape &Shape,
16671667
// Reset various things that the optimizer might have decided it
16681668
// "knows" about the coroutine function due to not seeing a return.
16691669
F.removeFnAttr(Attribute::NoReturn);
1670-
F.removeAttribute(AttributeList::ReturnIndex, Attribute::NoAlias);
1671-
F.removeAttribute(AttributeList::ReturnIndex, Attribute::NonNull);
1670+
F.removeRetAttr(Attribute::NoAlias);
1671+
F.removeRetAttr(Attribute::NonNull);
16721672

16731673
// Allocate the frame.
16741674
auto *Id = cast<AnyCoroIdRetconInst>(Shape.CoroBegin->getId());

llvm/lib/Transforms/IPO/FunctionAttrs.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,7 @@ static bool addReadAttrs(const SCCNodeSet &SCCNodes, AARGetterT &&AARGetter) {
303303
AttrsToRemove.addAttribute(Attribute::InaccessibleMemOnly);
304304
AttrsToRemove.addAttribute(Attribute::InaccessibleMemOrArgMemOnly);
305305
}
306-
F->removeAttributes(AttributeList::FunctionIndex, AttrsToRemove);
306+
F->removeFnAttrs(AttrsToRemove);
307307

308308
// Add in the new attribute.
309309
if (WritesMemory && !ReadsMemory)

llvm/lib/Transforms/Instrumentation/DataFlowSanitizer.cpp

+7-12
Original file line numberDiff line numberDiff line change
@@ -1154,14 +1154,12 @@ DataFlowSanitizer::buildWrapperFunction(Function *F, StringRef NewFName,
11541154
Function *NewF = Function::Create(NewFT, NewFLink, F->getAddressSpace(),
11551155
NewFName, F->getParent());
11561156
NewF->copyAttributesFrom(F);
1157-
NewF->removeAttributes(
1158-
AttributeList::ReturnIndex,
1157+
NewF->removeRetAttrs(
11591158
AttributeFuncs::typeIncompatible(NewFT->getReturnType()));
11601159

11611160
BasicBlock *BB = BasicBlock::Create(*Ctx, "entry", NewF);
11621161
if (F->isVarArg()) {
1163-
NewF->removeAttributes(AttributeList::FunctionIndex,
1164-
AttrBuilder().addAttribute("split-stack"));
1162+
NewF->removeFnAttrs(AttrBuilder().addAttribute("split-stack"));
11651163
CallInst::Create(DFSanVarargWrapperFn,
11661164
IRBuilder<>(BB).CreateGlobalStringPtr(F->getName()), "",
11671165
BB);
@@ -1464,8 +1462,7 @@ bool DataFlowSanitizer::runImpl(Module &M) {
14641462
Function *NewF = Function::Create(NewFT, F.getLinkage(),
14651463
F.getAddressSpace(), "", &M);
14661464
NewF->copyAttributesFrom(&F);
1467-
NewF->removeAttributes(
1468-
AttributeList::ReturnIndex,
1465+
NewF->removeRetAttrs(
14691466
AttributeFuncs::typeIncompatible(NewFT->getReturnType()));
14701467
for (Function::arg_iterator FArg = F.arg_begin(),
14711468
NewFArg = NewF->arg_begin(),
@@ -1513,7 +1510,7 @@ bool DataFlowSanitizer::runImpl(Module &M) {
15131510
std::string(F.getName()),
15141511
WrapperLinkage, NewFT);
15151512
if (getInstrumentedABI() == IA_TLS)
1516-
NewF->removeAttributes(AttributeList::FunctionIndex, ReadOnlyNoneAttrs);
1513+
NewF->removeFnAttrs(ReadOnlyNoneAttrs);
15171514

15181515
Value *WrappedFnCst =
15191516
ConstantExpr::getBitCast(NewF, PointerType::getUnqual(FT));
@@ -2953,8 +2950,7 @@ bool DFSanVisitor::visitWrappedCallBase(Function &F, CallBase &CB) {
29532950

29542951
// Custom functions returning non-void will write to the return label.
29552952
if (!FT->getReturnType()->isVoidTy()) {
2956-
CustomFn->removeAttributes(AttributeList::FunctionIndex,
2957-
DFSF.DFS.ReadOnlyNoneAttrs);
2953+
CustomFn->removeFnAttrs(DFSF.DFS.ReadOnlyNoneAttrs);
29582954
}
29592955
}
29602956

@@ -3176,9 +3172,8 @@ void DFSanVisitor::visitCallBase(CallBase &CB) {
31763172
NewCB = IRB.CreateCall(NewFT, Func, Args);
31773173
}
31783174
NewCB->setCallingConv(CB.getCallingConv());
3179-
NewCB->setAttributes(CB.getAttributes().removeAttributes(
3180-
*DFSF.DFS.Ctx, AttributeList::ReturnIndex,
3181-
AttributeFuncs::typeIncompatible(NewCB->getType())));
3175+
NewCB->setAttributes(CB.getAttributes().removeRetAttributes(
3176+
*DFSF.DFS.Ctx, AttributeFuncs::typeIncompatible(NewCB->getType())));
31823177

31833178
if (Next) {
31843179
ExtractValueInst *ExVal = ExtractValueInst::Create(NewCB, 0, "", Next);

0 commit comments

Comments
 (0)