Skip to content

Commit 42a4a61

Browse files
committed
Refactor instruction cost calculation
1 parent 436100b commit 42a4a61

File tree

5 files changed

+72
-59
lines changed

5 files changed

+72
-59
lines changed

python/src/mlogv32/preprocessor/app.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,12 @@ def build(
9393
config = BuildConfig.load(yaml_path)
9494

9595
worker_output = get_template_output_path(config.templates.worker)
96-
worker_code = render_template(config.templates.worker, worker_output)
96+
worker_code = render_template(
97+
config.templates.worker,
98+
worker_output,
99+
instructions=config.instructions,
100+
**config.inputs,
101+
)
97102

98103
labels = dict(iter_labels(parse_mlog(worker_code)))
99104

@@ -103,6 +108,7 @@ def build(
103108
controller_output,
104109
instructions=config.instructions,
105110
labels=labels,
111+
**config.inputs,
106112
)
107113

108114
lookups_schem = cast(

python/src/mlogv32/preprocessor/models.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ def _RelativePath_after(path: Path):
2828
class BuildConfig(BaseModel):
2929
templates: Templates
3030
schematics: Schematics
31+
inputs: dict[str, Any]
3132
instructions: list[Instruction]
3233

3334
class Templates(BaseModel):

src/cpu/config.yaml

Lines changed: 46 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,19 @@ schematics:
66
lookups: ../../schematics/lookup_procs.msch
77
ram: ../../schematics/ram_proc.msch
88

9+
inputs:
10+
# 46 (main) + 9 (end)
11+
base_instruction_cost: 55
12+
913
instructions:
1014
- label: BEQ
1115
cost: 2
1216
- label: BNE
1317
cost: 2
1418
- label: JAL
15-
cost: 1 + 9
19+
cost: 1 + 9 + 2
1620
- label: JALR
17-
cost: 3 + 8
21+
cost: 3 + 8 + 2
1822
- label: BLT
1923
cost: 2 + 5 + 2
2024
- label: BGE
@@ -24,17 +28,17 @@ instructions:
2428
- label: BGEU
2529
cost: 1
2630
- label: LB
27-
cost: 3 + 29
31+
cost: 3 + 29 + 2
2832
- label: LH
29-
cost: 3 + 29
33+
cost: 3 + 29 + 2
3034
- label: LW
31-
cost: 3 + 29
35+
cost: 3 + 29 + 2
3236
- label: LUI
33-
cost: 2
37+
cost: 2 + 2
3438
- label: LBU
35-
cost: 3 + 29
39+
cost: 3 + 29 + 2
3640
- label: LHU
37-
cost: 2 + 29
41+
cost: 2 + 29 + 2
3842
- label: SB
3943
cost: 2 + 28
4044
- label: SH
@@ -46,7 +50,7 @@ instructions:
4650
- label: AMOSWAP.W
4751
cost: 1 + 52
4852
- label: LR.W
49-
cost: 8 + 8 + 32
53+
cost: 8 + 8 + 32 + 2
5054
- label: SC.W
5155
cost: 7 + 8
5256
- label: AMOXOR.W
@@ -64,77 +68,77 @@ instructions:
6468
- label: AMOMAXU.W
6569
cost: 2 + 52
6670
- label: ADDI
67-
cost: 3
71+
cost: 3 + 2
6872
- label: SUB
69-
cost: 2 + 1 + 3
73+
cost: 2 + 1 + 3 + 2
7074
- label: SLTI
71-
cost: 1 + 2 + 5 + 1 + 2
75+
cost: 1 + 2 + 5 + 1 + 2 + 2
7276
- label: SLTIU
73-
cost: 2
77+
cost: 2 + 2
7478
- label: XORI
75-
cost: 2
79+
cost: 2 + 2
7680
- label: SRA
77-
cost: 1 + 5 + 6
81+
cost: 1 + 5 + 6 + 2
7882
- label: ORI
79-
cost: 2
83+
cost: 2 + 2
8084
- label: ANDI
81-
cost: 2
85+
cost: 2 + 2
8286
- label: ADD
83-
cost: 1 + 3
87+
cost: 1 + 3 + 2
8488
- label: SLL
85-
cost: 1 + 5
89+
cost: 1 + 5 + 2
8690
- label: SLT
87-
cost: 2 + 5 + 1 + 2
91+
cost: 2 + 5 + 1 + 2 + 2
8892
- label: SLTU
89-
cost: 1 + 2
93+
cost: 1 + 2 + 2
9094
- label: XOR
91-
cost: 1 + 2
95+
cost: 1 + 2 + 2
9296
- label: SRL
93-
cost: 1 + 2
97+
cost: 1 + 2 + 2
9498
- label: OR
95-
cost: 1 + 2
99+
cost: 1 + 2 + 2
96100
- label: AND
97-
cost: 1 + 2
101+
cost: 1 + 2 + 2
98102
- label: MUL
99-
cost: 7 + 9
103+
cost: 7 + 9 + 2
100104
- label: MULH
101-
cost: 2 + 13
105+
cost: 2 + 13 + 2
102106
- label: MULHSU
103-
cost: 3 + 13
107+
cost: 3 + 13 + 2
104108
- label: MULHU
105-
cost: 3 + 13
109+
cost: 3 + 13 + 2
106110
- label: DIV
107-
cost: 2 + 5 + 5
111+
cost: 2 + 5 + 5 + 2
108112
- label: DIVU
109-
cost: 5
113+
cost: 5 + 2
110114
- label: REM
111-
cost: 2 + 5 + 5
115+
cost: 2 + 5 + 5 + 2
112116
- label: REMU
113-
cost: 5
117+
cost: 5 + 2
114118
- label: PRIV
115119
cost: 5
116120
- label: CSRRW
117-
cost: 2 + 60 # probably an overestimate, but the CSR code is very complicated
121+
cost: 2 + 60 + 3 # probably an overestimate, but the CSR code is very complicated
118122
- label: CSRRS
119-
cost: 2 + 60
123+
cost: 2 + 60 + 3
120124
- label: CSRRC
121-
cost: 1 + 60
125+
cost: 1 + 60 + 3
122126
- label: AUIPC
123127
cost: 3
124128
- label: CSRRWI
125-
cost: 1 + 2 + 60
129+
cost: 1 + 2 + 60 + 3
126130
- label: CSRRSI
127-
cost: 1 + 2 + 60
131+
cost: 1 + 2 + 60 + 3
128132
- label: CSRRCI
129-
cost: 1 + 1 + 60
133+
cost: 1 + 1 + 60 + 3
130134
- label: FENCE
131135
cost: 1
132136
- label: SLLI
133-
cost: 5
137+
cost: 5 + 2
134138
- label: SRLI
135-
cost: 2
139+
cost: 2 + 2
136140
- label: SRAI
137-
cost: 5 + 6
141+
cost: 5 + 6 + 2
138142
- label: ILLEGAL_OP
139143
cost: 0
140144
- label: MLOGSYS

src/cpu/controller.mlog.jinja

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,9 +99,10 @@ init_incr:
9999
# initialize label/cost tables
100100

101101
#% for instruction in instructions
102+
#% set cost = base_instruction_cost + instruction.cost
102103
# {{ instruction.label }}
103104
write {{labels[instruction.label]}} {{LABELS}} {{loop.index0}}
104-
write {{instruction.cost}} {{COSTS}} {{loop.index0}}
105+
write {{cost}} {{COSTS}} {{loop.index0}}
105106
#% endfor
106107

107108
# initialize peripherals
@@ -362,7 +363,7 @@ set {{POWER_SWITCH}} null
362363
set {{PAUSE_SWITCH}} null
363364
set {{DISPLAY}} null
364365
set {{labels[instruction.label]}} null
365-
set {{instruction.cost}} null
366+
set {{cost}} null
366367
set {{loop.index0}} null
367368
# unused variables
368369
set _ UART_FIFO_MODULO

src/cpu/worker.mlog.jinja

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@
55
#%# constants
66

77
#%# the slowest instruction seems to be AMOMIN.W in icache-covered memory
8-
#%# safety factor * (check_interrupts + main + 2 * (decode + decode_BRANCH + end_decode) + AMOMIN.W + store_ram_word_unchecked)
9-
#% set MAX_INSTRUCTION_ACCUMULATOR_USAGE = (1.05 * (8 + 46 + 2 * (23 + 16 + 33) + 59 + 21))|round(method="ceil")|int
8+
#% set slowest_op = (instructions|selectattr("label", "equalto", "AMOMIN.W")|first).cost
9+
10+
#%# ... + check_interrupts + 2*(decode + decode_BRANCH + end_decode) + store_ram_word_unchecked
11+
#% set MAX_INSTRUCTION_ACCUMULATOR_USAGE = (base_instruction_cost + slowest_op + 8 + 2*(23 + 16 + 33) + 23)|round(method="ceil")|int
1012

1113
#%# linked buildings
1214
#%# IMPORTANT: the buildings must be linked in the order shown here
@@ -184,25 +186,25 @@ main__read_icache:
184186

185187
read delta {{COSTS}} index
186188
op sub accumulator accumulator delta
187-
op sub accumulator accumulator 46 # worst case across both paths
188189

189190
read @counter {{LABELS}} index
190191

191192
# this is a normal length for a label
193+
# cost: 3
192194
end_instruction_with_rd_and_interrupts:
193-
op sub accumulator accumulator 2
194195
set interrupts_pending true
195196
# continue into end_instruction_with_rd
196197

197198
# most instructions with an output register jump here after completing successfully
198199
# writes value rd to register rd_id if rd_id is not x0
200+
# cost: 2
199201
end_instruction_with_rd:
200-
op sub accumulator accumulator 3
201202
jump end_instruction equal rd_id 0
202203
write rd {{REGISTERS}} rd_id
203204
# continue into end_instruction
204205

205206
# all instructions jump here after completing successfully
207+
# cost: 9 (included in base cost)
206208
end_instruction:
207209
# increment instret
208210
op add csr_minstret csr_minstret 1
@@ -222,7 +224,6 @@ end_instruction_trap:
222224
jump state->pause equal pc breakpoint_address
223225
jump state->pause equal single_step_enabled true
224226

225-
op sub accumulator accumulator 10
226227
jump ipt_overrun notEqual current_tick @tick
227228
jump next_tick lessThanEq accumulator {{MAX_INSTRUCTION_ACCUMULATOR_USAGE}}
228229
jump main notEqual interrupts_pending true
@@ -1080,16 +1081,16 @@ BGEU:
10801081

10811082
# cost: dynamic
10821083
conditional_branch:
1083-
op sub accumulator accumulator 11
1084+
op sub accumulator accumulator 12 # -1 because we know we won't be writing to rd
10841085
set rd_id 0 # prevent end_instruction_with_rd from writing to a register
10851086
# continue into unconditional_pc_relative_jump
10861087

1087-
# cost: 9
1088+
# cost: 9 + 2
10881089
unconditional_pc_relative_jump:
10891090
op add next_pc pc imm
10901091
# continue into unconditional_jump
10911092

1092-
# cost: 8
1093+
# cost: 8 + 2
10931094
unconditional_jump:
10941095
op and next_pc next_pc 0xffffffff
10951096

@@ -1241,7 +1242,7 @@ store_value:
12411242

12421243
# cost: dynamic
12431244
store_ram_word_unchecked:
1244-
op sub accumulator accumulator 21
1245+
op sub accumulator accumulator 23
12451246

12461247
# otherwise, store the new value
12471248
op add ret @counter 1
@@ -1263,7 +1264,7 @@ store_ram_word_unchecked:
12631264
# atomics are not supported for MMIO, so we don't need to write to rd here
12641265
# however, rd_id is set regardless, so we can safely jump to end_instruction_with_rd_and_time_update
12651266
store_mmio_word_unchecked:
1266-
op sub accumulator accumulator 10
1267+
op sub accumulator accumulator 13
12671268

12681269
op sub _offset decode_address {{MMIO_START}}
12691270

@@ -1295,7 +1296,7 @@ store_mmio_word_unchecked:
12951296
# Transmitter Holding Register
12961297
# append value to queue
12971298
# NOTE: we don't check for overflow here, see readme
1298-
op sub accumulator accumulator 6 # -2
1299+
op sub accumulator accumulator 3 # -5
12991300

13001301
op add _index _tx_write {{UART_TX_START}}
13011302
op and value value 0xff
@@ -1309,7 +1310,7 @@ store_mmio_word_unchecked:
13091310
jump end_instruction always
13101311

13111312
store_mmio_word_unchecked__uart_fcr:
1312-
op sub accumulator accumulator 6 # -2
1313+
op sub accumulator accumulator 3 # -5
13131314

13141315
# FIFO Control Register
13151316

@@ -1754,7 +1755,7 @@ EBREAK:
17541755
MRET:
17551756
jump ILLEGAL_OP lessThan privilege_mode 0b11
17561757

1757-
op sub accumulator accumulator 11
1758+
op sub accumulator accumulator 14
17581759
# ^ static cost ends here
17591760

17601761
# set mstatus.MIE to mstatus.MPIE, set mstatus.MPIE to 1, set privilege_mode to mstatus.MPP, and set mstatus.MPP to U

0 commit comments

Comments
 (0)