Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/vyperlang/vyper into feat…
Browse files Browse the repository at this point in the history
…/convert_literal_bytestring
  • Loading branch information
tserg committed Feb 20, 2025
2 parents 4f38a7f + cd31867 commit 6e20391
Show file tree
Hide file tree
Showing 9 changed files with 36 additions and 44 deletions.
15 changes: 15 additions & 0 deletions tests/functional/codegen/features/iteration/test_for_in_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,21 @@ def data() -> int128:
assert c.data() == sum(xs)


def test_constant_list_iter(get_contract):
code = """
MY_LIST: constant(uint24[4]) = [1, 2, 3, 4]
@external
def foo() -> uint24:
x: uint24 = 0
for s: uint24 in MY_LIST:
x += s
return x
"""
c = get_contract(code)
assert c.foo() == sum([1, 2, 3, 4])


def test_basic_for_list_storage_address(get_contract):
code = """
addresses: address[3]
Expand Down
14 changes: 2 additions & 12 deletions tests/unit/compiler/venom/test_memmerging.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@
from tests.venom_utils import assert_ctx_eq, parse_from_basic_block, parse_venom
from vyper.evm.opcodes import version_check
from vyper.venom.analysis import IRAnalysesCache
from vyper.venom.passes import SCCP, MemMergePass
from vyper.venom.passes import SCCP, MemMergePass, RemoveUnusedVariablesPass


def _check_pre_post(pre, post):
ctx = parse_from_basic_block(pre)
for fn in ctx.functions.values():
ac = IRAnalysesCache(fn)
MemMergePass(ac, fn).run_pass()
RemoveUnusedVariablesPass(ac, fn).run_pass()
assert_ctx_eq(ctx, parse_from_basic_block(post))


Expand Down Expand Up @@ -853,8 +854,6 @@ def test_memzeroing_2():

post = """
_global:
%1 = calldatasize
%2 = calldatasize
%3 = calldatasize
calldatacopy 64, %3, 256
stop
Expand All @@ -881,8 +880,6 @@ def test_memzeroing_3():

post = """
_global:
%1 = calldatasize
%2 = calldatasize
%3 = calldatasize
calldatacopy 0, %3, 264
stop
Expand All @@ -905,7 +902,6 @@ def test_memzeroing_small_calldatacopy():

post = """
_global:
%1 = calldatasize
mstore 0, 0
stop
"""
Expand Down Expand Up @@ -935,13 +931,8 @@ def test_memzeroing_smaller_calldatacopy():

post = """
_global:
%1 = calldatasize
%2 = calldatasize
%6 = calldatasize
calldatacopy 0, %6, 24
%3 = calldatasize
%4 = calldatasize
%5 = calldatasize
mstore 100, 0
stop
"""
Expand All @@ -966,7 +957,6 @@ def test_memzeroing_overlap():

post = """
_global:
%1 = calldatasize
%2 = calldatasize
calldatacopy 100, %2, 64
stop
Expand Down
13 changes: 3 additions & 10 deletions vyper/builtins/functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -2293,19 +2293,12 @@ def build_IR(self, expr, args, kwargs, context):

else:
method_id = method_id_int("log(string,bytes)")
schema = args_abi_t.selector_name().encode("utf-8")
if len(schema) > 32:
raise CompilerPanic(f"print signature too long: {schema}")

schema = args_abi_t.selector_name().encode("utf-8")
schema_t = StringT(len(schema))
schema_buf = context.new_internal_variable(schema_t)
ret = ["seq"]
ret.append(["mstore", schema_buf, len(schema)])
schema_buf = Expr._make_bytelike(context, StringT, schema)

# TODO use Expr.make_bytelike, or better have a `bytestring` IRnode type
ret.append(
["mstore", add_ofst(schema_buf, 32), bytes_to_int(schema.ljust(32, b"\x00"))]
)
ret = ["seq"]

payload_buflen = args_abi_t.size_bound()
payload_t = BytesT(payload_buflen)
Expand Down
2 changes: 1 addition & 1 deletion vyper/codegen/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -560,7 +560,7 @@ def _get_element_ptr_array(parent, key, array_bounds_check):
return IRnode.from_list("~empty", subtype)

if parent.value == "multi":
assert isinstance(key.value, int)
assert isinstance(key.value, int), key
return parent.args[key.value]

ix = unwrap_location(key)
Expand Down
11 changes: 6 additions & 5 deletions vyper/codegen/expr.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,21 +130,22 @@ def parse_Hex(self):
# String literals
def parse_Str(self):
bytez = self.expr.value.encode("utf-8")
return self._make_bytelike(StringT, bytez)
return self._make_bytelike(self.context, StringT, bytez)

# Byte literals
def parse_Bytes(self):
return self._make_bytelike(BytesT, self.expr.value)
return self._make_bytelike(self.context, BytesT, self.expr.value)

def parse_HexBytes(self):
# HexBytes already has value as bytes
assert isinstance(self.expr.value, bytes)
return self._make_bytelike(BytesT, self.expr.value)
return self._make_bytelike(self.context, BytesT, self.expr.value)

def _make_bytelike(self, typeclass, bytez):
@classmethod
def _make_bytelike(cls, context, typeclass, bytez):
bytez_length = len(bytez)
btype = typeclass(bytez_length)
placeholder = self.context.new_internal_variable(btype)
placeholder = context.new_internal_variable(btype)
seq = []
seq.append(["mstore", placeholder, bytez_length])
for i in range(0, len(bytez), 32):
Expand Down
2 changes: 1 addition & 1 deletion vyper/codegen/stmt.py
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ def _parse_For_list(self):
ret = ["seq"]

# list literal, force it to memory first
if isinstance(self.stmt.iter, vy_ast.List):
if iter_list.is_literal:
tmp_list = self.context.new_internal_variable(iter_list.typ)
ret.append(make_setter(tmp_list, iter_list))
iter_list = tmp_list
Expand Down
14 changes: 3 additions & 11 deletions vyper/venom/basicblock.py
Original file line number Diff line number Diff line change
Expand Up @@ -469,8 +469,6 @@ def __init__(self, label: IRLabel, parent: "IRFunction") -> None:
self.out_vars = OrderedSet()
self.is_reachable = False

self._garbage_instructions: set[IRInstruction] = set()

def add_cfg_in(self, bb: "IRBasicBlock") -> None:
self.cfg_in.add(bb)

Expand Down Expand Up @@ -545,15 +543,9 @@ def insert_instruction(self, instruction: IRInstruction, index: Optional[int] =
instruction.error_msg = self.parent.error_msg
self.instructions.insert(index, instruction)

def mark_for_removal(self, instruction: IRInstruction) -> None:
self._garbage_instructions.add(instruction)

def clear_dead_instructions(self) -> None:
if len(self._garbage_instructions) > 0:
self.instructions = [
inst for inst in self.instructions if inst not in self._garbage_instructions
]
self._garbage_instructions.clear()
def clear_nops(self) -> None:
if any(inst.opcode == "nop" for inst in self.instructions):
self.instructions = [inst for inst in self.instructions if inst.opcode != "nop"]

def remove_instruction(self, instruction: IRInstruction) -> None:
assert isinstance(instruction, IRInstruction), "instruction must be an IRInstruction"
Expand Down
6 changes: 2 additions & 4 deletions vyper/venom/passes/memmerging.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ def _optimize_copy(self, bb: IRBasicBlock, copy_opcode: str, load_opcode: str):
if not all(use in copy.insts for use in uses):
continue

bb.mark_for_removal(inst)
inst.make_nop()

self._copies.clear()
self._loads.clear()
Expand Down Expand Up @@ -285,7 +285,6 @@ def _barrier():
_barrier()

_barrier()
bb.clear_dead_instructions()

# optimize memzeroing operations
def _optimize_memzero(self, bb: IRBasicBlock):
Expand All @@ -304,7 +303,7 @@ def _optimize_memzero(self, bb: IRBasicBlock):
inst.operands = [IRLiteral(copy.length), calldatasize, IRLiteral(copy.dst)]

for inst in copy.insts[:-1]:
bb.mark_for_removal(inst)
inst.make_nop()

self._copies.clear()
self._loads.clear()
Expand Down Expand Up @@ -350,7 +349,6 @@ def _barrier():
continue

_barrier()
bb.clear_dead_instructions()


def _volatile_memory(inst):
Expand Down
3 changes: 3 additions & 0 deletions vyper/venom/passes/remove_unused_variables.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ def run_pass(self):
inst = work_list.pop()
self._process_instruction(inst)

for bb in self.function.get_basic_blocks():
bb.clear_nops()

self.analyses_cache.invalidate_analysis(LivenessAnalysis)
self.analyses_cache.invalidate_analysis(DFGAnalysis)

Expand Down

0 comments on commit 6e20391

Please sign in to comment.