forked from erlang/otp
-
Notifications
You must be signed in to change notification settings - Fork 1
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
[pull] master from erlang:master #196
Merged
Merged
Conversation
This file contains 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
The internal instructions `bs_checked_get` and `bs_checked_skip` have confusing names. One might think that they do some extra checking, while the opposite is true. Rename them to `bs_ensured_get` and `bs_ensured_skip` to make the connection to the `bs_ensure` instruction clearer.
The compiler could sometimes generate unsafe code when matching literal data in a binary. For example: t(Data) -> <<Rest/bits>> = Data, {_, Bits} = ext:ernal(Rest), case Rest of <<0, _:Bits>> -> not_empty; <<>> -> empty end. The generated code for the first clause of the `case` would look like this: {bs_match,{f,20},{y,2},{commands,[{'=:=',nil,8,0}]}}. That is unsafe because the first command in a `bs_match` instruction must always be an `ensure*` instruction to ensure that the bitstring is of sufficient size. This unsafe code generation was introduced in 60f8f9e. This commit ensures that a `bs_match_string` instruction will be emitted instead of a `bs_match` instruction: {test,bs_match_string,{f,20},[{y,2},8,{string,<<0>>}]}. Fixes #9304
Consider this function: do_try(F) -> try F() catch C:R:Stk -> {'EXIT',C,R,Stk} end. The compiler in Erlang/OTP 27 generates the following code for the body of the function: {allocate,3,1}. % Allocate three Y registers. {init_yregs,{list,[{y,0},{y,1}]}}. {'try',{y,2},{f,3}}. {call_fun,0}. {try_end,{y,2}}. {deallocate,3}. return. {label,3}. {try_case,{y,2}}. %% {y,2} is free, but is not being reused. {move,{x,1},{y,0}}. {move,{x,0},{y,1}}. {move,{x,2},{x,0}}. build_stacktrace. {test_heap,5,1}. {put_tuple2,{x,0},{list,[{atom,'EXIT'},{y,1},{y,0},{x,0}]}}. {deallocate,3}. return. Note that a stackframe with three Y registers will be allocated. It would be sufficient to allocate two Y registers if `{y,2}` could be reused following the `{try_case,{y,2}}` instruction. This commit extends an existing optimization to ensure that `{y,2}` can be reused, resulting in the following code: {allocate,2,1}. % Allocate two Y registers. {init_yregs,{list,[{y,0}]}}. {'try',{y,1},{f,3}}. {line,[{location,"t.erl",7}]}. {call_fun,0}. {try_end,{y,1}}. {deallocate,2}. return. {label,3}. {try_case,{y,1}}. {move,{x,0},{y,0}}. {move,{x,1},{y,1}}. % Reusing {y,1}. {move,{x,2},{x,0}}. build_stacktrace. {test_heap,5,1}. {put_tuple2,{x,0},{list,[{atom,'EXIT'},{y,0},{y,1},{x,0}]}}. {deallocate,2}. return. This optimization improves the code in more than 200 modules out of the sample of about 1000 modules compiled by `./scripts/diffable`.
We assert that if we are in a literal range, the literal tag should also be set. This will only be true in the debug build, but as asserts are only active in debug builds that is fine.
* lukas/doc/update_ex_doc: Updated ex_doc version to v0.37.0-rc.2 otp: Fix redirect js scripts
erts: Use `processes_iterator/0, processes_next/1`
…OTP-19448 Fix unsafe code generation for binary matching
* bjorn/compiler/beam_validator/OTP-19449: beam_validator: Catch insufficient ensure_at_least/ensure_exactly
* maint: beam_validator: Catch insufficient ensure_at_least/ensure_exactly
…ord/OTP-19315 kernel: Add basic password support for noshell raw
…nto maint * lukas/erts/fix-persistent-term-literal-tag/OTP-19458: erts: Mark error in erl.c as noreturn erts: Strengthen literal tag assert erts: Make sure to tag orig with literal ptr
Improve register allocation for try ... catch constructs
Reviewer's Guide by SourceryThis pull request introduces several changes to the bitstring matching functionality in the compiler, including new instructions and optimizations. It also fixes a bug in the literal area collector and adds a new function to the io module. Sequence diagram for optimized bitstring matchingsequenceDiagram
participant C as Compiler
participant V as Validator
participant R as Runtime
C->>V: Translate bs_match to bs_ensured_* instructions
Note over C,V: When length check is already done
V->>V: Validate bs_match commands
V->>V: Track ensured bits
V->>R: Generate optimized code
Note over R: Execute bs_ensured_* instructions
Note over R: Skip redundant length checks
Class diagram for updated bitstring matching instructionsclassDiagram
class bs_match_instructions {
bs_get
bs_ensured_get
bs_skip
bs_ensured_skip
bs_match_string
bs_ensured_match_string
}
note for bs_match_instructions "New bs_ensured_* variants
don't check bitstring length"
class bs_match_command {
ensure_at_least(Size, Unit)
ensure_exactly(Size)
=:=(Bits, Value)
skip(Size)
get_tail(Live, Unit, Dst)
}
bs_match_instructions --> bs_match_command: translates to
State diagram for bitstring matching optimizationstateDiagram-v2
[*] --> Initial
Initial --> LengthChecked: bs_ensure
LengthChecked --> Matching: bs_ensured_* instructions
Initial --> Matching: bs_* instructions
Matching --> [*]: Success
Matching --> Failed: Match error
note right of LengthChecked: Length check done once
note right of Matching: Optimized path skips
redundant length checks
File-Level Changes
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
See Commits and Changes for more details.
Created by pull[bot] (v2.0.0-alpha.1)
Can you help keep this open source service alive? 💖 Please sponsor : )
Summary by Sourcery
Optimize bitstring matching by ensuring the bitstring length before matching and using specialized instructions for various match operations.
Bug Fixes:
bs_ensure_at_least
orbs_ensure_exactly
.Enhancements:
Tests:
bs_ensure
instruction.