Skip to content

Commit 88cf93e

Browse files
olwangclaude
andcommitted
rsscript: wire sin/trunc/floordiv/floormod + metal_* into reg-VM dispatch
The reg-VM (rss dev --run) has its own intrinsic dispatch table separate from the AOT lowering path; the new Tensor methods were only in runtime_abi/.rssi, so the VM rejected them ('reg VM v0 does not support intrinsic'). Adds the 7 RegIntrinsic variants, name->variant map entries, and execution arms (sin/trunc as unary float ops; floordiv/floormod in the int-binary group; matmul_metal as a Result binary; metal_available->Bool; metal_device_name->String). Verified from rss code via the VM: device = Apple M3 Max, GPU matmul [2,3]x[3,2] = 58 64 139 154 (correct). Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
1 parent 2fa0366 commit 88cf93e

1 file changed

Lines changed: 40 additions & 2 deletions

File tree

  • crates/rsscript/src/reg_vm

crates/rsscript/src/reg_vm/mod.rs

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2120,6 +2120,9 @@ enum RegIntrinsic {
21202120
TensorShape,
21212121
TensorRank,
21222122
TensorMatmul,
2123+
TensorMatmulMetal,
2124+
TensorMetalAvailable,
2125+
TensorMetalDeviceName,
21232126
TensorAdd,
21242127
TensorSub,
21252128
TensorMul,
@@ -2165,11 +2168,15 @@ enum RegIntrinsic {
21652168
TensorExp2,
21662169
TensorLog2,
21672170
TensorRsqrt,
2171+
TensorSin,
2172+
TensorTrunc,
21682173
TensorPow,
21692174
// bmm+int/bit (ops D)
21702175
TensorBmm,
21712176
TensorIdiv,
21722177
TensorMod,
2178+
TensorFloordiv,
2179+
TensorFloormod,
21732180
TensorShl,
21742181
TensorShr,
21752182
TensorAnd,
@@ -5432,6 +5439,9 @@ fn qualified_intrinsic(namespace: &str, name: &str) -> Option<RegIntrinsic> {
54325439
("Tensor", "shape") => Some(RegIntrinsic::TensorShape),
54335440
("Tensor", "rank") => Some(RegIntrinsic::TensorRank),
54345441
("Tensor", "matmul") => Some(RegIntrinsic::TensorMatmul),
5442+
("Tensor", "matmul_metal") => Some(RegIntrinsic::TensorMatmulMetal),
5443+
("Tensor", "metal_available") => Some(RegIntrinsic::TensorMetalAvailable),
5444+
("Tensor", "metal_device_name") => Some(RegIntrinsic::TensorMetalDeviceName),
54355445
("Tensor", "add") => Some(RegIntrinsic::TensorAdd),
54365446
("Tensor", "sub") => Some(RegIntrinsic::TensorSub),
54375447
("Tensor", "mul") => Some(RegIntrinsic::TensorMul),
@@ -5477,11 +5487,15 @@ fn qualified_intrinsic(namespace: &str, name: &str) -> Option<RegIntrinsic> {
54775487
("Tensor", "exp2") => Some(RegIntrinsic::TensorExp2),
54785488
("Tensor", "log2") => Some(RegIntrinsic::TensorLog2),
54795489
("Tensor", "rsqrt") => Some(RegIntrinsic::TensorRsqrt),
5490+
("Tensor", "sin") => Some(RegIntrinsic::TensorSin),
5491+
("Tensor", "trunc") => Some(RegIntrinsic::TensorTrunc),
54805492
("Tensor", "pow") => Some(RegIntrinsic::TensorPow),
54815493
// bmm+int/bit (ops D)
54825494
("Tensor", "bmm") => Some(RegIntrinsic::TensorBmm),
54835495
("Tensor", "idiv") => Some(RegIntrinsic::TensorIdiv),
54845496
("Tensor", "modulo") => Some(RegIntrinsic::TensorMod),
5497+
("Tensor", "floordiv") => Some(RegIntrinsic::TensorFloordiv),
5498+
("Tensor", "floormod") => Some(RegIntrinsic::TensorFloormod),
54855499
("Tensor", "shl") => Some(RegIntrinsic::TensorShl),
54865500
("Tensor", "shr") => Some(RegIntrinsic::TensorShr),
54875501
("Tensor", "bit_and") => Some(RegIntrinsic::TensorAnd),
@@ -9594,6 +9608,22 @@ impl RegVm {
95949608
)),
95959609
}))
95969610
}
9611+
RegIntrinsic::TensorMatmulMetal => {
9612+
let a = self.expect_tensor_ref(intrinsic_arg(&self.stack, base, args, 0)?)?;
9613+
let b = self.expect_tensor_ref(intrinsic_arg(&self.stack, base, args, 1)?)?;
9614+
Ok(json_result(match rsscript_runtime::tensor_matmul_metal(&a, &b) {
9615+
Ok(tensor) => Ok(self.store_tensor(tensor)),
9616+
Err(error) => Err(tensor_error_value(
9617+
rsscript_runtime::tensor_error_message(&error),
9618+
)),
9619+
}))
9620+
}
9621+
RegIntrinsic::TensorMetalAvailable => {
9622+
Ok(VmValue::Bool(rsscript_runtime::tensor_metal_available()))
9623+
}
9624+
RegIntrinsic::TensorMetalDeviceName => Ok(VmValue::String(Rc::new(
9625+
rsscript_runtime::tensor_metal_device_name(),
9626+
))),
95979627
RegIntrinsic::TensorAdd
95989628
| RegIntrinsic::TensorSub
95999629
| RegIntrinsic::TensorMul
@@ -9830,13 +9860,17 @@ impl RegVm {
98309860
RegIntrinsic::TensorReciprocal
98319861
| RegIntrinsic::TensorExp2
98329862
| RegIntrinsic::TensorLog2
9833-
| RegIntrinsic::TensorRsqrt => {
9863+
| RegIntrinsic::TensorRsqrt
9864+
| RegIntrinsic::TensorSin
9865+
| RegIntrinsic::TensorTrunc => {
98349866
let t = self.expect_tensor_ref(intrinsic_arg(&self.stack, base, args, 0)?)?;
98359867
let result = match intrinsic {
98369868
RegIntrinsic::TensorReciprocal => rsscript_runtime::tensor_reciprocal(&t),
98379869
RegIntrinsic::TensorExp2 => rsscript_runtime::tensor_exp2(&t),
98389870
RegIntrinsic::TensorLog2 => rsscript_runtime::tensor_log2(&t),
9839-
_ => rsscript_runtime::tensor_rsqrt(&t),
9871+
RegIntrinsic::TensorRsqrt => rsscript_runtime::tensor_rsqrt(&t),
9872+
RegIntrinsic::TensorSin => rsscript_runtime::tensor_sin(&t),
9873+
_ => rsscript_runtime::tensor_trunc(&t),
98409874
};
98419875
Ok(self.store_tensor(result))
98429876
}
@@ -9854,6 +9888,8 @@ impl RegVm {
98549888
RegIntrinsic::TensorBmm
98559889
| RegIntrinsic::TensorIdiv
98569890
| RegIntrinsic::TensorMod
9891+
| RegIntrinsic::TensorFloordiv
9892+
| RegIntrinsic::TensorFloormod
98579893
| RegIntrinsic::TensorShl
98589894
| RegIntrinsic::TensorShr
98599895
| RegIntrinsic::TensorAnd
@@ -9865,6 +9901,8 @@ impl RegVm {
98659901
RegIntrinsic::TensorBmm => rsscript_runtime::tensor_bmm(&a, &b),
98669902
RegIntrinsic::TensorIdiv => rsscript_runtime::tensor_idiv(&a, &b),
98679903
RegIntrinsic::TensorMod => rsscript_runtime::tensor_mod(&a, &b),
9904+
RegIntrinsic::TensorFloordiv => rsscript_runtime::tensor_floordiv(&a, &b),
9905+
RegIntrinsic::TensorFloormod => rsscript_runtime::tensor_floormod(&a, &b),
98689906
RegIntrinsic::TensorShl => rsscript_runtime::tensor_shl(&a, &b),
98699907
RegIntrinsic::TensorShr => rsscript_runtime::tensor_shr(&a, &b),
98709908
RegIntrinsic::TensorAnd => rsscript_runtime::tensor_and(&a, &b),

0 commit comments

Comments
 (0)