@@ -208,10 +208,20 @@ class Builder {
208208
209209 // Maps the given OpType Id to a Non-Semantic DebugType Id.
210210 Id getDebugType (Id type) {
211- if (emitNonSemanticShaderDebugInfo ) {
212- return debugId[type] ;
211+ if (auto it = debugTypeIdLookup. find (type); it != debugTypeIdLookup. end () ) {
212+ return it-> second ;
213213 }
214- return 0 ;
214+
215+ return NoType;
216+ }
217+
218+ // Maps the given OpFunction Id to a Non-Semantic DebugFunction Id.
219+ Id getDebugFunction (Id func) {
220+ if (auto it = debugFuncIdLookup.find (func); it != debugFuncIdLookup.end ()) {
221+ return it->second ;
222+ }
223+
224+ return NoResult;
215225 }
216226
217227 // For creating new types (will return old type if the requested one was already made).
@@ -1031,8 +1041,58 @@ class Builder {
10311041
10321042 // not output, internally used for quick & dirty canonical (unique) creation
10331043
1044+ // Key for scalar constants (handles both 32-bit and 64-bit)
1045+ struct ScalarConstantKey {
1046+ unsigned int typeClass; // OpTypeInt, OpTypeFloat, OpTypeBool
1047+ unsigned int opcode; // OpConstant, OpSpecConstant, OpConstantTrue, etc.
1048+ Id typeId; // The specific type
1049+ unsigned value1; // First operand (or only operand)
1050+ unsigned value2; // Second operand (0 for single-operand constants)
1051+
1052+ bool operator ==(const ScalarConstantKey& other) const {
1053+ return typeClass == other.typeClass &&
1054+ opcode == other.opcode &&
1055+ typeId == other.typeId &&
1056+ value1 == other.value1 &&
1057+ value2 == other.value2 ;
1058+ }
1059+ };
1060+
1061+ struct ScalarConstantKeyHash {
1062+ // 64/32 bit mix function from MurmurHash3
1063+ inline std::size_t hash_mix (std::size_t h) const {
1064+ if constexpr (sizeof (std::size_t ) == 8 ) {
1065+ h ^= h >> 33 ;
1066+ h *= UINT64_C (0xff51afd7ed558ccd );
1067+ h ^= h >> 33 ;
1068+ h *= UINT64_C (0xc4ceb9fe1a85ec53 );
1069+ h ^= h >> 33 ;
1070+ return h;
1071+ } else {
1072+ h ^= h >> 16 ;
1073+ h *= UINT32_C (0x85ebca6b );
1074+ h ^= h >> 13 ;
1075+ h *= UINT32_C (0xc2b2ae35 );
1076+ h ^= h >> 16 ;
1077+ return h;
1078+ }
1079+ }
1080+
1081+ // Hash combine from boost
1082+ inline std::size_t hash_combine (std::size_t seed, std::size_t v) const {
1083+ return hash_mix (seed + 0x9e3779b9 + v);
1084+ }
1085+
1086+ std::size_t operator ()(const ScalarConstantKey& k) const {
1087+ size_t hash1 = hash_combine (std::hash<unsigned >{}(k.typeClass ), std::hash<unsigned >{}(k.opcode ));
1088+ size_t hash2 = hash_combine (std::hash<Id>{}(k.value1 ), std::hash<unsigned >{}(k.value2 ));
1089+ size_t hash3 = hash_combine (hash1, hash2);
1090+ return hash_combine (hash3, std::hash<unsigned >{}(k.typeId ));
1091+ }
1092+ };
1093+
10341094 // map type opcodes to constant inst.
1035- std::unordered_map<unsigned int , std::vector<Instruction*>> groupedConstants ;
1095+ std::unordered_map<unsigned int , std::vector<Instruction*>> groupedCompositeConstants ;
10361096 // map struct-id to constant instructions
10371097 std::unordered_map<unsigned int , std::vector<Instruction*>> groupedStructConstants;
10381098 // map type opcodes to type instructions
@@ -1041,6 +1101,8 @@ class Builder {
10411101 std::unordered_map<unsigned int , std::vector<Instruction*>> groupedDebugTypes;
10421102 // list of OpConstantNull instructions
10431103 std::vector<Instruction*> nullConstants;
1104+ // map scalar constants to result IDs
1105+ std::unordered_map<ScalarConstantKey, Id, ScalarConstantKeyHash> groupedScalarConstantResultIDs;
10441106
10451107 // Track which types have explicit layouts, to avoid reusing in storage classes without layout.
10461108 // Currently only tracks array types.
@@ -1058,8 +1120,11 @@ class Builder {
10581120 // map from include file name ids to their contents
10591121 std::map<spv::Id, const std::string*> includeFiles;
10601122
1061- // map from core id to debug id
1062- std::map <spv::Id, spv::Id> debugId;
1123+ // maps from OpTypeXXX id to DebugTypeXXX id
1124+ std::unordered_map<spv::Id, spv::Id> debugTypeIdLookup;
1125+
1126+ // maps from OpFunction id to DebugFunction id
1127+ std::unordered_map<spv::Id, spv::Id> debugFuncIdLookup;
10631128
10641129 // map from file name string id to DebugSource id
10651130 std::unordered_map<spv::Id, spv::Id> debugSourceId;
0 commit comments