-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[MLIR] Make More Specific Function Header For StringLiteral Optimization in Diagnostic
#112154
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
Conversation
Thank you for submitting a Pull Request (PR) to the LLVM Project! This PR will be automatically labeled and the relevant teams will be notified. If you wish to, you can add reviewers by using the "Reviewers" section on this page. If this is not working for you, it is probably because you do not have write permissions for the repository. In which case you can instead tag reviewers by name in a comment by using If you have received no comments on your PR for a week, you can request a review by "ping"ing the PR by adding a comment “Ping”. The common courtesy "ping" rate is once a week. Please remember that you are asking for valuable time from other developers. If you have further questions, they may be answered by the LLVM GitHub User Guide. You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums. |
@llvm/pr-subscribers-mlir-core Author: Andrew Luo (AndrewZhaoLuo) ChangesDiagnostic stores various notes/error messages which might help the user in debugging. For the most part, the However, there is one optimization where given a This is problematic since We solve this problem by choosing a more specific function signature. While not full-proof, this should cover a lot more cases. A potentially better alternative is just deleting this special handling of string literals, but I am unsure of the implications (it does sound safe to do however with a negligble impact on performance). Full diff: https://github.com/llvm/llvm-project/pull/112154.diff 3 Files Affected:
diff --git a/mlir/include/mlir/IR/Diagnostics.h b/mlir/include/mlir/IR/Diagnostics.h
index cb30bb3f59688a..8429325412dc97 100644
--- a/mlir/include/mlir/IR/Diagnostics.h
+++ b/mlir/include/mlir/IR/Diagnostics.h
@@ -183,7 +183,8 @@ class Diagnostic {
Diagnostic &operator<<(StringAttr val);
/// Stream in a string literal.
- Diagnostic &operator<<(const char *val) {
+ template <size_t n>
+ Diagnostic &operator<<(const char (&val)[n]) {
arguments.push_back(DiagnosticArgument(val));
return *this;
}
diff --git a/mlir/unittests/IR/CMakeLists.txt b/mlir/unittests/IR/CMakeLists.txt
index 547e536dd9cbbf..384116ba5c457e 100644
--- a/mlir/unittests/IR/CMakeLists.txt
+++ b/mlir/unittests/IR/CMakeLists.txt
@@ -4,6 +4,7 @@ add_mlir_unittest(MLIRIRTests
AffineMapTest.cpp
AttributeTest.cpp
AttrTypeReplacerTest.cpp
+ Diagnostic.cpp
DialectTest.cpp
InterfaceTest.cpp
IRMapping.cpp
diff --git a/mlir/unittests/IR/Diagnostic.cpp b/mlir/unittests/IR/Diagnostic.cpp
new file mode 100644
index 00000000000000..dfc83001f173bc
--- /dev/null
+++ b/mlir/unittests/IR/Diagnostic.cpp
@@ -0,0 +1,61 @@
+//===- Diagnostic.cpp - Dialect unit tests -------------------------------===//
+//
+// 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 "mlir/IR/Diagnostics.h"
+#include "mlir/Support/TypeID.h"
+#include "gtest/gtest.h"
+
+using namespace mlir;
+using namespace mlir::detail;
+
+namespace {
+
+TEST(DiagnosticLifetime, TestCopiesConstCharStar) {
+ const auto *expectedMessage = "Error 1, don't mutate this";
+
+ // Copy expected message into a mutable container, and call the constructor.
+ std::string myStr(expectedMessage);
+
+ mlir::MLIRContext context;
+ Diagnostic diagnostic(mlir::UnknownLoc::get(&context), DiagnosticSeverity::Note);
+ diagnostic << myStr.c_str();
+
+ // Mutate underlying pointer, but ensure diagnostic still has orig. message
+ myStr[0] = '^';
+
+ std::string resultMessage;
+ llvm::raw_string_ostream stringStream(resultMessage);
+ diagnostic.print(stringStream);
+ ASSERT_STREQ(expectedMessage, resultMessage.c_str());
+}
+
+TEST(DiagnosticLifetime, TestLazyCopyStringLiteral) {
+ char charArr[21] = "Error 1, mutate this";
+ mlir::MLIRContext context;
+ Diagnostic diagnostic(mlir::UnknownLoc::get(&context), DiagnosticSeverity::Note);
+
+ // Diagnostic contains optimization which assumes string literals are
+ // represented by `const char[]` type. This is imperfect as we can sometimes
+ // trick the type system as seen below.
+ //
+ // Still we use this to check the diagnostic is lazily storing the pointer.
+ auto addToDiagnosticAsConst = [&diagnostic](const char (&charArr)[21]) {
+ diagnostic << charArr;
+ };
+ addToDiagnosticAsConst(charArr);
+
+ // Mutate the underlying pointer and ensure the string does change
+ charArr[0] = '^';
+
+ std::string resultMessage;
+ llvm::raw_string_ostream stringStream(resultMessage);
+ diagnostic.print(stringStream);
+ ASSERT_STREQ("^rror 1, mutate this", resultMessage.c_str());
+}
+
+} // namespace
|
@llvm/pr-subscribers-mlir Author: Andrew Luo (AndrewZhaoLuo) ChangesDiagnostic stores various notes/error messages which might help the user in debugging. For the most part, the However, there is one optimization where given a This is problematic since We solve this problem by choosing a more specific function signature. While not full-proof, this should cover a lot more cases. A potentially better alternative is just deleting this special handling of string literals, but I am unsure of the implications (it does sound safe to do however with a negligble impact on performance). Full diff: https://github.com/llvm/llvm-project/pull/112154.diff 3 Files Affected:
diff --git a/mlir/include/mlir/IR/Diagnostics.h b/mlir/include/mlir/IR/Diagnostics.h
index cb30bb3f59688a..8429325412dc97 100644
--- a/mlir/include/mlir/IR/Diagnostics.h
+++ b/mlir/include/mlir/IR/Diagnostics.h
@@ -183,7 +183,8 @@ class Diagnostic {
Diagnostic &operator<<(StringAttr val);
/// Stream in a string literal.
- Diagnostic &operator<<(const char *val) {
+ template <size_t n>
+ Diagnostic &operator<<(const char (&val)[n]) {
arguments.push_back(DiagnosticArgument(val));
return *this;
}
diff --git a/mlir/unittests/IR/CMakeLists.txt b/mlir/unittests/IR/CMakeLists.txt
index 547e536dd9cbbf..384116ba5c457e 100644
--- a/mlir/unittests/IR/CMakeLists.txt
+++ b/mlir/unittests/IR/CMakeLists.txt
@@ -4,6 +4,7 @@ add_mlir_unittest(MLIRIRTests
AffineMapTest.cpp
AttributeTest.cpp
AttrTypeReplacerTest.cpp
+ Diagnostic.cpp
DialectTest.cpp
InterfaceTest.cpp
IRMapping.cpp
diff --git a/mlir/unittests/IR/Diagnostic.cpp b/mlir/unittests/IR/Diagnostic.cpp
new file mode 100644
index 00000000000000..dfc83001f173bc
--- /dev/null
+++ b/mlir/unittests/IR/Diagnostic.cpp
@@ -0,0 +1,61 @@
+//===- Diagnostic.cpp - Dialect unit tests -------------------------------===//
+//
+// 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 "mlir/IR/Diagnostics.h"
+#include "mlir/Support/TypeID.h"
+#include "gtest/gtest.h"
+
+using namespace mlir;
+using namespace mlir::detail;
+
+namespace {
+
+TEST(DiagnosticLifetime, TestCopiesConstCharStar) {
+ const auto *expectedMessage = "Error 1, don't mutate this";
+
+ // Copy expected message into a mutable container, and call the constructor.
+ std::string myStr(expectedMessage);
+
+ mlir::MLIRContext context;
+ Diagnostic diagnostic(mlir::UnknownLoc::get(&context), DiagnosticSeverity::Note);
+ diagnostic << myStr.c_str();
+
+ // Mutate underlying pointer, but ensure diagnostic still has orig. message
+ myStr[0] = '^';
+
+ std::string resultMessage;
+ llvm::raw_string_ostream stringStream(resultMessage);
+ diagnostic.print(stringStream);
+ ASSERT_STREQ(expectedMessage, resultMessage.c_str());
+}
+
+TEST(DiagnosticLifetime, TestLazyCopyStringLiteral) {
+ char charArr[21] = "Error 1, mutate this";
+ mlir::MLIRContext context;
+ Diagnostic diagnostic(mlir::UnknownLoc::get(&context), DiagnosticSeverity::Note);
+
+ // Diagnostic contains optimization which assumes string literals are
+ // represented by `const char[]` type. This is imperfect as we can sometimes
+ // trick the type system as seen below.
+ //
+ // Still we use this to check the diagnostic is lazily storing the pointer.
+ auto addToDiagnosticAsConst = [&diagnostic](const char (&charArr)[21]) {
+ diagnostic << charArr;
+ };
+ addToDiagnosticAsConst(charArr);
+
+ // Mutate the underlying pointer and ensure the string does change
+ charArr[0] = '^';
+
+ std::string resultMessage;
+ llvm::raw_string_ostream stringStream(resultMessage);
+ diagnostic.print(stringStream);
+ ASSERT_STREQ("^rror 1, mutate this", resultMessage.c_str());
+}
+
+} // namespace
|
✅ With the latest revision this PR passed the C/C++ code formatter. |
…ion in `Diagnostic` Diagnostic stores various notes/error messages which might help the user in debugging. For the most part, the `Diagnostic` when receiving an error message will copy and own the contents of the string. However, there is one optimization where given a `const char*`, the class will assume this is a StringLiteral which is immutable and lifetime matches that of the entire program. As a result, instead of copying the message in these cases the class will simply store the underlying pointer. This is problematic since `const char*` is not specific enough to always imply a StringLiteral which can lead to bugs, e.g. if the underlying pointer is freed before the diagnostic reports. We solve this problem by choosing a more specific function signature. While not full-proof, this should cover a lot more cases. A potentially better alternative is just deleting this special handling of string literals, but I am unsure of the implications (it does sound safe to do however with a negligble impact on performance).
dd4bc6a
to
f01d5dc
Compare
nice, thank you for fixing this! |
@AndrewZhaoLuo Congratulations on having your first Pull Request (PR) merged into the LLVM Project! Your changes will be combined with recent changes from other authors, then tested by our build bots. If there is a problem with a build, you may receive a report in an email or a comment on this PR. Please check whether problems have been caused by your change specifically, as the builds can include changes from many authors. It is not uncommon for your change to be included in a build that fails due to someone else's changes, or infrastructure issues. How to do this, and the rest of the post-merge process, is covered in detail here. If your change does cause a problem, it may be reverted, or you can revert it yourself. This is a normal part of LLVM development. You can fix your changes and open a new PR to merge them again. If you don't get any reports, no action is required from you. Your changes are working as expected, well done! |
…ion in `Diagnostic` (llvm#112154) Diagnostic stores various notes/error messages which might help the user in debugging. For the most part, the `Diagnostic` when receiving an error message will copy and own the contents of the string. However, there is one optimization where given a `const char*`, the class will assume this is a StringLiteral which is immutable and lifetime matches that of the entire program. As a result, instead of copying the message in these cases the class will simply store the underlying pointer. This is problematic since `const char*` is not specific enough to always imply a StringLiteral which can lead to bugs, e.g. if the underlying pointer is freed before the diagnostic reports. We solve this problem by choosing a more specific function signature. While not full-proof, this should cover a lot more cases. A potentially better alternative is just deleting this special handling of string literals, but I am unsure of the implications (it does sound safe to do however with a negligble impact on performance).
Diagnostic stores various notes/error messages which might help the user in debugging. For the most part, the
Diagnostic
when receiving an error message will copy and own the contents of the string.However, there is one optimization where given a
const char*
, the class will assume this is a StringLiteral which is immutable and lifetime matches that of the entire program. As a result, instead of copying the message in these cases the class will simply store the underlying pointer.This is problematic since
const char*
is not specific enough to always imply a StringLiteral which can lead to bugs, e.g. if the underlying pointer is freed before the diagnostic reports.We solve this problem by choosing a more specific function signature. While not full-proof, this should cover a lot more cases.
A potentially better alternative is just deleting this special handling of string literals, but I am unsure of the implications (it does sound safe to do however with a negligble impact on performance).