Skip to content

[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

Merged
merged 1 commit into from
Oct 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion mlir/include/mlir/IR/Diagnostics.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
1 change: 1 addition & 0 deletions mlir/unittests/IR/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ add_mlir_unittest(MLIRIRTests
AffineMapTest.cpp
AttributeTest.cpp
AttrTypeReplacerTest.cpp
Diagnostic.cpp
DialectTest.cpp
InterfaceTest.cpp
IRMapping.cpp
Expand Down
63 changes: 63 additions & 0 deletions mlir/unittests/IR/Diagnostic.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
//===- 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
Loading