Skip to content

Commit 42f87a0

Browse files
committed
[mlir][ods] NFC Fix ASAN error in FormatParser
Some FormatElement subclasses contain `std::vector`. Since these use BumpPtrAllocator, they need to be converted to trailing objects. However, this is not a trivial fix so I will leave it as a FIXME and use a workaround.
1 parent a91a00d commit 42f87a0

File tree

2 files changed

+15
-4
lines changed

2 files changed

+15
-4
lines changed

mlir/tools/mlir-tblgen/FormatGen.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,8 @@ FormatToken FormatLexer::lexIdentifier(const char *tokStart) {
181181
// FormatParser
182182
//===----------------------------------------------------------------------===//
183183

184+
FormatElement::~FormatElement() = default;
185+
184186
FormatParser::~FormatParser() = default;
185187

186188
FailureOr<std::vector<FormatElement *>> FormatParser::parse() {

mlir/tools/mlir-tblgen/FormatGen.h

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,9 @@ class FormatLexer {
158158
/// dialect.
159159
class FormatElement {
160160
public:
161-
/// The top-level kinds of format elements.
161+
virtual ~FormatElement();
162+
163+
// The top-level kinds of format elements.
162164
enum Kind { Literal, Variable, Whitespace, Directive, Optional };
163165

164166
/// Support LLVM-style RTTI.
@@ -410,8 +412,12 @@ class FormatParser {
410412
/// Allocate and construct a format element.
411413
template <typename FormatElementT, typename... Args>
412414
FormatElementT *create(Args &&...args) {
413-
FormatElementT *ptr = allocator.Allocate<FormatElementT>();
414-
::new (ptr) FormatElementT(std::forward<Args>(args)...);
415+
// FormatElementT *ptr = allocator.Allocate<FormatElementT>();
416+
// ::new (ptr) FormatElementT(std::forward<Args>(args)...);
417+
// return ptr;
418+
auto mem = std::make_unique<FormatElementT>(std::forward<Args>(args)...);
419+
FormatElementT *ptr = mem.get();
420+
allocator.push_back(std::move(mem));
415421
return ptr;
416422
}
417423

@@ -493,7 +499,10 @@ class FormatParser {
493499
private:
494500
/// The format parser retains ownership of the format elements in a bump
495501
/// pointer allocator.
496-
llvm::BumpPtrAllocator allocator;
502+
// FIXME: FormatElement with `std::vector` need to be converted to use
503+
// trailing objects.
504+
// llvm::BumpPtrAllocator allocator;
505+
std::vector<std::unique_ptr<FormatElement>> allocator;
497506
/// The format lexer to use.
498507
FormatLexer lexer;
499508
/// The current token in the lexer.

0 commit comments

Comments
 (0)