Skip to content

Add cxx_virtual_method instruction. #26658

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions include/swift/SIL/SILBuilder.h
Original file line number Diff line number Diff line change
Expand Up @@ -1472,6 +1472,14 @@ class SILBuilder {
getSILDebugLocation(Loc), Operand, Member, MethodTy));
}

CXXVirtualMethodInst *createCXXVirtualMethod(SILLocation Loc,
SILValue Operand,
SILDeclRef Member,
SILType MethodTy) {
return insert(CXXVirtualMethodInst::create(getSILDebugLocation(Loc),
Operand, Member, MethodTy));
}

WitnessMethodInst *createWitnessMethod(SILLocation Loc, CanType LookupTy,
ProtocolConformanceRef Conformance,
SILDeclRef Member, SILType MethodTy) {
Expand Down
11 changes: 11 additions & 0 deletions include/swift/SIL/SILCloner.h
Original file line number Diff line number Diff line change
Expand Up @@ -2023,6 +2023,17 @@ SILCloner<ImplClass>::visitObjCSuperMethodInst(ObjCSuperMethodInst *Inst) {
Inst->getMember(), Inst->getType()));
}

template <typename ImplClass>
void SILCloner<ImplClass>::visitCXXVirtualMethodInst(
CXXVirtualMethodInst *Inst) {
getBuilder().setCurrentDebugScope(getOpScope(Inst->getDebugScope()));
recordClonedInstruction(
Inst,
getBuilder().createCXXVirtualMethod(
getOpLocation(Inst->getLoc()), getOpValue(Inst->getOperand()),
Inst->getMember(), getOpType(Inst->getExtractedMethod()->getType())));
}

template<typename ImplClass>
void
SILCloner<ImplClass>::visitWitnessMethodInst(WitnessMethodInst *Inst) {
Expand Down
60 changes: 60 additions & 0 deletions include/swift/SIL/SILInstruction.h
Original file line number Diff line number Diff line change
Expand Up @@ -5881,6 +5881,66 @@ class ObjCSuperMethodInst
: UnaryInstructionBase(DebugLoc, Operand, Ty, Member) {}
};

/// This is either the extracted method or the adjusted this ptr that
/// is the result of a CXXVirtualMethodInst.
class CXXVirtualMethodInst;
class CXXVirtualMethodResult final : public MultipleValueInstructionResult {
public:
CXXVirtualMethodResult(unsigned index, SILType type,
ValueOwnershipKind ownershipKind)
: MultipleValueInstructionResult(ValueKind::CXXVirtualMethodResult, index,
type, ownershipKind) {}

CXXVirtualMethodInst *getParent(); // inline below
const CXXVirtualMethodInst *getParent() const {
return const_cast<CXXVirtualMethodResult *>(this)->getParent();
}

bool isExtractedMethod() const { return getIndex() == 0; }
bool isAdjustedThisPtr() const { return getIndex() == 1; }

static bool classof(const SILNode *N) {
return N->getKind() == SILNodeKind::CXXVirtualMethodResult;
}
};

/// Represents looking up a virtual method from a c++ virtual method table and
/// adjusting the this pointer.
class CXXVirtualMethodInst final
: public UnaryInstructionBase<SILInstructionKind::CXXVirtualMethodInst,
MultipleValueInstruction>,
public MultipleValueInstructionTrailingObjects<CXXVirtualMethodInst,
CXXVirtualMethodResult> {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if there's a better way to have exactly two results than this. My SIL isn't strong enough. @gottesmm?

SILDeclRef Member;
friend SILBuilder;

template <class, class...> friend class llvm::TrailingObjects;
using MultipleValueInstructionTrailingObjects::numTrailingObjects;

using MultipleValueInstructionTrailingObjects::getTrailingObjects;

CXXVirtualMethodInst(SILDebugLocation debugLoc, SILValue ThisPtr,
SILDeclRef Member, SILType Ty);

static CXXVirtualMethodInst *create(SILDebugLocation debugLoc,
SILValue ThisPtr, SILDeclRef Member,
SILType Ty);

public:
using MultipleValueInstructionTrailingObjects::totalSizeToAlloc;

SILDeclRef getMember() const { return Member; }

SILValue getExtractedMethod() const { return &getAllResultsBuffer().front(); }

SILValue getAdjustedThisPtr() const { return &getAllResultsBuffer().back(); }
};

inline CXXVirtualMethodInst *CXXVirtualMethodResult::getParent() {
auto *Parent = MultipleValueInstructionResult::getParent();
return cast<CXXVirtualMethodInst>(Parent);
}

/// WitnessMethodInst - Given a type, a protocol conformance,
/// and a protocol method constant, extracts the implementation of that method
/// for the type.
Expand Down
11 changes: 7 additions & 4 deletions include/swift/SIL/SILNodes.def
Original file line number Diff line number Diff line change
Expand Up @@ -415,7 +415,8 @@ ABSTRACT_VALUE(MultipleValueInstructionResult, ValueBase)
MULTIPLE_VALUE_INST_RESULT(BeginApplyResult, MultipleValueInstructionResult)
MULTIPLE_VALUE_INST_RESULT(DestructureStructResult, MultipleValueInstructionResult)
MULTIPLE_VALUE_INST_RESULT(DestructureTupleResult, MultipleValueInstructionResult)
VALUE_RANGE(MultipleValueInstructionResult, BeginApplyResult, DestructureTupleResult)
MULTIPLE_VALUE_INST_RESULT(CXXVirtualMethodResult, MultipleValueInstructionResult)
VALUE_RANGE(MultipleValueInstructionResult, BeginApplyResult, CXXVirtualMethodResult)

VALUE(SILUndef, ValueBase)

Expand Down Expand Up @@ -855,10 +856,12 @@ MULTIPLE_VALUE_INST(DestructureStructInst, destructure_struct,
MultipleValueInstruction, None, DoesNotRelease)
MULTIPLE_VALUE_INST(DestructureTupleInst, destructure_tuple,
MultipleValueInstruction, None, DoesNotRelease)
INST_RANGE(MultipleValueInstruction, BeginApplyInst, DestructureTupleInst)
MULTIPLE_VALUE_INST(CXXVirtualMethodInst, cxx_virtual_method,
MultipleValueInstruction, None, DoesNotRelease)
INST_RANGE(MultipleValueInstruction, BeginApplyInst, CXXVirtualMethodInst)

NODE_RANGE(SILInstruction, AllocStackInst, DestructureTupleInst)
NODE_RANGE(SILNode, SILPhiArgument, DestructureTupleInst)
NODE_RANGE(SILInstruction, AllocStackInst, CXXVirtualMethodInst)
NODE_RANGE(SILNode, SILPhiArgument, CXXVirtualMethodInst)

#undef SINGLE_VALUE_INST_RANGE
#undef INST_RANGE
Expand Down
20 changes: 16 additions & 4 deletions lib/IRGen/GenCall.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ namespace {
/// function type.
void expandCoroutineContinuationType();

Signature getSignature();
Signature getSignature(llvm::FunctionType *llvmType = nullptr);

private:
void expand(SILParameterInfo param);
Expand Down Expand Up @@ -1424,10 +1424,11 @@ void SignatureExpansion::expandCoroutineContinuationType() {
expandCoroutineContinuationParameters();
}

Signature SignatureExpansion::getSignature() {
Signature SignatureExpansion::getSignature(llvm::FunctionType *llvmType) {
// Create the appropriate LLVM type.
llvm::FunctionType *llvmType =
llvm::FunctionType::get(ResultIRType, ParamIRTypes, /*variadic*/ false);
if (!llvmType)
llvmType =
llvm::FunctionType::get(ResultIRType, ParamIRTypes, /*variadic*/ false);

assert((ForeignInfo.ClangInfo != nullptr) ==
(FnType->getLanguage() == SILFunctionLanguage::C) &&
Expand Down Expand Up @@ -1470,6 +1471,17 @@ Signature Signature::forCoroutineContinuation(IRGenModule &IGM,
return expansion.getSignature();
}

Signature Signature::forCXXMethod(IRGenModule &IGM,
const clang::CXXMethodDecl *decl,
CanSILFunctionType fnType) {
SignatureExpansion expansion(IGM, fnType);
expansion.ForeignInfo.ClangInfo =
&clang::CodeGen::arrangeCXXMethodDeclaration(IGM.getClangCGM(), decl);
llvm::FunctionType *Ty = clang::CodeGen::getFunctionType(
IGM.getClangCGM(), *expansion.ForeignInfo.ClangInfo);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rjmccall or @jckarter should probably verify this rather than me.

return expansion.getSignature(Ty);
}

void irgen::extractScalarResults(IRGenFunction &IGF, llvm::Type *bodyType,
llvm::Value *call, Explosion &out) {
assert(!bodyType->isVoidTy() && "Unexpected void result type!");
Expand Down
63 changes: 43 additions & 20 deletions lib/IRGen/IRGenSIL.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,41 +16,43 @@
//===----------------------------------------------------------------------===//

#define DEBUG_TYPE "irgensil"
#include "llvm/IR/DIBuilder.h"
#include "llvm/IR/Function.h"
#include "llvm/IR/Module.h"
#include "llvm/IR/Instructions.h"
#include "llvm/IR/IntrinsicInst.h"
#include "llvm/IR/InlineAsm.h"
#include "llvm/IR/Intrinsics.h"
#include "llvm/ADT/MapVector.h"
#include "llvm/ADT/SmallBitVector.h"
#include "llvm/ADT/TinyPtrVector.h"
#include "llvm/Support/SaveAndRestore.h"
#include "llvm/Support/Debug.h"
#include "llvm/Transforms/Utils/Local.h"
#include "clang/AST/ASTContext.h"
#include "clang/Basic/TargetInfo.h"
#include "swift/Basic/ExternalUnion.h"
#include "swift/Basic/Range.h"
#include "swift/Basic/STLExtras.h"
#include "swift/AST/ASTContext.h"
#include "swift/AST/IRGenOptions.h"
#include "swift/AST/Pattern.h"
#include "swift/AST/ParameterList.h"
#include "swift/AST/Pattern.h"
#include "swift/AST/SubstitutionMap.h"
#include "swift/AST/Types.h"
#include "swift/Basic/ExternalUnion.h"
#include "swift/Basic/Range.h"
#include "swift/Basic/STLExtras.h"
#include "swift/SIL/ApplySite.h"
#include "swift/SIL/Dominance.h"
#include "swift/SIL/InstructionUtils.h"
#include "swift/SIL/PrettyStackTrace.h"
#include "swift/SIL/SILDebugScope.h"
#include "swift/SIL/SILDeclRef.h"
#include "swift/SIL/SILLinkage.h"
#include "swift/SIL/SILModule.h"
#include "swift/SIL/SILType.h"
#include "swift/SIL/SILVisitor.h"
#include "swift/SIL/InstructionUtils.h"
#include "clang/AST/ASTContext.h"
#include "clang/AST/DeclCXX.h"
#include "clang/Basic/TargetInfo.h"
#include "clang/CodeGen/CodeGenABITypes.h"
#include "clang/CodeGen/SwiftCallingConv.h"
#include "llvm/ADT/MapVector.h"
#include "llvm/ADT/SmallBitVector.h"
#include "llvm/ADT/TinyPtrVector.h"
#include "llvm/IR/DIBuilder.h"
#include "llvm/IR/Function.h"
#include "llvm/IR/InlineAsm.h"
#include "llvm/IR/Instructions.h"
#include "llvm/IR/IntrinsicInst.h"
#include "llvm/IR/Intrinsics.h"
#include "llvm/IR/Module.h"
#include "llvm/Support/Debug.h"
#include "llvm/Support/SaveAndRestore.h"
#include "llvm/Transforms/Utils/Local.h"

#include "CallEmission.h"
#include "Explosion.h"
Expand Down Expand Up @@ -990,6 +992,7 @@ class IRGenSILFunction :
void visitSuperMethodInst(SuperMethodInst *i);
void visitObjCMethodInst(ObjCMethodInst *i);
void visitObjCSuperMethodInst(ObjCSuperMethodInst *i);
void visitCXXVirtualMethodInst(CXXVirtualMethodInst *i);
void visitWitnessMethodInst(WitnessMethodInst *i);

void visitAllocValueBufferInst(AllocValueBufferInst *i);
Expand Down Expand Up @@ -5589,6 +5592,26 @@ void IRGenSILFunction::visitObjCMethodInst(swift::ObjCMethodInst *i) {
setLoweredObjCMethod(i, i->getMember());
}

void IRGenSILFunction::visitCXXVirtualMethodInst(CXXVirtualMethodInst *i) {
auto *cxxMethod = cast<clang::CXXMethodDecl>(
i->getMember().getAbstractFunctionDecl()->getClangDecl());

auto signature = Signature::forCXXMethod(
IGM, cxxMethod,
i->getExtractedMethod()->getType().castTo<SILFunctionType>());

auto selfAddress = getLoweredAddress(i->getOperand());
llvm::Value *self = selfAddress.getAddress();

auto *result = clang::CodeGen::swiftcall::lowerCXXVirtualMethodDeclReference(
IGM.getClangCGM(), cxxMethod, self,
selfAddress.getAlignment().asCharUnits(), signature.getType(), &Builder);

setLoweredValue(i->getExtractedMethod(), FunctionPointer(result, signature));
selfAddress = Address(self, selfAddress.getAlignment());
setLoweredAddress(i->getAdjustedThisPtr(), selfAddress);
}

void IRGenModule::emitSILStaticInitializers() {
SmallVector<SILFunction *, 8> StaticInitializers;
for (SILGlobalVariable &Global : getSILModule().getSILGlobals()) {
Expand Down
6 changes: 6 additions & 0 deletions lib/IRGen/Signature.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ namespace clang {
namespace CodeGen {
class CGFunctionInfo;
}
class CXXMethodDecl;
}

namespace swift {
Expand Down Expand Up @@ -126,6 +127,11 @@ class Signature {
static Signature forCoroutineContinuation(IRGenModule &IGM,
CanSILFunctionType coroType);

/// Compute the singature of a C++ method.
static Signature forCXXMethod(IRGenModule &IGM,
const clang::CXXMethodDecl *decl,
CanSILFunctionType fnType);

llvm::FunctionType *getType() const {
assert(isValid());
return Type;
Expand Down
4 changes: 4 additions & 0 deletions lib/ParseSIL/ParseSIL.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4243,6 +4243,7 @@ bool SILParser::parseSILInstruction(SILBuilder &B) {
case SILInstructionKind::ClassMethodInst:
case SILInstructionKind::SuperMethodInst:
case SILInstructionKind::ObjCMethodInst:
case SILInstructionKind::CXXVirtualMethodInst:
case SILInstructionKind::ObjCSuperMethodInst: {
SILDeclRef Member;
SILType MethodTy;
Expand Down Expand Up @@ -4274,6 +4275,9 @@ bool SILParser::parseSILInstruction(SILBuilder &B) {
case SILInstructionKind::ObjCSuperMethodInst:
ResultVal = B.createObjCSuperMethod(InstLoc, Val, Member, MethodTy);
break;
case SILInstructionKind::CXXVirtualMethodInst:
ResultVal = B.createCXXVirtualMethod(InstLoc, Val, Member, MethodTy);
break;
}
break;
}
Expand Down
1 change: 1 addition & 0 deletions lib/SIL/OperandOwnership.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,7 @@ ACCEPTS_ANY_NONTRIVIAL_OWNERSHIP_OR_METATYPE(MustBeLive, ClassMethod)
ACCEPTS_ANY_NONTRIVIAL_OWNERSHIP_OR_METATYPE(MustBeLive, ObjCMethod)
ACCEPTS_ANY_NONTRIVIAL_OWNERSHIP_OR_METATYPE(MustBeLive, ObjCSuperMethod)
ACCEPTS_ANY_NONTRIVIAL_OWNERSHIP_OR_METATYPE(MustBeLive, SuperMethod)
ACCEPTS_ANY_NONTRIVIAL_OWNERSHIP_OR_METATYPE(MustBeLive, CXXVirtualMethod)
#undef ACCEPTS_ANY_NONTRIVIAL_OWNERSHIP_OR_METATYPE

// Trivial if trivial typed, otherwise must accept owned?
Expand Down
16 changes: 12 additions & 4 deletions lib/SIL/SILDeclRef.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,21 @@
//===----------------------------------------------------------------------===//

#include "swift/SIL/SILDeclRef.h"
#include "swift/SIL/SILLocation.h"
#include "swift/AST/AnyFunctionRef.h"
#include "swift/AST/ASTContext.h"
#include "swift/AST/ASTMangler.h"
#include "swift/AST/AnyFunctionRef.h"
#include "swift/AST/Initializer.h"
#include "swift/AST/ParameterList.h"
#include "swift/ClangImporter/ClangImporter.h"
#include "swift/ClangImporter/ClangModule.h"
#include "swift/SIL/SILLinkage.h"
#include "llvm/Support/Compiler.h"
#include "llvm/Support/raw_ostream.h"
#include "swift/SIL/SILLocation.h"
#include "clang/AST/Attr.h"
#include "clang/AST/Decl.h"
#include "clang/AST/DeclCXX.h"
#include "clang/AST/DeclObjC.h"
#include "llvm/Support/Compiler.h"
#include "llvm/Support/raw_ostream.h"
using namespace swift;

/// Get the method dispatch mechanism for a method.
Expand All @@ -34,6 +35,13 @@ swift::getMethodDispatch(AbstractFunctionDecl *method) {
if (method->hasForcedStaticDispatch())
return MethodDispatch::Static;

if (auto *mdecl =
dyn_cast_or_null<clang::CXXMethodDecl>(method->getClangDecl())) {
if (mdecl->isVirtual()) {
return MethodDispatch::Class;
}
}

// Import-as-member declarations are always statically referenced.
if (method->isImportAsMember())
return MethodDispatch::Static;
Expand Down
22 changes: 22 additions & 0 deletions lib/SIL/SILInstructions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1715,6 +1715,28 @@ ObjCMethodInst::create(SILDebugLocation DebugLoc, SILValue Operand,
Member, Ty);
}

CXXVirtualMethodInst::CXXVirtualMethodInst(SILDebugLocation DebugLoc,
SILValue Operand, SILDeclRef Member,
SILType Ty)
: UnaryInstructionBase(DebugLoc, Operand),
MultipleValueInstructionTrailingObjects(
this, {Ty, Operand->getType()},
{ /* extracted method */ ValueOwnershipKind::Any,
/* adjusted this */ ValueOwnershipKind::Any}),
Member(Member) {}

CXXVirtualMethodInst *CXXVirtualMethodInst::create(SILDebugLocation DebugLoc,
SILValue Operand,
SILDeclRef Member,
SILType Ty) {
SILModule *Mod = Operand->getModule();
unsigned size =
totalSizeToAlloc<MultipleValueInstruction *, CXXVirtualMethodResult>(1,
2);
void *Buffer = Mod->allocateInst(size, alignof(CXXVirtualMethodInst));
return ::new (Buffer) CXXVirtualMethodInst(DebugLoc, Operand, Member, Ty);
}

InitExistentialAddrInst *InitExistentialAddrInst::create(
SILDebugLocation Loc, SILValue Existential, CanType ConcreteType,
SILType ConcreteLoweredType, ArrayRef<ProtocolConformanceRef> Conformances,
Expand Down
6 changes: 6 additions & 0 deletions lib/SIL/SILPrinter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1693,6 +1693,12 @@ class SILPrinter : public SILInstructionVisitor<SILPrinter> {
*this << ", ";
*this << AMI->getType();
}
void visitCXXVirtualMethodInst(CXXVirtualMethodInst *CXXVMI) {
*this << getIDAndType(CXXVMI->getOperand()) << ", " << CXXVMI->getMember();
*this << " : " << CXXVMI->getMember().getDecl()->getInterfaceType();
*this << ", ";
*this << CXXVMI->getExtractedMethod()->getType();
}
void visitWitnessMethodInst(WitnessMethodInst *WMI) {
PrintOptions QualifiedSILTypeOptions =
PrintOptions::printQualifiedSILType();
Expand Down
Loading