diff --git a/changelog/dmd.dummyoptimization-level-switches.dd b/changelog/dmd.dummyoptimization-level-switches.dd new file mode 100644 index 000000000000..2fa86246f910 --- /dev/null +++ b/changelog/dmd.dummyoptimization-level-switches.dd @@ -0,0 +1,27 @@ +Add dummy optimization-level switches + +Optimization-level switches as supported by compilers like $(I GDC) or $(I LDC) are now accepted. +As DMD does not actually implement support for multiple optimization levels, +these switches merely turn the optimizer on or off. + +This streamlines the user experience to bring it in line with $(I LDC)'s $(I $(CODE ldmd2)) wrapper. + +$(TABLE2 Optimization-level switches + + $(TR $(TH Switch) $(TH Optimizer) $(TH Emits warning)) + +
+ $(TR $(TH `-O`) $(TD on) $(TD no)) + $(TR $(TH `-O0`) $(TD off) $(TD no)) + $(TR $(TH `-O1`) $(TD on) $(TD no)) + $(TR $(TH `-O2`) $(TD on) $(TD no)) + $(TR $(TH `-O3`) $(TD on) $(TD no)) + $(TR $(TH `-Og`) $(TD off) $(TD yes)) + $(TR $(TH `-Os`) $(TD on) $(TD yes)) + $(TR $(TH `-Oz`) $(TD on) $(TD yes)) + $(TR $(TH `-Ofast`) $(TD on) $(TD yes)) + +) + +DMD will conditionally accept multiple differing optimization-level switches in one compiler invocation. +This is as long as they do not effectively contradict each other with reference to the activation of the optimizer. diff --git a/compiler/src/dmd/dmdparams.d b/compiler/src/dmd/dmdparams.d index 519e1c033605..7d70662c4f98 100644 --- a/compiler/src/dmd/dmdparams.d +++ b/compiler/src/dmd/dmdparams.d @@ -51,6 +51,7 @@ struct DMDparams bool oneobj; // write one object file instead of multiple ones bool optimize; // run optimizer + bool optimizeHasBeenSet;// whether `optimize` has been set by the driver (without regard to the assigned value) bool nofloat; // code should not pull in floating point support bool ibt; // generate indirect branch tracking PIC pic = PIC.fixed; // generate fixed, pic or pie code diff --git a/compiler/src/dmd/mars.d b/compiler/src/dmd/mars.d index d73aef468aad..a2070f0c8dcf 100644 --- a/compiler/src/dmd/mars.d +++ b/compiler/src/dmd/mars.d @@ -529,6 +529,18 @@ bool parseCommandLine(const ref Strings arguments, const size_t argc, out Param errors = true; } + // emits an informational-only warning + void emitNotice(Supplementals...)(string msg, Supplementals supplementals) + { + const backup = global.errorSink.useWarnings; + scope (exit) global.errorSink.useWarnings = backup; + + global.errorSink.useWarnings = DiagnosticReporting.inform; + eSink.warning(Loc.initial, "%.*s".ptr, cast(int) msg.length, msg.ptr); + foreach(suppl; supplementals) + eSink.warningSupplemental(Loc.initial, "%.*s".ptr, cast(int) suppl.length, suppl.ptr); + } + /** * Print an error messsage about an invalid switch. * If an optional supplemental message has been provided, @@ -1348,8 +1360,59 @@ bool parseCommandLine(const ref Strings arguments, const size_t argc, out Param // Rather, these old features will just be accepted without warning. // See also: @__edition_latest_do_not_use } - else if (arg == "-O") // https://dlang.org/dmd.html#switch-O - driverParams.optimize = true; + else if (startsWith(p + 1, "O")) // https://dlang.org/dmd.html#switch-O + { + static immutable msgSupplementalCompat = + "Optimization-level switches are solely accepted for command-line compatibility with other compilers."; + bool enableOptimize = true; + + if (arg.length >= 3) + { + const optLevel = arg[2 .. $]; + if (optLevel == "s" || optLevel == "z") + emitNotice( + "This compiler does not support optimization for size.", + "Will optimize for speed instead.", + msgSupplementalCompat, + ); + else if (optLevel == "fast") + emitNotice( + "This compiler does not support optimization for non-strict standards compliance.", + "Will optimize for speed instead.", + msgSupplementalCompat, + ); + else if (optLevel == "g") + { + enableOptimize = false; + emitNotice( + "This compiler does not support optimization for debugging experience.", + "Will disable non-mandatory optimizations instead.", + msgSupplementalCompat, + ); + } + else if (optLevel.ptr[0].isdigit) + { + uint optLevelNum; + if (!parseDigits(optLevelNum, optLevel)) + goto Lerror; + if (optLevelNum == 0) + enableOptimize = false; + version (none) if (optLevelNum > 1) + emitNotice( + "This compiler does not support optimization levels.", + msgSupplementalCompat, + ); + } + else + goto Lerror; + } + + if (driverParams.optimizeHasBeenSet && (driverParams.optimize != enableOptimize)) + error("Switch `%s` effectively contradicts a previously encountered optimization-level switch.", p); + + driverParams.optimize = enableOptimize; + driverParams.optimizeHasBeenSet = true; + } else if (arg == "-o-") // https://dlang.org/dmd.html#switch-o- params.obj = false; else if (p[1] == 'o')