mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-11-24 15:32:39 +00:00
Add Arm SIMD line scanning and extend it to build an id-end bitmask (#8093)
It turns out `memchr` alone left a bunch of performance on the table for Arm, similar to what we fixed for x86 with a dedicated AVX2 path. This PR lifts the SIMD line scanning into a `SourceBlock` helper class with scalar, x86, and Arm implementations. It uses the same technique as our hash tables to cross-check the scalar and SIMD implementations in debug builds. A key to making the Arm NEON implementation actually fast is avoiding a GPR-to-NEON round trip to do popcount. Beyond that, instead of scanning for the end of each identifier as it is lexed, `SourceBlock` also classifies every byte of the source while loading each 64-byte block to find newlines. This builds a one-bit-per-byte bitmap where each bit is set when the corresponding byte can't continue an identifier or keyword. This lets us compute the length of an identifier or keyword by loading an unaligned 8-bytes of the bitmap and counting the zero bits. If necessary, we loop, with a single 8-byte load for each additional 64-bytes of source scanned. We ensure the bitmap has sentinel bits past the end so that this can proceed throughout the source buffer. The result is essentially a 64-byte SWAR (SIMD Within A Register) computation of the identifier and keyword extent. We can also efficiently build this even with a scalar implementation, and so we always use the `SourceBlock` technique. This approach was originally described as an idea in a comment on the Arm path of `ScanForIdentifierPrefix`, along with the main challenge: the high latency of moving data from vector registers to general purpose registers. The SWAR approach, which only uses GPRs during lexing, neatly avoids that problem. This replaces both the byte-at-a-time loop and the SIMD identifier scan used on x86 -- we repurpose that code, and its nibble lookup tables to classify both newlines and the identifier ends. A test is added to ensure we continue to gracefully degrade on non-ASCII characters. The TODO to address these fully remains unchanged with this PR. My agent's benchmarks of the lex phase show the change in CPU time of each `BM_Compile*<Lang::Carbon, Phase::Lex>` benchmark by number of lines. For Arm, this uses an Apple M1 running Linux. For x86, this uses an AMD server, both with `-march=x86-64-v3` (v3) and with our default of `-march=x86-64-v2` (v2): | Benchmark | Target | 4K | 16K | 64K | 256K | | ---------------------- | ------ | -----: | -----: | -----: | -----: | | ApiFileDenseDecls | M1 | -15.4% | -18.4% | -18.5% | -17.0% | | | x86 v3 | -0.8% | ~ | ~ | -7.6% | | | x86 v2 | -8.7% | -7.1% | -11.1% | ~ | | ImplFile | M1 | -13.4% | -15.2% | -15.3% | -14.7% | | | x86 v3 | -5.8% | -4.1% | -11.0% | -5.9% | | | x86 v2 | -4.6% | -8.5% | -7.7% | -6.6% | | ApiFileRealisticHeader | M1 | -14.2% | -18.3% | -18.4% | -16.2% | | | x86 v3 | ~ | -2.2% | ~ | -3.2% | | | x86 v2 | -5.9% | -9.2% | -9.1% | ~ | `~` marks changes that weren't statistically significant (p >= 0.05), although their medians still improved by 3-9%. Assisted-by: Antigravity with Gemini and Opus --------- Co-authored-by: Dana Jansens <danakj@orodu.net>
This commit is contained in:
1 parent
e2603996e5
commit
4a46ea888f
3 files changed
+630
-263
No files matched your search
@@ -958,6 +958,82 @@ TEST_F(LexerTest, RawIdentifierIntroducerAtEndOfFile) {
|
||||
}
|
||||
}
|
||||
|
||||
TEST_F(LexerTest, SourceEndsNearBlockBoundary) {
|
||||
// The source is scanned in 64-byte blocks, with any final partial block
|
||||
// padded with zero bytes. Check sources ending just before, at, and just
|
||||
// after the end of a block, so the final block has 1, 63, or 64 bytes.
|
||||
for (int size : {1, 2, 63, 64, 65, 127, 128, 129}) {
|
||||
SCOPED_TRACE(llvm::formatv("Size: {0}", size));
|
||||
|
||||
// An identifier that ends at the end of the source.
|
||||
std::string id(size, 'a');
|
||||
auto& id_buffer = compile_helper_.GetTokenizedBuffer(id);
|
||||
EXPECT_FALSE(id_buffer.has_errors());
|
||||
EXPECT_THAT(
|
||||
id_buffer,
|
||||
HasTokens(llvm::ArrayRef<ExpectedToken>{
|
||||
{.kind = TokenKind::FileStart},
|
||||
{.kind = TokenKind::Identifier, .line = 1, .column = 1, .text = id},
|
||||
{.kind = TokenKind::FileEnd, .line = 1},
|
||||
}));
|
||||
|
||||
// A newline that is the last byte of the source.
|
||||
std::string line_text(size - 1, 'a');
|
||||
std::string line_source = line_text + "\n";
|
||||
auto& line_buffer = compile_helper_.GetTokenizedBuffer(line_source);
|
||||
EXPECT_FALSE(line_buffer.has_errors());
|
||||
llvm::SmallVector<ExpectedToken> line_expected = {
|
||||
{.kind = TokenKind::FileStart}};
|
||||
if (!line_text.empty()) {
|
||||
line_expected.push_back({.kind = TokenKind::Identifier,
|
||||
.line = 1,
|
||||
.column = 1,
|
||||
.text = line_text});
|
||||
}
|
||||
line_expected.push_back({.kind = TokenKind::FileEnd, .line = 1});
|
||||
EXPECT_THAT(line_buffer, HasTokens(llvm::ArrayRef(line_expected)));
|
||||
|
||||
// A newline just before the last byte of the source, which starts a
|
||||
// one-byte line.
|
||||
std::string next_line_source = line_text + "\nb";
|
||||
auto& next_line_buffer =
|
||||
compile_helper_.GetTokenizedBuffer(next_line_source);
|
||||
EXPECT_FALSE(next_line_buffer.has_errors());
|
||||
llvm::SmallVector<ExpectedToken> next_line_expected(
|
||||
line_expected.begin(), line_expected.end() - 1);
|
||||
next_line_expected.push_back(
|
||||
{.kind = TokenKind::Identifier, .line = 2, .column = 1, .text = "b"});
|
||||
next_line_expected.push_back({.kind = TokenKind::FileEnd, .line = 2});
|
||||
EXPECT_THAT(next_line_buffer,
|
||||
HasTokens(llvm::ArrayRef(next_line_expected)));
|
||||
}
|
||||
}
|
||||
|
||||
TEST_F(LexerTest, IdentifiersAcrossBlocks) {
|
||||
// The ends of identifiers are found with 64-bit loads from a bitmap built
|
||||
// from 64-byte blocks of the source. Check identifiers starting at a range of
|
||||
// offsets, with lengths around these sizes.
|
||||
for (int offset : {0, 1, 6, 7, 8, 63, 64}) {
|
||||
for (int length : {1, 2, 56, 57, 58, 63, 64, 65, 127, 128, 129}) {
|
||||
SCOPED_TRACE(llvm::formatv("Offset: {0}, length: {1}", offset, length));
|
||||
std::string id(length, 'a');
|
||||
std::string source = std::string(offset, ' ') + id + " b";
|
||||
auto& buffer = compile_helper_.GetTokenizedBuffer(source);
|
||||
EXPECT_FALSE(buffer.has_errors());
|
||||
EXPECT_THAT(
|
||||
buffer,
|
||||
HasTokens(llvm::ArrayRef<ExpectedToken>{
|
||||
{.kind = TokenKind::FileStart},
|
||||
{.kind = TokenKind::Identifier, .column = offset + 1, .text = id},
|
||||
{.kind = TokenKind::Identifier,
|
||||
.column = offset + length + 2,
|
||||
.text = "b"},
|
||||
{.kind = TokenKind::FileEnd},
|
||||
}));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
TEST_F(LexerTest, StringLiterals) {
|
||||
llvm::StringLiteral testcase = R"(
|
||||
"hello world\n"
|
||||
|
||||
Reference in new issue
Block a user