-
Notifications
You must be signed in to change notification settings - Fork 1k
compiler: use memcmp to compare strings #5149
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
Draft
niaow
wants to merge
1
commit into
tinygo-org:dev
Choose a base branch
from
niaow:string-memcmp
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,83 @@ | ||
| package compiler | ||
|
|
||
| import ( | ||
| "strconv" | ||
|
|
||
| "tinygo.org/x/go-llvm" | ||
| ) | ||
|
|
||
| func (b *builder) createStringEqual(lhs, rhs llvm.Value) llvm.Value { | ||
| // Compare the lengths. | ||
| lhsLen := b.CreateExtractValue(lhs, 1, "streq.lhs.len") | ||
| rhsLen := b.CreateExtractValue(rhs, 1, "streq.rhs.len") | ||
| lenCmp := b.CreateICmp(llvm.IntEQ, lhsLen, rhsLen, "streq.len.eq") | ||
|
|
||
| // Branch on the length comparison. | ||
| bodyCmpBlock := b.ctx.AddBasicBlock(b.llvmFn, "streq.body") | ||
| nextBlock := b.ctx.AddBasicBlock(b.llvmFn, "streq.next") | ||
| b.CreateCondBr(lenCmp, bodyCmpBlock, nextBlock) | ||
|
|
||
| // Use memcmp to compare the contents if the lengths are equal. | ||
| b.SetInsertPointAtEnd(bodyCmpBlock) | ||
| lhsPtr := b.CreateExtractValue(lhs, 0, "streq.lhs.ptr") | ||
| rhsPtr := b.CreateExtractValue(rhs, 0, "streq.rhs.ptr") | ||
| memcmp := b.createMemCmp(lhsPtr, rhsPtr, lhsLen, "streq.memcmp") | ||
| memcmpEq := b.CreateICmp(llvm.IntEQ, memcmp, llvm.ConstNull(b.cIntType), "streq.memcmp.eq") | ||
| b.CreateBr(nextBlock) | ||
|
|
||
| // Create a phi to join the results. | ||
| b.SetInsertPointAtEnd(nextBlock) | ||
| result := b.CreatePHI(b.ctx.Int1Type(), "") | ||
| result.AddIncoming([]llvm.Value{llvm.ConstNull(b.ctx.Int1Type()), memcmpEq}, []llvm.BasicBlock{b.currentBlockInfo.exit, bodyCmpBlock}) | ||
| b.currentBlockInfo.exit = nextBlock // adjust outgoing block for phi nodes | ||
|
|
||
| return result | ||
| } | ||
|
|
||
| func (b *builder) createStringLess(lhs, rhs llvm.Value) llvm.Value { | ||
| // Calculate the minimum of the two string lengths. | ||
| lhsLen := b.CreateExtractValue(lhs, 1, "strlt.lhs.len") | ||
| rhsLen := b.CreateExtractValue(rhs, 1, "strlt.rhs.len") | ||
| minFnName := "llvm.umin.i" + strconv.Itoa(b.uintptrType.IntTypeWidth()) | ||
| minFn := b.mod.NamedFunction(minFnName) | ||
| if minFn.IsNil() { | ||
| fnType := llvm.FunctionType(b.uintptrType, []llvm.Type{b.uintptrType, b.uintptrType}, false) | ||
| minFn = llvm.AddFunction(b.mod, minFnName, fnType) | ||
| } | ||
| minLen := b.CreateCall(minFn.GlobalValueType(), minFn, []llvm.Value{lhsLen, rhsLen}, "strlt.min.len") | ||
|
|
||
| // Compare the common-length body. | ||
| lhsPtr := b.CreateExtractValue(lhs, 0, "strlt.lhs.ptr") | ||
| rhsPtr := b.CreateExtractValue(rhs, 0, "strlt.rhs.ptr") | ||
| memcmp := b.createMemCmp(lhsPtr, rhsPtr, minLen, "strlt.memcmp") | ||
|
|
||
| // Evaluate the result as: memcmp == 0 ? lhsLen < rhsLen : memcmp < 0 | ||
| zero := llvm.ConstNull(b.cIntType) | ||
| memcmpEQ := b.CreateICmp(llvm.IntEQ, memcmp, zero, "strlt.memcmp.eq") | ||
| lenLT := b.CreateICmp(llvm.IntULT, lhsLen, rhsLen, "strlt.len.lt") | ||
| memcmpLT := b.CreateICmp(llvm.IntSLT, memcmp, zero, "strlt.memcmp.lt") | ||
| return b.CreateSelect(memcmpEQ, lenLT, memcmpLT, "strlt.result") | ||
| } | ||
|
|
||
| // createMemCmp compares memory by calling the libc function memcmp. | ||
| // This function is handled specially by LLVM: | ||
| // - It can be constant-folded in some trivial cases (e.g. len 0) | ||
| // - It can be replaced with loads and compares when the length is small and known | ||
| func (b *builder) createMemCmp(lhs, rhs, len llvm.Value, name string) llvm.Value { | ||
| memcmp := b.mod.NamedFunction("memcmp") | ||
| if memcmp.IsNil() { | ||
| fnType := llvm.FunctionType(b.cIntType, []llvm.Type{b.dataPtrType, b.dataPtrType, b.uintptrType}, false) | ||
| memcmp = llvm.AddFunction(b.mod, "memcmp", fnType) | ||
|
|
||
| // The memcmp call does not capture the string. | ||
| nocapture := b.ctx.CreateEnumAttribute(llvm.AttributeKindID("nocapture"), 0) | ||
| memcmp.AddAttributeAtIndex(1, nocapture) | ||
| memcmp.AddAttributeAtIndex(2, nocapture) | ||
|
|
||
| // The memcmp call does not modify the string. | ||
| readonly := b.ctx.CreateEnumAttribute(llvm.AttributeKindID("readonly"), 0) | ||
| memcmp.AddAttributeAtIndex(1, readonly) | ||
| memcmp.AddAttributeAtIndex(2, readonly) | ||
| } | ||
| return b.CreateCall(memcmp.GlobalValueType(), memcmp, []llvm.Value{lhs, rhs, len}, name) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Aren't string lengths signed?
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The top bit is never set so they are both, although AVR is slightly awkward since the length is actually
uintptr.