Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions changelog/dmd.dummyoptimization-level-switches.dd
Original file line number Diff line number Diff line change
@@ -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
<thead>
$(TR $(TH Switch) $(TH Optimizer) $(TH Emits warning))
</thead>
<tbody>
$(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))
</tbody>
)

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.
1 change: 1 addition & 0 deletions compiler/src/dmd/dmdparams.d
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
67 changes: 65 additions & 2 deletions compiler/src/dmd/mars.d
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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')
Expand Down
Loading