Skip to content

Commit 5d84938

Browse files
committed
don't load rules in parallel (not worth it) and simplify code
1 parent 77c2b8c commit 5d84938

3 files changed

Lines changed: 42 additions & 91 deletions

File tree

cpp2rust/converter/mapper.cpp

Lines changed: 22 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -386,75 +386,35 @@ decltype(types_)::const_iterator search(clang::QualType qual_type) {
386386
llvm::errs() << "search type " << type << ", result: "
387387
<< ((result == types_.end()) ? "None"
388388
: result->second.type_info.type)
389-
<< "\n";
389+
<< '\n';
390390
return result;
391391
}
392392

393393
void addRulesFromDirectory(const std::filesystem::path &dir, Model model) {
394394
std::vector<std::filesystem::path> paths;
395395
for (const auto &entry : std::filesystem::recursive_directory_iterator(dir)) {
396-
if (entry.is_regular_file() && entry.path().extension() == ".cpp") {
397-
paths.push_back(entry.path());
398-
}
399-
}
400-
401-
if (paths.empty()) {
402-
llvm::errs() << "Warning: no rules in " << dir << "\n";
403-
return;
404-
}
405-
406-
const unsigned hw = std::max(1u, std::thread::hardware_concurrency());
407-
const unsigned nthreads = std::min(hw, static_cast<unsigned>(paths.size()));
408-
std::atomic<size_t> next{0};
409-
410-
std::vector<std::vector<TranslationRule::Rule>> buckets(nthreads);
411-
412-
auto worker = [&](unsigned tid) {
413-
auto &local = buckets[tid];
414-
415-
while (true) {
416-
size_t i = next.fetch_add(1, std::memory_order_relaxed);
417-
if (i >= paths.size()) {
418-
break;
419-
}
420-
421-
auto rules = TranslationRule::Load(paths[i], model);
396+
auto &path = entry.path();
397+
if (entry.is_regular_file() && path.extension() == ".cpp") {
398+
auto rules = TranslationRule::Load(path, model);
422399
if (rules.empty()) {
423-
llvm::errs() << "No rules found in " << paths[i] << "\n";
424-
break;
400+
llvm::errs() << "No rules found in " << path << '\n';
401+
continue;
425402
}
426-
427403
for (auto &rule : rules) {
428-
local.push_back(std::move(rule));
429-
}
430-
}
431-
};
432-
433-
{
434-
llvm::DefaultThreadPool pool(
435-
llvm::heavyweight_hardware_concurrency(nthreads));
436-
for (unsigned t = 0; t < nthreads; ++t)
437-
pool.async(worker, t);
438-
pool.wait();
439-
}
440-
441-
for (auto &bucket : buckets) {
442-
for (auto &rule : bucket) {
443-
if (auto *expr = std::get_if<TranslationRule::ExprTgt>(&rule.tgt)) {
444-
if (exprs_.contains(rule.src)) {
445-
llvm::errs() << "Key: " << rule.src << ", added from "
446-
<< rule.src_path << " already exists in exprs_\n";
447-
assert(0);
448-
}
449-
exprs_[rule.src] = std::move(*expr);
450-
} else if (auto *type =
451-
std::get_if<TranslationRule::TypeTgt>(&rule.tgt)) {
452-
if (types_.contains(rule.src)) {
453-
llvm::errs() << "Key: " << rule.src << ", added from "
454-
<< rule.src_path << " already exists in types_\n";
455-
assert(0);
404+
if (auto *expr = std::get_if<TranslationRule::ExprTgt>(&rule.tgt)) {
405+
if (!exprs_.try_emplace(std::move(rule.src), std::move(*expr))
406+
.second) {
407+
llvm::errs() << "Key: " << rule.src << " already exists in exprs\n";
408+
assert(0);
409+
}
410+
} else if (auto *type =
411+
std::get_if<TranslationRule::TypeTgt>(&rule.tgt)) {
412+
if (!types_.try_emplace(std::move(rule.src), std::move(*type))
413+
.second) {
414+
llvm::errs() << "Key: " << rule.src << " already exists in types\n";
415+
assert(0);
416+
}
456417
}
457-
types_[rule.src] = *type;
458418
}
459419
}
460420
}
@@ -574,7 +534,7 @@ std::string mapTypeStringRecursive(const std::string &cpp_type) {
574534
auto rule = parallel_search(
575535
types_, [&](std::string tpl) { return matchTemplate(tpl, cpp_type); });
576536
if (rule == types_.end()) {
577-
llvm::errs() << "cpp_type: " << cpp_type << "\n";
537+
llvm::errs() << "cpp_type: " << cpp_type << '\n';
578538
assert(0 && "Type is not present in types_");
579539
}
580540
auto subs = matchTemplate(rule->first, cpp_type).value();
@@ -893,11 +853,11 @@ void LoadTranslationRules(Model model, clang::ASTContext &ctx,
893853
addBuiltinTypes(model);
894854

895855
for (auto &[src, expr] : exprs_) {
896-
llvm::errs() << "Expr: " << src << "\n";
856+
llvm::errs() << "Expr: " << src << '\n';
897857
expr.dump();
898858
}
899859
for (auto &[src, type_tgt] : types_) {
900-
llvm::errs() << "Type: " << src << "\n";
860+
llvm::errs() << "Type: " << src << '\n';
901861
type_tgt.dump();
902862
}
903863
}

cpp2rust/converter/translation_rule.cpp

Lines changed: 20 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ Access ParseAccessJSON(llvm::StringRef value) {
185185
} else if (value == "move") {
186186
return Access::kMove;
187187
} else {
188-
llvm::errs() << "Invalid access value: " << value << "\n";
188+
llvm::errs() << "Invalid access value: " << value << '\n';
189189
assert(0);
190190
return Access::kRead;
191191
}
@@ -279,23 +279,16 @@ TypeTgt ParseTypeTgtJSON(const llvm::json::Object &obj) {
279279
RuleMap LoadSrc(const std::filesystem::path &src_path) {
280280
clang::tooling::FixedCompilationDatabase compilations(
281281
".", getPlatformClangFlags());
282-
std::vector<std::string> sources = {src_path.string()};
283-
284282
RuleMap out;
285283
ActionFactory factory(out);
286-
clang::tooling::ClangTool tool(compilations, sources);
284+
clang::tooling::ClangTool tool(compilations, {src_path.string()});
287285
tool.run(&factory);
288286

289287
if (out.empty()) {
290288
llvm::errs() << "Warning: no symbols found in return statements for file: "
291-
<< src_path << "\n";
289+
<< src_path << '\n';
292290
return out;
293291
}
294-
295-
for (auto &[name, rule] : out) {
296-
rule.src_path = src_path;
297-
}
298-
299292
return out;
300293
}
301294

@@ -309,7 +302,7 @@ RuleMap LoadTgtFromIR(const std::filesystem::path &json_path) {
309302
auto parsed = llvm::json::parse((*buf)->getBuffer());
310303
if (!parsed) {
311304
llvm::errs() << "Failed to parse IR JSON: " << json_path << ": "
312-
<< llvm::toString(parsed.takeError()) << "\n";
305+
<< llvm::toString(parsed.takeError()) << '\n';
313306
assert(0);
314307
return out;
315308
}
@@ -360,7 +353,7 @@ void ValidateConsecutiveKeys(const Map &map, char prefix, int start,
360353
for (auto idx : indices) {
361354
llvm::errs() << " " << prefix << idx;
362355
}
363-
llvm::errs() << "\n";
356+
llvm::errs() << '\n';
364357
assert(0 && "indices must be consecutive");
365358
}
366359
}
@@ -389,13 +382,15 @@ void PlaceholderFragment::dump() const {
389382
llvm::errs() << " placeholder: " << arg << " (";
390383
switch (access) {
391384
case Access::kRead:
392-
llvm::errs() << "read";
385+
llvm::errs() << "read\n";
386+
break;
393387
case Access::kWrite:
394-
llvm::errs() << "write";
388+
llvm::errs() << "write\n";
389+
break;
395390
case Access::kMove:
396-
llvm::errs() << "move";
391+
llvm::errs() << "move\n";
392+
break;
397393
}
398-
llvm::errs() << "\n";
399394
}
400395

401396
const PlaceholderFragment *MethodCallFragment::getReceiverPlaceholder() const {
@@ -408,8 +403,8 @@ const PlaceholderFragment *MethodCallFragment::getReceiverPlaceholder() const {
408403
}
409404

410405
void MethodCallFragment::dump() const {
411-
llvm::errs() << " method_call:\n";
412-
llvm::errs() << " receiver:\n";
406+
llvm::errs() << " method_call:\n"
407+
" receiver:\n";
413408
for (const auto &frag : receiver) {
414409
BodyFragmentDump(frag);
415410
}
@@ -423,27 +418,27 @@ void ExprTgt::dump() const {
423418
for (auto &[name, info] : params) {
424419
llvm::errs() << " param " << name << ": ";
425420
info.dump();
426-
llvm::errs() << "\n";
421+
llvm::errs() << '\n';
427422
}
428423
if (!return_type.type.empty()) {
429424
llvm::errs() << " return: ";
430425
return_type.dump();
431-
llvm::errs() << "\n";
426+
llvm::errs() << '\n';
432427
}
433428
for (auto &[name, bounds] : generics) {
434429
llvm::errs() << " generic " << name << ":";
435430
for (auto &b : bounds) {
436431
llvm::errs() << " " << b;
437432
}
438-
llvm::errs() << "\n";
433+
llvm::errs() << '\n';
439434
}
440435
for (const auto &frag : body) {
441436
BodyFragmentDump(frag);
442437
}
443438
}
444439

445440
void GenericFragment::dump() const {
446-
llvm::errs() << " generic: " << name << "\n";
441+
llvm::errs() << " generic: " << name << '\n';
447442
}
448443

449444
void TypeInfo::dump() const {
@@ -457,9 +452,9 @@ void TypeInfo::dump() const {
457452
void TypeTgt::dump() const {
458453
llvm::errs() << " type: ";
459454
type_info.dump();
460-
llvm::errs() << "\n";
455+
llvm::errs() << '\n';
461456
if (!initializer.empty()) {
462-
llvm::errs() << " init: " << initializer << "\n";
457+
llvm::errs() << " init: " << initializer << '\n';
463458
}
464459
}
465460

@@ -488,10 +483,7 @@ std::vector<Rule> Load(const std::filesystem::path &path, Model model) {
488483
return {};
489484
}
490485
for (auto &[name, src_rule] : src_rules) {
491-
auto it = rules.find(name);
492-
assert(it != rules.end() && "Source symbol should have a target");
493-
it->second.src = std::move(src_rule.src);
494-
it->second.src_path = std::move(src_rule.src_path);
486+
rules.at(name).src = std::move(src_rule.src);
495487
}
496488

497489
std::vector<Rule> result;

cpp2rust/converter/translation_rule.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,6 @@ struct TypeTgt {
9292
struct Rule {
9393
std::string src;
9494
std::variant<ExprTgt, TypeTgt> tgt;
95-
std::filesystem::path src_path;
9695
};
9796

9897
std::vector<Rule> Load(const std::filesystem::path &path, Model model);

0 commit comments

Comments
 (0)