mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-09-24 22:02:23 +01:00
This switches `DCHECK` and `FATAL` as well. The goal is to reduce the code size impact of these assertions so that we can keep more of them enabled. Currently, the largest cost I see from `CHECK` is not the actual check or the cold code itself, but actually the failure to inline trivial functions due to the presence of the cold code. This means that our goal isn't to reduce apparent code size in the final binary but the LLVM IR cost assessed for these routines in the inliner, which closely correlates with code size but is a bit different. As discussed in #4283, experimentation shows that a single function call with a minimal number of arguments is the lowest cost model for these. This is easily achieved with a format-string API that internally uses `llvm::formatv`. This PR is essentially the `CHECK` version of #4283. However, the check macros are substantially harder to make work with both format strings and streaming because they also take a condition. Also, unexpectedly, I was very successful at devising a regular expression based automated rewrite from the streaming to the format string form with only low 10s of manual fixes. This includes compacting strings broken up across lines, etc. Given how well that went, I've prepared this PR which just directly switches to the format string API and migrate everything to use it. One nice side-effect is that the format string approach ends up greatly simplifying the implementation here as well. This is ... *shockingly* effective. Parsing speeds up by more than 3% with just this change. And checking speeds up by **8%** with this change alone: ``` BM_CompileAPIFileDenseDecls<Phase::Parse>/256 86.3µs ± 1% 82.9µs ± 1% -3.94% (p=0.000 n=17+19) BM_CompileAPIFileDenseDecls<Phase::Parse>/1024 431µs ± 1% 415µs ± 1% -3.76% (p=0.000 n=18+19) BM_CompileAPIFileDenseDecls<Phase::Parse>/4096 1.77ms ± 1% 1.71ms ± 1% -3.18% (p=0.000 n=18+19) BM_CompileAPIFileDenseDecls<Phase::Parse>/16384 7.44ms ± 1% 7.17ms ± 2% -3.56% (p=0.000 n=18+20) BM_CompileAPIFileDenseDecls<Phase::Parse>/65536 30.7ms ± 1% 29.7ms ± 1% -3.15% (p=0.000 n=18+20) BM_CompileAPIFileDenseDecls<Phase::Parse>/262144 131ms ± 1% 127ms ± 1% -2.81% (p=0.000 n=18+18) BM_CompileAPIFileDenseDecls<Phase::Check>/256 878µs ± 2% 800µs ± 1% -8.91% (p=0.000 n=19+20) BM_CompileAPIFileDenseDecls<Phase::Check>/1024 1.88ms ± 2% 1.72ms ± 1% -8.56% (p=0.000 n=19+20) BM_CompileAPIFileDenseDecls<Phase::Check>/4096 5.78ms ± 2% 5.28ms ± 1% -8.70% (p=0.000 n=20+18) BM_CompileAPIFileDenseDecls<Phase::Check>/16384 21.9ms ± 1% 20.1ms ± 1% -8.02% (p=0.000 n=18+20) BM_CompileAPIFileDenseDecls<Phase::Check>/65536 90.4ms ± 2% 83.1ms ± 1% -8.04% (p=0.000 n=19+20) BM_CompileAPIFileDenseDecls<Phase::Check>/262144 381ms ± 2% 352ms ± 1% -7.79% (p=0.000 n=19+19) ``` --------- Co-authored-by: Richard Smith <richard@metafoo.co.uk> Co-authored-by: josh11b <15258583+josh11b@users.noreply.github.com>
191 lines
6.9 KiB
C++
191 lines
6.9 KiB
C++
// Part of the Carbon Language project, under the Apache License v2.0 with LLVM
|
|
// Exceptions. See /LICENSE for license information.
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
|
|
|
#include "migrate_cpp/rewriter.h"
|
|
|
|
#include <gmock/gmock.h>
|
|
#include <gtest/gtest.h>
|
|
|
|
#include "clang/AST/ASTConsumer.h"
|
|
#include "clang/Frontend/CompilerInstance.h"
|
|
#include "clang/Frontend/FrontendAction.h"
|
|
#include "clang/Tooling/Tooling.h"
|
|
|
|
namespace Carbon::Testing {
|
|
namespace {
|
|
|
|
// Represents C++ source code with at most one region enclosed in $[[...]]$ as
|
|
// an annotated range.
|
|
class Annotations {
|
|
public:
|
|
explicit Annotations(llvm::StringRef annotated_source) {
|
|
size_t index = annotated_source.find("$[[");
|
|
if (index == llvm::StringRef::npos) {
|
|
source_code_ = std::string(annotated_source);
|
|
return;
|
|
}
|
|
start_ = index;
|
|
end_ = annotated_source.find("]]$", index);
|
|
CARBON_CHECK(end_ != llvm::StringRef::npos,
|
|
"Found `$[[` but no matching `]]$`");
|
|
source_code_ = (llvm::Twine(annotated_source.substr(0, start_)) +
|
|
annotated_source.substr(start_ + 3, end_ - start_ - 3) +
|
|
annotated_source.substr(end_ + 3))
|
|
.str();
|
|
// Update `end_` so that it is relative to the unannotated source (which
|
|
// means three characters earlier due to the `$[[` being removed.
|
|
end_ -= 3;
|
|
}
|
|
|
|
// Returns a view into the unannotated source.
|
|
auto source() const -> llvm::StringRef { return source_code_; }
|
|
|
|
// Returns the offsets in the file representing the annotated range if they
|
|
// exist and `{0, std::numeric_limits<size_t>::max()}` otherwise.
|
|
auto range() const -> std::pair<size_t, size_t> {
|
|
return std::pair(start_, end_);
|
|
}
|
|
|
|
private:
|
|
std::string source_code_;
|
|
size_t start_ = 0;
|
|
size_t end_ = std::numeric_limits<size_t>::max();
|
|
};
|
|
|
|
// Rewrites the `cpp_code`, return the Carbon equivalent. If the text has no
|
|
// source range annotated with $[[...]]$, the entire translation unit will be
|
|
// migrated and output. Otherwise, only the migrated output corresponding to the
|
|
// annotated range will be output. No more than one range may be annotated at
|
|
// all.
|
|
//
|
|
// This annotation mechanism is useful in that it allows us to specifically test
|
|
// the migration associated with specific nodes even when they require some
|
|
// additional context that we do not wish to be covered by the test.
|
|
auto RewriteText(llvm::StringRef cpp_code) -> std::string {
|
|
std::string result;
|
|
|
|
Annotations annotated_cpp_code(cpp_code);
|
|
|
|
bool success = clang::tooling::runToolOnCodeWithArgs(
|
|
std::make_unique<MigrationAction>(result, annotated_cpp_code.range()),
|
|
annotated_cpp_code.source(), {}, "test.cc", "clang-tool",
|
|
std::make_shared<clang::PCHContainerOperations>(),
|
|
clang::tooling::FileContentMappings());
|
|
|
|
return success ? result : "";
|
|
}
|
|
|
|
TEST(Rewriter, BoolLiteral) {
|
|
EXPECT_EQ(RewriteText("bool x = $[[true]]$;"), "true");
|
|
EXPECT_EQ(RewriteText("bool x = $[[false]]$;"), "false");
|
|
}
|
|
|
|
TEST(Rewriter, IntegerLiteral) {
|
|
EXPECT_EQ(RewriteText("int x = $[[0]]$;"), "0");
|
|
EXPECT_EQ(RewriteText("int x = $[[1]]$;"), "1");
|
|
EXPECT_EQ(RewriteText("int x = $[[1234]]$;"), "1234");
|
|
EXPECT_EQ(RewriteText("int x = $[[12'34]]$;"), "12_34");
|
|
EXPECT_EQ(RewriteText("int x = $[[12'3'4]]$;"), "12_3_4");
|
|
}
|
|
|
|
TEST(Rewriter, SingleDeclaration) {
|
|
EXPECT_EQ(RewriteText("bool b;"), "var b: bool;\n");
|
|
EXPECT_EQ(RewriteText("int i;"), "var i: i32;\n");
|
|
|
|
EXPECT_EQ(RewriteText("const bool b = false;"), "let b: bool = false;\n");
|
|
EXPECT_EQ(RewriteText("const int i = 17;"), "let i: i32 = 17;\n");
|
|
|
|
EXPECT_EQ(RewriteText("bool const b = false;"), "let b: bool = false;\n");
|
|
EXPECT_EQ(RewriteText("int const i = 1234;"), "let i: i32 = 1234;\n");
|
|
}
|
|
|
|
TEST(Rewriter, Pointers) {
|
|
// TODO: Add tests for pointers-to-const when the syntax is nailed down.
|
|
EXPECT_EQ(RewriteText("bool b;\n"
|
|
"$[[bool *p = &b]]$;"),
|
|
"var p: bool* = &b");
|
|
EXPECT_EQ(RewriteText("bool b;\n"
|
|
"$[[bool * const p = &b]]$;"),
|
|
"let p: bool* = &b");
|
|
|
|
// Pointers and non-pointers on the same DeclStmt.
|
|
EXPECT_EQ(RewriteText("bool b, *p;\n"),
|
|
"var b: bool;\n"
|
|
"var p: bool*;\n");
|
|
EXPECT_EQ(RewriteText("bool b, *p = &b;\n"),
|
|
"var b: bool;\n"
|
|
"var p: bool* = &b;\n");
|
|
}
|
|
|
|
TEST(Rewriter, DeclarationComma) {
|
|
EXPECT_EQ(RewriteText("int x, y;"),
|
|
"var x: i32;\n"
|
|
"var y: i32;\n");
|
|
EXPECT_EQ(RewriteText("int x = 7, y;"),
|
|
"var x: i32 = 7;\n"
|
|
"var y: i32;\n");
|
|
EXPECT_EQ(RewriteText("const int x = 1, y = 2;"),
|
|
"let x: i32 = 1;\n"
|
|
"let y: i32 = 2;\n");
|
|
EXPECT_EQ(RewriteText("int const x = 1234, y = 5678;"),
|
|
"let x: i32 = 1234;\n"
|
|
"let y: i32 = 5678;\n");
|
|
}
|
|
|
|
TEST(Rewriter, FunctionDeclaration) {
|
|
// Function declarations and definitions returning void.
|
|
EXPECT_EQ(RewriteText("void f();"), "fn f() -> ();\n");
|
|
EXPECT_EQ(RewriteText("void f() {}"),
|
|
"fn f() -> () {\n"
|
|
"}\n");
|
|
|
|
// Function declarations and definitions returning int.
|
|
EXPECT_EQ(RewriteText("int f();"), "fn f() -> i32;\n");
|
|
EXPECT_EQ(RewriteText("int f() { return 0; }"),
|
|
"fn f() -> i32 {\n"
|
|
"return 0;\n"
|
|
"}\n");
|
|
|
|
// Function declarations and definitions with a single parameter.
|
|
EXPECT_EQ(RewriteText("int f(bool);"), "fn f(_: bool) -> i32;\n");
|
|
EXPECT_EQ(RewriteText("int f(bool b);"), "fn f(b: bool) -> i32;\n");
|
|
EXPECT_EQ(RewriteText("int f(bool) { return 0; }"),
|
|
"fn f(_: bool) -> i32 {\n"
|
|
"return 0;\n"
|
|
"}\n");
|
|
EXPECT_EQ(RewriteText("int f(bool b) { return 0; }"),
|
|
"fn f(b: bool) -> i32 {\n"
|
|
"return 0;\n"
|
|
"}\n");
|
|
|
|
// Function declarations and definitions with a multiple parameters.
|
|
EXPECT_EQ(RewriteText("int f(bool, int);"),
|
|
"fn f(_: bool, _: i32) -> i32;\n");
|
|
EXPECT_EQ(RewriteText("int f(bool b, int n);"),
|
|
"fn f(b: bool, n: i32) -> i32;\n");
|
|
EXPECT_EQ(RewriteText("int f(bool, int n) { return 0; }"),
|
|
"fn f(_: bool, n: i32) -> i32 {\n"
|
|
"return 0;\n"
|
|
"}\n");
|
|
EXPECT_EQ(RewriteText("int f(bool b, int n) { return 0; }"),
|
|
"fn f(b: bool, n: i32) -> i32 {\n"
|
|
"return 0;\n"
|
|
"}\n");
|
|
EXPECT_EQ(RewriteText("int f(bool b, int n = 3) { return n; }"),
|
|
"fn f(b: bool, n: i32 = 3) -> i32 {\n"
|
|
"return n;\n"
|
|
"}\n");
|
|
|
|
// Function declarations with trailing-return syntax.
|
|
EXPECT_EQ(RewriteText("auto f(bool b, int n = 3) -> int;"),
|
|
"fn f(b: bool, n: i32 = 3) -> i32;\n");
|
|
EXPECT_EQ(RewriteText("auto f(bool b, int n = 3) -> int { return n; }"),
|
|
"fn f(b: bool, n: i32 = 3) -> i32 {\n"
|
|
"return n;\n"
|
|
"}\n");
|
|
}
|
|
|
|
} // namespace
|
|
} // namespace Carbon::Testing
|