Skip to content

Commit 95114f2

Browse files
committed
[clang][cli] Do not marshall only CC1Option flags in BoolOption
We cannot be sure whether a flag is CC1Option inside the definition of `BoolOption`. Take the example below: ``` let Flags = [CC1Option] in { defm xxx : BoolOption<...>; } ``` where TableGen applies `Flags = [CC1Option]` to the `xxx` and `no_xxx` records **after** they have been is fully declared by `BoolOption`. For the refactored `-f[no-]debug-pass-manager` flags (see the diff), this means `BoolOption` never adds any marshalling info, as it doesn't see either of the flags as `CC1Option`. For that reason, we should defensively append the marshalling information to both flags inside `BoolOption`. Now the check for `CC1Option` needs to happen later, in the parsing macro, when all TableGen logic has been resolved. However, for some flags defined through `BoolOption`, we can run into issues: ``` // Options.td def fenable_xxx : /* ... */; // Both flags are CC1Option, the first is implied. defm xxx : BoolOption<"xxx, "Opts.Xxx", DefaultsToFalse, ChangedBy<PosFlag, [CC1Option], "", [fenable_xxx]>, ResetBy<NegFlag, [CC1Option]>>; ``` When parsing `clang -cc1 -fenable-xxx`: * we run parsing for `PosFlag`: * set `Opts.Xxx` to default `false`, * discover `PosFlag` is implied by `-fenable-xxx`: set `Opts.Xxx` to `true`, * don't see `-fxxx` on command line: do nothing, * we run parsing for `NegFlag`: * set `Opts.Xxx` to default `false`, * discover `NegFlag` cannot be implied: do nothing, * don't see `-fno-xxx` on command line: do nothing. Now we ended up with `Opts.Xxx` set to `false` instead of `true`. For this reason, we need to ensure to append the same `ImpliedByAnyOf` instance to both flags. This means both parsing runs now behave identically (they set the same default value, run the same "implied by" check, and call `makeBooleanOptionNormalizer` that already has information on both flags, so it returns the same value in both calls). The solution works well, but what might be confusing is this: you have defined a flag **A** that is not `CC1Option`, but can be implied by another flag **B** that is `CC1Option`: * if **A** is defined manually, it will never get implied, as the code never runs ``` def no_signed_zeros : Flag<["-"], "fno-signed-zeros">, Group<f_Group>, Flags<[]>, MarshallingInfoFlag<"LangOpts->NoSignedZero">, ImpliedByAnyOf<[menable_unsafe_fp_math]>; ``` * if **A** is defined by `BoolOption`, it could get implied, as the code is run by its `CC1Option` counterpart: ``` defm signed_zeros : BoolOption<"signed-zeros", "LangOpts->NoSignedZero", DefaultsToFalse, ChangedBy<NegFlag, [], "Allow optimizations that ignore the sign of floating point zeros", [cl_no_signed_zeros, menable_unsafe_fp_math]>, ResetBy<PosFlag, [CC1Option]>, "f">, Group<f_Group>; ``` This is a surprising inconsistency. One solution might be to somehow propagate the final `Flags` of the implied flag in `ImpliedByAnyOf` and check whether it has `CC1Option` in the parsing macro. However, I think it doesn't make sense to spend time thinking about a corner case that won't come up in real code. An observation: it is unfortunate that the marshalling information is a part of the flag definition. If we represented it in a separate structure, we could avoid the "double parsing" problem by having a single source of truth. This would require a lot of additional work though. Note: the original patch missed the `CC1Option` check in the parsing macro, making my reasoning here incomplete. Moreover, it contained a change to denormalization that wasn't necessarily related to these changes, so I've extracted that to a follow-up patch: D93094. Reviewed By: dexonsmith, Bigcheese Differential Revision: https://reviews.llvm.org/D93008
1 parent b2851ae commit 95114f2

File tree

3 files changed

+63
-32
lines changed

3 files changed

+63
-32
lines changed

clang/include/clang/Driver/Options.td

Lines changed: 13 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -325,29 +325,21 @@ class FlagDefExpanded<FlagDef flag, string prefix, string name, string spelling>
325325
string Spelling
326326
= prefix#!cond(flag.Polarity.Value : "", true : "no-")#spelling;
327327

328-
// Does the flag have CC1Option?
329-
bit IsCC1 = !not(!empty(!filter(opt_flag, flag.OptionFlags,
330-
!eq(opt_flag, CC1Option))));
331-
332328
// Can the flag be implied by another flag?
333329
bit CanBeImplied = !not(!empty(flag.ImpliedBy));
334330

335331
// C++ code that will be assigned to the keypath when the flag is present.
336332
code ValueAsCode = !cond(flag.Value : "true", true: "false");
337333
}
338334

339-
// Creates simple flag record.
340-
class BoolOptionFlag<FlagDefExpanded flag, code keypath, Default default>
341-
: Flag<["-"], flag.Spelling>, Flags<flag.OptionFlags>, HelpText<flag.Help> {}
342-
343-
// Creates marshalled flag record.
344-
class CC1BoolOptionFlag<FlagDefExpanded flag, FlagDefExpanded other,
345-
code keypath, Default default>
335+
// Creates record with a marshalled flag.
336+
class BoolOptionFlag<FlagDefExpanded flag, FlagDefExpanded other,
337+
FlagDefExpanded implied, code keypath, Default default>
346338
: Flag<["-"], flag.Spelling>, Flags<flag.OptionFlags>, HelpText<flag.Help>,
347339
MarshallingInfoBooleanFlag<keypath, default.Value, flag.ValueAsCode,
348340
flag.RecordName, other.ValueAsCode,
349341
other.RecordName, other.Spelling>,
350-
ImpliedByAnyOf<flag.ImpliedBy, flag.ValueAsCode> {}
342+
ImpliedByAnyOf<implied.ImpliedBy, implied.ValueAsCode> {}
351343

352344
// Generates TableGen records for two command line flags that control the same
353345
// keypath via the marshalling infrastructure.
@@ -370,17 +362,10 @@ multiclass BoolOptionBase<string spelling_base, code keypath, Default default,
370362
// TODO: Assert that the flags have different value.
371363
// TODO: Assert that only one of the flags can be implied.
372364

373-
if flag1.IsCC1 then {
374-
def flag1.RecordName : CC1BoolOptionFlag<flag1, flag2, keypath, default>;
375-
} else {
376-
def flag1.RecordName : BoolOptionFlag<flag1, keypath, default>;
377-
}
378-
379-
if flag2.IsCC1 then {
380-
def flag2.RecordName : CC1BoolOptionFlag<flag2, flag1, keypath, default>;
381-
} else {
382-
def flag2.RecordName : BoolOptionFlag<flag2, keypath, default>;
383-
}
365+
defvar implied = !cond(flag1.CanBeImplied: flag1, true: flag2);
366+
367+
def flag1.RecordName : BoolOptionFlag<flag1, flag2, implied, keypath, default>;
368+
def flag2.RecordName : BoolOptionFlag<flag2, flag1, implied, keypath, default>;
384369
}
385370

386371
//===----------------------------------------------------------------------===//
@@ -4322,10 +4307,11 @@ def flto_visibility_public_std:
43224307
def flto_unit: Flag<["-"], "flto-unit">,
43234308
HelpText<"Emit IR to support LTO unit features (CFI, whole program vtable opt)">;
43244309
def fno_lto_unit: Flag<["-"], "fno-lto-unit">;
4325-
def fdebug_pass_manager : Flag<["-"], "fdebug-pass-manager">,
4326-
HelpText<"Prints debug information for the new pass manager">;
4327-
def fno_debug_pass_manager : Flag<["-"], "fno-debug-pass-manager">,
4328-
HelpText<"Disables debug printing for the new pass manager">;
4310+
defm debug_pass_manager : BoolOption<"debug-pass-manager",
4311+
"CodeGenOpts.DebugPassManager", DefaultsToFalse,
4312+
ChangedBy<PosFlag, [], "Prints debug information for the new pass manager">,
4313+
ResetBy<NegFlag, [], "Disables debug printing for the new pass manager">,
4314+
BothFlags<[]>, "f">;
43294315
def fexperimental_debug_variable_locations : Flag<["-"],
43304316
"fexperimental-debug-variable-locations">,
43314317
HelpText<"Use experimental new value-tracking variable locations">;

clang/lib/Frontend/CompilerInvocation.cpp

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -869,10 +869,6 @@ static bool ParseCodeGenArgs(CodeGenOptions &Opts, ArgList &Args, InputKind IK,
869869
}
870870
}
871871

872-
Opts.DebugPassManager =
873-
Args.hasFlag(OPT_fdebug_pass_manager, OPT_fno_debug_pass_manager,
874-
/* Default */ false);
875-
876872
if (Arg *A = Args.getLastArg(OPT_fveclib)) {
877873
StringRef Name = A->getValue();
878874
if (Name == "Accelerate")
@@ -3767,7 +3763,7 @@ bool CompilerInvocation::parseSimpleArgs(const ArgList &Args,
37673763
HELPTEXT, METAVAR, VALUES, SPELLING, ALWAYS_EMIT, KEYPATH, DEFAULT_VALUE, \
37683764
IMPLIED_CHECK, IMPLIED_VALUE, NORMALIZER, DENORMALIZER, MERGER, EXTRACTOR, \
37693765
TABLE_INDEX) \
3770-
{ \
3766+
if ((FLAGS)&options::CC1Option) { \
37713767
this->KEYPATH = MERGER(this->KEYPATH, DEFAULT_VALUE); \
37723768
if (IMPLIED_CHECK) \
37733769
this->KEYPATH = MERGER(this->KEYPATH, IMPLIED_VALUE); \

clang/unittests/Frontend/CompilerInvocationTest.cpp

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,55 @@ TEST_F(CommandLineTest, BoolOptionDefaultArbitraryTwoFlagsPresentReset) {
238238
ASSERT_THAT(GeneratedArgs, Not(Contains(StrEq(PassManagerChangedByFlag))));
239239
}
240240

241+
// Boolean option that gets the CC1Option flag from a let statement (which
242+
// is applied **after** the record is defined):
243+
//
244+
// let Flags = [CC1Option] in {
245+
// defm option : BoolOption<...>;
246+
// }
247+
248+
TEST_F(CommandLineTest, BoolOptionCC1ViaLetPresentNone) {
249+
const char *Args[] = {""};
250+
251+
CompilerInvocation::CreateFromArgs(Invocation, Args, *Diags);
252+
253+
ASSERT_FALSE(Diags->hasErrorOccurred());
254+
ASSERT_FALSE(Invocation.getCodeGenOpts().DebugPassManager);
255+
256+
Invocation.generateCC1CommandLine(GeneratedArgs, *this);
257+
258+
ASSERT_THAT(GeneratedArgs, Not(Contains(StrEq("-fdebug-pass-manager"))));
259+
ASSERT_THAT(GeneratedArgs, Not(Contains(StrEq("-fno-debug-pass-manager"))));
260+
}
261+
262+
TEST_F(CommandLineTest, BoolOptionCC1ViaLetPresentPos) {
263+
const char *Args[] = {"-fdebug-pass-manager"};
264+
265+
CompilerInvocation::CreateFromArgs(Invocation, Args, *Diags);
266+
267+
ASSERT_FALSE(Diags->hasErrorOccurred());
268+
ASSERT_TRUE(Invocation.getCodeGenOpts().DebugPassManager);
269+
270+
Invocation.generateCC1CommandLine(GeneratedArgs, *this);
271+
272+
ASSERT_THAT(GeneratedArgs, Contains(StrEq("-fdebug-pass-manager")));
273+
ASSERT_THAT(GeneratedArgs, Not(Contains(StrEq("-fno-debug-pass-manager"))));
274+
}
275+
276+
TEST_F(CommandLineTest, BoolOptionCC1ViaLetPresentNeg) {
277+
const char *Args[] = {"-fno-debug-pass-manager"};
278+
279+
CompilerInvocation::CreateFromArgs(Invocation, Args, *Diags);
280+
281+
ASSERT_FALSE(Diags->hasErrorOccurred());
282+
ASSERT_FALSE(Invocation.getCodeGenOpts().DebugPassManager);
283+
284+
Invocation.generateCC1CommandLine(GeneratedArgs, *this);
285+
286+
ASSERT_THAT(GeneratedArgs, Not(Contains(StrEq("-fno-debug-pass-manager"))));
287+
ASSERT_THAT(GeneratedArgs, Not(Contains(StrEq("-fdebug-pass-manager"))));
288+
}
289+
241290
TEST_F(CommandLineTest, CanGenerateCC1CommandLineFlag) {
242291
const char *Args[] = {"-fmodules-strict-context-hash"};
243292

0 commit comments

Comments
 (0)