Skip to content

Commit b5b3111

Browse files
fhahntstellar
authored andcommitted
[clang] Add -ffinite-loops & -fno-finite-loops options.
This cherry-picks the following patches on the release branch: 6280bb4 [clang] Remove redundant condition (NFC). 51bf4c0 [clang] Add -ffinite-loops & -fno-finite-loops options. fb4d8fe [clang] Update mustprogress tests This patch adds 2 new options to control when Clang adds `mustprogress`: 1. -ffinite-loops: assume all loops are finite; mustprogress is added to all loops, regardless of the selected language standard. 2. -fno-finite-loops: assume no loop is finite; mustprogress is not added to any loop or function. We could add mustprogress to functions without loops, but we would have to detect that in Clang, which is probably not worth it. Differential Revision: https://reviews.llvm.org/D96850
1 parent 610b51c commit b5b3111

File tree

12 files changed

+583
-836
lines changed

12 files changed

+583
-836
lines changed

clang/include/clang/Basic/CodeGenOptions.def

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,9 @@ CODEGENOPT(VectorizeLoop , 1, 0) ///< Run loop vectorizer.
266266
CODEGENOPT(VectorizeSLP , 1, 0) ///< Run SLP vectorizer.
267267
CODEGENOPT(ProfileSampleAccurate, 1, 0) ///< Sample profile is accurate.
268268

269+
/// Treat loops as finite: language, always, never.
270+
ENUM_CODEGENOPT(FiniteLoops, FiniteLoopsKind, 2, FiniteLoopsKind::Language)
271+
269272
/// Attempt to use register sized accesses to bit-fields in structures, when
270273
/// possible.
271274
CODEGENOPT(UseRegisterSizedBitfieldAccess , 1, 0)

clang/include/clang/Basic/CodeGenOptions.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,12 @@ class CodeGenOptions : public CodeGenOptionsBase {
140140
All, // Keep all frame pointers.
141141
};
142142

143+
enum FiniteLoopsKind {
144+
Language, // Not specified, use language standard.
145+
Always, // All loops are assumed to be finite.
146+
Never, // No loop is assumed to be finite.
147+
};
148+
143149
/// The code model to use (-mcmodel).
144150
std::string CodeModel;
145151

clang/include/clang/Driver/Options.td

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2410,6 +2410,11 @@ def fno_unroll_loops : Flag<["-"], "fno-unroll-loops">, Group<f_Group>,
24102410
defm reroll_loops : BoolFOption<"reroll-loops",
24112411
CodeGenOpts<"RerollLoops">, DefaultFalse,
24122412
PosFlag<SetTrue, [CC1Option], "Turn on loop reroller">, NegFlag<SetFalse>>;
2413+
def ffinite_loops: Flag<["-"], "ffinite-loops">, Group<f_Group>,
2414+
HelpText<"Assume all loops are finite.">, Flags<[CC1Option]>;
2415+
def fno_finite_loops: Flag<["-"], "fno-finite-loops">, Group<f_Group>,
2416+
HelpText<"Do not assume that any loop is finite.">, Flags<[CC1Option]>;
2417+
24132418
def ftrigraphs : Flag<["-"], "ftrigraphs">, Group<f_Group>,
24142419
HelpText<"Process trigraph sequences">, Flags<[CC1Option]>;
24152420
def fno_trigraphs : Flag<["-"], "fno-trigraphs">, Group<f_Group>,

clang/lib/CodeGen/CodeGenFunction.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -507,12 +507,23 @@ class CodeGenFunction : public CodeGenTypeCache {
507507

508508
/// True if the C++ Standard Requires Progress.
509509
bool CPlusPlusWithProgress() {
510+
if (CGM.getCodeGenOpts().getFiniteLoops() ==
511+
CodeGenOptions::FiniteLoopsKind::Never)
512+
return false;
513+
510514
return getLangOpts().CPlusPlus11 || getLangOpts().CPlusPlus14 ||
511515
getLangOpts().CPlusPlus17 || getLangOpts().CPlusPlus20;
512516
}
513517

514518
/// True if the C Standard Requires Progress.
515519
bool CWithProgress() {
520+
if (CGM.getCodeGenOpts().getFiniteLoops() ==
521+
CodeGenOptions::FiniteLoopsKind::Always)
522+
return true;
523+
if (CGM.getCodeGenOpts().getFiniteLoops() ==
524+
CodeGenOptions::FiniteLoopsKind::Never)
525+
return false;
526+
516527
return getLangOpts().C11 || getLangOpts().C17 || getLangOpts().C2x;
517528
}
518529

clang/lib/Driver/ToolChains/Clang.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5620,6 +5620,9 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA,
56205620
if (A->getOption().matches(options::OPT_freroll_loops))
56215621
CmdArgs.push_back("-freroll-loops");
56225622

5623+
Args.AddLastArg(CmdArgs, options::OPT_ffinite_loops,
5624+
options::OPT_fno_finite_loops);
5625+
56235626
Args.AddLastArg(CmdArgs, options::OPT_fwritable_strings);
56245627
Args.AddLastArg(CmdArgs, options::OPT_funroll_loops,
56255628
options::OPT_fno_unroll_loops);

clang/lib/Frontend/CompilerInvocation.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1037,7 +1037,6 @@ bool CompilerInvocation::ParseCodeGenArgs(CodeGenOptions &Opts, ArgList &Args,
10371037
Opts.UnrollLoops =
10381038
Args.hasFlag(OPT_funroll_loops, OPT_fno_unroll_loops,
10391039
(Opts.OptimizationLevel > 1));
1040-
10411040
Opts.BinutilsVersion =
10421041
std::string(Args.getLastArgValue(OPT_fbinutils_version_EQ));
10431042

@@ -1324,6 +1323,10 @@ bool CompilerInvocation::ParseCodeGenArgs(CodeGenOptions &Opts, ArgList &Args,
13241323

13251324
Opts.EmitVersionIdentMetadata = Args.hasFlag(OPT_Qy, OPT_Qn, true);
13261325

1326+
if (Args.hasArg(options::OPT_ffinite_loops))
1327+
Opts.FiniteLoops = CodeGenOptions::FiniteLoopsKind::Always;
1328+
else if (Args.hasArg(options::OPT_fno_finite_loops))
1329+
Opts.FiniteLoops = CodeGenOptions::FiniteLoopsKind::Never;
13271330
return Success;
13281331
}
13291332

clang/test/CodeGen/attr-mustprogress-0.c

Lines changed: 0 additions & 184 deletions
This file was deleted.

0 commit comments

Comments
 (0)