Skip to content

Commit e511026

Browse files
[MLIR] Make More Specific Function Header For StringLiteral Optimization in Diagnostic (#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).
1 parent c9f2727 commit e511026

File tree

3 files changed

+66
-1
lines changed

3 files changed

+66
-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: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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),
26+
DiagnosticSeverity::Note);
27+
diagnostic << myStr.c_str();
28+
29+
// Mutate underlying pointer, but ensure diagnostic still has orig. message
30+
myStr[0] = '^';
31+
32+
std::string resultMessage;
33+
llvm::raw_string_ostream stringStream(resultMessage);
34+
diagnostic.print(stringStream);
35+
ASSERT_STREQ(expectedMessage, resultMessage.c_str());
36+
}
37+
38+
TEST(DiagnosticLifetime, TestLazyCopyStringLiteral) {
39+
char charArr[21] = "Error 1, mutate this";
40+
mlir::MLIRContext context;
41+
Diagnostic diagnostic(mlir::UnknownLoc::get(&context),
42+
DiagnosticSeverity::Note);
43+
44+
// Diagnostic contains optimization which assumes string literals are
45+
// represented by `const char[]` type. This is imperfect as we can sometimes
46+
// trick the type system as seen below.
47+
//
48+
// Still we use this to check the diagnostic is lazily storing the pointer.
49+
auto addToDiagnosticAsConst = [&diagnostic](const char(&charArr)[21]) {
50+
diagnostic << charArr;
51+
};
52+
addToDiagnosticAsConst(charArr);
53+
54+
// Mutate the underlying pointer and ensure the string does change
55+
charArr[0] = '^';
56+
57+
std::string resultMessage;
58+
llvm::raw_string_ostream stringStream(resultMessage);
59+
diagnostic.print(stringStream);
60+
ASSERT_STREQ("^rror 1, mutate this", resultMessage.c_str());
61+
}
62+
63+
} // namespace

0 commit comments

Comments
 (0)