Skip to content

Commit e0f60d8

Browse files
committed
libswift: bridge RCIdentityAnalysis code
1 parent b9cc26b commit e0f60d8

File tree

5 files changed

+88
-11
lines changed

5 files changed

+88
-11
lines changed

include/swift/SIL/SILBridging.h

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,10 @@ typedef struct {
8686
SwiftObject obj;
8787
} BridgedFunction;
8888

89+
typedef struct {
90+
OptionalSwiftObject obj;
91+
} OptionalBridgedFunction;
92+
8993
typedef struct {
9094
SwiftObject obj;
9195
} BridgedGlobalVar;
@@ -136,6 +140,12 @@ typedef enum {
136140
MayHaveSideEffectsBehavior
137141
} BridgedMemoryBehavior;
138142

143+
// AST bridging
144+
145+
typedef struct {
146+
const void * _Nonnull op;
147+
} BridgedSubstitutionMap;
148+
139149
typedef enum {
140150
UnknownBuiltin = 0,
141151
#define BUILTIN(Id, Name, Attrs) Id##Builtin,
@@ -189,6 +199,7 @@ BridgedType SILValue_getType(BridgedValue value);
189199

190200
SwiftInt SILType_isAddress(BridgedType);
191201
SwiftInt SILType_isTrivial(BridgedType, BridgedFunction);
202+
BridgedSubstitutionMap SILType_getContextSubstitutionMap(BridgedType);
192203

193204
BridgedBasicBlock SILArgument_getParent(BridgedArgument argument);
194205

@@ -236,7 +247,7 @@ BridgedInstruction SILBuilder_createBuiltinBinaryFunction(
236247
BridgedLocation loc, BridgedStringRef name,
237248
BridgedType operandType, BridgedType resultType, BridgedValueArray arguments);
238249
BridgedInstruction SILBuilder_createCondFail(BridgedInstruction insertionPoint,
239-
BridgedLocation loc, BridgedValue condition, BridgedStringRef messge);
250+
BridgedLocation loc, BridgedValue condition, BridgedStringRef message);
240251
BridgedInstruction SILBuilder_createIntegerLiteral(BridgedInstruction insertionPoint,
241252
BridgedLocation loc, BridgedType type, SwiftInt value);
242253
BridgedInstruction SILBuilder_createDeallocStackRef(BridgedInstruction insertionPoint,

include/swift/SILOptimizer/OptimizerBridging.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,14 @@ enum {
6969
BridgedSlabCapacity = 64 * sizeof(uintptr_t)
7070
};
7171

72+
typedef struct {
73+
void * _Nullable rcia;
74+
} BridgedRCIdentityAnalysis;
75+
76+
typedef struct {
77+
void * _Nonnull functionInfo;
78+
} BridgedRCIdentityFunctionInfo;
79+
7280
typedef void (* _Nonnull BridgedFunctionPassRunFn)(BridgedFunctionPassCtxt);
7381
typedef void (* _Nonnull BridgedInstructionPassRunFn)(BridgedInstructionPassCtxt);
7482

@@ -133,6 +141,18 @@ void BasicBlockSet_erase(BridgedBasicBlockSet set, BridgedBasicBlock block);
133141
BridgedFunction BasicBlockSet_getFunction(BridgedBasicBlockSet set);
134142

135143
void AllocRefInstBase_setIsStackAllocatable(BridgedInstruction arb);
144+
BridgedRCIdentityAnalysis
145+
PassContext_getRCIdentityAnalysis(BridgedPassContext context);
146+
147+
BridgedRCIdentityFunctionInfo
148+
RCIdentityAnalysis_getFunctionInfo(BridgedRCIdentityAnalysis bridgedAnalysis,
149+
BridgedFunction function);
150+
151+
BridgedValue RCIdentityFunctionInfo_getRCIdentityRoot(
152+
BridgedRCIdentityFunctionInfo bridgedInfo, BridgedValue value);
153+
154+
OptionalBridgedFunction PassContext_getDestructor(BridgedPassContext context,
155+
BridgedType type);
136156

137157
#ifdef __cplusplus
138158
} // extern "C"

lib/SILOptimizer/Analysis/RCIdentityAnalysis.cpp

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,10 @@
1212

1313
#include "swift/SILOptimizer/Analysis/RCIdentityAnalysis.h"
1414
#include "swift/SILOptimizer/Analysis/DominanceAnalysis.h"
15-
#include "swift/SIL/SILInstruction.h"
1615
#include "swift/SIL/DynamicCasts.h"
16+
#include "swift/SIL/SILBridgingUtils.h"
17+
#include "swift/SIL/SILInstruction.h"
18+
#include "swift/SILOptimizer/OptimizerBridging.h"
1719
#include "llvm/Support/CommandLine.h"
1820

1921
using namespace swift;
@@ -605,3 +607,22 @@ void RCIdentityAnalysis::initialize(SILPassManager *PM) {
605607
SILAnalysis *swift::createRCIdentityAnalysis(SILModule *M) {
606608
return new RCIdentityAnalysis(M);
607609
}
610+
611+
//===----------------------------------------------------------------------===//
612+
// Swift Bridging
613+
//===----------------------------------------------------------------------===//
614+
615+
BridgedRCIdentityFunctionInfo
616+
RCIdentityAnalysis_getFunctionInfo(BridgedRCIdentityAnalysis bridgedAnalysis,
617+
BridgedFunction function) {
618+
RCIdentityAnalysis *rcia =
619+
static_cast<RCIdentityAnalysis *>(bridgedAnalysis.rcia);
620+
return {rcia->get(castToFunction(function))};
621+
}
622+
623+
BridgedValue RCIdentityFunctionInfo_getRCIdentityRoot(
624+
BridgedRCIdentityFunctionInfo bridgedInfo, BridgedValue value) {
625+
RCIdentityFunctionInfo *info =
626+
static_cast<RCIdentityFunctionInfo *>(bridgedInfo.functionInfo);
627+
return {info->getRCIdentityRoot(castToSILValue(value))};
628+
}

lib/SILOptimizer/PassManager/PassManager.cpp

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include "swift/SILOptimizer/Analysis/DeadEndBlocksAnalysis.h"
2525
#include "swift/SILOptimizer/Analysis/FunctionOrder.h"
2626
#include "swift/SILOptimizer/Analysis/DominanceAnalysis.h"
27+
#include "swift/SILOptimizer/Analysis/RCIdentityAnalysis.h"
2728
#include "swift/SILOptimizer/OptimizerBridging.h"
2829
#include "swift/SILOptimizer/PassManager/PrettyStackTrace.h"
2930
#include "swift/SILOptimizer/PassManager/Transforms.h"
@@ -1222,7 +1223,7 @@ void PassContext_notifyChanges(BridgedPassContext passContext,
12221223
}
12231224

12241225
void PassContext_eraseInstruction(BridgedPassContext passContext,
1225-
BridgedInstruction inst) {
1226+
BridgedInstruction inst) {
12261227
castToPassInvocation(passContext)->eraseInstruction(castToInst(inst));
12271228
}
12281229

@@ -1243,8 +1244,8 @@ void PassContext_fixStackNesting(BridgedPassContext passContext,
12431244
SwiftInt PassContext_isSwift51RuntimeAvailable(BridgedPassContext context) {
12441245
SILPassManager *pm = castToPassInvocation(context)->getPassManager();
12451246
ASTContext &ctxt = pm->getModule()->getASTContext();
1246-
return AvailabilityContext::forDeploymentTarget(ctxt).
1247-
isContainedIn(ctxt.getSwift51Availability());
1247+
return AvailabilityContext::forDeploymentTarget(ctxt).isContainedIn(
1248+
ctxt.getSwift51Availability());
12481249
}
12491250

12501251
BridgedAliasAnalysis PassContext_getAliasAnalysis(BridgedPassContext context) {
@@ -1253,7 +1254,8 @@ BridgedAliasAnalysis PassContext_getAliasAnalysis(BridgedPassContext context) {
12531254
return {pm->getAnalysis<AliasAnalysis>(invocation->getFunction())};
12541255
}
12551256

1256-
BridgedCalleeAnalysis PassContext_getCalleeAnalysis(BridgedPassContext context) {
1257+
BridgedCalleeAnalysis
1258+
PassContext_getCalleeAnalysis(BridgedPassContext context) {
12571259
SILPassManager *pm = castToPassInvocation(context)->getPassManager();
12581260
return {pm->getAnalysis<BasicCalleeAnalysis>()};
12591261
}
@@ -1318,4 +1320,32 @@ BridgedFunction BasicBlockSet_getFunction(BridgedBasicBlockSet set) {
13181320

13191321
void AllocRefInstBase_setIsStackAllocatable(BridgedInstruction arb) {
13201322
castToInst<AllocRefInstBase>(arb)->setStackAllocatable();
1323+
BridgedRCIdentityAnalysis
1324+
PassContext_getRCIdentityAnalysis(BridgedPassContext context) {
1325+
SILPassManager *pm = castToPassInvocation(context)->getPassManager();
1326+
return {pm->getAnalysis<RCIdentityAnalysis>()};
1327+
}
1328+
1329+
OptionalBridgedFunction PassContext_getDestructor(BridgedPassContext context,
1330+
BridgedType type) {
1331+
auto *cd = castToSILType(type).getClassOrBoundGenericClass();
1332+
assert(cd && "no class type allocated with alloc_ref");
1333+
1334+
auto *pm = castToPassInvocation(context)->getPassManager();
1335+
// Find the destructor of the type.
1336+
auto *destructor = cd->getDestructor();
1337+
SILDeclRef deallocRef(destructor, SILDeclRef::Kind::Deallocator);
1338+
1339+
return {pm->getModule()->lookUpFunction(deallocRef)};
1340+
}
1341+
1342+
BridgedSubstitutionMap
1343+
PassContext_getContextSubstitutionMap(BridgedPassContext context,
1344+
BridgedType bridgedType) {
1345+
auto type = castToSILType(bridgedType);
1346+
auto *ntd = type.getASTType()->getAnyNominal();
1347+
auto *pm = castToPassInvocation(context)->getPassManager();
1348+
auto *m = pm->getModule()->getSwiftModule();
1349+
1350+
return {type.getASTType()->getContextSubstitutionMap(m, ntd).getOpaqueValue()};
13211351
}

lib/SILOptimizer/Transforms/ReleaseDevirtualizer.cpp

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -133,15 +133,10 @@ bool ReleaseDevirtualizer::createDeallocCall(SILType AllocType,
133133
SILFunction *Dealloc = M.lookUpFunction(DeallocRef);
134134
if (!Dealloc)
135135
return false;
136-
TypeExpansionContext context(*ReleaseInst->getFunction());
137-
CanSILFunctionType DeallocType =
138-
Dealloc->getLoweredFunctionTypeInContext(context);
139136
auto *NTD = AllocType.getASTType()->getAnyNominal();
140137
auto AllocSubMap = AllocType.getASTType()
141138
->getContextSubstitutionMap(M.getSwiftModule(), NTD);
142139

143-
DeallocType = DeallocType->substGenericArgs(M, AllocSubMap, context);
144-
145140
SILBuilder B(ReleaseInst);
146141
if (object->getType() != AllocType)
147142
object = B.createUncheckedRefCast(ReleaseInst->getLoc(), object, AllocType);

0 commit comments

Comments
 (0)