-
-
Notifications
You must be signed in to change notification settings - Fork 836
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
feat[venom]: basic check #4483
Draft
HodanPlodky
wants to merge
4
commits into
vyperlang:master
Choose a base branch
from
HodanPlodky:feat/venom/basic-check
base: master
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
feat[venom]: basic check #4483
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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 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,70 @@ | ||
from tests.venom_utils import parse_from_basic_block | ||
from vyper.venom.check_venom import VenomSemanticErrorType, check_venom | ||
|
||
|
||
def test_venom_parser(): | ||
code = """ | ||
main: | ||
%1 = 1 | ||
ret %1 | ||
""" | ||
|
||
ctx = parse_from_basic_block(code) | ||
errors = check_venom(ctx) | ||
|
||
assert errors == [] | ||
|
||
|
||
def test_venom_parser_not_terminated(): | ||
code = """ | ||
bb0: | ||
%1 = 1 | ||
bb1: | ||
jmp @bb0 | ||
bb2: | ||
stop | ||
bb3: | ||
calldataload 10, 20, 30 | ||
""" | ||
|
||
ctx = parse_from_basic_block(code) | ||
errors = check_venom(ctx) | ||
|
||
all(e.error_type == VenomSemanticErrorType.NotTerminatedBasicBlock for e in errors) | ||
assert len(errors) == 2 | ||
assert list(e.metadata.label.name for e in errors) == ["bb0", "bb3"] | ||
|
||
|
||
def test_venom_parser_nonexistant_var(): | ||
code = """ | ||
main: | ||
ret %1 | ||
""" | ||
|
||
ctx = parse_from_basic_block(code) | ||
errors = check_venom(ctx) | ||
|
||
assert len(errors) == 1 | ||
|
||
|
||
def test_venom_parser_nonexistant_var2(): | ||
code = """ | ||
main: | ||
%par = param | ||
%1 = 1 | ||
jnz %par, @br1, @br2 | ||
br1: | ||
%2 = 2 | ||
jmp @join | ||
br2: | ||
%3 = 3 | ||
jmp @join | ||
join: | ||
ret %1, %2, %3 | ||
""" | ||
|
||
ctx = parse_from_basic_block(code) | ||
errors = check_venom(ctx) | ||
|
||
assert len(errors) == 2 | ||
assert {e.metadata.name for e in errors} == {"%2", "%3"} |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
from dataclasses import dataclass | ||
from enum import Enum | ||
from typing import Any | ||
|
||
from vyper.exceptions import CompilerPanic | ||
from vyper.venom.analysis import IRAnalysesCache, LivenessAnalysis | ||
from vyper.venom.basicblock import IRBasicBlock | ||
from vyper.venom.context import IRContext | ||
from vyper.venom.function import IRFunction | ||
|
||
|
||
class VenomSemanticErrorType(Enum): | ||
NotTerminatedBasicBlock = 1 | ||
NotDefinedVar = 2 | ||
|
||
def __repr__(self) -> str: | ||
if self == VenomSemanticErrorType.NotTerminatedBasicBlock: | ||
return "basic block does not terminate" | ||
elif self == VenomSemanticErrorType.NotDefinedVar: | ||
return "var not defined" | ||
else: | ||
raise CompilerPanic("unknown venom semantic error") | ||
|
||
|
||
@dataclass | ||
class VenomSemanticError: | ||
error_type: VenomSemanticErrorType | ||
metadata: Any | ||
|
||
|
||
def check_venom_fn(fn: IRFunction) -> list[VenomSemanticError]: | ||
errors = [] | ||
|
||
# check that all the bbs are terminated | ||
for bb in fn.get_basic_blocks(): | ||
if not bb.is_terminated: | ||
errors.append(VenomSemanticError(VenomSemanticErrorType.NotTerminatedBasicBlock, bb)) | ||
|
||
if errors != []: | ||
return errors | ||
|
||
ac = IRAnalysesCache(fn) | ||
ac.request_analysis(LivenessAnalysis) | ||
for bb in fn.get_basic_blocks(): | ||
e = _handle_incorect_liveness(bb) | ||
errors.extend(e) | ||
return errors | ||
|
||
|
||
def check_venom(context: IRContext) -> list[VenomSemanticError]: | ||
errors: list[VenomSemanticError] = [] | ||
|
||
for fn in context.functions.values(): | ||
errors.extend(check_venom_fn(fn)) | ||
|
||
return errors | ||
|
||
|
||
def _handle_incorect_liveness(bb: IRBasicBlock) -> list[VenomSemanticError]: | ||
errors = [] | ||
defined_here = set() | ||
for inst in bb.instructions: | ||
if inst.output is not None: | ||
defined_here.add(inst.output) | ||
|
||
before_live = set().union(*(in_bb.out_vars for in_bb in bb.cfg_in)) | ||
|
||
undef_vars = bb.instructions[-1].liveness.difference(before_live.union(defined_here)) | ||
|
||
for var in undef_vars: | ||
errors.append(VenomSemanticError(VenomSemanticErrorType.NotDefinedVar, var)) | ||
return errors |
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.
Check failure
Code scanning / CodeQL
Syntax error Error