Skip to content

Commit dd4bc6a

Browse files
committed
[MLIR] Make More Specific Function Header For StringLiteral Optimization 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).
1 parent b2c615f commit dd4bc6a

File tree

3 files changed

+64
-1
lines changed

3 files changed

+64
-1
lines changed

mlir/include/mlir/IR/Diagnostics.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,8 @@ class Diagnostic {
183183
Diagnostic &operator<<(StringAttr val);
184184

185185
/// Stream in a string literal.
186-
Diagnostic &operator<<(const char *val) {
186+
template <size_t n>
187+
Diagnostic &operator<<(const char (&val)[n]) {
187188
arguments.push_back(DiagnosticArgument(val));
188189
return *this;
189190
}

mlir/unittests/IR/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ add_mlir_unittest(MLIRIRTests
44
AffineMapTest.cpp
55
AttributeTest.cpp
66
AttrTypeReplacerTest.cpp
7+
Diagnostic.cpp
78
DialectTest.cpp
89
InterfaceTest.cpp
910
IRMapping.cpp

mlir/unittests/IR/Diagnostic.cpp

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
//===- Diagnostic.cpp - Dialect unit tests -------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#include "mlir/IR/Diagnostics.h"
10+
#include "mlir/Support/TypeID.h"
11+
#include "gtest/gtest.h"
12+
13+
using namespace mlir;
14+
using namespace mlir::detail;
15+
16+
namespace {
17+
18+
TEST(DiagnosticLifetime, TestCopiesConstCharStar) {
19+
const auto *expectedMessage = "Error 1, don't mutate this";
20+
21+
// Copy expected message into a mutable container, and call the constructor.
22+
std::string myStr(expectedMessage);
23+
24+
mlir::MLIRContext context;
25+
Diagnostic diagnostic(mlir::UnknownLoc::get(&context), DiagnosticSeverity::Note);
26+
diagnostic << myStr.c_str();
27+
28+
// Mutate underlying pointer, but ensure diagnostic still has orig. message
29+
myStr[0] = '^';
30+
31+
std::string resultMessage;
32+
llvm::raw_string_ostream stringStream(resultMessage);
33+
diagnostic.print(stringStream);
34+
ASSERT_STREQ(expectedMessage, resultMessage.c_str());
35+
}
36+
37+
TEST(DiagnosticLifetime, TestLazyCopyStringLiteral) {
38+
char charArr[21] = "Error 1, mutate this";
39+
mlir::MLIRContext context;
40+
Diagnostic diagnostic(mlir::UnknownLoc::get(&context), DiagnosticSeverity::Note);
41+
42+
// Diagnostic contains optimization which assumes string literals are
43+
// represented by `const char[]` type. This is imperfect as we can sometimes
44+
// trick the type system as seen below.
45+
//
46+
// Still we use this to check the diagnostic is lazily storing the pointer.
47+
auto addToDiagnosticAsConst = [&diagnostic](const char (&charArr)[21]) {
48+
diagnostic << charArr;
49+
};
50+
addToDiagnosticAsConst(charArr);
51+
52+
// Mutate the underlying pointer and ensure the string does change
53+
charArr[0] = '^';
54+
55+
std::string resultMessage;
56+
llvm::raw_string_ostream stringStream(resultMessage);
57+
diagnostic.print(stringStream);
58+
ASSERT_STREQ("^rror 1, mutate this", resultMessage.c_str());
59+
}
60+
61+
} // namespace

0 commit comments

Comments
 (0)