Skip to content

Commit 8510bdd

Browse files
authored
Merge pull request #67587 from kubamracek/move-target-const-folding-to-swift-simplification
Move target const folding to swift simplification
2 parents 984b4f2 + 97da252 commit 8510bdd

File tree

17 files changed

+153
-173
lines changed

17 files changed

+153
-173
lines changed

SwiftCompilerSources/Sources/Optimizer/InstructionSimplification/SimplifyBuiltin.swift

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ extension BuiltinInst : OnoneSimplifyable {
2828
optimizeCanBeClass(context)
2929
case .AssertConf:
3030
optimizeAssertConfig(context)
31+
case .Sizeof,
32+
.Strideof,
33+
.Alignof:
34+
optimizeTargetTypeConst(context)
3135
default:
3236
if let literal = constantFold(context) {
3337
uses.replaceAll(with: literal, context)
@@ -131,6 +135,33 @@ private extension BuiltinInst {
131135
uses.replaceAll(with: literal, context)
132136
context.erase(instruction: self)
133137
}
138+
139+
func optimizeTargetTypeConst(_ context: SimplifyContext) {
140+
guard let ty = substitutionMap.replacementTypes[0] else {
141+
return
142+
}
143+
144+
let value: Int?
145+
switch id {
146+
case .Sizeof:
147+
value = ty.getStaticSize(context: context)
148+
case .Strideof:
149+
value = ty.getStaticStride(context: context)
150+
case .Alignof:
151+
value = ty.getStaticAlignment(context: context)
152+
default:
153+
fatalError()
154+
}
155+
156+
guard let value else {
157+
return
158+
}
159+
160+
let builder = Builder(before: self, context)
161+
let literal = builder.createIntegerLiteral(value, type: type)
162+
uses.replaceAll(with: literal, context)
163+
context.erase(instruction: self)
164+
}
134165
}
135166

136167
private func hasSideEffectForBuiltinOnce(_ instruction: Instruction) -> Bool {

SwiftCompilerSources/Sources/Optimizer/PassManager/Context.swift

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,23 @@ struct SimplifyContext : MutatingContext {
279279
let preserveDebugInfo: Bool
280280
}
281281

282+
extension Type {
283+
func getStaticSize(context: SimplifyContext) -> Int? {
284+
let v = context._bridged.getStaticSize(self.bridged)
285+
return v == -1 ? nil : v
286+
}
287+
288+
func getStaticAlignment(context: SimplifyContext) -> Int? {
289+
let v = context._bridged.getStaticAlignment(self.bridged)
290+
return v == -1 ? nil : v
291+
}
292+
293+
func getStaticStride(context: SimplifyContext) -> Int? {
294+
let v = context._bridged.getStaticStride(self.bridged)
295+
return v == -1 ? nil : v
296+
}
297+
}
298+
282299
//===----------------------------------------------------------------------===//
283300
// Builder initialization
284301
//===----------------------------------------------------------------------===//

include/swift/SIL/SILBridging.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -419,7 +419,7 @@ struct BridgedTypeArray {
419419

420420
SWIFT_IMPORT_UNSAFE
421421
swift::SILType getAt(SwiftInt index) const {
422-
auto ty = swift::CanType(typeArray[index]);
422+
auto ty = typeArray[index]->getCanonicalType();
423423
if (ty->isLegalSILType())
424424
return swift::SILType::getPrimitiveObjectType(ty);
425425
return swift::SILType();

include/swift/SIL/SILModule.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -589,9 +589,8 @@ class SILModule {
589589

590590
const SILOptions &getOptions() const { return Options; }
591591
const IRGenOptions *getIRGenOptionsOrNull() const {
592-
// We don't want to serialize target specific SIL.
593-
assert(isSerialized() &&
594-
"Target specific options must not be used before serialization");
592+
// This exposes target specific information, therefore serialized SIL
593+
// is also target specific.
595594
return irgenOptions;
596595
}
597596

include/swift/SILOptimizer/OptimizerBridging.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,14 @@ struct BridgedPassContext {
260260
return {swift::SILUndef::get(type, *invocation->getFunction())};
261261
}
262262

263+
// IRGen
264+
265+
SwiftInt getStaticSize(swift::SILType type) const;
266+
267+
SwiftInt getStaticAlignment(swift::SILType type) const;
268+
269+
SwiftInt getStaticStride(swift::SILType type) const;
270+
263271
// Sets
264272

265273
SWIFT_IMPORT_UNSAFE

include/swift/SILOptimizer/PassManager/PassManager.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ class SILCombiner;
4141

4242
namespace irgen {
4343
class IRGenModule;
44+
class IRGenerator;
4445
}
4546

4647
/// The main entrypoint for executing a pipeline pass on a SIL module.
@@ -71,6 +72,12 @@ class SwiftPassInvocation {
7172

7273
SILSSAUpdater *ssaUpdater = nullptr;
7374

75+
/// IRGen module for passes that request it (e.g. simplification pass)
76+
irgen::IRGenModule *irgenModule = nullptr;
77+
78+
/// IRGenerator used by IRGenModule above
79+
irgen::IRGenerator *irgen = nullptr;
80+
7481
static constexpr int BlockSetCapacity = 8;
7582
char blockSetStorage[sizeof(BasicBlockSet) * BlockSetCapacity];
7683
bool aliveBlockSets[BlockSetCapacity];
@@ -95,12 +102,16 @@ class SwiftPassInvocation {
95102
SwiftPassInvocation(SILPassManager *passManager) :
96103
passManager(passManager) {}
97104

105+
~SwiftPassInvocation();
106+
98107
SILPassManager *getPassManager() const { return passManager; }
99108

100109
SILTransform *getTransform() const { return transform; }
101110

102111
SILFunction *getFunction() const { return function; }
103112

113+
irgen::IRGenModule *getIRGenModule();
114+
104115
FixedSizeSlab *allocSlab(FixedSizeSlab *afterSlab);
105116

106117
FixedSizeSlab *freeSlab(FixedSizeSlab *slab);

include/swift/SILOptimizer/PassManager/Passes.def

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -345,8 +345,6 @@ PASS(ReleaseHoisting, "release-hoisting",
345345
"SIL release Hoisting")
346346
PASS(LateReleaseHoisting, "late-release-hoisting",
347347
"Late SIL release Hoisting Preserving Epilogues")
348-
PASS(TargetConstantFolding, "target-constant-folding",
349-
"Target specific constant folding")
350348
IRGEN_PASS(LoadableByAddress, "loadable-address",
351349
"SIL Large Loadable type by-address lowering.")
352350
PASS(MandatorySILLinker, "mandatory-linker",

lib/SILOptimizer/CMakeLists.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ add_subdirectory(Analysis)
99
add_subdirectory(Differentiation)
1010
add_subdirectory(FunctionSignatureTransforms)
1111
add_subdirectory(IPO)
12-
add_subdirectory(IRGenTransforms)
1312
add_subdirectory(LoopTransforms)
1413
add_subdirectory(Mandatory)
1514
add_subdirectory(PassManager)

lib/SILOptimizer/IRGenTransforms/CMakeLists.txt

Lines changed: 0 additions & 3 deletions
This file was deleted.

lib/SILOptimizer/IRGenTransforms/TargetConstantFolding.cpp

Lines changed: 0 additions & 154 deletions
This file was deleted.

0 commit comments

Comments
 (0)