Skip to content

Commit d2d254e

Browse files
authored
[SYCL-MLIR] Use SmallString<0> to store arguments in ArgumentList (#7274)
std::string performs short string optimizations, so that moving an std::string might invalidate iterators to it. On the other hand, llvm::SmallString<0> is always heap-allocated, so moving does not involve invalidation. Signed-off-by: Victor Perez <[email protected]> Signed-off-by: Victor Perez <[email protected]>
1 parent ab54c7e commit d2d254e

File tree

2 files changed

+23
-7
lines changed

2 files changed

+23
-7
lines changed

polygeist/tools/cgeist/Options.h

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#ifndef CGEIST_OPTIONS_H_
1414
#define CGEIST_OPTIONS_H_
1515

16+
#include "llvm/ADT/SmallString.h"
1617
#include "llvm/Passes/OptimizationLevel.h"
1718
#include "llvm/Support/CommandLine.h"
1819
#include <string>
@@ -29,12 +30,11 @@ class ArgumentList {
2930
///
3031
/// The element stored will be owned by this.
3132
template <typename... ArgTy> void emplace_back(ArgTy &&...Args) {
32-
// Store as a string
33-
std::string Buffer;
34-
llvm::raw_string_ostream Stream(Buffer);
35-
(Stream << ... << Args);
36-
Storage.push_back(Stream.str());
37-
push_back(Storage.back());
33+
// Store as a SmallString
34+
push_back(Storage
35+
.emplace_back(std::initializer_list<llvm::StringRef>{
36+
std::forward<ArgTy>(Args)...})
37+
.c_str());
3838
}
3939

4040
/// Return the underling argument list.
@@ -45,7 +45,9 @@ class ArgumentList {
4545

4646
private:
4747
/// Helper storage.
48-
llvm::SmallVector<std::string> Storage;
48+
///
49+
/// llvm::SmallString<0> is needed to enforce heap allocation.
50+
llvm::SmallVector<llvm::SmallString<0>> Storage;
4951
/// List of arguments
5052
llvm::SmallVector<const char *> Args;
5153
};
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// RUN: cgeist -DPASS -DTEST0 -DTEST1 -DTEST2 %s -S -o -
2+
3+
// CHECK-LABEL: func.func @main() -> i32 attributes {llvm.linkage = #llvm.linkage<external>} {
4+
// CHECK-NEXT: %c0_i32 = arith.constant 0 : i32
5+
// CHECK-NEXT: return %c0_i32 : i32
6+
// CHECK-NEXT: }
7+
8+
int main() {
9+
#ifdef PASS
10+
return 0;
11+
#else
12+
return 1;
13+
#endif
14+
}

0 commit comments

Comments
 (0)