[SPIRV] Fix regression from e56b580d40a4f - minimal fix for constant expressions
Fixes regression introduced by commit e56b580d40a4f "Reapply '[SPIRV]
Emit intrinsics for globals only in function that references them'".
Root cause:
- When globals are used in constant expressions (e.g., ptrtoint in another
global's initializer), those expressions become OpSpecConstantOp during
SPIRV lowering
- OpSpecConstantOp instructions need the global definitions to be available
- The original optimization only emitted globals in functions that directly
reference them, missing the OpSpecConstantOp case
Minimal fix:
1. Fixed iterator initialization bug in collectGlobalUsers (line 76)
- Changed: SmallVector<const Value *> Stack = {begin, end}; (WRONG)
- To: SmallVector<const Value *> Stack(begin, end); (CORRECT)
2. Track which globals are used by other globals (constant expressions)
- Added isUsedByGlobalsWithNoFunctionUsers() method
- Emit such globals in the first function (where module-level OpSpecConstantOp
is processed)
- Regular globals used by function-referenced globals: emit in those functions
This preserves the optimization for regular globals (only emit where used)
while fixing the regression for:
- Metadata globals (e.g., @__TsanDeviceGlobalMetadata with ptrtoint)
- Globals in function-referenced constant expressions (e.g., @p_var with GEP)
Test cases:
- sycl/test-e2e/ThreadSanitizer/check_device_global.cpp
- test/CodeGen/SPIRV/const-nested-vecs.ll
Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>