Files
swift-mirror/include/swift/SILOptimizer/Utils/SILOptFunctionBuilder.h
T
Erik Eckstein 0f0aa0c17b Optimizer: require that there are no unreachable blocks and infinite loops in OSSA
These two new invariants eliminate corner cases which caused bugs if optimization didn't handle them.
Also, it will significantly simplify lifetime completion.

The implementation basically consists of these changes:
* add a flag in SILFunction which tells optimization if they need to take care of infinite loops
* add a utility to break infinite loops
* let all optimizations remove unreachable blocks and break infinite loops if necessary
* add verification to check the new SIL invariants

The new `breakIfniniteLoops` utility breaks infinite loops in the control flow by inserting an "artificial" loop exit to a new dead-end block with an `unreachable`.
It inserts a `cond_br` with a `builtin "infinite_loop_true_condition"`:
```
bb0:
  br bb1
bb1:
  br bb1              // back-end branch
```
->
```
bb0:
  br bb1
bb1:
  %1 = builtin "infinite_loop_true_condition"() // always true, but the compiler doesn't know
  cond_br %1, bb2, bb3
bb2:                  // new back-end block
  br bb1
bb3:                  // new dead-end block
  unreachable
```
2026-01-22 17:41:23 +01:00

78 lines
2.2 KiB
C++

//===--- SILOptFunctionBuilder.h --------------------------------*- C++ -*-===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2018 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
#ifndef SWIFT_SILOPTIMIZER_UTILS_SILOPTFUNCTIONBUILDER_H
#define SWIFT_SILOPTIMIZER_UTILS_SILOPTFUNCTIONBUILDER_H
#include "swift/SIL/SILFunctionBuilder.h"
#include "swift/SILOptimizer/PassManager/PassManager.h"
#include "swift/SILOptimizer/PassManager/Transforms.h"
namespace swift {
class SILOptFunctionBuilder {
SILTransform &transform;
SILFunctionBuilder builder;
public:
SILOptFunctionBuilder(SILTransform &transform)
: transform(transform),
builder(*transform.getPassManager()->getModule()) {}
template <class... ArgTys>
SILFunction *getOrCreateSharedFunction(ArgTys &&... args) {
SILFunction *f =
builder.getOrCreateSharedFunction(std::forward<ArgTys>(args)...);
notifyAddFunction(f);
return f;
}
template <class... ArgTys>
SILFunction *getOrCreateFunction(ArgTys &&... args) {
SILFunction *f = builder.getOrCreateFunction(std::forward<ArgTys>(args)...);
notifyAddFunction(f);
return f;
}
template <class... ArgTys> SILFunction *createFunction(ArgTys &&... args) {
SILFunction *f = builder.createFunction(std::forward<ArgTys>(args)...);
notifyAddFunction(f);
return f;
}
void eraseFunction(SILFunction *f) {
auto &pm = getPassManager();
pm.notifyWillDeleteFunction(f);
pm.getModule()->eraseFunction(f);
}
SILModule &getModule() const { return *getPassManager().getModule(); }
irgen::IRGenModule *getIRGenModule() const {
return transform.getIRGenModule();
}
SILPassManager &getPassManager() const {
return *transform.getPassManager();
}
private:
void notifyAddFunction(SILFunction *f) {
auto &pm = getPassManager();
pm.notifyOfNewFunction(f, &transform);
pm.notifyAnalysisOfFunction(f);
}
};
} // namespace swift
#endif