Skip to content

Commit 9ff519b

Browse files
Junda LiuFlakebi
authored andcommitted
Do not add null byte for immutable strings
`CreateGlobalString` will add a null byte to the end of string by default, which prevents us from getting the exact same string back as we set. This makes it hard to use for cases where we need exact string matching, such as function names lookup.
1 parent de745c4 commit 9ff519b

4 files changed

Lines changed: 4 additions & 4 deletions

File tree

include/llvm-dialects/Dialect/Dialect.td

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,7 @@ def : AttrLlvmType<AttrI32, I32>;
296296
def : AttrLlvmType<AttrI64, I64>;
297297

298298
def ImmutableStringAttr : Attr<"::llvm::StringRef"> {
299-
let toLlvmValue = [{ $_builder.CreateGlobalString($0, "str") }];
299+
let toLlvmValue = [{ $_builder.CreateGlobalString($0, "str", 0, nullptr, /* AddNull */ false) }];
300300
let fromLlvmValue = [{ ::llvm::cast<::llvm::ConstantDataArray>(::llvm::cast<::llvm::GlobalVariable>($0)->getInitializer())->getAsString() }];
301301
let isImmutable = true;
302302
}

test/example/generated/ExampleDialect.cpp.inc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2543,7 +2543,7 @@ initial
25432543

25442544

25452545
::llvm::SmallVector<::llvm::Value*, 1> args = {
2546-
b.CreateGlobalString(val, "str")
2546+
b.CreateGlobalString(val, "str", 0, nullptr, /* AddNull */ false)
25472547
};
25482548

25492549
return ::llvm::cast<StringAttrOp>(b.CreateCall(fn, args, instName));

test/example/test-builder.test

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
; RUN: llvm-dialects-example - | FileCheck --check-prefixes=CHECK %s
2323

2424
;.
25-
; CHECK: @str = private unnamed_addr constant [13 x i8] c"Hello world!\00", align 1
25+
; CHECK: @str = private unnamed_addr constant [12 x i8] c"Hello world!", align 1
2626
;.
2727
; CHECK-LABEL: define void @example() {
2828
; CHECK-NEXT: [[ENTRY:.*:]]

test/example/visitor-basic.ll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
; DEFAULT-NEXT: visiting ReturnInst: ret void
4444
; DEFAULT-NEXT: inner.counter = 1
4545

46-
@0 = private unnamed_addr constant [13 x i8] c"Hello world!\00", align 1
46+
@0 = private unnamed_addr constant [12 x i8] c"Hello world!", align 1
4747

4848
define void @test1(ptr %p) {
4949
entry:

0 commit comments

Comments
 (0)