Skip to content

Commit 87dc7d4

Browse files
s-barannikovinclyc
authored andcommitted
[clang][CodeGen] Factor out Swift ABI hooks (NFCI)
Swift calling conventions stands out in the way that they are lowered in mostly target-independent manner, with very few customization points. As such, swift-related methods of ABIInfo do not reference the rest of ABIInfo and vice versa. This change follows interface segregation principle; it removes dependency of SwiftABIInfo on ABIInfo. Targets must now implement SwiftABIInfo separately if they support Swift calling conventions. Almost all targets implemented `shouldPassIndirectly` the same way. This de-facto default implementation has been moved into the base class. `isSwiftErrorInRegister` used to be virtual, now it is not. It didn't accept any arguments which could have an effect on the returned value. This is now a static property of the target ABI. Reviewed By: rusyaev-roman, inclyc Differential Revision: https://reviews.llvm.org/D130394
1 parent dbff03b commit 87dc7d4

File tree

4 files changed

+143
-161
lines changed

4 files changed

+143
-161
lines changed

clang/lib/CodeGen/ABIInfo.h

Lines changed: 21 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ namespace CodeGen {
3333
class CGFunctionInfo;
3434
class CodeGenFunction;
3535
class CodeGenTypes;
36-
class SwiftABIInfo;
3736

3837
// FIXME: All of this stuff should be part of the target interface
3938
// somehow. It is currently here because it is not clear how to factor
@@ -44,18 +43,15 @@ namespace CodeGen {
4443
/// ABIInfo - Target specific hooks for defining how a type should be
4544
/// passed or returned from functions.
4645
class ABIInfo {
47-
public:
48-
CodeGen::CodeGenTypes &CGT;
4946
protected:
47+
CodeGen::CodeGenTypes &CGT;
5048
llvm::CallingConv::ID RuntimeCC;
5149
public:
5250
ABIInfo(CodeGen::CodeGenTypes &cgt)
5351
: CGT(cgt), RuntimeCC(llvm::CallingConv::C) {}
5452

5553
virtual ~ABIInfo();
5654

57-
virtual bool supportsSwift() const { return false; }
58-
5955
virtual bool allowBFloatArgsAndRet() const { return false; }
6056

6157
CodeGen::CGCXXABI &getCXXABI() const;
@@ -114,33 +110,33 @@ namespace CodeGen {
114110

115111
CodeGen::ABIArgInfo
116112
getNaturalAlignIndirectInReg(QualType Ty, bool Realign = false) const;
117-
118-
119113
};
120114

121-
/// A refining implementation of ABIInfo for targets that support swiftcall.
122-
///
123-
/// If we find ourselves wanting multiple such refinements, they'll probably
124-
/// be independent refinements, and we should probably find another way
125-
/// to do it than simple inheritance.
126-
class SwiftABIInfo : public ABIInfo {
127-
public:
128-
SwiftABIInfo(CodeGen::CodeGenTypes &cgt) : ABIInfo(cgt) {}
115+
/// Target specific hooks for defining how a type should be passed or returned
116+
/// from functions with one of the Swift calling conventions.
117+
class SwiftABIInfo {
118+
protected:
119+
CodeGenTypes &CGT;
120+
bool SwiftErrorInRegister;
129121

130-
bool supportsSwift() const final { return true; }
122+
public:
123+
SwiftABIInfo(CodeGen::CodeGenTypes &CGT, bool SwiftErrorInRegister)
124+
: CGT(CGT), SwiftErrorInRegister(SwiftErrorInRegister) {}
131125

132-
virtual bool shouldPassIndirectlyForSwift(ArrayRef<llvm::Type*> types,
133-
bool asReturnValue) const = 0;
126+
virtual ~SwiftABIInfo();
134127

135-
virtual bool isLegalVectorTypeForSwift(CharUnits totalSize,
136-
llvm::Type *eltTy,
137-
unsigned elts) const;
128+
/// Returns true if an aggregate which expands to the given type sequence
129+
/// should be passed / returned indirectly.
130+
virtual bool shouldPassIndirectly(ArrayRef<llvm::Type *> ComponentTys,
131+
bool AsReturnValue) const;
138132

139-
virtual bool isSwiftErrorInRegister() const = 0;
133+
/// Returns true if the given vector type is legal from Swift's calling
134+
/// convention perspective.
135+
virtual bool isLegalVectorType(CharUnits VectorSize, llvm::Type *EltTy,
136+
unsigned NumElts) const;
140137

141-
static bool classof(const ABIInfo *info) {
142-
return info->supportsSwift();
143-
}
138+
/// Returns true if swifterror is lowered to a register by the target ABI.
139+
bool isSwiftErrorInRegister() const { return SwiftErrorInRegister; };
144140
};
145141
} // end namespace CodeGen
146142
} // end namespace clang

clang/lib/CodeGen/SwiftCallingConv.cpp

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ using namespace CodeGen;
2121
using namespace swiftcall;
2222

2323
static const SwiftABIInfo &getSwiftABIInfo(CodeGenModule &CGM) {
24-
return cast<SwiftABIInfo>(CGM.getTargetCodeGenInfo().getABIInfo());
24+
return CGM.getTargetCodeGenInfo().getSwiftABIInfo();
2525
}
2626

2727
static bool isPowerOf2(unsigned n) {
@@ -631,25 +631,22 @@ bool SwiftAggLowering::shouldPassIndirectly(bool asReturnValue) const {
631631

632632
// Avoid copying the array of types when there's just a single element.
633633
if (Entries.size() == 1) {
634-
return getSwiftABIInfo(CGM).shouldPassIndirectlyForSwift(
635-
Entries.back().Type,
636-
asReturnValue);
634+
return getSwiftABIInfo(CGM).shouldPassIndirectly(Entries.back().Type,
635+
asReturnValue);
637636
}
638637

639638
SmallVector<llvm::Type*, 8> componentTys;
640639
componentTys.reserve(Entries.size());
641640
for (auto &entry : Entries) {
642641
componentTys.push_back(entry.Type);
643642
}
644-
return getSwiftABIInfo(CGM).shouldPassIndirectlyForSwift(componentTys,
645-
asReturnValue);
643+
return getSwiftABIInfo(CGM).shouldPassIndirectly(componentTys, asReturnValue);
646644
}
647645

648646
bool swiftcall::shouldPassIndirectly(CodeGenModule &CGM,
649647
ArrayRef<llvm::Type*> componentTys,
650648
bool asReturnValue) {
651-
return getSwiftABIInfo(CGM).shouldPassIndirectlyForSwift(componentTys,
652-
asReturnValue);
649+
return getSwiftABIInfo(CGM).shouldPassIndirectly(componentTys, asReturnValue);
653650
}
654651

655652
CharUnits swiftcall::getMaximumVoluntaryIntegerSize(CodeGenModule &CGM) {
@@ -699,8 +696,7 @@ bool swiftcall::isLegalVectorType(CodeGenModule &CGM, CharUnits vectorSize,
699696
bool swiftcall::isLegalVectorType(CodeGenModule &CGM, CharUnits vectorSize,
700697
llvm::Type *eltTy, unsigned numElts) {
701698
assert(numElts > 1 && "illegal vector length");
702-
return getSwiftABIInfo(CGM)
703-
.isLegalVectorTypeForSwift(vectorSize, eltTy, numElts);
699+
return getSwiftABIInfo(CGM).isLegalVectorType(vectorSize, eltTy, numElts);
704700
}
705701

706702
std::pair<llvm::Type*, unsigned>

0 commit comments

Comments
 (0)