Skip to content

Commit

Permalink
show changes without AI prompt
Browse files Browse the repository at this point in the history
therkels committed Nov 20, 2024

Unverified

No user is associated with the committer email.
1 parent 3f072e8 commit c3b74df
Showing 3 changed files with 28 additions and 17 deletions.
Binary file modified jac/examples/ginsScripts/cfg.gv.pdf
Binary file not shown.
27 changes: 19 additions & 8 deletions jac/jaclang/compiler/passes/cfg.py
Original file line number Diff line number Diff line change
@@ -2,7 +2,6 @@
import sys
import types
from collections import defaultdict
import graphviz
from typing import Dict, Set, List, Tuple, Optional


@@ -23,13 +22,25 @@ def __init__(self):
self.coverage_data: Dict[types.CodeType,
Set[Tuple[int, int]]] = defaultdict(set)

def instruction_sizes(self, instructions: List[dis.Instruction]) -> Dict[int, int]:
"""Calculate the size of each instruction"""
sizes = {}
for i, inst in enumerate(instructions):
if i + 1 < len(instructions):
sizes[inst.offset] = instructions[i + 1].offset - inst.offset
else:
# Last instruction, assume size 1 for simplicity
sizes[inst.offset] = 1
return sizes

def build_cfg(self, code: types.CodeType) -> Dict[int, CFGNode]:
"""Build CFG from bytecode"""
if code in self.cfg_cache:
return self.cfg_cache[code]

# Get bytecode instructions
instructions = list(dis.get_instructions(code))
instruction_sizes = self.instruction_sizes(instructions)

# Find basic block boundaries
leaders = {0} # First instruction is always a leader
@@ -66,23 +77,23 @@ def build_cfg(self, code: types.CodeType) -> Dict[int, CFGNode]:
elif last_inst.opname in {'POP_JUMP_IF_TRUE', 'POP_JUMP_IF_FALSE',
'JUMP_IF_TRUE_OR_POP', 'JUMP_IF_FALSE_OR_POP'}:
block.next.append(blocks[last_inst.argval]) # Branch target
if offset + last_inst.size < instructions[-1].offset:
if offset + instruction_sizes[offset] < instructions[-1].offset:
# Find next instruction's block
next_offset = offset + last_inst.size
next_offset = offset + instruction_sizes[offset]
while next_offset not in blocks:
next_offset += 1
block.next.append(blocks[next_offset]) # Fall-through
elif offset + last_inst.size < instructions[-1].offset:
elif offset + instruction_sizes[offset] < instructions[-1].offset:
# Sequential flow
next_offset = offset + last_inst.size
next_offset = offset + instruction_sizes[offset]
while next_offset not in blocks:
next_offset += 1
block.next.append(blocks[next_offset])

self.cfg_cache[code] = blocks
return blocks

def trace_callback(self, frame: types.FrameType, event: str, arg: Any) -> Optional[types.TraceFunction]:
def trace_callback(self, frame: types.FrameType, event: str, arg: any):
"""Trace function to track executed branches"""
if event != 'line':
return self.trace_callback
@@ -98,7 +109,7 @@ def trace_callback(self, frame: types.FrameType, event: str, arg: Any) -> Option
# Find the block containing this offset
current_block = None
for block in blocks.values():
if block.offset <= current_offset <= block.offset + sum(inst.size for inst in block.instructions):
if block.offset <= current_offset < block.offset + sum(self.instruction_sizes(block.instructions).values()):
current_block = block
break

@@ -201,4 +212,4 @@ def example_function(x: int) -> int:
print(tracker.get_coverage_report(example_function.__code__))

# Visualize the CFG
tracker.visualize_cfg(example_function.__code__, 'example_cfg')
tracker.visualize_cfg(example_function.__code__, 'example_cfg')
18 changes: 9 additions & 9 deletions jac/jaclang/runtimelib/machine.py
Original file line number Diff line number Diff line change
@@ -344,16 +344,16 @@ def worker(self):
print(self.cfgs)
# genai.configure(api_key=os.getenv("GEN_AI_KEY"))
# model = genai.GenerativeModel("gemini-1.5-flash")
# response_dict = {'cfg': self.cfgs}
# prompt = []
# for k,v in response_dict.items():
# prompt.append(f"here is my {k}:\n{v}")
# prompt.append("\nCan you identify where the code could have an error?")
response_dict = {'cfg': self.cfgs}
prompt = []
for k,v in response_dict.items():
prompt.append(f"here is my {k}:\n{v}")
prompt.append("\nCan you identify where the code could have an error?")
# response = model.generate_content("".join(prompt))

# print("PROMPT:\n")
# print("".join(prompt))
# print("RESPONSE:\n")
print("PROMPT:\n")
print("".join(prompt))
print("RESPONSE:\n")
# print(response.text)

# print(self.cfgs['hot_path'].display_instructions())
print(self.cfgs['hot_path'].display_instructions())

0 comments on commit c3b74df

Please sign in to comment.