Skip to content

Commit e70b2db

Browse files
committed
Add jit feature when running tests, and run rustfmt.
1 parent c773d9f commit e70b2db

File tree

3 files changed

+26
-15
lines changed

3 files changed

+26
-15
lines changed

.github/workflows/ci.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ on:
66
name: CI
77

88
env:
9-
CARGO_ARGS: --features ssl
9+
CARGO_ARGS: --features ssl jit
1010

1111
jobs:
1212
rust_tests:

jit/src/instructions.rs

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,41 @@
1-
use std::collections::HashMap;
21
use cranelift::prelude::*;
32
use num_traits::cast::ToPrimitive;
43
use rustpython_bytecode::bytecode::{BinaryOperator, Constant, Instruction, NameScope};
4+
use std::collections::HashMap;
55

66
use super::JITCompileError;
77

88
pub struct FunctionCompiler<'a, 'b> {
99
builder: &'a mut FunctionBuilder<'b>,
1010
stack: Vec<Value>,
11-
variables: HashMap<String, Variable>
11+
variables: HashMap<String, Variable>,
1212
}
1313

1414
impl<'a, 'b> FunctionCompiler<'a, 'b> {
1515
pub fn new(builder: &'a mut FunctionBuilder<'b>) -> FunctionCompiler<'a, 'b> {
1616
FunctionCompiler {
1717
builder,
1818
stack: Vec::new(),
19-
variables: HashMap::new()
19+
variables: HashMap::new(),
2020
}
2121
}
2222

2323
pub fn add_instruction(&mut self, instruction: &Instruction) -> Result<(), JITCompileError> {
2424
match instruction {
2525
Instruction::LoadName {
2626
name,
27-
scope: NameScope::Local
27+
scope: NameScope::Local,
2828
} => {
29-
let var = self.variables.get(name).ok_or(JITCompileError::BadBytecode)?;
29+
let var = self
30+
.variables
31+
.get(name)
32+
.ok_or(JITCompileError::BadBytecode)?;
3033
self.stack.push(self.builder.use_var(*var));
3134
Ok(())
3235
}
3336
Instruction::StoreName {
3437
name,
35-
scope: NameScope::Local
38+
scope: NameScope::Local,
3639
} => {
3740
let var = match self.variables.get(name) {
3841
Some(var) => *var,
@@ -43,7 +46,8 @@ impl<'a, 'b> FunctionCompiler<'a, 'b> {
4346
var
4447
}
4548
};
46-
self.builder.def_var(var, self.stack.pop().ok_or(JITCompileError::BadBytecode)?);
49+
self.builder
50+
.def_var(var, self.stack.pop().ok_or(JITCompileError::BadBytecode)?);
4751
Ok(())
4852
}
4953
Instruction::LoadConst {
@@ -62,22 +66,27 @@ impl<'a, 'b> FunctionCompiler<'a, 'b> {
6266
.return_(&[self.stack.pop().ok_or(JITCompileError::BadBytecode)?]);
6367
Ok(())
6468
}
65-
Instruction::BinaryOperation {
66-
op,
67-
..
68-
} => {
69+
Instruction::BinaryOperation { op, .. } => {
6970
let a = self.stack.pop().ok_or(JITCompileError::BadBytecode)?;
7071
let b = self.stack.pop().ok_or(JITCompileError::BadBytecode)?;
7172
match op {
7273
BinaryOperator::Add => {
7374
let (out, carry) = self.builder.ins().iadd_ifcout(a, b);
74-
self.builder.ins().trapif(IntCC::Overflow, carry, TrapCode::IntegerOverflow);
75+
self.builder.ins().trapif(
76+
IntCC::Overflow,
77+
carry,
78+
TrapCode::IntegerOverflow,
79+
);
7580
self.stack.push(out);
7681
Ok(())
7782
}
7883
BinaryOperator::Subtract => {
7984
let (out, carry) = self.builder.ins().isub_ifbout(a, b);
80-
self.builder.ins().trapif(IntCC::Overflow, carry, TrapCode::IntegerOverflow);
85+
self.builder.ins().trapif(
86+
IntCC::Overflow,
87+
carry,
88+
TrapCode::IntegerOverflow,
89+
);
8190
self.stack.push(out);
8291
Ok(())
8392
}

tests/test_snippets.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ class _TestType(enum.Enum):
2525
CPYTHON_RUNNER_DIR = os.path.abspath(os.path.join(ROOT_DIR, "py_code_object"))
2626
RUSTPYTHON_RUNNER_DIR = os.path.abspath(os.path.join(ROOT_DIR))
2727
RUSTPYTHON_LIB_DIR = os.path.abspath(os.path.join(ROOT_DIR, "Lib"))
28+
RUSTPYTHON_FEATURES = ["jit"]
29+
2830

2931
@contextlib.contextmanager
3032
def pushd(path):
@@ -136,7 +138,7 @@ def setUpClass(cls):
136138

137139
# cargo stuff
138140
profile_args = [] if RUST_DEBUG else ["--release"]
139-
subprocess.check_call(["cargo", "build", *profile_args])
141+
subprocess.check_call(["cargo", "build", "--features", *RUSTPYTHON_FEATURES, *profile_args])
140142

141143
@classmethod
142144
def tearDownClass(cls):

0 commit comments

Comments
 (0)