From 092b697198884347c07e39d0e817a9ff76ca684e Mon Sep 17 00:00:00 2001 From: ASDAlexander77 Date: Thu, 12 Nov 2026 09:20:14 +0000 Subject: [PATCH] Constant arrays and tuples are named from their content, not from attribute addresses calc_hash_value combined hash_value of the elements' attributes, which hashes their addresses with a weak mix: [1, 2] and [3, 4] were named about 1200 apart, the names changed from run to run, and a collision gave one constant array another's data (00owned_iteration failed once on wasm32 rc debug, a rebuild passed). The name is now an xxh3 hash of the type and elements as MLIR prints them, and an array's size is kept apart from it ("a_2" + "11..." was "a_21" + "1..."). Co-Authored-By: Claude Opus 5.5 --- .../TypeScript/LowerToLLVM/LLVMCodeHelper.h | 21 +++++++++++-------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/tslang/include/TypeScript/LowerToLLVM/LLVMCodeHelper.h b/tslang/include/TypeScript/LowerToLLVM/LLVMCodeHelper.h index 2f283148d..aada79cc3 100644 --- a/tslang/include/TypeScript/LowerToLLVM/LLVMCodeHelper.h +++ b/tslang/include/TypeScript/LowerToLLVM/LLVMCodeHelper.h @@ -23,6 +23,7 @@ #include "llvm/ADT/TypeSwitch.h" #include "llvm/Support/Debug.h" +#include "llvm/Support/xxhash.h" #define DEBUG_TYPE "llvm" @@ -40,18 +41,19 @@ class LLVMCodeHelper : public LLVMCodeHelperBase { } + // The name of the global that holds a constant: `prefix` and a hash of the constant's text - its + // type and elements, as MLIR prints them. A global is shared by every constant with the same + // name, so the name has to follow the content. hash_value of an attribute or a type hashes its + // address: weakly mixed, two arrays' names were often only a few hundred apart and could meet, + // handing one constant array another's data, and they changed from one run to the next. std::string calc_hash_value(ArrayAttr &arrayAttr, mlir::Type llvmType, const char *prefix) const { - auto opHash = 0ULL; - opHash ^= hash_value(llvmType) + 0x9e3779b9 + (opHash << 6) + (opHash >> 2); - for (auto item : arrayAttr) - { - opHash ^= hash_value(item) + 0x9e3779b9 + (opHash << 6) + (opHash >> 2); - } + std::string text; + llvm::raw_string_ostream os(text); + os << llvmType << '|' << arrayAttr; - // calculate name; std::stringstream vecVarName; - vecVarName << prefix << opHash; + vecVarName << prefix << llvm::xxh3_64bits(text); return vecVarName.str(); } @@ -436,7 +438,8 @@ class LLVMCodeHelper : public LLVMCodeHelperBase std::string getGlobalArrayName(mlir::Type originalElementType, unsigned size, ArrayAttr arrayAttr) { std::stringstream ss; - ss << "a_" << size; + // the size apart from the hash: "a_2" + "11..." and "a_21" + "1..." were the same name + ss << "a_" << size << "_"; return calc_hash_value(arrayAttr, originalElementType, ss.str().c_str()); }