Skip to content

Commit f619c71

Browse files
committed
Write tests, complete PR feedback
1 parent 3c583c8 commit f619c71

4 files changed

Lines changed: 121 additions & 41 deletions

File tree

zjit/src/hir.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1091,9 +1091,7 @@ impl Insn {
10911091
/// Note: These are restrictions on the `write` `EffectSet` only. Even instructions with
10921092
/// `read: effects::Any` could potentially be omitted.
10931093
fn is_elidable(&self) -> bool {
1094-
Effect::from_write(effect_sets::Allocator).includes(
1095-
self.get_effects()
1096-
)
1094+
effect_sets::Allocator.includes(self.get_effects().write())
10971095
}
10981096
}
10991097

zjit/src/hir_effect/gen_hir_effect.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,13 @@ def add_union name, effect_names
145145
puts "}"
146146

147147

148+
puts "pub mod effects {
149+
use super::*;"
150+
$bits.keys.sort.map {|effect_name|
151+
puts " pub const #{effect_name}: Effect = Effect::from_set(effect_sets::#{effect_name});"
152+
}
153+
puts "}"
154+
148155
# puts "pub mod effects {
149156
# use super::*;"
150157
# $bits.keys.sort.map {|effect_name|

zjit/src/hir_effect/hir_effect.inc.rs

Lines changed: 13 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

zjit/src/hir_effect/mod.rs

Lines changed: 100 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,6 @@ pub struct EffectSet {
2828
bits: EffectBits
2929
}
3030

31-
// TODO(Jacob): Add tests for Effect
32-
// TODO(Jacob): Modify ruby generation of effects to include nice labels for Effects instead of just EffectSets
33-
// TODO(Jacob): Modify these labels to be callected effect_sets in the inc.rs, and create others called effects
3431
#[derive(Clone, Copy, Debug)]
3532
pub struct Effect {
3633
/// Unlike ZJIT's type system, effects do not have a notion of subclasses.
@@ -146,27 +143,36 @@ impl EffectSet {
146143
}
147144
}
148145

149-
// TODO(Jacob): Add interferes function
150146
impl Effect {
151-
pub fn from_sets(read: EffectSet, write: EffectSet) -> Effect {
147+
pub const fn from_sets(read: EffectSet, write: EffectSet) -> Effect {
152148
Effect { read, write }
153149
}
154150

155151
// This function addresses the special case where the read and write sets are the same
156-
pub fn from_set(set: EffectSet) -> Effect {
152+
pub const fn from_set(set: EffectSet) -> Effect {
157153
Effect {read: set, write: set }
158154
}
159155

160156
// This function accepts write and sets read to Any
161-
pub fn from_write(write: EffectSet) -> Effect {
157+
pub const fn from_write(write: EffectSet) -> Effect {
162158
Effect { read: effect_sets::Any, write }
163159
}
164160

165161
// This function accepts read and sets read to Any
166-
pub fn from_read(read: EffectSet) -> Effect {
162+
pub const fn from_read(read: EffectSet) -> Effect {
167163
Effect { read, write: effect_sets::Any }
168164
}
169165

166+
// Method to access the private read field
167+
pub fn read(&self) -> EffectSet {
168+
self.read
169+
}
170+
171+
// Method to access the private write field
172+
pub fn write(&self) -> EffectSet {
173+
self.write
174+
}
175+
170176
pub fn union(&self, other: Effect) -> Effect {
171177
Effect::from_sets(
172178
self.read.union(other.read),
@@ -214,8 +220,8 @@ mod tests {
214220
use super::*;
215221

216222
#[track_caller]
217-
fn assert_bit_equal(left: EffectSet, right: EffectSet) {
218-
assert_eq!(left.bits, right.bits, "{left} bits are not equal to {right} bits");
223+
fn assert_set_bit_equal(left: EffectSet, right: EffectSet) {
224+
assert!(left.bit_equal(right), "{left} bits are not equal to {right} bits");
219225
}
220226

221227
#[track_caller]
@@ -228,6 +234,12 @@ mod tests {
228234
assert!(!right.includes(left), "{left} is a subeffect set of {right}");
229235
}
230236

237+
#[track_caller]
238+
fn assert_bit_equal(left: Effect, right: Effect) {
239+
assert!(left.bit_equal(right), "{left} bits are not equal to {right} bits");
240+
}
241+
242+
231243
#[track_caller]
232244
fn assert_subeffect(left: Effect, right: Effect) {
233245
assert!(right.includes(left), "{left} is not a subeffect of {right}");
@@ -239,7 +251,7 @@ mod tests {
239251
}
240252

241253
#[test]
242-
fn none_is_subeffect_of_everything() {
254+
fn effect_set_none_is_subeffect_of_everything() {
243255
assert_subeffect_set(effect_sets::Empty, effect_sets::Empty);
244256
assert_subeffect_set(effect_sets::Empty, effect_sets::Any);
245257
assert_subeffect_set(effect_sets::Empty, effect_sets::Control);
@@ -250,7 +262,7 @@ mod tests {
250262
}
251263

252264
#[test]
253-
fn everything_is_subeffect_of_any() {
265+
fn effect_set_everything_is_subeffect_of_any() {
254266
assert_subeffect_set(effect_sets::Empty, effect_sets::Any);
255267
assert_subeffect_set(effect_sets::Any, effect_sets::Any);
256268
assert_subeffect_set(effect_sets::Control, effect_sets::Any);
@@ -261,7 +273,7 @@ mod tests {
261273
}
262274

263275
#[test]
264-
fn union_never_shrinks() {
276+
fn effect_set_union_never_shrinks() {
265277
// iterate over all effect entries from bottom to top
266278
for i in [0, 1, 4, 6, 10, 15] {
267279
let e = EffectSet::from_bits(i);
@@ -273,7 +285,7 @@ mod tests {
273285
}
274286

275287
#[test]
276-
fn intersect_never_grows() {
288+
fn effect_set_intersect_never_grows() {
277289
// Randomly selected values from bottom to top
278290
for i in [0, 3, 6, 8, 15] {
279291
let e = EffectSet::from_bits(i);
@@ -285,48 +297,34 @@ mod tests {
285297
}
286298

287299
#[test]
288-
fn self_is_included() {
300+
fn effect_set_self_is_included() {
289301
assert!(effect_sets::Stack.includes(effect_sets::Stack));
290302
assert!(effect_sets::Any.includes(effect_sets::Any));
291303
assert!(effect_sets::Empty.includes(effect_sets::Empty));
292304
}
293305

294306
#[test]
295-
fn frame_includes_stack_locals_and_pc() {
307+
fn effect_set_frame_includes_stack_locals_and_pc() {
296308
assert_subeffect_set(effect_sets::Stack, effect_sets::Frame);
297309
assert_subeffect_set(effect_sets::Locals, effect_sets::Frame);
298310
assert_subeffect_set(effect_sets::PC, effect_sets::Frame);
299311
}
300312

301313
#[test]
302-
fn frame_is_stack_locals_and_pc() {
314+
fn effect_set_frame_is_stack_locals_and_pc() {
303315
let union = effect_sets::Stack.union(effect_sets::Locals.union(effect_sets::PC));
304-
assert_bit_equal(effect_sets::Frame, union);
316+
assert_set_bit_equal(effect_sets::Frame, union);
305317
}
306318

307319
#[test]
308-
fn any_includes_some_subeffects() {
320+
fn effect_set_any_includes_some_subeffects() {
309321
assert_subeffect_set(effect_sets::Allocator, effect_sets::Any);
310322
assert_subeffect_set(effect_sets::Frame, effect_sets::Any);
311323
assert_subeffect_set(effect_sets::Memory, effect_sets::Any);
312324
}
313325

314-
// TODO(Jacob): Fill In
315326
#[test]
316-
fn any_includes_everything() {
317-
}
318-
319-
// TODO(Jacob): Test the following
320-
// union
321-
// intersect
322-
// includes
323-
// exclude
324-
// bit_equal
325-
// overlaps
326-
// print
327-
328-
#[test]
329-
fn display_exact_bits_match() {
327+
fn effect_set_display_exact_bits_match() {
330328
assert_eq!(format!("{}", effect_sets::Empty), "Empty");
331329
assert_eq!(format!("{}", effect_sets::PC), "PC");
332330
assert_eq!(format!("{}", effect_sets::Any), "Any");
@@ -335,12 +333,76 @@ mod tests {
335333
}
336334

337335
#[test]
338-
fn display_multiple_bits() {
339-
let union = effect_sets::Stack.union(effect_sets::Locals);
336+
fn effect_set_display_multiple_bits() {
340337
assert_eq!(format!("{}", effect_sets::Stack.union(effect_sets::Locals.union(effect_sets::PC))), "Frame");
341-
println!("{}", union);
342-
println!("{}", effect_sets::Stack.union(effect_sets::Locals.union(effect_sets::PC)));
343338
assert_eq!(format!("{}", effect_sets::Stack.union(effect_sets::Locals)), "Stack|Locals");
344339
assert_eq!(format!("{}", effect_sets::PC.union(effect_sets::Allocator)), "PC|Allocator");
345340
}
341+
342+
#[test]
343+
fn effect_any_includes_everything() {
344+
assert_subeffect(effects::Allocator, effects::Any);
345+
assert_subeffect(effects::Frame, effects::Any);
346+
assert_subeffect(effects::Memory, effects::Any);
347+
// Let's do a less standard effect too
348+
assert_subeffect(
349+
Effect::from_sets(effect_sets::Control, effect_sets::Any),
350+
effects::Any
351+
);
352+
}
353+
354+
#[test]
355+
fn effect_union_works() {
356+
assert_bit_equal(
357+
Effect::from_read(effect_sets::Any)
358+
.union(Effect::from_write(effect_sets::Any)),
359+
effects::Any
360+
);
361+
assert_bit_equal(
362+
effects::Empty.union(effects::Empty),
363+
effects::Empty
364+
);
365+
assert_subeffect(
366+
effects::Control.union(effects::Frame),
367+
effects::Any
368+
);
369+
assert_not_subeffect(
370+
effects::Frame.union(effects::Locals),
371+
effects::PC
372+
);
373+
}
374+
375+
#[test]
376+
fn effect_intersect_works() {
377+
assert_subeffect(effects::Memory.intersect(effects::Control), effects::Empty);
378+
assert_subeffect(effects::Frame.intersect(effects::PC), effects::PC);
379+
assert_subeffect(
380+
Effect::from_sets(effect_sets::Allocator, effect_sets::Other)
381+
.intersect(Effect::from_sets(effect_sets::Stack, effect_sets::PC)),
382+
effects::Empty
383+
)
384+
}
385+
386+
#[test]
387+
fn effect_display_exact_bits_match() {
388+
assert_eq!(format!("{}", effects::Empty), "Empty, Empty");
389+
assert_eq!(format!("{}", effects::PC), "PC, PC");
390+
assert_eq!(format!("{}", effects::Any), "Any, Any");
391+
assert_eq!(format!("{}", effects::Frame), "Frame, Frame");
392+
assert_eq!(format!("{}", effects::Stack.union(effects::Locals.union(effects::PC))), "Frame, Frame");
393+
assert_eq!(format!("{}", Effect::from_write(effect_sets::Control)), "Any, Control");
394+
assert_eq!(format!("{}", Effect::from_sets(effect_sets::Allocator, effect_sets::Memory)), "Allocator, Memory");
395+
}
396+
397+
#[test]
398+
fn effect_display_multiple_bits() {
399+
assert_eq!(format!("{}", effects::Stack.union(effects::Locals.union(effects::PC))), "Frame, Frame");
400+
assert_eq!(format!("{}", effects::Stack.union(effects::Locals)), "Stack|Locals, Stack|Locals");
401+
assert_eq!(format!("{}", effects::PC.union(effects::Allocator)), "PC|Allocator, PC|Allocator");
402+
assert_eq!(format!("{}", Effect::from_sets(effect_sets::Other, effect_sets::PC)
403+
.union(Effect::from_sets(effect_sets::Memory, effect_sets::Stack))),
404+
"Memory, Stack|PC"
405+
);
406+
}
407+
346408
}

0 commit comments

Comments
 (0)