Skip to content

Commit de6480a

Browse files
author
Erich Keane
committed
[NFC] Move storage of dispatch-version to GlobalDecl
As suggested by Richard Smith, and initially put up for review here: https://reviews.llvm.org/D53341, this patch removes a hack that was used to ensure that proper target-feature lists were used when emitting cpu-dispatch (and eventually, target-clones) implementations. As a part of this, the GlobalDecl object is proliferated to a bunch more locations. Originally, this was put up for review (see above) to get acceptance on the approach, though discussion with Richard in San Diego showed he approved of the approach taken here. Thus, I believe this is acceptable for Review-After-commit Differential Revision: https://reviews.llvm.org/D53341 Change-Id: I0a0bd673340d334d93feac789d653e03d9f6b1d5 llvm-svn: 346757
1 parent 28e2dbb commit de6480a

21 files changed

+182
-120
lines changed

clang/include/clang/AST/GlobalDecl.h

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ namespace clang {
3434
/// a VarDecl, a FunctionDecl or a BlockDecl.
3535
class GlobalDecl {
3636
llvm::PointerIntPair<const Decl *, 2> Value;
37+
unsigned MultiVersionIndex = 0;
3738

3839
void Init(const Decl *D) {
3940
assert(!isa<CXXConstructorDecl>(D) && "Use other ctor with ctor decls!");
@@ -45,7 +46,10 @@ class GlobalDecl {
4546
public:
4647
GlobalDecl() = default;
4748
GlobalDecl(const VarDecl *D) { Init(D);}
48-
GlobalDecl(const FunctionDecl *D) { Init(D); }
49+
GlobalDecl(const FunctionDecl *D, unsigned MVIndex = 0)
50+
: MultiVersionIndex(MVIndex) {
51+
Init(D);
52+
}
4953
GlobalDecl(const BlockDecl *D) { Init(D); }
5054
GlobalDecl(const CapturedDecl *D) { Init(D); }
5155
GlobalDecl(const ObjCMethodDecl *D) { Init(D); }
@@ -57,6 +61,7 @@ class GlobalDecl {
5761
GlobalDecl CanonGD;
5862
CanonGD.Value.setPointer(Value.getPointer()->getCanonicalDecl());
5963
CanonGD.Value.setInt(Value.getInt());
64+
CanonGD.MultiVersionIndex = MultiVersionIndex;
6065

6166
return CanonGD;
6267
}
@@ -73,8 +78,17 @@ class GlobalDecl {
7378
return static_cast<CXXDtorType>(Value.getInt());
7479
}
7580

81+
unsigned getMultiVersionIndex() const {
82+
assert(isa<FunctionDecl>(getDecl()) &&
83+
!isa<CXXConstructorDecl>(getDecl()) &&
84+
!isa<CXXDestructorDecl>(getDecl()) &&
85+
"Decl is not a plain FunctionDecl!");
86+
return MultiVersionIndex;
87+
}
88+
7689
friend bool operator==(const GlobalDecl &LHS, const GlobalDecl &RHS) {
77-
return LHS.Value == RHS.Value;
90+
return LHS.Value == RHS.Value &&
91+
LHS.MultiVersionIndex == RHS.MultiVersionIndex;
7892
}
7993

8094
void *getAsOpaquePtr() const { return Value.getOpaqueValue(); }
@@ -90,6 +104,16 @@ class GlobalDecl {
90104
Result.Value.setPointer(D);
91105
return Result;
92106
}
107+
108+
GlobalDecl getWithMultiVersionIndex(unsigned Index) {
109+
assert(isa<FunctionDecl>(getDecl()) &&
110+
!isa<CXXConstructorDecl>(getDecl()) &&
111+
!isa<CXXDestructorDecl>(getDecl()) &&
112+
"Decl is not a plain FunctionDecl!");
113+
GlobalDecl Result(*this);
114+
Result.MultiVersionIndex = Index;
115+
return Result;
116+
}
93117
};
94118

95119
} // namespace clang

clang/include/clang/Basic/Attr.td

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -864,10 +864,8 @@ def CPUSpecific : InheritableAttr {
864864
let Subjects = SubjectList<[Function]>;
865865
let Documentation = [CPUSpecificCPUDispatchDocs];
866866
let AdditionalMembers = [{
867-
unsigned ActiveArgIndex = 0;
868-
869-
IdentifierInfo *getCurCPUName() const {
870-
return *(cpus_begin() + ActiveArgIndex);
867+
IdentifierInfo *getCPUName(unsigned Index) const {
868+
return *(cpus_begin() + Index);
871869
}
872870
}];
873871
}

clang/lib/CodeGen/CGBlocks.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1983,7 +1983,7 @@ static void setBlockHelperAttributesVisibility(bool CapturesNonExternalType,
19831983
} else {
19841984
Fn->setVisibility(llvm::GlobalValue::HiddenVisibility);
19851985
Fn->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global);
1986-
CGM.SetLLVMFunctionAttributes(nullptr, FI, Fn);
1986+
CGM.SetLLVMFunctionAttributes(GlobalDecl(), FI, Fn);
19871987
CGM.SetLLVMFunctionAttributesForDefinition(nullptr, Fn);
19881988
}
19891989
}

clang/lib/CodeGen/CGBuiltin.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -385,7 +385,7 @@ static Value *EmitSignBit(CodeGenFunction &CGF, Value *V) {
385385

386386
static RValue emitLibraryCall(CodeGenFunction &CGF, const FunctionDecl *FD,
387387
const CallExpr *E, llvm::Constant *calleeValue) {
388-
CGCallee callee = CGCallee::forDirect(calleeValue, FD);
388+
CGCallee callee = CGCallee::forDirect(calleeValue, GlobalDecl(FD));
389389
return CGF.EmitCall(E->getCallee()->getType(), callee, E, ReturnValueSlot());
390390
}
391391

@@ -1103,7 +1103,7 @@ llvm::Function *CodeGenFunction::generateBuiltinOSLogHelperFunction(
11031103
llvm::Function *Fn = llvm::Function::Create(
11041104
FuncTy, llvm::GlobalValue::LinkOnceODRLinkage, Name, &CGM.getModule());
11051105
Fn->setVisibility(llvm::GlobalValue::HiddenVisibility);
1106-
CGM.SetLLVMFunctionAttributes(nullptr, FI, Fn);
1106+
CGM.SetLLVMFunctionAttributes(GlobalDecl(), FI, Fn);
11071107
CGM.SetLLVMFunctionAttributesForDefinition(nullptr, Fn);
11081108

11091109
// Attach 'noinline' at -Oz.
@@ -1424,9 +1424,10 @@ RValue CodeGenFunction::emitRotate(const CallExpr *E, bool IsRotateRight) {
14241424
return RValue::get(Builder.CreateCall(F, { Src, Src, ShiftAmt }));
14251425
}
14261426

1427-
RValue CodeGenFunction::EmitBuiltinExpr(const FunctionDecl *FD,
1428-
unsigned BuiltinID, const CallExpr *E,
1427+
RValue CodeGenFunction::EmitBuiltinExpr(const GlobalDecl GD, unsigned BuiltinID,
1428+
const CallExpr *E,
14291429
ReturnValueSlot ReturnValue) {
1430+
const FunctionDecl *FD = GD.getDecl()->getAsFunction();
14301431
// See if we can constant fold this builtin. If so, don't emit it at all.
14311432
Expr::EvalResult Result;
14321433
if (E->EvaluateAsRValue(Result, CGM.getContext()) &&

clang/lib/CodeGen/CGCXX.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,7 @@ static CGCallee BuildAppleKextVirtualCall(CodeGenFunction &CGF,
276276
CGF.Builder.CreateConstInBoundsGEP1_64(VTable, VTableIndex, "vfnkxt");
277277
llvm::Value *VFunc =
278278
CGF.Builder.CreateAlignedLoad(VFuncPtr, CGF.PointerAlignInBytes);
279-
CGCallee Callee(GD.getDecl()->getCanonicalDecl(), VFunc);
279+
CGCallee Callee(GD, VFunc);
280280
return Callee;
281281
}
282282

clang/lib/CodeGen/CGCall.cpp

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1833,7 +1833,7 @@ void CodeGenModule::ConstructAttributeList(
18331833
AddAttributesFromFunctionProtoType(getContext(), FuncAttrs,
18341834
CalleeInfo.getCalleeFunctionProtoType());
18351835

1836-
const Decl *TargetDecl = CalleeInfo.getCalleeDecl();
1836+
const Decl *TargetDecl = CalleeInfo.getCalleeDecl().getDecl();
18371837

18381838
bool HasOptnone = false;
18391839
// FIXME: handle sseregparm someday...
@@ -1941,7 +1941,7 @@ void CodeGenModule::ConstructAttributeList(
19411941

19421942
FuncAttrs.addAttribute("disable-tail-calls",
19431943
llvm::toStringRef(DisableTailCalls));
1944-
GetCPUAndFeaturesAttributes(TargetDecl, FuncAttrs);
1944+
GetCPUAndFeaturesAttributes(CalleeInfo.getCalleeDecl(), FuncAttrs);
19451945
}
19461946

19471947
ClangToLLVMArgMapping IRFunctionArgs(getContext(), FI);
@@ -4260,8 +4260,11 @@ RValue CodeGenFunction::EmitCall(const CGFunctionInfo &CallInfo,
42604260
// Apply always_inline to all calls within flatten functions.
42614261
// FIXME: should this really take priority over __try, below?
42624262
if (CurCodeDecl && CurCodeDecl->hasAttr<FlattenAttr>() &&
4263-
!(Callee.getAbstractInfo().getCalleeDecl() &&
4264-
Callee.getAbstractInfo().getCalleeDecl()->hasAttr<NoInlineAttr>())) {
4263+
!(Callee.getAbstractInfo().getCalleeDecl().getDecl() &&
4264+
Callee.getAbstractInfo()
4265+
.getCalleeDecl()
4266+
.getDecl()
4267+
->hasAttr<NoInlineAttr>())) {
42654268
Attrs =
42664269
Attrs.addAttribute(getLLVMContext(), llvm::AttributeList::FunctionIndex,
42674270
llvm::Attribute::AlwaysInline);
@@ -4346,7 +4349,7 @@ RValue CodeGenFunction::EmitCall(const CGFunctionInfo &CallInfo,
43464349

43474350
// Suppress tail calls if requested.
43484351
if (llvm::CallInst *Call = dyn_cast<llvm::CallInst>(CI)) {
4349-
const Decl *TargetDecl = Callee.getAbstractInfo().getCalleeDecl();
4352+
const Decl *TargetDecl = Callee.getAbstractInfo().getCalleeDecl().getDecl();
43504353
if (TargetDecl && TargetDecl->hasAttr<NotTailCalledAttr>())
43514354
Call->setTailCallKind(llvm::CallInst::TCK_NoTail);
43524355
}
@@ -4493,7 +4496,7 @@ RValue CodeGenFunction::EmitCall(const CGFunctionInfo &CallInfo,
44934496
} ();
44944497

44954498
// Emit the assume_aligned check on the return value.
4496-
const Decl *TargetDecl = Callee.getAbstractInfo().getCalleeDecl();
4499+
const Decl *TargetDecl = Callee.getAbstractInfo().getCalleeDecl().getDecl();
44974500
if (Ret.isScalar() && TargetDecl) {
44984501
if (const auto *AA = TargetDecl->getAttr<AssumeAlignedAttr>()) {
44994502
llvm::Value *OffsetValue = nullptr;

clang/lib/CodeGen/CGCall.h

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -46,21 +46,21 @@ class CGCalleeInfo {
4646
/// The function prototype of the callee.
4747
const FunctionProtoType *CalleeProtoTy;
4848
/// The function declaration of the callee.
49-
const Decl *CalleeDecl;
49+
GlobalDecl CalleeDecl;
5050

5151
public:
52-
explicit CGCalleeInfo() : CalleeProtoTy(nullptr), CalleeDecl(nullptr) {}
53-
CGCalleeInfo(const FunctionProtoType *calleeProtoTy, const Decl *calleeDecl)
52+
explicit CGCalleeInfo() : CalleeProtoTy(nullptr), CalleeDecl() {}
53+
CGCalleeInfo(const FunctionProtoType *calleeProtoTy, GlobalDecl calleeDecl)
5454
: CalleeProtoTy(calleeProtoTy), CalleeDecl(calleeDecl) {}
5555
CGCalleeInfo(const FunctionProtoType *calleeProtoTy)
56-
: CalleeProtoTy(calleeProtoTy), CalleeDecl(nullptr) {}
57-
CGCalleeInfo(const Decl *calleeDecl)
56+
: CalleeProtoTy(calleeProtoTy), CalleeDecl() {}
57+
CGCalleeInfo(GlobalDecl calleeDecl)
5858
: CalleeProtoTy(nullptr), CalleeDecl(calleeDecl) {}
5959

6060
const FunctionProtoType *getCalleeFunctionProtoType() const {
6161
return CalleeProtoTy;
6262
}
63-
const Decl *getCalleeDecl() const { return CalleeDecl; }
63+
const GlobalDecl getCalleeDecl() const { return CalleeDecl; }
6464
};
6565

6666
/// All available information about a concrete callee.
@@ -171,7 +171,7 @@ class CGCalleeInfo {
171171
}
172172
CGCalleeInfo getAbstractInfo() const {
173173
if (isVirtual())
174-
return VirtualInfo.MD.getDecl();
174+
return VirtualInfo.MD;
175175
assert(isOrdinary());
176176
return AbstractInfo;
177177
}

clang/lib/CodeGen/CGClass.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2122,7 +2122,7 @@ void CodeGenFunction::EmitCXXConstructorCall(const CXXConstructorDecl *D,
21222122
CGM.getAddrOfCXXStructor(D, getFromCtorType(Type));
21232123
const CGFunctionInfo &Info = CGM.getTypes().arrangeCXXConstructorCall(
21242124
Args, D, Type, ExtraArgs.Prefix, ExtraArgs.Suffix, PassPrototypeArgs);
2125-
CGCallee Callee = CGCallee::forDirect(CalleePtr, D);
2125+
CGCallee Callee = CGCallee::forDirect(CalleePtr, GlobalDecl(D, Type));
21262126
EmitCall(Info, Callee, ReturnValueSlot(), Args);
21272127

21282128
// Generate vtable assumptions if we're constructing a complete object
@@ -2808,7 +2808,7 @@ void CodeGenFunction::EmitForwardingCallToLambda(
28082808
// variadic arguments.
28092809

28102810
// Now emit our call.
2811-
auto callee = CGCallee::forDirect(calleePtr, callOperator);
2811+
auto callee = CGCallee::forDirect(calleePtr, GlobalDecl(callOperator));
28122812
RValue RV = EmitCall(calleeFnInfo, callee, returnSlot, callArgs);
28132813

28142814
// If necessary, copy the returned value into the slot.

clang/lib/CodeGen/CGException.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1878,7 +1878,7 @@ void CodeGenFunction::startOutlinedSEHHelper(CodeGenFunction &ParentCGF,
18781878
OutlinedStmt->getBeginLoc(), OutlinedStmt->getBeginLoc());
18791879
CurSEHParent = ParentCGF.CurSEHParent;
18801880

1881-
CGM.SetLLVMFunctionAttributes(nullptr, FnInfo, CurFn);
1881+
CGM.SetLLVMFunctionAttributes(GlobalDecl(), FnInfo, CurFn);
18821882
EmitCapturedLocals(ParentCGF, OutlinedStmt, IsFilter);
18831883
}
18841884

clang/lib/CodeGen/CGExpr.cpp

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4375,7 +4375,7 @@ static CGCallee EmitDirectCallee(CodeGenFunction &CGF, const FunctionDecl *FD) {
43754375
}
43764376

43774377
llvm::Constant *calleePtr = EmitFunctionDeclPointer(CGF.CGM, FD);
4378-
return CGCallee::forDirect(calleePtr, FD);
4378+
return CGCallee::forDirect(calleePtr, GlobalDecl(FD));
43794379
}
43804380

43814381
CGCallee CodeGenFunction::EmitCallee(const Expr *E) {
@@ -4419,8 +4419,13 @@ CGCallee CodeGenFunction::EmitCallee(const Expr *E) {
44194419
calleePtr = EmitLValue(E).getPointer();
44204420
}
44214421
assert(functionType->isFunctionType());
4422-
CGCalleeInfo calleeInfo(functionType->getAs<FunctionProtoType>(),
4423-
E->getReferencedDeclOfCallee());
4422+
4423+
GlobalDecl GD;
4424+
if (const auto *VD =
4425+
dyn_cast_or_null<VarDecl>(E->getReferencedDeclOfCallee()))
4426+
GD = GlobalDecl(VD);
4427+
4428+
CGCalleeInfo calleeInfo(functionType->getAs<FunctionProtoType>(), GD);
44244429
CGCallee callee(calleeInfo, calleePtr);
44254430
return callee;
44264431
}
@@ -4605,7 +4610,8 @@ RValue CodeGenFunction::EmitCall(QualType CalleeType, const CGCallee &OrigCallee
46054610
assert(CalleeType->isFunctionPointerType() &&
46064611
"Call must have function pointer type!");
46074612

4608-
const Decl *TargetDecl = OrigCallee.getAbstractInfo().getCalleeDecl();
4613+
const Decl *TargetDecl =
4614+
OrigCallee.getAbstractInfo().getCalleeDecl().getDecl();
46094615

46104616
if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(TargetDecl))
46114617
// We can only guarantee that a function is called from the correct

0 commit comments

Comments
 (0)