[llvm] r371233 - [Remarks] Add support for internalizing a remark in a string table
Francis Visoiu Mistrih via llvm-commits
llvm-commits at lists.llvm.org
Fri Sep 6 10:22:52 PDT 2019
Author: thegameg
Date: Fri Sep 6 10:22:51 2019
New Revision: 371233
URL: http://llvm.org/viewvc/llvm-project?rev=371233&view=rev
Log:
[Remarks] Add support for internalizing a remark in a string table
In order to keep remarks around, we need to make them tied to a string
table.
Users then can delete the parser and rely on the string table to keep
the memory of the strings alive and deduplicated.
Modified:
llvm/trunk/include/llvm/Remarks/RemarkStringTable.h
llvm/trunk/lib/Remarks/RemarkStringTable.cpp
llvm/trunk/unittests/Remarks/RemarksAPITest.cpp
Modified: llvm/trunk/include/llvm/Remarks/RemarkStringTable.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Remarks/RemarkStringTable.h?rev=371233&r1=371232&r2=371233&view=diff
==============================================================================
--- llvm/trunk/include/llvm/Remarks/RemarkStringTable.h (original)
+++ llvm/trunk/include/llvm/Remarks/RemarkStringTable.h Fri Sep 6 10:22:51 2019
@@ -51,6 +51,9 @@ struct StringTable {
/// Add a string to the table. It returns an unique ID of the string.
std::pair<unsigned, StringRef> add(StringRef Str);
+ /// Modify \p R to use strings from this string table. If the string table
+ /// does not contain the strings, it adds them.
+ void internalize(Remark &R);
/// Serialize the string table to a stream. It is serialized as a little
/// endian uint64 (the size of the table in bytes) followed by a sequence of
/// NULL-terminated strings, where the N-th string is the string with the ID N
Modified: llvm/trunk/lib/Remarks/RemarkStringTable.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Remarks/RemarkStringTable.cpp?rev=371233&r1=371232&r2=371233&view=diff
==============================================================================
--- llvm/trunk/lib/Remarks/RemarkStringTable.cpp (original)
+++ llvm/trunk/lib/Remarks/RemarkStringTable.cpp Fri Sep 6 10:22:51 2019
@@ -11,6 +11,7 @@
//===----------------------------------------------------------------------===//
#include "llvm/Remarks/RemarkStringTable.h"
+#include "llvm/Remarks/Remark.h"
#include "llvm/Remarks/RemarkParser.h"
#include "llvm/Support/EndianStream.h"
#include "llvm/Support/Error.h"
@@ -37,6 +38,22 @@ std::pair<unsigned, StringRef> StringTab
return {KV.first->second, KV.first->first()};
}
+void StringTable::internalize(Remark &R) {
+ auto Impl = [&](StringRef &S) { S = add(S).second; };
+ Impl(R.PassName);
+ Impl(R.RemarkName);
+ Impl(R.FunctionName);
+ if (R.Loc)
+ Impl(R.Loc->SourceFilePath);
+ for (Argument &Arg : R.Args) {
+ // We need to mutate elements from an ArrayRef here.
+ Impl(Arg.Key);
+ Impl(Arg.Val);
+ if (Arg.Loc)
+ Impl(Arg.Loc->SourceFilePath);
+ }
+}
+
void StringTable::serialize(raw_ostream &OS) const {
// Emit the sequence of strings.
for (StringRef Str : serialize()) {
Modified: llvm/trunk/unittests/Remarks/RemarksAPITest.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/Remarks/RemarksAPITest.cpp?rev=371233&r1=371232&r2=371233&view=diff
==============================================================================
--- llvm/trunk/unittests/Remarks/RemarksAPITest.cpp (original)
+++ llvm/trunk/unittests/Remarks/RemarksAPITest.cpp Fri Sep 6 10:22:51 2019
@@ -7,6 +7,7 @@
//===----------------------------------------------------------------------===//
#include "llvm/Remarks/Remark.h"
+#include "llvm/Remarks/RemarkStringTable.h"
#include "gtest/gtest.h"
using namespace llvm;
@@ -75,3 +76,48 @@ TEST(RemarksAPI, ArgsAsMsg) {
EXPECT_EQ(R.getArgsAsMsg(), "can not do this because of that.");
}
+
+TEST(RemarksAPI, StringTableInternalize) {
+ remarks::StringTable StrTab;
+
+ // Empty table.
+ EXPECT_EQ(StrTab.SerializedSize, 0UL);
+
+ remarks::Remark R;
+ R.RemarkType = remarks::Type::Missed;
+ R.PassName = "pass";
+ R.RemarkName = "name";
+ R.FunctionName = "func";
+ R.Loc = remarks::RemarkLocation{"path", 3, 4};
+ R.Args.emplace_back();
+ R.Args.back().Key = "keydebug";
+ R.Args.back().Val = "valuedebug";
+ R.Args.back().Loc = remarks::RemarkLocation{"argpath", 6, 7};
+
+ // Check that internalize starts using the strings from the string table.
+ remarks::Remark R2 = R.clone();
+ StrTab.internalize(R2);
+
+ // Check that the pointers in the remarks are different.
+ EXPECT_NE(R.PassName.data(), R2.PassName.data());
+ EXPECT_NE(R.RemarkName.data(), R2.RemarkName.data());
+ EXPECT_NE(R.FunctionName.data(), R2.FunctionName.data());
+ EXPECT_NE(R.Loc->SourceFilePath.data(), R2.Loc->SourceFilePath.data());
+ EXPECT_NE(R.Args.back().Key.data(), R2.Args.back().Key.data());
+ EXPECT_NE(R.Args.back().Val.data(), R2.Args.back().Val.data());
+ EXPECT_NE(R.Args.back().Loc->SourceFilePath.data(),
+ R2.Args.back().Loc->SourceFilePath.data());
+
+ // Check that the internalized remark is using the pointers from the string table.
+ EXPECT_EQ(StrTab.add(R.PassName).second.data(), R2.PassName.data());
+ EXPECT_EQ(StrTab.add(R.RemarkName).second.data(), R2.RemarkName.data());
+ EXPECT_EQ(StrTab.add(R.FunctionName).second.data(), R2.FunctionName.data());
+ EXPECT_EQ(StrTab.add(R.Loc->SourceFilePath).second.data(),
+ R2.Loc->SourceFilePath.data());
+ EXPECT_EQ(StrTab.add(R.Args.back().Key).second.data(),
+ R2.Args.back().Key.data());
+ EXPECT_EQ(StrTab.add(R.Args.back().Val).second.data(),
+ R2.Args.back().Val.data());
+ EXPECT_EQ(StrTab.add(R.Args.back().Loc->SourceFilePath).second.data(),
+ R2.Args.back().Loc->SourceFilePath.data());
+}
More information about the llvm-commits
mailing list