-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[StrTable] Use string literal emission for intrinsics on non-MSVC platforms #124856
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
c63fe05
[StrTable] Use string literal emission for non-MSVC platforms
rnk 1761ecd
fix copy paste and update comment
rnk 9f71e6b
Use long-string-literals with MSVC for other subprojects (clang/mlir/…
rnk 5bdcaef
Merge branch 'main' into strtab-longstr
rnk 2fdde27
Move new cl::opt to lib/TableGen/Main.cpp so it always gets registered
rnk 110c18d
Fix for mlir-src-sharder
rnk File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
//===- StringToOffsetTable.cpp - Emit a big concatenated string -*- C++ -*-===// | ||
// | ||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#include "llvm/TableGen/StringToOffsetTable.h" | ||
#include "llvm/Support/FormatVariadic.h" | ||
#include "llvm/Support/raw_ostream.h" | ||
#include "llvm/TableGen/Main.h" | ||
|
||
using namespace llvm; | ||
|
||
unsigned StringToOffsetTable::GetOrAddStringOffset(StringRef Str, | ||
bool appendZero) { | ||
auto [II, Inserted] = StringOffset.insert({Str, size()}); | ||
if (Inserted) { | ||
// Add the string to the aggregate if this is the first time found. | ||
AggregateString.append(Str.begin(), Str.end()); | ||
if (appendZero) | ||
AggregateString += '\0'; | ||
} | ||
|
||
return II->second; | ||
} | ||
|
||
void StringToOffsetTable::EmitStringTableDef(raw_ostream &OS, const Twine &Name, | ||
const Twine &Indent) const { | ||
OS << formatv(R"( | ||
#ifdef __GNUC__ | ||
#pragma GCC diagnostic push | ||
#pragma GCC diagnostic ignored "-Woverlength-strings" | ||
#endif | ||
{0}static constexpr char {1}Storage[] = )", | ||
Indent, Name); | ||
|
||
// MSVC silently miscompiles string literals longer than 64k in some | ||
// circumstances. The build system sets EmitLongStrLiterals to false when it | ||
// detects that it is targetting MSVC. When that option is false and the | ||
// string table is longer than 64k, emit it as an array of character | ||
// literals. | ||
bool UseChars = !EmitLongStrLiterals && AggregateString.size() > (64 * 1024); | ||
OS << (UseChars ? "{\n" : "\n"); | ||
|
||
llvm::ListSeparator LineSep(UseChars ? ",\n" : "\n"); | ||
llvm::SmallVector<StringRef> Strings(split(AggregateString, '\0')); | ||
// We should always have an empty string at the start, and because these are | ||
// null terminators rather than separators, we'll have one at the end as | ||
// well. Skip the end one. | ||
assert(Strings.front().empty() && "Expected empty initial string!"); | ||
assert(Strings.back().empty() && | ||
"Expected empty string at the end due to terminators!"); | ||
Strings.pop_back(); | ||
for (StringRef Str : Strings) { | ||
OS << LineSep << Indent << " "; | ||
// If we can, just emit this as a string literal to be concatenated. | ||
if (!UseChars) { | ||
OS << "\""; | ||
OS.write_escaped(Str); | ||
OS << "\\0\""; | ||
continue; | ||
} | ||
|
||
llvm::ListSeparator CharSep(", "); | ||
for (char C : Str) { | ||
OS << CharSep << "'"; | ||
OS.write_escaped(StringRef(&C, 1)); | ||
OS << "'"; | ||
} | ||
OS << CharSep << "'\\0'"; | ||
} | ||
OS << LineSep << Indent << (UseChars ? "};" : " ;"); | ||
|
||
OS << formatv(R"( | ||
#ifdef __GNUC__ | ||
#pragma GCC diagnostic pop | ||
#endif | ||
|
||
{0}static constexpr llvm::StringTable {1} = | ||
{0} {1}Storage; | ||
)", | ||
Indent, Name); | ||
} | ||
|
||
void StringToOffsetTable::EmitString(raw_ostream &O) const { | ||
// Escape the string. | ||
SmallString<256> EscapedStr; | ||
raw_svector_ostream(EscapedStr).write_escaped(AggregateString); | ||
|
||
O << " \""; | ||
unsigned CharsPrinted = 0; | ||
for (unsigned i = 0, e = EscapedStr.size(); i != e; ++i) { | ||
if (CharsPrinted > 70) { | ||
O << "\"\n \""; | ||
CharsPrinted = 0; | ||
} | ||
O << EscapedStr[i]; | ||
++CharsPrinted; | ||
|
||
// Print escape sequences all together. | ||
if (EscapedStr[i] != '\\') | ||
continue; | ||
|
||
assert(i + 1 < EscapedStr.size() && "Incomplete escape sequence!"); | ||
if (isDigit(EscapedStr[i + 1])) { | ||
assert(isDigit(EscapedStr[i + 2]) && isDigit(EscapedStr[i + 3]) && | ||
"Expected 3 digit octal escape!"); | ||
O << EscapedStr[++i]; | ||
O << EscapedStr[++i]; | ||
O << EscapedStr[++i]; | ||
CharsPrinted += 3; | ||
} else { | ||
O << EscapedStr[++i]; | ||
++CharsPrinted; | ||
} | ||
} | ||
O << "\""; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(Note that the updated coding standard doesn't require this line to contain filename/description/c++.)