Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize LIR #23

Merged
merged 1 commit into from
Jul 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 60 additions & 5 deletions crates/rue-compiler/src/optimizer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,18 @@ impl<'a> Optimizer<'a> {
fn opt_add(&mut self, env_id: EnvironmentId, lhs: MirId, rhs: MirId) -> LirId {
let lhs = self.opt_mir(env_id, lhs);
let rhs = self.opt_mir(env_id, rhs);
self.db.alloc_lir(Lir::Add(vec![lhs, rhs]))
let mut args = Vec::new();
if let Lir::Add(lhs) = self.db.lir(lhs) {
args.extend(lhs);
} else {
args.push(lhs);
}
if let Lir::Add(rhs) = self.db.lir(rhs) {
args.extend(rhs);
} else {
args.push(rhs);
}
self.db.alloc_lir(Lir::Add(args))
}

fn opt_subtract(&mut self, env_id: EnvironmentId, lhs: MirId, rhs: MirId) -> LirId {
Expand All @@ -241,7 +252,18 @@ impl<'a> Optimizer<'a> {
fn opt_multiply(&mut self, env_id: EnvironmentId, lhs: MirId, rhs: MirId) -> LirId {
let lhs = self.opt_mir(env_id, lhs);
let rhs = self.opt_mir(env_id, rhs);
self.db.alloc_lir(Lir::Mul(vec![lhs, rhs]))
let mut args = Vec::new();
if let Lir::Mul(lhs) = self.db.lir(lhs) {
args.extend(lhs);
} else {
args.push(lhs);
}
if let Lir::Mul(rhs) = self.db.lir(rhs) {
args.extend(rhs);
} else {
args.push(rhs);
}
self.db.alloc_lir(Lir::Mul(args))
}

fn opt_divide(&mut self, env_id: EnvironmentId, lhs: MirId, rhs: MirId) -> LirId {
Expand Down Expand Up @@ -316,7 +338,18 @@ impl<'a> Optimizer<'a> {
fn opt_concat(&mut self, env_id: EnvironmentId, lhs: MirId, rhs: MirId) -> LirId {
let lhs = self.opt_mir(env_id, lhs);
let rhs = self.opt_mir(env_id, rhs);
self.db.alloc_lir(Lir::Concat(vec![lhs, rhs]))
let mut args = Vec::new();
if let Lir::Concat(lhs) = self.db.lir(lhs) {
args.extend(lhs);
} else {
args.push(lhs);
}
if let Lir::Concat(rhs) = self.db.lir(rhs) {
args.extend(rhs);
} else {
args.push(rhs);
}
self.db.alloc_lir(Lir::Concat(args))
}

fn opt_point_add(&mut self, env_id: EnvironmentId, lhs: MirId, rhs: MirId) -> LirId {
Expand All @@ -338,13 +371,35 @@ impl<'a> Optimizer<'a> {
fn opt_any(&mut self, env_id: EnvironmentId, lhs: MirId, rhs: MirId) -> LirId {
let lhs = self.opt_mir(env_id, lhs);
let rhs = self.opt_mir(env_id, rhs);
self.db.alloc_lir(Lir::Any(vec![lhs, rhs]))
let mut args = Vec::new();
if let Lir::Any(lhs) = self.db.lir(lhs) {
args.extend(lhs);
} else {
args.push(lhs);
}
if let Lir::Any(rhs) = self.db.lir(rhs) {
args.extend(rhs);
} else {
args.push(rhs);
}
self.db.alloc_lir(Lir::Any(args))
}

fn opt_all(&mut self, env_id: EnvironmentId, lhs: MirId, rhs: MirId) -> LirId {
let lhs = self.opt_mir(env_id, lhs);
let rhs = self.opt_mir(env_id, rhs);
self.db.alloc_lir(Lir::All(vec![lhs, rhs]))
let mut args = Vec::new();
if let Lir::All(lhs) = self.db.lir(lhs) {
args.extend(lhs);
} else {
args.push(lhs);
}
if let Lir::All(rhs) = self.db.lir(rhs) {
args.extend(rhs);
} else {
args.push(rhs);
}
self.db.alloc_lir(Lir::All(args))
}

fn opt_not(&mut self, env_id: EnvironmentId, value: MirId) -> LirId {
Expand Down
116 changes: 58 additions & 58 deletions tests.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ output = "120"
hash = "e287a4cdf0ee9e003ee4450c1408d162e08db92a141035bf3ff932d46ca87f93"

[const_multiple_reference]
bytes = 77
cost = 2805
bytes = 65
cost = 1672
input = "()"
output = "\"Hello, Bob!\""
hash = "ad1d14ebd66b8fad929ee4b816aa51410d12f987bbeabffd571d0701fe061281"
hash = "3e851b5ad6b5c395bd46219991d2c7bb113e91918a065c705cc9068445bcc2a7"

[inline_function_cycle]
parser_errors = []
Expand All @@ -32,11 +32,11 @@ parser_errors = []
compiler_errors = ["Error: Cannot recursively call inline function. (5:27)"]

[mixed_consts]
bytes = 43
cost = 2144
bytes = 39
cost = 1711
input = "()"
output = "126"
hash = "834c349efb47a4863d393d9266c217f5b2ab0da53d1b4cd03b1df43ac19ca85f"
hash = "14870051d753aa4295466e166782fb5da7f18ad450a0ed0af9d7ccb83a82bed9"

[inline_const_function_cycle]
parser_errors = []
Expand Down Expand Up @@ -98,11 +98,11 @@ output = "0x288b694028"
hash = "0f74e7fd57bb9a7e05cf979ee986a8e0120801d2b18019c275a9717fce22f379"

[struct_optional_initializer]
bytes = 323
cost = 50921
bytes = 319
cost = 43841
input = "()"
output = "()"
hash = "7346d21032e40d7c691f7411e3d115810f167d1985389d65acf581670c230d0e"
hash = "6e01e2f4d94692686fd7696643431390d821103b211ae74e23def8b42031be97"

[struct_single_optional]
bytes = 83
Expand All @@ -119,11 +119,11 @@ output = "()"
hash = "9d76d68d33ab457ba1482630892e687c2a062b6081d5a386b344696908bab9e8"

[struct_optional]
bytes = 127
cost = 5030
bytes = 123
cost = 4597
input = "()"
output = "221"
hash = "cca28c8cd08bf5e84d1b51883b045c1018acc21064c8f82c5d0c223f5e1039a8"
hash = "f74e8a1315435c77b0677225e1e68bd3a92ba7048b1f9946b7cf9a1c723d787b"

[struct]
bytes = 37
Expand All @@ -140,11 +140,11 @@ output = "76"
hash = "917b0e1ee2c8ad5755e1f97c4642ea653288acf816eee1b0d537dd8e01106711"

[block_const]
bytes = 101
cost = 4468
bytes = 97
cost = 4022
input = "()"
output = "1168"
hash = "9417e13c05b85934e8a305f36ccc83bf55fec11978461c6ebb630b739f837c43"
hash = "09949696fa19e25b6dd9e208fd55fd96d22298930ea41668b1276d5280d67b43"

[block_nested_raise]
bytes = 21
Expand All @@ -155,11 +155,11 @@ hash = "6fdd7c92cc7f9e42a53e07e6123bb5267cc5cc08912d5cb8ed26925aa6fd9672"
error = "\"Explicit raise\""

[block_let]
bytes = 47
cost = 3460
bytes = 39
cost = 2581
input = "()"
output = "1168"
hash = "3070c823a9d006d346744228f842e6f86692158d6afa8705d01a4be322a8e751"
hash = "4d0b3e7905efc3108e842985d16a05f6284e177cd8075c182ab11b99ba716f18"

[block_raise]
bytes = 21
Expand Down Expand Up @@ -195,11 +195,11 @@ output = "84"
hash = "1a6d758fe06cbcd5d1911a5419c1308b52fc69c95872483506e28f6348b08a44"

[block_inline_const]
bytes = 37
cost = 3150
bytes = 25
cost = 1838
input = "()"
output = "1168"
hash = "11efffdca3fc8786b6e51cc51f13a52e02fc6af181c5b7019424251ae785bd00"
hash = "6636275e009c26ccaba0bde1f76d4e1451f57462b1d2528a3a5df0a3202c1f78"

[block_nested_return]
parser_errors = []
Expand Down Expand Up @@ -227,18 +227,18 @@ output = "()"
hash = "a904e8177e07f7f7c2454b8cb53017f85181b0b7841ba9275950e4d6151c9b84"

[enum_mixed]
bytes = 279
cost = 33360
bytes = 275
cost = 29112
input = "()"
output = "()"
hash = "0af9a22b05c1ceb929e756159fb1c9dffbbc725179e6669ab4c00e712e0008ab"
hash = "951c93c0f57cdebc0c526b69556d6414c62d03be39bad27b08f27bddae98744d"

[enum_empty]
bytes = 177
cost = 12614
bytes = 173
cost = 11198
input = "()"
output = "()"
hash = "b1a579071d1400cffb70369511e553d9ece5c6966bb03c5b19cd48bde773c8d5"
hash = "600c2c11a7ceb22fd3f5d2559d22120f9ad7e8d47e0966ddcfbcce20e02f373f"

[function_call]
parser_errors = []
Expand Down Expand Up @@ -279,11 +279,11 @@ compiler_errors = [
]

[struct_empty]
bytes = 165
bytes = 161
cost = 4104
input = "()"
output = "()"
hash = "bb2f6e4150d64965b692025d779786e459fc00ab5f7a6cf748315c5d470d64e6"
hash = "bf0ebac819c080030caeaaff2dde6b8382e18c39c30949a382368520e7026771"

[birecursive_functions]
bytes = 195
Expand All @@ -293,11 +293,11 @@ output = "()"
hash = "770bb2c0b2582924adf403e62374f8424a2ed510eef70b5f450eccab238a4911"

[inner_inline_functions]
bytes = 329
cost = 16899
bytes = 301
cost = 15121
input = "()"
output = "0x0a37e3fbe2"
hash = "921423faa6da77bee705fcefd72b1a48d260fd74f197a387c39168e2438cdc8b"
hash = "d0518a7a1bb65cb587cd00b19dc487cf8aae4950fdeea6a4e8c163962fd6ad81"

[infer_generic_lambda]
bytes = 65
Expand Down Expand Up @@ -379,25 +379,25 @@ output = "152"
hash = "38b1cec180a0bc0f5ec91097cec51971df126e3b18af54ddba4a3e4a36f9c285"

[std]
bytes = 832
cost = 91299
bytes = 828
cost = 82803
input = "()"
output = "()"
hash = "7eab50734f88cbad6a3794be152d314496d7ae932f39947f34a7884ab1492f67"
hash = "962d75284aa716681f671c5deac36f613c51b117f7625f3ece1310b590b79b34"

[literal_bytes]
bytes = 371
cost = 17954
bytes = 367
cost = 15830
input = "()"
output = "0xf1fb6ca14066f4236b3cc44a79a78fe90d44f1d45e04f9b9939863db71b99560"
hash = "da23d6cdd53ce5a6807d40aa0a73b8d2d748a0e8684394e94681c00708f3e660"
hash = "14c7dd45e2f1dba24d5941e5b5ca95eb44dcaee423bf45a8edf8f8a1737ae674"

[concat_bytes]
bytes = 42
cost = 2500
bytes = 30
cost = 1159
input = "()"
output = "\"Hello, world!\""
hash = "6da79c81642d2949a1ca049098cd148cfb1ec7444a86cd6392fb46faf6e76391"
hash = "d30484c7f6402b6fe4ab2874dd328ff0c5385394e68d9743cc29ea36405c6961"

[integer_separator]
bytes = 6
Expand Down Expand Up @@ -449,11 +449,11 @@ output = "0x064cc0"
hash = "96a314da556b577d15a2e0a2db42e3b7f22610ab4336ed86713716c5a029e787"

[boolean_arithmetic]
bytes = 55
cost = 4977
bytes = 47
cost = 3975
input = "()"
output = "1"
hash = "bc9d13262f5a2ab4c0e75a62ade773d493a624e05248c596a58bd85d8466c68f"
hash = "7c0bf0b36503c7abd7e264b2d63cc829162cd1fdff59d1f34792cc043a24e9a5"

[bitwise_shift]
bytes = 41
Expand Down Expand Up @@ -493,18 +493,18 @@ compiler_errors = [
]

[p2_fusion]
bytes = 722
cost = 87151
bytes = 678
cost = 69048
input = "((0x4696e7a2b7682e2df01ab47e6e002d0dca895f99c6172e4a55a3e033499532b7 0x32ed6e4964102d7093ef350019dfd14c99a3ea9feac1f3502194384b8976f7c1 0x770bb2c0b2582924adf403e62374f8424a2ed510eef70b5f450eccab238a4911) 0x8d496dc7cdbc417db2132eda6894b29a91511f176e93a1ea943d210cd27822b2 0x3878fc2ed6b703c7c3d00f9f5d8f170ec252ca439be6e5b5b516e1dce1adc0d7 0x3fe4d62a7db919b25377cb207f16fa0fb6519e123366eaa23014dd5d7c967ca2 0x837525fb91b88dbf02d9ef5a0b322f6943f93424b6e8fe6d26dd1be6d3311871 1 0xc19110971a0cea01368f0c7599b8984f74e301b7cda20bd7f86eb2296bbf16c5)"
output = "((73 1) (72 0x6155f55414a5cd9193bef33744095a78d57852cf24241b25edeffad6e544c499) (63 0x9d6824bfdfb4c726334a210b657a5e4126c3fbb378598bb3a5b7a210bb75cdb8) (g1_negate 0xc19110971a0cea01368f0c7599b8984f74e301b7cda20bd7f86eb2296bbf16c5 1 (0xc19110971a0cea01368f0c7599b8984f74e301b7cda20bd7f86eb2296bbf16c5)) (60 0x81fdd3fbc407906bc0875522e7a2e77409ee5ef17d3eaa611b505b344588f6b6))"
hash = "1f6abfdd1000d9ed87740ca5a0a888a042d64a8a7b6d60f082aab5be1f0915b4"
hash = "2bd56215c0c88b31c1d488c5080ecf4a131a0bf52c99bb222869da2451ad7540"

[multisig]
bytes = 489
cost = 27062
bytes = 485
cost = 24230
input = "((0xc00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 0xc00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000) 1 (()) ((51 0x0000000000000000000000000000000000000000000000000000000000000000 1000)))"
output = "((g1_multiply 0xc00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 0x502cea601814d49886e0713402860d6054f6947d811b3b93100c6b364626651a) (g1_negate 0x0000000000000000000000000000000000000000000000000000000000000000 1000))"
hash = "173385b87af5d8940767c328026fe5f8e76bc238d2a3aaddf4f55e844f400fca"
hash = "cc57ebae1e4370a56127589fa2808a5be775dc801b056cb60dbc950010cb0da4"

[hello_world]
bytes = 16
Expand Down Expand Up @@ -535,25 +535,25 @@ output = "((60 36) (73 0x0186a0) (g1_negate 0x42840c6aebec47ce2e01629ce381b461c1
hash = "7eb7bdb24d4069d7dc9787e7727ef4f0feef354712b1963667d475065122b9de"

[p2_conditions]
bytes = 145
cost = 18933
bytes = 141
cost = 16101
input = "(0xc00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 ((51 0x291e4594b43d58e833cab95e4b165c5fac6b4d8391c81ebfd20efdd8d58b92d8 1000)))"
output = "((g1_multiply 0xc00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 0xc239bbcfc69fb5abacdd8dc174c6b33170c6d902dec3bc1c87662020cf044313) (g1_negate 0x291e4594b43d58e833cab95e4b165c5fac6b4d8391c81ebfd20efdd8d58b92d8 1000))"
hash = "e69958bce8b4294e16e9b54f6f6795228fbd6daa47ff27ab58a953c611367be2"
hash = "9bdda9e1aa36d402104334fad3d7d9ce5b7b507b92f985e24c21513a012f60c4"

[p2_delegated_or_hidden]
bytes = 253
cost = 24446
bytes = 249
cost = 20906
input = "(0xc00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 () (q . ((51 0x291e4594b43d58e833cab95e4b165c5fac6b4d8391c81ebfd20efdd8d58b92d8 1000))) 1)"
output = "((g1_multiply 0xc00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 0x3d7e1145b5969c12f4889f4f1f66bde0e4d3ba54b91784cf604294d162b44b69) (g1_negate 0x291e4594b43d58e833cab95e4b165c5fac6b4d8391c81ebfd20efdd8d58b92d8 1000))"
hash = "9b1c580707ca8282534c02c1a055427e0954818b6195a29f4442ac3e7ea8e8ee"
hash = "21f96d7bb1b15b83ce81dff3525d4c98793f906f6cc7ebba52a76524a7db6943"

[singleton]
bytes = 1427
bytes = 1395
cost = 0
input = "((0x42840c6aebec47ce2e01629ce381b461c19695264281a7b1aab5d4ff54506775 0x4696e7a2b7682e2df01ab47e6e002d0dca895f99c6172e4a55a3e033499532b7 0x291e4594b43d58e833cab95e4b165c5fac6b4d8391c81ebfd20efdd8d58b92d8) 1 (0x9b1c580707ca8282534c02c1a055427e0954818b6195a29f4442ac3e7ea8e8ee () 1) 1 ((51 0x173385b87af5d8940767c328026fe5f8e76bc238d2a3aaddf4f55e844f400fca 1)))"
output = "()"
hash = "ca8bea1d975df6e382a60ef70f92d14de0eca974c548175adc6ee8c79597f91b"
hash = "37a397e01acadf7d7dc4266250055e482b4bcbd8f62469c9794b0e433983c8a0"
error = "()"

[enum_type_guard]
Expand Down