Skip to content

Commit

Permalink
wip callocas
Browse files Browse the repository at this point in the history
  • Loading branch information
charles-cooper committed Nov 22, 2024
1 parent 6f61f53 commit 6be1300
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 16 deletions.
4 changes: 2 additions & 2 deletions vyper/codegen/function_definitions/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from functools import cached_property
from typing import Optional

from vyper.codegen.context import Constancy, Context
from vyper.codegen.context import Constancy, Context, VariableRecord
from vyper.codegen.ir_node import IRnode
from vyper.codegen.memory_allocator import MemoryAllocator
from vyper.evm.opcodes import version_check
Expand All @@ -16,7 +16,7 @@
class FrameInfo:
frame_start: int
frame_size: int
frame_vars: dict[str, tuple[int, VyperType]]
frame_vars: dict[str, VariableRecord]

@property
def mem_used(self):
Expand Down
24 changes: 18 additions & 6 deletions vyper/codegen/self_call.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
from vyper.codegen.context import Context
import dataclasses

Check notice

Code scanning / CodeQL

Unused import Note

Import of 'dataclasses' is not used.
import copy
from vyper.codegen.context import Context,Alloca

Check notice

Code scanning / CodeQL

Unused import Note

Import of 'Context' is not used.
Import of 'Alloca' is not used.
from vyper.codegen.core import _freshname, eval_once_check, make_setter
from vyper.codegen.ir_node import IRnode
from vyper.codegen.memory_allocator import MemoryAllocator

Check notice

Code scanning / CodeQL

Unused import Note

Import of 'MemoryAllocator' is not used.
Expand Down Expand Up @@ -70,11 +72,21 @@ def ir_for_self_call(stmt_expr, context):
dst_tuple_t = TupleT(tuple(func_t.argument_types))
if context.settings.experimental_codegen:
arg_items = ["multi"]
framestart = func_t._ir_info.frame_info.frame_start
freshctx = Context(context.module_ctx, MemoryAllocator(framestart))
for arg_t in func_t.argument_types:
varname = freshctx.fresh_varname("param") # dummy varname
arg_items.append(freshctx.new_variable(varname, arg_t))
#framestart = func_t._ir_info.frame_info.frame_start
frame_info = func_t._ir_info.frame_info

for var in frame_info.frame_vars.values():
var = copy.copy(var)
alloca = var.alloca
assert alloca is not None
assert isinstance(var.pos, str) # help mypy
if not var.pos.startswith("$palloca"):
continue
newname = var.pos.replace("$palloca", "$alloca")
var.pos = newname
irnode = var.as_ir_node()
irnode.passthrough_metadata["alloca"] = alloca
arg_items.append(irnode)
args_dst = IRnode.from_list(arg_items, typ=dst_tuple_t)
else:
args_dst = IRnode(func_t._ir_info.frame_info.frame_start, typ=dst_tuple_t, location=MEMORY)
Expand Down
12 changes: 7 additions & 5 deletions vyper/venom/ir_node_to_venom.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,15 +108,17 @@

SymbolTable = dict[str, Optional[IROperand]]
_alloca_table: SymbolTable = None # type: ignore
_palloca_table: SymbolTable = None # type: ignore
MAIN_ENTRY_LABEL_NAME = "__main_entry"


# convert IRnode directly to venom
def ir_node_to_venom(ir: IRnode) -> IRContext:
_ = ir.unique_symbols # run unique symbols check

global _alloca_table
global _alloca_table, _palloca_table
_alloca_table = {}
_palloca_table = {}

ctx = IRContext()
fn = ctx.create_function(MAIN_ENTRY_LABEL_NAME)
Expand Down Expand Up @@ -238,7 +240,7 @@ def pop_source(*args, **kwargs):
def _convert_ir_bb(fn, ir, symbols):
assert isinstance(ir, IRnode), ir
# TODO: refactor these to not be globals
global _break_target, _continue_target, _alloca_table
global _break_target, _continue_target, _alloca_table, _palloca_table

# keep a map from external functions to all possible entry points

Expand Down Expand Up @@ -542,12 +544,12 @@ def emit_body_blocks():

elif ir.value.startswith("$palloca"):
alloca = ir.passthrough_metadata["alloca"]
if alloca._id not in _alloca_table:
if alloca._id not in _palloca_table:
ptr = fn.get_basic_block().append_instruction(
"palloca", alloca.offset, alloca.size, alloca._id
)
_alloca_table[alloca._id] = ptr
return _alloca_table[alloca._id]
_palloca_table[alloca._id] = ptr
return _palloca_table[alloca._id]

return symbols.get(ir.value)
elif ir.is_literal:
Expand Down
8 changes: 5 additions & 3 deletions vyper/venom/passes/function_inliner.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def _build_alloca_map(self):
for bb in self.function.get_basic_blocks():
for inst in bb.instructions:
if inst.opcode == "alloca":
ret[inst.operands[0]] = inst
ret.setdefault(inst.operands[2], []).append( inst)
return ret

@property
Expand Down Expand Up @@ -87,9 +87,11 @@ def _handle_invoke(self, invoke_inst, invoke_idx):
inst.operands = [next_bb.label]
inst.output = None
if inst.opcode == "palloca":
alloca_id = inst.operands[0]
alloca_id = inst.operands[2]
allocas = self._alloca_map[alloca_id]
assert len(allocas) == 1, allocas
inst.opcode = "store"
inst.operands = [self._alloca_map[alloca_id].output]
inst.operands = [allocas[0].output]
if inst.opcode == "param":
inst.opcode = "store"
inst.operands = [invoke_inst.operands[-i-1]]
Expand Down

0 comments on commit 6be1300

Please sign in to comment.