Skip to content

Commit 055bbb0

Browse files
author
Omar Ahmed
committed
Generate vulkanMemoryModel shader
1 parent c6e20fb commit 055bbb0

39 files changed

+843
-27
lines changed

docs/OpenCLCOnVulkan.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,10 +199,15 @@ shader is as follows:
199199
ID is reported in the descriptor map file, generated via the `-descriptormap`
200200
option.
201201

202-
The shaders produced use the GLSL450 memory model. As such, there is an assumption of
202+
#### Memory Model
203+
204+
The default shaders produced use the GLSL450 memory model. As such, there is an assumption of
203205
no aliasing by default. The compiler does not generate *Aliased* decorations
204206
currently. Users should be aware of this and ensure they are not relying on aliasing.
205207

208+
`vulkan-memory-model` option could be used to produce Vulkan memory model shaders. Memory scopes for loads and stores in this mode will be decided
209+
according to the pointer type.
210+
206211
#### Embedded Reflection Instructions
207212

208213
Clspv embeds reflection information via use of the [NonSemantic.ClspvReflection](http://htmlpreview.github.io/?https://github.com/KhronosGroup/SPIRV-Registry/blob/master/nonsemantic/NonSemantic.ClspvReflection.html)

include/clspv/Option.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,9 @@ Vec3ToVec4SupportClass Vec3ToVec4();
260260
// Returns true if opaque pointers are enabled
261261
bool OpaquePointers();
262262

263+
// Returns true if vulkan memory model is enabled
264+
bool VulkanMemoryModel();
265+
263266
// Returns true if the debug information should be generated
264267
bool DebugInfo();
265268

lib/Option.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -381,6 +381,11 @@ static llvm::cl::opt<bool>
381381
llvm::cl::desc("Use opaque pointers"),
382382
llvm::cl::init(false));
383383

384+
static llvm::cl::opt<bool>
385+
vulkan_memory_model("vulkan-memory-model",
386+
llvm::cl::desc("Use vulkan memory model"),
387+
llvm::cl::init(false));
388+
384389
static llvm::cl::opt<bool>
385390
debug_info("g", llvm::cl::init(false),
386391
llvm::cl::desc("Produce debug information."));
@@ -500,6 +505,8 @@ Vec3ToVec4SupportClass Vec3ToVec4() {
500505

501506
bool OpaquePointers() { return opaque_pointers; }
502507

508+
bool VulkanMemoryModel() { return vulkan_memory_model; }
509+
503510
bool DebugInfo() { return debug_info; }
504511

505512
std::set<FeatureMacro> EnabledFeatureMacros() { return enabled_feature_macros; }

lib/ReplaceOpenCLBuiltinPass.cpp

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3510,9 +3510,21 @@ bool ReplaceOpenCLBuiltinPass::replaceAtomics(Function &F, spv::Op Op) {
35103510

35113511
// We need to map the OpenCL constants to the SPIR-V equivalents.
35123512
const auto ConstantScopeDevice = ConstantInt::get(IntTy, spv::ScopeDevice);
3513-
const auto ConstantMemorySemantics = ConstantInt::get(
3514-
IntTy, spv::MemorySemanticsUniformMemoryMask |
3515-
spv::MemorySemanticsSequentiallyConsistentMask);
3513+
auto EqualMemorySemanticsMask =
3514+
clspv::Option::VulkanMemoryModel()
3515+
? spv::MemorySemanticsAcquireReleaseMask
3516+
: spv::MemorySemanticsSequentiallyConsistentMask;
3517+
auto UnEqualMemorySemanticsMask =
3518+
clspv::Option::VulkanMemoryModel()
3519+
? spv::MemorySemanticsAcquireMask
3520+
: spv::MemorySemanticsSequentiallyConsistentMask;
3521+
3522+
const auto EqualConstantMemorySemantics =
3523+
ConstantInt::get(IntTy, spv::MemorySemanticsUniformMemoryMask |
3524+
EqualMemorySemanticsMask);
3525+
const auto UnEqualConstantMemorySemantics =
3526+
ConstantInt::get(IntTy, spv::MemorySemanticsUniformMemoryMask |
3527+
UnEqualMemorySemanticsMask);
35163528

35173529
SmallVector<Value *, 5> Params;
35183530

@@ -3523,11 +3535,11 @@ bool ReplaceOpenCLBuiltinPass::replaceAtomics(Function &F, spv::Op Op) {
35233535
Params.push_back(ConstantScopeDevice);
35243536

35253537
// The memory semantics.
3526-
Params.push_back(ConstantMemorySemantics);
3538+
Params.push_back(EqualConstantMemorySemantics);
35273539

35283540
if (2 < CI->arg_size()) {
35293541
// The unequal memory semantics.
3530-
Params.push_back(ConstantMemorySemantics);
3542+
Params.push_back(UnEqualConstantMemorySemantics);
35313543

35323544
// The value.
35333545
Params.push_back(CI->getArgOperand(2));
@@ -3548,9 +3560,12 @@ bool ReplaceOpenCLBuiltinPass::replaceAtomics(Function &F,
35483560
return replaceCallsWithValue(F, [&](CallInst *CI) {
35493561
auto align = F.getParent()->getDataLayout().getABITypeAlign(
35503562
CI->getArgOperand(1)->getType());
3563+
auto MemorySemantics = clspv::Option::VulkanMemoryModel()
3564+
? AtomicOrdering::AcquireRelease
3565+
: AtomicOrdering::SequentiallyConsistent;
3566+
35513567
return new AtomicRMWInst(Op, CI->getArgOperand(0), CI->getArgOperand(1),
3552-
align, AtomicOrdering::SequentiallyConsistent,
3553-
SyncScope::System, CI);
3568+
align, MemorySemantics, SyncScope::System, CI);
35543569
});
35553570
}
35563571

0 commit comments

Comments
 (0)