Skip to content

Commit 77deb0e

Browse files
committed
Add a -opt-mode flag to sil-opt
To indicate whether the specified passes should be run as-if under -Onone, -Osize, or -O. Important for proper debug info handling.
1 parent a50108f commit 77deb0e

File tree

1 file changed

+31
-2
lines changed

1 file changed

+31
-2
lines changed

tools/sil-opt/SILOpt.cpp

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,25 @@ static llvm::cl::opt<std::string>
181181
Target("target", llvm::cl::desc("target triple"),
182182
llvm::cl::init(llvm::sys::getDefaultTargetTriple()));
183183

184+
// This primarily determines semantics of debug information. The compiler does
185+
// not directly expose a "preserve debug info mode". It is derived from the
186+
// optimization level. At -Onone, all debug info must be preserved. At higher
187+
// levels, debug info cannot change the compiler output.
188+
//
189+
// Diagnostics should be "equivalent" at all levels. For example, programs that
190+
// compile at -Onone should compile at -O. However, it is difficult to guarantee
191+
// identical diagnostic output given the changes in SIL caused by debug info
192+
// preservation.
193+
static llvm::cl::opt<OptimizationMode> OptModeFlag(
194+
"opt-mode", llvm::cl::desc("optimization mode"),
195+
llvm::cl::values(clEnumValN(OptimizationMode::NoOptimization, "none",
196+
"preserve debug info"),
197+
clEnumValN(OptimizationMode::ForSize, "size",
198+
"ignore debug info, reduce size"),
199+
clEnumValN(OptimizationMode::ForSpeed, "speed",
200+
"ignore debug info, reduce runtime")),
201+
llvm::cl::init(OptimizationMode::NotSet));
202+
184203
static llvm::cl::opt<OptGroup> OptimizationGroup(
185204
llvm::cl::desc("Predefined optimization groups:"),
186205
llvm::cl::values(
@@ -485,8 +504,6 @@ int main(int argc, char **argv) {
485504
SILOpts.VerifyNone = SILVerifyNone;
486505
SILOpts.RemoveRuntimeAsserts = RemoveRuntimeAsserts;
487506
SILOpts.AssertConfig = AssertConfId;
488-
if (OptimizationGroup != OptGroup::Diagnostics)
489-
SILOpts.OptMode = OptimizationMode::ForSpeed;
490507
SILOpts.VerifySILOwnership = !DisableSILOwnershipVerifier;
491508
SILOpts.OptRecordFile = RemarksFilename;
492509
SILOpts.OptRecordPasses = RemarksPasses;
@@ -547,6 +564,18 @@ int main(int argc, char **argv) {
547564
if (!EnableLexicalBorrowScopes)
548565
SILOpts.LexicalLifetimes = LexicalLifetimesOption::Off;
549566

567+
if (OptModeFlag == OptimizationMode::NotSet) {
568+
if (OptimizationGroup == OptGroup::Diagnostics)
569+
SILOpts.OptMode = OptimizationMode::NoOptimization;
570+
else
571+
SILOpts.OptMode = OptimizationMode::ForSpeed;
572+
} else {
573+
SILOpts.OptMode = OptModeFlag;
574+
}
575+
576+
// Note: SILOpts must be set before the CompilerInstance is initializer below
577+
// based on Invocation.
578+
550579
serialization::ExtendedValidationInfo extendedInfo;
551580
llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> FileBufOrErr =
552581
Invocation.setUpInputForSILTool(InputFilename, ModuleName,

0 commit comments

Comments
 (0)