Skip to content

Commit e6db6bb

Browse files
committed
TargetConstantFolding: address review comments
* move the source file to SILOptimizer/IRGenTransforms * add a file level comment * document and verify that the pass runs after serialization * catch overflows when truncating a constant value
1 parent 94fa79c commit e6db6bb

File tree

5 files changed

+27
-6
lines changed

5 files changed

+27
-6
lines changed

include/swift/SIL/SILModule.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -579,7 +579,12 @@ class SILModule {
579579
bool isOptimizedOnoneSupportModule() const;
580580

581581
const SILOptions &getOptions() const { return Options; }
582-
const IRGenOptions *getIRGenOptionsOrNull() const { return irgenOptions; }
582+
const IRGenOptions *getIRGenOptionsOrNull() const {
583+
// We don't want to serialize target specific SIL.
584+
assert(isSerialized() &&
585+
"Target specific options must not be used before serialization");
586+
return irgenOptions;
587+
}
583588

584589
using iterator = FunctionListType::iterator;
585590
using const_iterator = FunctionListType::const_iterator;

lib/IRGen/CMakeLists.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@ add_swift_host_library(swiftIRGen STATIC
5757
Outlining.cpp
5858
StructLayout.cpp
5959
SwiftTargetInfo.cpp
60-
TargetConstantFolding.cpp
6160
TypeLayout.cpp
6261
TypeLayoutDumper.cpp
6362
TypeLayoutVerifier.cpp

lib/SILOptimizer/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ add_subdirectory(Analysis)
99
add_subdirectory(Differentiation)
1010
add_subdirectory(FunctionSignatureTransforms)
1111
add_subdirectory(IPO)
12+
add_subdirectory(IRGenTransforms)
1213
add_subdirectory(LoopTransforms)
1314
add_subdirectory(Mandatory)
1415
add_subdirectory(PassManager)
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
target_sources(swiftSILOptimizer PRIVATE
2+
TargetConstantFolding.cpp)
3+

lib/IRGen/TargetConstantFolding.cpp renamed to lib/SILOptimizer/IRGenTransforms/TargetConstantFolding.cpp

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,14 @@
1515
// It is a mandatory IRGen preparation pass (not a diagnostic pass).
1616
//
1717
//===----------------------------------------------------------------------===//
18+
///
19+
/// This file contains a pass for target specific constant folding:
20+
/// `TargetConstantFolding`. For details see the comments there.
21+
///
22+
//===----------------------------------------------------------------------===//
1823

1924
#define DEBUG_TYPE "target-constant-folding"
20-
#include "IRGenModule.h"
25+
#include "../../IRGen/IRGenModule.h"
2126
#include "swift/SIL/SILBuilder.h"
2227
#include "swift/SILOptimizer/PassManager/Transforms.h"
2328
#include "swift/SILOptimizer/Utils/InstructionDeleter.h"
@@ -43,22 +48,24 @@ class TargetConstantFolding : public SILModuleTransform {
4348
private:
4449
/// The entry point to the transformation.
4550
void run() override {
46-
auto *irgenOpts = getModule()->getIRGenOptionsOrNull();
51+
SILModule *module = getModule();
52+
53+
auto *irgenOpts = module->getIRGenOptionsOrNull();
4754
if (!irgenOpts)
4855
return;
4956

5057
// We need an IRGenModule to get the actual sizes from type lowering.
5158
// Creating an IRGenModule involves some effort. Therefore this is a
5259
// module pass rather than a function pass so that this one-time setup
5360
// only needs to be done once and not for all functions in a module.
54-
IRGenerator irgen(*irgenOpts, *getModule());
61+
IRGenerator irgen(*irgenOpts, *module);
5562
auto targetMachine = irgen.createTargetMachine();
5663
if (!targetMachine)
5764
return;
5865
IRGenModule IGM(irgen, std::move(targetMachine));
5966

6067
// Scan all instructions in the module for constant foldable instructions.
61-
for (SILFunction &function : *getModule()) {
68+
for (SILFunction &function : *module) {
6269

6370
if (!function.shouldOptimize())
6471
continue;
@@ -118,6 +125,12 @@ class TargetConstantFolding : public SILModuleTransform {
118125
if (!intTy)
119126
return false;
120127

128+
// The bit widths can differ if we are compiling for a 32 bit target.
129+
if (value.getActiveBits() > intTy->getGreatestWidth()) {
130+
// It's unlikely that a size/stride overflows 32 bits, but let's be on
131+
// the safe side and catch a potential overflow.
132+
return false;
133+
}
121134
value = value.sextOrTrunc(intTy->getGreatestWidth());
122135

123136
// Replace the builtin by an integer literal.

0 commit comments

Comments
 (0)