Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit e4491f3

Browse files
committedJan 17, 2025
Validate atomic operations after address space specialization
Address space specialization for SPIR-V is not done as part of `linkAndOptimizeIR`, as it is for e.g. Metal, so opt out and add a separate call for SPIR-V.
1 parent fa1dd94 commit e4491f3

File tree

3 files changed

+13
-1
lines changed

3 files changed

+13
-1
lines changed
 

‎source/slang/slang-emit.cpp

+3-1
Original file line numberDiff line numberDiff line change
@@ -1322,7 +1322,9 @@ Result linkAndOptimizeIR(
13221322
byteAddressBufferOptions);
13231323
}
13241324

1325-
validateAtomicOperations(sink, irModule->getModuleInst());
1325+
// For SPIR-V, this function is called elsewhere, so that it can happen after address space specialization
1326+
if (target != CodeGenTarget::SPIRV && target != CodeGenTarget::SPIRVAssembly)
1327+
validateAtomicOperations(sink, irModule->getModuleInst());
13261328

13271329
// For CUDA targets only, we will need to turn operations
13281330
// the implicitly reference the "active mask" into ones

‎source/slang/slang-ir-spirv-legalize.cpp

+3
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include "slang-ir-simplify-cfg.h"
2323
#include "slang-ir-specialize-address-space.h"
2424
#include "slang-ir-util.h"
25+
#include "slang-ir-validate.h"
2526
#include "slang-ir.h"
2627
#include "slang-legalize-types.h"
2728

@@ -1931,6 +1932,8 @@ struct SPIRVLegalizationContext : public SourceEmitterBase
19311932
// Specalize address space for all pointers.
19321933
SpirvAddressSpaceAssigner addressSpaceAssigner;
19331934
specializeAddressSpace(m_module, &addressSpaceAssigner);
1935+
1936+
validateAtomicOperations(m_sink, m_module->getModuleInst());
19341937
}
19351938

19361939
void updateFunctionTypes()

‎source/slang/slang-ir-validate.h

+7
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,13 @@ void validateIRModuleIfEnabled(CodeGenContext* codeGenContext, IRModule* module)
3838
void disableIRValidationAtInsert();
3939
void enableIRValidationAtInsert();
4040

41+
// Validate that the destination of an atomic operation is appropriate, meaning it's
42+
// either 'groupshared' or in a device buffer.
43+
// Note that validation of atomic operations should be done after address space
44+
// specialization for targets (e.g. SPIR-V and Metal) which support this kind of use-case:
45+
// void atomicOp(inout int array){ InterlockedAdd(array, 1);}
46+
// groupshared int gArray;
47+
// [numthreads(1, 1, 1)] void main() { atomicOp(gArray); }
4148
void validateAtomicOperations(DiagnosticSink* sink, IRInst* inst);
4249

4350
} // namespace Slang

0 commit comments

Comments
 (0)
Please sign in to comment.