Skip to content

Commit d8e4e50

Browse files
committed
Optimizer: add some SIL modification APIs to Context
* `insertFunctionArgument` * `BeginAccessInst.set(accessKind:)` * `erase(function:)`
1 parent de28cf0 commit d8e4e50

File tree

6 files changed

+44
-2
lines changed

6 files changed

+44
-2
lines changed

SwiftCompilerSources/Sources/Optimizer/PassManager/Context.swift

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -609,6 +609,14 @@ extension BasicBlock {
609609
return bridged.addFunctionArgument(type.bridged).argument as! FunctionArgument
610610
}
611611

612+
func insertFunctionArgument(atPosition: Int, type: Type, ownership: Ownership, decl: ValueDecl? = nil,
613+
_ context: some MutatingContext) -> FunctionArgument
614+
{
615+
context.notifyInstructionsChanged()
616+
return bridged.insertFunctionArgument(atPosition, type.bridged, ownership._bridged,
617+
(decl as Decl?).bridged).argument as! FunctionArgument
618+
}
619+
612620
func eraseArgument(at index: Int, _ context: some MutatingContext) {
613621
context.notifyInstructionsChanged()
614622
bridged.eraseArgument(index)
@@ -791,6 +799,14 @@ extension MarkDependenceInstruction {
791799
}
792800
}
793801

802+
extension BeginAccessInst {
803+
func set(accessKind: BeginAccessInst.AccessKind, context: some MutatingContext) {
804+
context.notifyInstructionsChanged()
805+
bridged.BeginAccess_setAccessKind(accessKind.rawValue)
806+
context.notifyInstructionChanged(self)
807+
}
808+
}
809+
794810
extension TermInst {
795811
func replaceBranchTarget(from fromBlock: BasicBlock, to toBlock: BasicBlock, _ context: some MutatingContext) {
796812
context.notifyBranchesChanged()

SwiftCompilerSources/Sources/Optimizer/PassManager/ModulePassContext.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,10 @@ struct ModulePassContext : Context, CustomStringConvertible {
206206
}
207207
}
208208

209+
func erase(function: Function) {
210+
_bridged.eraseFunction(function.bridged)
211+
}
212+
209213
func notifyFunctionTablesChanged() {
210214
_bridged.asNotificationHandler().notifyChanges(.functionTablesChanged)
211215
}

include/swift/SIL/SILBridging.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -811,6 +811,7 @@ struct BridgedInstruction {
811811
BRIDGED_INLINE SwiftInt BeginAccessInst_getAccessKind() const;
812812
BRIDGED_INLINE bool BeginAccessInst_isStatic() const;
813813
BRIDGED_INLINE bool BeginAccessInst_isUnsafe() const;
814+
BRIDGED_INLINE void BeginAccess_setAccessKind(SwiftInt accessKind) const;
814815
BRIDGED_INLINE bool CopyAddrInst_isTakeOfSrc() const;
815816
BRIDGED_INLINE bool CopyAddrInst_isInitializationOfDest() const;
816817
BRIDGED_INLINE void CopyAddrInst_setIsTakeOfSrc(bool isTakeOfSrc) const;
@@ -939,8 +940,11 @@ struct BridgedBasicBlock {
939940
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE OptionalBridgedInstruction getLastInst() const;
940941
BRIDGED_INLINE SwiftInt getNumArguments() const;
941942
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedArgument getArgument(SwiftInt index) const;
942-
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedArgument addBlockArgument(BridgedType type, BridgedValue::Ownership ownership) const;
943-
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedArgument addFunctionArgument(BridgedType type) const;
943+
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedArgument addBlockArgument(BridgedType type, BridgedValue::Ownership ownership) const;
944+
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedArgument addFunctionArgument(BridgedType type) const;
945+
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedArgument insertFunctionArgument(SwiftInt atPosition, BridgedType type,
946+
BridgedValue::Ownership ownership,
947+
OptionalBridgedDeclObj decl) const;
944948
BRIDGED_INLINE void eraseArgument(SwiftInt index) const;
945949
BRIDGED_INLINE void moveAllInstructionsToBegin(BridgedBasicBlock dest) const;
946950
BRIDGED_INLINE void moveAllInstructionsToEnd(BridgedBasicBlock dest) const;

include/swift/SIL/SILBridgingImpl.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1463,6 +1463,10 @@ bool BridgedInstruction::BeginAccessInst_isUnsafe() const {
14631463
return getAs<swift::BeginAccessInst>()->getEnforcement() == swift::SILAccessEnforcement::Unsafe;
14641464
}
14651465

1466+
void BridgedInstruction::BeginAccess_setAccessKind(SwiftInt accessKind) const {
1467+
getAs<swift::BeginAccessInst>()->setAccessKind((swift::SILAccessKind)accessKind);
1468+
}
1469+
14661470
bool BridgedInstruction::CopyAddrInst_isTakeOfSrc() const {
14671471
return getAs<swift::CopyAddrInst>()->isTakeOfSrc();
14681472
}
@@ -1809,6 +1813,14 @@ BridgedArgument BridgedBasicBlock::addFunctionArgument(BridgedType type) const {
18091813
return {unbridged()->createFunctionArgument(type.unbridged())};
18101814
}
18111815

1816+
BridgedArgument BridgedBasicBlock::insertFunctionArgument(SwiftInt atPosition, BridgedType type,
1817+
BridgedValue::Ownership ownership,
1818+
OptionalBridgedDeclObj decl) const {
1819+
return {unbridged()->insertFunctionArgument((unsigned)atPosition, type.unbridged(),
1820+
BridgedValue::unbridge(ownership),
1821+
decl.getAs<swift::ValueDecl>())};
1822+
}
1823+
18121824
void BridgedBasicBlock::eraseArgument(SwiftInt index) const {
18131825
unbridged()->eraseArgument(index);
18141826
}

include/swift/SILOptimizer/OptimizerBridging.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,7 @@ struct BridgedPassContext {
262262
void inlineFunction(BridgedInstruction apply, bool mandatoryInline) const;
263263
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedValue getSILUndef(BridgedType type) const;
264264
BRIDGED_INLINE bool eliminateDeadAllocations(BridgedFunction f) const;
265+
void eraseFunction(BridgedFunction function) const;
265266

266267
BRIDGED_INLINE bool shouldExpand(BridgedType type) const;
267268

lib/SILOptimizer/Utils/OptimizerBridging.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,11 @@ void BridgedPassContext::inlineFunction(BridgedInstruction apply, bool mandatory
156156
funcBuilder, deleter);
157157
}
158158

159+
void BridgedPassContext::eraseFunction(BridgedFunction function) const {
160+
invocation->getPassManager()->notifyWillDeleteFunction(function.getFunction());
161+
invocation->getPassManager()->getModule()->eraseFunction(function.getFunction());
162+
}
163+
159164
static const irgen::TypeInfo &getTypeInfoOfBuiltin(swift::SILType type, irgen::IRGenModule &IGM) {
160165
SILType lowered = IGM.getLoweredType(swift::Lowering::AbstractionPattern::getOpaque(), type.getASTType());
161166
return IGM.getTypeInfo(lowered);

0 commit comments

Comments
 (0)