Repository navigation
A global initialised with a constant string concatenation runs as a constructor (#469) - #473
Merged
Merged
Conversation
…onstructor (#469) `let S = "text" + 4;` at module level, read by a function, failed to compile in every model: "ops with side effects not allowed in global initializers". GlobalOpLowering puts an initializer that looks constant into the llvm.mlir.global's own region and anything that runs code into a per-global constructor. Its list knew ts.StringConcat, but at that point the concatenation is still a ts.ArithmeticBinary with a string result, and the number's conversion (`ts.Cast` si32 -> string, a sprintf into a new block) was not on the list either, so both landed in the region, where allocating is not allowed. With a variable or a call among the operands it was already a constructor, and a global nothing reads was dropped before it mattered. Now a string-typed ts.ArithmeticBinary, and a cast to string from anything that is not already a string, make the global a constructor. A plain string constant stays in place. Test: 00global_constant_concat (compile and JIT, and in the rc/none corpus): `"text" + 4`, `"text" + "four"`, a `string | null` and `"" + 6.5` globals read in main, a plain string constant, and a reassignment. It failed to compile before the change. Fixes #469. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #469.
let S = "text" + 4;at module level, read by a function, failed to compile in every model with "ops with side effects not allowed in global initializers".GlobalOpLoweringdecides where each global's initializer goes:llvm.mlir.global's own region;Its list of code-running ops included
ts.StringConcat, but two ops were missing:ts.ArithmeticBinarywith a string result;ts.Castsi32 → string, asprintfinto a new block) was not on the list either.So both landed in the region, where allocating is not allowed. With a variable or a call among the operands the global was already a constructor, and a global that nothing reads was dropped before it mattered.
Now a string-typed
ts.ArithmeticBinary, or a cast to string from anything that is not already a string, makes the global a constructor. A plain string constant stays in place.Tests
00global_constant_concat.ts, registered as compile and JIT tests and in the rc/none corpus. It failed to compile before the change. It covers:main:"text" + 4,"text" + "four", astring | null, and"" + 6.5;test-compile-gc-defaultlib-collector, the known local LNK2005 against the stale installed default library, which fails on main the same way.🤖 Generated with Claude Code