Skip to content

Commit

Permalink
Merge pull request #67395 from eeckstein/redundant-load-elimination
Browse files Browse the repository at this point in the history
Optimizer: re-implement the RedundantLoadElimination pass in Swift
  • Loading branch information
eeckstein authored Jul 21, 2023
2 parents 627175f + 29246fd commit b9d0aa3
Show file tree
Hide file tree
Showing 51 changed files with 1,290 additions and 3,887 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ struct AliasAnalysis {
static func register() {
BridgedAliasAnalysis.registerAnalysis(
// getMemEffectsFn
{ (bridgedCtxt: BridgedPassContext, bridgedVal: BridgedValue, bridgedInst: BridgedInstruction) -> swift.MemoryBehavior in
{ (bridgedCtxt: BridgedPassContext, bridgedVal: BridgedValue, bridgedInst: BridgedInstruction, complexityBudget: Int) -> swift.MemoryBehavior in
let context = FunctionPassContext(_bridged: bridgedCtxt)
let inst = bridgedInst.instruction
let val = bridgedVal.value
Expand All @@ -47,7 +47,8 @@ struct AliasAnalysis {
case let builtin as BuiltinInst:
return getMemoryEffect(ofBuiltin: builtin, for: val, path: path, context).bridged
default:
if val.at(path).isEscaping(using: EscapesToInstructionVisitor(target: inst, isAddress: true), context) {
if val.at(path).isEscaping(using: EscapesToInstructionVisitor(target: inst, isAddress: true),
complexityBudget: complexityBudget, context) {
return .MayReadWrite
}
return .None
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ swift_compiler_sources(Optimizer
ObjCBridgingOptimization.swift
MergeCondFails.swift
NamedReturnValueOptimization.swift
RedundantLoadElimination.swift
ReleaseDevirtualizer.swift
SimplificationPasses.swift
StackPromotion.swift
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ private func tryEliminate(store: StoreInst, _ context: FunctionPassContext) {
case .dead:
// The new individual stores are inserted right after the current store and
// will be optimized in the following loop iterations.
store.split(context)
store.trySplit(context)
}
}
}
Expand Down Expand Up @@ -158,27 +158,6 @@ private extension StoreInst {
}
}

func split(_ context: FunctionPassContext) {
let builder = Builder(after: self, context)
let type = source.type
if type.isStruct {
for idx in 0..<type.getNominalFields(in: parentFunction).count {
let srcField = builder.createStructExtract(struct: source, fieldIndex: idx)
let destFieldAddr = builder.createStructElementAddr(structAddress: destination, fieldIndex: idx)
builder.createStore(source: srcField, destination: destFieldAddr, ownership: storeOwnership)
}
} else if type.isTuple {
for idx in 0..<type.tupleElements.count {
let srcField = builder.createTupleExtract(tuple: source, elementIndex: idx)
let destFieldAddr = builder.createTupleElementAddr(tupleAddress: destination, elementIndex: idx)
builder.createStore(source: srcField, destination: destFieldAddr, ownership: storeOwnership)
}
} else {
fatalError("a materializable projection path should only contain struct and tuple projections")
}
context.erase(instruction: self)
}

var hasValidOwnershipForDeadStoreElimination: Bool {
switch storeOwnership {
case .unqualified, .trivial:
Expand Down
Loading

0 comments on commit b9d0aa3

Please sign in to comment.