From c64e807bc6e75a3566c9505952c7b83d153d77ea Mon Sep 17 00:00:00 2001 From: ASDAlexander77 Date: Fri, 30 Oct 2026 22:41:33 +0000 Subject: [PATCH] -mm=own: no CSE ahead of inference; two equal concatenations are two owners Under --opt (on for --emit=jit) CSE was added to the FuncOp pipeline that runs before own's inference. It merged two equal allocations, e.g. `const s1 = "Hel" + "lo"` and `const s2 = "Hel" + "lo"`, into one value with two named owners, and inference refused it: "'this value' takes a second reference". CSE after inference would leave two releases of one allocation, so under own it does not run; rc, gc and none keep it. Co-Authored-By: Claude Opus 5.5 --- tslang/test/tester/CMakeLists.txt | 8 ++-- .../tester/own/own_equal_strings_into_any.ts | 47 +++++++++++++++++++ tslang/tslang/transform.cpp | 8 +++- 3 files changed, 58 insertions(+), 5 deletions(-) create mode 100644 tslang/test/tester/own/own_equal_strings_into_any.ts diff --git a/tslang/test/tester/CMakeLists.txt b/tslang/test/tester/CMakeLists.txt index 29d7f6c7a..9c035f288 100644 --- a/tslang/test/tester/CMakeLists.txt +++ b/tslang/test/tester/CMakeLists.txt @@ -4088,7 +4088,7 @@ foreach(own_test own_fresh_string own_fresh_array own_return_new own_try_local o own_generator_take own_rest_array_borrows own_string_copies own_captured_string_callback own_callback_and_call own_callback_captured own_literal_closures own_literal_variable_closures own_temporary_field_take own_union_narrowed_calls own_named_field_take own_let_field_take own_literal_captures own_literal_object_captures own_literal_interface_captures own_literal_let_captures own_interface_generic_method own_back_reference own_field_function_result own_callback_shapes own_callback_extended own_callback_interface - own_generic_specializations own_callback_cast own_callback_interface_slot) + own_generic_specializations own_callback_cast own_callback_interface_slot own_equal_strings_into_any) tslang_add_test(NAME test-jit-own-${own_test} COMMAND test-runner -jit -mm=own "${PROJECT_SOURCE_DIR}/test/tester/own/${own_test}.ts") tslang_add_test(NAME test-compile-own-${own_test} COMMAND test-runner -mm=own "${PROJECT_SOURCE_DIR}/test/tester/own/${own_test}.ts") # The promise: no counting at all in what own emits. @@ -4117,8 +4117,8 @@ endforeach() # A generator whose `@keeps` makes its state own its arguments (spec 38), and one given a generator # that borrows, and one over its `this` parameter; strings copied within a parameter the result borrows; -# elements and fields swapped; a callback given on through a parameter; the same programs under every -# other model. +# elements and fields swapped; a callback given on through a parameter; two equal concatenations +# each boxed into an `any` once; the same programs under every other model. foreach(keeps_mm rc gc none) foreach(keeps_test own_generator_keeps own_generator_keeps_bounded own_generator_this_param own_copy_within_returned_param own_swap_elements own_callback_forwarded @@ -4126,7 +4126,7 @@ foreach(keeps_mm rc gc none) own_generator_take own_rest_array_borrows own_string_copies own_captured_string_callback own_callback_and_call own_callback_captured own_literal_closures own_literal_variable_closures own_temporary_field_take own_union_narrowed_calls own_named_field_take own_let_field_take own_literal_captures own_literal_object_captures own_literal_interface_captures own_literal_let_captures own_interface_generic_method own_back_reference own_field_function_result own_callback_shapes own_callback_extended own_callback_interface - own_generic_specializations own_callback_cast own_callback_interface_slot) + own_generic_specializations own_callback_cast own_callback_interface_slot own_equal_strings_into_any) tslang_add_test(NAME test-jit-${keeps_mm}-${keeps_test} COMMAND test-runner -jit -mm=${keeps_mm} "${PROJECT_SOURCE_DIR}/test/tester/own/${keeps_test}.ts") tslang_add_test(NAME test-compile-${keeps_mm}-${keeps_test} COMMAND test-runner -mm=${keeps_mm} "${PROJECT_SOURCE_DIR}/test/tester/own/${keeps_test}.ts") endforeach() diff --git a/tslang/test/tester/own/own_equal_strings_into_any.ts b/tslang/test/tester/own/own_equal_strings_into_any.ts new file mode 100644 index 000000000..ca395e503 --- /dev/null +++ b/tslang/test/tester/own/own_equal_strings_into_any.ts @@ -0,0 +1,47 @@ +// -mm=own: two named consts built from the same run-time concatenation are two strings, each with +// its own owner. The optimiser's CSE would merge them into one value with two owners, so under own +// it does not run ahead of inference (the JIT optimises: `-jit` is the run that found it). Each is +// boxed into an `any` argument once, and both are read after a `churn()`. +class Bag { + keys: any[] = []; + has(key: any) { + for (const k of this.keys) if (k == key) return true; + return false; + } +} + +class C { + has(a: any) { + return true; + } +} + +function churn() { + const junk: string[] = []; + for (let i = 0; i < 64; i++) { + junk.push("junk" + i); + } +} + +function main() { + const c = new C(); + const s1 = "Hel" + "lo"; + print(c.has(s1)); + const s2 = "Hel" + "lo"; + print(c.has(s2)); + + const b = new Bag(); + let found = 0; + for (let i = 0; i < 1000; i++) { + const k1 = "k" + "ey"; + const k2 = "k" + "ey"; + if (b.has(k1)) found++; + if (b.has(k2)) found++; + churn(); + assert(k1 == "key" && k2 == "key"); + } + + assert(found == 0); + assert(s1 == "Hello" && s2 == "Hello"); + print("done."); +} diff --git a/tslang/tslang/transform.cpp b/tslang/tslang/transform.cpp index 56aef0206..4e06e62d0 100644 --- a/tslang/tslang/transform.cpp +++ b/tslang/tslang/transform.cpp @@ -198,7 +198,13 @@ int runMLIRPasses(mlir::MLIRContext &context, llvm::SourceMgr &sourceMgr, mlir:: #ifdef ENABLE_OPT_PASSES if (enableOpt) { - optPM.addPass(mlir::createCSEPass()); + // optPM runs ahead of own's inference. Not under own: CSE merges two equal allocations + // (`"a" + b` twice) into one value, which before inference has two owners and after it + // two releases. Under rc their retains and releases stay paired. + if (compileOptions.memoryModel != MemoryModelOwn) + { + optPM.addPass(mlir::createCSEPass()); + } pm.addPass(mlir::createLoopInvariantCodeMotionPass()); // Not under --di: stripping left every function a subprogram and every call no // location, which the LLVM dialect's verifier rejects for a call it could inline