From 8b145a186fdf4bd19ff13de9e264a0e877f4fd5d Mon Sep 17 00:00:00 2001 From: ASDAlexander77 Date: Thu, 12 Nov 2026 09:45:18 +0000 Subject: [PATCH] Type descriptors, ownership routines and object vtables are named from the type's text td_, tsrel_, tsret_, tsshr_, tsshrbox_, tsrelrec_ and the other ownership routines, and an object's interface vtable, were named from hash_value(type): the type's storage address, weakly mixed. The names changed from run to run, and two types could share one name and so one descriptor or release routine. MLIRHelper::hashOfType hashes the type as MLIR prints it (xxh3), as the constant arrays' names now are. A program now compiles to the same IR every time. Co-Authored-By: Claude Opus 5.5 --- .../TypeScript/LowerToLLVM/LLVMCodeHelper.h | 2 +- .../TypeScript/LowerToLLVM/OwnershipRoutineLogic.h | 14 +++++++------- tslang/include/TypeScript/MLIRLogic/MLIRHelper.h | 11 +++++++++++ tslang/lib/TypeScript/MLIRGenImpl.h | 2 +- 4 files changed, 20 insertions(+), 9 deletions(-) diff --git a/tslang/include/TypeScript/LowerToLLVM/LLVMCodeHelper.h b/tslang/include/TypeScript/LowerToLLVM/LLVMCodeHelper.h index aada79cc3..2a638002a 100644 --- a/tslang/include/TypeScript/LowerToLLVM/LLVMCodeHelper.h +++ b/tslang/include/TypeScript/LowerToLLVM/LLVMCodeHelper.h @@ -366,7 +366,7 @@ class LLVMCodeHelper : public LLVMCodeHelperBase // keyed by the concrete type rather than by the name: every class reports the name // "class", but each needs its own record, which is the point of having one at all std::stringstream varName; - varName << "td_" << (size_t)hash_value(type) << variant.str() << "_" << name; + varName << "td_" << MLIRHelper::hashOfType(type) << variant.str() << "_" << name; TypeConverterHelper tch(typeConverter); auto blockWordType = getHeapBlockWordType(); diff --git a/tslang/include/TypeScript/LowerToLLVM/OwnershipRoutineLogic.h b/tslang/include/TypeScript/LowerToLLVM/OwnershipRoutineLogic.h index 40c23bdd5..6dfe40740 100644 --- a/tslang/include/TypeScript/LowerToLLVM/OwnershipRoutineLogic.h +++ b/tslang/include/TypeScript/LowerToLLVM/OwnershipRoutineLogic.h @@ -209,7 +209,7 @@ class OwnershipRoutineLogic } std::stringstream nameStream; - nameStream << "tsrelrec_" << (size_t)hash_value(type); + nameStream << "tsrelrec_" << MLIRHelper::hashOfType(type); for (auto index : borrowed) { nameStream << "_" << index; @@ -586,7 +586,7 @@ class OwnershipRoutineLogic } std::stringstream nameStream; - nameStream << "tsshr_" << (size_t)hash_value(type); + nameStream << "tsshr_" << MLIRHelper::hashOfType(type); auto name = nameStream.str(); auto parentModule = op->getParentOfType(); @@ -627,7 +627,7 @@ class OwnershipRoutineLogic } std::stringstream nameStream; - nameStream << "tsshrbox_" << (size_t)hash_value(captureRefType); + nameStream << "tsshrbox_" << MLIRHelper::hashOfType(captureRefType); auto name = nameStream.str(); auto parentModule = op->getParentOfType(); @@ -720,7 +720,7 @@ class OwnershipRoutineLogic std::string getRoutineName(mlir::Type type) { std::stringstream ss; - ss << "tsrel_" << (size_t)hash_value(type); + ss << "tsrel_" << MLIRHelper::hashOfType(type); return ss.str(); } @@ -745,7 +745,7 @@ class OwnershipRoutineLogic } std::stringstream ss; - ss << "tsret_" << (size_t)hash_value(type); + ss << "tsret_" << MLIRHelper::hashOfType(type); return ss.str(); } @@ -766,7 +766,7 @@ class OwnershipRoutineLogic } std::stringstream nameStream; - nameStream << prefix.str() << (size_t)hash_value(type); + nameStream << prefix.str() << MLIRHelper::hashOfType(type); auto name = nameStream.str(); auto parentModule = op->getParentOfType(); @@ -1518,7 +1518,7 @@ class OwnershipRoutineLogic bool holdsNothing = false, bool borrowsContents = false) { std::stringstream nameStream; - nameStream << prefix.str() << (size_t)hash_value(captureRefType); + nameStream << prefix.str() << MLIRHelper::hashOfType(captureRefType); auto name = nameStream.str(); auto parentModule = op->getParentOfType(); diff --git a/tslang/include/TypeScript/MLIRLogic/MLIRHelper.h b/tslang/include/TypeScript/MLIRLogic/MLIRHelper.h index 8e943e56d..887156df5 100644 --- a/tslang/include/TypeScript/MLIRLogic/MLIRHelper.h +++ b/tslang/include/TypeScript/MLIRLogic/MLIRHelper.h @@ -207,6 +207,17 @@ class MLIRHelper return getCallSiteLocation(callee.getDefiningOp()->getLoc(), caller, enable); } + // A hash of a type for a symbol's name: of its text, as MLIR prints it. hash_value(type) hashes + // the type's storage address, weakly mixed: names made from it were different in every run, and + // two types could meet in one name - and so share a type descriptor or a release routine. + static uint64_t hashOfType(mlir::Type type) + { + std::string text; + llvm::raw_string_ostream os(text); + os << type; + return llvm::xxh3_64bits(text); + } + static void getAnonymousNameStep(std::stringstream &ssName, mlir::Location loc) { mlir::TypeSwitch(loc) diff --git a/tslang/lib/TypeScript/MLIRGenImpl.h b/tslang/lib/TypeScript/MLIRGenImpl.h index ae1d61fdc..ca73a7a41 100644 --- a/tslang/lib/TypeScript/MLIRGenImpl.h +++ b/tslang/lib/TypeScript/MLIRGenImpl.h @@ -14050,7 +14050,7 @@ class MLIRGenImpl StringRef interfaceVTableNameForObject(mlir_ts::ObjectType objectType, InterfaceInfo::TypePtr newInterfacePtr) { std::stringstream ss; - ss << hash_value(objectType); + ss << MLIRHelper::hashOfType(objectType); return concat(newInterfacePtr->fullName, ss.str().c_str(), VTABLE_NAME); }