Skip to content

Commit 07d5b91

Browse files
committed
build: a failure at a step an optimization acted on says how to check that entry alone; the graph-shaping switches are part of the replayed request
1 parent 40368cb commit 07d5b91

7 files changed

Lines changed: 595 additions & 2 deletions

File tree

‎modules/buildopt/src/advice.cppm‎

Lines changed: 206 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,21 @@
1515
// module the rebuild started at, when the importers that followed are many,
1616
// and once a build directory: the developer who chose the shape is not told
1717
// on every edit.
18+
//
19+
// A SECOND ADVICE: WHEN A BUILD FAILS AT A STEP AN ENTRY ACTED ON (SPEC-019
20+
// §9). Most projects are right with every entry on; the few that are not need
21+
// to learn, from the failure itself, how to find out whether an entry is the
22+
// cause, without it being said on every failure. It reads the step that
23+
// failed, not the entries that ran: a source built as two units that failed
24+
// (R1), a unit that imports a BMI kept with the lines of an earlier source
25+
// that failed (R2), a link that failed where either is in the graph (R3). Any
26+
// other failure says nothing (R4). The text is made of the register's own key,
27+
// environment name and `check` value: there is no second spelling of a switch.
1828

1929
export module mcpp.buildopt.advice;
2030

2131
import std;
32+
import mcpp.buildopt.registry;
2233

2334
export namespace mcpp::buildopt::advice {
2435

@@ -53,6 +64,46 @@ std::string line(const Cascade& c);
5364
// yet; saying it is recorded there (`buildopt-advice.list`).
5465
bool first_time(const std::filesystem::path& dir, std::string_view module);
5566

67+
// WHAT THE PLAN HOLDS THAT A FAILURE READS (SPEC-019 §9), kept beside
68+
// build.ninja (`buildopt-applied.tsv`) because the fast path, which has no
69+
// plan, reports a failure as well.
70+
struct Fragment {
71+
std::string written; // the source as it is written
72+
std::vector<std::string> units; // the files it is built as
73+
};
74+
struct Importer {
75+
std::string source; // a unit that imports a module
76+
std::vector<std::string> imports; // the modules it imports
77+
};
78+
struct Applied {
79+
std::vector<Fragment> fragments;
80+
std::vector<Importer> importers;
81+
std::string fragmentCheck; // the `private_fragment` value that checks, for this compiler
82+
};
83+
inline constexpr std::string_view kAppliedFile = "buildopt-applied.tsv";
84+
// Written when it differs from what is there.
85+
void write_applied(const std::filesystem::path& dir, const Applied& applied);
86+
Applied read_applied(const std::filesystem::path& dir);
87+
88+
// The step that failed: the first input of each failed step (the source, for a
89+
// compile), and whether the linker said a link failed.
90+
struct Failed {
91+
std::vector<std::string> sources;
92+
bool linked = false;
93+
};
94+
// The outputs each `FAILED:` line of ninja's output names.
95+
std::vector<std::vector<std::string>> failed_outputs(std::string_view ninjaOutput);
96+
// Whether the output says a link failed, in either linker's words.
97+
bool linker_failed(std::string_view ninjaOutput);
98+
99+
struct Tip {
100+
std::string domain;
101+
std::string text;
102+
};
103+
// The tips for a failure. `kept` names the BMIs the build directory keeps with
104+
// the lines of an earlier source (`bmi-kept.list`).
105+
std::vector<Tip> suspects(const Applied& applied, const std::vector<std::string>& kept, const Failed& failed);
106+
56107
} // namespace mcpp::buildopt::advice
57108

58109
namespace mcpp::buildopt::advice {
@@ -109,6 +160,29 @@ std::string line(const Cascade& c) {
109160
c.source, c.module, c.importers, c.importers);
110161
}
111162

163+
namespace {
164+
165+
// A source as a step names it: relative to its package's root, or absolute.
166+
// The plan's units are absolute, so one names the other when it is the tail.
167+
bool names(std::string_view planned, std::string_view stated) {
168+
if (stated.empty()) return false;
169+
if (planned == stated) return true;
170+
return planned.size() > stated.size() && planned.ends_with(stated)
171+
&& planned[planned.size() - stated.size() - 1] == '/';
172+
}
173+
174+
std::string bmi_stem_of(std::string module) {
175+
std::ranges::replace(module, ':', '-');
176+
return module;
177+
}
178+
179+
// The invocation that checks one entry, and the statement that keeps it.
180+
std::string how_to_check(const registry::Entry& e, std::string_view check) {
181+
return std::format("{}={} mcpp build ({} = \"{}\" keeps it)", e.env, check, e.key, check);
182+
}
183+
184+
} // namespace
185+
112186
bool first_time(const std::filesystem::path& dir, std::string_view module) {
113187
const auto file = dir / "buildopt-advice.list";
114188
{
@@ -121,4 +195,136 @@ bool first_time(const std::filesystem::path& dir, std::string_view module) {
121195
return true;
122196
}
123197

198+
void write_applied(const std::filesystem::path& dir, const Applied& applied) {
199+
std::string text;
200+
if (!applied.fragments.empty()) text += std::format("check\t{}\n", applied.fragmentCheck);
201+
for (auto const& f : applied.fragments) {
202+
text += std::format("fragment\t{}", f.written);
203+
for (auto const& u : f.units) text += "\t" + u;
204+
text += "\n";
205+
}
206+
for (auto const& i : applied.importers) {
207+
text += std::format("imports\t{}", i.source);
208+
for (auto const& m : i.imports) text += "\t" + m;
209+
text += "\n";
210+
}
211+
const auto file = dir / kAppliedFile;
212+
std::string have;
213+
{
214+
std::ifstream in(file, std::ios::binary);
215+
have.assign(std::istreambuf_iterator<char>(in), {});
216+
}
217+
if (have == text) return;
218+
std::ofstream(file, std::ios::binary | std::ios::trunc) << text;
219+
}
220+
221+
Applied read_applied(const std::filesystem::path& dir) {
222+
Applied a;
223+
std::ifstream in(dir / kAppliedFile, std::ios::binary);
224+
for (std::string line; std::getline(in, line);) {
225+
std::vector<std::string> cells;
226+
for (std::size_t at = 0;;) {
227+
const auto tab = line.find('\t', at);
228+
cells.push_back(line.substr(at, tab == std::string::npos ? tab : tab - at));
229+
if (tab == std::string::npos) break;
230+
at = tab + 1;
231+
}
232+
if (cells.size() < 2) continue;
233+
if (cells[0] == "check") a.fragmentCheck = cells[1];
234+
else if (cells[0] == "fragment") a.fragments.push_back({cells[1], {cells.begin() + 2, cells.end()}});
235+
else if (cells[0] == "imports") a.importers.push_back({cells[1], {cells.begin() + 2, cells.end()}});
236+
}
237+
return a;
238+
}
239+
240+
std::vector<std::vector<std::string>> failed_outputs(std::string_view output) {
241+
std::vector<std::vector<std::string>> steps;
242+
while (!output.empty()) {
243+
const auto end = output.find('\n');
244+
auto line = output.substr(0, end);
245+
output = end == std::string_view::npos ? std::string_view{} : output.substr(end + 1);
246+
while (!line.empty() && (line.front() == ' ' || line.front() == '\t')) line.remove_prefix(1);
247+
if (!line.starts_with("FAILED:")) continue;
248+
line.remove_prefix(7);
249+
if (line.find("[code=") != std::string_view::npos) line = line.substr(line.find(']') + 1);
250+
std::vector<std::string> outputs;
251+
for (std::size_t at = 0; at < line.size();) {
252+
const auto next = line.find(' ', at);
253+
const auto word = line.substr(at, next == std::string_view::npos ? next : next - at);
254+
if (!word.empty()) outputs.emplace_back(word);
255+
if (next == std::string_view::npos) break;
256+
at = next + 1;
257+
}
258+
if (!outputs.empty()) steps.push_back(std::move(outputs));
259+
}
260+
return steps;
261+
}
262+
263+
bool linker_failed(std::string_view output) {
264+
for (std::string_view words : {"undefined reference to", "undefined symbol", "multiple definition of",
265+
"duplicate symbol", "ld returned", "ld.lld: error", "lld-link: error"})
266+
if (output.find(words) != std::string_view::npos) return true;
267+
return false;
268+
}
269+
270+
std::vector<Tip> suspects(const Applied& applied, const std::vector<std::string>& kept, const Failed& failed) {
271+
std::vector<Tip> tips;
272+
const auto* fragment = registry::find(registry::kPrivateFragment);
273+
const auto* cutoff = registry::find(registry::kGcmLocations);
274+
const std::string_view off = registry::kStandardEnv;
275+
276+
// R1: the step that failed compiled a source built as units of its own.
277+
for (auto const& f : applied.fragments) {
278+
const bool hit = std::ranges::any_of(f.units, [&](const std::string& unit) {
279+
return std::ranges::any_of(failed.sources, [&](const std::string& s) { return names(unit, s); });
280+
});
281+
if (!hit || !fragment) continue;
282+
tips.push_back({"buildopt/private-fragment", std::format(
283+
"{} was built as two units (private-fragment), and one of them failed.\n"
284+
" To check whether that is the cause: {}\n"
285+
" If it builds, report the file; {}=off turns every optimization off for one run.",
286+
f.written, how_to_check(*fragment, applied.fragmentCheck.empty() ? fragment->check : applied.fragmentCheck),
287+
off)});
288+
break;
289+
}
290+
291+
// R2: the step that failed compiled a unit that imports a BMI kept with the
292+
// lines of an earlier source.
293+
std::vector<std::string> keptStems;
294+
for (auto const& k : kept) keptStems.push_back(std::filesystem::path(k).stem().string());
295+
std::set<std::string> modules;
296+
for (auto const& i : applied.importers) {
297+
if (!std::ranges::any_of(failed.sources, [&](const std::string& s) { return names(i.source, s); })) continue;
298+
for (auto const& m : i.imports)
299+
if (std::ranges::contains(keptStems, bmi_stem_of(m))) modules.insert(m);
300+
}
301+
if (!modules.empty() && cutoff) {
302+
std::string list;
303+
std::size_t shown = 0;
304+
for (auto const& m : modules) {
305+
if (shown++ == 3) { list += ", ..."; break; }
306+
list += (list.empty() ? "" : ", ") + m;
307+
}
308+
tips.push_back({"buildopt/bmi-cutoff", std::format(
309+
"the BMI of module {} was kept although its source changed (bmi_cutoff), and a unit that imports it failed.\n"
310+
" To check whether that is the cause: {}\n"
311+
" If it builds, report the module; {}=off turns every optimization off for one run.",
312+
list, how_to_check(*cutoff, cutoff->check), off)});
313+
}
314+
315+
// R3: a link failed, and the graph holds something either entry acted on.
316+
if (tips.empty() && failed.linked) {
317+
if (!applied.fragments.empty() && fragment)
318+
tips.push_back({"buildopt/private-fragment", std::format(
319+
"the link failed, and {} source(s) of this build are built as two units (private-fragment); the link cannot say whether that is the cause.\n"
320+
" To check: {}", applied.fragments.size(),
321+
how_to_check(*fragment, applied.fragmentCheck.empty() ? fragment->check : applied.fragmentCheck))});
322+
if (!kept.empty() && cutoff)
323+
tips.push_back({"buildopt/bmi-cutoff", std::format(
324+
"the link failed, and {} BMI(s) of this build are kept with the lines of an earlier source (bmi_cutoff); the link cannot say whether that is the cause.\n"
325+
" To check: {}", kept.size(), how_to_check(*cutoff, cutoff->check))});
326+
}
327+
return tips;
328+
}
329+
124330
} // namespace mcpp::buildopt::advice

‎modules/buildopt/src/keep/keep.cppm‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,10 @@ struct Run {
8383
};
8484
Run run_end(const std::filesystem::path& dir);
8585

86+
// The BMIs the directory keeps with the lines of an earlier source, as the
87+
// record names them (`bmi-kept.list`); what a failed step is asked against.
88+
std::vector<std::string> kept(const std::filesystem::path& dir);
89+
8690
// What a run says of its BMIs after it, one line each; empty when none changed.
8791
// `the BMI of c.gcm changed: its importers were compiled again`, and for a
8892
// kept one what was not done and the key that states otherwise.
@@ -169,6 +173,10 @@ void run_begin(const std::filesystem::path& dir, std::optional<bool> ignoreLocat
169173
std::filesystem::remove(keptFile, ec);
170174
}
171175

176+
std::vector<std::string> kept(const std::filesystem::path& dir) {
177+
return read_lines(dir / kKeptFile);
178+
}
179+
172180
std::vector<std::string> summary(const Run& run) {
173181
const auto names = [](const std::vector<std::string>& v) {
174182
std::string s;

‎modules/buildopt/src/registry.cppm‎

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,15 @@ struct Entry {
6060
std::string_view key; // the manifest key that states otherwise; empty: none
6161
std::string_view env; // the same for one invocation
6262
std::string_view values; // the key's values, the default first
63+
// The value that checks the entry: stated for one invocation, it turns this
64+
// entry off and no other, so a failure that is the entry's goes with it.
65+
// Empty: the entry has no value of its own to check with, and
66+
// `MCPP_BUILDOPT=off` is the one statement (the failure advice reads it).
67+
std::string_view check;
68+
// The same on a compiler where `check` is not available: GCC cannot build a
69+
// private module fragment as written, so the check there is the rule before
70+
// the entry's narrowing (`conservative`). Empty: `check` holds everywhere.
71+
std::string_view checkGcc;
6372
};
6473

6574
inline constexpr std::string_view kKeepBmi = "keep-bmi";
@@ -79,14 +88,14 @@ inline constexpr std::array<Entry, 7> kEntries{{
7988
Seam::Edge, Level::Exact, true, {}, {}, {}},
8089
{kGcmLocations, "a GCC BMI whose line maps alone moved is kept; its importers name the earlier lines",
8190
Seam::Edge, Level::Conditional, true, "[profile.<name>] bmi_cutoff", "MCPP_BMI_CUTOFF",
82-
"ignore-locations | exact"},
91+
"ignore-locations | exact", "exact"},
8392
{kScanPass, "the scans of a graph run as a pass of their own before its compiles",
8493
Seam::Graph, Level::Exact, true, {}, {}, {}},
8594
{kSplitSchedule, "importers start when a BMI exists, not when its compiler exits (two-phase, detached code generation)",
8695
Seam::Graph, Level::Shape, false, "[build] bmi_schedule", "MCPP_BMI_SCHEDULE", "auto | on | off"},
8796
{kPrivateFragment, "a private module fragment is built as a unit of its own, so an edit of it reaches no importer",
8897
Seam::Source, Level::Source, true, "[build] private_fragment", "MCPP_PRIVATE_FRAGMENT",
89-
"unit | as-written"},
98+
"unit | conservative | as-written", "as-written", "conservative"},
9099
}};
91100

92101
constexpr const Entry* find(std::string_view id) {

0 commit comments

Comments
 (0)