Skip to content

Commit

Permalink
Merge with master
Browse files Browse the repository at this point in the history
Signed-off-by: Ramon Bertran Monfort <[email protected]>
  • Loading branch information
rbertran committed May 21, 2024
2 parents 5dc8de8 + 300a542 commit 3c9ad0b
Show file tree
Hide file tree
Showing 27 changed files with 198 additions and 63 deletions.
19 changes: 13 additions & 6 deletions src/microprobe/passes/register/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ class DefaultRegisterAllocationPass(microprobe.passes.Pass):
"""

def __init__(self,
rand: random.Random,
minimize=False,
value=None,
dd: Union[int, float] = 0,
Expand All @@ -64,6 +65,7 @@ def __init__(self,
"""
super(DefaultRegisterAllocationPass, self).__init__()
self._rand = rand
self._min = minimize
if self._min:
raise NotImplementedError("Feature changed need to be"
Expand Down Expand Up @@ -159,7 +161,8 @@ def __call__(self, building_block, target):
LOG.warning("Operand set to: '%s'", value)

else:
operand.set_value(operand.type.random_value())
operand.set_value(operand.type.random_value(
self._rand))

elif operand.type.address_relative:
LOG.warning(
Expand Down Expand Up @@ -349,7 +352,7 @@ class CycleMinimalAllocationPass(microprobe.passes.Pass):
"""

def __init__(self, size, reads, writes, value=None):
def __init__(self, size, reads, writes, rand: random.Random, value=None):
"""
:param size:
Expand All @@ -363,6 +366,7 @@ def __init__(self, size, reads, writes, value=None):
self._reads = reads # max reads in a group to the same register
self._writes = writes # max writes in a group to the same register
self._rdwr = min(reads, writes)
self._rand = rand
if value is not None:
self._immediate = value
else:
Expand Down Expand Up @@ -482,7 +486,8 @@ def update_group(group):
else:

instroperands.append(
operand.assembly(operand.random_value()))
operand.assembly(operand.random_value(
self._rand)))

elif not operand.immediate():

Expand Down Expand Up @@ -1013,18 +1018,20 @@ class RandomAllocationPass(microprobe.passes.Pass):
"""

def __init__(self):
def __init__(self, rand: random.Random):
""" """
super(RandomAllocationPass, self).__init__()
self._description = "Random Allocation of operands"
self._rand = rand

def __call__(self, building_block, dummy_target):

for bbl in building_block.cfg.bbls:
for instr in bbl.instrs:
for operand in instr.operands():
operand.set_value(operand.type.random_value())
operand.set_value(operand.type.random_value(self._rand))
# TODO: This is a POWERPC hack (remove in future)
if operand.type.name == "BO_Values":
while operand.value in [17, 19, 21]:
operand.set_value(operand.type.random_value())
operand.set_value(operand.type.random_value(
self._rand))
6 changes: 4 additions & 2 deletions src/microprobe/passes/switch/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ class SwitchingInstructions(microprobe.passes.Pass):
"""

def __init__(self, replicate=1, strict=False):
def __init__(self, rand: random.Random, replicate=1, strict=False):
"""
:param replicate: (Default value = 1)
Expand All @@ -201,6 +201,7 @@ def __init__(self, replicate=1, strict=False):
super(SwitchingInstructions, self).__init__()
self._description = "Maximize switching factors on instruction "\
"operands"
self._rand = rand
self._replicate = replicate
self._strict = strict

Expand Down Expand Up @@ -560,7 +561,8 @@ def __call__(self, building_block, target):
)
else:
opsize = \
operand.type.random_value().type.size
operand.type.random_value(
self._rand).type.size
assert opsize % size == 0
mult = int(opsize / size)
switch_input = prefix + switch_input * mult
Expand Down
10 changes: 7 additions & 3 deletions src/microprobe/target/isa/instruction.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@ def import_definition(cls, filenames, args):
LOG.debug("Start importing instruction definitions")
iformats, defined_operands = args

rand = random.Random()
rand.seed(13)

defined_memory_operands = []
instructions = {}

Expand Down Expand Up @@ -106,7 +109,7 @@ def import_definition(cls, filenames, args):
defined_operands)

ioperands = _translate_ioperands(name, filename, ioperands,
defined_operands)
defined_operands, rand)

moperands = _translate_moperands(name, filename, operands,
ioperands, moperands,
Expand Down Expand Up @@ -369,7 +372,8 @@ def _translate_moperands(name, filename, operands, ioperands, moperands,


@typeguard_testsuite
def _translate_ioperands(name, filename, ioperands, defined_operands):
def _translate_ioperands(name, filename, ioperands, defined_operands,
rand: random.Random):
"""
:param name:
Expand All @@ -393,7 +397,7 @@ def _translate_ioperands(name, filename, ioperands, defined_operands):

try:
if operand.constant:
reg = operand.random_value()
reg = operand.random_value(rand)
else:
reg = [
value for value in operand.values()
Expand Down
28 changes: 14 additions & 14 deletions src/microprobe/target/isa/operand.py
Original file line number Diff line number Diff line change
Expand Up @@ -631,7 +631,7 @@ def filtered_values(
raise NotImplementedError

@abc.abstractmethod
def random_value(self):
def random_value(self, rand: random.Random):
"""Return a random possible value for the operand."""
raise NotImplementedError

Expand Down Expand Up @@ -888,12 +888,12 @@ def codification(self, value):
"""
return value.codification

def random_value(self):
def random_value(self, rand: random.Random):
"""Return a random possible value for the operand.
:rtype: :class:`~.Register`
"""
return list(self._regs.keys())[random.randrange(0, len(self._regs))]
return list(self._regs.keys())[rand.randrange(0, len(self._regs))]

def access(self, value):
"""
Expand Down Expand Up @@ -1013,21 +1013,21 @@ def set_valid_values(self, values):
self._computed_values = values
self._const = len(values) == 1

def random_value(self):
def random_value(self, rand: random.Random):
"""Return a random possible value for the operand.
:rtype: ::class:`~.int`
"""
if self._computed_values is not None:
return self._computed_values[random.randrange(
return self._computed_values[rand.randrange(
0, len(self._computed_values))]

value = random.randrange(self._min, self._max + 1, self._step)
value = rand.randrange(self._min, self._max + 1, self._step)

if value not in self._novalues:
return value
else:
return self.random_value()
return self.random_value(rand)

def representation(self, value):
"""
Expand Down Expand Up @@ -1174,12 +1174,12 @@ def codification(self, value):
"""
return str(value)

def random_value(self):
def random_value(self, rand: random.Random):
"""Return a random possible value for the operand.
:rtype: ::class:`~.int`
"""
return self._values[random.randrange(0, len(self._values))]
return self._values[rand.randrange(0, len(self._values))]

def access(self, dummy):
"""
Expand Down Expand Up @@ -1271,7 +1271,7 @@ def codification(self, value):
"""
return str(value)

def random_value(self):
def random_value(self, rand: random.Random):
"""Return a random possible value for the operand.
:rtype: ::class:`~.int`
Expand Down Expand Up @@ -1368,7 +1368,7 @@ def filtered_values(
):
return super().filtered_values(context, fieldname)

def random_value(self):
def random_value(self, rand: random.Random):
"""Return a random possible value for the operand.
:rtype: :class:`~.Register`
Expand Down Expand Up @@ -1481,20 +1481,20 @@ def filtered_values(
):
return super().filtered_values(context, fieldname)

def random_value(self):
def random_value(self, rand: random.Random):
"""Return a random possible value for the operand.
:rtype: ::class:`~.int`
"""
value = random.randrange(self._mindispl, self._maxdispl) << self._shift
value = rand.randrange(self._mindispl, self._maxdispl) << self._shift

if value <= (self._maxdispl << self._shift) and \
value >= (self._mindispl << self._shift) and \
not self._in_except_ranges(value) and \
value % self._step == 0:
return value
else:
return self.random_value()
return self.random_value(rand)

return value

Expand Down
8 changes: 7 additions & 1 deletion targets/generic/policies/debug.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
# Futures
from __future__ import absolute_import

# Built-in modules
import random

# Own modules
import microprobe.code
import microprobe.passes.address
Expand Down Expand Up @@ -62,6 +65,9 @@ def policy(target: Target, wrapper, **kwargs):
"Policy '%s' not valid for target '%s'. Supported targets are:"
" %s" % (NAME, target.name, ",".join(SUPPORTED_TARGETS)))

rand = random.Random()
rand.seed(13)

sequence = [kwargs['instruction']]
synth = microprobe.code.Synthesizer(target, wrapper)
synth.add_pass(
Expand All @@ -76,7 +82,7 @@ def policy(target: Target, wrapper, **kwargs):

synth.add_pass(microprobe.passes.register.NoHazardsAllocationPass())
synth.add_pass(
microprobe.passes.register.DefaultRegisterAllocationPass(dd=99,
microprobe.passes.register.DefaultRegisterAllocationPass(rand, dd=99,
relax=True))
synth.add_pass(microprobe.passes.address.UpdateInstructionAddressesPass())
synth.add_pass(microprobe.passes.symbol.ResolveSymbolicReferencesPass())
Expand Down
17 changes: 12 additions & 5 deletions targets/generic/tests/targets/targets_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,8 @@ def riscv_v22_function(self):
"""
riscv_v22_function
"""
rand = random.Random()
rand.seed(13)

target = self.target
instr = target.instructions[self.instr_name]
Expand All @@ -153,7 +155,7 @@ def riscv_v22_function(self):
microprobe.passes.instruction.SetInstructionTypeBySequencePass(
sequence))
# synth.add_pass(microprobe.passes.branch.BranchNextPass())
synth.add_pass(microprobe.passes.register.RandomAllocationPass())
synth.add_pass(microprobe.passes.register.RandomAllocationPass(rand))
# synth.add_pass(microprobe.passes.register.NoHazardsAllocationPass())
# synth.add_pass(
# microprobe.passes.register.DefaultRegisterAllocationPass(
Expand Down Expand Up @@ -317,7 +319,7 @@ def binary_benchmark(self, function):
self.assertEqual(line_bin, asmline_bin)


def self_codification_function(self):
def self_codification_function(self, rand):
"""
self_codification_function
"""
Expand All @@ -340,7 +342,7 @@ def self_codification_function(self):

for idx, operand in enumerate(instruction.operands()):
if idx >= len(values):
values.append(operand.type.random_value())
values.append(operand.type.random_value(rand))
operand.set_value(values[idx])

print("Operands to set: %s" % values)
Expand Down Expand Up @@ -388,6 +390,9 @@ def self_assembly_function(self):
target = self.target
instr = target.instructions[self.instr_name]

rand = random.Random()
rand.seed(13)

repetition = 0
while repetition < REPETITIONS:
instruction = microprobe.code.ins.Instruction()
Expand All @@ -396,7 +401,7 @@ def self_assembly_function(self):
print(instruction)

for operand in instruction.operands():
operand.set_value(operand.type.random_value())
operand.set_value(operand.type.random_value(rand))
print(operand)

assembly = instruction.assembly()
Expand Down Expand Up @@ -693,7 +698,9 @@ def function_4(self):
"""
function_4
"""
return self_codification_function(self)
rand = random.Random()
rand.seed(13)
return self_codification_function(self, rand)

setattr(newclass, f4name, copy_func(function_4, f4name))

Expand Down
Loading

0 comments on commit 3c9ad0b

Please sign in to comment.